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 | |
| 27 | } // namespace |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 28 | |
| 29 | class 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 | |
| 43 | TEST_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 Takabayashi | 2d72804 | 2018-12-10 09:19:00 +0900 | [diff] [blame] | 52 | TEST_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 Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 61 | TEST_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 Takabayashi | f6a3680 | 2018-08-14 16:23:05 +0900 | [diff] [blame] | 74 | TEST_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 Takabayashi | 9a58752 | 2018-10-29 09:36:27 +0900 | [diff] [blame] | 97 | TEST_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 Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 120 | TEST_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 | |
| 162 | TEST_F(CrashCommonUtilTest, GetCachedKeyValueDefault) { |
| 163 | std::string value; |
| 164 | EXPECT_FALSE( |
| 165 | GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value)); |
| 166 | |
Jeffrey Kardatzke | 8ed15d1 | 2019-03-21 16:57:20 -0700 | [diff] [blame^] | 167 | // kEtcDirectory is the second candidate directory. |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 168 | ASSERT_TRUE(test_util::CreateFile( |
Jeffrey Kardatzke | 8ed15d1 | 2019-03-21 16:57:20 -0700 | [diff] [blame^] | 169 | paths::GetAt(paths::kEtcDirectory, "test.txt"), "FOO=2\n")); |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 170 | 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 Kardatzke | 8ed15d1 | 2019-03-21 16:57:20 -0700 | [diff] [blame^] | 176 | paths::GetAt(paths::kCrashReporterStateDirectory, "test.txt"), |
| 177 | "FOO=1\n")); |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 178 | EXPECT_TRUE( |
| 179 | GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value)); |
| 180 | EXPECT_EQ("1", value); |
| 181 | } |
| 182 | |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 183 | TEST_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 Takabayashi | b09f705 | 2018-10-01 15:26:29 +0900 | [diff] [blame] | 197 | 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 Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 201 | } |
| 202 | |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 203 | } // namespace util |