blob: e90f5b1b75bc081aaa81172ba6cf9aaaf1514e91 [file] [log] [blame]
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +09001// Copyright 2018 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CRASH_REPORTER_TEST_UTIL_H_
6#define CRASH_REPORTER_TEST_UTIL_H_
7
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +09008#include <map>
9#include <string>
10
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090011#include <base/files/file_path.h>
12#include <base/strings/string_piece.h>
Ian Barkley-Yeunga43ffc22019-10-09 11:05:21 -070013#include <base/time/clock.h>
Ian Barkley-Yeungc377b092019-10-09 19:23:53 -070014#include <base/time/time.h>
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090015
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090016#include <session_manager/dbus-proxy-mocks.h>
17
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090018namespace test_util {
19
Ian Barkley-Yeunga43ffc22019-10-09 11:05:21 -070020// A Clock that advances 10 seconds on each call, used in tests and fuzzers.
21// Unlike a MockClock, it will not fail the test regardless of how many times it
22// is or isn't called, and it always eventually reaches the desired time. In
23// particular, having an advancing clock in the crash sender code is useful
24// because if AcquireLockFileOrDie can't get the lock, the test will eventually
25// fail instead of going into an infinite loop.
26class AdvancingClock : public base::Clock {
27 public:
28 // Start clock at GetDefaultTime()
29 AdvancingClock();
30
hscham5d4970c2019-12-09 14:28:51 +090031 base::Time Now() const override;
Ian Barkley-Yeunga43ffc22019-10-09 11:05:21 -070032
33 private:
hscham5d4970c2019-12-09 14:28:51 +090034 mutable base::Time time_;
Ian Barkley-Yeunga43ffc22019-10-09 11:05:21 -070035};
36
Ian Barkley-Yeungc377b092019-10-09 19:23:53 -070037// Get an assumed "now" for things that mocks out the current time. Always
38// returns 2018-04-20 13:53.
39base::Time GetDefaultTime();
40
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090041// Creates a file at |file_path| with |content|, with parent directories.
42// Returns true on success. If you want the test function to stop when the file
43// creation failed, wrap this function with ASSERT_TRUE().
44bool CreateFile(const base::FilePath& file_path, base::StringPiece content);
45
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090046// Configures |mock| so that RetrieveActiveSessions() returns |sessions|.
47void SetActiveSessions(org::chromium::SessionManagerInterfaceProxyMock* mock,
48 const std::map<std::string, std::string>& sessions);
49
Tim Zheng2d09b952019-03-08 16:20:59 -080050// Returns true if at least one file in this directory matches the pattern.
51// found_file_path is not assigned if found_file_path is nullptr.
52// Only the first found path is stored into found_file_path.
53bool DirectoryHasFileWithPattern(const base::FilePath& directory,
54 const std::string& pattern,
55 base::FilePath* found_file_path);
56
Chris Morin92419a72019-06-19 12:35:22 -070057// Return path to an input files used by unit tests.
58base::FilePath GetTestDataPath(const std::string& name);
59
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090060} // namespace test_util
61
62#endif // CRASH_REPORTER_TEST_UTIL_H_