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 | |
Kansho Nishida | 630cc7a | 2019-10-23 17:37:41 +0900 | [diff] [blame] | 9 | #include <fcntl.h> |
Miriam Zimmerman | 3604286 | 2019-11-14 20:01:29 -0800 | [diff] [blame] | 10 | #include <limits> |
Satoru Takabayashi | 9a58752 | 2018-10-29 09:36:27 +0900 | [diff] [blame] | 11 | #include <memory> |
Tim Zheng | 11a665e | 2019-06-26 17:44:01 -0700 | [diff] [blame] | 12 | #include <sys/mman.h> |
Satoru Takabayashi | 9a58752 | 2018-10-29 09:36:27 +0900 | [diff] [blame] | 13 | |
Miriam Zimmerman | a23f18f | 2020-11-12 11:09:27 -0800 | [diff] [blame] | 14 | #include <base/command_line.h> |
Chloe Pelling | 83dda4c | 2021-02-17 17:01:21 +1100 | [diff] [blame] | 15 | #include <base/files/file_path.h> |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 16 | #include <base/files/file_util.h> |
| 17 | #include <base/files/scoped_temp_dir.h> |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 18 | #include <base/rand_util.h> |
Ian Barkley-Yeung | c377b09 | 2019-10-09 19:23:53 -0700 | [diff] [blame] | 19 | #include <base/test/simple_test_clock.h> |
Kansho Nishida | 630cc7a | 2019-10-23 17:37:41 +0900 | [diff] [blame] | 20 | #include <base/time/time.h> |
Simon Glass | 2b1da09 | 2020-05-21 12:24:16 -0600 | [diff] [blame] | 21 | #include <brillo/process/process.h> |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 22 | #include <brillo/streams/memory_stream.h> |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 23 | #include <gtest/gtest.h> |
| 24 | |
| 25 | #include "crash-reporter/crash_sender_paths.h" |
| 26 | #include "crash-reporter/paths.h" |
| 27 | #include "crash-reporter/test_util.h" |
Miriam Zimmerman | a23f18f | 2020-11-12 11:09:27 -0800 | [diff] [blame] | 28 | #include "metrics/metrics_library_mock.h" |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 29 | |
Tim Zheng | 11a665e | 2019-06-26 17:44:01 -0700 | [diff] [blame] | 30 | // The QEMU emulator we use to run unit tests on simulated ARM boards does not |
| 31 | // support memfd_create. (https://bugs.launchpad.net/qemu/+bug/1734792) Skip |
| 32 | // tests that rely on memfd_create on ARM boards. |
| 33 | #if defined(ARCH_CPU_ARM_FAMILY) |
| 34 | #define DISABLED_ON_QEMU_FOR_MEMFD_CREATE(test_name) DISABLED_##test_name |
| 35 | #else |
| 36 | #define DISABLED_ON_QEMU_FOR_MEMFD_CREATE(test_name) test_name |
| 37 | #endif |
| 38 | |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 39 | namespace util { |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 40 | namespace { |
| 41 | |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 42 | constexpr char kLsbReleaseContents[] = |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 43 | "CHROMEOS_RELEASE_BOARD=bob\n" |
| 44 | "CHROMEOS_RELEASE_NAME=Chromium OS\n" |
| 45 | "CHROMEOS_RELEASE_VERSION=10964.0.2018_08_13_1405\n"; |
| 46 | |
Jeffrey Kardatzke | ea33393 | 2019-04-12 10:17:51 -0700 | [diff] [blame] | 47 | constexpr char kHwClassContents[] = "fake_hwclass"; |
| 48 | |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 49 | constexpr char kGzipPath[] = "/bin/gzip"; |
| 50 | |
| 51 | constexpr char kSemiRandomData[] = |
| 52 | "ABJCI239AJSDLKJ;kalkjkjsd98723;KJHASD87;kqw3p088ad;lKJASDP823;KJ"; |
| 53 | constexpr int kRandomDataMinLength = 32768; // 32kB |
| 54 | constexpr int kRandomDataMaxLength = 262144; // 256kB |
| 55 | |
Kansho Nishida | 630cc7a | 2019-10-23 17:37:41 +0900 | [diff] [blame] | 56 | constexpr char kReadFdToStreamContents[] = "1234567890"; |
| 57 | |
Chloe Pelling | 83dda4c | 2021-02-17 17:01:21 +1100 | [diff] [blame] | 58 | constexpr char kLdArgv0[] = "LD_ARGV0"; |
| 59 | |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 60 | // Verifies that |raw_file| corresponds to the gzip'd version of |
| 61 | // |compressed_file| by decompressing it and comparing the contents. Returns |
| 62 | // true if they match, false otherwise. This will overwrite the contents of |
| 63 | // |compressed_file| in the process of doing this. |
| 64 | bool VerifyCompression(const base::FilePath& raw_file, |
| 65 | const base::FilePath& compressed_file) { |
| 66 | if (!base::PathExists(raw_file)) { |
| 67 | LOG(ERROR) << "raw_file doesn't exist for verifying compression: " |
| 68 | << raw_file.value(); |
| 69 | return false; |
| 70 | } |
| 71 | if (!base::PathExists(compressed_file)) { |
| 72 | LOG(ERROR) << "compressed_file doesn't exist for verifying compression: " |
| 73 | << compressed_file.value(); |
| 74 | return false; |
| 75 | } |
| 76 | brillo::ProcessImpl proc; |
| 77 | proc.AddArg(kGzipPath); |
| 78 | proc.AddArg("-d"); // decompress |
| 79 | proc.AddArg(compressed_file.value()); |
| 80 | std::string error; |
| 81 | const int res = util::RunAndCaptureOutput(&proc, STDERR_FILENO, &error); |
| 82 | if (res < 0) { |
| 83 | PLOG(ERROR) << "Failed to execute gzip"; |
| 84 | return false; |
| 85 | } |
| 86 | if (res != 0) { |
| 87 | LOG(ERROR) << "Failed to un-gzip " << compressed_file.value(); |
| 88 | util::LogMultilineError(error); |
| 89 | return false; |
| 90 | } |
| 91 | base::FilePath uncompressed_file = compressed_file.RemoveFinalExtension(); |
| 92 | std::string raw_contents; |
| 93 | std::string uncompressed_contents; |
| 94 | if (!base::ReadFileToString(raw_file, &raw_contents)) { |
| 95 | LOG(ERROR) << "Failed reading in raw_file " << raw_file.value(); |
| 96 | return false; |
| 97 | } |
| 98 | if (!base::ReadFileToString(uncompressed_file, &uncompressed_contents)) { |
| 99 | LOG(ERROR) << "Failed reading in uncompressed_file " |
| 100 | << uncompressed_file.value(); |
| 101 | return false; |
| 102 | } |
| 103 | return raw_contents == uncompressed_contents; |
| 104 | } |
| 105 | |
| 106 | // We use a somewhat random string of ASCII data to better reflect the data we |
| 107 | // would be compressing for real. We also shouldn't use something like |
| 108 | // base::RandBytesAsString() because that will generate uniformly random data |
| 109 | // which does not compress. |
| 110 | std::string CreateSemiRandomString(size_t size) { |
| 111 | std::string result; |
| 112 | result.reserve(size); |
| 113 | while (result.length() < size) { |
| 114 | int rem = size - result.length(); |
| 115 | if (rem > sizeof(kSemiRandomData) - 1) |
| 116 | rem = sizeof(kSemiRandomData) - 1; |
| 117 | int rand_start = base::RandInt(0, rem - 1); |
| 118 | int rand_end = base::RandInt(rand_start + 1, rem); |
| 119 | result.append(&kSemiRandomData[rand_start], rand_end - rand_start); |
| 120 | } |
| 121 | return result; |
| 122 | } |
| 123 | |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 124 | } // namespace |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 125 | |
| 126 | class CrashCommonUtilTest : public testing::Test { |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 127 | protected: |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 128 | void SetUp() override { |
| 129 | ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); |
| 130 | test_dir_ = scoped_temp_dir_.GetPath(); |
| 131 | paths::SetPrefixForTesting(test_dir_); |
Kansho Nishida | 630cc7a | 2019-10-23 17:37:41 +0900 | [diff] [blame] | 132 | base::FilePath file = scoped_temp_dir_.GetPath().Append("tmpfile"); |
| 133 | ASSERT_TRUE(test_util::CreateFile(file, kReadFdToStreamContents)); |
| 134 | fd_ = open(file.value().c_str(), O_RDONLY); |
Miriam Zimmerman | a23f18f | 2020-11-12 11:09:27 -0800 | [diff] [blame] | 135 | |
| 136 | // We need to properly init the CommandLine object for the metrics tests, |
| 137 | // which log it. |
| 138 | base::CommandLine::Init(0, nullptr); |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void TearDown() override { paths::SetPrefixForTesting(base::FilePath()); } |
| 142 | |
| 143 | base::FilePath test_dir_; |
| 144 | base::ScopedTempDir scoped_temp_dir_; |
Kansho Nishida | 630cc7a | 2019-10-23 17:37:41 +0900 | [diff] [blame] | 145 | unsigned int fd_; |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | TEST_F(CrashCommonUtilTest, IsCrashTestInProgress) { |
| 149 | EXPECT_FALSE(IsCrashTestInProgress()); |
| 150 | ASSERT_TRUE( |
| 151 | test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory, |
| 152 | paths::kCrashTestInProgress), |
| 153 | "")); |
| 154 | EXPECT_TRUE(IsCrashTestInProgress()); |
| 155 | } |
| 156 | |
Satoru Takabayashi | 2d72804 | 2018-12-10 09:19:00 +0900 | [diff] [blame] | 157 | TEST_F(CrashCommonUtilTest, IsDeviceCoredumpUploadAllowed) { |
| 158 | EXPECT_FALSE(IsDeviceCoredumpUploadAllowed()); |
| 159 | ASSERT_TRUE( |
| 160 | test_util::CreateFile(paths::GetAt(paths::kCrashReporterStateDirectory, |
| 161 | paths::kDeviceCoredumpUploadAllowed), |
| 162 | "")); |
| 163 | EXPECT_TRUE(IsDeviceCoredumpUploadAllowed()); |
| 164 | } |
| 165 | |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 166 | TEST_F(CrashCommonUtilTest, IsDeveloperImage) { |
| 167 | EXPECT_FALSE(IsDeveloperImage()); |
| 168 | |
| 169 | ASSERT_TRUE(test_util::CreateFile(paths::Get(paths::kLeaveCoreFile), "")); |
| 170 | EXPECT_TRUE(IsDeveloperImage()); |
| 171 | |
| 172 | ASSERT_TRUE( |
| 173 | test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory, |
| 174 | paths::kCrashTestInProgress), |
| 175 | "")); |
| 176 | EXPECT_FALSE(IsDeveloperImage()); |
| 177 | } |
| 178 | |
Satoru Takabayashi | f6a3680 | 2018-08-14 16:23:05 +0900 | [diff] [blame] | 179 | TEST_F(CrashCommonUtilTest, IsTestImage) { |
| 180 | EXPECT_FALSE(IsTestImage()); |
| 181 | |
| 182 | // Should return false because the channel is stable. |
| 183 | ASSERT_TRUE(test_util::CreateFile( |
| 184 | paths::GetAt(paths::kEtcDirectory, paths::kLsbRelease), |
| 185 | "CHROMEOS_RELEASE_TRACK=stable-channel")); |
| 186 | EXPECT_FALSE(IsTestImage()); |
| 187 | |
| 188 | // Should return true because the channel is testimage. |
| 189 | ASSERT_TRUE(test_util::CreateFile( |
| 190 | paths::GetAt(paths::kEtcDirectory, paths::kLsbRelease), |
| 191 | "CHROMEOS_RELEASE_TRACK=testimage-channel")); |
| 192 | EXPECT_TRUE(IsTestImage()); |
| 193 | |
| 194 | // Should return false if kCrashTestInProgress is present. |
| 195 | ASSERT_TRUE( |
| 196 | test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory, |
| 197 | paths::kCrashTestInProgress), |
| 198 | "")); |
| 199 | EXPECT_FALSE(IsTestImage()); |
| 200 | } |
| 201 | |
Satoru Takabayashi | 9a58752 | 2018-10-29 09:36:27 +0900 | [diff] [blame] | 202 | TEST_F(CrashCommonUtilTest, IsOfficialImage) { |
| 203 | EXPECT_FALSE(IsOfficialImage()); |
| 204 | |
Satoru Takabayashi | 9a58752 | 2018-10-29 09:36:27 +0900 | [diff] [blame] | 205 | // Check if lsb-release is handled correctly. |
| 206 | ASSERT_TRUE(test_util::CreateFile( |
| 207 | paths::Get("/etc/lsb-release"), |
| 208 | "CHROMEOS_RELEASE_DESCRIPTION=10964.0 (Test Build) developer-build")); |
| 209 | EXPECT_FALSE(IsOfficialImage()); |
| 210 | |
| 211 | ASSERT_TRUE(test_util::CreateFile( |
| 212 | paths::Get("/etc/lsb-release"), |
| 213 | "CHROMEOS_RELEASE_DESCRIPTION=10964.0 (Official Build) canary-channel")); |
| 214 | EXPECT_TRUE(IsOfficialImage()); |
| 215 | } |
| 216 | |
Ian Barkley-Yeung | b12d6cc | 2020-02-12 14:00:54 -0800 | [diff] [blame] | 217 | TEST_F(CrashCommonUtilTest, HasMockConsent) { |
Miriam Zimmerman | 3c95116 | 2020-03-18 09:39:21 -0700 | [diff] [blame] | 218 | ASSERT_TRUE(test_util::CreateFile(paths::Get("/etc/lsb-release"), |
| 219 | "CHROMEOS_RELEASE_TRACK=testimage-channel\n" |
| 220 | "CHROMEOS_RELEASE_DESCRIPTION=12985.0.0 " |
| 221 | "(Official Build) dev-channel asuka test")); |
Ian Barkley-Yeung | b12d6cc | 2020-02-12 14:00:54 -0800 | [diff] [blame] | 222 | EXPECT_FALSE(HasMockConsent()); |
| 223 | ASSERT_TRUE(test_util::CreateFile( |
| 224 | paths::GetAt(paths::kSystemRunStateDirectory, paths::kMockConsent), "")); |
| 225 | EXPECT_TRUE(HasMockConsent()); |
| 226 | } |
| 227 | |
Miriam Zimmerman | a23f18f | 2020-11-12 11:09:27 -0800 | [diff] [blame] | 228 | TEST_F(CrashCommonUtilTest, IgnoresMockConsentNonTest) { |
| 229 | ASSERT_TRUE(test_util::CreateFile(paths::Get("/etc/lsb-release"), |
| 230 | "CHROMEOS_RELEASE_TRACK=dev-channel\n" |
| 231 | "CHROMEOS_RELEASE_DESCRIPTION=12985.0.0 " |
| 232 | "(Official Build) dev-channel asuka")); |
| 233 | EXPECT_FALSE(HasMockConsent()); |
| 234 | ASSERT_TRUE(test_util::CreateFile( |
| 235 | paths::GetAt(paths::kSystemRunStateDirectory, paths::kMockConsent), "")); |
| 236 | EXPECT_FALSE(HasMockConsent()); |
| 237 | } |
| 238 | |
Jeffrey Kardatzke | e3fb8fb | 2019-05-13 13:59:12 -0700 | [diff] [blame] | 239 | TEST_F(CrashCommonUtilTest, GetOsTimestamp) { |
| 240 | // If we can't read /etc/lsb-release then we should be returning the null |
| 241 | // time. |
| 242 | EXPECT_TRUE(util::GetOsTimestamp().is_null()); |
| 243 | |
| 244 | base::FilePath lsb_file_path = paths::Get("/etc/lsb-release"); |
| 245 | ASSERT_TRUE(test_util::CreateFile(lsb_file_path, "foo=bar")); |
| 246 | base::Time old_time = base::Time::Now() - base::TimeDelta::FromDays(366); |
| 247 | ASSERT_TRUE(base::TouchFile(lsb_file_path, old_time, old_time)); |
| 248 | // ext2/ext3 seem to have a timestamp granularity of 1s. |
| 249 | EXPECT_EQ(util::GetOsTimestamp().ToTimeVal().tv_sec, |
| 250 | old_time.ToTimeVal().tv_sec); |
| 251 | } |
| 252 | |
| 253 | TEST_F(CrashCommonUtilTest, IsOsTimestampTooOldForUploads) { |
Ian Barkley-Yeung | c377b09 | 2019-10-09 19:23:53 -0700 | [diff] [blame] | 254 | base::SimpleTestClock clock; |
| 255 | const base::Time now = test_util::GetDefaultTime(); |
| 256 | clock.SetNow(now); |
| 257 | |
| 258 | EXPECT_FALSE(util::IsOsTimestampTooOldForUploads(base::Time(), &clock)); |
Jeffrey Kardatzke | e3fb8fb | 2019-05-13 13:59:12 -0700 | [diff] [blame] | 259 | EXPECT_FALSE(util::IsOsTimestampTooOldForUploads( |
Ian Barkley-Yeung | c377b09 | 2019-10-09 19:23:53 -0700 | [diff] [blame] | 260 | now - base::TimeDelta::FromDays(179), &clock)); |
Jeffrey Kardatzke | e3fb8fb | 2019-05-13 13:59:12 -0700 | [diff] [blame] | 261 | EXPECT_TRUE(util::IsOsTimestampTooOldForUploads( |
Ian Barkley-Yeung | c377b09 | 2019-10-09 19:23:53 -0700 | [diff] [blame] | 262 | now - base::TimeDelta::FromDays(181), &clock)); |
Miriam Zimmerman | 3604286 | 2019-11-14 20:01:29 -0800 | [diff] [blame] | 263 | |
| 264 | // Crashes with invalid timestamps should upload. |
| 265 | EXPECT_FALSE(util::IsOsTimestampTooOldForUploads( |
| 266 | now + base::TimeDelta::FromDays(1), &clock)); |
| 267 | EXPECT_FALSE(util::IsOsTimestampTooOldForUploads( |
| 268 | base::Time::FromTimeT(std::numeric_limits<time_t>::min()), &clock)); |
Jeffrey Kardatzke | e3fb8fb | 2019-05-13 13:59:12 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Jeffrey Kardatzke | ea33393 | 2019-04-12 10:17:51 -0700 | [diff] [blame] | 271 | TEST_F(CrashCommonUtilTest, GetHardwareClass) { |
| 272 | EXPECT_EQ("undefined", GetHardwareClass()); |
| 273 | |
| 274 | ASSERT_TRUE(test_util::CreateFile( |
| 275 | paths::Get("/sys/devices/platform/chromeos_acpi/HWID"), |
| 276 | kHwClassContents)); |
| 277 | EXPECT_EQ(kHwClassContents, GetHardwareClass()); |
| 278 | } |
| 279 | |
| 280 | TEST_F(CrashCommonUtilTest, GetBootModeString) { |
| 281 | EXPECT_EQ("missing-crossystem", GetBootModeString()); |
| 282 | |
Jeffrey Kardatzke | ea33393 | 2019-04-12 10:17:51 -0700 | [diff] [blame] | 283 | ASSERT_TRUE( |
| 284 | test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory, |
| 285 | paths::kCrashTestInProgress), |
| 286 | "")); |
| 287 | EXPECT_EQ("", GetBootModeString()); |
| 288 | } |
| 289 | |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 290 | TEST_F(CrashCommonUtilTest, GetCachedKeyValue) { |
| 291 | ASSERT_TRUE(test_util::CreateFile(paths::Get("/etc/lsb-release"), |
| 292 | kLsbReleaseContents)); |
| 293 | ASSERT_TRUE(test_util::CreateFile(paths::Get("/empty/lsb-release"), "")); |
| 294 | |
| 295 | std::string value; |
| 296 | // No directories are specified. |
| 297 | EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"), |
| 298 | "CHROMEOS_RELEASE_VERSION", {}, &value)); |
| 299 | // A non-existent directory is specified. |
| 300 | EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"), |
| 301 | "CHROMEOS_RELEASE_VERSION", |
| 302 | {paths::Get("/non-existent")}, &value)); |
| 303 | |
| 304 | // A non-existent base name is specified. |
| 305 | EXPECT_FALSE(GetCachedKeyValue(base::FilePath("non-existent"), |
| 306 | "CHROMEOS_RELEASE_VERSION", |
| 307 | {paths::Get("/etc")}, &value)); |
| 308 | |
| 309 | // A wrong key is specified. |
| 310 | EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"), "WRONG_KEY", |
| 311 | {paths::Get("/etc")}, &value)); |
| 312 | |
| 313 | // This should succeed. |
| 314 | EXPECT_TRUE(GetCachedKeyValue(base::FilePath("lsb-release"), |
| 315 | "CHROMEOS_RELEASE_VERSION", |
| 316 | {paths::Get("/etc")}, &value)); |
| 317 | EXPECT_EQ("10964.0.2018_08_13_1405", value); |
| 318 | |
| 319 | // A non-existent directory is included, but this should still succeed. |
| 320 | EXPECT_TRUE(GetCachedKeyValue( |
| 321 | base::FilePath("lsb-release"), "CHROMEOS_RELEASE_VERSION", |
| 322 | {paths::Get("/non-existent"), paths::Get("/etc")}, &value)); |
| 323 | EXPECT_EQ("10964.0.2018_08_13_1405", value); |
| 324 | |
| 325 | // A empty file is included, but this should still succeed. |
| 326 | EXPECT_TRUE(GetCachedKeyValue( |
| 327 | base::FilePath("lsb-release"), "CHROMEOS_RELEASE_VERSION", |
| 328 | {paths::Get("/empty"), paths::Get("/etc")}, &value)); |
| 329 | EXPECT_EQ("10964.0.2018_08_13_1405", value); |
| 330 | } |
| 331 | |
| 332 | TEST_F(CrashCommonUtilTest, GetCachedKeyValueDefault) { |
| 333 | std::string value; |
| 334 | EXPECT_FALSE( |
| 335 | GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value)); |
| 336 | |
Jeffrey Kardatzke | 8ed15d1 | 2019-03-21 16:57:20 -0700 | [diff] [blame] | 337 | // kEtcDirectory is the second candidate directory. |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 338 | ASSERT_TRUE(test_util::CreateFile( |
Jeffrey Kardatzke | 8ed15d1 | 2019-03-21 16:57:20 -0700 | [diff] [blame] | 339 | paths::GetAt(paths::kEtcDirectory, "test.txt"), "FOO=2\n")); |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 340 | EXPECT_TRUE( |
| 341 | GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value)); |
| 342 | EXPECT_EQ("2", value); |
| 343 | |
| 344 | // kCrashReporterStateDirectory is the first candidate directory. |
| 345 | ASSERT_TRUE(test_util::CreateFile( |
Jeffrey Kardatzke | 8ed15d1 | 2019-03-21 16:57:20 -0700 | [diff] [blame] | 346 | paths::GetAt(paths::kCrashReporterStateDirectory, "test.txt"), |
| 347 | "FOO=1\n")); |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 348 | EXPECT_TRUE( |
| 349 | GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value)); |
| 350 | EXPECT_EQ("1", value); |
| 351 | } |
| 352 | |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 353 | TEST_F(CrashCommonUtilTest, GetUserCrashDirectories) { |
| 354 | auto mock = |
| 355 | std::make_unique<org::chromium::SessionManagerInterfaceProxyMock>(); |
| 356 | |
| 357 | std::vector<base::FilePath> directories; |
| 358 | |
| 359 | test_util::SetActiveSessions(mock.get(), {}); |
| 360 | EXPECT_TRUE(GetUserCrashDirectories(mock.get(), &directories)); |
| 361 | EXPECT_TRUE(directories.empty()); |
| 362 | |
| 363 | test_util::SetActiveSessions(mock.get(), |
| 364 | {{"user1", "hash1"}, {"user2", "hash2"}}); |
| 365 | EXPECT_TRUE(GetUserCrashDirectories(mock.get(), &directories)); |
| 366 | EXPECT_EQ(2, directories.size()); |
Satoru Takabayashi | b09f705 | 2018-10-01 15:26:29 +0900 | [diff] [blame] | 367 | EXPECT_EQ(paths::Get("/home/user/hash1/crash").value(), |
| 368 | directories[0].value()); |
| 369 | EXPECT_EQ(paths::Get("/home/user/hash2/crash").value(), |
| 370 | directories[1].value()); |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 371 | } |
| 372 | |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 373 | TEST_F(CrashCommonUtilTest, GzipStream) { |
| 374 | std::string content = CreateSemiRandomString( |
| 375 | base::RandInt(kRandomDataMinLength, kRandomDataMaxLength)); |
Ian Barkley-Yeung | e87b4f4 | 2019-05-22 15:05:13 -0700 | [diff] [blame] | 376 | std::vector<unsigned char> compressed_content = |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 377 | util::GzipStream(brillo::MemoryStream::OpenCopyOf( |
| 378 | content.c_str(), content.length(), nullptr)); |
| 379 | EXPECT_FALSE(compressed_content.empty()); |
Ian Barkley-Yeung | e87b4f4 | 2019-05-22 15:05:13 -0700 | [diff] [blame] | 380 | EXPECT_LT(compressed_content.size(), content.size()) |
| 381 | << "Didn't actually compress"; |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 382 | base::FilePath raw_file; |
| 383 | ASSERT_TRUE(base::CreateTemporaryFileInDir(test_dir_, &raw_file)); |
Ian Barkley-Yeung | e87b4f4 | 2019-05-22 15:05:13 -0700 | [diff] [blame] | 384 | base::FilePath compressed_file_name; |
| 385 | ASSERT_TRUE(base::CreateTemporaryFileInDir(test_dir_, &compressed_file_name)); |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 386 | // Remove the file we will decompress to or gzip will fail on decompression. |
hscham | 53cf73a | 2020-11-30 15:58:42 +0900 | [diff] [blame] | 387 | ASSERT_TRUE(base::DeleteFile(compressed_file_name)); |
Ian Barkley-Yeung | e87b4f4 | 2019-05-22 15:05:13 -0700 | [diff] [blame] | 388 | compressed_file_name = compressed_file_name.AddExtension(".gz"); |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 389 | ASSERT_EQ(base::WriteFile(raw_file, content.c_str(), content.length()), |
| 390 | content.length()); |
Ian Barkley-Yeung | e87b4f4 | 2019-05-22 15:05:13 -0700 | [diff] [blame] | 391 | { |
| 392 | base::File compressed_file( |
| 393 | compressed_file_name, base::File::FLAG_WRITE | base::File::FLAG_CREATE); |
| 394 | ASSERT_TRUE(compressed_file.IsValid()); |
| 395 | ssize_t write_result = HANDLE_EINTR(write(compressed_file.GetPlatformFile(), |
| 396 | compressed_content.data(), |
| 397 | compressed_content.size())); |
| 398 | ASSERT_EQ(write_result, compressed_content.size()); |
| 399 | } |
| 400 | EXPECT_TRUE(VerifyCompression(raw_file, compressed_file_name)) |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 401 | << "Random input data: " << content; |
| 402 | } |
| 403 | |
Tim Zheng | 11a665e | 2019-06-26 17:44:01 -0700 | [diff] [blame] | 404 | TEST_F(CrashCommonUtilTest, |
| 405 | DISABLED_ON_QEMU_FOR_MEMFD_CREATE(ReadMemfdToStringEmpty)) { |
| 406 | int memfd = memfd_create("test_memfd", 0); |
| 407 | std::string read_outs; |
| 408 | EXPECT_FALSE(ReadMemfdToString(memfd, &read_outs)); |
| 409 | } |
| 410 | |
| 411 | TEST_F(CrashCommonUtilTest, |
| 412 | DISABLED_ON_QEMU_FOR_MEMFD_CREATE(ReadMemfdToStringSuccess)) { |
| 413 | int memfd = memfd_create("test_memfd", 0); |
| 414 | const std::string write_ins = "Test data to write into memfd"; |
| 415 | ASSERT_EQ(write(memfd, write_ins.c_str(), strlen(write_ins.c_str())), |
| 416 | strlen(write_ins.c_str())); |
| 417 | std::string read_outs; |
| 418 | EXPECT_TRUE(ReadMemfdToString(memfd, &read_outs)); |
| 419 | EXPECT_EQ(read_outs, write_ins); |
| 420 | } |
| 421 | |
Kansho Nishida | 630cc7a | 2019-10-23 17:37:41 +0900 | [diff] [blame] | 422 | TEST_F(CrashCommonUtilTest, ReadFdToStream) { |
| 423 | std::stringstream stream; |
| 424 | EXPECT_TRUE(ReadFdToStream(fd_, &stream)); |
| 425 | EXPECT_EQ(kReadFdToStreamContents, stream.str()); |
| 426 | } |
| 427 | |
Miriam Zimmerman | a23f18f | 2020-11-12 11:09:27 -0800 | [diff] [blame] | 428 | TEST_F(CrashCommonUtilTest, IsFeedbackAllowedMock) { |
| 429 | MetricsLibraryMock mock_metrics; |
| 430 | mock_metrics.set_metrics_enabled(false); |
| 431 | |
| 432 | ASSERT_TRUE(test_util::CreateFile(paths::Get("/etc/lsb-release"), |
| 433 | "CHROMEOS_RELEASE_TRACK=testimage-channel\n" |
| 434 | "CHROMEOS_RELEASE_DESCRIPTION=12985.0.0 " |
| 435 | "(Official Build) dev-channel asuka test")); |
| 436 | |
| 437 | EXPECT_FALSE(IsFeedbackAllowed(&mock_metrics)); |
| 438 | ASSERT_TRUE(test_util::CreateFile( |
| 439 | paths::GetAt(paths::kSystemRunStateDirectory, paths::kMockConsent), "")); |
| 440 | EXPECT_TRUE(HasMockConsent()); |
| 441 | |
| 442 | EXPECT_TRUE(IsFeedbackAllowed(&mock_metrics)); |
| 443 | } |
| 444 | |
| 445 | TEST_F(CrashCommonUtilTest, IsFeedbackAllowedDev) { |
| 446 | MetricsLibraryMock mock_metrics; |
| 447 | mock_metrics.set_metrics_enabled(false); |
| 448 | |
| 449 | EXPECT_FALSE(IsFeedbackAllowed(&mock_metrics)); |
| 450 | |
| 451 | ASSERT_TRUE(test_util::CreateFile(paths::Get(paths::kLeaveCoreFile), "")); |
| 452 | |
| 453 | EXPECT_TRUE(IsFeedbackAllowed(&mock_metrics)); |
| 454 | } |
| 455 | |
| 456 | // Disable this test when in a VM because there's no easy way to mock the |
| 457 | // VmSupport class. |
| 458 | // TODO(https://crbug.com/1150011): When that class can be replaced for tests, |
| 459 | // use a fake implementation here to set metrics consent appropriately. |
| 460 | #if !USE_KVM_GUEST |
| 461 | TEST_F(CrashCommonUtilTest, IsFeedbackAllowedRespectsMetricsLib) { |
| 462 | MetricsLibraryMock mock_metrics; |
| 463 | mock_metrics.set_metrics_enabled(false); |
| 464 | |
| 465 | EXPECT_FALSE(IsFeedbackAllowed(&mock_metrics)); |
| 466 | |
| 467 | mock_metrics.set_metrics_enabled(true); |
| 468 | EXPECT_TRUE(IsFeedbackAllowed(&mock_metrics)); |
| 469 | } |
| 470 | #endif // USE_KVM_GUEST |
| 471 | |
Miriam Zimmerman | 9322118 | 2021-04-02 13:47:58 -0700 | [diff] [blame] | 472 | // Verify that SkipCrashCollection behaves as expected for filter-in. |
| 473 | TEST_F(CrashCommonUtilTest, SkipCrashCollection_FilterIn) { |
| 474 | // Force test image. |
| 475 | ASSERT_TRUE(test_util::CreateFile( |
| 476 | paths::GetAt(paths::kEtcDirectory, paths::kLsbRelease), |
| 477 | "CHROMEOS_RELEASE_TRACK=testimage-channel")); |
| 478 | |
| 479 | int argc = 2; |
| 480 | const char* argv_some_exec[] = {"/sbin/crash_reporter", |
| 481 | "--user=--user=14074:11:0:0:some_exec"}; |
| 482 | const char* argv_foobar[] = {"/sbin/crash_reporter", |
| 483 | "--user=--user=14074:11:0:0:foobar"}; |
| 484 | |
| 485 | // With neither file existing, both should be collected. |
| 486 | ASSERT_FALSE(base::PathExists( |
| 487 | paths::GetAt(paths::kSystemRunStateDirectory, paths::kFilterInFile))); |
| 488 | ASSERT_FALSE(base::PathExists( |
| 489 | paths::GetAt(paths::kSystemRunStateDirectory, paths::kFilterOutFile))); |
| 490 | |
| 491 | EXPECT_FALSE(SkipCrashCollection(argc, argv_some_exec)); |
| 492 | EXPECT_FALSE(SkipCrashCollection(argc, argv_foobar)); |
| 493 | |
| 494 | // Create filter-in with "none" -- both should be skipped |
| 495 | ASSERT_TRUE(test_util::CreateFile( |
| 496 | paths::GetAt(paths::kSystemRunStateDirectory, paths::kFilterInFile), |
| 497 | "none")); |
| 498 | EXPECT_TRUE(SkipCrashCollection(argc, argv_some_exec)); |
| 499 | EXPECT_TRUE(SkipCrashCollection(argc, argv_foobar)); |
| 500 | |
| 501 | // Create filter-in with "some_exec" -- some_exec should be allowed. |
| 502 | ASSERT_TRUE(test_util::CreateFile( |
| 503 | paths::GetAt(paths::kSystemRunStateDirectory, paths::kFilterInFile), |
| 504 | "some_exec")); |
| 505 | EXPECT_FALSE(SkipCrashCollection(argc, argv_some_exec)); |
| 506 | EXPECT_TRUE(SkipCrashCollection(argc, argv_foobar)); |
| 507 | } |
| 508 | |
| 509 | // Verify that SkipCrashCollection behaves as expected for filter-out. |
| 510 | TEST_F(CrashCommonUtilTest, SkipCrashCollection_FilterOut) { |
| 511 | // Force test image. |
| 512 | ASSERT_TRUE(test_util::CreateFile( |
| 513 | paths::GetAt(paths::kEtcDirectory, paths::kLsbRelease), |
| 514 | "CHROMEOS_RELEASE_TRACK=testimage-channel")); |
| 515 | |
| 516 | int argc = 2; |
| 517 | const char* argv_some_exec[] = {"/sbin/crash_reporter", |
| 518 | "--user=--user=14074:11:0:0:some_exec"}; |
| 519 | const char* argv_foobar[] = {"/sbin/crash_reporter", |
| 520 | "--user=--user=14074:11:0:0:foobar"}; |
| 521 | |
| 522 | // With neither file existing, both should be collected. |
| 523 | ASSERT_FALSE(base::PathExists( |
| 524 | paths::GetAt(paths::kSystemRunStateDirectory, paths::kFilterInFile))); |
| 525 | ASSERT_FALSE(base::PathExists( |
| 526 | paths::GetAt(paths::kSystemRunStateDirectory, paths::kFilterOutFile))); |
| 527 | |
| 528 | EXPECT_FALSE(SkipCrashCollection(argc, argv_some_exec)); |
| 529 | EXPECT_FALSE(SkipCrashCollection(argc, argv_foobar)); |
| 530 | |
| 531 | // Create filter-out with "some_exec" -- some_exec should be skipped, but |
| 532 | // not foobar. |
| 533 | ASSERT_TRUE(test_util::CreateFile( |
| 534 | paths::GetAt(paths::kSystemRunStateDirectory, paths::kFilterOutFile), |
| 535 | "some_exec")); |
| 536 | EXPECT_TRUE(SkipCrashCollection(argc, argv_some_exec)); |
| 537 | EXPECT_FALSE(SkipCrashCollection(argc, argv_foobar)); |
| 538 | } |
| 539 | |
Chloe Pelling | 83dda4c | 2021-02-17 17:01:21 +1100 | [diff] [blame] | 540 | // Test fixture for |GetPathToThisBinary()|. |
| 541 | class CrashCommonUtilGetPathToThisBinaryTest : public CrashCommonUtilTest { |
| 542 | public: |
| 543 | CrashCommonUtilGetPathToThisBinaryTest() |
| 544 | : argv_path_("fake_crash_reporter"), override_path_("override") {} |
| 545 | |
| 546 | protected: |
| 547 | void SetUp() override { |
| 548 | CrashCommonUtilTest::SetUp(); |
| 549 | |
| 550 | // Set working directory and create temp files to serve as fake "binaries". |
| 551 | // Needed because the paths given to GetPathToThisBinary() must actually |
| 552 | // exist for it to operate as intended. |
| 553 | EXPECT_TRUE(GetCurrentDirectory(&original_cwd_)); |
| 554 | EXPECT_TRUE(SetCurrentDirectory(test_dir_)); |
| 555 | ASSERT_TRUE(base::WriteFile(argv_path_, "")); |
| 556 | ASSERT_TRUE(base::WriteFile(override_path_, "")); |
| 557 | } |
| 558 | |
| 559 | void TearDown() override { |
| 560 | EXPECT_TRUE(SetCurrentDirectory(original_cwd_)); |
| 561 | EXPECT_EQ(unsetenv(kLdArgv0), 0); |
| 562 | |
| 563 | CrashCommonUtilTest::TearDown(); |
| 564 | } |
| 565 | |
| 566 | const char* test_argv_[2] = {"fake_crash_reporter", "--install"}; |
| 567 | base::FilePath argv_path_; |
| 568 | base::FilePath override_path_; |
| 569 | |
| 570 | private: |
| 571 | base::FilePath original_cwd_; |
| 572 | }; |
| 573 | |
| 574 | TEST_F(CrashCommonUtilGetPathToThisBinaryTest, UsesArgv) { |
| 575 | // If the overriding environment variable is not set, argv[0] is used |
| 576 | // to determine this binary's path. |
| 577 | EXPECT_EQ(unsetenv(kLdArgv0), 0); |
| 578 | base::FilePath path = util::GetPathToThisBinary(test_argv_); |
| 579 | |
| 580 | EXPECT_EQ(path.DirName(), test_dir_); |
| 581 | EXPECT_EQ(path.BaseName(), argv_path_); |
| 582 | } |
| 583 | |
| 584 | TEST_F(CrashCommonUtilGetPathToThisBinaryTest, UsesEnvVar) { |
| 585 | EXPECT_EQ(setenv(kLdArgv0, override_path_.value().c_str(), 1 /* replace */), |
| 586 | 0); |
| 587 | base::FilePath path = util::GetPathToThisBinary(test_argv_); |
| 588 | |
| 589 | EXPECT_EQ(path.DirName(), test_dir_); |
| 590 | EXPECT_EQ(path.BaseName(), override_path_); |
| 591 | } |
| 592 | |
| 593 | TEST_F(CrashCommonUtilGetPathToThisBinaryTest, IgnoresEmptyEnvVar) { |
| 594 | EXPECT_EQ(setenv(kLdArgv0, "", 1 /* replace */), 0); |
| 595 | base::FilePath path = util::GetPathToThisBinary(test_argv_); |
| 596 | |
| 597 | EXPECT_EQ(path.DirName(), test_dir_); |
| 598 | EXPECT_EQ(path.BaseName(), argv_path_); |
| 599 | } |
| 600 | |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 601 | } // namespace util |