blob: 7cba6d23bac0040d1cfcdaa30d7efa4ec2721092 [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
Tim Zheng2d09b952019-03-08 16:20:59 -08007#include <base/files/file_enumerator.h>
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +09008#include <base/files/file_util.h>
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +09009#include <gtest/gtest.h>
10
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090011using testing::_;
Tom Hughes3b234ac2020-08-24 18:14:58 -070012using testing::Invoke;
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090013
14namespace test_util {
15
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090016namespace {
17
18std::map<std::string, std::string>* g_active_sessions;
19
20// Implementation of
21// SessionManagerInterfaceProxyMock::RetrieveActiveSessions().
22bool 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-Yeung3f821eb2020-08-12 12:05:00 -070033AdvancingClock::AdvancingClock()
34 : time_(GetDefaultTime()),
35 advance_amount_(base::TimeDelta::FromSeconds(10)) {}
36
37AdvancingClock::AdvancingClock(base::TimeDelta advance_amount)
38 : time_(GetDefaultTime()), advance_amount_(advance_amount) {}
Ian Barkley-Yeunga43ffc22019-10-09 11:05:21 -070039
hscham5d4970c2019-12-09 14:28:51 +090040base::Time AdvancingClock::Now() const {
Ian Barkley-Yeung3f821eb2020-08-12 12:05:00 -070041 time_ += advance_amount_;
Ian Barkley-Yeunga43ffc22019-10-09 11:05:21 -070042 return time_;
43}
44
Ian Barkley-Yeungc377b092019-10-09 19:23:53 -070045base::Time GetDefaultTime() {
46 base::Time time;
47 // Date is basically arbitrary, but far enough back that
48 // IsOsTimestampTooOldForUploads (the function with the longest duration in
49 // it) would return true for this date. This avoids any possibility of unit
50 // tests suddenly failing if someone is (incorrectly) comparing this to the
51 // real base::Time::Now().
52 CHECK(base::Time::FromUTCString("2018-04-20 13:53", &time));
53 return time;
54}
55
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090056bool CreateFile(const base::FilePath& file_path, base::StringPiece content) {
57 if (!base::CreateDirectory(file_path.DirName()))
58 return false;
59 return base::WriteFile(file_path, content.data(), content.size()) ==
60 content.size();
61}
62
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090063void SetActiveSessions(org::chromium::SessionManagerInterfaceProxyMock* mock,
64 const std::map<std::string, std::string>& sessions) {
65 if (g_active_sessions)
66 delete g_active_sessions;
67 g_active_sessions = new std::map<std::string, std::string>(sessions);
68
69 EXPECT_CALL(*mock, RetrieveActiveSessions(_, _, _))
70 .WillRepeatedly(Invoke(&RetrieveActiveSessionsImpl));
71}
72
Tim Zheng2d09b952019-03-08 16:20:59 -080073bool DirectoryHasFileWithPattern(const base::FilePath& directory,
74 const std::string& pattern,
75 base::FilePath* found_file_path) {
76 base::FileEnumerator enumerator(
77 directory, false, base::FileEnumerator::FileType::FILES, pattern);
78 base::FilePath path = enumerator.Next();
79 if (!path.empty() && found_file_path)
80 *found_file_path = path;
81 return !path.empty();
82}
83
Chris Morin92419a72019-06-19 12:35:22 -070084base::FilePath GetTestDataPath(const std::string& name) {
85 return base::FilePath(getenv("SRC")).Append(name);
86}
87
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090088} // namespace test_util