OLD | NEW |
---|---|
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 18 matching lines...) Expand all Loading... | |
29 struct stat st; | 29 struct stat st; |
30 return lstat(path.value().c_str(), &st) == 0; | 30 return lstat(path.value().c_str(), &st) == 0; |
31 #elif defined(OS_WIN) | 31 #elif defined(OS_WIN) |
32 struct _stat st; | 32 struct _stat st; |
33 return _wstat(path.value().c_str(), &st) == 0; | 33 return _wstat(path.value().c_str(), &st) == 0; |
34 #else | 34 #else |
35 #error "Not implemented" | 35 #error "Not implemented" |
36 #endif | 36 #endif |
37 } | 37 } |
38 | 38 |
39 void CreateFile(const base::FilePath& path) { | 39 class CrashReportDatabaseTest : public testing::Test { |
40 FileHandle handle = LoggingOpenFileForWrite(path, | 40 public: |
41 FileWriteMode::kCreateOrFail, | 41 CrashReportDatabaseTest() { |
42 FilePermissions::kWorldReadable); | 42 } |
43 #if defined(OS_POSIX) | |
44 ASSERT_GE(handle, 0); | |
45 #elif defined(OS_WIN) | |
46 ASSERT_NE(handle, nullptr); | |
47 #endif | |
48 ASSERT_TRUE( | |
49 LoggingWriteFile(handle, path.value().c_str(), path.value().length())); | |
50 ASSERT_TRUE(LoggingCloseFile(handle)); | |
51 } | |
52 | 43 |
53 class CrashReportDatabaseTest : public testing::Test { | |
54 protected: | 44 protected: |
55 // testing::Test: | 45 // testing::Test: |
56 void SetUp() override { | 46 void SetUp() override { |
57 db_ = CrashReportDatabase::Initialize(path()); | 47 db_ = CrashReportDatabase::Initialize(path()); |
58 ASSERT_TRUE(db_.get()); | 48 ASSERT_TRUE(db_); |
59 } | 49 } |
60 | 50 |
61 void ResetDatabase() { | 51 void ResetDatabase() { |
62 db_.reset(); | 52 db_.reset(); |
63 } | 53 } |
64 | 54 |
65 CrashReportDatabase* db() const { return db_.get(); } | 55 CrashReportDatabase* db() { return db_.get(); } |
66 const base::FilePath& path() const { return temp_dir_.path(); } | 56 const base::FilePath& path() const { return temp_dir_.path(); } |
67 | 57 |
68 void CreateCrashReport(CrashReportDatabase::Report* report) { | 58 void CreateCrashReport(CrashReportDatabase::Report* report) { |
69 CrashReportDatabase::NewReport* new_report; | 59 CrashReportDatabase::NewReport* new_report = nullptr; |
70 EXPECT_EQ(CrashReportDatabase::kNoError, | 60 ASSERT_EQ(CrashReportDatabase::kNoError, |
71 db_->PrepareNewCrashReport(&new_report)); | 61 db_->PrepareNewCrashReport(&new_report)); |
72 const char kTest[] = "test"; | 62 const char kTest[] = "test"; |
73 ASSERT_TRUE(LoggingWriteFile(new_report->handle, kTest, sizeof(kTest))); | 63 ASSERT_TRUE(LoggingWriteFile(new_report->handle, kTest, sizeof(kTest))); |
74 | 64 |
75 UUID uuid; | 65 UUID uuid; |
76 EXPECT_EQ(CrashReportDatabase::kNoError, | 66 EXPECT_EQ(CrashReportDatabase::kNoError, |
77 db_->FinishedWritingCrashReport(new_report, &uuid)); | 67 db_->FinishedWritingCrashReport(new_report, &uuid)); |
78 | 68 |
79 EXPECT_EQ(CrashReportDatabase::kNoError, | 69 EXPECT_EQ(CrashReportDatabase::kNoError, |
80 db_->LookUpCrashReport(uuid, report)); | 70 db_->LookUpCrashReport(uuid, report)); |
81 ExpectPreparedCrashReport(*report); | 71 ExpectPreparedCrashReport(*report); |
82 ASSERT_TRUE(FileExistsAtPath(report->file_path)); | 72 ASSERT_TRUE(FileExistsAtPath(report->file_path)); |
83 } | 73 } |
84 | 74 |
85 void UploadReport(const UUID& uuid, bool successful, const std::string& id) { | 75 void UploadReport(const UUID& uuid, bool successful, const std::string& id) { |
86 const CrashReportDatabase::Report* report = nullptr; | 76 const CrashReportDatabase::Report* report = nullptr; |
87 EXPECT_EQ(CrashReportDatabase::kNoError, | 77 ASSERT_EQ(CrashReportDatabase::kNoError, |
88 db_->GetReportForUploading(uuid, &report)); | 78 db_->GetReportForUploading(uuid, &report)); |
89 EXPECT_TRUE(report); | |
90 EXPECT_NE(UUID(), report->uuid); | 79 EXPECT_NE(UUID(), report->uuid); |
91 EXPECT_FALSE(report->file_path.empty()); | 80 EXPECT_FALSE(report->file_path.empty()); |
92 EXPECT_TRUE(FileExistsAtPath(report->file_path)) | 81 EXPECT_TRUE(FileExistsAtPath(report->file_path)) |
93 << report->file_path.value(); | 82 << report->file_path.value(); |
94 EXPECT_GT(report->creation_time, 0); | 83 EXPECT_GT(report->creation_time, 0); |
95 EXPECT_EQ(CrashReportDatabase::kNoError, | 84 EXPECT_EQ(CrashReportDatabase::kNoError, |
96 db_->RecordUploadAttempt(report, successful, id)); | 85 db_->RecordUploadAttempt(report, successful, id)); |
97 } | 86 } |
98 | 87 |
99 void ExpectPreparedCrashReport(const CrashReportDatabase::Report& report) { | 88 void ExpectPreparedCrashReport(const CrashReportDatabase::Report& report) { |
100 EXPECT_NE(UUID(), report.uuid); | 89 EXPECT_NE(UUID(), report.uuid); |
101 EXPECT_FALSE(report.file_path.empty()); | 90 EXPECT_FALSE(report.file_path.empty()); |
102 EXPECT_TRUE(FileExistsAtPath(report.file_path)) << report.file_path.value(); | 91 EXPECT_TRUE(FileExistsAtPath(report.file_path)) << report.file_path.value(); |
103 EXPECT_TRUE(report.id.empty()); | 92 EXPECT_TRUE(report.id.empty()); |
104 EXPECT_GT(report.creation_time, 0); | 93 EXPECT_GT(report.creation_time, 0); |
105 EXPECT_FALSE(report.uploaded); | 94 EXPECT_FALSE(report.uploaded); |
106 EXPECT_EQ(0, report.last_upload_attempt_time); | 95 EXPECT_EQ(0, report.last_upload_attempt_time); |
107 EXPECT_EQ(0, report.upload_attempts); | 96 EXPECT_EQ(0, report.upload_attempts); |
108 } | 97 } |
109 | 98 |
110 void RelocateDatabase() { | 99 void RelocateDatabase() { |
111 ResetDatabase(); | 100 ResetDatabase(); |
112 temp_dir_.Rename(); | 101 temp_dir_.Rename(); |
113 SetUp(); | 102 SetUp(); |
114 } | 103 } |
115 | 104 |
116 private: | 105 private: |
117 ScopedTempDir temp_dir_; | 106 ScopedTempDir temp_dir_; |
118 scoped_ptr<CrashReportDatabase> db_; | 107 scoped_ptr<CrashReportDatabase> db_; |
108 | |
109 DISALLOW_COPY_AND_ASSIGN(CrashReportDatabaseTest); | |
119 }; | 110 }; |
120 | 111 |
121 TEST_F(CrashReportDatabaseTest, Initialize) { | 112 TEST_F(CrashReportDatabaseTest, Initialize) { |
122 // Initialize the database for the first time, creating it. | 113 // Initialize the database for the first time, creating it. |
123 EXPECT_TRUE(db()); | 114 EXPECT_TRUE(db()); |
124 | 115 |
125 // Close and reopen the database at the same path. | 116 // Close and reopen the database at the same path. |
126 ResetDatabase(); | 117 ResetDatabase(); |
127 EXPECT_FALSE(db()); | 118 EXPECT_FALSE(db()); |
128 auto db = CrashReportDatabase::Initialize(path()); | 119 auto db = CrashReportDatabase::Initialize(path()); |
129 EXPECT_TRUE(db.get()); | 120 ASSERT_TRUE(db); |
130 | 121 |
131 std::vector<const CrashReportDatabase::Report> reports; | 122 std::vector<const CrashReportDatabase::Report> reports; |
132 EXPECT_EQ(CrashReportDatabase::kNoError, db->GetPendingReports(&reports)); | 123 EXPECT_EQ(CrashReportDatabase::kNoError, db->GetPendingReports(&reports)); |
133 EXPECT_TRUE(reports.empty()); | 124 EXPECT_TRUE(reports.empty()); |
125 reports.clear(); | |
134 EXPECT_EQ(CrashReportDatabase::kNoError, db->GetCompletedReports(&reports)); | 126 EXPECT_EQ(CrashReportDatabase::kNoError, db->GetCompletedReports(&reports)); |
135 EXPECT_TRUE(reports.empty()); | 127 EXPECT_TRUE(reports.empty()); |
136 } | 128 } |
137 | 129 |
138 TEST_F(CrashReportDatabaseTest, NewCrashReport) { | 130 TEST_F(CrashReportDatabaseTest, NewCrashReport) { |
139 CrashReportDatabase::NewReport* new_report; | 131 CrashReportDatabase::NewReport* new_report; |
140 EXPECT_EQ(CrashReportDatabase::kNoError, | 132 EXPECT_EQ(CrashReportDatabase::kNoError, |
141 db()->PrepareNewCrashReport(&new_report)); | 133 db()->PrepareNewCrashReport(&new_report)); |
142 EXPECT_TRUE(FileExistsAtPath(new_report->path)) << new_report->path.value(); | 134 EXPECT_TRUE(FileExistsAtPath(new_report->path)) << new_report->path.value(); |
143 UUID uuid; | 135 UUID uuid; |
(...skipping 11 matching lines...) Expand all Loading... | |
155 ASSERT_EQ(1u, reports.size()); | 147 ASSERT_EQ(1u, reports.size()); |
156 EXPECT_EQ(report.uuid, reports[0].uuid); | 148 EXPECT_EQ(report.uuid, reports[0].uuid); |
157 | 149 |
158 reports.clear(); | 150 reports.clear(); |
159 EXPECT_EQ(CrashReportDatabase::kNoError, | 151 EXPECT_EQ(CrashReportDatabase::kNoError, |
160 db()->GetCompletedReports(&reports)); | 152 db()->GetCompletedReports(&reports)); |
161 EXPECT_TRUE(reports.empty()); | 153 EXPECT_TRUE(reports.empty()); |
162 } | 154 } |
163 | 155 |
164 TEST_F(CrashReportDatabaseTest, ErrorWritingCrashReport) { | 156 TEST_F(CrashReportDatabaseTest, ErrorWritingCrashReport) { |
165 CrashReportDatabase::NewReport* new_report; | 157 CrashReportDatabase::NewReport* new_report = nullptr; |
166 EXPECT_EQ(CrashReportDatabase::kNoError, | 158 ASSERT_EQ(CrashReportDatabase::kNoError, |
167 db()->PrepareNewCrashReport(&new_report)); | 159 db()->PrepareNewCrashReport(&new_report)); |
168 base::FilePath new_report_path = new_report->path; | 160 base::FilePath new_report_path = new_report->path; |
169 EXPECT_TRUE(FileExistsAtPath(new_report_path)) << new_report_path.value(); | 161 EXPECT_TRUE(FileExistsAtPath(new_report_path)) << new_report_path.value(); |
170 EXPECT_EQ(CrashReportDatabase::kNoError, | 162 EXPECT_EQ(CrashReportDatabase::kNoError, |
171 db()->ErrorWritingCrashReport(new_report)); | 163 db()->ErrorWritingCrashReport(new_report)); |
172 EXPECT_FALSE(FileExistsAtPath(new_report_path)) << new_report_path.value(); | 164 EXPECT_FALSE(FileExistsAtPath(new_report_path)) << new_report_path.value(); |
173 } | 165 } |
174 | 166 |
175 TEST_F(CrashReportDatabaseTest, LookUpCrashReport) { | 167 TEST_F(CrashReportDatabaseTest, LookUpCrashReport) { |
176 UUID uuid; | 168 UUID uuid; |
177 | 169 |
178 { | 170 { |
179 CrashReportDatabase::Report report; | 171 CrashReportDatabase::Report report; |
180 CreateCrashReport(&report); | 172 CreateCrashReport(&report); |
181 uuid = report.uuid; | 173 uuid = report.uuid; |
182 } | 174 } |
183 | 175 |
184 { | 176 { |
185 CrashReportDatabase::Report report; | 177 CrashReportDatabase::Report report; |
186 EXPECT_EQ(CrashReportDatabase::kNoError, | 178 EXPECT_EQ(CrashReportDatabase::kNoError, |
187 db()->LookUpCrashReport(uuid, &report)); | 179 db()->LookUpCrashReport(uuid, &report)); |
188 EXPECT_EQ(uuid, report.uuid); | 180 EXPECT_EQ(uuid, report.uuid); |
189 EXPECT_NE(std::string::npos, report.file_path.value().find(path().value())); | 181 EXPECT_NE(std::string::npos, report.file_path.value().find(path().value())); |
190 EXPECT_EQ("", report.id); | 182 EXPECT_EQ(std::string(), report.id); |
191 EXPECT_FALSE(report.uploaded); | 183 EXPECT_FALSE(report.uploaded); |
192 EXPECT_EQ(0, report.last_upload_attempt_time); | 184 EXPECT_EQ(0, report.last_upload_attempt_time); |
193 EXPECT_EQ(0, report.upload_attempts); | 185 EXPECT_EQ(0, report.upload_attempts); |
194 } | 186 } |
195 | 187 |
196 UploadReport(uuid, true, "test"); | 188 UploadReport(uuid, true, "test"); |
197 | 189 |
198 { | 190 { |
199 CrashReportDatabase::Report report; | 191 CrashReportDatabase::Report report; |
200 EXPECT_EQ(CrashReportDatabase::kNoError, | 192 EXPECT_EQ(CrashReportDatabase::kNoError, |
201 db()->LookUpCrashReport(uuid, &report)); | 193 db()->LookUpCrashReport(uuid, &report)); |
202 EXPECT_EQ(uuid, report.uuid); | 194 EXPECT_EQ(uuid, report.uuid); |
203 EXPECT_NE(std::string::npos, report.file_path.value().find(path().value())); | 195 EXPECT_NE(std::string::npos, report.file_path.value().find(path().value())); |
204 EXPECT_EQ("test", report.id); | 196 EXPECT_EQ("test", report.id); |
205 EXPECT_TRUE(report.uploaded); | 197 EXPECT_TRUE(report.uploaded); |
206 EXPECT_NE(0, report.last_upload_attempt_time); | 198 EXPECT_NE(0, report.last_upload_attempt_time); |
207 EXPECT_EQ(1, report.upload_attempts); | 199 EXPECT_EQ(1, report.upload_attempts); |
208 } | 200 } |
209 } | 201 } |
210 | 202 |
211 TEST_F(CrashReportDatabaseTest, RecordUploadAttempt) { | 203 TEST_F(CrashReportDatabaseTest, RecordUploadAttempt) { |
212 std::vector<CrashReportDatabase::Report> reports(3); | 204 std::vector<CrashReportDatabase::Report> reports(3); |
213 CreateCrashReport(&reports[0]); | 205 CreateCrashReport(&reports[0]); |
214 CreateCrashReport(&reports[1]); | 206 CreateCrashReport(&reports[1]); |
215 CreateCrashReport(&reports[2]); | 207 CreateCrashReport(&reports[2]); |
216 | 208 |
217 // Record two attempts: one successful, one not. | 209 // Record two attempts: one successful, one not. |
218 UploadReport(reports[1].uuid, false, ""); | 210 UploadReport(reports[1].uuid, false, ""); |
Peter Kasting
2015/02/14 00:24:23
Nit: Some more "" -> std::string() (multiple place
scottmg
2015/02/14 00:42:45
Done.
| |
219 UploadReport(reports[2].uuid, true, "abc123"); | 211 UploadReport(reports[2].uuid, true, "abc123"); |
220 | 212 |
221 std::vector<CrashReportDatabase::Report> query(3); | 213 std::vector<CrashReportDatabase::Report> query(3); |
222 | 214 |
223 EXPECT_EQ(CrashReportDatabase::kNoError, | 215 EXPECT_EQ(CrashReportDatabase::kNoError, |
224 db()->LookUpCrashReport(reports[0].uuid, &query[0])); | 216 db()->LookUpCrashReport(reports[0].uuid, &query[0])); |
225 EXPECT_EQ(CrashReportDatabase::kNoError, | 217 EXPECT_EQ(CrashReportDatabase::kNoError, |
226 db()->LookUpCrashReport(reports[1].uuid, &query[1])); | 218 db()->LookUpCrashReport(reports[1].uuid, &query[1])); |
227 EXPECT_EQ(CrashReportDatabase::kNoError, | 219 EXPECT_EQ(CrashReportDatabase::kNoError, |
228 db()->LookUpCrashReport(reports[2].uuid, &query[2])); | 220 db()->LookUpCrashReport(reports[2].uuid, &query[2])); |
229 | 221 |
230 EXPECT_EQ("", query[0].id); | 222 EXPECT_EQ(std::string(), query[0].id); |
231 EXPECT_EQ("", query[1].id); | 223 EXPECT_EQ(std::string(), query[1].id); |
232 EXPECT_EQ("abc123", query[2].id); | 224 EXPECT_EQ("abc123", query[2].id); |
233 | 225 |
234 EXPECT_FALSE(query[0].uploaded); | 226 EXPECT_FALSE(query[0].uploaded); |
235 EXPECT_FALSE(query[1].uploaded); | 227 EXPECT_FALSE(query[1].uploaded); |
236 EXPECT_TRUE(query[2].uploaded); | 228 EXPECT_TRUE(query[2].uploaded); |
237 | 229 |
238 EXPECT_EQ(0, query[0].last_upload_attempt_time); | 230 EXPECT_EQ(0, query[0].last_upload_attempt_time); |
239 EXPECT_NE(0, query[1].last_upload_attempt_time); | 231 EXPECT_NE(0, query[1].last_upload_attempt_time); |
240 EXPECT_NE(0, query[2].last_upload_attempt_time); | 232 EXPECT_NE(0, query[2].last_upload_attempt_time); |
241 | 233 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
332 ASSERT_EQ(1u, completed.size()); | 324 ASSERT_EQ(1u, completed.size()); |
333 | 325 |
334 for (const auto& report : pending) | 326 for (const auto& report : pending) |
335 EXPECT_NE(report_1_uuid, report.uuid); | 327 EXPECT_NE(report_1_uuid, report.uuid); |
336 EXPECT_EQ(report_1_uuid, completed[0].uuid); | 328 EXPECT_EQ(report_1_uuid, completed[0].uuid); |
337 EXPECT_EQ("report1", completed[0].id); | 329 EXPECT_EQ("report1", completed[0].id); |
338 EXPECT_EQ(true, completed[0].uploaded); | 330 EXPECT_EQ(true, completed[0].uploaded); |
339 EXPECT_GT(completed[0].last_upload_attempt_time, 0); | 331 EXPECT_GT(completed[0].last_upload_attempt_time, 0); |
340 EXPECT_EQ(1, completed[0].upload_attempts); | 332 EXPECT_EQ(1, completed[0].upload_attempts); |
341 | 333 |
342 const CrashReportDatabase::Report completed_report_1 = completed[0]; | |
343 | |
344 // Fail to upload one report. | 334 // Fail to upload one report. |
345 UploadReport(report_2_uuid, false, ""); | 335 UploadReport(report_2_uuid, false, ""); |
346 | 336 |
347 pending.clear(); | 337 pending.clear(); |
348 EXPECT_EQ(CrashReportDatabase::kNoError, | 338 EXPECT_EQ(CrashReportDatabase::kNoError, |
349 db()->GetPendingReports(&pending)); | 339 db()->GetPendingReports(&pending)); |
350 completed.clear(); | 340 completed.clear(); |
351 EXPECT_EQ(CrashReportDatabase::kNoError, | 341 EXPECT_EQ(CrashReportDatabase::kNoError, |
352 db()->GetCompletedReports(&completed)); | 342 db()->GetCompletedReports(&completed)); |
353 | 343 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
454 CrashReportDatabase::Report report; | 444 CrashReportDatabase::Report report; |
455 EXPECT_EQ(CrashReportDatabase::kNoError, | 445 EXPECT_EQ(CrashReportDatabase::kNoError, |
456 db()->LookUpCrashReport(uuid, &report)); | 446 db()->LookUpCrashReport(uuid, &report)); |
457 ExpectPreparedCrashReport(report); | 447 ExpectPreparedCrashReport(report); |
458 EXPECT_TRUE(FileExistsAtPath(report.file_path)) << report.file_path.value(); | 448 EXPECT_TRUE(FileExistsAtPath(report.file_path)) << report.file_path.value(); |
459 } | 449 } |
460 | 450 |
461 } // namespace | 451 } // namespace |
462 } // namespace test | 452 } // namespace test |
463 } // namespace crashpad | 453 } // namespace crashpad |
OLD | NEW |