blob: 82e06a1cbe6a82cf4dd3dc5b1fc5124d84ef14b5 [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
Satoru Takabayashi9a587522018-10-29 09:36:27 +09007#include <stdlib.h>
8
9#include <memory>
10
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +090011#include <base/files/file_util.h>
12#include <base/files/scoped_temp_dir.h>
13#include <gtest/gtest.h>
14
15#include "crash-reporter/crash_sender_paths.h"
16#include "crash-reporter/paths.h"
17#include "crash-reporter/test_util.h"
18
19namespace util {
Satoru Takabayashib2ca40d2018-08-09 14:02:04 +090020namespace {
21
22const char kLsbReleaseContents[] =
23 "CHROMEOS_RELEASE_BOARD=bob\n"
24 "CHROMEOS_RELEASE_NAME=Chromium OS\n"
25 "CHROMEOS_RELEASE_VERSION=10964.0.2018_08_13_1405\n";
26
27} // namespace
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +090028
29class CrashCommonUtilTest : public testing::Test {
30 private:
31 void SetUp() override {
32 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
33 test_dir_ = scoped_temp_dir_.GetPath();
34 paths::SetPrefixForTesting(test_dir_);
35 }
36
37 void TearDown() override { paths::SetPrefixForTesting(base::FilePath()); }
38
39 base::FilePath test_dir_;
40 base::ScopedTempDir scoped_temp_dir_;
41};
42
43TEST_F(CrashCommonUtilTest, IsCrashTestInProgress) {
44 EXPECT_FALSE(IsCrashTestInProgress());
45 ASSERT_TRUE(
46 test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory,
47 paths::kCrashTestInProgress),
48 ""));
49 EXPECT_TRUE(IsCrashTestInProgress());
50}
51
Satoru Takabayashi2d728042018-12-10 09:19:00 +090052TEST_F(CrashCommonUtilTest, IsDeviceCoredumpUploadAllowed) {
53 EXPECT_FALSE(IsDeviceCoredumpUploadAllowed());
54 ASSERT_TRUE(
55 test_util::CreateFile(paths::GetAt(paths::kCrashReporterStateDirectory,
56 paths::kDeviceCoredumpUploadAllowed),
57 ""));
58 EXPECT_TRUE(IsDeviceCoredumpUploadAllowed());
59}
60
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +090061TEST_F(CrashCommonUtilTest, IsDeveloperImage) {
62 EXPECT_FALSE(IsDeveloperImage());
63
64 ASSERT_TRUE(test_util::CreateFile(paths::Get(paths::kLeaveCoreFile), ""));
65 EXPECT_TRUE(IsDeveloperImage());
66
67 ASSERT_TRUE(
68 test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory,
69 paths::kCrashTestInProgress),
70 ""));
71 EXPECT_FALSE(IsDeveloperImage());
72}
73
Satoru Takabayashif6a36802018-08-14 16:23:05 +090074TEST_F(CrashCommonUtilTest, IsTestImage) {
75 EXPECT_FALSE(IsTestImage());
76
77 // Should return false because the channel is stable.
78 ASSERT_TRUE(test_util::CreateFile(
79 paths::GetAt(paths::kEtcDirectory, paths::kLsbRelease),
80 "CHROMEOS_RELEASE_TRACK=stable-channel"));
81 EXPECT_FALSE(IsTestImage());
82
83 // Should return true because the channel is testimage.
84 ASSERT_TRUE(test_util::CreateFile(
85 paths::GetAt(paths::kEtcDirectory, paths::kLsbRelease),
86 "CHROMEOS_RELEASE_TRACK=testimage-channel"));
87 EXPECT_TRUE(IsTestImage());
88
89 // Should return false if kCrashTestInProgress is present.
90 ASSERT_TRUE(
91 test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory,
92 paths::kCrashTestInProgress),
93 ""));
94 EXPECT_FALSE(IsTestImage());
95}
96
Satoru Takabayashi9a587522018-10-29 09:36:27 +090097TEST_F(CrashCommonUtilTest, IsOfficialImage) {
98 EXPECT_FALSE(IsOfficialImage());
99
100 // Check if FORCE_OFFICIAL is handled correctly.
101 setenv("FORCE_OFFICIAL", "0", 1 /* overwrite */);
102 EXPECT_FALSE(IsOfficialImage());
103
104 setenv("FORCE_OFFICIAL", "1", 1 /* overwrite */);
105 EXPECT_TRUE(IsOfficialImage());
106 unsetenv("FORCE_OFFICIAL");
107
108 // Check if lsb-release is handled correctly.
109 ASSERT_TRUE(test_util::CreateFile(
110 paths::Get("/etc/lsb-release"),
111 "CHROMEOS_RELEASE_DESCRIPTION=10964.0 (Test Build) developer-build"));
112 EXPECT_FALSE(IsOfficialImage());
113
114 ASSERT_TRUE(test_util::CreateFile(
115 paths::Get("/etc/lsb-release"),
116 "CHROMEOS_RELEASE_DESCRIPTION=10964.0 (Official Build) canary-channel"));
117 EXPECT_TRUE(IsOfficialImage());
118}
119
Satoru Takabayashib2ca40d2018-08-09 14:02:04 +0900120TEST_F(CrashCommonUtilTest, GetCachedKeyValue) {
121 ASSERT_TRUE(test_util::CreateFile(paths::Get("/etc/lsb-release"),
122 kLsbReleaseContents));
123 ASSERT_TRUE(test_util::CreateFile(paths::Get("/empty/lsb-release"), ""));
124
125 std::string value;
126 // No directories are specified.
127 EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"),
128 "CHROMEOS_RELEASE_VERSION", {}, &value));
129 // A non-existent directory is specified.
130 EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"),
131 "CHROMEOS_RELEASE_VERSION",
132 {paths::Get("/non-existent")}, &value));
133
134 // A non-existent base name is specified.
135 EXPECT_FALSE(GetCachedKeyValue(base::FilePath("non-existent"),
136 "CHROMEOS_RELEASE_VERSION",
137 {paths::Get("/etc")}, &value));
138
139 // A wrong key is specified.
140 EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"), "WRONG_KEY",
141 {paths::Get("/etc")}, &value));
142
143 // This should succeed.
144 EXPECT_TRUE(GetCachedKeyValue(base::FilePath("lsb-release"),
145 "CHROMEOS_RELEASE_VERSION",
146 {paths::Get("/etc")}, &value));
147 EXPECT_EQ("10964.0.2018_08_13_1405", value);
148
149 // A non-existent directory is included, but this should still succeed.
150 EXPECT_TRUE(GetCachedKeyValue(
151 base::FilePath("lsb-release"), "CHROMEOS_RELEASE_VERSION",
152 {paths::Get("/non-existent"), paths::Get("/etc")}, &value));
153 EXPECT_EQ("10964.0.2018_08_13_1405", value);
154
155 // A empty file is included, but this should still succeed.
156 EXPECT_TRUE(GetCachedKeyValue(
157 base::FilePath("lsb-release"), "CHROMEOS_RELEASE_VERSION",
158 {paths::Get("/empty"), paths::Get("/etc")}, &value));
159 EXPECT_EQ("10964.0.2018_08_13_1405", value);
160}
161
162TEST_F(CrashCommonUtilTest, GetCachedKeyValueDefault) {
163 std::string value;
164 EXPECT_FALSE(
165 GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value));
166
Jeffrey Kardatzke8ed15d12019-03-21 16:57:20 -0700167 // kEtcDirectory is the second candidate directory.
Satoru Takabayashib2ca40d2018-08-09 14:02:04 +0900168 ASSERT_TRUE(test_util::CreateFile(
Jeffrey Kardatzke8ed15d12019-03-21 16:57:20 -0700169 paths::GetAt(paths::kEtcDirectory, "test.txt"), "FOO=2\n"));
Satoru Takabayashib2ca40d2018-08-09 14:02:04 +0900170 EXPECT_TRUE(
171 GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value));
172 EXPECT_EQ("2", value);
173
174 // kCrashReporterStateDirectory is the first candidate directory.
175 ASSERT_TRUE(test_util::CreateFile(
Jeffrey Kardatzke8ed15d12019-03-21 16:57:20 -0700176 paths::GetAt(paths::kCrashReporterStateDirectory, "test.txt"),
177 "FOO=1\n"));
Satoru Takabayashib2ca40d2018-08-09 14:02:04 +0900178 EXPECT_TRUE(
179 GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value));
180 EXPECT_EQ("1", value);
181}
182
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +0900183TEST_F(CrashCommonUtilTest, GetUserCrashDirectories) {
184 auto mock =
185 std::make_unique<org::chromium::SessionManagerInterfaceProxyMock>();
186
187 std::vector<base::FilePath> directories;
188
189 test_util::SetActiveSessions(mock.get(), {});
190 EXPECT_TRUE(GetUserCrashDirectories(mock.get(), &directories));
191 EXPECT_TRUE(directories.empty());
192
193 test_util::SetActiveSessions(mock.get(),
194 {{"user1", "hash1"}, {"user2", "hash2"}});
195 EXPECT_TRUE(GetUserCrashDirectories(mock.get(), &directories));
196 EXPECT_EQ(2, directories.size());
Satoru Takabayashib09f7052018-10-01 15:26:29 +0900197 EXPECT_EQ(paths::Get("/home/user/hash1/crash").value(),
198 directories[0].value());
199 EXPECT_EQ(paths::Get("/home/user/hash2/crash").value(),
200 directories[1].value());
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +0900201}
202
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +0900203} // namespace util