blob: 60d91e5c3a413e6bb1a828b8fde1faacacaeb658 [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>
Miriam Zimmermanc57b2b32020-10-28 15:59:07 -070010#include <vector>
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090011
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090012#include <base/files/file_path.h>
13#include <base/strings/string_piece.h>
Ian Barkley-Yeunga43ffc22019-10-09 11:05:21 -070014#include <base/time/clock.h>
Ian Barkley-Yeungc377b092019-10-09 19:23:53 -070015#include <base/time/time.h>
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090016
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090017#include <session_manager/dbus-proxy-mocks.h>
18
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090019namespace test_util {
20
Miriam Zimmermanc57b2b32020-10-28 15:59:07 -070021constexpr char kFakeClientId[] = "00112233445566778899aabbccddeeff";
22
Ian Barkley-Yeung3f821eb2020-08-12 12:05:00 -070023// A Clock that advances 10 seconds (by default) on each call, used in tests and
24// fuzzers. Unlike a MockClock, it will not fail the test regardless of how many
25// times it is or isn't called, and it always eventually reaches any desired
26// time. In particular, having an advancing clock in the crash sender code is
27// useful because if AcquireLockFileOrDie can't get the lock, the test will
28// eventually fail instead of going into an infinite loop.
Ian Barkley-Yeunga43ffc22019-10-09 11:05:21 -070029class AdvancingClock : public base::Clock {
30 public:
31 // Start clock at GetDefaultTime()
32 AdvancingClock();
Ian Barkley-Yeung3f821eb2020-08-12 12:05:00 -070033 // Start clock at GetDefaultTime(). Each call to Now() will advance the
34 // clock by |advance_amount|.
35 explicit AdvancingClock(base::TimeDelta advance_amount);
Ian Barkley-Yeunga43ffc22019-10-09 11:05:21 -070036
hscham5d4970c2019-12-09 14:28:51 +090037 base::Time Now() const override;
Ian Barkley-Yeunga43ffc22019-10-09 11:05:21 -070038
39 private:
hscham5d4970c2019-12-09 14:28:51 +090040 mutable base::Time time_;
Ian Barkley-Yeung3f821eb2020-08-12 12:05:00 -070041 const base::TimeDelta advance_amount_;
Ian Barkley-Yeunga43ffc22019-10-09 11:05:21 -070042};
43
Miriam Zimmermanc57b2b32020-10-28 15:59:07 -070044void FakeSleep(std::vector<base::TimeDelta>* sleep_times,
45 base::TimeDelta duration);
46
47bool CreateClientIdFile();
48
Ian Barkley-Yeungc377b092019-10-09 19:23:53 -070049// Get an assumed "now" for things that mocks out the current time. Always
50// returns 2018-04-20 13:53.
51base::Time GetDefaultTime();
52
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090053// Creates a file at |file_path| with |content|, with parent directories.
54// Returns true on success. If you want the test function to stop when the file
55// creation failed, wrap this function with ASSERT_TRUE().
56bool CreateFile(const base::FilePath& file_path, base::StringPiece content);
57
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090058// Configures |mock| so that RetrieveActiveSessions() returns |sessions|.
59void SetActiveSessions(org::chromium::SessionManagerInterfaceProxyMock* mock,
60 const std::map<std::string, std::string>& sessions);
61
Tim Zheng2d09b952019-03-08 16:20:59 -080062// Returns true if at least one file in this directory matches the pattern.
63// found_file_path is not assigned if found_file_path is nullptr.
64// Only the first found path is stored into found_file_path.
65bool DirectoryHasFileWithPattern(const base::FilePath& directory,
66 const std::string& pattern,
67 base::FilePath* found_file_path);
68
Chris Morin92419a72019-06-19 12:35:22 -070069// Return path to an input files used by unit tests.
70base::FilePath GetTestDataPath(const std::string& name);
71
Miriam Zimmerman8004f012020-10-01 16:32:23 -070072// Helper function for calling base::TouchFile() concisely for tests.
73bool TouchFileHelper(const base::FilePath& file_name, base::Time modified_time);
74
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090075} // namespace test_util
76
77#endif // CRASH_REPORTER_TEST_UTIL_H_