blob: 44b71127c6624c8e61dab7d644ee5a5cd70ecdfb [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-Yeung3f821eb2020-08-12 12:05:00 -070020// 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-Yeunga43ffc22019-10-09 11:05:21 -070026class AdvancingClock : public base::Clock {
27 public:
28 // Start clock at GetDefaultTime()
29 AdvancingClock();
Ian Barkley-Yeung3f821eb2020-08-12 12:05:00 -070030 // 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-Yeunga43ffc22019-10-09 11:05:21 -070033
hscham5d4970c2019-12-09 14:28:51 +090034 base::Time Now() const override;
Ian Barkley-Yeunga43ffc22019-10-09 11:05:21 -070035
36 private:
hscham5d4970c2019-12-09 14:28:51 +090037 mutable base::Time time_;
Ian Barkley-Yeung3f821eb2020-08-12 12:05:00 -070038 const base::TimeDelta advance_amount_;
Ian Barkley-Yeunga43ffc22019-10-09 11:05:21 -070039};
40
Ian Barkley-Yeungc377b092019-10-09 19:23:53 -070041// Get an assumed "now" for things that mocks out the current time. Always
42// returns 2018-04-20 13:53.
43base::Time GetDefaultTime();
44
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090045// 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().
48bool CreateFile(const base::FilePath& file_path, base::StringPiece content);
49
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090050// Configures |mock| so that RetrieveActiveSessions() returns |sessions|.
51void SetActiveSessions(org::chromium::SessionManagerInterfaceProxyMock* mock,
52 const std::map<std::string, std::string>& sessions);
53
Tim Zheng2d09b952019-03-08 16:20:59 -080054// 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.
57bool DirectoryHasFileWithPattern(const base::FilePath& directory,
58 const std::string& pattern,
59 base::FilePath* found_file_path);
60
Chris Morin92419a72019-06-19 12:35:22 -070061// Return path to an input files used by unit tests.
62base::FilePath GetTestDataPath(const std::string& name);
63
Miriam Zimmerman8004f012020-10-01 16:32:23 -070064// Helper function for calling base::TouchFile() concisely for tests.
65bool TouchFileHelper(const base::FilePath& file_name, base::Time modified_time);
66
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090067} // namespace test_util
68
69#endif // CRASH_REPORTER_TEST_UTIL_H_