blob: 5c11d8d1a3f34123d24d78224db710978459cfff [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#include "crash-reporter/test_util.h"
6
Qijiang Fan713061e2021-03-08 15:45:12 +09007#include <base/check.h>
Tim Zheng2d09b952019-03-08 16:20:59 -08008#include <base/files/file_enumerator.h>
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +09009#include <base/files/file_util.h>
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090010#include <gtest/gtest.h>
11
Miriam Zimmermanc57b2b32020-10-28 15:59:07 -070012#include "crash-reporter/crash_sender_paths.h"
13#include "crash-reporter/paths.h"
14
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090015using testing::_;
Tom Hughes3b234ac2020-08-24 18:14:58 -070016using testing::Invoke;
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090017
18namespace test_util {
19
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090020namespace {
21
22std::map<std::string, std::string>* g_active_sessions;
23
24// Implementation of
25// SessionManagerInterfaceProxyMock::RetrieveActiveSessions().
26bool RetrieveActiveSessionsImpl(
27 std::map<std::string, std::string>* out_sessions,
28 brillo::ErrorPtr* error,
29 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) {
30 DCHECK(g_active_sessions); // Set in SetActiveSessions().
31 *out_sessions = *g_active_sessions;
32 return true;
33}
34
35} // namespace
36
Ian Barkley-Yeung3f821eb2020-08-12 12:05:00 -070037AdvancingClock::AdvancingClock()
38 : time_(GetDefaultTime()),
39 advance_amount_(base::TimeDelta::FromSeconds(10)) {}
40
41AdvancingClock::AdvancingClock(base::TimeDelta advance_amount)
42 : time_(GetDefaultTime()), advance_amount_(advance_amount) {}
Ian Barkley-Yeunga43ffc22019-10-09 11:05:21 -070043
hscham5d4970c2019-12-09 14:28:51 +090044base::Time AdvancingClock::Now() const {
Ian Barkley-Yeung3f821eb2020-08-12 12:05:00 -070045 time_ += advance_amount_;
Ian Barkley-Yeunga43ffc22019-10-09 11:05:21 -070046 return time_;
47}
48
Miriam Zimmermanc57b2b32020-10-28 15:59:07 -070049// Fake sleep function that records the requested sleep time.
50void FakeSleep(std::vector<base::TimeDelta>* sleep_times,
51 base::TimeDelta duration) {
52 sleep_times->push_back(duration);
53}
54
55// Creates the client ID file and stores the fake client ID in it.
56bool CreateClientIdFile() {
57 return test_util::CreateFile(
58 paths::GetAt(paths::kCrashSenderStateDirectory, paths::kClientId),
59 kFakeClientId);
60}
61
Ian Barkley-Yeungc377b092019-10-09 19:23:53 -070062base::Time GetDefaultTime() {
63 base::Time time;
64 // Date is basically arbitrary, but far enough back that
65 // IsOsTimestampTooOldForUploads (the function with the longest duration in
66 // it) would return true for this date. This avoids any possibility of unit
67 // tests suddenly failing if someone is (incorrectly) comparing this to the
68 // real base::Time::Now().
69 CHECK(base::Time::FromUTCString("2018-04-20 13:53", &time));
70 return time;
71}
72
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090073bool CreateFile(const base::FilePath& file_path, base::StringPiece content) {
74 if (!base::CreateDirectory(file_path.DirName()))
75 return false;
76 return base::WriteFile(file_path, content.data(), content.size()) ==
77 content.size();
78}
79
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090080void SetActiveSessions(org::chromium::SessionManagerInterfaceProxyMock* mock,
81 const std::map<std::string, std::string>& sessions) {
82 if (g_active_sessions)
83 delete g_active_sessions;
84 g_active_sessions = new std::map<std::string, std::string>(sessions);
85
86 EXPECT_CALL(*mock, RetrieveActiveSessions(_, _, _))
87 .WillRepeatedly(Invoke(&RetrieveActiveSessionsImpl));
88}
89
Tim Zheng2d09b952019-03-08 16:20:59 -080090bool DirectoryHasFileWithPattern(const base::FilePath& directory,
91 const std::string& pattern,
92 base::FilePath* found_file_path) {
93 base::FileEnumerator enumerator(
94 directory, false, base::FileEnumerator::FileType::FILES, pattern);
95 base::FilePath path = enumerator.Next();
96 if (!path.empty() && found_file_path)
97 *found_file_path = path;
98 return !path.empty();
99}
100
Chris Morin92419a72019-06-19 12:35:22 -0700101base::FilePath GetTestDataPath(const std::string& name) {
102 return base::FilePath(getenv("SRC")).Append(name);
103}
104
Miriam Zimmerman8004f012020-10-01 16:32:23 -0700105bool TouchFileHelper(const base::FilePath& file_name,
106 base::Time modified_time) {
107 return base::TouchFile(file_name, modified_time, modified_time);
108}
109
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +0900110} // namespace test_util