blob: 3d3a11965461cdcbaaa6690561421c941f53c05a [file] [log] [blame]
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +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/util.h"
6
7#include <base/files/file_util.h>
8#include <base/files/scoped_temp_dir.h>
9#include <gtest/gtest.h>
10
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090011#include <memory>
12
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +090013#include "crash-reporter/crash_sender_paths.h"
14#include "crash-reporter/paths.h"
15#include "crash-reporter/test_util.h"
16
17namespace util {
Satoru Takabayashib2ca40d2018-08-09 14:02:04 +090018namespace {
19
20const char kLsbReleaseContents[] =
21 "CHROMEOS_RELEASE_BOARD=bob\n"
22 "CHROMEOS_RELEASE_NAME=Chromium OS\n"
23 "CHROMEOS_RELEASE_VERSION=10964.0.2018_08_13_1405\n";
24
25} // namespace
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +090026
27class CrashCommonUtilTest : public testing::Test {
28 private:
29 void SetUp() override {
30 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
31 test_dir_ = scoped_temp_dir_.GetPath();
32 paths::SetPrefixForTesting(test_dir_);
33 }
34
35 void TearDown() override { paths::SetPrefixForTesting(base::FilePath()); }
36
37 base::FilePath test_dir_;
38 base::ScopedTempDir scoped_temp_dir_;
39};
40
41TEST_F(CrashCommonUtilTest, IsCrashTestInProgress) {
42 EXPECT_FALSE(IsCrashTestInProgress());
43 ASSERT_TRUE(
44 test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory,
45 paths::kCrashTestInProgress),
46 ""));
47 EXPECT_TRUE(IsCrashTestInProgress());
48}
49
50TEST_F(CrashCommonUtilTest, IsDeveloperImage) {
51 EXPECT_FALSE(IsDeveloperImage());
52
53 ASSERT_TRUE(test_util::CreateFile(paths::Get(paths::kLeaveCoreFile), ""));
54 EXPECT_TRUE(IsDeveloperImage());
55
56 ASSERT_TRUE(
57 test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory,
58 paths::kCrashTestInProgress),
59 ""));
60 EXPECT_FALSE(IsDeveloperImage());
61}
62
Satoru Takabayashif6a36802018-08-14 16:23:05 +090063TEST_F(CrashCommonUtilTest, IsTestImage) {
64 EXPECT_FALSE(IsTestImage());
65
66 // Should return false because the channel is stable.
67 ASSERT_TRUE(test_util::CreateFile(
68 paths::GetAt(paths::kEtcDirectory, paths::kLsbRelease),
69 "CHROMEOS_RELEASE_TRACK=stable-channel"));
70 EXPECT_FALSE(IsTestImage());
71
72 // Should return true because the channel is testimage.
73 ASSERT_TRUE(test_util::CreateFile(
74 paths::GetAt(paths::kEtcDirectory, paths::kLsbRelease),
75 "CHROMEOS_RELEASE_TRACK=testimage-channel"));
76 EXPECT_TRUE(IsTestImage());
77
78 // Should return false if kCrashTestInProgress is present.
79 ASSERT_TRUE(
80 test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory,
81 paths::kCrashTestInProgress),
82 ""));
83 EXPECT_FALSE(IsTestImage());
84}
85
Satoru Takabayashib2ca40d2018-08-09 14:02:04 +090086TEST_F(CrashCommonUtilTest, GetCachedKeyValue) {
87 ASSERT_TRUE(test_util::CreateFile(paths::Get("/etc/lsb-release"),
88 kLsbReleaseContents));
89 ASSERT_TRUE(test_util::CreateFile(paths::Get("/empty/lsb-release"), ""));
90
91 std::string value;
92 // No directories are specified.
93 EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"),
94 "CHROMEOS_RELEASE_VERSION", {}, &value));
95 // A non-existent directory is specified.
96 EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"),
97 "CHROMEOS_RELEASE_VERSION",
98 {paths::Get("/non-existent")}, &value));
99
100 // A non-existent base name is specified.
101 EXPECT_FALSE(GetCachedKeyValue(base::FilePath("non-existent"),
102 "CHROMEOS_RELEASE_VERSION",
103 {paths::Get("/etc")}, &value));
104
105 // A wrong key is specified.
106 EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"), "WRONG_KEY",
107 {paths::Get("/etc")}, &value));
108
109 // This should succeed.
110 EXPECT_TRUE(GetCachedKeyValue(base::FilePath("lsb-release"),
111 "CHROMEOS_RELEASE_VERSION",
112 {paths::Get("/etc")}, &value));
113 EXPECT_EQ("10964.0.2018_08_13_1405", value);
114
115 // A non-existent directory is included, but this should still succeed.
116 EXPECT_TRUE(GetCachedKeyValue(
117 base::FilePath("lsb-release"), "CHROMEOS_RELEASE_VERSION",
118 {paths::Get("/non-existent"), paths::Get("/etc")}, &value));
119 EXPECT_EQ("10964.0.2018_08_13_1405", value);
120
121 // A empty file is included, but this should still succeed.
122 EXPECT_TRUE(GetCachedKeyValue(
123 base::FilePath("lsb-release"), "CHROMEOS_RELEASE_VERSION",
124 {paths::Get("/empty"), paths::Get("/etc")}, &value));
125 EXPECT_EQ("10964.0.2018_08_13_1405", value);
126}
127
128TEST_F(CrashCommonUtilTest, GetCachedKeyValueDefault) {
129 std::string value;
130 EXPECT_FALSE(
131 GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value));
132
133 // kEtcDirectory is the third candidate directory.
134 ASSERT_TRUE(test_util::CreateFile(
135 paths::GetAt(paths::kEtcDirectory, "test.txt"), "FOO=3\n"));
136 EXPECT_TRUE(
137 GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value));
138 EXPECT_EQ("3", value);
139
140 // kSystemCrashDirectory is the second candidate directory.
141 ASSERT_TRUE(test_util::CreateFile(
142 paths::GetAt(paths::kSystemCrashDirectory, "test.txt"), "FOO=2\n"));
143 EXPECT_TRUE(
144 GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value));
145 EXPECT_EQ("2", value);
146
147 // kCrashReporterStateDirectory is the first candidate directory.
148 ASSERT_TRUE(test_util::CreateFile(
149 paths::GetAt(paths::kSystemCrashDirectory, "test.txt"), "FOO=1\n"));
150 EXPECT_TRUE(
151 GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value));
152 EXPECT_EQ("1", value);
153}
154
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +0900155TEST_F(CrashCommonUtilTest, GetUserCrashDirectories) {
156 auto mock =
157 std::make_unique<org::chromium::SessionManagerInterfaceProxyMock>();
158
159 std::vector<base::FilePath> directories;
160
161 test_util::SetActiveSessions(mock.get(), {});
162 EXPECT_TRUE(GetUserCrashDirectories(mock.get(), &directories));
163 EXPECT_TRUE(directories.empty());
164
165 test_util::SetActiveSessions(mock.get(),
166 {{"user1", "hash1"}, {"user2", "hash2"}});
167 EXPECT_TRUE(GetUserCrashDirectories(mock.get(), &directories));
168 EXPECT_EQ(2, directories.size());
Satoru Takabayashib09f7052018-10-01 15:26:29 +0900169 EXPECT_EQ(paths::Get("/home/user/hash1/crash").value(),
170 directories[0].value());
171 EXPECT_EQ(paths::Get("/home/user/hash2/crash").value(),
172 directories[1].value());
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +0900173}
174
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +0900175} // namespace util