| 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" |
| 6 |
| 5 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 6 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 7 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 8 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 9 #include "content/browser/browser_thread.h" | 11 #include "content/browser/browser_thread.h" |
| 10 #include "content/browser/download/base_file.h" | |
| 11 #include "net/base/file_stream.h" | 12 #include "net/base/file_stream.h" |
| 13 #include "net/base/mock_file_stream.h" |
| 14 #include "net/base/net_errors.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 16 |
| 14 namespace { | 17 namespace { |
| 15 | 18 |
| 16 const char kTestData1[] = "Let's write some data to the file!\n"; | 19 const char kTestData1[] = "Let's write some data to the file!\n"; |
| 17 const char kTestData2[] = "Writing more data.\n"; | 20 const char kTestData2[] = "Writing more data.\n"; |
| 18 const char kTestData3[] = "Final line."; | 21 const char kTestData3[] = "Final line."; |
| 19 | 22 |
| 23 } // namespace |
| 24 |
| 20 class BaseFileTest : public testing::Test { | 25 class BaseFileTest : public testing::Test { |
| 21 public: | 26 public: |
| 22 BaseFileTest() | 27 BaseFileTest() |
| 23 : expect_file_survives_(false), | 28 : expect_file_survives_(false), |
| 24 file_thread_(BrowserThread::FILE, &message_loop_) { | 29 file_thread_(BrowserThread::FILE, &message_loop_) { |
| 25 } | 30 } |
| 26 | 31 |
| 27 virtual void SetUp() { | 32 virtual void SetUp() { |
| 28 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 33 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 29 base_file_.reset( | 34 base_file_.reset( |
| (...skipping 14 matching lines...) Expand all Loading... |
| 44 EXPECT_EQ(expected_data_, disk_data); | 49 EXPECT_EQ(expected_data_, disk_data); |
| 45 } | 50 } |
| 46 | 51 |
| 47 // Make sure the mock BrowserThread outlives the BaseFile to satisfy | 52 // Make sure the mock BrowserThread outlives the BaseFile to satisfy |
| 48 // thread checks inside it. | 53 // thread checks inside it. |
| 49 base_file_.reset(); | 54 base_file_.reset(); |
| 50 | 55 |
| 51 EXPECT_EQ(expect_file_survives_, file_util::PathExists(full_path)); | 56 EXPECT_EQ(expect_file_survives_, file_util::PathExists(full_path)); |
| 52 } | 57 } |
| 53 | 58 |
| 54 void AppendDataToFile(const std::string& data) { | 59 bool OpenMockFileStream() { |
| 55 ASSERT_TRUE(base_file_->in_progress()); | 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 56 base_file_->AppendDataToFile(data.data(), data.size()); | 61 |
| 62 FilePath path; |
| 63 if (!file_util::CreateTemporaryFile(&path)) |
| 64 return false; |
| 65 |
| 66 // Create a new file stream. |
| 67 mock_file_stream_.reset(new net::testing::MockFileStream); |
| 68 if (mock_file_stream_->Open( |
| 69 path, |
| 70 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE) != 0) { |
| 71 mock_file_stream_.reset(); |
| 72 return false; |
| 73 } |
| 74 |
| 75 return true; |
| 76 } |
| 77 |
| 78 void ForceError(net::Error error) { |
| 79 mock_file_stream_->set_forced_error(error); |
| 80 } |
| 81 |
| 82 bool AppendDataToFile(const std::string& data) { |
| 83 EXPECT_TRUE(base_file_->in_progress()); |
| 84 bool appended = base_file_->AppendDataToFile(data.data(), data.size()); |
| 57 expected_data_ += data; | 85 expected_data_ += data; |
| 58 EXPECT_EQ(static_cast<int64>(expected_data_.size()), | 86 EXPECT_EQ(static_cast<int64>(expected_data_.size()), |
| 59 base_file_->bytes_so_far()); | 87 base_file_->bytes_so_far()); |
| 88 return appended; |
| 60 } | 89 } |
| 61 | 90 |
| 62 protected: | 91 protected: |
| 63 linked_ptr<net::FileStream> file_stream_; | 92 linked_ptr<net::FileStream> file_stream_; |
| 93 linked_ptr<net::testing::MockFileStream> mock_file_stream_; |
| 64 | 94 |
| 65 // BaseClass instance we are testing. | 95 // BaseClass instance we are testing. |
| 66 scoped_ptr<BaseFile> base_file_; | 96 scoped_ptr<BaseFile> base_file_; |
| 67 | 97 |
| 68 // Temporary directory for renamed downloads. | 98 // Temporary directory for renamed downloads. |
| 69 ScopedTempDir temp_dir_; | 99 ScopedTempDir temp_dir_; |
| 70 | 100 |
| 71 // Expect the file to survive deletion of the BaseFile instance. | 101 // Expect the file to survive deletion of the BaseFile instance. |
| 72 bool expect_file_survives_; | 102 bool expect_file_survives_; |
| 73 | 103 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 93 EXPECT_TRUE(file_util::PathExists(base_file_->full_path())); | 123 EXPECT_TRUE(file_util::PathExists(base_file_->full_path())); |
| 94 base_file_->Cancel(); | 124 base_file_->Cancel(); |
| 95 EXPECT_FALSE(file_util::PathExists(base_file_->full_path())); | 125 EXPECT_FALSE(file_util::PathExists(base_file_->full_path())); |
| 96 EXPECT_NE(FilePath().value(), base_file_->full_path().value()); | 126 EXPECT_NE(FilePath().value(), base_file_->full_path().value()); |
| 97 } | 127 } |
| 98 | 128 |
| 99 // Write data to the file and detach it, so it doesn't get deleted | 129 // Write data to the file and detach it, so it doesn't get deleted |
| 100 // automatically when base_file_ is destructed. | 130 // automatically when base_file_ is destructed. |
| 101 TEST_F(BaseFileTest, WriteAndDetach) { | 131 TEST_F(BaseFileTest, WriteAndDetach) { |
| 102 ASSERT_TRUE(base_file_->Initialize(false)); | 132 ASSERT_TRUE(base_file_->Initialize(false)); |
| 103 AppendDataToFile(kTestData1); | 133 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 104 base_file_->Finish(); | 134 base_file_->Finish(); |
| 105 base_file_->Detach(); | 135 base_file_->Detach(); |
| 106 expect_file_survives_ = true; | 136 expect_file_survives_ = true; |
| 107 } | 137 } |
| 108 | 138 |
| 109 // Write data to the file and detach it, and calculate its sha256 hash. | 139 // Write data to the file and detach it, and calculate its sha256 hash. |
| 110 TEST_F(BaseFileTest, WriteWithHashAndDetach) { | 140 TEST_F(BaseFileTest, WriteWithHashAndDetach) { |
| 111 ASSERT_TRUE(base_file_->Initialize(true)); | 141 ASSERT_TRUE(base_file_->Initialize(true)); |
| 112 AppendDataToFile(kTestData1); | 142 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 113 base_file_->Finish(); | 143 base_file_->Finish(); |
| 114 | 144 |
| 115 std::string hash; | 145 std::string hash; |
| 116 base_file_->GetSha256Hash(&hash); | 146 base_file_->GetSha256Hash(&hash); |
| 117 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", | 147 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", |
| 118 base::HexEncode(hash.data(), hash.size())); | 148 base::HexEncode(hash.data(), hash.size())); |
| 119 | 149 |
| 120 base_file_->Detach(); | 150 base_file_->Detach(); |
| 121 expect_file_survives_ = true; | 151 expect_file_survives_ = true; |
| 122 } | 152 } |
| 123 | 153 |
| 124 // Rename the file after writing to it, then detach. | 154 // Rename the file after writing to it, then detach. |
| 125 TEST_F(BaseFileTest, WriteThenRenameAndDetach) { | 155 TEST_F(BaseFileTest, WriteThenRenameAndDetach) { |
| 126 ASSERT_TRUE(base_file_->Initialize(false)); | 156 ASSERT_TRUE(base_file_->Initialize(false)); |
| 127 | 157 |
| 128 FilePath initial_path(base_file_->full_path()); | 158 FilePath initial_path(base_file_->full_path()); |
| 129 EXPECT_TRUE(file_util::PathExists(initial_path)); | 159 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 130 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 160 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
| 131 EXPECT_FALSE(file_util::PathExists(new_path)); | 161 EXPECT_FALSE(file_util::PathExists(new_path)); |
| 132 | 162 |
| 133 AppendDataToFile(kTestData1); | 163 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 134 | 164 |
| 135 EXPECT_TRUE(base_file_->Rename(new_path)); | 165 EXPECT_TRUE(base_file_->Rename(new_path)); |
| 136 EXPECT_FALSE(file_util::PathExists(initial_path)); | 166 EXPECT_FALSE(file_util::PathExists(initial_path)); |
| 137 EXPECT_TRUE(file_util::PathExists(new_path)); | 167 EXPECT_TRUE(file_util::PathExists(new_path)); |
| 138 | 168 |
| 139 base_file_->Finish(); | 169 base_file_->Finish(); |
| 140 base_file_->Detach(); | 170 base_file_->Detach(); |
| 141 expect_file_survives_ = true; | 171 expect_file_survives_ = true; |
| 142 } | 172 } |
| 143 | 173 |
| 144 // Write data to the file once. | 174 // Write data to the file once. |
| 145 TEST_F(BaseFileTest, SingleWrite) { | 175 TEST_F(BaseFileTest, SingleWrite) { |
| 146 ASSERT_TRUE(base_file_->Initialize(false)); | 176 ASSERT_TRUE(base_file_->Initialize(false)); |
| 147 AppendDataToFile(kTestData1); | 177 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 148 base_file_->Finish(); | 178 base_file_->Finish(); |
| 149 } | 179 } |
| 150 | 180 |
| 151 // Write data to the file multiple times. | 181 // Write data to the file multiple times. |
| 152 TEST_F(BaseFileTest, MultipleWrites) { | 182 TEST_F(BaseFileTest, MultipleWrites) { |
| 153 ASSERT_TRUE(base_file_->Initialize(false)); | 183 ASSERT_TRUE(base_file_->Initialize(false)); |
| 154 AppendDataToFile(kTestData1); | 184 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 155 AppendDataToFile(kTestData2); | 185 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 156 AppendDataToFile(kTestData3); | 186 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
| 157 std::string hash; | 187 std::string hash; |
| 158 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); | 188 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); |
| 159 base_file_->Finish(); | 189 base_file_->Finish(); |
| 160 } | 190 } |
| 161 | 191 |
| 162 // Write data to the file once and calculate its sha256 hash. | 192 // Write data to the file once and calculate its sha256 hash. |
| 163 TEST_F(BaseFileTest, SingleWriteWithHash) { | 193 TEST_F(BaseFileTest, SingleWriteWithHash) { |
| 164 ASSERT_TRUE(base_file_->Initialize(true)); | 194 ASSERT_TRUE(base_file_->Initialize(true)); |
| 165 AppendDataToFile(kTestData1); | 195 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 166 base_file_->Finish(); | 196 base_file_->Finish(); |
| 167 | 197 |
| 168 std::string hash; | 198 std::string hash; |
| 169 base_file_->GetSha256Hash(&hash); | 199 base_file_->GetSha256Hash(&hash); |
| 170 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", | 200 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", |
| 171 base::HexEncode(hash.data(), hash.size())); | 201 base::HexEncode(hash.data(), hash.size())); |
| 172 } | 202 } |
| 173 | 203 |
| 174 // Write data to the file multiple times and calculate its sha256 hash. | 204 // Write data to the file multiple times and calculate its sha256 hash. |
| 175 TEST_F(BaseFileTest, MultipleWritesWithHash) { | 205 TEST_F(BaseFileTest, MultipleWritesWithHash) { |
| 176 std::string hash; | 206 std::string hash; |
| 177 | 207 |
| 178 ASSERT_TRUE(base_file_->Initialize(true)); | 208 ASSERT_TRUE(base_file_->Initialize(true)); |
| 179 AppendDataToFile(kTestData1); | 209 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 180 AppendDataToFile(kTestData2); | 210 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 181 AppendDataToFile(kTestData3); | 211 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
| 182 // no hash before Finish() is called either. | 212 // no hash before Finish() is called either. |
| 183 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); | 213 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); |
| 184 base_file_->Finish(); | 214 base_file_->Finish(); |
| 185 | 215 |
| 186 EXPECT_TRUE(base_file_->GetSha256Hash(&hash)); | 216 EXPECT_TRUE(base_file_->GetSha256Hash(&hash)); |
| 187 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8", | 217 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8", |
| 188 base::HexEncode(hash.data(), hash.size())); | 218 base::HexEncode(hash.data(), hash.size())); |
| 189 } | 219 } |
| 190 | 220 |
| 191 // Rename the file after all writes to it. | 221 // Rename the file after all writes to it. |
| 192 TEST_F(BaseFileTest, WriteThenRename) { | 222 TEST_F(BaseFileTest, WriteThenRename) { |
| 193 ASSERT_TRUE(base_file_->Initialize(false)); | 223 ASSERT_TRUE(base_file_->Initialize(false)); |
| 194 | 224 |
| 195 FilePath initial_path(base_file_->full_path()); | 225 FilePath initial_path(base_file_->full_path()); |
| 196 EXPECT_TRUE(file_util::PathExists(initial_path)); | 226 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 197 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 227 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
| 198 EXPECT_FALSE(file_util::PathExists(new_path)); | 228 EXPECT_FALSE(file_util::PathExists(new_path)); |
| 199 | 229 |
| 200 AppendDataToFile(kTestData1); | 230 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 201 | 231 |
| 202 EXPECT_TRUE(base_file_->Rename(new_path)); | 232 EXPECT_TRUE(base_file_->Rename(new_path)); |
| 203 EXPECT_FALSE(file_util::PathExists(initial_path)); | 233 EXPECT_FALSE(file_util::PathExists(initial_path)); |
| 204 EXPECT_TRUE(file_util::PathExists(new_path)); | 234 EXPECT_TRUE(file_util::PathExists(new_path)); |
| 205 | 235 |
| 206 base_file_->Finish(); | 236 base_file_->Finish(); |
| 207 } | 237 } |
| 208 | 238 |
| 209 // Rename the file while the download is still in progress. | 239 // Rename the file while the download is still in progress. |
| 210 TEST_F(BaseFileTest, RenameWhileInProgress) { | 240 TEST_F(BaseFileTest, RenameWhileInProgress) { |
| 211 ASSERT_TRUE(base_file_->Initialize(false)); | 241 ASSERT_TRUE(base_file_->Initialize(false)); |
| 212 | 242 |
| 213 FilePath initial_path(base_file_->full_path()); | 243 FilePath initial_path(base_file_->full_path()); |
| 214 EXPECT_TRUE(file_util::PathExists(initial_path)); | 244 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 215 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 245 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
| 216 EXPECT_FALSE(file_util::PathExists(new_path)); | 246 EXPECT_FALSE(file_util::PathExists(new_path)); |
| 217 | 247 |
| 218 AppendDataToFile(kTestData1); | 248 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 219 | 249 |
| 220 EXPECT_TRUE(base_file_->in_progress()); | 250 EXPECT_TRUE(base_file_->in_progress()); |
| 221 EXPECT_TRUE(base_file_->Rename(new_path)); | 251 EXPECT_TRUE(base_file_->Rename(new_path)); |
| 222 EXPECT_FALSE(file_util::PathExists(initial_path)); | 252 EXPECT_FALSE(file_util::PathExists(initial_path)); |
| 223 EXPECT_TRUE(file_util::PathExists(new_path)); | 253 EXPECT_TRUE(file_util::PathExists(new_path)); |
| 224 | 254 |
| 225 AppendDataToFile(kTestData2); | 255 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 226 | 256 |
| 227 base_file_->Finish(); | 257 base_file_->Finish(); |
| 228 } | 258 } |
| 229 | 259 |
| 230 } // namespace | 260 // Write data to the file multiple times. |
| 261 TEST_F(BaseFileTest, MultipleWritesWithError) { |
| 262 ASSERT_TRUE(OpenMockFileStream()); |
| 263 base_file_.reset(new BaseFile(mock_file_stream_->get_path(), |
| 264 GURL(), GURL(), 0, mock_file_stream_)); |
| 265 ASSERT_TRUE(base_file_->Initialize(false)); |
| 266 ASSERT_TRUE(AppendDataToFile(kTestData1)); |
| 267 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 268 ForceError(net::ERR_FAILED); |
| 269 ASSERT_FALSE(AppendDataToFile(kTestData3)); |
| 270 std::string hash; |
| 271 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); |
| 272 base_file_->Finish(); |
| 273 } |
| OLD | NEW |