| 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 <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
| 7 #include "base/files/file.h" | 10 #include "base/files/file.h" |
| 8 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/macros.h" |
| 11 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 12 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/test/test_file_util.h" | 17 #include "base/test/test_file_util.h" |
| 18 #include "build/build_config.h" |
| 14 #include "content/browser/browser_thread_impl.h" | 19 #include "content/browser/browser_thread_impl.h" |
| 15 #include "content/public/browser/download_interrupt_reasons.h" | 20 #include "content/public/browser/download_interrupt_reasons.h" |
| 16 #include "crypto/secure_hash.h" | 21 #include "crypto/secure_hash.h" |
| 17 #include "crypto/sha2.h" | 22 #include "crypto/sha2.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 24 |
| 20 namespace content { | 25 namespace content { |
| 21 namespace { | 26 namespace { |
| 22 | 27 |
| 23 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"; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 54 0, | 59 0, |
| 55 false, | 60 false, |
| 56 std::string(), | 61 std::string(), |
| 57 base::File(), | 62 base::File(), |
| 58 net::BoundNetLog())); | 63 net::BoundNetLog())); |
| 59 } | 64 } |
| 60 | 65 |
| 61 void TearDown() override { | 66 void TearDown() override { |
| 62 EXPECT_FALSE(base_file_->in_progress()); | 67 EXPECT_FALSE(base_file_->in_progress()); |
| 63 if (!expected_error_) { | 68 if (!expected_error_) { |
| 64 EXPECT_EQ(static_cast<int64>(expected_data_.size()), | 69 EXPECT_EQ(static_cast<int64_t>(expected_data_.size()), |
| 65 base_file_->bytes_so_far()); | 70 base_file_->bytes_so_far()); |
| 66 } | 71 } |
| 67 | 72 |
| 68 base::FilePath full_path = base_file_->full_path(); | 73 base::FilePath full_path = base_file_->full_path(); |
| 69 | 74 |
| 70 if (!expected_data_.empty() && !expected_error_) { | 75 if (!expected_data_.empty() && !expected_error_) { |
| 71 // Make sure the data has been properly written to disk. | 76 // Make sure the data has been properly written to disk. |
| 72 std::string disk_data; | 77 std::string disk_data; |
| 73 EXPECT_TRUE(base::ReadFileToString(full_path, &disk_data)); | 78 EXPECT_TRUE(base::ReadFileToString(full_path, &disk_data)); |
| 74 EXPECT_EQ(expected_data_, disk_data); | 79 EXPECT_EQ(expected_data_, disk_data); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 EXPECT_EQ(expect_in_progress_, base_file_->in_progress()); | 124 EXPECT_EQ(expect_in_progress_, base_file_->in_progress()); |
| 120 DownloadInterruptReason result = | 125 DownloadInterruptReason result = |
| 121 base_file_->AppendDataToFile(data.data(), data.size()); | 126 base_file_->AppendDataToFile(data.data(), data.size()); |
| 122 if (result == DOWNLOAD_INTERRUPT_REASON_NONE) | 127 if (result == DOWNLOAD_INTERRUPT_REASON_NONE) |
| 123 EXPECT_TRUE(expect_in_progress_) << " result = " << result; | 128 EXPECT_TRUE(expect_in_progress_) << " result = " << result; |
| 124 | 129 |
| 125 EXPECT_EQ(expected_error_, result); | 130 EXPECT_EQ(expected_error_, result); |
| 126 if (base_file_->in_progress()) { | 131 if (base_file_->in_progress()) { |
| 127 expected_data_ += data; | 132 expected_data_ += data; |
| 128 if (expected_error_ == DOWNLOAD_INTERRUPT_REASON_NONE) { | 133 if (expected_error_ == DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 129 EXPECT_EQ(static_cast<int64>(expected_data_.size()), | 134 EXPECT_EQ(static_cast<int64_t>(expected_data_.size()), |
| 130 base_file_->bytes_so_far()); | 135 base_file_->bytes_so_far()); |
| 131 } | 136 } |
| 132 } | 137 } |
| 133 return result == DOWNLOAD_INTERRUPT_REASON_NONE; | 138 return result == DOWNLOAD_INTERRUPT_REASON_NONE; |
| 134 } | 139 } |
| 135 | 140 |
| 136 void set_expected_data(const std::string& data) { expected_data_ = data; } | 141 void set_expected_data(const std::string& data) { expected_data_ = data; } |
| 137 | 142 |
| 138 // Helper functions. | 143 // Helper functions. |
| 139 // Create a file. Returns the complete file path. | 144 // Create a file. Returns the complete file path. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 base::File(), | 179 base::File(), |
| 175 net::BoundNetLog()); | 180 net::BoundNetLog()); |
| 176 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 181 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 177 duplicate_file.Initialize(temp_dir_.path())); | 182 duplicate_file.Initialize(temp_dir_.path())); |
| 178 // Write something into it. | 183 // Write something into it. |
| 179 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); | 184 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); |
| 180 // Detach the file so it isn't deleted on destruction of |duplicate_file|. | 185 // Detach the file so it isn't deleted on destruction of |duplicate_file|. |
| 181 duplicate_file.Detach(); | 186 duplicate_file.Detach(); |
| 182 } | 187 } |
| 183 | 188 |
| 184 int64 CurrentSpeedAtTime(base::TimeTicks current_time) { | 189 int64_t CurrentSpeedAtTime(base::TimeTicks current_time) { |
| 185 EXPECT_TRUE(base_file_.get()); | 190 EXPECT_TRUE(base_file_.get()); |
| 186 return base_file_->CurrentSpeedAtTime(current_time); | 191 return base_file_->CurrentSpeedAtTime(current_time); |
| 187 } | 192 } |
| 188 | 193 |
| 189 base::TimeTicks StartTick() { | 194 base::TimeTicks StartTick() { |
| 190 EXPECT_TRUE(base_file_.get()); | 195 EXPECT_TRUE(base_file_.get()); |
| 191 return base_file_->start_tick_; | 196 return base_file_->start_tick_; |
| 192 } | 197 } |
| 193 | 198 |
| 194 void set_expected_error(DownloadInterruptReason err) { | 199 void set_expected_error(DownloadInterruptReason err) { |
| (...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 684 | 689 |
| 685 const char kData[] = "hello"; | 690 const char kData[] = "hello"; |
| 686 const int kDataLength = static_cast<int>(arraysize(kData) - 1); | 691 const int kDataLength = static_cast<int>(arraysize(kData) - 1); |
| 687 ASSERT_EQ(kDataLength, base::WriteFile(full_path, kData, kDataLength)); | 692 ASSERT_EQ(kDataLength, base::WriteFile(full_path, kData, kDataLength)); |
| 688 // The file that we created here should stick around when the BaseFile is | 693 // The file that we created here should stick around when the BaseFile is |
| 689 // destroyed during TearDown. | 694 // destroyed during TearDown. |
| 690 expect_file_survives_ = true; | 695 expect_file_survives_ = true; |
| 691 } | 696 } |
| 692 | 697 |
| 693 } // namespace content | 698 } // namespace content |
| OLD | NEW |