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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 Settings* const settings = db_->GetSettings(); | 68 Settings* const settings = db_->GetSettings(); |
69 ASSERT_TRUE(settings); | 69 ASSERT_TRUE(settings); |
70 time_t times[2]; | 70 time_t times[2]; |
71 ASSERT_TRUE(settings->GetLastUploadAttemptTime(×[0])); | 71 ASSERT_TRUE(settings->GetLastUploadAttemptTime(×[0])); |
72 | 72 |
73 const CrashReportDatabase::Report* report = nullptr; | 73 const CrashReportDatabase::Report* report = nullptr; |
74 ASSERT_EQ(CrashReportDatabase::kNoError, | 74 ASSERT_EQ(CrashReportDatabase::kNoError, |
75 db_->GetReportForUploading(uuid, &report)); | 75 db_->GetReportForUploading(uuid, &report)); |
76 EXPECT_NE(UUID(), report->uuid); | 76 EXPECT_NE(UUID(), report->uuid); |
77 EXPECT_FALSE(report->file_path.empty()); | 77 EXPECT_FALSE(report->file_path.empty()); |
78 EXPECT_TRUE(FileExists(report->file_path)) | 78 EXPECT_TRUE(FileExists(report->file_path)) << report->file_path.value(); |
79 << report->file_path.value(); | |
80 EXPECT_GT(report->creation_time, 0); | 79 EXPECT_GT(report->creation_time, 0); |
81 EXPECT_EQ(CrashReportDatabase::kNoError, | 80 EXPECT_EQ(CrashReportDatabase::kNoError, |
82 db_->RecordUploadAttempt(report, successful, id)); | 81 db_->RecordUploadAttempt(report, successful, id)); |
83 | 82 |
84 ASSERT_TRUE(settings->GetLastUploadAttemptTime(×[1])); | 83 ASSERT_TRUE(settings->GetLastUploadAttemptTime(×[1])); |
85 EXPECT_NE(times[1], 0); | 84 EXPECT_NE(times[1], 0); |
86 EXPECT_GE(times[1], times[0]); | 85 EXPECT_GE(times[1], times[0]); |
87 } | 86 } |
88 | 87 |
89 void ExpectPreparedCrashReport(const CrashReportDatabase::Report& report) { | 88 void ExpectPreparedCrashReport(const CrashReportDatabase::Report& report) { |
(...skipping 19 matching lines...) Expand all Loading... |
109 | 108 |
110 DISALLOW_COPY_AND_ASSIGN(CrashReportDatabaseTest); | 109 DISALLOW_COPY_AND_ASSIGN(CrashReportDatabaseTest); |
111 }; | 110 }; |
112 | 111 |
113 TEST_F(CrashReportDatabaseTest, Initialize) { | 112 TEST_F(CrashReportDatabaseTest, Initialize) { |
114 // Initialize the database for the first time, creating it. | 113 // Initialize the database for the first time, creating it. |
115 ASSERT_TRUE(db()); | 114 ASSERT_TRUE(db()); |
116 | 115 |
117 Settings* settings = db()->GetSettings(); | 116 Settings* settings = db()->GetSettings(); |
118 | 117 |
119 UUID client_ids[2]; | 118 UUID client_ids[3]; |
120 ASSERT_TRUE(settings->GetClientID(&client_ids[0])); | 119 ASSERT_TRUE(settings->GetClientID(&client_ids[0])); |
121 EXPECT_NE(client_ids[0], UUID()); | 120 EXPECT_NE(client_ids[0], UUID()); |
122 | 121 |
123 time_t last_upload_attempt_time; | 122 time_t last_upload_attempt_time; |
124 ASSERT_TRUE(settings->GetLastUploadAttemptTime(&last_upload_attempt_time)); | 123 ASSERT_TRUE(settings->GetLastUploadAttemptTime(&last_upload_attempt_time)); |
125 EXPECT_EQ(0, last_upload_attempt_time); | 124 EXPECT_EQ(0, last_upload_attempt_time); |
126 | 125 |
127 // Close and reopen the database at the same path. | 126 // Close and reopen the database at the same path. |
128 ResetDatabase(); | 127 ResetDatabase(); |
129 EXPECT_FALSE(db()); | 128 EXPECT_FALSE(db()); |
130 auto db = CrashReportDatabase::Initialize(path()); | 129 auto db = CrashReportDatabase::InitializeWithoutCreating(path()); |
131 ASSERT_TRUE(db); | 130 ASSERT_TRUE(db); |
132 | 131 |
133 settings = db->GetSettings(); | 132 settings = db->GetSettings(); |
134 | 133 |
135 ASSERT_TRUE(settings->GetClientID(&client_ids[1])); | 134 ASSERT_TRUE(settings->GetClientID(&client_ids[1])); |
136 EXPECT_EQ(client_ids[0], client_ids[1]); | 135 EXPECT_EQ(client_ids[0], client_ids[1]); |
137 | 136 |
138 ASSERT_TRUE(settings->GetLastUploadAttemptTime(&last_upload_attempt_time)); | 137 ASSERT_TRUE(settings->GetLastUploadAttemptTime(&last_upload_attempt_time)); |
139 EXPECT_EQ(0, last_upload_attempt_time); | 138 EXPECT_EQ(0, last_upload_attempt_time); |
140 | 139 |
| 140 // Check that the database can also be opened by the method that is permitted |
| 141 // to create it. |
| 142 db = CrashReportDatabase::Initialize(path()); |
| 143 ASSERT_TRUE(db); |
| 144 |
| 145 settings = db->GetSettings(); |
| 146 |
| 147 ASSERT_TRUE(settings->GetClientID(&client_ids[2])); |
| 148 EXPECT_EQ(client_ids[0], client_ids[2]); |
| 149 |
| 150 ASSERT_TRUE(settings->GetLastUploadAttemptTime(&last_upload_attempt_time)); |
| 151 EXPECT_EQ(0, last_upload_attempt_time); |
| 152 |
141 std::vector<CrashReportDatabase::Report> reports; | 153 std::vector<CrashReportDatabase::Report> reports; |
142 EXPECT_EQ(CrashReportDatabase::kNoError, db->GetPendingReports(&reports)); | 154 EXPECT_EQ(CrashReportDatabase::kNoError, db->GetPendingReports(&reports)); |
143 EXPECT_TRUE(reports.empty()); | 155 EXPECT_TRUE(reports.empty()); |
144 reports.clear(); | 156 reports.clear(); |
145 EXPECT_EQ(CrashReportDatabase::kNoError, db->GetCompletedReports(&reports)); | 157 EXPECT_EQ(CrashReportDatabase::kNoError, db->GetCompletedReports(&reports)); |
146 EXPECT_TRUE(reports.empty()); | 158 EXPECT_TRUE(reports.empty()); |
| 159 |
| 160 // InitializeWithoutCreating() shouldn’t create a nonexistent database. |
| 161 base::FilePath non_database_path = |
| 162 path().DirName().Append(FILE_PATH_LITERAL("not_a_database")); |
| 163 db = CrashReportDatabase::InitializeWithoutCreating(non_database_path); |
| 164 EXPECT_FALSE(db); |
147 } | 165 } |
148 | 166 |
149 TEST_F(CrashReportDatabaseTest, NewCrashReport) { | 167 TEST_F(CrashReportDatabaseTest, NewCrashReport) { |
150 CrashReportDatabase::NewReport* new_report; | 168 CrashReportDatabase::NewReport* new_report; |
151 EXPECT_EQ(CrashReportDatabase::kNoError, | 169 EXPECT_EQ(CrashReportDatabase::kNoError, |
152 db()->PrepareNewCrashReport(&new_report)); | 170 db()->PrepareNewCrashReport(&new_report)); |
153 UUID expect_uuid = new_report->uuid; | 171 UUID expect_uuid = new_report->uuid; |
154 EXPECT_TRUE(FileExists(new_report->path)) << new_report->path.value(); | 172 EXPECT_TRUE(FileExists(new_report->path)) << new_report->path.value(); |
155 UUID uuid; | 173 UUID uuid; |
156 EXPECT_EQ(CrashReportDatabase::kNoError, | 174 EXPECT_EQ(CrashReportDatabase::kNoError, |
157 db()->FinishedWritingCrashReport(new_report, &uuid)); | 175 db()->FinishedWritingCrashReport(new_report, &uuid)); |
158 EXPECT_EQ(expect_uuid, uuid); | 176 EXPECT_EQ(expect_uuid, uuid); |
159 | 177 |
160 CrashReportDatabase::Report report; | 178 CrashReportDatabase::Report report; |
161 EXPECT_EQ(CrashReportDatabase::kNoError, | 179 EXPECT_EQ(CrashReportDatabase::kNoError, |
162 db()->LookUpCrashReport(uuid, &report)); | 180 db()->LookUpCrashReport(uuid, &report)); |
163 ExpectPreparedCrashReport(report); | 181 ExpectPreparedCrashReport(report); |
164 | 182 |
165 std::vector<CrashReportDatabase::Report> reports; | 183 std::vector<CrashReportDatabase::Report> reports; |
166 EXPECT_EQ(CrashReportDatabase::kNoError, | 184 EXPECT_EQ(CrashReportDatabase::kNoError, db()->GetPendingReports(&reports)); |
167 db()->GetPendingReports(&reports)); | |
168 ASSERT_EQ(1u, reports.size()); | 185 ASSERT_EQ(1u, reports.size()); |
169 EXPECT_EQ(report.uuid, reports[0].uuid); | 186 EXPECT_EQ(report.uuid, reports[0].uuid); |
170 | 187 |
171 reports.clear(); | 188 reports.clear(); |
172 EXPECT_EQ(CrashReportDatabase::kNoError, | 189 EXPECT_EQ(CrashReportDatabase::kNoError, db()->GetCompletedReports(&reports)); |
173 db()->GetCompletedReports(&reports)); | |
174 EXPECT_TRUE(reports.empty()); | 190 EXPECT_TRUE(reports.empty()); |
175 } | 191 } |
176 | 192 |
177 TEST_F(CrashReportDatabaseTest, ErrorWritingCrashReport) { | 193 TEST_F(CrashReportDatabaseTest, ErrorWritingCrashReport) { |
178 CrashReportDatabase::NewReport* new_report = nullptr; | 194 CrashReportDatabase::NewReport* new_report = nullptr; |
179 ASSERT_EQ(CrashReportDatabase::kNoError, | 195 ASSERT_EQ(CrashReportDatabase::kNoError, |
180 db()->PrepareNewCrashReport(&new_report)); | 196 db()->PrepareNewCrashReport(&new_report)); |
181 base::FilePath new_report_path = new_report->path; | 197 base::FilePath new_report_path = new_report->path; |
182 EXPECT_TRUE(FileExists(new_report_path)) << new_report_path.value(); | 198 EXPECT_TRUE(FileExists(new_report_path)) << new_report_path.value(); |
183 EXPECT_EQ(CrashReportDatabase::kNoError, | 199 EXPECT_EQ(CrashReportDatabase::kNoError, |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 CreateCrashReport(&reports[3]); | 329 CreateCrashReport(&reports[3]); |
314 CreateCrashReport(&reports[4]); | 330 CreateCrashReport(&reports[4]); |
315 | 331 |
316 const UUID& report_0_uuid = reports[0].uuid; | 332 const UUID& report_0_uuid = reports[0].uuid; |
317 const UUID& report_1_uuid = reports[1].uuid; | 333 const UUID& report_1_uuid = reports[1].uuid; |
318 const UUID& report_2_uuid = reports[2].uuid; | 334 const UUID& report_2_uuid = reports[2].uuid; |
319 const UUID& report_3_uuid = reports[3].uuid; | 335 const UUID& report_3_uuid = reports[3].uuid; |
320 const UUID& report_4_uuid = reports[4].uuid; | 336 const UUID& report_4_uuid = reports[4].uuid; |
321 | 337 |
322 std::vector<CrashReportDatabase::Report> pending; | 338 std::vector<CrashReportDatabase::Report> pending; |
323 EXPECT_EQ(CrashReportDatabase::kNoError, | 339 EXPECT_EQ(CrashReportDatabase::kNoError, db()->GetPendingReports(&pending)); |
324 db()->GetPendingReports(&pending)); | |
325 | 340 |
326 std::vector<CrashReportDatabase::Report> completed; | 341 std::vector<CrashReportDatabase::Report> completed; |
327 EXPECT_EQ(CrashReportDatabase::kNoError, | 342 EXPECT_EQ(CrashReportDatabase::kNoError, |
328 db()->GetCompletedReports(&completed)); | 343 db()->GetCompletedReports(&completed)); |
329 | 344 |
330 EXPECT_EQ(reports.size(), pending.size()); | 345 EXPECT_EQ(reports.size(), pending.size()); |
331 EXPECT_EQ(0u, completed.size()); | 346 EXPECT_EQ(0u, completed.size()); |
332 | 347 |
333 // Upload one report successfully. | 348 // Upload one report successfully. |
334 UploadReport(report_1_uuid, true, "report1"); | 349 UploadReport(report_1_uuid, true, "report1"); |
335 | 350 |
336 pending.clear(); | 351 pending.clear(); |
337 EXPECT_EQ(CrashReportDatabase::kNoError, | 352 EXPECT_EQ(CrashReportDatabase::kNoError, db()->GetPendingReports(&pending)); |
338 db()->GetPendingReports(&pending)); | |
339 completed.clear(); | 353 completed.clear(); |
340 EXPECT_EQ(CrashReportDatabase::kNoError, | 354 EXPECT_EQ(CrashReportDatabase::kNoError, |
341 db()->GetCompletedReports(&completed)); | 355 db()->GetCompletedReports(&completed)); |
342 | 356 |
343 EXPECT_EQ(4u, pending.size()); | 357 EXPECT_EQ(4u, pending.size()); |
344 ASSERT_EQ(1u, completed.size()); | 358 ASSERT_EQ(1u, completed.size()); |
345 | 359 |
346 for (const auto& report : pending) { | 360 for (const auto& report : pending) { |
347 EXPECT_NE(report_1_uuid, report.uuid); | 361 EXPECT_NE(report_1_uuid, report.uuid); |
348 EXPECT_FALSE(report.file_path.empty()); | 362 EXPECT_FALSE(report.file_path.empty()); |
349 } | 363 } |
350 EXPECT_EQ(report_1_uuid, completed[0].uuid); | 364 EXPECT_EQ(report_1_uuid, completed[0].uuid); |
351 EXPECT_EQ("report1", completed[0].id); | 365 EXPECT_EQ("report1", completed[0].id); |
352 EXPECT_EQ(true, completed[0].uploaded); | 366 EXPECT_EQ(true, completed[0].uploaded); |
353 EXPECT_GT(completed[0].last_upload_attempt_time, 0); | 367 EXPECT_GT(completed[0].last_upload_attempt_time, 0); |
354 EXPECT_EQ(1, completed[0].upload_attempts); | 368 EXPECT_EQ(1, completed[0].upload_attempts); |
355 | 369 |
356 // Fail to upload one report. | 370 // Fail to upload one report. |
357 UploadReport(report_2_uuid, false, std::string()); | 371 UploadReport(report_2_uuid, false, std::string()); |
358 | 372 |
359 pending.clear(); | 373 pending.clear(); |
360 EXPECT_EQ(CrashReportDatabase::kNoError, | 374 EXPECT_EQ(CrashReportDatabase::kNoError, db()->GetPendingReports(&pending)); |
361 db()->GetPendingReports(&pending)); | |
362 completed.clear(); | 375 completed.clear(); |
363 EXPECT_EQ(CrashReportDatabase::kNoError, | 376 EXPECT_EQ(CrashReportDatabase::kNoError, |
364 db()->GetCompletedReports(&completed)); | 377 db()->GetCompletedReports(&completed)); |
365 | 378 |
366 EXPECT_EQ(4u, pending.size()); | 379 EXPECT_EQ(4u, pending.size()); |
367 ASSERT_EQ(1u, completed.size()); | 380 ASSERT_EQ(1u, completed.size()); |
368 | 381 |
369 for (const auto& report : pending) { | 382 for (const auto& report : pending) { |
370 if (report.upload_attempts != 0) { | 383 if (report.upload_attempts != 0) { |
371 EXPECT_EQ(report_2_uuid, report.uuid); | 384 EXPECT_EQ(report_2_uuid, report.uuid); |
372 EXPECT_GT(report.last_upload_attempt_time, 0); | 385 EXPECT_GT(report.last_upload_attempt_time, 0); |
373 EXPECT_FALSE(report.uploaded); | 386 EXPECT_FALSE(report.uploaded); |
374 EXPECT_TRUE(report.id.empty()); | 387 EXPECT_TRUE(report.id.empty()); |
375 } | 388 } |
376 EXPECT_FALSE(report.file_path.empty()); | 389 EXPECT_FALSE(report.file_path.empty()); |
377 } | 390 } |
378 | 391 |
379 // Upload a second report. | 392 // Upload a second report. |
380 UploadReport(report_4_uuid, true, "report_4"); | 393 UploadReport(report_4_uuid, true, "report_4"); |
381 | 394 |
382 pending.clear(); | 395 pending.clear(); |
383 EXPECT_EQ(CrashReportDatabase::kNoError, | 396 EXPECT_EQ(CrashReportDatabase::kNoError, db()->GetPendingReports(&pending)); |
384 db()->GetPendingReports(&pending)); | |
385 completed.clear(); | 397 completed.clear(); |
386 EXPECT_EQ(CrashReportDatabase::kNoError, | 398 EXPECT_EQ(CrashReportDatabase::kNoError, |
387 db()->GetCompletedReports(&completed)); | 399 db()->GetCompletedReports(&completed)); |
388 | 400 |
389 EXPECT_EQ(3u, pending.size()); | 401 EXPECT_EQ(3u, pending.size()); |
390 ASSERT_EQ(2u, completed.size()); | 402 ASSERT_EQ(2u, completed.size()); |
391 | 403 |
392 // Succeed the failed report. | 404 // Succeed the failed report. |
393 UploadReport(report_2_uuid, true, "report 2"); | 405 UploadReport(report_2_uuid, true, "report 2"); |
394 | 406 |
395 pending.clear(); | 407 pending.clear(); |
396 EXPECT_EQ(CrashReportDatabase::kNoError, | 408 EXPECT_EQ(CrashReportDatabase::kNoError, db()->GetPendingReports(&pending)); |
397 db()->GetPendingReports(&pending)); | |
398 completed.clear(); | 409 completed.clear(); |
399 EXPECT_EQ(CrashReportDatabase::kNoError, | 410 EXPECT_EQ(CrashReportDatabase::kNoError, |
400 db()->GetCompletedReports(&completed)); | 411 db()->GetCompletedReports(&completed)); |
401 | 412 |
402 EXPECT_EQ(2u, pending.size()); | 413 EXPECT_EQ(2u, pending.size()); |
403 ASSERT_EQ(3u, completed.size()); | 414 ASSERT_EQ(3u, completed.size()); |
404 | 415 |
405 for (const auto& report : pending) { | 416 for (const auto& report : pending) { |
406 EXPECT_TRUE(report.uuid == report_0_uuid || | 417 EXPECT_TRUE(report.uuid == report_0_uuid || report.uuid == report_3_uuid); |
407 report.uuid == report_3_uuid); | |
408 EXPECT_FALSE(report.file_path.empty()); | 418 EXPECT_FALSE(report.file_path.empty()); |
409 } | 419 } |
410 | 420 |
411 // Skip upload for one report. | 421 // Skip upload for one report. |
412 EXPECT_EQ(CrashReportDatabase::kNoError, | 422 EXPECT_EQ(CrashReportDatabase::kNoError, |
413 db()->SkipReportUpload(report_3_uuid)); | 423 db()->SkipReportUpload(report_3_uuid)); |
414 | 424 |
415 pending.clear(); | 425 pending.clear(); |
416 EXPECT_EQ(CrashReportDatabase::kNoError, | 426 EXPECT_EQ(CrashReportDatabase::kNoError, db()->GetPendingReports(&pending)); |
417 db()->GetPendingReports(&pending)); | |
418 completed.clear(); | 427 completed.clear(); |
419 EXPECT_EQ(CrashReportDatabase::kNoError, | 428 EXPECT_EQ(CrashReportDatabase::kNoError, |
420 db()->GetCompletedReports(&completed)); | 429 db()->GetCompletedReports(&completed)); |
421 | 430 |
422 ASSERT_EQ(1u, pending.size()); | 431 ASSERT_EQ(1u, pending.size()); |
423 ASSERT_EQ(4u, completed.size()); | 432 ASSERT_EQ(4u, completed.size()); |
424 | 433 |
425 EXPECT_EQ(report_0_uuid, pending[0].uuid); | 434 EXPECT_EQ(report_0_uuid, pending[0].uuid); |
426 | 435 |
427 for (const auto& report : completed) { | 436 for (const auto& report : completed) { |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
542 | 551 |
543 EXPECT_EQ(CrashReportDatabase::kNoError, | 552 EXPECT_EQ(CrashReportDatabase::kNoError, |
544 db()->LookUpCrashReport(keep_pending.uuid, &keep_pending)); | 553 db()->LookUpCrashReport(keep_pending.uuid, &keep_pending)); |
545 EXPECT_EQ(CrashReportDatabase::kNoError, | 554 EXPECT_EQ(CrashReportDatabase::kNoError, |
546 db()->LookUpCrashReport(keep_completed.uuid, &keep_completed)); | 555 db()->LookUpCrashReport(keep_completed.uuid, &keep_completed)); |
547 } | 556 } |
548 | 557 |
549 } // namespace | 558 } // namespace |
550 } // namespace test | 559 } // namespace test |
551 } // namespace crashpad | 560 } // namespace crashpad |
OLD | NEW |