OLD | NEW |
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 "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 } | 47 } |
48 | 48 |
49 virtual void SetUp() { | 49 virtual void SetUp() { |
50 ResetHash(); | 50 ResetHash(); |
51 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 51 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
52 base_file_.reset(new BaseFile(base::FilePath(), | 52 base_file_.reset(new BaseFile(base::FilePath(), |
53 GURL(), | 53 GURL(), |
54 GURL(), | 54 GURL(), |
55 0, | 55 0, |
56 false, | 56 false, |
57 "", | 57 std::string(), |
58 scoped_ptr<net::FileStream>(), | 58 scoped_ptr<net::FileStream>(), |
59 net::BoundNetLog())); | 59 net::BoundNetLog())); |
60 } | 60 } |
61 | 61 |
62 virtual void TearDown() { | 62 virtual void TearDown() { |
63 EXPECT_FALSE(base_file_->in_progress()); | 63 EXPECT_FALSE(base_file_->in_progress()); |
64 if (!expected_error_) { | 64 if (!expected_error_) { |
65 EXPECT_EQ(static_cast<int64>(expected_data_.size()), | 65 EXPECT_EQ(static_cast<int64>(expected_data_.size()), |
66 base_file_->bytes_so_far()); | 66 base_file_->bytes_so_far()); |
67 } | 67 } |
(...skipping 30 matching lines...) Expand all Loading... |
98 sizeof(sha256_hash_)); | 98 sizeof(sha256_hash_)); |
99 return hash; | 99 return hash; |
100 } | 100 } |
101 | 101 |
102 void MakeFileWithHash() { | 102 void MakeFileWithHash() { |
103 base_file_.reset(new BaseFile(base::FilePath(), | 103 base_file_.reset(new BaseFile(base::FilePath(), |
104 GURL(), | 104 GURL(), |
105 GURL(), | 105 GURL(), |
106 0, | 106 0, |
107 true, | 107 true, |
108 "", | 108 std::string(), |
109 scoped_ptr<net::FileStream>(), | 109 scoped_ptr<net::FileStream>(), |
110 net::BoundNetLog())); | 110 net::BoundNetLog())); |
111 } | 111 } |
112 | 112 |
113 bool InitializeFile() { | 113 bool InitializeFile() { |
114 DownloadInterruptReason result = base_file_->Initialize(temp_dir_.path()); | 114 DownloadInterruptReason result = base_file_->Initialize(temp_dir_.path()); |
115 EXPECT_EQ(expected_error_, result); | 115 EXPECT_EQ(expected_error_, result); |
116 return result == DOWNLOAD_INTERRUPT_REASON_NONE; | 116 return result == DOWNLOAD_INTERRUPT_REASON_NONE; |
117 } | 117 } |
118 | 118 |
(...skipping 19 matching lines...) Expand all Loading... |
138 | 138 |
139 // Helper functions. | 139 // Helper functions. |
140 // Create a file. Returns the complete file path. | 140 // Create a file. Returns the complete file path. |
141 base::FilePath CreateTestFile() { | 141 base::FilePath CreateTestFile() { |
142 base::FilePath file_name; | 142 base::FilePath file_name; |
143 BaseFile file(base::FilePath(), | 143 BaseFile file(base::FilePath(), |
144 GURL(), | 144 GURL(), |
145 GURL(), | 145 GURL(), |
146 0, | 146 0, |
147 false, | 147 false, |
148 "", | 148 std::string(), |
149 scoped_ptr<net::FileStream>(), | 149 scoped_ptr<net::FileStream>(), |
150 net::BoundNetLog()); | 150 net::BoundNetLog()); |
151 | 151 |
152 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 152 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
153 file.Initialize(temp_dir_.path())); | 153 file.Initialize(temp_dir_.path())); |
154 file_name = file.full_path(); | 154 file_name = file.full_path(); |
155 EXPECT_NE(base::FilePath::StringType(), file_name.value()); | 155 EXPECT_NE(base::FilePath::StringType(), file_name.value()); |
156 | 156 |
157 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 157 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
158 file.AppendDataToFile(kTestData4, kTestDataLength4)); | 158 file.AppendDataToFile(kTestData4, kTestDataLength4)); |
159 | 159 |
160 // Keep the file from getting deleted when existing_file_name is deleted. | 160 // Keep the file from getting deleted when existing_file_name is deleted. |
161 file.Detach(); | 161 file.Detach(); |
162 | 162 |
163 return file_name; | 163 return file_name; |
164 } | 164 } |
165 | 165 |
166 // Create a file with the specified file name. | 166 // Create a file with the specified file name. |
167 void CreateFileWithName(const base::FilePath& file_name) { | 167 void CreateFileWithName(const base::FilePath& file_name) { |
168 EXPECT_NE(base::FilePath::StringType(), file_name.value()); | 168 EXPECT_NE(base::FilePath::StringType(), file_name.value()); |
169 BaseFile duplicate_file(file_name, | 169 BaseFile duplicate_file(file_name, |
170 GURL(), | 170 GURL(), |
171 GURL(), | 171 GURL(), |
172 0, | 172 0, |
173 false, | 173 false, |
174 "", | 174 std::string(), |
175 scoped_ptr<net::FileStream>(), | 175 scoped_ptr<net::FileStream>(), |
176 net::BoundNetLog()); | 176 net::BoundNetLog()); |
177 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 177 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
178 duplicate_file.Initialize(temp_dir_.path())); | 178 duplicate_file.Initialize(temp_dir_.path())); |
179 // Write something into it. | 179 // Write something into it. |
180 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); | 180 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); |
181 // Detach the file so it isn't deleted on destruction of |duplicate_file|. | 181 // Detach the file so it isn't deleted on destruction of |duplicate_file|. |
182 duplicate_file.Detach(); | 182 duplicate_file.Detach(); |
183 } | 183 } |
184 | 184 |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE)); | 496 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE)); |
497 | 497 |
498 // Copy of mock_file_stream; we pass ownership and rely on the BaseFile | 498 // Copy of mock_file_stream; we pass ownership and rely on the BaseFile |
499 // not deleting it until it is reset. | 499 // not deleting it until it is reset. |
500 | 500 |
501 base_file_.reset(new BaseFile(mock_file_stream->get_path(), | 501 base_file_.reset(new BaseFile(mock_file_stream->get_path(), |
502 GURL(), | 502 GURL(), |
503 GURL(), | 503 GURL(), |
504 0, | 504 0, |
505 false, | 505 false, |
506 "", | 506 std::string(), |
507 mock_file_stream_scoped_ptr.Pass(), | 507 mock_file_stream_scoped_ptr.Pass(), |
508 net::BoundNetLog())); | 508 net::BoundNetLog())); |
509 ASSERT_TRUE(InitializeFile()); | 509 ASSERT_TRUE(InitializeFile()); |
510 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 510 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
511 ASSERT_TRUE(AppendDataToFile(kTestData2)); | 511 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
512 mock_file_stream->set_forced_error(net::ERR_ACCESS_DENIED); | 512 mock_file_stream->set_forced_error(net::ERR_ACCESS_DENIED); |
513 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 513 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
514 ASSERT_FALSE(AppendDataToFile(kTestData3)); | 514 ASSERT_FALSE(AppendDataToFile(kTestData3)); |
515 std::string hash; | 515 std::string hash; |
516 EXPECT_FALSE(base_file_->GetHash(&hash)); | 516 EXPECT_FALSE(base_file_->GetHash(&hash)); |
(...skipping 26 matching lines...) Expand all Loading... |
543 base::FilePath existing_file_name = CreateTestFile(); | 543 base::FilePath existing_file_name = CreateTestFile(); |
544 | 544 |
545 set_expected_data(kTestData4); | 545 set_expected_data(kTestData4); |
546 | 546 |
547 // Use the file we've just created. | 547 // Use the file we've just created. |
548 base_file_.reset(new BaseFile(existing_file_name, | 548 base_file_.reset(new BaseFile(existing_file_name, |
549 GURL(), | 549 GURL(), |
550 GURL(), | 550 GURL(), |
551 kTestDataLength4, | 551 kTestDataLength4, |
552 false, | 552 false, |
553 "", | 553 std::string(), |
554 scoped_ptr<net::FileStream>(), | 554 scoped_ptr<net::FileStream>(), |
555 net::BoundNetLog())); | 555 net::BoundNetLog())); |
556 | 556 |
557 ASSERT_TRUE(InitializeFile()); | 557 ASSERT_TRUE(InitializeFile()); |
558 | 558 |
559 const base::FilePath file_name = base_file_->full_path(); | 559 const base::FilePath file_name = base_file_->full_path(); |
560 EXPECT_NE(base::FilePath::StringType(), file_name.value()); | 560 EXPECT_NE(base::FilePath::StringType(), file_name.value()); |
561 | 561 |
562 // Write into the file. | 562 // Write into the file. |
563 EXPECT_TRUE(AppendDataToFile(kTestData1)); | 563 EXPECT_TRUE(AppendDataToFile(kTestData1)); |
(...skipping 13 matching lines...) Expand all Loading... |
577 | 577 |
578 // Make it read-only. | 578 // Make it read-only. |
579 EXPECT_TRUE(file_util::MakeFileUnwritable(readonly_file_name)); | 579 EXPECT_TRUE(file_util::MakeFileUnwritable(readonly_file_name)); |
580 | 580 |
581 // Try to overwrite it. | 581 // Try to overwrite it. |
582 base_file_.reset(new BaseFile(readonly_file_name, | 582 base_file_.reset(new BaseFile(readonly_file_name, |
583 GURL(), | 583 GURL(), |
584 GURL(), | 584 GURL(), |
585 0, | 585 0, |
586 false, | 586 false, |
587 "", | 587 std::string(), |
588 scoped_ptr<net::FileStream>(), | 588 scoped_ptr<net::FileStream>(), |
589 net::BoundNetLog())); | 589 net::BoundNetLog())); |
590 | 590 |
591 expect_in_progress_ = false; | 591 expect_in_progress_ = false; |
592 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 592 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
593 EXPECT_FALSE(InitializeFile()); | 593 EXPECT_FALSE(InitializeFile()); |
594 | 594 |
595 const base::FilePath file_name = base_file_->full_path(); | 595 const base::FilePath file_name = base_file_->full_path(); |
596 EXPECT_NE(base::FilePath::StringType(), file_name.value()); | 596 EXPECT_NE(base::FilePath::StringType(), file_name.value()); |
597 | 597 |
598 // Write into the file. | 598 // Write into the file. |
599 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); | 599 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
600 EXPECT_FALSE(AppendDataToFile(kTestData1)); | 600 EXPECT_FALSE(AppendDataToFile(kTestData1)); |
601 | 601 |
602 base_file_->Finish(); | 602 base_file_->Finish(); |
603 base_file_->Detach(); | 603 base_file_->Detach(); |
604 expect_file_survives_ = true; | 604 expect_file_survives_ = true; |
605 } | 605 } |
606 | 606 |
607 TEST_F(BaseFileTest, IsEmptyHash) { | 607 TEST_F(BaseFileTest, IsEmptyHash) { |
608 std::string empty(BaseFile::kSha256HashLen, '\x00'); | 608 std::string empty(BaseFile::kSha256HashLen, '\x00'); |
609 EXPECT_TRUE(BaseFile::IsEmptyHash(empty)); | 609 EXPECT_TRUE(BaseFile::IsEmptyHash(empty)); |
610 std::string not_empty(BaseFile::kSha256HashLen, '\x01'); | 610 std::string not_empty(BaseFile::kSha256HashLen, '\x01'); |
611 EXPECT_FALSE(BaseFile::IsEmptyHash(not_empty)); | 611 EXPECT_FALSE(BaseFile::IsEmptyHash(not_empty)); |
612 EXPECT_FALSE(BaseFile::IsEmptyHash("")); | 612 EXPECT_FALSE(BaseFile::IsEmptyHash(std::string())); |
613 } | 613 } |
614 | 614 |
615 // Test that calculating speed after no writes. | 615 // Test that calculating speed after no writes. |
616 TEST_F(BaseFileTest, SpeedWithoutWrite) { | 616 TEST_F(BaseFileTest, SpeedWithoutWrite) { |
617 ASSERT_TRUE(InitializeFile()); | 617 ASSERT_TRUE(InitializeFile()); |
618 base::TimeTicks current = StartTick() + kElapsedTimeDelta; | 618 base::TimeTicks current = StartTick() + kElapsedTimeDelta; |
619 ASSERT_EQ(0, CurrentSpeedAtTime(current)); | 619 ASSERT_EQ(0, CurrentSpeedAtTime(current)); |
620 base_file_->Finish(); | 620 base_file_->Finish(); |
621 } | 621 } |
622 | 622 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
665 base::FilePath temp_file; | 665 base::FilePath temp_file; |
666 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), | 666 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), |
667 &temp_file)); | 667 &temp_file)); |
668 ASSERT_FALSE(temp_file.empty()); | 668 ASSERT_FALSE(temp_file.empty()); |
669 EXPECT_STREQ(temp_file.DirName().value().c_str(), | 669 EXPECT_STREQ(temp_file.DirName().value().c_str(), |
670 base_file_->full_path().DirName().value().c_str()); | 670 base_file_->full_path().DirName().value().c_str()); |
671 base_file_->Finish(); | 671 base_file_->Finish(); |
672 } | 672 } |
673 | 673 |
674 } // namespace content | 674 } // namespace content |
OLD | NEW |