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 | #include "crash-reporter/test_util.h" |
| 6 | |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 7 | #include <base/check.h> |
Tim Zheng | 2d09b95 | 2019-03-08 16:20:59 -0800 | [diff] [blame] | 8 | #include <base/files/file_enumerator.h> |
Satoru Takabayashi | 5ebb4ce | 2018-08-16 10:28:13 +0900 | [diff] [blame] | 9 | #include <base/files/file_util.h> |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 10 | #include <gtest/gtest.h> |
| 11 | |
Miriam Zimmerman | c57b2b3 | 2020-10-28 15:59:07 -0700 | [diff] [blame] | 12 | #include "crash-reporter/crash_sender_paths.h" |
| 13 | #include "crash-reporter/paths.h" |
| 14 | |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 15 | using testing::_; |
Tom Hughes | 3b234ac | 2020-08-24 18:14:58 -0700 | [diff] [blame] | 16 | using testing::Invoke; |
Satoru Takabayashi | 5ebb4ce | 2018-08-16 10:28:13 +0900 | [diff] [blame] | 17 | |
| 18 | namespace test_util { |
| 19 | |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | std::map<std::string, std::string>* g_active_sessions; |
| 23 | |
| 24 | // Implementation of |
| 25 | // SessionManagerInterfaceProxyMock::RetrieveActiveSessions(). |
| 26 | bool 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-Yeung | 3f821eb | 2020-08-12 12:05:00 -0700 | [diff] [blame] | 37 | AdvancingClock::AdvancingClock() |
| 38 | : time_(GetDefaultTime()), |
| 39 | advance_amount_(base::TimeDelta::FromSeconds(10)) {} |
| 40 | |
| 41 | AdvancingClock::AdvancingClock(base::TimeDelta advance_amount) |
| 42 | : time_(GetDefaultTime()), advance_amount_(advance_amount) {} |
Ian Barkley-Yeung | a43ffc2 | 2019-10-09 11:05:21 -0700 | [diff] [blame] | 43 | |
hscham | 5d4970c | 2019-12-09 14:28:51 +0900 | [diff] [blame] | 44 | base::Time AdvancingClock::Now() const { |
Ian Barkley-Yeung | 3f821eb | 2020-08-12 12:05:00 -0700 | [diff] [blame] | 45 | time_ += advance_amount_; |
Ian Barkley-Yeung | a43ffc2 | 2019-10-09 11:05:21 -0700 | [diff] [blame] | 46 | return time_; |
| 47 | } |
| 48 | |
Miriam Zimmerman | c57b2b3 | 2020-10-28 15:59:07 -0700 | [diff] [blame] | 49 | // Fake sleep function that records the requested sleep time. |
| 50 | void 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. |
| 56 | bool CreateClientIdFile() { |
| 57 | return test_util::CreateFile( |
| 58 | paths::GetAt(paths::kCrashSenderStateDirectory, paths::kClientId), |
| 59 | kFakeClientId); |
| 60 | } |
| 61 | |
Ian Barkley-Yeung | c377b09 | 2019-10-09 19:23:53 -0700 | [diff] [blame] | 62 | base::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 Takabayashi | 5ebb4ce | 2018-08-16 10:28:13 +0900 | [diff] [blame] | 73 | bool 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 Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 80 | void 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 Zheng | 2d09b95 | 2019-03-08 16:20:59 -0800 | [diff] [blame] | 90 | bool 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 Morin | 92419a7 | 2019-06-19 12:35:22 -0700 | [diff] [blame] | 101 | base::FilePath GetTestDataPath(const std::string& name) { |
| 102 | return base::FilePath(getenv("SRC")).Append(name); |
| 103 | } |
| 104 | |
Miriam Zimmerman | 8004f01 | 2020-10-01 16:32:23 -0700 | [diff] [blame] | 105 | bool TouchFileHelper(const base::FilePath& file_name, |
| 106 | base::Time modified_time) { |
| 107 | return base::TouchFile(file_name, modified_time, modified_time); |
| 108 | } |
| 109 | |
Satoru Takabayashi | 5ebb4ce | 2018-08-16 10:28:13 +0900 | [diff] [blame] | 110 | } // namespace test_util |