blob: 1611a36e690ee2938cf66f6fb90d904c5b0f9742 [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
11using testing::Invoke;
12using testing::_;
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-Yeunga43ffc22019-10-09 11:05:21 -070033AdvancingClock::AdvancingClock() : time_(GetDefaultTime()) {}
34
35base::Time AdvancingClock::Now() {
36 time_ += base::TimeDelta::FromSeconds(10);
37 return time_;
38}
39
Ian Barkley-Yeungc377b092019-10-09 19:23:53 -070040base::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 Takabayashi5ebb4ce2018-08-16 10:28:13 +090051bool 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 Takabayashi8ce6db82018-08-17 15:18:41 +090058void 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 Zheng2d09b952019-03-08 16:20:59 -080068bool 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 Morin92419a72019-06-19 12:35:22 -070079base::FilePath GetTestDataPath(const std::string& name) {
80 return base::FilePath(getenv("SRC")).Append(name);
81}
82
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090083} // namespace test_util