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 | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame^] | 61 | TEST_F(CrashCommonUtilTest, GetCachedKeyValue) { |
| 62 | ASSERT_TRUE(test_util::CreateFile(paths::Get("/etc/lsb-release"), |
| 63 | kLsbReleaseContents)); |
| 64 | ASSERT_TRUE(test_util::CreateFile(paths::Get("/empty/lsb-release"), "")); |
| 65 | |
| 66 | std::string value; |
| 67 | // No directories are specified. |
| 68 | EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"), |
| 69 | "CHROMEOS_RELEASE_VERSION", {}, &value)); |
| 70 | // A non-existent directory is specified. |
| 71 | EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"), |
| 72 | "CHROMEOS_RELEASE_VERSION", |
| 73 | {paths::Get("/non-existent")}, &value)); |
| 74 | |
| 75 | // A non-existent base name is specified. |
| 76 | EXPECT_FALSE(GetCachedKeyValue(base::FilePath("non-existent"), |
| 77 | "CHROMEOS_RELEASE_VERSION", |
| 78 | {paths::Get("/etc")}, &value)); |
| 79 | |
| 80 | // A wrong key is specified. |
| 81 | EXPECT_FALSE(GetCachedKeyValue(base::FilePath("lsb-release"), "WRONG_KEY", |
| 82 | {paths::Get("/etc")}, &value)); |
| 83 | |
| 84 | // This should succeed. |
| 85 | EXPECT_TRUE(GetCachedKeyValue(base::FilePath("lsb-release"), |
| 86 | "CHROMEOS_RELEASE_VERSION", |
| 87 | {paths::Get("/etc")}, &value)); |
| 88 | EXPECT_EQ("10964.0.2018_08_13_1405", value); |
| 89 | |
| 90 | // A non-existent directory is included, but this should still succeed. |
| 91 | EXPECT_TRUE(GetCachedKeyValue( |
| 92 | base::FilePath("lsb-release"), "CHROMEOS_RELEASE_VERSION", |
| 93 | {paths::Get("/non-existent"), paths::Get("/etc")}, &value)); |
| 94 | EXPECT_EQ("10964.0.2018_08_13_1405", value); |
| 95 | |
| 96 | // A empty file is included, but this should still succeed. |
| 97 | EXPECT_TRUE(GetCachedKeyValue( |
| 98 | base::FilePath("lsb-release"), "CHROMEOS_RELEASE_VERSION", |
| 99 | {paths::Get("/empty"), paths::Get("/etc")}, &value)); |
| 100 | EXPECT_EQ("10964.0.2018_08_13_1405", value); |
| 101 | } |
| 102 | |
| 103 | TEST_F(CrashCommonUtilTest, GetCachedKeyValueDefault) { |
| 104 | std::string value; |
| 105 | EXPECT_FALSE( |
| 106 | GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value)); |
| 107 | |
| 108 | // kEtcDirectory is the third candidate directory. |
| 109 | ASSERT_TRUE(test_util::CreateFile( |
| 110 | paths::GetAt(paths::kEtcDirectory, "test.txt"), "FOO=3\n")); |
| 111 | EXPECT_TRUE( |
| 112 | GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value)); |
| 113 | EXPECT_EQ("3", value); |
| 114 | |
| 115 | // kSystemCrashDirectory is the second candidate directory. |
| 116 | ASSERT_TRUE(test_util::CreateFile( |
| 117 | paths::GetAt(paths::kSystemCrashDirectory, "test.txt"), "FOO=2\n")); |
| 118 | EXPECT_TRUE( |
| 119 | GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value)); |
| 120 | EXPECT_EQ("2", value); |
| 121 | |
| 122 | // kCrashReporterStateDirectory is the first candidate directory. |
| 123 | ASSERT_TRUE(test_util::CreateFile( |
| 124 | paths::GetAt(paths::kSystemCrashDirectory, "test.txt"), "FOO=1\n")); |
| 125 | EXPECT_TRUE( |
| 126 | GetCachedKeyValueDefault(base::FilePath("test.txt"), "FOO", &value)); |
| 127 | EXPECT_EQ("1", value); |
| 128 | } |
| 129 | |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 130 | } // namespace util |