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 | |
Tim Zheng | 2d09b95 | 2019-03-08 16:20:59 -0800 | [diff] [blame] | 7 | #include <base/files/file_enumerator.h> |
Satoru Takabayashi | 5ebb4ce | 2018-08-16 10:28:13 +0900 | [diff] [blame] | 8 | #include <base/files/file_util.h> |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | using testing::Invoke; |
| 12 | using testing::_; |
Satoru Takabayashi | 5ebb4ce | 2018-08-16 10:28:13 +0900 | [diff] [blame] | 13 | |
| 14 | namespace test_util { |
| 15 | |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | std::map<std::string, std::string>* g_active_sessions; |
| 19 | |
| 20 | // Implementation of |
| 21 | // SessionManagerInterfaceProxyMock::RetrieveActiveSessions(). |
| 22 | bool RetrieveActiveSessionsImpl( |
| 23 | std::map<std::string, std::string>* out_sessions, |
| 24 | brillo::ErrorPtr* error, |
| 25 | int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) { |
| 26 | DCHECK(g_active_sessions); // Set in SetActiveSessions(). |
| 27 | *out_sessions = *g_active_sessions; |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | } // namespace |
| 32 | |
Ian Barkley-Yeung | a43ffc2 | 2019-10-09 11:05:21 -0700 | [diff] [blame^] | 33 | AdvancingClock::AdvancingClock() : time_(GetDefaultTime()) {} |
| 34 | |
| 35 | base::Time AdvancingClock::Now() { |
| 36 | time_ += base::TimeDelta::FromSeconds(10); |
| 37 | return time_; |
| 38 | } |
| 39 | |
Ian Barkley-Yeung | c377b09 | 2019-10-09 19:23:53 -0700 | [diff] [blame] | 40 | base::Time GetDefaultTime() { |
| 41 | base::Time time; |
| 42 | // Date is basically arbitrary, but far enough back that |
| 43 | // IsOsTimestampTooOldForUploads (the function with the longest duration in |
| 44 | // it) would return true for this date. This avoids any possibility of unit |
| 45 | // tests suddenly failing if someone is (incorrectly) comparing this to the |
| 46 | // real base::Time::Now(). |
| 47 | CHECK(base::Time::FromUTCString("2018-04-20 13:53", &time)); |
| 48 | return time; |
| 49 | } |
| 50 | |
Satoru Takabayashi | 5ebb4ce | 2018-08-16 10:28:13 +0900 | [diff] [blame] | 51 | bool CreateFile(const base::FilePath& file_path, base::StringPiece content) { |
| 52 | if (!base::CreateDirectory(file_path.DirName())) |
| 53 | return false; |
| 54 | return base::WriteFile(file_path, content.data(), content.size()) == |
| 55 | content.size(); |
| 56 | } |
| 57 | |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 58 | void SetActiveSessions(org::chromium::SessionManagerInterfaceProxyMock* mock, |
| 59 | const std::map<std::string, std::string>& sessions) { |
| 60 | if (g_active_sessions) |
| 61 | delete g_active_sessions; |
| 62 | g_active_sessions = new std::map<std::string, std::string>(sessions); |
| 63 | |
| 64 | EXPECT_CALL(*mock, RetrieveActiveSessions(_, _, _)) |
| 65 | .WillRepeatedly(Invoke(&RetrieveActiveSessionsImpl)); |
| 66 | } |
| 67 | |
Tim Zheng | 2d09b95 | 2019-03-08 16:20:59 -0800 | [diff] [blame] | 68 | bool DirectoryHasFileWithPattern(const base::FilePath& directory, |
| 69 | const std::string& pattern, |
| 70 | base::FilePath* found_file_path) { |
| 71 | base::FileEnumerator enumerator( |
| 72 | directory, false, base::FileEnumerator::FileType::FILES, pattern); |
| 73 | base::FilePath path = enumerator.Next(); |
| 74 | if (!path.empty() && found_file_path) |
| 75 | *found_file_path = path; |
| 76 | return !path.empty(); |
| 77 | } |
| 78 | |
Chris Morin | 92419a7 | 2019-06-19 12:35:22 -0700 | [diff] [blame] | 79 | base::FilePath GetTestDataPath(const std::string& name) { |
| 80 | return base::FilePath(getenv("SRC")).Append(name); |
| 81 | } |
| 82 | |
Satoru Takabayashi | 5ebb4ce | 2018-08-16 10:28:13 +0900 | [diff] [blame] | 83 | } // namespace test_util |