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> |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 13 | #include <base/rand_util.h> |
| 14 | #include <brillo/process.h> |
| 15 | #include <brillo/streams/memory_stream.h> |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 16 | #include <gtest/gtest.h> |
| 17 | |
| 18 | #include "crash-reporter/crash_sender_paths.h" |
| 19 | #include "crash-reporter/paths.h" |
| 20 | #include "crash-reporter/test_util.h" |
| 21 | |
| 22 | namespace util { |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 23 | namespace { |
| 24 | |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 25 | constexpr char kLsbReleaseContents[] = |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 26 | "CHROMEOS_RELEASE_BOARD=bob\n" |
| 27 | "CHROMEOS_RELEASE_NAME=Chromium OS\n" |
| 28 | "CHROMEOS_RELEASE_VERSION=10964.0.2018_08_13_1405\n"; |
| 29 | |
Jeffrey Kardatzke | ea33393 | 2019-04-12 10:17:51 -0700 | [diff] [blame] | 30 | constexpr char kHwClassContents[] = "fake_hwclass"; |
| 31 | |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 32 | constexpr char kGzipPath[] = "/bin/gzip"; |
| 33 | |
| 34 | constexpr char kSemiRandomData[] = |
| 35 | "ABJCI239AJSDLKJ;kalkjkjsd98723;KJHASD87;kqw3p088ad;lKJASDP823;KJ"; |
| 36 | constexpr int kRandomDataMinLength = 32768; // 32kB |
| 37 | constexpr int kRandomDataMaxLength = 262144; // 256kB |
| 38 | |
| 39 | // Verifies that |raw_file| corresponds to the gzip'd version of |
| 40 | // |compressed_file| by decompressing it and comparing the contents. Returns |
| 41 | // true if they match, false otherwise. This will overwrite the contents of |
| 42 | // |compressed_file| in the process of doing this. |
| 43 | bool VerifyCompression(const base::FilePath& raw_file, |
| 44 | const base::FilePath& compressed_file) { |
| 45 | if (!base::PathExists(raw_file)) { |
| 46 | LOG(ERROR) << "raw_file doesn't exist for verifying compression: " |
| 47 | << raw_file.value(); |
| 48 | return false; |
| 49 | } |
| 50 | if (!base::PathExists(compressed_file)) { |
| 51 | LOG(ERROR) << "compressed_file doesn't exist for verifying compression: " |
| 52 | << compressed_file.value(); |
| 53 | return false; |
| 54 | } |
| 55 | brillo::ProcessImpl proc; |
| 56 | proc.AddArg(kGzipPath); |
| 57 | proc.AddArg("-d"); // decompress |
| 58 | proc.AddArg(compressed_file.value()); |
| 59 | std::string error; |
| 60 | const int res = util::RunAndCaptureOutput(&proc, STDERR_FILENO, &error); |
| 61 | if (res < 0) { |
| 62 | PLOG(ERROR) << "Failed to execute gzip"; |
| 63 | return false; |
| 64 | } |
| 65 | if (res != 0) { |
| 66 | LOG(ERROR) << "Failed to un-gzip " << compressed_file.value(); |
| 67 | util::LogMultilineError(error); |
| 68 | return false; |
| 69 | } |
| 70 | base::FilePath uncompressed_file = compressed_file.RemoveFinalExtension(); |
| 71 | std::string raw_contents; |
| 72 | std::string uncompressed_contents; |
| 73 | if (!base::ReadFileToString(raw_file, &raw_contents)) { |
| 74 | LOG(ERROR) << "Failed reading in raw_file " << raw_file.value(); |
| 75 | return false; |
| 76 | } |
| 77 | if (!base::ReadFileToString(uncompressed_file, &uncompressed_contents)) { |
| 78 | LOG(ERROR) << "Failed reading in uncompressed_file " |
| 79 | << uncompressed_file.value(); |
| 80 | return false; |
| 81 | } |
| 82 | return raw_contents == uncompressed_contents; |
| 83 | } |
| 84 | |
| 85 | // We use a somewhat random string of ASCII data to better reflect the data we |
| 86 | // would be compressing for real. We also shouldn't use something like |
| 87 | // base::RandBytesAsString() because that will generate uniformly random data |
| 88 | // which does not compress. |
| 89 | std::string CreateSemiRandomString(size_t size) { |
| 90 | std::string result; |
| 91 | result.reserve(size); |
| 92 | while (result.length() < size) { |
| 93 | int rem = size - result.length(); |
| 94 | if (rem > sizeof(kSemiRandomData) - 1) |
| 95 | rem = sizeof(kSemiRandomData) - 1; |
| 96 | int rand_start = base::RandInt(0, rem - 1); |
| 97 | int rand_end = base::RandInt(rand_start + 1, rem); |
| 98 | result.append(&kSemiRandomData[rand_start], rand_end - rand_start); |
| 99 | } |
| 100 | return result; |
| 101 | } |
| 102 | |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 103 | } // namespace |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 104 | |
| 105 | class CrashCommonUtilTest : public testing::Test { |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 106 | protected: |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 107 | void SetUp() override { |
| 108 | ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); |
| 109 | test_dir_ = scoped_temp_dir_.GetPath(); |
| 110 | paths::SetPrefixForTesting(test_dir_); |
| 111 | } |
| 112 | |
| 113 | void TearDown() override { paths::SetPrefixForTesting(base::FilePath()); } |
| 114 | |
| 115 | base::FilePath test_dir_; |
| 116 | base::ScopedTempDir scoped_temp_dir_; |
| 117 | }; |
| 118 | |
| 119 | TEST_F(CrashCommonUtilTest, IsCrashTestInProgress) { |
| 120 | EXPECT_FALSE(IsCrashTestInProgress()); |
| 121 | ASSERT_TRUE( |
| 122 | test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory, |
| 123 | paths::kCrashTestInProgress), |
| 124 | "")); |
| 125 | EXPECT_TRUE(IsCrashTestInProgress()); |
| 126 | } |
| 127 | |
Satoru Takabayashi | 2d72804 | 2018-12-10 09:19:00 +0900 | [diff] [blame] | 128 | TEST_F(CrashCommonUtilTest, IsDeviceCoredumpUploadAllowed) { |
| 129 | EXPECT_FALSE(IsDeviceCoredumpUploadAllowed()); |
| 130 | ASSERT_TRUE( |
| 131 | test_util::CreateFile(paths::GetAt(paths::kCrashReporterStateDirectory, |
| 132 | paths::kDeviceCoredumpUploadAllowed), |
| 133 | "")); |
| 134 | EXPECT_TRUE(IsDeviceCoredumpUploadAllowed()); |
| 135 | } |
| 136 | |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 137 | TEST_F(CrashCommonUtilTest, IsDeveloperImage) { |
| 138 | EXPECT_FALSE(IsDeveloperImage()); |
| 139 | |
| 140 | ASSERT_TRUE(test_util::CreateFile(paths::Get(paths::kLeaveCoreFile), "")); |
| 141 | EXPECT_TRUE(IsDeveloperImage()); |
| 142 | |
| 143 | ASSERT_TRUE( |
| 144 | test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory, |
| 145 | paths::kCrashTestInProgress), |
| 146 | "")); |
| 147 | EXPECT_FALSE(IsDeveloperImage()); |
| 148 | } |
| 149 | |
Satoru Takabayashi | f6a3680 | 2018-08-14 16:23:05 +0900 | [diff] [blame] | 150 | TEST_F(CrashCommonUtilTest, IsTestImage) { |
| 151 | EXPECT_FALSE(IsTestImage()); |
| 152 | |
| 153 | // Should return false because the channel is stable. |
| 154 | ASSERT_TRUE(test_util::CreateFile( |
| 155 | paths::GetAt(paths::kEtcDirectory, paths::kLsbRelease), |
| 156 | "CHROMEOS_RELEASE_TRACK=stable-channel")); |
| 157 | EXPECT_FALSE(IsTestImage()); |
| 158 | |
| 159 | // Should return true because the channel is testimage. |
| 160 | ASSERT_TRUE(test_util::CreateFile( |
| 161 | paths::GetAt(paths::kEtcDirectory, paths::kLsbRelease), |
| 162 | "CHROMEOS_RELEASE_TRACK=testimage-channel")); |
| 163 | EXPECT_TRUE(IsTestImage()); |
| 164 | |
| 165 | // Should return false if kCrashTestInProgress is present. |
| 166 | ASSERT_TRUE( |
| 167 | test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory, |
| 168 | paths::kCrashTestInProgress), |
| 169 | "")); |
| 170 | EXPECT_FALSE(IsTestImage()); |
| 171 | } |
| 172 | |
Satoru Takabayashi | 9a58752 | 2018-10-29 09:36:27 +0900 | [diff] [blame] | 173 | TEST_F(CrashCommonUtilTest, IsOfficialImage) { |
| 174 | EXPECT_FALSE(IsOfficialImage()); |
| 175 | |
Satoru Takabayashi | 9a58752 | 2018-10-29 09:36:27 +0900 | [diff] [blame] | 176 | // Check if lsb-release is handled correctly. |
| 177 | ASSERT_TRUE(test_util::CreateFile( |
| 178 | paths::Get("/etc/lsb-release"), |
| 179 | "CHROMEOS_RELEASE_DESCRIPTION=10964.0 (Test Build) developer-build")); |
| 180 | EXPECT_FALSE(IsOfficialImage()); |
| 181 | |
| 182 | ASSERT_TRUE(test_util::CreateFile( |
| 183 | paths::Get("/etc/lsb-release"), |
| 184 | "CHROMEOS_RELEASE_DESCRIPTION=10964.0 (Official Build) canary-channel")); |
| 185 | EXPECT_TRUE(IsOfficialImage()); |
| 186 | } |
| 187 | |
Jeffrey Kardatzke | e3fb8fb | 2019-05-13 13:59:12 -0700 | [diff] [blame^] | 188 | TEST_F(CrashCommonUtilTest, GetOsTimestamp) { |
| 189 | // If we can't read /etc/lsb-release then we should be returning the null |
| 190 | // time. |
| 191 | EXPECT_TRUE(util::GetOsTimestamp().is_null()); |
| 192 | |
| 193 | base::FilePath lsb_file_path = paths::Get("/etc/lsb-release"); |
| 194 | ASSERT_TRUE(test_util::CreateFile(lsb_file_path, "foo=bar")); |
| 195 | base::Time old_time = base::Time::Now() - base::TimeDelta::FromDays(366); |
| 196 | ASSERT_TRUE(base::TouchFile(lsb_file_path, old_time, old_time)); |
| 197 | // ext2/ext3 seem to have a timestamp granularity of 1s. |
| 198 | EXPECT_EQ(util::GetOsTimestamp().ToTimeVal().tv_sec, |
| 199 | old_time.ToTimeVal().tv_sec); |
| 200 | } |
| 201 | |
| 202 | TEST_F(CrashCommonUtilTest, IsOsTimestampTooOldForUploads) { |
| 203 | EXPECT_FALSE(util::IsOsTimestampTooOldForUploads(base::Time())); |
| 204 | EXPECT_FALSE(util::IsOsTimestampTooOldForUploads( |
| 205 | base::Time::Now() - base::TimeDelta::FromDays(179))); |
| 206 | EXPECT_TRUE(util::IsOsTimestampTooOldForUploads( |
| 207 | base::Time::Now() - base::TimeDelta::FromDays(181))); |
| 208 | } |
| 209 | |
Jeffrey Kardatzke | ea33393 | 2019-04-12 10:17:51 -0700 | [diff] [blame] | 210 | TEST_F(CrashCommonUtilTest, GetHardwareClass) { |
| 211 | EXPECT_EQ("undefined", GetHardwareClass()); |
| 212 | |
| 213 | ASSERT_TRUE(test_util::CreateFile( |
| 214 | paths::Get("/sys/devices/platform/chromeos_acpi/HWID"), |
| 215 | kHwClassContents)); |
| 216 | EXPECT_EQ(kHwClassContents, GetHardwareClass()); |
| 217 | } |
| 218 | |
| 219 | TEST_F(CrashCommonUtilTest, GetBootModeString) { |
| 220 | EXPECT_EQ("missing-crossystem", GetBootModeString()); |
| 221 | |
Jeffrey Kardatzke | ea33393 | 2019-04-12 10:17:51 -0700 | [diff] [blame] | 222 | ASSERT_TRUE( |
| 223 | test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory, |
| 224 | paths::kCrashTestInProgress), |
| 225 | "")); |
| 226 | EXPECT_EQ("", GetBootModeString()); |
| 227 | } |
| 228 | |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 229 | TEST_F(CrashCommonUtilTest, GetCachedKeyValue) { |
| 230 | ASSERT_TRUE(test_util::CreateFile(paths::Get("/etc/lsb-release"), |
| 231 | kLsbReleaseContents)); |
| 232 | ASSERT_TRUE(test_util::CreateFile(paths::Get("/empty/lsb-release"), "")); |
| 233 | |
| 234 | std::string value; |
| 235 | // No directories are specified. |
| 236 | EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"), |
| 237 | "CHROMEOS_RELEASE_VERSION", {}, &value)); |
| 238 | // A non-existent directory is specified. |
| 239 | EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"), |
| 240 | "CHROMEOS_RELEASE_VERSION", |
| 241 | {paths::Get("/non-existent")}, &value)); |
| 242 | |
| 243 | // A non-existent base name is specified. |
| 244 | EXPECT_FALSE(GetCachedKeyValue(base::FilePath("non-existent"), |
| 245 | "CHROMEOS_RELEASE_VERSION", |
| 246 | {paths::Get("/etc")}, &value)); |
| 247 | |
| 248 | // A wrong key is specified. |
| 249 | EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"), "WRONG_KEY", |
| 250 | {paths::Get("/etc")}, &value)); |
| 251 | |
| 252 | // This should succeed. |
| 253 | EXPECT_TRUE(GetCachedKeyValue(base::FilePath("lsb-release"), |
| 254 | "CHROMEOS_RELEASE_VERSION", |
| 255 | {paths::Get("/etc")}, &value)); |
| 256 | EXPECT_EQ("10964.0.2018_08_13_1405", value); |
| 257 | |
| 258 | // A non-existent directory is included, but this should still succeed. |
| 259 | EXPECT_TRUE(GetCachedKeyValue( |
| 260 | base::FilePath("lsb-release"), "CHROMEOS_RELEASE_VERSION", |
| 261 | {paths::Get("/non-existent"), paths::Get("/etc")}, &value)); |
| 262 | EXPECT_EQ("10964.0.2018_08_13_1405", value); |
| 263 | |
| 264 | // A empty file is included, but this should still succeed. |
| 265 | EXPECT_TRUE(GetCachedKeyValue( |
| 266 | base::FilePath("lsb-release"), "CHROMEOS_RELEASE_VERSION", |
| 267 | {paths::Get("/empty"), paths::Get("/etc")}, &value)); |
| 268 | EXPECT_EQ("10964.0.2018_08_13_1405", value); |
| 269 | } |
| 270 | |
| 271 | TEST_F(CrashCommonUtilTest, GetCachedKeyValueDefault) { |
| 272 | std::string value; |
| 273 | EXPECT_FALSE( |
| 274 | GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value)); |
| 275 | |
Jeffrey Kardatzke | 8ed15d1 | 2019-03-21 16:57:20 -0700 | [diff] [blame] | 276 | // kEtcDirectory is the second candidate directory. |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 277 | ASSERT_TRUE(test_util::CreateFile( |
Jeffrey Kardatzke | 8ed15d1 | 2019-03-21 16:57:20 -0700 | [diff] [blame] | 278 | paths::GetAt(paths::kEtcDirectory, "test.txt"), "FOO=2\n")); |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 279 | EXPECT_TRUE( |
| 280 | GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value)); |
| 281 | EXPECT_EQ("2", value); |
| 282 | |
| 283 | // kCrashReporterStateDirectory is the first candidate directory. |
| 284 | ASSERT_TRUE(test_util::CreateFile( |
Jeffrey Kardatzke | 8ed15d1 | 2019-03-21 16:57:20 -0700 | [diff] [blame] | 285 | paths::GetAt(paths::kCrashReporterStateDirectory, "test.txt"), |
| 286 | "FOO=1\n")); |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 287 | EXPECT_TRUE( |
| 288 | GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value)); |
| 289 | EXPECT_EQ("1", value); |
| 290 | } |
| 291 | |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 292 | TEST_F(CrashCommonUtilTest, GetUserCrashDirectories) { |
| 293 | auto mock = |
| 294 | std::make_unique<org::chromium::SessionManagerInterfaceProxyMock>(); |
| 295 | |
| 296 | std::vector<base::FilePath> directories; |
| 297 | |
| 298 | test_util::SetActiveSessions(mock.get(), {}); |
| 299 | EXPECT_TRUE(GetUserCrashDirectories(mock.get(), &directories)); |
| 300 | EXPECT_TRUE(directories.empty()); |
| 301 | |
| 302 | test_util::SetActiveSessions(mock.get(), |
| 303 | {{"user1", "hash1"}, {"user2", "hash2"}}); |
| 304 | EXPECT_TRUE(GetUserCrashDirectories(mock.get(), &directories)); |
| 305 | EXPECT_EQ(2, directories.size()); |
Satoru Takabayashi | b09f705 | 2018-10-01 15:26:29 +0900 | [diff] [blame] | 306 | EXPECT_EQ(paths::Get("/home/user/hash1/crash").value(), |
| 307 | directories[0].value()); |
| 308 | EXPECT_EQ(paths::Get("/home/user/hash2/crash").value(), |
| 309 | directories[1].value()); |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 310 | } |
| 311 | |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 312 | TEST_F(CrashCommonUtilTest, GzipFile) { |
| 313 | // Create a temp file of semi-structured ASCII data then compress it using our |
| 314 | // function and then decompress it and see if we have the same data. Don't use |
| 315 | // random data because random data doesn't compress well. :) |
| 316 | base::FilePath file; |
| 317 | ASSERT_TRUE(base::CreateTemporaryFileInDir(test_dir_, &file)); |
| 318 | base::FilePath file_copy; |
| 319 | ASSERT_TRUE(base::CreateTemporaryFileInDir(test_dir_, &file_copy)); |
| 320 | std::string content = CreateSemiRandomString( |
| 321 | base::RandInt(kRandomDataMinLength, kRandomDataMaxLength)); |
| 322 | ASSERT_EQ(base::WriteFile(file, content.c_str(), content.length()), |
| 323 | content.length()); |
| 324 | ASSERT_TRUE(base::CopyFile(file, file_copy)); |
| 325 | base::FilePath zip_file = util::GzipFile(file); |
| 326 | EXPECT_EQ(zip_file, file.AddExtension(".gz")); |
| 327 | EXPECT_TRUE(VerifyCompression(file_copy, zip_file)) |
| 328 | << "Random input data: " << content; |
| 329 | } |
| 330 | |
| 331 | TEST_F(CrashCommonUtilTest, GzipStream) { |
| 332 | std::string content = CreateSemiRandomString( |
| 333 | base::RandInt(kRandomDataMinLength, kRandomDataMaxLength)); |
| 334 | std::string compressed_content = |
| 335 | util::GzipStream(brillo::MemoryStream::OpenCopyOf( |
| 336 | content.c_str(), content.length(), nullptr)); |
| 337 | EXPECT_FALSE(compressed_content.empty()); |
| 338 | base::FilePath raw_file; |
| 339 | ASSERT_TRUE(base::CreateTemporaryFileInDir(test_dir_, &raw_file)); |
| 340 | base::FilePath compressed_file; |
| 341 | ASSERT_TRUE(base::CreateTemporaryFileInDir(test_dir_, &compressed_file)); |
| 342 | // Remove the file we will decompress to or gzip will fail on decompression. |
| 343 | ASSERT_TRUE(base::DeleteFile(compressed_file, false)); |
| 344 | compressed_file = compressed_file.AddExtension(".gz"); |
| 345 | ASSERT_EQ(base::WriteFile(raw_file, content.c_str(), content.length()), |
| 346 | content.length()); |
| 347 | ASSERT_EQ(base::WriteFile(compressed_file, compressed_content.c_str(), |
| 348 | compressed_content.length()), |
| 349 | compressed_content.length()); |
| 350 | EXPECT_TRUE(VerifyCompression(raw_file, compressed_file)) |
| 351 | << "Random input data: " << content; |
| 352 | } |
| 353 | |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 354 | } // namespace util |