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

Side by Side Diff: content/browser/download/base_file_unittest.cc

Issue 2695153002: Refactor BaseFile class to support sparse files (Closed)
Patch Set: remove the while loop around write Created 3 years, 10 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/download/base_file.h" 5 #include "content/browser/download/base_file.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 13 matching lines...) Expand all
24 24
25 namespace content { 25 namespace content {
26 namespace { 26 namespace {
27 27
28 const char kTestData1[] = "Let's write some data to the file!\n"; 28 const char kTestData1[] = "Let's write some data to the file!\n";
29 const char kTestData2[] = "Writing more data.\n"; 29 const char kTestData2[] = "Writing more data.\n";
30 const char kTestData3[] = "Final line."; 30 const char kTestData3[] = "Final line.";
31 const char kTestData4[] = "supercalifragilisticexpialidocious"; 31 const char kTestData4[] = "supercalifragilisticexpialidocious";
32 const int kTestDataLength1 = arraysize(kTestData1) - 1; 32 const int kTestDataLength1 = arraysize(kTestData1) - 1;
33 const int kTestDataLength2 = arraysize(kTestData2) - 1; 33 const int kTestDataLength2 = arraysize(kTestData2) - 1;
34 const int kTestDataLength3 = arraysize(kTestData3) - 1;
34 const int kTestDataLength4 = arraysize(kTestData4) - 1; 35 const int kTestDataLength4 = arraysize(kTestData4) - 1;
35 36
36 // SHA-256 hash of kTestData1 (excluding terminating NUL). 37 // SHA-256 hash of kTestData1 (excluding terminating NUL).
37 const uint8_t kHashOfTestData1[] = { 38 const uint8_t kHashOfTestData1[] = {
38 0x0b, 0x2d, 0x3f, 0x3f, 0x79, 0x43, 0xad, 0x64, 0xb8, 0x60, 0xdf, 39 0x0b, 0x2d, 0x3f, 0x3f, 0x79, 0x43, 0xad, 0x64, 0xb8, 0x60, 0xdf,
39 0x94, 0xd0, 0x5c, 0xb5, 0x6a, 0x8a, 0x97, 0xc6, 0xec, 0x57, 0x68, 40 0x94, 0xd0, 0x5c, 0xb5, 0x6a, 0x8a, 0x97, 0xc6, 0xec, 0x57, 0x68,
40 0xb5, 0xb7, 0x0b, 0x93, 0x0c, 0x5a, 0xa7, 0xfa, 0x9a, 0xde}; 41 0xb5, 0xb7, 0x0b, 0x93, 0x0c, 0x5a, 0xa7, 0xfa, 0x9a, 0xde};
41 42
42 // SHA-256 hash of kTestData1 ++ kTestData2 ++ kTestData3 (excluding terminating 43 // SHA-256 hash of kTestData1 ++ kTestData2 ++ kTestData3 (excluding terminating
43 // NUL). 44 // NUL).
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 // Make sure the mock BrowserThread outlives the BaseFile to satisfy 82 // Make sure the mock BrowserThread outlives the BaseFile to satisfy
82 // thread checks inside it. 83 // thread checks inside it.
83 base_file_.reset(); 84 base_file_.reset();
84 85
85 EXPECT_EQ(expect_file_survives_, base::PathExists(full_path)); 86 EXPECT_EQ(expect_file_survives_, base::PathExists(full_path));
86 } 87 }
87 88
88 bool InitializeFile() { 89 bool InitializeFile() {
89 DownloadInterruptReason result = base_file_->Initialize( 90 DownloadInterruptReason result = base_file_->Initialize(
90 base::FilePath(), temp_dir_.GetPath(), base::File(), 0, std::string(), 91 base::FilePath(), temp_dir_.GetPath(), base::File(), 0, std::string(),
91 std::unique_ptr<crypto::SecureHash>()); 92 std::unique_ptr<crypto::SecureHash>(), false);
92 EXPECT_EQ(expected_error_, result); 93 EXPECT_EQ(expected_error_, result);
93 return result == DOWNLOAD_INTERRUPT_REASON_NONE; 94 return result == DOWNLOAD_INTERRUPT_REASON_NONE;
94 } 95 }
95 96
96 bool AppendDataToFile(const std::string& data) { 97 bool AppendDataToFile(const std::string& data) {
97 EXPECT_EQ(expect_in_progress_, base_file_->in_progress()); 98 EXPECT_EQ(expect_in_progress_, base_file_->in_progress());
98 DownloadInterruptReason result = 99 DownloadInterruptReason result =
99 base_file_->AppendDataToFile(data.data(), data.size()); 100 base_file_->AppendDataToFile(data.data(), data.size());
100 if (result == DOWNLOAD_INTERRUPT_REASON_NONE) 101 if (result == DOWNLOAD_INTERRUPT_REASON_NONE)
101 EXPECT_TRUE(expect_in_progress_) << " result = " << result; 102 EXPECT_TRUE(expect_in_progress_) << " result = " << result;
(...skipping 13 matching lines...) Expand all
115 116
116 // Helper functions. 117 // Helper functions.
117 // Create a file. Returns the complete file path. 118 // Create a file. Returns the complete file path.
118 base::FilePath CreateTestFile() { 119 base::FilePath CreateTestFile() {
119 base::FilePath file_name; 120 base::FilePath file_name;
120 BaseFile file((net::NetLogWithSource())); 121 BaseFile file((net::NetLogWithSource()));
121 122
122 EXPECT_EQ( 123 EXPECT_EQ(
123 DOWNLOAD_INTERRUPT_REASON_NONE, 124 DOWNLOAD_INTERRUPT_REASON_NONE,
124 file.Initialize(base::FilePath(), temp_dir_.GetPath(), base::File(), 0, 125 file.Initialize(base::FilePath(), temp_dir_.GetPath(), base::File(), 0,
125 std::string(), std::unique_ptr<crypto::SecureHash>())); 126 std::string(), std::unique_ptr<crypto::SecureHash>(),
127 false));
126 file_name = file.full_path(); 128 file_name = file.full_path();
127 EXPECT_NE(base::FilePath::StringType(), file_name.value()); 129 EXPECT_NE(base::FilePath::StringType(), file_name.value());
128 130
129 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 131 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
130 file.AppendDataToFile(kTestData4, kTestDataLength4)); 132 file.AppendDataToFile(kTestData4, kTestDataLength4));
131 133
132 // Keep the file from getting deleted when existing_file_name is deleted. 134 // Keep the file from getting deleted when existing_file_name is deleted.
133 file.Detach(); 135 file.Detach();
134 136
135 return file_name; 137 return file_name;
136 } 138 }
137 139
138 // Create a file with the specified file name. 140 // Create a file with the specified file name.
139 void CreateFileWithName(const base::FilePath& file_name) { 141 void CreateFileWithName(const base::FilePath& file_name) {
140 EXPECT_NE(base::FilePath::StringType(), file_name.value()); 142 EXPECT_NE(base::FilePath::StringType(), file_name.value());
141 BaseFile duplicate_file((net::NetLogWithSource())); 143 BaseFile duplicate_file((net::NetLogWithSource()));
142 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 144 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
143 duplicate_file.Initialize(file_name, temp_dir_.GetPath(), 145 duplicate_file.Initialize(file_name, temp_dir_.GetPath(),
144 base::File(), 0, std::string(), 146 base::File(), 0, std::string(),
145 std::unique_ptr<crypto::SecureHash>())); 147 std::unique_ptr<crypto::SecureHash>(),
148 false));
146 // Write something into it. 149 // Write something into it.
147 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); 150 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4);
148 // Detach the file so it isn't deleted on destruction of |duplicate_file|. 151 // Detach the file so it isn't deleted on destruction of |duplicate_file|.
149 duplicate_file.Detach(); 152 duplicate_file.Detach();
150 } 153 }
151 154
152 int64_t CurrentSpeedAtTime(base::TimeTicks current_time) { 155 int64_t CurrentSpeedAtTime(base::TimeTicks current_time) {
153 EXPECT_TRUE(base_file_.get()); 156 EXPECT_TRUE(base_file_.get());
154 return base_file_->CurrentSpeedAtTime(current_time); 157 return base_file_->CurrentSpeedAtTime(current_time);
155 } 158 }
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 ASSERT_TRUE(base::CopyFile(base_file_->full_path(), new_file_path)); 292 ASSERT_TRUE(base::CopyFile(base_file_->full_path(), new_file_path));
290 293
291 // Create another file 294 // Create another file
292 BaseFile second_file((net::NetLogWithSource())); 295 BaseFile second_file((net::NetLogWithSource()));
293 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 296 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
294 second_file.Initialize(new_file_path, 297 second_file.Initialize(new_file_path,
295 base::FilePath(), 298 base::FilePath(),
296 base::File(), 299 base::File(),
297 base_file_->bytes_so_far(), 300 base_file_->bytes_so_far(),
298 std::string(), 301 std::string(),
299 std::move(hash_state))); 302 std::move(hash_state),
303 false));
300 std::string data(kTestData3); 304 std::string data(kTestData3);
301 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 305 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
302 second_file.AppendDataToFile(data.data(), data.size())); 306 second_file.AppendDataToFile(data.data(), data.size()));
303 ExpectHashValue(kHashOfTestData1To3, second_file.Finish()); 307 ExpectHashValue(kHashOfTestData1To3, second_file.Finish());
304 } 308 }
305 309
306 // Rename the file after all writes to it. 310 // Rename the file after all writes to it.
307 TEST_F(BaseFileTest, WriteThenRename) { 311 TEST_F(BaseFileTest, WriteThenRename) {
308 ASSERT_TRUE(InitializeFile()); 312 ASSERT_TRUE(InitializeFile());
309 313
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 base::FilePath path; 415 base::FilePath path;
412 ASSERT_TRUE(base::CreateTemporaryFile(&path)); 416 ASSERT_TRUE(base::CreateTemporaryFile(&path));
413 417
414 // Pass a file handle which was opened without the WRITE flag. 418 // Pass a file handle which was opened without the WRITE flag.
415 // This should result in an error when writing. 419 // This should result in an error when writing.
416 base::File file(path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ); 420 base::File file(path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ);
417 base_file_.reset(new BaseFile(net::NetLogWithSource())); 421 base_file_.reset(new BaseFile(net::NetLogWithSource()));
418 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 422 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
419 base_file_->Initialize(path, base::FilePath(), std::move(file), 0, 423 base_file_->Initialize(path, base::FilePath(), std::move(file), 0,
420 std::string(), 424 std::string(),
421 std::unique_ptr<crypto::SecureHash>())); 425 std::unique_ptr<crypto::SecureHash>(),
426 false));
422 #if defined(OS_WIN) 427 #if defined(OS_WIN)
423 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); 428 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED);
424 #elif defined (OS_POSIX) 429 #elif defined (OS_POSIX)
425 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 430 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED);
426 #endif 431 #endif
427 ASSERT_FALSE(AppendDataToFile(kTestData1)); 432 ASSERT_FALSE(AppendDataToFile(kTestData1));
428 base_file_->Finish(); 433 base_file_->Finish();
429 } 434 }
430 435
431 // Try to write to uninitialized file. 436 // Try to write to uninitialized file.
(...skipping 21 matching lines...) Expand all
453 // Create a new file. 458 // Create a new file.
454 base::FilePath existing_file_name = CreateTestFile(); 459 base::FilePath existing_file_name = CreateTestFile();
455 set_expected_data(kTestData4); 460 set_expected_data(kTestData4);
456 461
457 // Use the file we've just created. 462 // Use the file we've just created.
458 base_file_.reset(new BaseFile(net::NetLogWithSource())); 463 base_file_.reset(new BaseFile(net::NetLogWithSource()));
459 ASSERT_EQ( 464 ASSERT_EQ(
460 DOWNLOAD_INTERRUPT_REASON_NONE, 465 DOWNLOAD_INTERRUPT_REASON_NONE,
461 base_file_->Initialize(existing_file_name, base::FilePath(), base::File(), 466 base_file_->Initialize(existing_file_name, base::FilePath(), base::File(),
462 kTestDataLength4, std::string(), 467 kTestDataLength4, std::string(),
463 std::unique_ptr<crypto::SecureHash>())); 468 std::unique_ptr<crypto::SecureHash>(),
469 false));
464 470
465 const base::FilePath file_name = base_file_->full_path(); 471 const base::FilePath file_name = base_file_->full_path();
466 EXPECT_NE(base::FilePath::StringType(), file_name.value()); 472 EXPECT_NE(base::FilePath::StringType(), file_name.value());
467 473
468 // Write into the file. 474 // Write into the file.
469 EXPECT_TRUE(AppendDataToFile(kTestData1)); 475 EXPECT_TRUE(AppendDataToFile(kTestData1));
470 476
471 base_file_->Finish(); 477 base_file_->Finish();
472 base_file_->Detach(); 478 base_file_->Detach();
473 expect_file_survives_ = true; 479 expect_file_survives_ = true;
474 } 480 }
475 481
476 // Create a read-only file and attempt to write to it. 482 // Create a read-only file and attempt to write to it.
477 TEST_F(BaseFileTest, ReadonlyBaseFile) { 483 TEST_F(BaseFileTest, ReadonlyBaseFile) {
478 // Create a new file. 484 // Create a new file.
479 base::FilePath readonly_file_name = CreateTestFile(); 485 base::FilePath readonly_file_name = CreateTestFile();
480 486
481 // Restore permissions to the file when we are done with this test. 487 // Restore permissions to the file when we are done with this test.
482 base::FilePermissionRestorer restore_permissions(readonly_file_name); 488 base::FilePermissionRestorer restore_permissions(readonly_file_name);
483 489
484 // Make it read-only. 490 // Make it read-only.
485 EXPECT_TRUE(base::MakeFileUnwritable(readonly_file_name)); 491 EXPECT_TRUE(base::MakeFileUnwritable(readonly_file_name));
486 492
487 // Try to overwrite it. 493 // Try to overwrite it.
488 base_file_.reset(new BaseFile(net::NetLogWithSource())); 494 base_file_.reset(new BaseFile(net::NetLogWithSource()));
489 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, 495 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED,
490 base_file_->Initialize(readonly_file_name, base::FilePath(), 496 base_file_->Initialize(readonly_file_name, base::FilePath(),
491 base::File(), 0, std::string(), 497 base::File(), 0, std::string(),
492 std::unique_ptr<crypto::SecureHash>())); 498 std::unique_ptr<crypto::SecureHash>(),
499 false));
493 500
494 expect_in_progress_ = false; 501 expect_in_progress_ = false;
495 502
496 const base::FilePath file_name = base_file_->full_path(); 503 const base::FilePath file_name = base_file_->full_path();
497 EXPECT_NE(base::FilePath::StringType(), file_name.value()); 504 EXPECT_NE(base::FilePath::StringType(), file_name.value());
498 505
499 // Write into the file. 506 // Write into the file.
500 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 507 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED);
501 EXPECT_FALSE(AppendDataToFile(kTestData1)); 508 EXPECT_FALSE(AppendDataToFile(kTestData1));
502 509
503 base_file_->Finish(); 510 base_file_->Finish();
504 base_file_->Detach(); 511 base_file_->Detach();
505 expect_file_survives_ = true; 512 expect_file_survives_ = true;
506 } 513 }
507 514
508 // Open an existing file and continue writing to it. The hash of the partial 515 // Open an existing file and continue writing to it. The hash of the partial
509 // file is known and matches the existing contents. 516 // file is known and matches the existing contents.
510 TEST_F(BaseFileTest, ExistingBaseFileKnownHash) { 517 TEST_F(BaseFileTest, ExistingBaseFileKnownHash) {
511 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); 518 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing");
512 ASSERT_EQ(kTestDataLength1, 519 ASSERT_EQ(kTestDataLength1,
513 base::WriteFile(file_path, kTestData1, kTestDataLength1)); 520 base::WriteFile(file_path, kTestData1, kTestDataLength1));
514 521
515 std::string hash_so_far(std::begin(kHashOfTestData1), 522 std::string hash_so_far(std::begin(kHashOfTestData1),
516 std::end(kHashOfTestData1)); 523 std::end(kHashOfTestData1));
517 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 524 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
518 base_file_->Initialize(file_path, base::FilePath(), base::File(), 525 base_file_->Initialize(file_path, base::FilePath(), base::File(),
519 kTestDataLength1, hash_so_far, 526 kTestDataLength1, hash_so_far,
520 std::unique_ptr<crypto::SecureHash>())); 527 std::unique_ptr<crypto::SecureHash>(),
528 false));
521 set_expected_data(kTestData1); 529 set_expected_data(kTestData1);
522 ASSERT_TRUE(AppendDataToFile(kTestData2)); 530 ASSERT_TRUE(AppendDataToFile(kTestData2));
523 ASSERT_TRUE(AppendDataToFile(kTestData3)); 531 ASSERT_TRUE(AppendDataToFile(kTestData3));
524 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); 532 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish());
525 } 533 }
526 534
527 // Open an existing file and continue writing to it. The hash of the partial 535 // Open an existing file and continue writing to it. The hash of the partial
528 // file is unknown. 536 // file is unknown.
529 TEST_F(BaseFileTest, ExistingBaseFileUnknownHash) { 537 TEST_F(BaseFileTest, ExistingBaseFileUnknownHash) {
530 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); 538 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing");
531 ASSERT_EQ(kTestDataLength1, 539 ASSERT_EQ(kTestDataLength1,
532 base::WriteFile(file_path, kTestData1, kTestDataLength1)); 540 base::WriteFile(file_path, kTestData1, kTestDataLength1));
533 541
534 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 542 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
535 base_file_->Initialize(file_path, base::FilePath(), base::File(), 543 base_file_->Initialize(file_path, base::FilePath(), base::File(),
536 kTestDataLength1, std::string(), 544 kTestDataLength1, std::string(),
537 std::unique_ptr<crypto::SecureHash>())); 545 std::unique_ptr<crypto::SecureHash>(),
546 false));
538 set_expected_data(kTestData1); 547 set_expected_data(kTestData1);
539 ASSERT_TRUE(AppendDataToFile(kTestData2)); 548 ASSERT_TRUE(AppendDataToFile(kTestData2));
540 ASSERT_TRUE(AppendDataToFile(kTestData3)); 549 ASSERT_TRUE(AppendDataToFile(kTestData3));
541 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); 550 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish());
542 } 551 }
543 552
544 // Open an existing file. The contentsof the file doesn't match the known hash. 553 // Open an existing file. The contentsof the file doesn't match the known hash.
545 TEST_F(BaseFileTest, ExistingBaseFileIncorrectHash) { 554 TEST_F(BaseFileTest, ExistingBaseFileIncorrectHash) {
546 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); 555 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing");
547 ASSERT_EQ(kTestDataLength2, 556 ASSERT_EQ(kTestDataLength2,
548 base::WriteFile(file_path, kTestData2, kTestDataLength2)); 557 base::WriteFile(file_path, kTestData2, kTestDataLength2));
549 558
550 std::string hash_so_far(std::begin(kHashOfTestData1), 559 std::string hash_so_far(std::begin(kHashOfTestData1),
551 std::end(kHashOfTestData1)); 560 std::end(kHashOfTestData1));
552 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH, 561 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH,
553 base_file_->Initialize(file_path, base::FilePath(), base::File(), 562 base_file_->Initialize(file_path, base::FilePath(), base::File(),
554 kTestDataLength2, hash_so_far, 563 kTestDataLength2, hash_so_far,
555 std::unique_ptr<crypto::SecureHash>())); 564 std::unique_ptr<crypto::SecureHash>(),
565 false));
556 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH); 566 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH);
557 } 567 }
558 568
559 // Open a large existing file with a known hash and continue writing to it. 569 // Open a large existing file with a known hash and continue writing to it.
560 TEST_F(BaseFileTest, ExistingBaseFileLargeSizeKnownHash) { 570 TEST_F(BaseFileTest, ExistingBaseFileLargeSizeKnownHash) {
561 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); 571 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing");
562 std::string big_buffer(1024 * 200, 'a'); 572 std::string big_buffer(1024 * 200, 'a');
563 ASSERT_EQ(static_cast<int>(big_buffer.size()), 573 ASSERT_EQ(static_cast<int>(big_buffer.size()),
564 base::WriteFile(file_path, big_buffer.data(), big_buffer.size())); 574 base::WriteFile(file_path, big_buffer.data(), big_buffer.size()));
565 575
566 // Hash of partial file (1024*200 * 'a') 576 // Hash of partial file (1024*200 * 'a')
567 const uint8_t kExpectedPartialHash[] = { 577 const uint8_t kExpectedPartialHash[] = {
568 0x4b, 0x4f, 0x0f, 0x46, 0xac, 0x02, 0xd1, 0x77, 0xde, 0xa0, 0xab, 578 0x4b, 0x4f, 0x0f, 0x46, 0xac, 0x02, 0xd1, 0x77, 0xde, 0xa0, 0xab,
569 0x36, 0xa6, 0x6a, 0x65, 0x78, 0x40, 0xe2, 0xfb, 0x98, 0xb2, 0x0b, 579 0x36, 0xa6, 0x6a, 0x65, 0x78, 0x40, 0xe2, 0xfb, 0x98, 0xb2, 0x0b,
570 0xb2, 0x7a, 0x68, 0x8d, 0xb4, 0xd8, 0xea, 0x9c, 0xd2, 0x2c}; 580 0xb2, 0x7a, 0x68, 0x8d, 0xb4, 0xd8, 0xea, 0x9c, 0xd2, 0x2c};
571 581
572 // Hash of entire file (1024*400 * 'a') 582 // Hash of entire file (1024*400 * 'a')
573 const uint8_t kExpectedFullHash[] = { 583 const uint8_t kExpectedFullHash[] = {
574 0x0c, 0xe9, 0xf6, 0x78, 0x6b, 0x0f, 0x58, 0x49, 0x36, 0xe8, 0x83, 584 0x0c, 0xe9, 0xf6, 0x78, 0x6b, 0x0f, 0x58, 0x49, 0x36, 0xe8, 0x83,
575 0xc5, 0x09, 0x16, 0xbc, 0x5e, 0x2d, 0x07, 0x95, 0xb9, 0x42, 0x20, 585 0xc5, 0x09, 0x16, 0xbc, 0x5e, 0x2d, 0x07, 0x95, 0xb9, 0x42, 0x20,
576 0x41, 0x7c, 0xb3, 0x38, 0xd3, 0xf4, 0xe0, 0x78, 0x89, 0x46}; 586 0x41, 0x7c, 0xb3, 0x38, 0xd3, 0xf4, 0xe0, 0x78, 0x89, 0x46};
577 587
578 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 588 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
579 base_file_->Initialize(file_path, base::FilePath(), base::File(), 589 base_file_->Initialize(file_path, base::FilePath(), base::File(),
580 big_buffer.size(), 590 big_buffer.size(),
581 std::string(std::begin(kExpectedPartialHash), 591 std::string(std::begin(kExpectedPartialHash),
582 std::end(kExpectedPartialHash)), 592 std::end(kExpectedPartialHash)),
583 std::unique_ptr<crypto::SecureHash>())); 593 std::unique_ptr<crypto::SecureHash>(),
594 false));
584 set_expected_data(big_buffer); // Contents of the file on Open. 595 set_expected_data(big_buffer); // Contents of the file on Open.
585 ASSERT_TRUE(AppendDataToFile(big_buffer)); 596 ASSERT_TRUE(AppendDataToFile(big_buffer));
586 ExpectHashValue(kExpectedFullHash, base_file_->Finish()); 597 ExpectHashValue(kExpectedFullHash, base_file_->Finish());
587 } 598 }
588 599
589 // Open a large existing file. The contents doesn't match the known hash. 600 // Open a large existing file. The contents doesn't match the known hash.
590 TEST_F(BaseFileTest, ExistingBaseFileLargeSizeIncorrectHash) { 601 TEST_F(BaseFileTest, ExistingBaseFileLargeSizeIncorrectHash) {
591 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); 602 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing");
592 std::string big_buffer(1024 * 200, 'a'); 603 std::string big_buffer(1024 * 200, 'a');
593 ASSERT_EQ(static_cast<int>(big_buffer.size()), 604 ASSERT_EQ(static_cast<int>(big_buffer.size()),
594 base::WriteFile(file_path, big_buffer.data(), big_buffer.size())); 605 base::WriteFile(file_path, big_buffer.data(), big_buffer.size()));
595 606
596 // Incorrect hash of partial file (1024*200 * 'a') 607 // Incorrect hash of partial file (1024*200 * 'a')
597 const uint8_t kExpectedPartialHash[] = { 608 const uint8_t kExpectedPartialHash[] = {
598 0xc2, 0xa9, 0x08, 0xd9, 0x8f, 0x5d, 0xf9, 0x87, 0xad, 0xe4, 0x1b, 609 0xc2, 0xa9, 0x08, 0xd9, 0x8f, 0x5d, 0xf9, 0x87, 0xad, 0xe4, 0x1b,
599 0x5f, 0xce, 0x21, 0x30, 0x67, 0xef, 0x6c, 0xc2, 0x1e, 0xf2, 0x24, 610 0x5f, 0xce, 0x21, 0x30, 0x67, 0xef, 0x6c, 0xc2, 0x1e, 0xf2, 0x24,
600 0x02, 0x12, 0xa4, 0x1e, 0x54, 0xb5, 0xe7, 0xc2, 0x8a, 0xe5}; 611 0x02, 0x12, 0xa4, 0x1e, 0x54, 0xb5, 0xe7, 0xc2, 0x8a, 0xe5};
601 612
602 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH, 613 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH,
603 base_file_->Initialize(file_path, base::FilePath(), base::File(), 614 base_file_->Initialize(file_path, base::FilePath(), base::File(),
604 big_buffer.size(), 615 big_buffer.size(),
605 std::string(std::begin(kExpectedPartialHash), 616 std::string(std::begin(kExpectedPartialHash),
606 std::end(kExpectedPartialHash)), 617 std::end(kExpectedPartialHash)),
607 std::unique_ptr<crypto::SecureHash>())); 618 std::unique_ptr<crypto::SecureHash>(),
619 false));
608 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH); 620 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH);
609 } 621 }
610 622
611 // Open an existing file. The size of the file is too short. 623 // Open an existing file. The size of the file is too short.
612 TEST_F(BaseFileTest, ExistingBaseFileTooShort) { 624 TEST_F(BaseFileTest, ExistingBaseFileTooShort) {
613 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); 625 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing");
614 ASSERT_EQ(kTestDataLength1, 626 ASSERT_EQ(kTestDataLength1,
615 base::WriteFile(file_path, kTestData1, kTestDataLength1)); 627 base::WriteFile(file_path, kTestData1, kTestDataLength1));
616 628
617 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT, 629 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT,
618 base_file_->Initialize(file_path, base::FilePath(), base::File(), 630 base_file_->Initialize(file_path, base::FilePath(), base::File(),
619 kTestDataLength1 + 1, std::string(), 631 kTestDataLength1 + 1, std::string(),
620 std::unique_ptr<crypto::SecureHash>())); 632 std::unique_ptr<crypto::SecureHash>(),
633 false));
621 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT); 634 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT);
622 } 635 }
623 636
624 // Open an existing file. The size is larger than expected. 637 // Open an existing file. The size is larger than expected.
625 TEST_F(BaseFileTest, ExistingBaseFileKnownHashTooLong) { 638 TEST_F(BaseFileTest, ExistingBaseFileKnownHashTooLong) {
626 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); 639 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing");
627 std::string contents; 640 std::string contents;
628 contents.append(kTestData1); 641 contents.append(kTestData1);
629 contents.append("Something extra"); 642 contents.append("Something extra");
630 ASSERT_EQ(static_cast<int>(contents.size()), 643 ASSERT_EQ(static_cast<int>(contents.size()),
631 base::WriteFile(file_path, contents.data(), contents.size())); 644 base::WriteFile(file_path, contents.data(), contents.size()));
632 645
633 std::string hash_so_far(std::begin(kHashOfTestData1), 646 std::string hash_so_far(std::begin(kHashOfTestData1),
634 std::end(kHashOfTestData1)); 647 std::end(kHashOfTestData1));
635 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 648 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
636 base_file_->Initialize(file_path, base::FilePath(), base::File(), 649 base_file_->Initialize(file_path, base::FilePath(), base::File(),
637 kTestDataLength1, hash_so_far, 650 kTestDataLength1, hash_so_far,
638 std::unique_ptr<crypto::SecureHash>())); 651 std::unique_ptr<crypto::SecureHash>(),
652 false));
639 set_expected_data(kTestData1); // Our starting position. 653 set_expected_data(kTestData1); // Our starting position.
640 ASSERT_TRUE(AppendDataToFile(kTestData2)); 654 ASSERT_TRUE(AppendDataToFile(kTestData2));
641 ASSERT_TRUE(AppendDataToFile(kTestData3)); 655 ASSERT_TRUE(AppendDataToFile(kTestData3));
642 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); 656 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish());
643 } 657 }
644 658
645 // Open an existing file. The size is large than expected and the hash is 659 // Open an existing file. The size is large than expected and the hash is
646 // unknown. 660 // unknown.
647 TEST_F(BaseFileTest, ExistingBaseFileUnknownHashTooLong) { 661 TEST_F(BaseFileTest, ExistingBaseFileUnknownHashTooLong) {
648 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); 662 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing");
649 std::string contents; 663 std::string contents;
650 contents.append(kTestData1); 664 contents.append(kTestData1);
651 contents.append("Something extra"); 665 contents.append("Something extra");
652 ASSERT_EQ(static_cast<int>(contents.size()), 666 ASSERT_EQ(static_cast<int>(contents.size()),
653 base::WriteFile(file_path, contents.data(), contents.size())); 667 base::WriteFile(file_path, contents.data(), contents.size()));
654 668
655 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 669 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
656 base_file_->Initialize(file_path, base::FilePath(), base::File(), 670 base_file_->Initialize(file_path, base::FilePath(), base::File(),
657 kTestDataLength1, std::string(), 671 kTestDataLength1, std::string(),
658 std::unique_ptr<crypto::SecureHash>())); 672 std::unique_ptr<crypto::SecureHash>(),
673 false));
659 set_expected_data(kTestData1); 674 set_expected_data(kTestData1);
660 ASSERT_TRUE(AppendDataToFile(kTestData2)); 675 ASSERT_TRUE(AppendDataToFile(kTestData2));
661 ASSERT_TRUE(AppendDataToFile(kTestData3)); 676 ASSERT_TRUE(AppendDataToFile(kTestData3));
662 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); 677 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish());
663 } 678 }
664 679
665 // Similar to ExistingBaseFileKnownHashTooLong test, but with a file large 680 // Similar to ExistingBaseFileKnownHashTooLong test, but with a file large
666 // enough to requre multiple Read()s to complete. This provides additional code 681 // enough to requre multiple Read()s to complete. This provides additional code
667 // coverage for the CalculatePartialHash() logic. 682 // coverage for the CalculatePartialHash() logic.
668 TEST_F(BaseFileTest, ExistingBaseFileUnknownHashTooLongForLargeFile) { 683 TEST_F(BaseFileTest, ExistingBaseFileUnknownHashTooLongForLargeFile) {
669 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); 684 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing");
670 const size_t kFileSize = 1024 * 1024; 685 const size_t kFileSize = 1024 * 1024;
671 const size_t kIntermediateSize = kFileSize / 2 + 111; 686 const size_t kIntermediateSize = kFileSize / 2 + 111;
672 // |contents| is 100 bytes longer than kIntermediateSize. The latter is the 687 // |contents| is 100 bytes longer than kIntermediateSize. The latter is the
673 // expected size. 688 // expected size.
674 std::string contents(kIntermediateSize + 100, 'a'); 689 std::string contents(kIntermediateSize + 100, 'a');
675 ASSERT_EQ(static_cast<int>(contents.size()), 690 ASSERT_EQ(static_cast<int>(contents.size()),
676 base::WriteFile(file_path, contents.data(), contents.size())); 691 base::WriteFile(file_path, contents.data(), contents.size()));
677 692
678 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 693 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
679 base_file_->Initialize(file_path, base::FilePath(), base::File(), 694 base_file_->Initialize(file_path, base::FilePath(), base::File(),
680 kIntermediateSize, std::string(), 695 kIntermediateSize, std::string(),
681 std::unique_ptr<crypto::SecureHash>())); 696 std::unique_ptr<crypto::SecureHash>(),
697 false));
682 // The extra bytes should be stripped during Initialize(). 698 // The extra bytes should be stripped during Initialize().
683 contents.resize(kIntermediateSize, 'a'); 699 contents.resize(kIntermediateSize, 'a');
684 set_expected_data(contents); 700 set_expected_data(contents);
685 std::string new_data(kFileSize - kIntermediateSize, 'a'); 701 std::string new_data(kFileSize - kIntermediateSize, 'a');
686 ASSERT_TRUE(AppendDataToFile(new_data)); 702 ASSERT_TRUE(AppendDataToFile(new_data));
687 const uint8_t kExpectedHash[] = { 703 const uint8_t kExpectedHash[] = {
688 0x9b, 0xc1, 0xb2, 0xa2, 0x88, 0xb2, 0x6a, 0xf7, 0x25, 0x7a, 0x36, 704 0x9b, 0xc1, 0xb2, 0xa2, 0x88, 0xb2, 0x6a, 0xf7, 0x25, 0x7a, 0x36,
689 0x27, 0x7a, 0xe3, 0x81, 0x6a, 0x7d, 0x4f, 0x16, 0xe8, 0x9c, 0x1e, 705 0x27, 0x7a, 0xe3, 0x81, 0x6a, 0x7d, 0x4f, 0x16, 0xe8, 0x9c, 0x1e,
690 0x7e, 0x77, 0xd0, 0xa5, 0xc4, 0x8b, 0xad, 0x62, 0xb3, 0x60, 706 0x7e, 0x77, 0xd0, 0xa5, 0xc4, 0x8b, 0xad, 0x62, 0xb3, 0x60,
691 }; 707 };
(...skipping 29 matching lines...) Expand all
721 ASSERT_FALSE(base::PathExists(full_path)); 737 ASSERT_FALSE(base::PathExists(full_path));
722 738
723 const char kData[] = "hello"; 739 const char kData[] = "hello";
724 const int kDataLength = static_cast<int>(arraysize(kData) - 1); 740 const int kDataLength = static_cast<int>(arraysize(kData) - 1);
725 ASSERT_EQ(kDataLength, base::WriteFile(full_path, kData, kDataLength)); 741 ASSERT_EQ(kDataLength, base::WriteFile(full_path, kData, kDataLength));
726 // The file that we created here should stick around when the BaseFile is 742 // The file that we created here should stick around when the BaseFile is
727 // destroyed during TearDown. 743 // destroyed during TearDown.
728 expect_file_survives_ = true; 744 expect_file_survives_ = true;
729 } 745 }
730 746
747 // Test that writing data to a sparse file works.
748 TEST_F(BaseFileTest, WriteDataToSparseFile) {
749 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing");
750 std::string contents = kTestData1;
751 ASSERT_EQ(static_cast<int>(contents.size()),
752 base::WriteFile(file_path, contents.data(), contents.size()));
753
754 base_file_->Initialize(file_path, base::FilePath(), base::File(),
755 kTestDataLength1,
756 std::string(),
757 std::unique_ptr<crypto::SecureHash>(),
758 true);
759 // This will create a hole in the file.
760 base_file_->WriteDataToFile(kTestDataLength1 + kTestDataLength2,
761 kTestData3,
762 kTestDataLength3);
763 // This should fill the hole.
764 base_file_->WriteDataToFile(kTestDataLength1,
765 kTestData2,
766 kTestDataLength2);
767 set_expected_data(contents + kTestData2 + kTestData3);
768 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish());
769 }
770
731 } // namespace content 771 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/download/base_file.cc ('k') | content/browser/download/base_file_win_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698