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 "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 #include "base/test/test_file_util.h" | 8 #include "base/test/test_file_util.h" |
9 #include "content/browser/browser_thread_impl.h" | 9 #include "content/browser/browser_thread_impl.h" |
10 #include "content/browser/byte_stream.h" | 10 #include "content/browser/byte_stream.h" |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 | 157 |
158 ::testing::Mock::VerifyAndClearExpectations(input_stream_); | 158 ::testing::Mock::VerifyAndClearExpectations(input_stream_); |
159 return result == DOWNLOAD_INTERRUPT_REASON_NONE; | 159 return result == DOWNLOAD_INTERRUPT_REASON_NONE; |
160 } | 160 } |
161 | 161 |
162 virtual void DestroyDownloadFile(int offset) { | 162 virtual void DestroyDownloadFile(int offset) { |
163 EXPECT_FALSE(download_file_->InProgress()); | 163 EXPECT_FALSE(download_file_->InProgress()); |
164 | 164 |
165 // Make sure the data has been properly written to disk. | 165 // Make sure the data has been properly written to disk. |
166 std::string disk_data; | 166 std::string disk_data; |
167 EXPECT_TRUE(file_util::ReadFileToString(download_file_->FullPath(), | 167 EXPECT_TRUE(base::ReadFileToString(download_file_->FullPath(), &disk_data)); |
168 &disk_data)); | |
169 EXPECT_EQ(expected_data_, disk_data); | 168 EXPECT_EQ(expected_data_, disk_data); |
170 | 169 |
171 // Make sure the Browser and File threads outlive the DownloadFile | 170 // Make sure the Browser and File threads outlive the DownloadFile |
172 // to satisfy thread checks inside it. | 171 // to satisfy thread checks inside it. |
173 download_file_.reset(); | 172 download_file_.reset(); |
174 } | 173 } |
175 | 174 |
176 // Setup the stream to do be a data append; don't actually trigger | 175 // Setup the stream to do be a data append; don't actually trigger |
177 // the callback or do verifications. | 176 // the callback or do verifications. |
178 void SetupDataAppend(const char **data_chunks, size_t num_chunks, | 177 void SetupDataAppend(const char **data_chunks, size_t num_chunks, |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 EXPECT_TRUE(download_file_->GetHash(&hash)); | 409 EXPECT_TRUE(download_file_->GetHash(&hash)); |
411 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size())); | 410 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size())); |
412 | 411 |
413 // Check that a rename with overwrite to an existing file succeeds. | 412 // Check that a rename with overwrite to an existing file succeeds. |
414 std::string file_contents; | 413 std::string file_contents; |
415 ASSERT_FALSE(base::PathExists(path_5)); | 414 ASSERT_FALSE(base::PathExists(path_5)); |
416 static const char file_data[] = "xyzzy"; | 415 static const char file_data[] = "xyzzy"; |
417 ASSERT_EQ(static_cast<int>(sizeof(file_data) - 1), | 416 ASSERT_EQ(static_cast<int>(sizeof(file_data) - 1), |
418 file_util::WriteFile(path_5, file_data, sizeof(file_data) - 1)); | 417 file_util::WriteFile(path_5, file_data, sizeof(file_data) - 1)); |
419 ASSERT_TRUE(base::PathExists(path_5)); | 418 ASSERT_TRUE(base::PathExists(path_5)); |
420 EXPECT_TRUE(file_util::ReadFileToString(path_5, &file_contents)); | 419 EXPECT_TRUE(base::ReadFileToString(path_5, &file_contents)); |
421 EXPECT_EQ(std::string(file_data), file_contents); | 420 EXPECT_EQ(std::string(file_data), file_contents); |
422 | 421 |
423 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 422 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
424 RenameAndAnnotate(path_5, &output_path)); | 423 RenameAndAnnotate(path_5, &output_path)); |
425 EXPECT_EQ(path_5, output_path); | 424 EXPECT_EQ(path_5, output_path); |
426 | 425 |
427 file_contents = ""; | 426 file_contents = ""; |
428 EXPECT_TRUE(file_util::ReadFileToString(path_5, &file_contents)); | 427 EXPECT_TRUE(base::ReadFileToString(path_5, &file_contents)); |
429 EXPECT_NE(std::string(file_data), file_contents); | 428 EXPECT_NE(std::string(file_data), file_contents); |
430 | 429 |
431 DestroyDownloadFile(0); | 430 DestroyDownloadFile(0); |
432 } | 431 } |
433 | 432 |
434 // Test to make sure the rename uniquifies if we aren't overwriting | 433 // Test to make sure the rename uniquifies if we aren't overwriting |
435 // and there's a file where we're aiming. | 434 // and there's a file where we're aiming. |
436 TEST_F(DownloadFileTest, RenameUniquifies) { | 435 TEST_F(DownloadFileTest, RenameUniquifies) { |
437 ASSERT_TRUE(CreateDownloadFile(0, true)); | 436 ASSERT_TRUE(CreateDownloadFile(0, true)); |
438 base::FilePath initial_path(download_file_->FullPath()); | 437 base::FilePath initial_path(download_file_->FullPath()); |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
597 | 596 |
598 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)), | 597 EXPECT_EQ(static_cast<int64>(strlen(kTestData1) + strlen(kTestData2)), |
599 bytes_); | 598 bytes_); |
600 EXPECT_EQ(download_file_->GetHashState(), hash_state_); | 599 EXPECT_EQ(download_file_->GetHashState(), hash_state_); |
601 | 600 |
602 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true); | 601 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true); |
603 DestroyDownloadFile(0); | 602 DestroyDownloadFile(0); |
604 } | 603 } |
605 | 604 |
606 } // namespace content | 605 } // namespace content |
OLD | NEW |