| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 BaseFileTest() | 42 BaseFileTest() |
| 43 : expect_file_survives_(false), | 43 : expect_file_survives_(false), |
| 44 expect_in_progress_(true), | 44 expect_in_progress_(true), |
| 45 expected_error_(DOWNLOAD_INTERRUPT_REASON_NONE), | 45 expected_error_(DOWNLOAD_INTERRUPT_REASON_NONE), |
| 46 file_thread_(BrowserThread::FILE, &message_loop_) { | 46 file_thread_(BrowserThread::FILE, &message_loop_) { |
| 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(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 "", |
| 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 } |
| 68 | 68 |
| 69 FilePath full_path = base_file_->full_path(); | 69 base::FilePath full_path = base_file_->full_path(); |
| 70 | 70 |
| 71 if (!expected_data_.empty() && !expected_error_) { | 71 if (!expected_data_.empty() && !expected_error_) { |
| 72 // Make sure the data has been properly written to disk. | 72 // Make sure the data has been properly written to disk. |
| 73 std::string disk_data; | 73 std::string disk_data; |
| 74 EXPECT_TRUE(file_util::ReadFileToString(full_path, &disk_data)); | 74 EXPECT_TRUE(file_util::ReadFileToString(full_path, &disk_data)); |
| 75 EXPECT_EQ(expected_data_, disk_data); | 75 EXPECT_EQ(expected_data_, disk_data); |
| 76 } | 76 } |
| 77 | 77 |
| 78 // Make sure the mock BrowserThread outlives the BaseFile to satisfy | 78 // Make sure the mock BrowserThread outlives the BaseFile to satisfy |
| 79 // thread checks inside it. | 79 // thread checks inside it. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 93 | 93 |
| 94 std::string GetFinalHash() { | 94 std::string GetFinalHash() { |
| 95 std::string hash; | 95 std::string hash; |
| 96 secure_hash_->Finish(sha256_hash_, kSha256HashLen); | 96 secure_hash_->Finish(sha256_hash_, kSha256HashLen); |
| 97 hash.assign(reinterpret_cast<const char*>(sha256_hash_), | 97 hash.assign(reinterpret_cast<const char*>(sha256_hash_), |
| 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(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 "", |
| 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() { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 131 base_file_->bytes_so_far()); | 131 base_file_->bytes_so_far()); |
| 132 } | 132 } |
| 133 } | 133 } |
| 134 return result == DOWNLOAD_INTERRUPT_REASON_NONE; | 134 return result == DOWNLOAD_INTERRUPT_REASON_NONE; |
| 135 } | 135 } |
| 136 | 136 |
| 137 void set_expected_data(const std::string& data) { expected_data_ = data; } | 137 void set_expected_data(const std::string& data) { expected_data_ = data; } |
| 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 FilePath CreateTestFile() { | 141 base::FilePath CreateTestFile() { |
| 142 FilePath file_name; | 142 base::FilePath file_name; |
| 143 BaseFile file(FilePath(), | 143 BaseFile file(base::FilePath(), |
| 144 GURL(), | 144 GURL(), |
| 145 GURL(), | 145 GURL(), |
| 146 0, | 146 0, |
| 147 false, | 147 false, |
| 148 "", | 148 "", |
| 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(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 FilePath& file_name) { | 167 void CreateFileWithName(const base::FilePath& file_name) { |
| 168 EXPECT_NE(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 "", |
| 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())); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 BrowserThreadImpl file_thread_; | 226 BrowserThreadImpl file_thread_; |
| 227 }; | 227 }; |
| 228 | 228 |
| 229 // This will initialize the entire array to zero. | 229 // This will initialize the entire array to zero. |
| 230 const unsigned char BaseFileTest::kEmptySha256Hash[] = { 0 }; | 230 const unsigned char BaseFileTest::kEmptySha256Hash[] = { 0 }; |
| 231 | 231 |
| 232 // Test the most basic scenario: just create the object and do a sanity check | 232 // Test the most basic scenario: just create the object and do a sanity check |
| 233 // on all its accessors. This is actually a case that rarely happens | 233 // on all its accessors. This is actually a case that rarely happens |
| 234 // in production, where we would at least Initialize it. | 234 // in production, where we would at least Initialize it. |
| 235 TEST_F(BaseFileTest, CreateDestroy) { | 235 TEST_F(BaseFileTest, CreateDestroy) { |
| 236 EXPECT_EQ(FilePath().value(), base_file_->full_path().value()); | 236 EXPECT_EQ(base::FilePath().value(), base_file_->full_path().value()); |
| 237 } | 237 } |
| 238 | 238 |
| 239 // Cancel the download explicitly. | 239 // Cancel the download explicitly. |
| 240 TEST_F(BaseFileTest, Cancel) { | 240 TEST_F(BaseFileTest, Cancel) { |
| 241 ASSERT_TRUE(InitializeFile()); | 241 ASSERT_TRUE(InitializeFile()); |
| 242 EXPECT_TRUE(file_util::PathExists(base_file_->full_path())); | 242 EXPECT_TRUE(file_util::PathExists(base_file_->full_path())); |
| 243 base_file_->Cancel(); | 243 base_file_->Cancel(); |
| 244 EXPECT_FALSE(file_util::PathExists(base_file_->full_path())); | 244 EXPECT_FALSE(file_util::PathExists(base_file_->full_path())); |
| 245 EXPECT_NE(FilePath().value(), base_file_->full_path().value()); | 245 EXPECT_NE(base::FilePath().value(), base_file_->full_path().value()); |
| 246 } | 246 } |
| 247 | 247 |
| 248 // Write data to the file and detach it, so it doesn't get deleted | 248 // Write data to the file and detach it, so it doesn't get deleted |
| 249 // automatically when base_file_ is destructed. | 249 // automatically when base_file_ is destructed. |
| 250 TEST_F(BaseFileTest, WriteAndDetach) { | 250 TEST_F(BaseFileTest, WriteAndDetach) { |
| 251 ASSERT_TRUE(InitializeFile()); | 251 ASSERT_TRUE(InitializeFile()); |
| 252 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 252 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 253 base_file_->Finish(); | 253 base_file_->Finish(); |
| 254 base_file_->Detach(); | 254 base_file_->Detach(); |
| 255 expect_file_survives_ = true; | 255 expect_file_survives_ = true; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 276 EXPECT_EQ(expected_hash_hex, base::HexEncode(hash.data(), hash.size())); | 276 EXPECT_EQ(expected_hash_hex, base::HexEncode(hash.data(), hash.size())); |
| 277 | 277 |
| 278 base_file_->Detach(); | 278 base_file_->Detach(); |
| 279 expect_file_survives_ = true; | 279 expect_file_survives_ = true; |
| 280 } | 280 } |
| 281 | 281 |
| 282 // Rename the file after writing to it, then detach. | 282 // Rename the file after writing to it, then detach. |
| 283 TEST_F(BaseFileTest, WriteThenRenameAndDetach) { | 283 TEST_F(BaseFileTest, WriteThenRenameAndDetach) { |
| 284 ASSERT_TRUE(InitializeFile()); | 284 ASSERT_TRUE(InitializeFile()); |
| 285 | 285 |
| 286 FilePath initial_path(base_file_->full_path()); | 286 base::FilePath initial_path(base_file_->full_path()); |
| 287 EXPECT_TRUE(file_util::PathExists(initial_path)); | 287 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 288 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 288 base::FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
| 289 EXPECT_FALSE(file_util::PathExists(new_path)); | 289 EXPECT_FALSE(file_util::PathExists(new_path)); |
| 290 | 290 |
| 291 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 291 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 292 | 292 |
| 293 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path)); | 293 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path)); |
| 294 EXPECT_FALSE(file_util::PathExists(initial_path)); | 294 EXPECT_FALSE(file_util::PathExists(initial_path)); |
| 295 EXPECT_TRUE(file_util::PathExists(new_path)); | 295 EXPECT_TRUE(file_util::PathExists(new_path)); |
| 296 | 296 |
| 297 base_file_->Finish(); | 297 base_file_->Finish(); |
| 298 base_file_->Detach(); | 298 base_file_->Detach(); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 ASSERT_TRUE(InitializeFile()); | 382 ASSERT_TRUE(InitializeFile()); |
| 383 // Write some data | 383 // Write some data |
| 384 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 384 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 385 ASSERT_TRUE(AppendDataToFile(kTestData2)); | 385 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 386 // Get the hash state and file name. | 386 // Get the hash state and file name. |
| 387 std::string hash_state; | 387 std::string hash_state; |
| 388 hash_state = base_file_->GetHashState(); | 388 hash_state = base_file_->GetHashState(); |
| 389 // Finish the file. | 389 // Finish the file. |
| 390 base_file_->Finish(); | 390 base_file_->Finish(); |
| 391 | 391 |
| 392 FilePath new_file_path(temp_dir_.path().Append( | 392 base::FilePath new_file_path(temp_dir_.path().Append( |
| 393 FilePath(FILE_PATH_LITERAL("second_file")))); | 393 base::FilePath(FILE_PATH_LITERAL("second_file")))); |
| 394 | 394 |
| 395 ASSERT_TRUE(file_util::CopyFile(base_file_->full_path(), new_file_path)); | 395 ASSERT_TRUE(file_util::CopyFile(base_file_->full_path(), new_file_path)); |
| 396 | 396 |
| 397 // Create another file | 397 // Create another file |
| 398 BaseFile second_file(new_file_path, | 398 BaseFile second_file(new_file_path, |
| 399 GURL(), | 399 GURL(), |
| 400 GURL(), | 400 GURL(), |
| 401 base_file_->bytes_so_far(), | 401 base_file_->bytes_so_far(), |
| 402 true, | 402 true, |
| 403 hash_state, | 403 hash_state, |
| 404 scoped_ptr<net::FileStream>(), | 404 scoped_ptr<net::FileStream>(), |
| 405 net::BoundNetLog()); | 405 net::BoundNetLog()); |
| 406 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 406 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 407 second_file.Initialize(FilePath())); | 407 second_file.Initialize(base::FilePath())); |
| 408 std::string data(kTestData3); | 408 std::string data(kTestData3); |
| 409 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 409 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 410 second_file.AppendDataToFile(data.data(), data.size())); | 410 second_file.AppendDataToFile(data.data(), data.size())); |
| 411 second_file.Finish(); | 411 second_file.Finish(); |
| 412 | 412 |
| 413 std::string hash; | 413 std::string hash; |
| 414 EXPECT_TRUE(second_file.GetHash(&hash)); | 414 EXPECT_TRUE(second_file.GetHash(&hash)); |
| 415 // This will fail until getting the hash state is supported in SecureHash. | 415 // This will fail until getting the hash state is supported in SecureHash. |
| 416 EXPECT_STREQ(expected_hash_hex.c_str(), | 416 EXPECT_STREQ(expected_hash_hex.c_str(), |
| 417 base::HexEncode(hash.data(), hash.size()).c_str()); | 417 base::HexEncode(hash.data(), hash.size()).c_str()); |
| 418 } | 418 } |
| 419 | 419 |
| 420 // Rename the file after all writes to it. | 420 // Rename the file after all writes to it. |
| 421 TEST_F(BaseFileTest, WriteThenRename) { | 421 TEST_F(BaseFileTest, WriteThenRename) { |
| 422 ASSERT_TRUE(InitializeFile()); | 422 ASSERT_TRUE(InitializeFile()); |
| 423 | 423 |
| 424 FilePath initial_path(base_file_->full_path()); | 424 base::FilePath initial_path(base_file_->full_path()); |
| 425 EXPECT_TRUE(file_util::PathExists(initial_path)); | 425 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 426 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 426 base::FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
| 427 EXPECT_FALSE(file_util::PathExists(new_path)); | 427 EXPECT_FALSE(file_util::PathExists(new_path)); |
| 428 | 428 |
| 429 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 429 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 430 | 430 |
| 431 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 431 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 432 base_file_->Rename(new_path)); | 432 base_file_->Rename(new_path)); |
| 433 EXPECT_FALSE(file_util::PathExists(initial_path)); | 433 EXPECT_FALSE(file_util::PathExists(initial_path)); |
| 434 EXPECT_TRUE(file_util::PathExists(new_path)); | 434 EXPECT_TRUE(file_util::PathExists(new_path)); |
| 435 | 435 |
| 436 base_file_->Finish(); | 436 base_file_->Finish(); |
| 437 } | 437 } |
| 438 | 438 |
| 439 // Rename the file while the download is still in progress. | 439 // Rename the file while the download is still in progress. |
| 440 TEST_F(BaseFileTest, RenameWhileInProgress) { | 440 TEST_F(BaseFileTest, RenameWhileInProgress) { |
| 441 ASSERT_TRUE(InitializeFile()); | 441 ASSERT_TRUE(InitializeFile()); |
| 442 | 442 |
| 443 FilePath initial_path(base_file_->full_path()); | 443 base::FilePath initial_path(base_file_->full_path()); |
| 444 EXPECT_TRUE(file_util::PathExists(initial_path)); | 444 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 445 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 445 base::FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
| 446 EXPECT_FALSE(file_util::PathExists(new_path)); | 446 EXPECT_FALSE(file_util::PathExists(new_path)); |
| 447 | 447 |
| 448 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 448 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 449 | 449 |
| 450 EXPECT_TRUE(base_file_->in_progress()); | 450 EXPECT_TRUE(base_file_->in_progress()); |
| 451 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path)); | 451 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path)); |
| 452 EXPECT_FALSE(file_util::PathExists(initial_path)); | 452 EXPECT_FALSE(file_util::PathExists(initial_path)); |
| 453 EXPECT_TRUE(file_util::PathExists(new_path)); | 453 EXPECT_TRUE(file_util::PathExists(new_path)); |
| 454 | 454 |
| 455 ASSERT_TRUE(AppendDataToFile(kTestData2)); | 455 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 456 | 456 |
| 457 base_file_->Finish(); | 457 base_file_->Finish(); |
| 458 } | 458 } |
| 459 | 459 |
| 460 // Test that a failed rename reports the correct error. | 460 // Test that a failed rename reports the correct error. |
| 461 TEST_F(BaseFileTest, RenameWithError) { | 461 TEST_F(BaseFileTest, RenameWithError) { |
| 462 ASSERT_TRUE(InitializeFile()); | 462 ASSERT_TRUE(InitializeFile()); |
| 463 | 463 |
| 464 // TestDir is a subdirectory in |temp_dir_| that we will make read-only so | 464 // TestDir is a subdirectory in |temp_dir_| that we will make read-only so |
| 465 // that the rename will fail. | 465 // that the rename will fail. |
| 466 FilePath test_dir(temp_dir_.path().AppendASCII("TestDir")); | 466 base::FilePath test_dir(temp_dir_.path().AppendASCII("TestDir")); |
| 467 ASSERT_TRUE(file_util::CreateDirectory(test_dir)); | 467 ASSERT_TRUE(file_util::CreateDirectory(test_dir)); |
| 468 | 468 |
| 469 FilePath new_path(test_dir.AppendASCII("TestFile")); | 469 base::FilePath new_path(test_dir.AppendASCII("TestFile")); |
| 470 EXPECT_FALSE(file_util::PathExists(new_path)); | 470 EXPECT_FALSE(file_util::PathExists(new_path)); |
| 471 | 471 |
| 472 { | 472 { |
| 473 file_util::PermissionRestorer restore_permissions_for(test_dir); | 473 file_util::PermissionRestorer restore_permissions_for(test_dir); |
| 474 ASSERT_TRUE(file_util::MakeFileUnwritable(test_dir)); | 474 ASSERT_TRUE(file_util::MakeFileUnwritable(test_dir)); |
| 475 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, | 475 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, |
| 476 base_file_->Rename(new_path)); | 476 base_file_->Rename(new_path)); |
| 477 } | 477 } |
| 478 | 478 |
| 479 base_file_->Finish(); | 479 base_file_->Finish(); |
| 480 } | 480 } |
| 481 | 481 |
| 482 // Write data to the file multiple times. | 482 // Write data to the file multiple times. |
| 483 TEST_F(BaseFileTest, MultipleWritesWithError) { | 483 TEST_F(BaseFileTest, MultipleWritesWithError) { |
| 484 FilePath path; | 484 base::FilePath path; |
| 485 ASSERT_TRUE(file_util::CreateTemporaryFile(&path)); | 485 ASSERT_TRUE(file_util::CreateTemporaryFile(&path)); |
| 486 // Create a new file stream. scoped_ptr takes ownership and passes it to | 486 // Create a new file stream. scoped_ptr takes ownership and passes it to |
| 487 // BaseFile; we use the pointer anyway and rely on the BaseFile not | 487 // BaseFile; we use the pointer anyway and rely on the BaseFile not |
| 488 // deleting the MockFileStream until the BaseFile is reset. | 488 // deleting the MockFileStream until the BaseFile is reset. |
| 489 net::testing::MockFileStream* mock_file_stream( | 489 net::testing::MockFileStream* mock_file_stream( |
| 490 new net::testing::MockFileStream(NULL)); | 490 new net::testing::MockFileStream(NULL)); |
| 491 scoped_ptr<net::FileStream> mock_file_stream_scoped_ptr(mock_file_stream); | 491 scoped_ptr<net::FileStream> mock_file_stream_scoped_ptr(mock_file_stream); |
| 492 | 492 |
| 493 ASSERT_EQ(0, | 493 ASSERT_EQ(0, |
| 494 mock_file_stream->OpenSync( | 494 mock_file_stream->OpenSync( |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 533 // Create another |BaseFile| referring to the file that |base_file_| owns. | 533 // Create another |BaseFile| referring to the file that |base_file_| owns. |
| 534 CreateFileWithName(base_file_->full_path()); | 534 CreateFileWithName(base_file_->full_path()); |
| 535 | 535 |
| 536 ASSERT_TRUE(AppendDataToFile(kTestData1)); | 536 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 537 base_file_->Finish(); | 537 base_file_->Finish(); |
| 538 } | 538 } |
| 539 | 539 |
| 540 // Create a file and append to it. | 540 // Create a file and append to it. |
| 541 TEST_F(BaseFileTest, AppendToBaseFile) { | 541 TEST_F(BaseFileTest, AppendToBaseFile) { |
| 542 // Create a new file. | 542 // Create a new file. |
| 543 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 "", |
| 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 FilePath file_name = base_file_->full_path(); | 559 const base::FilePath file_name = base_file_->full_path(); |
| 560 EXPECT_NE(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)); |
| 564 | 564 |
| 565 base_file_->Finish(); | 565 base_file_->Finish(); |
| 566 base_file_->Detach(); | 566 base_file_->Detach(); |
| 567 expect_file_survives_ = true; | 567 expect_file_survives_ = true; |
| 568 } | 568 } |
| 569 | 569 |
| 570 // Create a read-only file and attempt to write to it. | 570 // Create a read-only file and attempt to write to it. |
| 571 TEST_F(BaseFileTest, ReadonlyBaseFile) { | 571 TEST_F(BaseFileTest, ReadonlyBaseFile) { |
| 572 // Create a new file. | 572 // Create a new file. |
| 573 FilePath readonly_file_name = CreateTestFile(); | 573 base::FilePath readonly_file_name = CreateTestFile(); |
| 574 | 574 |
| 575 // Restore permissions to the file when we are done with this test. | 575 // Restore permissions to the file when we are done with this test. |
| 576 file_util::PermissionRestorer restore_permissions(readonly_file_name); | 576 file_util::PermissionRestorer restore_permissions(readonly_file_name); |
| 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 "", |
| 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 FilePath file_name = base_file_->full_path(); | 595 const base::FilePath file_name = base_file_->full_path(); |
| 596 EXPECT_NE(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 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 655 // Test that a temporary file is created in the default download directory. | 655 // Test that a temporary file is created in the default download directory. |
| 656 TEST_F(BaseFileTest, CreatedInDefaultDirectory) { | 656 TEST_F(BaseFileTest, CreatedInDefaultDirectory) { |
| 657 ASSERT_TRUE(base_file_->full_path().empty()); | 657 ASSERT_TRUE(base_file_->full_path().empty()); |
| 658 ASSERT_TRUE(InitializeFile()); | 658 ASSERT_TRUE(InitializeFile()); |
| 659 EXPECT_FALSE(base_file_->full_path().empty()); | 659 EXPECT_FALSE(base_file_->full_path().empty()); |
| 660 | 660 |
| 661 // On Windows, CreateTemporaryFileInDir() will cause a path with short names | 661 // On Windows, CreateTemporaryFileInDir() will cause a path with short names |
| 662 // to be expanded into a path with long names. Thus temp_dir.path() might not | 662 // to be expanded into a path with long names. Thus temp_dir.path() might not |
| 663 // be a string-wise match to base_file_->full_path().DirName() even though | 663 // be a string-wise match to base_file_->full_path().DirName() even though |
| 664 // they are in the same directory. | 664 // they are in the same directory. |
| 665 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 |