Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: client/settings_test.cc

Issue 988063003: Define the Settings interface for a CrashReportDatabase and provide a Mac implementation. (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Address comments Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« client/settings.cc ('K') | « client/settings.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 settings(settings_path());
67 UUID actual;
68 EXPECT_TRUE(settings.GetClientID(&actual));
69 EXPECT_EQ(client_id, actual);
70 }
71
72 TEST_F(SettingsTest, UploadsEnabled) {
73 bool enabled = true;
74 // Default value is false.
75 EXPECT_TRUE(settings()->GetUploadsEnabled(&enabled));
76 EXPECT_FALSE(enabled);
77
78 EXPECT_TRUE(settings()->SetUploadsEnabled(true));
79 EXPECT_TRUE(settings()->GetUploadsEnabled(&enabled));
80 EXPECT_TRUE(enabled);
81
82 Settings local_settings(settings_path());
83 EXPECT_TRUE(local_settings.Initialize());
84 enabled = false;
85 EXPECT_TRUE(local_settings.GetUploadsEnabled(&enabled));
86 EXPECT_TRUE(enabled);
87
88 EXPECT_TRUE(settings()->SetUploadsEnabled(false));
89 EXPECT_TRUE(settings()->GetUploadsEnabled(&enabled));
90 EXPECT_FALSE(enabled);
91 }
Mark Mentovai 2015/03/09 22:00:58 Recheck that local_settings.GetUploadsEnabled() re
92
93 TEST_F(SettingsTest, LastUploadAttemptTime) {
94 time_t actual = -1;
95 EXPECT_TRUE(settings()->GetLastUploadAttemptTime(&actual));
96 // Default value is 0.
97 EXPECT_EQ(0, actual);
98
99 const time_t expected = time(nullptr);
100 EXPECT_TRUE(settings()->SetLastUploadAttemptTime(expected));
101 EXPECT_TRUE(settings()->GetLastUploadAttemptTime(&actual));
102 EXPECT_EQ(expected, actual);
103
104 Settings local_settings(settings_path());
105 EXPECT_TRUE(local_settings.Initialize());
106 actual = -1;
107 EXPECT_TRUE(local_settings.GetLastUploadAttemptTime(&actual));
108 EXPECT_EQ(expected, actual);
109 }
110
111 // The following tests write a corrupt settings file and test the recovery
112 // operation.
113
114 TEST_F(SettingsTest, BadFileOnInitialize) {
115 InitializeBadFile();
116
117 Settings settings(settings_path());
118 EXPECT_TRUE(settings.Initialize());
119 }
120
121 TEST_F(SettingsTest, BadFileOnGet) {
122 InitializeBadFile();
123
124 UUID client_id;
125 EXPECT_TRUE(settings()->GetClientID(&client_id));
126 EXPECT_NE(UUID(), client_id);
127
128 Settings local_settings(settings_path());
129 EXPECT_TRUE(local_settings.Initialize());
130 UUID actual;
131 EXPECT_TRUE(local_settings.GetClientID(&actual));
132 EXPECT_EQ(client_id, actual);
133 }
134
135 TEST_F(SettingsTest, BadFileOnSet) {
136 InitializeBadFile();
137
138 EXPECT_TRUE(settings()->SetUploadsEnabled(true));
139 bool enabled = false;
140 EXPECT_TRUE(settings()->GetUploadsEnabled(&enabled));
141 EXPECT_TRUE(enabled);
142 }
143
144 TEST_F(SettingsTest, UnlinkFile) {
145 UUID client_id;
146 EXPECT_TRUE(settings()->GetClientID(&client_id));
147
148 EXPECT_EQ(0, unlink(settings_path().value().c_str()));
149
150 Settings local_settings(settings_path());
151 EXPECT_TRUE(local_settings.Initialize());
152 UUID new_client_id;
153 EXPECT_TRUE(local_settings.GetClientID(&new_client_id));
154 EXPECT_NE(client_id, new_client_id);
Mark Mentovai 2015/03/09 22:00:58 Check that the other fields are at their expected
155 }
156
157 } // namespace
158 } // namespace test
159 } // namespace crashpad
OLDNEW
« client/settings.cc ('K') | « client/settings.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698