blob: 18e0c24db803efb37c0ef0db64d7b2a0d26dddb1 [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-Yeungc377b092019-10-09 19:23:53 -070033base::Time GetDefaultTime() {
34 base::Time time;
35 // Date is basically arbitrary, but far enough back that
36 // IsOsTimestampTooOldForUploads (the function with the longest duration in
37 // it) would return true for this date. This avoids any possibility of unit
38 // tests suddenly failing if someone is (incorrectly) comparing this to the
39 // real base::Time::Now().
40 CHECK(base::Time::FromUTCString("2018-04-20 13:53", &time));
41 return time;
42}
43
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090044bool CreateFile(const base::FilePath& file_path, base::StringPiece content) {
45 if (!base::CreateDirectory(file_path.DirName()))
46 return false;
47 return base::WriteFile(file_path, content.data(), content.size()) ==
48 content.size();
49}
50
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090051void SetActiveSessions(org::chromium::SessionManagerInterfaceProxyMock* mock,
52 const std::map<std::string, std::string>& sessions) {
53 if (g_active_sessions)
54 delete g_active_sessions;
55 g_active_sessions = new std::map<std::string, std::string>(sessions);
56
57 EXPECT_CALL(*mock, RetrieveActiveSessions(_, _, _))
58 .WillRepeatedly(Invoke(&RetrieveActiveSessionsImpl));
59}
60
Tim Zheng2d09b952019-03-08 16:20:59 -080061bool DirectoryHasFileWithPattern(const base::FilePath& directory,
62 const std::string& pattern,
63 base::FilePath* found_file_path) {
64 base::FileEnumerator enumerator(
65 directory, false, base::FileEnumerator::FileType::FILES, pattern);
66 base::FilePath path = enumerator.Next();
67 if (!path.empty() && found_file_path)
68 *found_file_path = path;
69 return !path.empty();
70}
71
Chris Morin92419a72019-06-19 12:35:22 -070072base::FilePath GetTestDataPath(const std::string& name) {
73 return base::FilePath(getenv("SRC")).Append(name);
74}
75
Satoru Takabayashi5ebb4ce2018-08-16 10:28:13 +090076} // namespace test_util