Satoru Takabayashi | 5ebb4ce | 2018-08-16 10:28:13 +0900 | [diff] [blame] | 1 | // 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 Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 8 | #include <map> |
| 9 | #include <string> |
| 10 | |
Satoru Takabayashi | 5ebb4ce | 2018-08-16 10:28:13 +0900 | [diff] [blame] | 11 | #include <base/files/file_path.h> |
| 12 | #include <base/strings/string_piece.h> |
Ian Barkley-Yeung | a43ffc2 | 2019-10-09 11:05:21 -0700 | [diff] [blame] | 13 | #include <base/time/clock.h> |
Ian Barkley-Yeung | c377b09 | 2019-10-09 19:23:53 -0700 | [diff] [blame] | 14 | #include <base/time/time.h> |
Satoru Takabayashi | 5ebb4ce | 2018-08-16 10:28:13 +0900 | [diff] [blame] | 15 | |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 16 | #include <session_manager/dbus-proxy-mocks.h> |
| 17 | |
Satoru Takabayashi | 5ebb4ce | 2018-08-16 10:28:13 +0900 | [diff] [blame] | 18 | namespace test_util { |
| 19 | |
Ian Barkley-Yeung | 3f821eb | 2020-08-12 12:05:00 -0700 | [diff] [blame^] | 20 | // A Clock that advances 10 seconds (by default) on each call, used in tests and |
| 21 | // fuzzers. Unlike a MockClock, it will not fail the test regardless of how many |
| 22 | // times it is or isn't called, and it always eventually reaches any desired |
| 23 | // time. In particular, having an advancing clock in the crash sender code is |
| 24 | // useful because if AcquireLockFileOrDie can't get the lock, the test will |
| 25 | // eventually fail instead of going into an infinite loop. |
Ian Barkley-Yeung | a43ffc2 | 2019-10-09 11:05:21 -0700 | [diff] [blame] | 26 | class AdvancingClock : public base::Clock { |
| 27 | public: |
| 28 | // Start clock at GetDefaultTime() |
| 29 | AdvancingClock(); |
Ian Barkley-Yeung | 3f821eb | 2020-08-12 12:05:00 -0700 | [diff] [blame^] | 30 | // Start clock at GetDefaultTime(). Each call to Now() will advance the |
| 31 | // clock by |advance_amount|. |
| 32 | explicit AdvancingClock(base::TimeDelta advance_amount); |
Ian Barkley-Yeung | a43ffc2 | 2019-10-09 11:05:21 -0700 | [diff] [blame] | 33 | |
hscham | 5d4970c | 2019-12-09 14:28:51 +0900 | [diff] [blame] | 34 | base::Time Now() const override; |
Ian Barkley-Yeung | a43ffc2 | 2019-10-09 11:05:21 -0700 | [diff] [blame] | 35 | |
| 36 | private: |
hscham | 5d4970c | 2019-12-09 14:28:51 +0900 | [diff] [blame] | 37 | mutable base::Time time_; |
Ian Barkley-Yeung | 3f821eb | 2020-08-12 12:05:00 -0700 | [diff] [blame^] | 38 | const base::TimeDelta advance_amount_; |
Ian Barkley-Yeung | a43ffc2 | 2019-10-09 11:05:21 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
Ian Barkley-Yeung | c377b09 | 2019-10-09 19:23:53 -0700 | [diff] [blame] | 41 | // Get an assumed "now" for things that mocks out the current time. Always |
| 42 | // returns 2018-04-20 13:53. |
| 43 | base::Time GetDefaultTime(); |
| 44 | |
Satoru Takabayashi | 5ebb4ce | 2018-08-16 10:28:13 +0900 | [diff] [blame] | 45 | // Creates a file at |file_path| with |content|, with parent directories. |
| 46 | // Returns true on success. If you want the test function to stop when the file |
| 47 | // creation failed, wrap this function with ASSERT_TRUE(). |
| 48 | bool CreateFile(const base::FilePath& file_path, base::StringPiece content); |
| 49 | |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 50 | // Configures |mock| so that RetrieveActiveSessions() returns |sessions|. |
| 51 | void SetActiveSessions(org::chromium::SessionManagerInterfaceProxyMock* mock, |
| 52 | const std::map<std::string, std::string>& sessions); |
| 53 | |
Tim Zheng | 2d09b95 | 2019-03-08 16:20:59 -0800 | [diff] [blame] | 54 | // Returns true if at least one file in this directory matches the pattern. |
| 55 | // found_file_path is not assigned if found_file_path is nullptr. |
| 56 | // Only the first found path is stored into found_file_path. |
| 57 | bool DirectoryHasFileWithPattern(const base::FilePath& directory, |
| 58 | const std::string& pattern, |
| 59 | base::FilePath* found_file_path); |
| 60 | |
Chris Morin | 92419a7 | 2019-06-19 12:35:22 -0700 | [diff] [blame] | 61 | // Return path to an input files used by unit tests. |
| 62 | base::FilePath GetTestDataPath(const std::string& name); |
| 63 | |
Satoru Takabayashi | 5ebb4ce | 2018-08-16 10:28:13 +0900 | [diff] [blame] | 64 | } // namespace test_util |
| 65 | |
| 66 | #endif // CRASH_REPORTER_TEST_UTIL_H_ |