Chromium Code Reviews| 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> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 | 25 |
| 26 namespace content { | 26 namespace content { |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 const char kTestData1[] = "Let's write some data to the file!\n"; | 29 const char kTestData1[] = "Let's write some data to the file!\n"; |
| 30 const char kTestData2[] = "Writing more data.\n"; | 30 const char kTestData2[] = "Writing more data.\n"; |
| 31 const char kTestData3[] = "Final line."; | 31 const char kTestData3[] = "Final line."; |
| 32 const char kTestData4[] = "supercalifragilisticexpialidocious"; | 32 const char kTestData4[] = "supercalifragilisticexpialidocious"; |
| 33 const int kTestDataLength1 = arraysize(kTestData1) - 1; | 33 const int kTestDataLength1 = arraysize(kTestData1) - 1; |
| 34 const int kTestDataLength2 = arraysize(kTestData2) - 1; | 34 const int kTestDataLength2 = arraysize(kTestData2) - 1; |
| 35 const int kTestDataLength3 = arraysize(kTestData3) - 1; | |
| 36 const int kTestDataLength4 = arraysize(kTestData4) - 1; | 35 const int kTestDataLength4 = arraysize(kTestData4) - 1; |
| 37 const int kElapsedTimeSeconds = 5; | 36 const int kElapsedTimeSeconds = 5; |
| 38 const base::TimeDelta kElapsedTimeDelta = base::TimeDelta::FromSeconds( | 37 const base::TimeDelta kElapsedTimeDelta = base::TimeDelta::FromSeconds( |
| 39 kElapsedTimeSeconds); | 38 kElapsedTimeSeconds); |
| 40 | 39 |
| 40 // SHA-256 hash of kTestData1 (excluding terminating NUL). | |
| 41 const uint8_t kHashOfTestData1[] = { | |
| 42 0x0b, 0x2d, 0x3f, 0x3f, 0x79, 0x43, 0xad, 0x64, 0xb8, 0x60, 0xdf, | |
| 43 0x94, 0xd0, 0x5c, 0xb5, 0x6a, 0x8a, 0x97, 0xc6, 0xec, 0x57, 0x68, | |
| 44 0xb5, 0xb7, 0x0b, 0x93, 0x0c, 0x5a, 0xa7, 0xfa, 0x9a, 0xde}; | |
| 45 | |
| 46 // SHA-256 hash of kTestData1 ++ kTestData2 ++ kTestData3 (excluding terminating | |
| 47 // NUL). | |
| 48 const uint8_t kHashOfTestData1To3[] = { | |
| 49 0xcb, 0xf6, 0x8b, 0xf1, 0x0f, 0x80, 0x03, 0xdb, 0x86, 0xb3, 0x13, | |
| 50 0x43, 0xaf, 0xac, 0x8c, 0x71, 0x75, 0xbd, 0x03, 0xfb, 0x5f, 0xc9, | |
| 51 0x05, 0x65, 0x0f, 0x8c, 0x80, 0xaf, 0x08, 0x74, 0x43, 0xa8}; | |
| 52 | |
| 41 } // namespace | 53 } // namespace |
| 42 | 54 |
| 43 class BaseFileTest : public testing::Test { | 55 class BaseFileTest : public testing::Test { |
| 44 public: | 56 public: |
| 45 static const unsigned char kEmptySha256Hash[crypto::kSHA256Length]; | 57 static const unsigned char kEmptySha256Hash[crypto::kSHA256Length]; |
| 46 | 58 |
| 47 BaseFileTest() | 59 BaseFileTest() |
| 48 : expect_file_survives_(false), | 60 : expect_file_survives_(false), |
| 49 expect_in_progress_(true), | 61 expect_in_progress_(true), |
| 50 expected_error_(DOWNLOAD_INTERRUPT_REASON_NONE), | 62 expected_error_(DOWNLOAD_INTERRUPT_REASON_NONE), |
| 51 file_thread_(BrowserThread::FILE, &message_loop_) { | 63 file_thread_(BrowserThread::FILE, &message_loop_) { |
| 52 } | 64 } |
| 53 | 65 |
| 54 void SetUp() override { | 66 void SetUp() override { |
| 55 ResetHash(); | |
| 56 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 67 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 57 base_file_.reset(new BaseFile(base::FilePath(), | 68 base_file_.reset(new BaseFile(net::BoundNetLog())); |
| 58 GURL(), | |
| 59 GURL(), | |
| 60 0, | |
| 61 false, | |
| 62 std::string(), | |
| 63 base::File(), | |
| 64 net::BoundNetLog())); | |
| 65 } | 69 } |
| 66 | 70 |
| 67 void TearDown() override { | 71 void TearDown() override { |
| 68 EXPECT_FALSE(base_file_->in_progress()); | 72 EXPECT_FALSE(base_file_->in_progress()); |
| 69 if (!expected_error_) { | 73 if (!expected_error_) { |
| 70 EXPECT_EQ(static_cast<int64_t>(expected_data_.size()), | 74 EXPECT_EQ(static_cast<int64_t>(expected_data_.size()), |
| 71 base_file_->bytes_so_far()); | 75 base_file_->bytes_so_far()); |
| 72 } | 76 } |
| 73 | 77 |
| 74 base::FilePath full_path = base_file_->full_path(); | 78 base::FilePath full_path = base_file_->full_path(); |
| 75 | 79 |
| 76 if (!expected_data_.empty() && !expected_error_) { | 80 if (!expected_data_.empty() && !expected_error_) { |
| 77 // Make sure the data has been properly written to disk. | 81 // Make sure the data has been properly written to disk. |
| 78 std::string disk_data; | 82 std::string disk_data; |
| 79 EXPECT_TRUE(base::ReadFileToString(full_path, &disk_data)); | 83 EXPECT_TRUE(base::ReadFileToString(full_path, &disk_data)); |
| 80 EXPECT_EQ(expected_data_, disk_data); | 84 EXPECT_EQ(expected_data_, disk_data); |
| 81 } | 85 } |
| 82 | 86 |
| 83 // Make sure the mock BrowserThread outlives the BaseFile to satisfy | 87 // Make sure the mock BrowserThread outlives the BaseFile to satisfy |
| 84 // thread checks inside it. | 88 // thread checks inside it. |
| 85 base_file_.reset(); | 89 base_file_.reset(); |
| 86 | 90 |
| 87 EXPECT_EQ(expect_file_survives_, base::PathExists(full_path)); | 91 EXPECT_EQ(expect_file_survives_, base::PathExists(full_path)); |
| 88 } | 92 } |
| 89 | 93 |
| 90 void ResetHash() { | |
| 91 secure_hash_.reset(crypto::SecureHash::Create(crypto::SecureHash::SHA256)); | |
| 92 memcpy(sha256_hash_, kEmptySha256Hash, crypto::kSHA256Length); | |
| 93 } | |
| 94 | |
| 95 void UpdateHash(const char* data, size_t length) { | |
| 96 secure_hash_->Update(data, length); | |
| 97 } | |
| 98 | |
| 99 std::string GetFinalHash() { | |
| 100 std::string hash; | |
| 101 secure_hash_->Finish(sha256_hash_, crypto::kSHA256Length); | |
| 102 hash.assign(reinterpret_cast<const char*>(sha256_hash_), | |
| 103 sizeof(sha256_hash_)); | |
| 104 return hash; | |
| 105 } | |
| 106 | |
| 107 void MakeFileWithHash() { | |
| 108 base_file_.reset(new BaseFile(base::FilePath(), | |
| 109 GURL(), | |
| 110 GURL(), | |
| 111 0, | |
| 112 true, | |
| 113 std::string(), | |
| 114 base::File(), | |
| 115 net::BoundNetLog())); | |
| 116 } | |
| 117 | |
| 118 bool InitializeFile() { | 94 bool InitializeFile() { |
| 119 DownloadInterruptReason result = base_file_->Initialize(temp_dir_.path()); | 95 DownloadInterruptReason result = base_file_->Initialize( |
| 96 base::FilePath(), temp_dir_.path(), base::File(), 0, std::string(), | |
| 97 scoped_ptr<crypto::SecureHash>()); | |
| 120 EXPECT_EQ(expected_error_, result); | 98 EXPECT_EQ(expected_error_, result); |
| 121 return result == DOWNLOAD_INTERRUPT_REASON_NONE; | 99 return result == DOWNLOAD_INTERRUPT_REASON_NONE; |
| 122 } | 100 } |
| 123 | 101 |
| 124 bool AppendDataToFile(const std::string& data) { | 102 bool AppendDataToFile(const std::string& data) { |
| 125 EXPECT_EQ(expect_in_progress_, base_file_->in_progress()); | 103 EXPECT_EQ(expect_in_progress_, base_file_->in_progress()); |
| 126 DownloadInterruptReason result = | 104 DownloadInterruptReason result = |
| 127 base_file_->AppendDataToFile(data.data(), data.size()); | 105 base_file_->AppendDataToFile(data.data(), data.size()); |
| 128 if (result == DOWNLOAD_INTERRUPT_REASON_NONE) | 106 if (result == DOWNLOAD_INTERRUPT_REASON_NONE) |
| 129 EXPECT_TRUE(expect_in_progress_) << " result = " << result; | 107 EXPECT_TRUE(expect_in_progress_) << " result = " << result; |
| 130 | 108 |
| 131 EXPECT_EQ(expected_error_, result); | 109 EXPECT_EQ(expected_error_, result); |
| 132 if (base_file_->in_progress()) { | 110 if (base_file_->in_progress()) { |
| 133 expected_data_ += data; | 111 expected_data_ += data; |
| 134 if (expected_error_ == DOWNLOAD_INTERRUPT_REASON_NONE) { | 112 if (expected_error_ == DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 135 EXPECT_EQ(static_cast<int64_t>(expected_data_.size()), | 113 EXPECT_EQ(static_cast<int64_t>(expected_data_.size()), |
| 136 base_file_->bytes_so_far()); | 114 base_file_->bytes_so_far()); |
| 137 } | 115 } |
| 138 } | 116 } |
| 139 return result == DOWNLOAD_INTERRUPT_REASON_NONE; | 117 return result == DOWNLOAD_INTERRUPT_REASON_NONE; |
| 140 } | 118 } |
| 141 | 119 |
| 142 void set_expected_data(const std::string& data) { expected_data_ = data; } | 120 void set_expected_data(const std::string& data) { expected_data_ = data; } |
| 143 | 121 |
| 144 // Helper functions. | 122 // Helper functions. |
| 145 // Create a file. Returns the complete file path. | 123 // Create a file. Returns the complete file path. |
| 146 base::FilePath CreateTestFile() { | 124 base::FilePath CreateTestFile() { |
| 147 base::FilePath file_name; | 125 base::FilePath file_name; |
| 148 BaseFile file(base::FilePath(), | 126 BaseFile file((net::BoundNetLog())); |
| 149 GURL(), | |
| 150 GURL(), | |
| 151 0, | |
| 152 false, | |
| 153 std::string(), | |
| 154 base::File(), | |
| 155 net::BoundNetLog()); | |
| 156 | 127 |
| 157 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 128 EXPECT_EQ( |
| 158 file.Initialize(temp_dir_.path())); | 129 DOWNLOAD_INTERRUPT_REASON_NONE, |
| 130 file.Initialize(base::FilePath(), temp_dir_.path(), base::File(), 0, | |
| 131 std::string(), scoped_ptr<crypto::SecureHash>())); | |
| 159 file_name = file.full_path(); | 132 file_name = file.full_path(); |
| 160 EXPECT_NE(base::FilePath::StringType(), file_name.value()); | 133 EXPECT_NE(base::FilePath::StringType(), file_name.value()); |
| 161 | 134 |
| 162 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 135 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 163 file.AppendDataToFile(kTestData4, kTestDataLength4)); | 136 file.AppendDataToFile(kTestData4, kTestDataLength4)); |
| 164 | 137 |
| 165 // Keep the file from getting deleted when existing_file_name is deleted. | 138 // Keep the file from getting deleted when existing_file_name is deleted. |
| 166 file.Detach(); | 139 file.Detach(); |
| 167 | 140 |
| 168 return file_name; | 141 return file_name; |
| 169 } | 142 } |
| 170 | 143 |
| 171 // Create a file with the specified file name. | 144 // Create a file with the specified file name. |
| 172 void CreateFileWithName(const base::FilePath& file_name) { | 145 void CreateFileWithName(const base::FilePath& file_name) { |
| 173 EXPECT_NE(base::FilePath::StringType(), file_name.value()); | 146 EXPECT_NE(base::FilePath::StringType(), file_name.value()); |
| 174 BaseFile duplicate_file(file_name, | 147 BaseFile duplicate_file((net::BoundNetLog())); |
| 175 GURL(), | |
| 176 GURL(), | |
| 177 0, | |
| 178 false, | |
| 179 std::string(), | |
| 180 base::File(), | |
| 181 net::BoundNetLog()); | |
| 182 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 148 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 183 duplicate_file.Initialize(temp_dir_.path())); | 149 duplicate_file.Initialize(file_name, temp_dir_.path(), |
| 150 base::File(), 0, std::string(), | |
| 151 scoped_ptr<crypto::SecureHash>())); | |
| 184 // Write something into it. | 152 // Write something into it. |
| 185 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); | 153 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); |
| 186 // Detach the file so it isn't deleted on destruction of |duplicate_file|. | 154 // Detach the file so it isn't deleted on destruction of |duplicate_file|. |
| 187 duplicate_file.Detach(); | 155 duplicate_file.Detach(); |
| 188 } | 156 } |
| 189 | 157 |
| 190 int64_t CurrentSpeedAtTime(base::TimeTicks current_time) { | 158 int64_t CurrentSpeedAtTime(base::TimeTicks current_time) { |
| 191 EXPECT_TRUE(base_file_.get()); | 159 EXPECT_TRUE(base_file_.get()); |
| 192 return base_file_->CurrentSpeedAtTime(current_time); | 160 return base_file_->CurrentSpeedAtTime(current_time); |
| 193 } | 161 } |
| 194 | 162 |
| 195 base::TimeTicks StartTick() { | 163 base::TimeTicks StartTick() { |
| 196 EXPECT_TRUE(base_file_.get()); | 164 EXPECT_TRUE(base_file_.get()); |
| 197 return base_file_->start_tick_; | 165 return base_file_->start_tick_; |
| 198 } | 166 } |
| 199 | 167 |
| 200 void set_expected_error(DownloadInterruptReason err) { | 168 void set_expected_error(DownloadInterruptReason err) { |
| 201 expected_error_ = err; | 169 expected_error_ = err; |
| 202 } | 170 } |
| 203 | 171 |
| 204 void ExpectPermissionError(DownloadInterruptReason err) { | 172 void ExpectPermissionError(DownloadInterruptReason err) { |
| 205 EXPECT_TRUE(err == DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR || | 173 EXPECT_TRUE(err == DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR || |
| 206 err == DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED) | 174 err == DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED) |
| 207 << "Interrupt reason = " << err; | 175 << "Interrupt reason = " << err; |
| 208 } | 176 } |
| 209 | 177 |
| 178 template <size_t SZ> | |
| 179 static void ExpectHashValue(const uint8_t (&expected_hash)[SZ], | |
| 180 scoped_ptr<crypto::SecureHash> hash_state) { | |
| 181 std::vector<char> hash_value(hash_state->GetHashLength()); | |
| 182 hash_state->Finish(&hash_value.front(), hash_value.size()); | |
| 183 ASSERT_EQ(hash_state->GetHashLength(), hash_value.size()); | |
| 184 ASSERT_EQ(SZ, hash_value.size()); | |
| 185 EXPECT_EQ(0, memcmp(expected_hash, &hash_value.front(), SZ)); | |
| 186 } | |
| 187 | |
| 210 protected: | 188 protected: |
| 211 // BaseClass instance we are testing. | 189 // BaseClass instance we are testing. |
| 212 scoped_ptr<BaseFile> base_file_; | 190 scoped_ptr<BaseFile> base_file_; |
| 213 | 191 |
| 214 // Temporary directory for renamed downloads. | 192 // Temporary directory for renamed downloads. |
| 215 base::ScopedTempDir temp_dir_; | 193 base::ScopedTempDir temp_dir_; |
| 216 | 194 |
| 217 // Expect the file to survive deletion of the BaseFile instance. | 195 // Expect the file to survive deletion of the BaseFile instance. |
| 218 bool expect_file_survives_; | 196 bool expect_file_survives_; |
| 219 | 197 |
| 220 // Expect the file to be in progress. | 198 // Expect the file to be in progress. |
| 221 bool expect_in_progress_; | 199 bool expect_in_progress_; |
| 222 | 200 |
| 223 // Hash calculator. | |
| 224 scoped_ptr<crypto::SecureHash> secure_hash_; | |
| 225 | |
| 226 unsigned char sha256_hash_[crypto::kSHA256Length]; | |
| 227 | |
| 228 private: | 201 private: |
| 229 // Keep track of what data should be saved to the disk file. | 202 // Keep track of what data should be saved to the disk file. |
| 230 std::string expected_data_; | 203 std::string expected_data_; |
| 231 DownloadInterruptReason expected_error_; | 204 DownloadInterruptReason expected_error_; |
| 232 | 205 |
| 233 // Mock file thread to satisfy debug checks in BaseFile. | 206 // Mock file thread to satisfy debug checks in BaseFile. |
| 234 base::MessageLoop message_loop_; | 207 base::MessageLoop message_loop_; |
| 235 BrowserThreadImpl file_thread_; | 208 BrowserThreadImpl file_thread_; |
| 236 }; | 209 }; |
| 237 | 210 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 259 TEST_F(BaseFileTest, WriteAndDetach) { | 232 TEST_F(BaseFileTest, WriteAndDetach) { |
| 260 ASSERT_TRUE(InitializeFile()); | 233 ASSERT_TRUE(InitializeFile()); |
| 261 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 234 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 262 base_file_->Finish(); | 235 base_file_->Finish(); |
| 263 base_file_->Detach(); | 236 base_file_->Detach(); |
| 264 expect_file_survives_ = true; | 237 expect_file_survives_ = true; |
| 265 } | 238 } |
| 266 | 239 |
| 267 // Write data to the file and detach it, and calculate its sha256 hash. | 240 // Write data to the file and detach it, and calculate its sha256 hash. |
| 268 TEST_F(BaseFileTest, WriteWithHashAndDetach) { | 241 TEST_F(BaseFileTest, WriteWithHashAndDetach) { |
| 269 // Calculate the final hash. | |
| 270 ResetHash(); | |
| 271 UpdateHash(kTestData1, kTestDataLength1); | |
| 272 std::string expected_hash = GetFinalHash(); | |
| 273 std::string expected_hash_hex = | |
| 274 base::HexEncode(expected_hash.data(), expected_hash.size()); | |
| 275 | |
| 276 MakeFileWithHash(); | |
| 277 ASSERT_TRUE(InitializeFile()); | 242 ASSERT_TRUE(InitializeFile()); |
| 278 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 243 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 279 base_file_->Finish(); | 244 ExpectHashValue(kHashOfTestData1, base_file_->Finish()); |
| 280 | |
| 281 std::string hash; | |
| 282 base_file_->GetHash(&hash); | |
| 283 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", | |
| 284 expected_hash_hex); | |
| 285 EXPECT_EQ(expected_hash_hex, base::HexEncode(hash.data(), hash.size())); | |
| 286 | |
| 287 base_file_->Detach(); | 245 base_file_->Detach(); |
| 288 expect_file_survives_ = true; | 246 expect_file_survives_ = true; |
| 289 } | 247 } |
| 290 | 248 |
| 291 // Rename the file after writing to it, then detach. | 249 // Rename the file after writing to it, then detach. |
| 292 TEST_F(BaseFileTest, WriteThenRenameAndDetach) { | 250 TEST_F(BaseFileTest, WriteThenRenameAndDetach) { |
| 293 ASSERT_TRUE(InitializeFile()); | 251 ASSERT_TRUE(InitializeFile()); |
| 294 | 252 |
| 295 base::FilePath initial_path(base_file_->full_path()); | 253 base::FilePath initial_path(base_file_->full_path()); |
| 296 EXPECT_TRUE(base::PathExists(initial_path)); | 254 EXPECT_TRUE(base::PathExists(initial_path)); |
| 297 base::FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 255 base::FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
| 298 EXPECT_FALSE(base::PathExists(new_path)); | 256 EXPECT_FALSE(base::PathExists(new_path)); |
| 299 | 257 |
| 300 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 258 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 301 | 259 |
| 302 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path)); | 260 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path)); |
| 303 EXPECT_FALSE(base::PathExists(initial_path)); | 261 EXPECT_FALSE(base::PathExists(initial_path)); |
| 304 EXPECT_TRUE(base::PathExists(new_path)); | 262 EXPECT_TRUE(base::PathExists(new_path)); |
| 305 | 263 |
| 306 base_file_->Finish(); | 264 ExpectHashValue(kHashOfTestData1, base_file_->Finish()); |
| 307 base_file_->Detach(); | 265 base_file_->Detach(); |
| 308 expect_file_survives_ = true; | 266 expect_file_survives_ = true; |
| 309 } | 267 } |
| 310 | 268 |
| 311 // Write data to the file once. | 269 // Write data to the file once. |
| 312 TEST_F(BaseFileTest, SingleWrite) { | 270 TEST_F(BaseFileTest, SingleWrite) { |
| 313 ASSERT_TRUE(InitializeFile()); | 271 ASSERT_TRUE(InitializeFile()); |
| 314 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 272 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 315 base_file_->Finish(); | 273 ExpectHashValue(kHashOfTestData1, base_file_->Finish()); |
| 316 } | 274 } |
| 317 | 275 |
| 318 // Write data to the file multiple times. | 276 // Write data to the file multiple times. |
| 319 TEST_F(BaseFileTest, MultipleWrites) { | 277 TEST_F(BaseFileTest, MultipleWrites) { |
| 320 ASSERT_TRUE(InitializeFile()); | 278 ASSERT_TRUE(InitializeFile()); |
| 321 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 279 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 322 ASSERT_TRUE(AppendDataToFile(kTestData2)); | 280 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 323 ASSERT_TRUE(AppendDataToFile(kTestData3)); | 281 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
| 324 std::string hash; | 282 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); |
| 325 EXPECT_FALSE(base_file_->GetHash(&hash)); | |
| 326 base_file_->Finish(); | |
| 327 } | |
| 328 | |
| 329 // Write data to the file once and calculate its sha256 hash. | |
| 330 TEST_F(BaseFileTest, SingleWriteWithHash) { | |
| 331 // Calculate the final hash. | |
| 332 ResetHash(); | |
| 333 UpdateHash(kTestData1, kTestDataLength1); | |
| 334 std::string expected_hash = GetFinalHash(); | |
| 335 std::string expected_hash_hex = | |
| 336 base::HexEncode(expected_hash.data(), expected_hash.size()); | |
| 337 | |
| 338 MakeFileWithHash(); | |
| 339 ASSERT_TRUE(InitializeFile()); | |
| 340 // Can get partial hash states before Finish() is called. | |
| 341 EXPECT_STRNE(std::string().c_str(), base_file_->GetHashState().c_str()); | |
| 342 ASSERT_TRUE(AppendDataToFile(kTestData1)); | |
| 343 EXPECT_STRNE(std::string().c_str(), base_file_->GetHashState().c_str()); | |
| 344 base_file_->Finish(); | |
| 345 | |
| 346 std::string hash; | |
| 347 base_file_->GetHash(&hash); | |
| 348 EXPECT_EQ(expected_hash_hex, base::HexEncode(hash.data(), hash.size())); | |
| 349 } | 283 } |
| 350 | 284 |
| 351 // Write data to the file multiple times and calculate its sha256 hash. | 285 // Write data to the file multiple times and calculate its sha256 hash. |
| 352 TEST_F(BaseFileTest, MultipleWritesWithHash) { | 286 TEST_F(BaseFileTest, MultipleWritesWithHash) { |
|
svaldez
2016/03/09 19:27:34
Isn't this just a duplicate of the MultipleWrites
asanka
2016/03/10 16:48:08
Good catch. Removed.
| |
| 353 // Calculate the final hash. | |
| 354 ResetHash(); | |
| 355 UpdateHash(kTestData1, kTestDataLength1); | |
| 356 UpdateHash(kTestData2, kTestDataLength2); | |
| 357 UpdateHash(kTestData3, kTestDataLength3); | |
| 358 std::string expected_hash = GetFinalHash(); | |
| 359 std::string expected_hash_hex = | |
| 360 base::HexEncode(expected_hash.data(), expected_hash.size()); | |
| 361 | |
| 362 std::string hash; | |
| 363 MakeFileWithHash(); | |
| 364 ASSERT_TRUE(InitializeFile()); | 287 ASSERT_TRUE(InitializeFile()); |
| 365 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 288 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 366 ASSERT_TRUE(AppendDataToFile(kTestData2)); | 289 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 367 ASSERT_TRUE(AppendDataToFile(kTestData3)); | 290 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
| 368 // No hash before Finish() is called. | 291 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); |
| 369 EXPECT_FALSE(base_file_->GetHash(&hash)); | |
| 370 base_file_->Finish(); | |
| 371 | |
| 372 EXPECT_TRUE(base_file_->GetHash(&hash)); | |
| 373 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8", | |
| 374 expected_hash_hex); | |
| 375 EXPECT_EQ(expected_hash_hex, base::HexEncode(hash.data(), hash.size())); | |
| 376 } | 292 } |
| 377 | 293 |
| 378 // Write data to the file multiple times, interrupt it, and continue using | 294 // Write data to the file multiple times, interrupt it, and continue using |
| 379 // another file. Calculate the resulting combined sha256 hash. | 295 // another file. Calculate the resulting combined sha256 hash. |
| 380 TEST_F(BaseFileTest, MultipleWritesInterruptedWithHash) { | 296 TEST_F(BaseFileTest, MultipleWritesInterruptedWithHash) { |
| 381 // Calculate the final hash. | |
| 382 ResetHash(); | |
| 383 UpdateHash(kTestData1, kTestDataLength1); | |
| 384 UpdateHash(kTestData2, kTestDataLength2); | |
| 385 UpdateHash(kTestData3, kTestDataLength3); | |
| 386 std::string expected_hash = GetFinalHash(); | |
| 387 std::string expected_hash_hex = | |
| 388 base::HexEncode(expected_hash.data(), expected_hash.size()); | |
| 389 | |
| 390 MakeFileWithHash(); | |
| 391 ASSERT_TRUE(InitializeFile()); | 297 ASSERT_TRUE(InitializeFile()); |
| 392 // Write some data | 298 // Write some data |
| 393 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 299 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 394 ASSERT_TRUE(AppendDataToFile(kTestData2)); | 300 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 395 // Get the hash state and file name. | 301 // Get the hash state and file name. |
| 396 std::string hash_state; | 302 scoped_ptr<crypto::SecureHash> hash_state = base_file_->Finish(); |
| 397 hash_state = base_file_->GetHashState(); | |
| 398 // Finish the file. | |
| 399 base_file_->Finish(); | |
| 400 | 303 |
| 401 base::FilePath new_file_path(temp_dir_.path().Append( | 304 base::FilePath new_file_path(temp_dir_.path().Append( |
| 402 base::FilePath(FILE_PATH_LITERAL("second_file")))); | 305 base::FilePath(FILE_PATH_LITERAL("second_file")))); |
| 403 | 306 |
| 404 ASSERT_TRUE(base::CopyFile(base_file_->full_path(), new_file_path)); | 307 ASSERT_TRUE(base::CopyFile(base_file_->full_path(), new_file_path)); |
| 405 | 308 |
| 406 // Create another file | 309 // Create another file |
| 407 BaseFile second_file(new_file_path, | 310 BaseFile second_file((net::BoundNetLog())); |
| 408 GURL(), | |
| 409 GURL(), | |
| 410 base_file_->bytes_so_far(), | |
| 411 true, | |
| 412 hash_state, | |
| 413 base::File(), | |
| 414 net::BoundNetLog()); | |
| 415 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 311 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 416 second_file.Initialize(base::FilePath())); | 312 second_file.Initialize(new_file_path, base::FilePath(), |
| 313 base::File(), base_file_->bytes_so_far(), | |
| 314 std::string(), std::move(hash_state))); | |
| 417 std::string data(kTestData3); | 315 std::string data(kTestData3); |
| 418 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 316 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 419 second_file.AppendDataToFile(data.data(), data.size())); | 317 second_file.AppendDataToFile(data.data(), data.size())); |
| 420 second_file.Finish(); | 318 ExpectHashValue(kHashOfTestData1To3, second_file.Finish()); |
| 421 | |
| 422 std::string hash; | |
| 423 EXPECT_TRUE(second_file.GetHash(&hash)); | |
| 424 // This will fail until getting the hash state is supported in SecureHash. | |
| 425 EXPECT_STREQ(expected_hash_hex.c_str(), | |
| 426 base::HexEncode(hash.data(), hash.size()).c_str()); | |
| 427 } | 319 } |
| 428 | 320 |
| 429 // Rename the file after all writes to it. | 321 // Rename the file after all writes to it. |
| 430 TEST_F(BaseFileTest, WriteThenRename) { | 322 TEST_F(BaseFileTest, WriteThenRename) { |
| 431 ASSERT_TRUE(InitializeFile()); | 323 ASSERT_TRUE(InitializeFile()); |
| 432 | 324 |
| 433 base::FilePath initial_path(base_file_->full_path()); | 325 base::FilePath initial_path(base_file_->full_path()); |
| 434 EXPECT_TRUE(base::PathExists(initial_path)); | 326 EXPECT_TRUE(base::PathExists(initial_path)); |
| 435 base::FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 327 base::FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
| 436 EXPECT_FALSE(base::PathExists(new_path)); | 328 EXPECT_FALSE(base::PathExists(new_path)); |
| 437 | 329 |
| 438 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 330 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 439 | 331 |
| 440 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 332 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 441 base_file_->Rename(new_path)); | 333 base_file_->Rename(new_path)); |
| 442 EXPECT_FALSE(base::PathExists(initial_path)); | 334 EXPECT_FALSE(base::PathExists(initial_path)); |
| 443 EXPECT_TRUE(base::PathExists(new_path)); | 335 EXPECT_TRUE(base::PathExists(new_path)); |
| 444 | 336 |
| 445 base_file_->Finish(); | 337 ExpectHashValue(kHashOfTestData1, base_file_->Finish()); |
| 446 } | 338 } |
| 447 | 339 |
| 448 // Rename the file while the download is still in progress. | 340 // Rename the file while the download is still in progress. |
| 449 TEST_F(BaseFileTest, RenameWhileInProgress) { | 341 TEST_F(BaseFileTest, RenameWhileInProgress) { |
| 450 ASSERT_TRUE(InitializeFile()); | 342 ASSERT_TRUE(InitializeFile()); |
| 451 | 343 |
| 452 base::FilePath initial_path(base_file_->full_path()); | 344 base::FilePath initial_path(base_file_->full_path()); |
| 453 EXPECT_TRUE(base::PathExists(initial_path)); | 345 EXPECT_TRUE(base::PathExists(initial_path)); |
| 454 base::FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 346 base::FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
| 455 EXPECT_FALSE(base::PathExists(new_path)); | 347 EXPECT_FALSE(base::PathExists(new_path)); |
| 456 | 348 |
| 457 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 349 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 458 | 350 |
| 459 EXPECT_TRUE(base_file_->in_progress()); | 351 EXPECT_TRUE(base_file_->in_progress()); |
| 460 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path)); | 352 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path)); |
| 461 EXPECT_FALSE(base::PathExists(initial_path)); | 353 EXPECT_FALSE(base::PathExists(initial_path)); |
| 462 EXPECT_TRUE(base::PathExists(new_path)); | 354 EXPECT_TRUE(base::PathExists(new_path)); |
| 463 | 355 |
| 464 ASSERT_TRUE(AppendDataToFile(kTestData2)); | 356 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 357 ASSERT_TRUE(AppendDataToFile(kTestData3)); | |
| 465 | 358 |
| 466 base_file_->Finish(); | 359 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); |
| 467 } | 360 } |
| 468 | 361 |
| 469 // Test that a failed rename reports the correct error. | 362 // Test that a failed rename reports the correct error. |
| 470 TEST_F(BaseFileTest, RenameWithError) { | 363 TEST_F(BaseFileTest, RenameWithError) { |
| 471 ASSERT_TRUE(InitializeFile()); | 364 ASSERT_TRUE(InitializeFile()); |
| 472 | 365 |
| 473 // TestDir is a subdirectory in |temp_dir_| that we will make read-only so | 366 // TestDir is a subdirectory in |temp_dir_| that we will make read-only so |
| 474 // that the rename will fail. | 367 // that the rename will fail. |
| 475 base::FilePath test_dir(temp_dir_.path().AppendASCII("TestDir")); | 368 base::FilePath test_dir(temp_dir_.path().AppendASCII("TestDir")); |
| 476 ASSERT_TRUE(base::CreateDirectory(test_dir)); | 369 ASSERT_TRUE(base::CreateDirectory(test_dir)); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 518 // Try to rename again, just for kicks. It should still fail. | 411 // Try to rename again, just for kicks. It should still fail. |
| 519 ExpectPermissionError(base_file_->Rename(new_path)); | 412 ExpectPermissionError(base_file_->Rename(new_path)); |
| 520 } | 413 } |
| 521 | 414 |
| 522 // Now that TestDir is writeable again, we should be able to successfully | 415 // Now that TestDir is writeable again, we should be able to successfully |
| 523 // rename the file. | 416 // rename the file. |
| 524 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path)); | 417 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path)); |
| 525 ASSERT_EQ(new_path.value(), base_file_->full_path().value()); | 418 ASSERT_EQ(new_path.value(), base_file_->full_path().value()); |
| 526 ASSERT_TRUE(AppendDataToFile(kTestData3)); | 419 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
| 527 | 420 |
| 528 base_file_->Finish(); | 421 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); |
| 529 | |
| 530 // The contents of the file should be intact. | |
| 531 std::string file_contents; | |
| 532 std::string expected_contents(kTestData1); | |
| 533 expected_contents += kTestData2; | |
| 534 expected_contents += kTestData3; | |
| 535 ASSERT_TRUE(base::ReadFileToString(new_path, &file_contents)); | |
| 536 EXPECT_EQ(expected_contents, file_contents); | |
| 537 } | 422 } |
| 538 | 423 |
| 539 // Test that a failed write reports an error. | 424 // Test that a failed write reports an error. |
| 540 TEST_F(BaseFileTest, WriteWithError) { | 425 TEST_F(BaseFileTest, WriteWithError) { |
| 541 base::FilePath path; | 426 base::FilePath path; |
| 542 ASSERT_TRUE(base::CreateTemporaryFile(&path)); | 427 ASSERT_TRUE(base::CreateTemporaryFile(&path)); |
| 543 | 428 |
| 544 // Pass a file handle which was opened without the WRITE flag. | 429 // Pass a file handle which was opened without the WRITE flag. |
| 545 // This should result in an error when writing. | 430 // This should result in an error when writing. |
| 546 base::File file(path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ); | 431 base::File file(path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ); |
| 547 base_file_.reset(new BaseFile(path, GURL(), GURL(), 0, false, std::string(), | 432 base_file_.reset(new BaseFile(net::BoundNetLog())); |
| 548 std::move(file), net::BoundNetLog())); | 433 EXPECT_EQ( |
| 549 ASSERT_TRUE(InitializeFile()); | 434 DOWNLOAD_INTERRUPT_REASON_NONE, |
| 435 base_file_->Initialize(path, base::FilePath(), std::move(file), 0, | |
| 436 std::string(), scoped_ptr<crypto::SecureHash>())); | |
| 550 #if defined(OS_WIN) | 437 #if defined(OS_WIN) |
| 551 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 438 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
| 552 #elif defined (OS_POSIX) | 439 #elif defined (OS_POSIX) |
| 553 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); | 440 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
| 554 #endif | 441 #endif |
| 555 ASSERT_FALSE(AppendDataToFile(kTestData1)); | 442 ASSERT_FALSE(AppendDataToFile(kTestData1)); |
| 556 base_file_->Finish(); | 443 base_file_->Finish(); |
| 557 } | 444 } |
| 558 | 445 |
| 559 // Try to write to uninitialized file. | 446 // Try to write to uninitialized file. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 573 CreateFileWithName(base_file_->full_path()); | 460 CreateFileWithName(base_file_->full_path()); |
| 574 | 461 |
| 575 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 462 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 576 base_file_->Finish(); | 463 base_file_->Finish(); |
| 577 } | 464 } |
| 578 | 465 |
| 579 // Create a file and append to it. | 466 // Create a file and append to it. |
| 580 TEST_F(BaseFileTest, AppendToBaseFile) { | 467 TEST_F(BaseFileTest, AppendToBaseFile) { |
| 581 // Create a new file. | 468 // Create a new file. |
| 582 base::FilePath existing_file_name = CreateTestFile(); | 469 base::FilePath existing_file_name = CreateTestFile(); |
| 583 | |
| 584 set_expected_data(kTestData4); | 470 set_expected_data(kTestData4); |
| 585 | 471 |
| 586 // Use the file we've just created. | 472 // Use the file we've just created. |
| 587 base_file_.reset(new BaseFile(existing_file_name, | 473 base_file_.reset(new BaseFile(net::BoundNetLog())); |
| 588 GURL(), | 474 ASSERT_EQ( |
| 589 GURL(), | 475 DOWNLOAD_INTERRUPT_REASON_NONE, |
| 590 kTestDataLength4, | 476 base_file_->Initialize(existing_file_name, base::FilePath(), base::File(), |
| 591 false, | 477 kTestDataLength4, std::string(), |
| 592 std::string(), | 478 scoped_ptr<crypto::SecureHash>())); |
| 593 base::File(), | |
| 594 net::BoundNetLog())); | |
| 595 | |
| 596 ASSERT_TRUE(InitializeFile()); | |
| 597 | 479 |
| 598 const base::FilePath file_name = base_file_->full_path(); | 480 const base::FilePath file_name = base_file_->full_path(); |
| 599 EXPECT_NE(base::FilePath::StringType(), file_name.value()); | 481 EXPECT_NE(base::FilePath::StringType(), file_name.value()); |
| 600 | 482 |
| 601 // Write into the file. | 483 // Write into the file. |
| 602 EXPECT_TRUE(AppendDataToFile(kTestData1)); | 484 EXPECT_TRUE(AppendDataToFile(kTestData1)); |
| 603 | 485 |
| 604 base_file_->Finish(); | 486 base_file_->Finish(); |
| 605 base_file_->Detach(); | 487 base_file_->Detach(); |
| 606 expect_file_survives_ = true; | 488 expect_file_survives_ = true; |
| 607 } | 489 } |
| 608 | 490 |
| 609 // Create a read-only file and attempt to write to it. | 491 // Create a read-only file and attempt to write to it. |
| 610 TEST_F(BaseFileTest, ReadonlyBaseFile) { | 492 TEST_F(BaseFileTest, ReadonlyBaseFile) { |
| 611 // Create a new file. | 493 // Create a new file. |
| 612 base::FilePath readonly_file_name = CreateTestFile(); | 494 base::FilePath readonly_file_name = CreateTestFile(); |
| 613 | 495 |
| 614 // Restore permissions to the file when we are done with this test. | 496 // Restore permissions to the file when we are done with this test. |
| 615 base::FilePermissionRestorer restore_permissions(readonly_file_name); | 497 base::FilePermissionRestorer restore_permissions(readonly_file_name); |
| 616 | 498 |
| 617 // Make it read-only. | 499 // Make it read-only. |
| 618 EXPECT_TRUE(base::MakeFileUnwritable(readonly_file_name)); | 500 EXPECT_TRUE(base::MakeFileUnwritable(readonly_file_name)); |
| 619 | 501 |
| 620 // Try to overwrite it. | 502 // Try to overwrite it. |
| 621 base_file_.reset(new BaseFile(readonly_file_name, | 503 base_file_.reset(new BaseFile(net::BoundNetLog())); |
| 622 GURL(), | 504 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, |
| 623 GURL(), | 505 base_file_->Initialize(readonly_file_name, base::FilePath(), |
| 624 0, | 506 base::File(), 0, std::string(), |
| 625 false, | 507 scoped_ptr<crypto::SecureHash>())); |
| 626 std::string(), | |
| 627 base::File(), | |
| 628 net::BoundNetLog())); | |
| 629 | 508 |
| 630 expect_in_progress_ = false; | 509 expect_in_progress_ = false; |
| 631 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | |
| 632 EXPECT_FALSE(InitializeFile()); | |
| 633 | 510 |
| 634 const base::FilePath file_name = base_file_->full_path(); | 511 const base::FilePath file_name = base_file_->full_path(); |
| 635 EXPECT_NE(base::FilePath::StringType(), file_name.value()); | 512 EXPECT_NE(base::FilePath::StringType(), file_name.value()); |
| 636 | 513 |
| 637 // Write into the file. | 514 // Write into the file. |
| 638 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); | 515 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
| 639 EXPECT_FALSE(AppendDataToFile(kTestData1)); | 516 EXPECT_FALSE(AppendDataToFile(kTestData1)); |
| 640 | 517 |
| 641 base_file_->Finish(); | 518 base_file_->Finish(); |
| 642 base_file_->Detach(); | 519 base_file_->Detach(); |
| 643 expect_file_survives_ = true; | 520 expect_file_survives_ = true; |
| 644 } | 521 } |
| 645 | 522 |
| 646 TEST_F(BaseFileTest, IsEmptyHash) { | 523 // Open an existing file and continue writing to it. The hash of the partial |
| 647 std::string empty(crypto::kSHA256Length, '\x00'); | 524 // file is known and matches the existing contents. |
| 648 EXPECT_TRUE(BaseFile::IsEmptyHash(empty)); | 525 TEST_F(BaseFileTest, ExistingBaseFileKnownHash) { |
| 649 std::string not_empty(crypto::kSHA256Length, '\x01'); | 526 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); |
| 650 EXPECT_FALSE(BaseFile::IsEmptyHash(not_empty)); | 527 ASSERT_TRUE(base::WriteFile(file_path, kTestData1, kTestDataLength1)); |
| 651 EXPECT_FALSE(BaseFile::IsEmptyHash(std::string())); | |
| 652 | 528 |
| 653 std::string also_not_empty = empty; | 529 std::string hash_so_far(std::begin(kHashOfTestData1), |
| 654 also_not_empty[crypto::kSHA256Length - 1] = '\x01'; | 530 std::end(kHashOfTestData1)); |
| 655 EXPECT_FALSE(BaseFile::IsEmptyHash(also_not_empty)); | 531 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 532 base_file_->Initialize(file_path, base::FilePath(), base::File(), | |
| 533 kTestDataLength1, hash_so_far, | |
| 534 scoped_ptr<crypto::SecureHash>())); | |
| 535 set_expected_data(kTestData1); | |
| 536 ASSERT_TRUE(AppendDataToFile(kTestData2)); | |
| 537 ASSERT_TRUE(AppendDataToFile(kTestData3)); | |
| 538 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); | |
| 539 } | |
| 540 | |
| 541 // Open an existing file and continue writing to it. The hash of the partial | |
| 542 // file is unknown. | |
| 543 TEST_F(BaseFileTest, ExistingBaseFileUnknownHash) { | |
| 544 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); | |
| 545 ASSERT_TRUE(base::WriteFile(file_path, kTestData1, kTestDataLength1)); | |
| 546 | |
| 547 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | |
| 548 base_file_->Initialize(file_path, base::FilePath(), base::File(), | |
| 549 kTestDataLength1, std::string(), | |
| 550 scoped_ptr<crypto::SecureHash>())); | |
| 551 set_expected_data(kTestData1); | |
| 552 ASSERT_TRUE(AppendDataToFile(kTestData2)); | |
| 553 ASSERT_TRUE(AppendDataToFile(kTestData3)); | |
| 554 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); | |
| 555 } | |
| 556 | |
| 557 // Open an existing file. The contentsof the file doesn't match the known hash. | |
| 558 TEST_F(BaseFileTest, ExistingBaseFileIncorrectHash) { | |
| 559 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); | |
| 560 ASSERT_TRUE(base::WriteFile(file_path, kTestData2, kTestDataLength2)); | |
| 561 | |
| 562 std::string hash_so_far(std::begin(kHashOfTestData1), | |
| 563 std::end(kHashOfTestData1)); | |
| 564 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH, | |
| 565 base_file_->Initialize(file_path, base::FilePath(), base::File(), | |
| 566 kTestDataLength2, hash_so_far, | |
| 567 scoped_ptr<crypto::SecureHash>())); | |
| 568 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH); | |
| 569 } | |
| 570 | |
| 571 // Open a large existing file with a known hash and continue writing to it. | |
| 572 TEST_F(BaseFileTest, ExistingBaseFileLargeSizeKnownHash) { | |
| 573 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); | |
| 574 std::string big_buffer(1024 * 200, 'a'); | |
| 575 ASSERT_TRUE(base::WriteFile(file_path, big_buffer.data(), big_buffer.size())); | |
| 576 | |
| 577 // Hash of partial file (1024*200 * 'a') | |
| 578 const uint8_t kExpectedPartialHash[] = { | |
| 579 0x4b, 0x4f, 0x0f, 0x46, 0xac, 0x02, 0xd1, 0x77, 0xde, 0xa0, 0xab, | |
| 580 0x36, 0xa6, 0x6a, 0x65, 0x78, 0x40, 0xe2, 0xfb, 0x98, 0xb2, 0x0b, | |
| 581 0xb2, 0x7a, 0x68, 0x8d, 0xb4, 0xd8, 0xea, 0x9c, 0xd2, 0x2c}; | |
| 582 | |
| 583 // Hash of entire file (1024*400 * 'a') | |
| 584 const uint8_t kExpectedFullHash[] = { | |
| 585 0x0c, 0xe9, 0xf6, 0x78, 0x6b, 0x0f, 0x58, 0x49, 0x36, 0xe8, 0x83, | |
| 586 0xc5, 0x09, 0x16, 0xbc, 0x5e, 0x2d, 0x07, 0x95, 0xb9, 0x42, 0x20, | |
| 587 0x41, 0x7c, 0xb3, 0x38, 0xd3, 0xf4, 0xe0, 0x78, 0x89, 0x46}; | |
| 588 | |
| 589 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | |
| 590 base_file_->Initialize(file_path, base::FilePath(), base::File(), | |
| 591 big_buffer.size(), | |
| 592 std::string(std::begin(kExpectedPartialHash), | |
| 593 std::end(kExpectedPartialHash)), | |
| 594 scoped_ptr<crypto::SecureHash>())); | |
| 595 set_expected_data(big_buffer); // Contents of the file on Open. | |
| 596 ASSERT_TRUE(AppendDataToFile(big_buffer)); | |
| 597 ExpectHashValue(kExpectedFullHash, base_file_->Finish()); | |
| 598 } | |
| 599 | |
| 600 // Open a large existing file. The contents doesn't match the known hash. | |
| 601 TEST_F(BaseFileTest, ExistingBaseFileLargeSizeIncorrectHash) { | |
| 602 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); | |
| 603 std::string big_buffer(1024 * 200, 'a'); | |
| 604 ASSERT_TRUE(base::WriteFile(file_path, big_buffer.data(), big_buffer.size())); | |
| 605 | |
| 606 // Incorrect hash of partial file (1024*200 * 'a') | |
| 607 const uint8_t kExpectedPartialHash[] = { | |
| 608 0xc2, 0xa9, 0x08, 0xd9, 0x8f, 0x5d, 0xf9, 0x87, 0xad, 0xe4, 0x1b, | |
| 609 0x5f, 0xce, 0x21, 0x30, 0x67, 0xef, 0x6c, 0xc2, 0x1e, 0xf2, 0x24, | |
| 610 0x02, 0x12, 0xa4, 0x1e, 0x54, 0xb5, 0xe7, 0xc2, 0x8a, 0xe5}; | |
| 611 | |
| 612 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH, | |
| 613 base_file_->Initialize(file_path, base::FilePath(), base::File(), | |
| 614 big_buffer.size(), | |
| 615 std::string(std::begin(kExpectedPartialHash), | |
| 616 std::end(kExpectedPartialHash)), | |
| 617 scoped_ptr<crypto::SecureHash>())); | |
| 618 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH); | |
| 619 } | |
| 620 | |
| 621 // Open an existing file. The size of the file is too short. | |
| 622 TEST_F(BaseFileTest, ExistingBaseFileTooShort) { | |
| 623 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); | |
| 624 ASSERT_TRUE(base::WriteFile(file_path, kTestData1, kTestDataLength1)); | |
| 625 | |
| 626 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT, | |
| 627 base_file_->Initialize(file_path, base::FilePath(), base::File(), | |
| 628 kTestDataLength1 + 1, std::string(), | |
| 629 scoped_ptr<crypto::SecureHash>())); | |
| 630 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT); | |
| 631 } | |
| 632 | |
| 633 // Open an existing file. The size is larger than expected. | |
| 634 TEST_F(BaseFileTest, ExistingBaseFileKnownHashTooLong) { | |
| 635 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); | |
| 636 std::string contents; | |
| 637 contents.append(kTestData1); | |
| 638 contents.append("Something extra"); | |
| 639 ASSERT_TRUE(base::WriteFile(file_path, contents.data(), contents.size())); | |
| 640 | |
| 641 std::string hash_so_far(std::begin(kHashOfTestData1), | |
| 642 std::end(kHashOfTestData1)); | |
| 643 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | |
| 644 base_file_->Initialize(file_path, base::FilePath(), base::File(), | |
| 645 kTestDataLength1, hash_so_far, | |
| 646 scoped_ptr<crypto::SecureHash>())); | |
| 647 set_expected_data(kTestData1); // Our starting position. | |
| 648 ASSERT_TRUE(AppendDataToFile(kTestData2)); | |
| 649 ASSERT_TRUE(AppendDataToFile(kTestData3)); | |
| 650 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); | |
| 651 } | |
| 652 | |
| 653 // Open an existing file. The size is large than expected and the hash is | |
| 654 // unknown. | |
| 655 TEST_F(BaseFileTest, ExistingBaseFileUnknownHashTooLong) { | |
| 656 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); | |
| 657 std::string contents; | |
| 658 contents.append(kTestData1); | |
| 659 contents.append("Something extra"); | |
| 660 ASSERT_TRUE(base::WriteFile(file_path, contents.data(), contents.size())); | |
| 661 | |
| 662 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | |
| 663 base_file_->Initialize(file_path, base::FilePath(), base::File(), | |
| 664 kTestDataLength1, std::string(), | |
| 665 scoped_ptr<crypto::SecureHash>())); | |
| 666 set_expected_data(kTestData1); | |
| 667 ASSERT_TRUE(AppendDataToFile(kTestData2)); | |
| 668 ASSERT_TRUE(AppendDataToFile(kTestData3)); | |
| 669 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); | |
| 656 } | 670 } |
| 657 | 671 |
| 658 // Test that a temporary file is created in the default download directory. | 672 // Test that a temporary file is created in the default download directory. |
| 659 TEST_F(BaseFileTest, CreatedInDefaultDirectory) { | 673 TEST_F(BaseFileTest, CreatedInDefaultDirectory) { |
| 660 ASSERT_TRUE(base_file_->full_path().empty()); | 674 ASSERT_TRUE(base_file_->full_path().empty()); |
| 661 ASSERT_TRUE(InitializeFile()); | 675 ASSERT_TRUE(InitializeFile()); |
| 662 EXPECT_FALSE(base_file_->full_path().empty()); | 676 EXPECT_FALSE(base_file_->full_path().empty()); |
| 663 | 677 |
| 664 // On Windows, CreateTemporaryFileInDir() will cause a path with short names | 678 // On Windows, CreateTemporaryFileInDir() will cause a path with short names |
| 665 // to be expanded into a path with long names. Thus temp_dir.path() might not | 679 // to be expanded into a path with long names. Thus temp_dir.path() might not |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 684 | 698 |
| 685 const char kData[] = "hello"; | 699 const char kData[] = "hello"; |
| 686 const int kDataLength = static_cast<int>(arraysize(kData) - 1); | 700 const int kDataLength = static_cast<int>(arraysize(kData) - 1); |
| 687 ASSERT_EQ(kDataLength, base::WriteFile(full_path, kData, kDataLength)); | 701 ASSERT_EQ(kDataLength, base::WriteFile(full_path, kData, kDataLength)); |
| 688 // The file that we created here should stick around when the BaseFile is | 702 // The file that we created here should stick around when the BaseFile is |
| 689 // destroyed during TearDown. | 703 // destroyed during TearDown. |
| 690 expect_file_survives_ = true; | 704 expect_file_survives_ = true; |
| 691 } | 705 } |
| 692 | 706 |
| 693 } // namespace content | 707 } // namespace content |
| OLD | NEW |