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 | |
| 7 | #include <base/files/file_util.h> |
| 8 | #include <base/files/scoped_temp_dir.h> |
| 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | #include "crash-reporter/crash_sender_paths.h" |
| 12 | #include "crash-reporter/paths.h" |
| 13 | #include "crash-reporter/test_util.h" |
| 14 | |
| 15 | namespace util { |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | const char kLsbReleaseContents[] = |
| 19 | "CHROMEOS_RELEASE_BOARD=bob\n" |
| 20 | "CHROMEOS_RELEASE_NAME=Chromium OS\n" |
| 21 | "CHROMEOS_RELEASE_VERSION=10964.0.2018_08_13_1405\n"; |
| 22 | |
| 23 | } // namespace |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 24 | |
| 25 | class CrashCommonUtilTest : public testing::Test { |
| 26 | private: |
| 27 | void SetUp() override { |
| 28 | ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); |
| 29 | test_dir_ = scoped_temp_dir_.GetPath(); |
| 30 | paths::SetPrefixForTesting(test_dir_); |
| 31 | } |
| 32 | |
| 33 | void TearDown() override { paths::SetPrefixForTesting(base::FilePath()); } |
| 34 | |
| 35 | base::FilePath test_dir_; |
| 36 | base::ScopedTempDir scoped_temp_dir_; |
| 37 | }; |
| 38 | |
| 39 | TEST_F(CrashCommonUtilTest, IsCrashTestInProgress) { |
| 40 | EXPECT_FALSE(IsCrashTestInProgress()); |
| 41 | ASSERT_TRUE( |
| 42 | test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory, |
| 43 | paths::kCrashTestInProgress), |
| 44 | "")); |
| 45 | EXPECT_TRUE(IsCrashTestInProgress()); |
| 46 | } |
| 47 | |
| 48 | TEST_F(CrashCommonUtilTest, IsDeveloperImage) { |
| 49 | EXPECT_FALSE(IsDeveloperImage()); |
| 50 | |
| 51 | ASSERT_TRUE(test_util::CreateFile(paths::Get(paths::kLeaveCoreFile), "")); |
| 52 | EXPECT_TRUE(IsDeveloperImage()); |
| 53 | |
| 54 | ASSERT_TRUE( |
| 55 | test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory, |
| 56 | paths::kCrashTestInProgress), |
| 57 | "")); |
| 58 | EXPECT_FALSE(IsDeveloperImage()); |
| 59 | } |
| 60 | |
Satoru Takabayashi | f6a3680 | 2018-08-14 16:23:05 +0900 | [diff] [blame] | 61 | TEST_F(CrashCommonUtilTest, IsTestImage) { |
| 62 | EXPECT_FALSE(IsTestImage()); |
| 63 | |
| 64 | // Should return false because the channel is stable. |
| 65 | ASSERT_TRUE(test_util::CreateFile( |
| 66 | paths::GetAt(paths::kEtcDirectory, paths::kLsbRelease), |
| 67 | "CHROMEOS_RELEASE_TRACK=stable-channel")); |
| 68 | EXPECT_FALSE(IsTestImage()); |
| 69 | |
| 70 | // Should return true because the channel is testimage. |
| 71 | ASSERT_TRUE(test_util::CreateFile( |
| 72 | paths::GetAt(paths::kEtcDirectory, paths::kLsbRelease), |
| 73 | "CHROMEOS_RELEASE_TRACK=testimage-channel")); |
| 74 | EXPECT_TRUE(IsTestImage()); |
| 75 | |
| 76 | // Should return false if kCrashTestInProgress is present. |
| 77 | ASSERT_TRUE( |
| 78 | test_util::CreateFile(paths::GetAt(paths::kSystemRunStateDirectory, |
| 79 | paths::kCrashTestInProgress), |
| 80 | "")); |
| 81 | EXPECT_FALSE(IsTestImage()); |
| 82 | } |
| 83 | |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 84 | TEST_F(CrashCommonUtilTest, GetCachedKeyValue) { |
| 85 | ASSERT_TRUE(test_util::CreateFile(paths::Get("/etc/lsb-release"), |
| 86 | kLsbReleaseContents)); |
| 87 | ASSERT_TRUE(test_util::CreateFile(paths::Get("/empty/lsb-release"), "")); |
| 88 | |
| 89 | std::string value; |
| 90 | // No directories are specified. |
| 91 | EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"), |
| 92 | "CHROMEOS_RELEASE_VERSION", {}, &value)); |
| 93 | // A non-existent directory is specified. |
| 94 | EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"), |
| 95 | "CHROMEOS_RELEASE_VERSION", |
| 96 | {paths::Get("/non-existent")}, &value)); |
| 97 | |
| 98 | // A non-existent base name is specified. |
| 99 | EXPECT_FALSE(GetCachedKeyValue(base::FilePath("non-existent"), |
| 100 | "CHROMEOS_RELEASE_VERSION", |
| 101 | {paths::Get("/etc")}, &value)); |
| 102 | |
| 103 | // A wrong key is specified. |
| 104 | EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"), "WRONG_KEY", |
| 105 | {paths::Get("/etc")}, &value)); |
| 106 | |
| 107 | // This should succeed. |
| 108 | EXPECT_TRUE(GetCachedKeyValue(base::FilePath("lsb-release"), |
| 109 | "CHROMEOS_RELEASE_VERSION", |
| 110 | {paths::Get("/etc")}, &value)); |
| 111 | EXPECT_EQ("10964.0.2018_08_13_1405", value); |
| 112 | |
| 113 | // A non-existent directory is included, but this should still succeed. |
| 114 | EXPECT_TRUE(GetCachedKeyValue( |
| 115 | base::FilePath("lsb-release"), "CHROMEOS_RELEASE_VERSION", |
| 116 | {paths::Get("/non-existent"), paths::Get("/etc")}, &value)); |
| 117 | EXPECT_EQ("10964.0.2018_08_13_1405", value); |
| 118 | |
| 119 | // A empty file is included, but this should still succeed. |
| 120 | EXPECT_TRUE(GetCachedKeyValue( |
| 121 | base::FilePath("lsb-release"), "CHROMEOS_RELEASE_VERSION", |
| 122 | {paths::Get("/empty"), paths::Get("/etc")}, &value)); |
| 123 | EXPECT_EQ("10964.0.2018_08_13_1405", value); |
| 124 | } |
| 125 | |
| 126 | TEST_F(CrashCommonUtilTest, GetCachedKeyValueDefault) { |
| 127 | std::string value; |
| 128 | EXPECT_FALSE( |
| 129 | GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value)); |
| 130 | |
| 131 | // kEtcDirectory is the third candidate directory. |
| 132 | ASSERT_TRUE(test_util::CreateFile( |
| 133 | paths::GetAt(paths::kEtcDirectory, "test.txt"), "FOO=3\n")); |
| 134 | EXPECT_TRUE( |
| 135 | GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value)); |
| 136 | EXPECT_EQ("3", value); |
| 137 | |
| 138 | // kSystemCrashDirectory is the second candidate directory. |
| 139 | ASSERT_TRUE(test_util::CreateFile( |
| 140 | paths::GetAt(paths::kSystemCrashDirectory, "test.txt"), "FOO=2\n")); |
| 141 | EXPECT_TRUE( |
| 142 | GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value)); |
| 143 | EXPECT_EQ("2", value); |
| 144 | |
| 145 | // kCrashReporterStateDirectory is the first candidate directory. |
| 146 | ASSERT_TRUE(test_util::CreateFile( |
| 147 | paths::GetAt(paths::kSystemCrashDirectory, "test.txt"), "FOO=1\n")); |
| 148 | EXPECT_TRUE( |
| 149 | GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value)); |
| 150 | EXPECT_EQ("1", value); |
| 151 | } |
| 152 | |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 153 | } // namespace util |