summaryrefslogtreecommitdiff
path: root/content/browser/download/base_file_unittest.cc
diff options
context:
space:
mode:
authorPrimiano Tucci <primiano@google.com>2014-09-30 14:45:55 +0100
committerPrimiano Tucci <primiano@google.com>2014-09-30 14:45:55 +0100
commit1320f92c476a1ad9d19dba2a48c72b75566198e9 (patch)
treeea7f149ccad687b22c18a72b729646568b2d54fb /content/browser/download/base_file_unittest.cc
parent39b78c562f50ad7d5551ee861121f899239525a2 (diff)
downloadchromium_org-1320f92c476a1ad9d19dba2a48c72b75566198e9.tar.gz
Merge from Chromium at DEPS revision 267aeeb8d85c
This commit was generated by merge_to_master.py. Change-Id: Id3aac9713b301fae64408cdaee0888724eeb7c0e
Diffstat (limited to 'content/browser/download/base_file_unittest.cc')
-rw-r--r--content/browser/download/base_file_unittest.cc60
1 files changed, 57 insertions, 3 deletions
diff --git a/content/browser/download/base_file_unittest.cc b/content/browser/download/base_file_unittest.cc
index b3f2e6ca98..090e4da7b4 100644
--- a/content/browser/download/base_file_unittest.cc
+++ b/content/browser/download/base_file_unittest.cc
@@ -4,8 +4,8 @@
#include "content/browser/download/base_file.h"
-#include "base/file_util.h"
#include "base/files/file.h"
+#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
@@ -195,6 +195,12 @@ class BaseFileTest : public testing::Test {
expected_error_ = err;
}
+ void ExpectPermissionError(DownloadInterruptReason err) {
+ EXPECT_TRUE(err == DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR ||
+ err == DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED)
+ << "Interrupt reason = " << err;
+ }
+
protected:
// BaseClass instance we are testing.
scoped_ptr<BaseFile> base_file_;
@@ -469,13 +475,61 @@ TEST_F(BaseFileTest, RenameWithError) {
{
base::FilePermissionRestorer restore_permissions_for(test_dir);
ASSERT_TRUE(base::MakeFileUnwritable(test_dir));
- EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED,
- base_file_->Rename(new_path));
+ ExpectPermissionError(base_file_->Rename(new_path));
}
base_file_->Finish();
}
+// Test that if a rename fails for an in-progress BaseFile, it remains writeable
+// and renameable.
+TEST_F(BaseFileTest, RenameWithErrorInProgress) {
+ ASSERT_TRUE(InitializeFile());
+
+ base::FilePath test_dir(temp_dir_.path().AppendASCII("TestDir"));
+ ASSERT_TRUE(base::CreateDirectory(test_dir));
+
+ base::FilePath new_path(test_dir.AppendASCII("TestFile"));
+ EXPECT_FALSE(base::PathExists(new_path));
+
+ // Write some data to start with.
+ ASSERT_TRUE(AppendDataToFile(kTestData1));
+ ASSERT_TRUE(base_file_->in_progress());
+
+ base::FilePath old_path = base_file_->full_path();
+
+ {
+ base::FilePermissionRestorer restore_permissions_for(test_dir);
+ ASSERT_TRUE(base::MakeFileUnwritable(test_dir));
+ ExpectPermissionError(base_file_->Rename(new_path));
+
+ // The file should still be open and we should be able to continue writing
+ // to it.
+ ASSERT_TRUE(base_file_->in_progress());
+ ASSERT_TRUE(AppendDataToFile(kTestData2));
+ ASSERT_EQ(old_path.value(), base_file_->full_path().value());
+
+ // Try to rename again, just for kicks. It should still fail.
+ ExpectPermissionError(base_file_->Rename(new_path));
+ }
+
+ // Now that TestDir is writeable again, we should be able to successfully
+ // rename the file.
+ EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, base_file_->Rename(new_path));
+ ASSERT_EQ(new_path.value(), base_file_->full_path().value());
+ ASSERT_TRUE(AppendDataToFile(kTestData3));
+
+ base_file_->Finish();
+
+ // The contents of the file should be intact.
+ std::string file_contents;
+ std::string expected_contents(kTestData1);
+ expected_contents += kTestData2;
+ expected_contents += kTestData3;
+ ASSERT_TRUE(base::ReadFileToString(new_path, &file_contents));
+ EXPECT_EQ(expected_contents, file_contents);
+}
+
// Test that a failed write reports an error.
TEST_F(BaseFileTest, WriteWithError) {
base::FilePath path;