| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/pickle.h" |
| 10 #include "base/scoped_temp_dir.h" | 11 #include "base/scoped_temp_dir.h" |
| 11 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
| 12 #include "base/test/test_file_util.h" | 13 #include "base/test/test_file_util.h" |
| 13 #include "content/browser/browser_thread_impl.h" | 14 #include "content/browser/browser_thread_impl.h" |
| 14 #include "net/base/file_stream.h" | 15 #include "net/base/file_stream.h" |
| 15 #include "net/base/mock_file_stream.h" | 16 #include "net/base/mock_file_stream.h" |
| 16 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 using content::BrowserThread; | 20 using content::BrowserThread; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 37 BaseFileTest() | 38 BaseFileTest() |
| 38 : expect_file_survives_(false), | 39 : expect_file_survives_(false), |
| 39 expect_in_progress_(true), | 40 expect_in_progress_(true), |
| 40 expected_error_(false), | 41 expected_error_(false), |
| 41 file_thread_(BrowserThread::FILE, &message_loop_) { | 42 file_thread_(BrowserThread::FILE, &message_loop_) { |
| 42 } | 43 } |
| 43 | 44 |
| 44 virtual void SetUp() { | 45 virtual void SetUp() { |
| 45 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 46 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 46 base_file_.reset( | 47 base_file_.reset( |
| 47 new BaseFile(FilePath(), GURL(), GURL(), 0, file_stream_)); | 48 new BaseFile(FilePath(), GURL(), GURL(), 0, Pickle(), file_stream_)); |
| 48 } | 49 } |
| 49 | 50 |
| 50 virtual void TearDown() { | 51 virtual void TearDown() { |
| 51 EXPECT_FALSE(base_file_->in_progress()); | 52 EXPECT_FALSE(base_file_->in_progress()); |
| 52 if (!expected_error_) { | 53 if (!expected_error_) { |
| 53 EXPECT_EQ(static_cast<int64>(expected_data_.size()), | 54 EXPECT_EQ(static_cast<int64>(expected_data_.size()), |
| 54 base_file_->bytes_so_far()); | 55 base_file_->bytes_so_far()); |
| 55 } | 56 } |
| 56 | 57 |
| 57 FilePath full_path = base_file_->full_path(); | 58 FilePath full_path = base_file_->full_path(); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 return appended; | 112 return appended; |
| 112 } | 113 } |
| 113 | 114 |
| 114 void set_expected_data(const std::string& data) { expected_data_ = data; } | 115 void set_expected_data(const std::string& data) { expected_data_ = data; } |
| 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 static FilePath CreateTestFile() { | 119 static FilePath CreateTestFile() { |
| 119 FilePath file_name; | 120 FilePath file_name; |
| 120 linked_ptr<net::FileStream> dummy_file_stream; | 121 linked_ptr<net::FileStream> dummy_file_stream; |
| 121 BaseFile file(FilePath(), GURL(), GURL(), 0, dummy_file_stream); | 122 BaseFile file(FilePath(), GURL(), GURL(), 0, Pickle(), dummy_file_stream); |
| 122 | 123 |
| 123 EXPECT_EQ(net::OK, file.Initialize(false)); | 124 EXPECT_EQ(net::OK, file.Initialize(false)); |
| 124 file_name = file.full_path(); | 125 file_name = file.full_path(); |
| 125 EXPECT_NE(FilePath::StringType(), file_name.value()); | 126 EXPECT_NE(FilePath::StringType(), file_name.value()); |
| 126 | 127 |
| 127 EXPECT_EQ(net::OK, file.AppendDataToFile(kTestData4, kTestDataLength4)); | 128 EXPECT_EQ(net::OK, file.AppendDataToFile(kTestData4, kTestDataLength4)); |
| 128 | 129 |
| 129 // Keep the file from getting deleted when existing_file_name is deleted. | 130 // Keep the file from getting deleted when existing_file_name is deleted. |
| 130 file.Detach(); | 131 file.Detach(); |
| 131 | 132 |
| 132 return file_name; | 133 return file_name; |
| 133 } | 134 } |
| 134 | 135 |
| 135 // Create a file with the specified file name. | 136 // Create a file with the specified file name. |
| 136 static void CreateFileWithName(const FilePath& file_name) { | 137 static void CreateFileWithName(const FilePath& file_name) { |
| 137 EXPECT_NE(FilePath::StringType(), file_name.value()); | 138 EXPECT_NE(FilePath::StringType(), file_name.value()); |
| 138 linked_ptr<net::FileStream> dummy_file_stream; | 139 linked_ptr<net::FileStream> dummy_file_stream; |
| 139 BaseFile duplicate_file(file_name, GURL(), GURL(), 0, dummy_file_stream); | 140 BaseFile duplicate_file( |
| 141 file_name, GURL(), GURL(), 0, Pickle(), dummy_file_stream); |
| 140 EXPECT_EQ(net::OK, duplicate_file.Initialize(false)); | 142 EXPECT_EQ(net::OK, duplicate_file.Initialize(false)); |
| 141 // Write something into it. | 143 // Write something into it. |
| 142 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); | 144 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); |
| 143 // Detach the file so it isn't deleted on destruction of |duplicate_file|. | 145 // Detach the file so it isn't deleted on destruction of |duplicate_file|. |
| 144 duplicate_file.Detach(); | 146 duplicate_file.Detach(); |
| 145 } | 147 } |
| 146 | 148 |
| 147 protected: | 149 protected: |
| 148 linked_ptr<net::FileStream> file_stream_; | 150 linked_ptr<net::FileStream> file_stream_; |
| 149 linked_ptr<net::testing::MockFileStream> mock_file_stream_; | 151 linked_ptr<net::testing::MockFileStream> mock_file_stream_; |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 246 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 245 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | 247 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); |
| 246 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); | 248 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); |
| 247 std::string hash; | 249 std::string hash; |
| 248 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); | 250 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); |
| 249 base_file_->Finish(); | 251 base_file_->Finish(); |
| 250 } | 252 } |
| 251 | 253 |
| 252 // Write data to the file once and calculate its sha256 hash. | 254 // Write data to the file once and calculate its sha256 hash. |
| 253 TEST_F(BaseFileTest, SingleWriteWithHash) { | 255 TEST_F(BaseFileTest, SingleWriteWithHash) { |
| 256 std::string partial_hash; |
| 254 ASSERT_EQ(net::OK, base_file_->Initialize(true)); | 257 ASSERT_EQ(net::OK, base_file_->Initialize(true)); |
| 258 // Can get partial hashes before Finish() is called. |
| 259 EXPECT_TRUE(base_file_->GetSha256Hash(&partial_hash)); |
| 255 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 260 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 261 EXPECT_TRUE(base_file_->GetSha256Hash(&partial_hash)); |
| 256 base_file_->Finish(); | 262 base_file_->Finish(); |
| 257 | 263 |
| 258 std::string hash; | 264 std::string hash; |
| 259 base_file_->GetSha256Hash(&hash); | 265 base_file_->GetSha256Hash(&hash); |
| 260 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", | 266 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", |
| 261 base::HexEncode(hash.data(), hash.size())); | 267 base::HexEncode(hash.data(), hash.size())); |
| 262 } | 268 } |
| 263 | 269 |
| 264 // Write data to the file multiple times and calculate its sha256 hash. | 270 // Write data to the file multiple times and calculate its sha256 hash. |
| 265 TEST_F(BaseFileTest, MultipleWritesWithHash) { | 271 TEST_F(BaseFileTest, MultipleWritesWithHash) { |
| 266 std::string hash; | 272 std::string hash; |
| 267 | 273 |
| 268 ASSERT_EQ(net::OK, base_file_->Initialize(true)); | 274 ASSERT_EQ(net::OK, base_file_->Initialize(true)); |
| 269 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 275 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 270 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | 276 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); |
| 271 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); | 277 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); |
| 272 // no hash before Finish() is called either. | |
| 273 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); | |
| 274 base_file_->Finish(); | 278 base_file_->Finish(); |
| 275 | 279 |
| 276 EXPECT_TRUE(base_file_->GetSha256Hash(&hash)); | 280 EXPECT_TRUE(base_file_->GetSha256Hash(&hash)); |
| 277 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8", | 281 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8", |
| 278 base::HexEncode(hash.data(), hash.size())); | 282 base::HexEncode(hash.data(), hash.size())); |
| 279 } | 283 } |
| 280 | 284 |
| 285 // Write data to the file multiple times, interrupt it, and continue using |
| 286 // another file. Calculate the resulting combined sha256 hash. |
| 287 TEST_F(BaseFileTest, MultipleWritesInterruptedWithHash) { |
| 288 ASSERT_EQ(net::OK, base_file_->Initialize(true)); |
| 289 // Write some data |
| 290 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 291 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); |
| 292 // Get the hash state and file name. |
| 293 Pickle hash_state; |
| 294 base_file_->GetSha256HashState(&hash_state); |
| 295 // Finish the file. |
| 296 base_file_->Finish(); |
| 297 |
| 298 // Create another file |
| 299 linked_ptr<net::FileStream> second_stream; |
| 300 BaseFile second_file(FilePath(), |
| 301 GURL(), |
| 302 GURL(), |
| 303 base_file_->bytes_so_far(), |
| 304 hash_state, |
| 305 second_stream); |
| 306 ASSERT_EQ(net::OK, second_file.Initialize(true)); |
| 307 std::string data(kTestData3); |
| 308 EXPECT_EQ(net::OK, second_file.AppendDataToFile(data.data(), data.size())); |
| 309 second_file.Finish(); |
| 310 |
| 311 std::string hash; |
| 312 EXPECT_TRUE(second_file.GetSha256Hash(&hash)); |
| 313 // This will fail until getting the hash state is supported in SecureHash. |
| 314 EXPECT_STREQ( |
| 315 "CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8", |
| 316 base::HexEncode(hash.data(), hash.size()).c_str()); |
| 317 } |
| 318 |
| 281 // Rename the file after all writes to it. | 319 // Rename the file after all writes to it. |
| 282 TEST_F(BaseFileTest, WriteThenRename) { | 320 TEST_F(BaseFileTest, WriteThenRename) { |
| 283 ASSERT_EQ(net::OK, base_file_->Initialize(false)); | 321 ASSERT_EQ(net::OK, base_file_->Initialize(false)); |
| 284 | 322 |
| 285 FilePath initial_path(base_file_->full_path()); | 323 FilePath initial_path(base_file_->full_path()); |
| 286 EXPECT_TRUE(file_util::PathExists(initial_path)); | 324 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 287 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 325 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
| 288 EXPECT_FALSE(file_util::PathExists(new_path)); | 326 EXPECT_FALSE(file_util::PathExists(new_path)); |
| 289 | 327 |
| 290 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 328 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 314 | 352 |
| 315 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | 353 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); |
| 316 | 354 |
| 317 base_file_->Finish(); | 355 base_file_->Finish(); |
| 318 } | 356 } |
| 319 | 357 |
| 320 // Write data to the file multiple times. | 358 // Write data to the file multiple times. |
| 321 TEST_F(BaseFileTest, MultipleWritesWithError) { | 359 TEST_F(BaseFileTest, MultipleWritesWithError) { |
| 322 ASSERT_TRUE(OpenMockFileStream()); | 360 ASSERT_TRUE(OpenMockFileStream()); |
| 323 base_file_.reset(new BaseFile(mock_file_stream_->get_path(), | 361 base_file_.reset(new BaseFile(mock_file_stream_->get_path(), |
| 324 GURL(), GURL(), 0, mock_file_stream_)); | 362 GURL(), |
| 363 GURL(), |
| 364 0, |
| 365 Pickle(), |
| 366 mock_file_stream_)); |
| 325 EXPECT_EQ(net::OK, base_file_->Initialize(false)); | 367 EXPECT_EQ(net::OK, base_file_->Initialize(false)); |
| 326 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 368 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 327 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | 369 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); |
| 328 ForceError(net::ERR_ACCESS_DENIED); | 370 ForceError(net::ERR_ACCESS_DENIED); |
| 329 ASSERT_NE(net::OK, AppendDataToFile(kTestData3)); | 371 ASSERT_NE(net::OK, AppendDataToFile(kTestData3)); |
| 330 std::string hash; | 372 std::string hash; |
| 331 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); | 373 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); |
| 332 base_file_->Finish(); | 374 base_file_->Finish(); |
| 333 } | 375 } |
| 334 | 376 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 352 } | 394 } |
| 353 | 395 |
| 354 // Create a file and append to it. | 396 // Create a file and append to it. |
| 355 TEST_F(BaseFileTest, AppendToBaseFile) { | 397 TEST_F(BaseFileTest, AppendToBaseFile) { |
| 356 // Create a new file. | 398 // Create a new file. |
| 357 FilePath existing_file_name = CreateTestFile(); | 399 FilePath existing_file_name = CreateTestFile(); |
| 358 | 400 |
| 359 set_expected_data(kTestData4); | 401 set_expected_data(kTestData4); |
| 360 | 402 |
| 361 // Use the file we've just created. | 403 // Use the file we've just created. |
| 362 base_file_.reset( | 404 base_file_.reset(new BaseFile(existing_file_name, |
| 363 new BaseFile(existing_file_name, GURL(), GURL(), kTestDataLength4, | 405 GURL(), |
| 364 file_stream_)); | 406 GURL(), |
| 407 kTestDataLength4, |
| 408 Pickle(), |
| 409 file_stream_)); |
| 365 | 410 |
| 366 EXPECT_EQ(net::OK, base_file_->Initialize(false)); | 411 EXPECT_EQ(net::OK, base_file_->Initialize(false)); |
| 367 | 412 |
| 368 const FilePath file_name = base_file_->full_path(); | 413 const FilePath file_name = base_file_->full_path(); |
| 369 EXPECT_NE(FilePath::StringType(), file_name.value()); | 414 EXPECT_NE(FilePath::StringType(), file_name.value()); |
| 370 | 415 |
| 371 // Write into the file. | 416 // Write into the file. |
| 372 EXPECT_EQ(net::OK, AppendDataToFile(kTestData1)); | 417 EXPECT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 373 | 418 |
| 374 base_file_->Finish(); | 419 base_file_->Finish(); |
| 375 base_file_->Detach(); | 420 base_file_->Detach(); |
| 376 expect_file_survives_ = true; | 421 expect_file_survives_ = true; |
| 377 } | 422 } |
| 378 | 423 |
| 379 // Create a read-only file and attempt to write to it. | 424 // Create a read-only file and attempt to write to it. |
| 380 TEST_F(BaseFileTest, ReadonlyBaseFile) { | 425 TEST_F(BaseFileTest, ReadonlyBaseFile) { |
| 381 // Create a new file. | 426 // Create a new file. |
| 382 FilePath readonly_file_name = CreateTestFile(); | 427 FilePath readonly_file_name = CreateTestFile(); |
| 383 | 428 |
| 384 // Make it read-only. | 429 // Make it read-only. |
| 385 EXPECT_TRUE(file_util::MakeFileUnwritable(readonly_file_name)); | 430 EXPECT_TRUE(file_util::MakeFileUnwritable(readonly_file_name)); |
| 386 | 431 |
| 387 // Try to overwrite it. | 432 // Try to overwrite it. |
| 388 base_file_.reset( | 433 base_file_.reset(new BaseFile(readonly_file_name, |
| 389 new BaseFile(readonly_file_name, GURL(), GURL(), 0, file_stream_)); | 434 GURL(), |
| 435 GURL(), |
| 436 0, |
| 437 Pickle(), |
| 438 file_stream_)); |
| 390 | 439 |
| 391 expect_in_progress_ = false; | 440 expect_in_progress_ = false; |
| 392 | 441 |
| 393 int init_error = base_file_->Initialize(false); | 442 int init_error = base_file_->Initialize(false); |
| 394 DVLOG(1) << " init_error = " << init_error; | 443 DVLOG(1) << " init_error = " << init_error; |
| 395 EXPECT_NE(net::OK, init_error); | 444 EXPECT_NE(net::OK, init_error); |
| 396 | 445 |
| 397 const FilePath file_name = base_file_->full_path(); | 446 const FilePath file_name = base_file_->full_path(); |
| 398 EXPECT_NE(FilePath::StringType(), file_name.value()); | 447 EXPECT_NE(FilePath::StringType(), file_name.value()); |
| 399 | 448 |
| 400 // Write into the file. | 449 // Write into the file. |
| 401 EXPECT_NE(net::OK, AppendDataToFile(kTestData1)); | 450 EXPECT_NE(net::OK, AppendDataToFile(kTestData1)); |
| 402 | 451 |
| 403 base_file_->Finish(); | 452 base_file_->Finish(); |
| 404 base_file_->Detach(); | 453 base_file_->Detach(); |
| 405 expect_file_survives_ = true; | 454 expect_file_survives_ = true; |
| 406 } | 455 } |
| 407 | 456 |
| 408 TEST_F(BaseFileTest, IsEmptySha256Hash) { | 457 TEST_F(BaseFileTest, IsEmptySha256Hash) { |
| 409 std::string empty(BaseFile::kSha256HashLen, '\x00'); | 458 std::string empty(BaseFile::kSha256HashLen, '\x00'); |
| 410 EXPECT_TRUE(BaseFile::IsEmptySha256Hash(empty)); | 459 EXPECT_TRUE(BaseFile::IsEmptySha256Hash(empty)); |
| 411 std::string not_empty(BaseFile::kSha256HashLen, '\x01'); | 460 std::string not_empty(BaseFile::kSha256HashLen, '\x01'); |
| 412 EXPECT_FALSE(BaseFile::IsEmptySha256Hash(not_empty)); | 461 EXPECT_FALSE(BaseFile::IsEmptySha256Hash(not_empty)); |
| 413 EXPECT_FALSE(BaseFile::IsEmptySha256Hash("")); | 462 EXPECT_FALSE(BaseFile::IsEmptySha256Hash("")); |
| 414 } | 463 } |
| OLD | NEW |