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

Side by Side Diff: client/crash_report_database_test.cc

Issue 1390023002: Add FileWriteMode::kCreateOrFail (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Test that kReuseOrCreate can create Created 5 years, 2 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
« no previous file with comments | « no previous file | test/file.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 #include "client/crash_report_database.h" 15 #include "client/crash_report_database.h"
16 16
17 #include <sys/stat.h>
18
19 #include "build/build_config.h" 17 #include "build/build_config.h"
20 #include "client/settings.h" 18 #include "client/settings.h"
21 #include "gtest/gtest.h" 19 #include "gtest/gtest.h"
22 #include "test/errors.h" 20 #include "test/errors.h"
21 #include "test/file.h"
23 #include "test/scoped_temp_dir.h" 22 #include "test/scoped_temp_dir.h"
24 #include "util/file/file_io.h" 23 #include "util/file/file_io.h"
25 24
26 namespace crashpad { 25 namespace crashpad {
27 namespace test { 26 namespace test {
28 namespace { 27 namespace {
29 28
30 bool FileExistsAtPath(const base::FilePath& path) {
31 #if defined(OS_POSIX)
32 struct stat st;
33 return lstat(path.value().c_str(), &st) == 0;
34 #elif defined(OS_WIN)
35 struct _stat st;
36 return _wstat(path.value().c_str(), &st) == 0;
37 #else
38 #error "Not implemented"
39 #endif
40 }
41
42 class CrashReportDatabaseTest : public testing::Test { 29 class CrashReportDatabaseTest : public testing::Test {
43 public: 30 public:
44 CrashReportDatabaseTest() { 31 CrashReportDatabaseTest() {
45 } 32 }
46 33
47 protected: 34 protected:
48 // testing::Test: 35 // testing::Test:
49 void SetUp() override { 36 void SetUp() override {
50 db_ = CrashReportDatabase::Initialize(path()); 37 db_ = CrashReportDatabase::Initialize(path());
51 ASSERT_TRUE(db_); 38 ASSERT_TRUE(db_);
(...skipping 15 matching lines...) Expand all
67 const char kTest[] = "test"; 54 const char kTest[] = "test";
68 ASSERT_TRUE(LoggingWriteFile(new_report->handle, kTest, sizeof(kTest))); 55 ASSERT_TRUE(LoggingWriteFile(new_report->handle, kTest, sizeof(kTest)));
69 56
70 UUID uuid; 57 UUID uuid;
71 EXPECT_EQ(CrashReportDatabase::kNoError, 58 EXPECT_EQ(CrashReportDatabase::kNoError,
72 db_->FinishedWritingCrashReport(new_report, &uuid)); 59 db_->FinishedWritingCrashReport(new_report, &uuid));
73 60
74 EXPECT_EQ(CrashReportDatabase::kNoError, 61 EXPECT_EQ(CrashReportDatabase::kNoError,
75 db_->LookUpCrashReport(uuid, report)); 62 db_->LookUpCrashReport(uuid, report));
76 ExpectPreparedCrashReport(*report); 63 ExpectPreparedCrashReport(*report);
77 ASSERT_TRUE(FileExistsAtPath(report->file_path)); 64 ASSERT_TRUE(FileExists(report->file_path));
78 } 65 }
79 66
80 void UploadReport(const UUID& uuid, bool successful, const std::string& id) { 67 void UploadReport(const UUID& uuid, bool successful, const std::string& id) {
81 #if !defined(OS_WIN) 68 #if !defined(OS_WIN)
82 // Enable when ported to Windows. 69 // Enable when ported to Windows.
83 // https://code.google.com/p/crashpad/issues/detail?id=13 70 // https://code.google.com/p/crashpad/issues/detail?id=13
84 Settings* const settings = db_->GetSettings(); 71 Settings* const settings = db_->GetSettings();
85 ASSERT_TRUE(settings); 72 ASSERT_TRUE(settings);
86 time_t times[2]; 73 time_t times[2];
87 ASSERT_TRUE(settings->GetLastUploadAttemptTime(&times[0])); 74 ASSERT_TRUE(settings->GetLastUploadAttemptTime(&times[0]));
88 #endif 75 #endif
89 76
90 const CrashReportDatabase::Report* report = nullptr; 77 const CrashReportDatabase::Report* report = nullptr;
91 ASSERT_EQ(CrashReportDatabase::kNoError, 78 ASSERT_EQ(CrashReportDatabase::kNoError,
92 db_->GetReportForUploading(uuid, &report)); 79 db_->GetReportForUploading(uuid, &report));
93 EXPECT_NE(UUID(), report->uuid); 80 EXPECT_NE(UUID(), report->uuid);
94 EXPECT_FALSE(report->file_path.empty()); 81 EXPECT_FALSE(report->file_path.empty());
95 EXPECT_TRUE(FileExistsAtPath(report->file_path)) 82 EXPECT_TRUE(FileExists(report->file_path))
96 << report->file_path.value(); 83 << report->file_path.value();
97 EXPECT_GT(report->creation_time, 0); 84 EXPECT_GT(report->creation_time, 0);
98 EXPECT_EQ(CrashReportDatabase::kNoError, 85 EXPECT_EQ(CrashReportDatabase::kNoError,
99 db_->RecordUploadAttempt(report, successful, id)); 86 db_->RecordUploadAttempt(report, successful, id));
100 87
101 #if !defined(OS_WIN) 88 #if !defined(OS_WIN)
102 // Enable when ported to Windows. 89 // Enable when ported to Windows.
103 // https://code.google.com/p/crashpad/issues/detail?id=13 90 // https://code.google.com/p/crashpad/issues/detail?id=13
104 ASSERT_TRUE(settings->GetLastUploadAttemptTime(&times[1])); 91 ASSERT_TRUE(settings->GetLastUploadAttemptTime(&times[1]));
105 EXPECT_NE(times[1], 0); 92 EXPECT_NE(times[1], 0);
106 EXPECT_GE(times[1], times[0]); 93 EXPECT_GE(times[1], times[0]);
107 #endif 94 #endif
108 } 95 }
109 96
110 void ExpectPreparedCrashReport(const CrashReportDatabase::Report& report) { 97 void ExpectPreparedCrashReport(const CrashReportDatabase::Report& report) {
111 EXPECT_NE(UUID(), report.uuid); 98 EXPECT_NE(UUID(), report.uuid);
112 EXPECT_FALSE(report.file_path.empty()); 99 EXPECT_FALSE(report.file_path.empty());
113 EXPECT_TRUE(FileExistsAtPath(report.file_path)) << report.file_path.value(); 100 EXPECT_TRUE(FileExists(report.file_path)) << report.file_path.value();
114 EXPECT_TRUE(report.id.empty()); 101 EXPECT_TRUE(report.id.empty());
115 EXPECT_GT(report.creation_time, 0); 102 EXPECT_GT(report.creation_time, 0);
116 EXPECT_FALSE(report.uploaded); 103 EXPECT_FALSE(report.uploaded);
117 EXPECT_EQ(0, report.last_upload_attempt_time); 104 EXPECT_EQ(0, report.last_upload_attempt_time);
118 EXPECT_EQ(0, report.upload_attempts); 105 EXPECT_EQ(0, report.upload_attempts);
119 } 106 }
120 107
121 void RelocateDatabase() { 108 void RelocateDatabase() {
122 ResetDatabase(); 109 ResetDatabase();
123 temp_dir_.Rename(); 110 temp_dir_.Rename();
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 reports.clear(); 160 reports.clear();
174 EXPECT_EQ(CrashReportDatabase::kNoError, db->GetCompletedReports(&reports)); 161 EXPECT_EQ(CrashReportDatabase::kNoError, db->GetCompletedReports(&reports));
175 EXPECT_TRUE(reports.empty()); 162 EXPECT_TRUE(reports.empty());
176 } 163 }
177 164
178 TEST_F(CrashReportDatabaseTest, NewCrashReport) { 165 TEST_F(CrashReportDatabaseTest, NewCrashReport) {
179 CrashReportDatabase::NewReport* new_report; 166 CrashReportDatabase::NewReport* new_report;
180 EXPECT_EQ(CrashReportDatabase::kNoError, 167 EXPECT_EQ(CrashReportDatabase::kNoError,
181 db()->PrepareNewCrashReport(&new_report)); 168 db()->PrepareNewCrashReport(&new_report));
182 UUID expect_uuid = new_report->uuid; 169 UUID expect_uuid = new_report->uuid;
183 EXPECT_TRUE(FileExistsAtPath(new_report->path)) << new_report->path.value(); 170 EXPECT_TRUE(FileExists(new_report->path)) << new_report->path.value();
184 UUID uuid; 171 UUID uuid;
185 EXPECT_EQ(CrashReportDatabase::kNoError, 172 EXPECT_EQ(CrashReportDatabase::kNoError,
186 db()->FinishedWritingCrashReport(new_report, &uuid)); 173 db()->FinishedWritingCrashReport(new_report, &uuid));
187 EXPECT_EQ(expect_uuid, uuid); 174 EXPECT_EQ(expect_uuid, uuid);
188 175
189 CrashReportDatabase::Report report; 176 CrashReportDatabase::Report report;
190 EXPECT_EQ(CrashReportDatabase::kNoError, 177 EXPECT_EQ(CrashReportDatabase::kNoError,
191 db()->LookUpCrashReport(uuid, &report)); 178 db()->LookUpCrashReport(uuid, &report));
192 ExpectPreparedCrashReport(report); 179 ExpectPreparedCrashReport(report);
193 180
194 std::vector<CrashReportDatabase::Report> reports; 181 std::vector<CrashReportDatabase::Report> reports;
195 EXPECT_EQ(CrashReportDatabase::kNoError, 182 EXPECT_EQ(CrashReportDatabase::kNoError,
196 db()->GetPendingReports(&reports)); 183 db()->GetPendingReports(&reports));
197 ASSERT_EQ(1u, reports.size()); 184 ASSERT_EQ(1u, reports.size());
198 EXPECT_EQ(report.uuid, reports[0].uuid); 185 EXPECT_EQ(report.uuid, reports[0].uuid);
199 186
200 reports.clear(); 187 reports.clear();
201 EXPECT_EQ(CrashReportDatabase::kNoError, 188 EXPECT_EQ(CrashReportDatabase::kNoError,
202 db()->GetCompletedReports(&reports)); 189 db()->GetCompletedReports(&reports));
203 EXPECT_TRUE(reports.empty()); 190 EXPECT_TRUE(reports.empty());
204 } 191 }
205 192
206 TEST_F(CrashReportDatabaseTest, ErrorWritingCrashReport) { 193 TEST_F(CrashReportDatabaseTest, ErrorWritingCrashReport) {
207 CrashReportDatabase::NewReport* new_report = nullptr; 194 CrashReportDatabase::NewReport* new_report = nullptr;
208 ASSERT_EQ(CrashReportDatabase::kNoError, 195 ASSERT_EQ(CrashReportDatabase::kNoError,
209 db()->PrepareNewCrashReport(&new_report)); 196 db()->PrepareNewCrashReport(&new_report));
210 base::FilePath new_report_path = new_report->path; 197 base::FilePath new_report_path = new_report->path;
211 EXPECT_TRUE(FileExistsAtPath(new_report_path)) << new_report_path.value(); 198 EXPECT_TRUE(FileExists(new_report_path)) << new_report_path.value();
212 EXPECT_EQ(CrashReportDatabase::kNoError, 199 EXPECT_EQ(CrashReportDatabase::kNoError,
213 db()->ErrorWritingCrashReport(new_report)); 200 db()->ErrorWritingCrashReport(new_report));
214 EXPECT_FALSE(FileExistsAtPath(new_report_path)) << new_report_path.value(); 201 EXPECT_FALSE(FileExists(new_report_path)) << new_report_path.value();
215 } 202 }
216 203
217 TEST_F(CrashReportDatabaseTest, LookUpCrashReport) { 204 TEST_F(CrashReportDatabaseTest, LookUpCrashReport) {
218 UUID uuid; 205 UUID uuid;
219 206
220 { 207 {
221 CrashReportDatabase::Report report; 208 CrashReportDatabase::Report report;
222 CreateCrashReport(&report); 209 CreateCrashReport(&report);
223 uuid = report.uuid; 210 uuid = report.uuid;
224 } 211 }
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
481 EXPECT_FALSE(upload_report_2); 468 EXPECT_FALSE(upload_report_2);
482 469
483 EXPECT_EQ(CrashReportDatabase::kNoError, 470 EXPECT_EQ(CrashReportDatabase::kNoError,
484 db()->RecordUploadAttempt(upload_report, true, std::string())); 471 db()->RecordUploadAttempt(upload_report, true, std::string()));
485 } 472 }
486 473
487 TEST_F(CrashReportDatabaseTest, MoveDatabase) { 474 TEST_F(CrashReportDatabaseTest, MoveDatabase) {
488 CrashReportDatabase::NewReport* new_report; 475 CrashReportDatabase::NewReport* new_report;
489 EXPECT_EQ(CrashReportDatabase::kNoError, 476 EXPECT_EQ(CrashReportDatabase::kNoError,
490 db()->PrepareNewCrashReport(&new_report)); 477 db()->PrepareNewCrashReport(&new_report));
491 EXPECT_TRUE(FileExistsAtPath(new_report->path)) << new_report->path.value(); 478 EXPECT_TRUE(FileExists(new_report->path)) << new_report->path.value();
492 UUID uuid; 479 UUID uuid;
493 EXPECT_EQ(CrashReportDatabase::kNoError, 480 EXPECT_EQ(CrashReportDatabase::kNoError,
494 db()->FinishedWritingCrashReport(new_report, &uuid)); 481 db()->FinishedWritingCrashReport(new_report, &uuid));
495 482
496 RelocateDatabase(); 483 RelocateDatabase();
497 484
498 CrashReportDatabase::Report report; 485 CrashReportDatabase::Report report;
499 EXPECT_EQ(CrashReportDatabase::kNoError, 486 EXPECT_EQ(CrashReportDatabase::kNoError,
500 db()->LookUpCrashReport(uuid, &report)); 487 db()->LookUpCrashReport(uuid, &report));
501 ExpectPreparedCrashReport(report); 488 ExpectPreparedCrashReport(report);
502 EXPECT_TRUE(FileExistsAtPath(report.file_path)) << report.file_path.value(); 489 EXPECT_TRUE(FileExists(report.file_path)) << report.file_path.value();
503 } 490 }
504 491
505 TEST_F(CrashReportDatabaseTest, ReportRemoved) { 492 TEST_F(CrashReportDatabaseTest, ReportRemoved) {
506 CrashReportDatabase::NewReport* new_report; 493 CrashReportDatabase::NewReport* new_report;
507 EXPECT_EQ(CrashReportDatabase::kNoError, 494 EXPECT_EQ(CrashReportDatabase::kNoError,
508 db()->PrepareNewCrashReport(&new_report)); 495 db()->PrepareNewCrashReport(&new_report));
509 EXPECT_TRUE(FileExistsAtPath(new_report->path)) << new_report->path.value(); 496 EXPECT_TRUE(FileExists(new_report->path)) << new_report->path.value();
510 UUID uuid; 497 UUID uuid;
511 EXPECT_EQ(CrashReportDatabase::kNoError, 498 EXPECT_EQ(CrashReportDatabase::kNoError,
512 db()->FinishedWritingCrashReport(new_report, &uuid)); 499 db()->FinishedWritingCrashReport(new_report, &uuid));
513 500
514 CrashReportDatabase::Report report; 501 CrashReportDatabase::Report report;
515 EXPECT_EQ(CrashReportDatabase::kNoError, 502 EXPECT_EQ(CrashReportDatabase::kNoError,
516 db()->LookUpCrashReport(uuid, &report)); 503 db()->LookUpCrashReport(uuid, &report));
517 504
518 #if defined(OS_WIN) 505 #if defined(OS_WIN)
519 EXPECT_EQ(0, _wunlink(report.file_path.value().c_str())); 506 EXPECT_EQ(0, _wunlink(report.file_path.value().c_str()));
520 #else 507 #else
521 EXPECT_EQ(0, unlink(report.file_path.value().c_str())) 508 EXPECT_EQ(0, unlink(report.file_path.value().c_str()))
522 << ErrnoMessage("unlink"); 509 << ErrnoMessage("unlink");
523 #endif 510 #endif
524 511
525 EXPECT_EQ(CrashReportDatabase::kReportNotFound, 512 EXPECT_EQ(CrashReportDatabase::kReportNotFound,
526 db()->LookUpCrashReport(uuid, &report)); 513 db()->LookUpCrashReport(uuid, &report));
527 } 514 }
528 515
529 } // namespace 516 } // namespace
530 } // namespace test 517 } // namespace test
531 } // namespace crashpad 518 } // namespace crashpad
OLDNEW
« no previous file with comments | « no previous file | test/file.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698