OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "client/settings.h" |
| 16 |
| 17 #include "client/crash_report_database.h" |
| 18 #include "gtest/gtest.h" |
| 19 #include "util/file/file_io.h" |
| 20 #include "util/test/scoped_temp_dir.h" |
| 21 |
| 22 namespace crashpad { |
| 23 namespace test { |
| 24 namespace { |
| 25 |
| 26 class SettingsTest : public testing::Test { |
| 27 public: |
| 28 SettingsTest() : settings_(settings_path()) {} |
| 29 |
| 30 base::FilePath settings_path() { |
| 31 return temp_dir_.path().Append("settings"); |
| 32 } |
| 33 |
| 34 Settings* settings() { return &settings_; } |
| 35 |
| 36 void InitializeBadFile() { |
| 37 ScopedFileHandle handle( |
| 38 LoggingOpenFileForWrite(settings_path(), |
| 39 FileWriteMode::kTruncateOrCreate, |
| 40 FilePermissions::kWorldReadable)); |
| 41 ASSERT_TRUE(handle.is_valid()); |
| 42 |
| 43 const char kBuf[] = "test bad file"; |
| 44 ASSERT_TRUE(LoggingWriteFile(handle.get(), kBuf, sizeof(kBuf))); |
| 45 handle.reset(); |
| 46 } |
| 47 |
| 48 protected: |
| 49 // testing::Test: |
| 50 void SetUp() override { |
| 51 ASSERT_TRUE(settings()->Initialize()); |
| 52 } |
| 53 |
| 54 private: |
| 55 ScopedTempDir temp_dir_; |
| 56 Settings settings_; |
| 57 |
| 58 DISALLOW_COPY_AND_ASSIGN(SettingsTest); |
| 59 }; |
| 60 |
| 61 TEST_F(SettingsTest, ClientID) { |
| 62 UUID client_id; |
| 63 EXPECT_TRUE(settings()->GetClientID(&client_id)); |
| 64 EXPECT_NE(UUID(), client_id); |
| 65 |
| 66 Settings local_settings(settings_path()); |
| 67 EXPECT_TRUE(local_settings.Initialize()); |
| 68 UUID actual; |
| 69 EXPECT_TRUE(local_settings.GetClientID(&actual)); |
| 70 EXPECT_EQ(client_id, actual); |
| 71 } |
| 72 |
| 73 TEST_F(SettingsTest, UploadsEnabled) { |
| 74 bool enabled = true; |
| 75 // Default value is false. |
| 76 EXPECT_TRUE(settings()->GetUploadsEnabled(&enabled)); |
| 77 EXPECT_FALSE(enabled); |
| 78 |
| 79 EXPECT_TRUE(settings()->SetUploadsEnabled(true)); |
| 80 EXPECT_TRUE(settings()->GetUploadsEnabled(&enabled)); |
| 81 EXPECT_TRUE(enabled); |
| 82 |
| 83 Settings local_settings(settings_path()); |
| 84 EXPECT_TRUE(local_settings.Initialize()); |
| 85 enabled = false; |
| 86 EXPECT_TRUE(local_settings.GetUploadsEnabled(&enabled)); |
| 87 EXPECT_TRUE(enabled); |
| 88 |
| 89 EXPECT_TRUE(settings()->SetUploadsEnabled(false)); |
| 90 EXPECT_TRUE(settings()->GetUploadsEnabled(&enabled)); |
| 91 EXPECT_FALSE(enabled); |
| 92 |
| 93 enabled = true; |
| 94 EXPECT_TRUE(local_settings.GetUploadsEnabled(&enabled)); |
| 95 EXPECT_FALSE(enabled); |
| 96 } |
| 97 |
| 98 TEST_F(SettingsTest, LastUploadAttemptTime) { |
| 99 time_t actual = -1; |
| 100 EXPECT_TRUE(settings()->GetLastUploadAttemptTime(&actual)); |
| 101 // Default value is 0. |
| 102 EXPECT_EQ(0, actual); |
| 103 |
| 104 const time_t expected = time(nullptr); |
| 105 EXPECT_TRUE(settings()->SetLastUploadAttemptTime(expected)); |
| 106 EXPECT_TRUE(settings()->GetLastUploadAttemptTime(&actual)); |
| 107 EXPECT_EQ(expected, actual); |
| 108 |
| 109 Settings local_settings(settings_path()); |
| 110 EXPECT_TRUE(local_settings.Initialize()); |
| 111 actual = -1; |
| 112 EXPECT_TRUE(local_settings.GetLastUploadAttemptTime(&actual)); |
| 113 EXPECT_EQ(expected, actual); |
| 114 } |
| 115 |
| 116 // The following tests write a corrupt settings file and test the recovery |
| 117 // operation. |
| 118 |
| 119 TEST_F(SettingsTest, BadFileOnInitialize) { |
| 120 InitializeBadFile(); |
| 121 |
| 122 Settings settings(settings_path()); |
| 123 EXPECT_TRUE(settings.Initialize()); |
| 124 } |
| 125 |
| 126 TEST_F(SettingsTest, BadFileOnGet) { |
| 127 InitializeBadFile(); |
| 128 |
| 129 UUID client_id; |
| 130 EXPECT_TRUE(settings()->GetClientID(&client_id)); |
| 131 EXPECT_NE(UUID(), client_id); |
| 132 |
| 133 Settings local_settings(settings_path()); |
| 134 EXPECT_TRUE(local_settings.Initialize()); |
| 135 UUID actual; |
| 136 EXPECT_TRUE(local_settings.GetClientID(&actual)); |
| 137 EXPECT_EQ(client_id, actual); |
| 138 } |
| 139 |
| 140 TEST_F(SettingsTest, BadFileOnSet) { |
| 141 InitializeBadFile(); |
| 142 |
| 143 EXPECT_TRUE(settings()->SetUploadsEnabled(true)); |
| 144 bool enabled = false; |
| 145 EXPECT_TRUE(settings()->GetUploadsEnabled(&enabled)); |
| 146 EXPECT_TRUE(enabled); |
| 147 } |
| 148 |
| 149 TEST_F(SettingsTest, UnlinkFile) { |
| 150 UUID client_id; |
| 151 EXPECT_TRUE(settings()->GetClientID(&client_id)); |
| 152 EXPECT_TRUE(settings()->SetUploadsEnabled(true)); |
| 153 EXPECT_TRUE(settings()->SetLastUploadAttemptTime(time(nullptr))); |
| 154 |
| 155 EXPECT_EQ(0, unlink(settings_path().value().c_str())); |
| 156 |
| 157 Settings local_settings(settings_path()); |
| 158 EXPECT_TRUE(local_settings.Initialize()); |
| 159 UUID new_client_id; |
| 160 EXPECT_TRUE(local_settings.GetClientID(&new_client_id)); |
| 161 EXPECT_NE(client_id, new_client_id); |
| 162 |
| 163 // Check that all values are reset. |
| 164 bool enabled = true; |
| 165 EXPECT_TRUE(local_settings.GetUploadsEnabled(&enabled)); |
| 166 EXPECT_FALSE(enabled); |
| 167 |
| 168 time_t time = -1; |
| 169 EXPECT_TRUE(local_settings.GetLastUploadAttemptTime(&time)); |
| 170 EXPECT_EQ(0, time); |
| 171 } |
| 172 |
| 173 } // namespace |
| 174 } // namespace test |
| 175 } // namespace crashpad |
OLD | NEW |