Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 1 | // 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 Takabayashi | 9a58752 | 2018-10-29 09:36:27 +0900 | [diff] [blame] | 7 | #include <stdlib.h> |
| 8 | |
| 9 | #include <memory> |
| 10 | |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 11 | #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 | |
| 19 | namespace util { |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | const 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 | |
Jeffrey Kardatzke | ea33393 | 2019-04-12 10:17:51 -0700 | [diff] [blame] | 27 | constexpr char kHwClassContents[] = "fake_hwclass"; |
| 28 | |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 29 | } // namespace |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 30 | |
| 31 | class CrashCommonUtilTest : public testing::Test { |
| 32 | private: |
| 33 | void SetUp() override { |
| 34 | ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); |
| 35 | test_dir_ = scoped_temp_dir_.GetPath(); |
| 36 | paths::SetPrefixForTesting(test_dir_); |
| 37 | } |
| 38 | |
| 39 | void TearDown() override { paths::SetPrefixForTesting(base::FilePath()); } |
| 40 | |
| 41 | base::FilePath test_dir_; |
| 42 | base::ScopedTempDir scoped_temp_dir_; |
| 43 | }; |
| 44 | |
| 45 | TEST_F(CrashCommonUtilTest, IsCrashTestInProgress) { |
| 46 | EXPECT_FALSE(IsCrashTestInProgress()); |
| 47 | ASSERT_TRUE( |
| 48 | test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory, |
| 49 | paths::kCrashTestInProgress), |
| 50 | "")); |
| 51 | EXPECT_TRUE(IsCrashTestInProgress()); |
| 52 | } |
| 53 | |
Satoru Takabayashi | 2d72804 | 2018-12-10 09:19:00 +0900 | [diff] [blame] | 54 | TEST_F(CrashCommonUtilTest, IsDeviceCoredumpUploadAllowed) { |
| 55 | EXPECT_FALSE(IsDeviceCoredumpUploadAllowed()); |
| 56 | ASSERT_TRUE( |
| 57 | test_util::CreateFile(paths::GetAt(paths::kCrashReporterStateDirectory, |
| 58 | paths::kDeviceCoredumpUploadAllowed), |
| 59 | "")); |
| 60 | EXPECT_TRUE(IsDeviceCoredumpUploadAllowed()); |
| 61 | } |
| 62 | |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 63 | TEST_F(CrashCommonUtilTest, IsDeveloperImage) { |
| 64 | EXPECT_FALSE(IsDeveloperImage()); |
| 65 | |
| 66 | ASSERT_TRUE(test_util::CreateFile(paths::Get(paths::kLeaveCoreFile), "")); |
| 67 | EXPECT_TRUE(IsDeveloperImage()); |
| 68 | |
| 69 | ASSERT_TRUE( |
| 70 | test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory, |
| 71 | paths::kCrashTestInProgress), |
| 72 | "")); |
| 73 | EXPECT_FALSE(IsDeveloperImage()); |
| 74 | } |
| 75 | |
Satoru Takabayashi | f6a3680 | 2018-08-14 16:23:05 +0900 | [diff] [blame] | 76 | TEST_F(CrashCommonUtilTest, IsTestImage) { |
| 77 | EXPECT_FALSE(IsTestImage()); |
| 78 | |
| 79 | // Should return false because the channel is stable. |
| 80 | ASSERT_TRUE(test_util::CreateFile( |
| 81 | paths::GetAt(paths::kEtcDirectory, paths::kLsbRelease), |
| 82 | "CHROMEOS_RELEASE_TRACK=stable-channel")); |
| 83 | EXPECT_FALSE(IsTestImage()); |
| 84 | |
| 85 | // Should return true because the channel is testimage. |
| 86 | ASSERT_TRUE(test_util::CreateFile( |
| 87 | paths::GetAt(paths::kEtcDirectory, paths::kLsbRelease), |
| 88 | "CHROMEOS_RELEASE_TRACK=testimage-channel")); |
| 89 | EXPECT_TRUE(IsTestImage()); |
| 90 | |
| 91 | // Should return false if kCrashTestInProgress is present. |
| 92 | ASSERT_TRUE( |
| 93 | test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory, |
| 94 | paths::kCrashTestInProgress), |
| 95 | "")); |
| 96 | EXPECT_FALSE(IsTestImage()); |
| 97 | } |
| 98 | |
Satoru Takabayashi | 9a58752 | 2018-10-29 09:36:27 +0900 | [diff] [blame] | 99 | TEST_F(CrashCommonUtilTest, IsOfficialImage) { |
| 100 | EXPECT_FALSE(IsOfficialImage()); |
| 101 | |
Satoru Takabayashi | 9a58752 | 2018-10-29 09:36:27 +0900 | [diff] [blame] | 102 | // Check if lsb-release is handled correctly. |
| 103 | ASSERT_TRUE(test_util::CreateFile( |
| 104 | paths::Get("/etc/lsb-release"), |
| 105 | "CHROMEOS_RELEASE_DESCRIPTION=10964.0 (Test Build) developer-build")); |
| 106 | EXPECT_FALSE(IsOfficialImage()); |
| 107 | |
| 108 | ASSERT_TRUE(test_util::CreateFile( |
| 109 | paths::Get("/etc/lsb-release"), |
| 110 | "CHROMEOS_RELEASE_DESCRIPTION=10964.0 (Official Build) canary-channel")); |
| 111 | EXPECT_TRUE(IsOfficialImage()); |
| 112 | } |
| 113 | |
Jeffrey Kardatzke | ea33393 | 2019-04-12 10:17:51 -0700 | [diff] [blame] | 114 | TEST_F(CrashCommonUtilTest, GetHardwareClass) { |
| 115 | EXPECT_EQ("undefined", GetHardwareClass()); |
| 116 | |
| 117 | ASSERT_TRUE(test_util::CreateFile( |
| 118 | paths::Get("/sys/devices/platform/chromeos_acpi/HWID"), |
| 119 | kHwClassContents)); |
| 120 | EXPECT_EQ(kHwClassContents, GetHardwareClass()); |
| 121 | } |
| 122 | |
| 123 | TEST_F(CrashCommonUtilTest, GetBootModeString) { |
| 124 | EXPECT_EQ("missing-crossystem", GetBootModeString()); |
| 125 | |
Jeffrey Kardatzke | ea33393 | 2019-04-12 10:17:51 -0700 | [diff] [blame] | 126 | ASSERT_TRUE( |
| 127 | test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory, |
| 128 | paths::kCrashTestInProgress), |
| 129 | "")); |
| 130 | EXPECT_EQ("", GetBootModeString()); |
| 131 | } |
| 132 | |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 133 | TEST_F(CrashCommonUtilTest, GetCachedKeyValue) { |
| 134 | ASSERT_TRUE(test_util::CreateFile(paths::Get("/etc/lsb-release"), |
| 135 | kLsbReleaseContents)); |
| 136 | ASSERT_TRUE(test_util::CreateFile(paths::Get("/empty/lsb-release"), "")); |
| 137 | |
| 138 | std::string value; |
| 139 | // No directories are specified. |
| 140 | EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"), |
| 141 | "CHROMEOS_RELEASE_VERSION", {}, &value)); |
| 142 | // A non-existent directory is specified. |
| 143 | EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"), |
| 144 | "CHROMEOS_RELEASE_VERSION", |
| 145 | {paths::Get("/non-existent")}, &value)); |
| 146 | |
| 147 | // A non-existent base name is specified. |
| 148 | EXPECT_FALSE(GetCachedKeyValue(base::FilePath("non-existent"), |
| 149 | "CHROMEOS_RELEASE_VERSION", |
| 150 | {paths::Get("/etc")}, &value)); |
| 151 | |
| 152 | // A wrong key is specified. |
| 153 | EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"), "WRONG_KEY", |
| 154 | {paths::Get("/etc")}, &value)); |
| 155 | |
| 156 | // This should succeed. |
| 157 | EXPECT_TRUE(GetCachedKeyValue(base::FilePath("lsb-release"), |
| 158 | "CHROMEOS_RELEASE_VERSION", |
| 159 | {paths::Get("/etc")}, &value)); |
| 160 | EXPECT_EQ("10964.0.2018_08_13_1405", value); |
| 161 | |
| 162 | // A non-existent directory is included, but this should still succeed. |
| 163 | EXPECT_TRUE(GetCachedKeyValue( |
| 164 | base::FilePath("lsb-release"), "CHROMEOS_RELEASE_VERSION", |
| 165 | {paths::Get("/non-existent"), paths::Get("/etc")}, &value)); |
| 166 | EXPECT_EQ("10964.0.2018_08_13_1405", value); |
| 167 | |
| 168 | // A empty file is included, but this should still succeed. |
| 169 | EXPECT_TRUE(GetCachedKeyValue( |
| 170 | base::FilePath("lsb-release"), "CHROMEOS_RELEASE_VERSION", |
| 171 | {paths::Get("/empty"), paths::Get("/etc")}, &value)); |
| 172 | EXPECT_EQ("10964.0.2018_08_13_1405", value); |
| 173 | } |
| 174 | |
| 175 | TEST_F(CrashCommonUtilTest, GetCachedKeyValueDefault) { |
| 176 | std::string value; |
| 177 | EXPECT_FALSE( |
| 178 | GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value)); |
| 179 | |
Jeffrey Kardatzke | 8ed15d1 | 2019-03-21 16:57:20 -0700 | [diff] [blame] | 180 | // kEtcDirectory is the second candidate directory. |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 181 | ASSERT_TRUE(test_util::CreateFile( |
Jeffrey Kardatzke | 8ed15d1 | 2019-03-21 16:57:20 -0700 | [diff] [blame] | 182 | paths::GetAt(paths::kEtcDirectory, "test.txt"), "FOO=2\n")); |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 183 | EXPECT_TRUE( |
| 184 | GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value)); |
| 185 | EXPECT_EQ("2", value); |
| 186 | |
| 187 | // kCrashReporterStateDirectory is the first candidate directory. |
| 188 | ASSERT_TRUE(test_util::CreateFile( |
Jeffrey Kardatzke | 8ed15d1 | 2019-03-21 16:57:20 -0700 | [diff] [blame] | 189 | paths::GetAt(paths::kCrashReporterStateDirectory, "test.txt"), |
| 190 | "FOO=1\n")); |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 191 | EXPECT_TRUE( |
| 192 | GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value)); |
| 193 | EXPECT_EQ("1", value); |
| 194 | } |
| 195 | |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 196 | TEST_F(CrashCommonUtilTest, GetUserCrashDirectories) { |
| 197 | auto mock = |
| 198 | std::make_unique<org::chromium::SessionManagerInterfaceProxyMock>(); |
| 199 | |
| 200 | std::vector<base::FilePath> directories; |
| 201 | |
| 202 | test_util::SetActiveSessions(mock.get(), {}); |
| 203 | EXPECT_TRUE(GetUserCrashDirectories(mock.get(), &directories)); |
| 204 | EXPECT_TRUE(directories.empty()); |
| 205 | |
| 206 | test_util::SetActiveSessions(mock.get(), |
| 207 | {{"user1", "hash1"}, {"user2", "hash2"}}); |
| 208 | EXPECT_TRUE(GetUserCrashDirectories(mock.get(), &directories)); |
| 209 | EXPECT_EQ(2, directories.size()); |
Satoru Takabayashi | b09f705 | 2018-10-01 15:26:29 +0900 | [diff] [blame] | 210 | EXPECT_EQ(paths::Get("/home/user/hash1/crash").value(), |
| 211 | directories[0].value()); |
| 212 | EXPECT_EQ(paths::Get("/home/user/hash2/crash").value(), |
| 213 | directories[1].value()); |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 214 | } |
| 215 | |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 216 | } // namespace util |