Chromium Code Reviews| 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/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 40 BaseFileTest() | 40 BaseFileTest() |
| 41 : expect_file_survives_(false), | 41 : expect_file_survives_(false), |
| 42 expect_in_progress_(true), | 42 expect_in_progress_(true), |
| 43 expected_error_(false), | 43 expected_error_(false), |
| 44 file_thread_(BrowserThread::FILE, &message_loop_) { | 44 file_thread_(BrowserThread::FILE, &message_loop_) { |
| 45 } | 45 } |
| 46 | 46 |
| 47 virtual void SetUp() { | 47 virtual void SetUp() { |
| 48 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 48 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 49 base_file_.reset( | 49 base_file_.reset( |
| 50 new BaseFile(FilePath(), GURL(), GURL(), 0, file_stream_)); | 50 new BaseFile(FilePath(), GURL(), GURL(), 0, false, "", file_stream_)); |
| 51 } | 51 } |
| 52 | 52 |
| 53 virtual void TearDown() { | 53 virtual void TearDown() { |
| 54 EXPECT_FALSE(base_file_->in_progress()); | 54 EXPECT_FALSE(base_file_->in_progress()); |
| 55 if (!expected_error_) { | 55 if (!expected_error_) { |
| 56 EXPECT_EQ(static_cast<int64>(expected_data_.size()), | 56 EXPECT_EQ(static_cast<int64>(expected_data_.size()), |
| 57 base_file_->bytes_so_far()); | 57 base_file_->bytes_so_far()); |
| 58 } | 58 } |
| 59 | 59 |
| 60 FilePath full_path = base_file_->full_path(); | 60 FilePath full_path = base_file_->full_path(); |
| 61 | 61 |
| 62 if (!expected_data_.empty() && !expected_error_) { | 62 if (!expected_data_.empty() && !expected_error_) { |
| 63 // Make sure the data has been properly written to disk. | 63 // Make sure the data has been properly written to disk. |
| 64 std::string disk_data; | 64 std::string disk_data; |
| 65 EXPECT_TRUE(file_util::ReadFileToString(full_path, &disk_data)); | 65 EXPECT_TRUE(file_util::ReadFileToString(full_path, &disk_data)); |
| 66 EXPECT_EQ(expected_data_, disk_data); | 66 EXPECT_EQ(expected_data_, disk_data); |
| 67 } | 67 } |
| 68 | 68 |
| 69 // Make sure the mock BrowserThread outlives the BaseFile to satisfy | 69 // Make sure the mock BrowserThread outlives the BaseFile to satisfy |
| 70 // thread checks inside it. | 70 // thread checks inside it. |
| 71 base_file_.reset(); | 71 base_file_.reset(); |
| 72 | 72 |
| 73 EXPECT_EQ(expect_file_survives_, file_util::PathExists(full_path)); | 73 EXPECT_EQ(expect_file_survives_, file_util::PathExists(full_path)); |
| 74 } | 74 } |
| 75 | 75 |
| 76 void MakeFileWithHash() { | |
| 77 base_file_.reset( | |
| 78 new BaseFile(FilePath(), GURL(), GURL(), 0, true, "", file_stream_)); | |
| 79 } | |
| 80 | |
| 76 bool OpenMockFileStream() { | 81 bool OpenMockFileStream() { |
| 77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 78 | 83 |
| 79 FilePath path; | 84 FilePath path; |
| 80 if (!file_util::CreateTemporaryFile(&path)) | 85 if (!file_util::CreateTemporaryFile(&path)) |
| 81 return false; | 86 return false; |
| 82 | 87 |
| 83 // Create a new file stream. | 88 // Create a new file stream. |
| 84 mock_file_stream_.reset(new net::testing::MockFileStream); | 89 mock_file_stream_.reset(new net::testing::MockFileStream); |
| 85 if (mock_file_stream_->Open( | 90 if (mock_file_stream_->Open( |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 114 return appended; | 119 return appended; |
| 115 } | 120 } |
| 116 | 121 |
| 117 void set_expected_data(const std::string& data) { expected_data_ = data; } | 122 void set_expected_data(const std::string& data) { expected_data_ = data; } |
| 118 | 123 |
| 119 // Helper functions. | 124 // Helper functions. |
| 120 // Create a file. Returns the complete file path. | 125 // Create a file. Returns the complete file path. |
| 121 static FilePath CreateTestFile() { | 126 static FilePath CreateTestFile() { |
| 122 FilePath file_name; | 127 FilePath file_name; |
| 123 linked_ptr<net::FileStream> dummy_file_stream; | 128 linked_ptr<net::FileStream> dummy_file_stream; |
| 124 BaseFile file(FilePath(), GURL(), GURL(), 0, dummy_file_stream); | 129 BaseFile file(FilePath(), GURL(), GURL(), 0, false, "", dummy_file_stream); |
| 125 | 130 |
| 126 EXPECT_EQ(net::OK, file.Initialize(false)); | 131 EXPECT_EQ(net::OK, file.Initialize()); |
| 127 file_name = file.full_path(); | 132 file_name = file.full_path(); |
| 128 EXPECT_NE(FilePath::StringType(), file_name.value()); | 133 EXPECT_NE(FilePath::StringType(), file_name.value()); |
| 129 | 134 |
| 130 EXPECT_EQ(net::OK, file.AppendDataToFile(kTestData4, kTestDataLength4)); | 135 EXPECT_EQ(net::OK, file.AppendDataToFile(kTestData4, kTestDataLength4)); |
| 131 | 136 |
| 132 // Keep the file from getting deleted when existing_file_name is deleted. | 137 // Keep the file from getting deleted when existing_file_name is deleted. |
| 133 file.Detach(); | 138 file.Detach(); |
| 134 | 139 |
| 135 return file_name; | 140 return file_name; |
| 136 } | 141 } |
| 137 | 142 |
| 138 // Create a file with the specified file name. | 143 // Create a file with the specified file name. |
| 139 static void CreateFileWithName(const FilePath& file_name) { | 144 static void CreateFileWithName(const FilePath& file_name) { |
| 140 EXPECT_NE(FilePath::StringType(), file_name.value()); | 145 EXPECT_NE(FilePath::StringType(), file_name.value()); |
| 141 linked_ptr<net::FileStream> dummy_file_stream; | 146 linked_ptr<net::FileStream> dummy_file_stream; |
| 142 BaseFile duplicate_file(file_name, GURL(), GURL(), 0, dummy_file_stream); | 147 BaseFile duplicate_file( |
| 143 EXPECT_EQ(net::OK, duplicate_file.Initialize(false)); | 148 file_name, GURL(), GURL(), 0, false, "", dummy_file_stream); |
| 149 EXPECT_EQ(net::OK, duplicate_file.Initialize()); | |
| 144 // Write something into it. | 150 // Write something into it. |
| 145 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); | 151 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); |
| 146 // Detach the file so it isn't deleted on destruction of |duplicate_file|. | 152 // Detach the file so it isn't deleted on destruction of |duplicate_file|. |
| 147 duplicate_file.Detach(); | 153 duplicate_file.Detach(); |
| 148 } | 154 } |
| 149 | 155 |
| 150 int64 CurrentSpeedAtTime(base::TimeTicks current_time) { | 156 int64 CurrentSpeedAtTime(base::TimeTicks current_time) { |
| 151 EXPECT_TRUE(base_file_.get()); | 157 EXPECT_TRUE(base_file_.get()); |
| 152 return base_file_->CurrentSpeedAtTime(current_time); | 158 return base_file_->CurrentSpeedAtTime(current_time); |
| 153 } | 159 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 185 | 191 |
| 186 // Test the most basic scenario: just create the object and do a sanity check | 192 // Test the most basic scenario: just create the object and do a sanity check |
| 187 // on all its accessors. This is actually a case that rarely happens | 193 // on all its accessors. This is actually a case that rarely happens |
| 188 // in production, where we would at least Initialize it. | 194 // in production, where we would at least Initialize it. |
| 189 TEST_F(BaseFileTest, CreateDestroy) { | 195 TEST_F(BaseFileTest, CreateDestroy) { |
| 190 EXPECT_EQ(FilePath().value(), base_file_->full_path().value()); | 196 EXPECT_EQ(FilePath().value(), base_file_->full_path().value()); |
| 191 } | 197 } |
| 192 | 198 |
| 193 // Cancel the download explicitly. | 199 // Cancel the download explicitly. |
| 194 TEST_F(BaseFileTest, Cancel) { | 200 TEST_F(BaseFileTest, Cancel) { |
| 195 ASSERT_EQ(net::OK, base_file_->Initialize(false)); | 201 ASSERT_EQ(net::OK, base_file_->Initialize()); |
| 196 EXPECT_TRUE(file_util::PathExists(base_file_->full_path())); | 202 EXPECT_TRUE(file_util::PathExists(base_file_->full_path())); |
| 197 base_file_->Cancel(); | 203 base_file_->Cancel(); |
| 198 EXPECT_FALSE(file_util::PathExists(base_file_->full_path())); | 204 EXPECT_FALSE(file_util::PathExists(base_file_->full_path())); |
| 199 EXPECT_NE(FilePath().value(), base_file_->full_path().value()); | 205 EXPECT_NE(FilePath().value(), base_file_->full_path().value()); |
| 200 } | 206 } |
| 201 | 207 |
| 202 // Write data to the file and detach it, so it doesn't get deleted | 208 // Write data to the file and detach it, so it doesn't get deleted |
| 203 // automatically when base_file_ is destructed. | 209 // automatically when base_file_ is destructed. |
| 204 TEST_F(BaseFileTest, WriteAndDetach) { | 210 TEST_F(BaseFileTest, WriteAndDetach) { |
| 205 ASSERT_EQ(net::OK, base_file_->Initialize(false)); | 211 ASSERT_EQ(net::OK, base_file_->Initialize()); |
| 206 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 212 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 207 base_file_->Finish(); | 213 base_file_->Finish(); |
| 208 base_file_->Detach(); | 214 base_file_->Detach(); |
| 209 expect_file_survives_ = true; | 215 expect_file_survives_ = true; |
| 210 } | 216 } |
| 211 | 217 |
| 212 // Write data to the file and detach it, and calculate its sha256 hash. | 218 // Write data to the file and detach it, and calculate its sha256 hash. |
| 213 TEST_F(BaseFileTest, WriteWithHashAndDetach) { | 219 TEST_F(BaseFileTest, WriteWithHashAndDetach) { |
| 214 ASSERT_EQ(net::OK, base_file_->Initialize(true)); | 220 MakeFileWithHash(); |
| 221 ASSERT_EQ(net::OK, base_file_->Initialize()); | |
| 215 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 222 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 216 base_file_->Finish(); | 223 base_file_->Finish(); |
| 217 | 224 |
| 218 std::string hash; | 225 std::string hash; |
| 219 base_file_->GetSha256Hash(&hash); | 226 base_file_->GetHash(&hash); |
| 220 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", | 227 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", |
|
Randy Smith (Not in Mondays)
2011/12/20 17:05:40
nit, suggestion: An ideal world would have this au
ahendrickson
2011/12/20 22:06:54
Done.
| |
| 221 base::HexEncode(hash.data(), hash.size())); | 228 base::HexEncode(hash.data(), hash.size())); |
| 222 | 229 |
| 223 base_file_->Detach(); | 230 base_file_->Detach(); |
| 224 expect_file_survives_ = true; | 231 expect_file_survives_ = true; |
| 225 } | 232 } |
| 226 | 233 |
| 227 // Rename the file after writing to it, then detach. | 234 // Rename the file after writing to it, then detach. |
| 228 TEST_F(BaseFileTest, WriteThenRenameAndDetach) { | 235 TEST_F(BaseFileTest, WriteThenRenameAndDetach) { |
| 229 ASSERT_EQ(net::OK, base_file_->Initialize(false)); | 236 ASSERT_EQ(net::OK, base_file_->Initialize()); |
| 230 | 237 |
| 231 FilePath initial_path(base_file_->full_path()); | 238 FilePath initial_path(base_file_->full_path()); |
| 232 EXPECT_TRUE(file_util::PathExists(initial_path)); | 239 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 233 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 240 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
| 234 EXPECT_FALSE(file_util::PathExists(new_path)); | 241 EXPECT_FALSE(file_util::PathExists(new_path)); |
| 235 | 242 |
| 236 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 243 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 237 | 244 |
| 238 EXPECT_EQ(net::OK, base_file_->Rename(new_path)); | 245 EXPECT_EQ(net::OK, base_file_->Rename(new_path)); |
| 239 EXPECT_FALSE(file_util::PathExists(initial_path)); | 246 EXPECT_FALSE(file_util::PathExists(initial_path)); |
| 240 EXPECT_TRUE(file_util::PathExists(new_path)); | 247 EXPECT_TRUE(file_util::PathExists(new_path)); |
| 241 | 248 |
| 242 base_file_->Finish(); | 249 base_file_->Finish(); |
| 243 base_file_->Detach(); | 250 base_file_->Detach(); |
| 244 expect_file_survives_ = true; | 251 expect_file_survives_ = true; |
| 245 } | 252 } |
| 246 | 253 |
| 247 // Write data to the file once. | 254 // Write data to the file once. |
| 248 TEST_F(BaseFileTest, SingleWrite) { | 255 TEST_F(BaseFileTest, SingleWrite) { |
| 249 ASSERT_EQ(net::OK, base_file_->Initialize(false)); | 256 ASSERT_EQ(net::OK, base_file_->Initialize()); |
| 250 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 257 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 251 base_file_->Finish(); | 258 base_file_->Finish(); |
| 252 } | 259 } |
| 253 | 260 |
| 254 // Write data to the file multiple times. | 261 // Write data to the file multiple times. |
| 255 TEST_F(BaseFileTest, MultipleWrites) { | 262 TEST_F(BaseFileTest, MultipleWrites) { |
| 256 ASSERT_EQ(net::OK, base_file_->Initialize(false)); | 263 ASSERT_EQ(net::OK, base_file_->Initialize()); |
| 257 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 264 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 258 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | 265 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); |
| 259 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); | 266 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); |
| 260 std::string hash; | 267 std::string hash; |
| 261 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); | 268 EXPECT_FALSE(base_file_->GetHash(&hash)); |
| 262 base_file_->Finish(); | 269 base_file_->Finish(); |
| 263 } | 270 } |
| 264 | 271 |
| 265 // Write data to the file once and calculate its sha256 hash. | 272 // Write data to the file once and calculate its sha256 hash. |
| 266 TEST_F(BaseFileTest, SingleWriteWithHash) { | 273 TEST_F(BaseFileTest, SingleWriteWithHash) { |
| 267 ASSERT_EQ(net::OK, base_file_->Initialize(true)); | 274 MakeFileWithHash(); |
| 275 ASSERT_EQ(net::OK, base_file_->Initialize()); | |
| 276 // Can get partial hash states before Finish() is called. | |
| 277 EXPECT_STRNE(std::string().c_str(), base_file_->GetHashState().c_str()); | |
| 268 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 278 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 279 EXPECT_STRNE(std::string().c_str(), base_file_->GetHashState().c_str()); | |
| 269 base_file_->Finish(); | 280 base_file_->Finish(); |
| 270 | 281 |
| 271 std::string hash; | 282 std::string hash; |
| 272 base_file_->GetSha256Hash(&hash); | 283 base_file_->GetHash(&hash); |
| 273 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", | 284 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", |
| 274 base::HexEncode(hash.data(), hash.size())); | 285 base::HexEncode(hash.data(), hash.size())); |
| 275 } | 286 } |
| 276 | 287 |
| 277 // Write data to the file multiple times and calculate its sha256 hash. | 288 // Write data to the file multiple times and calculate its sha256 hash. |
| 278 TEST_F(BaseFileTest, MultipleWritesWithHash) { | 289 TEST_F(BaseFileTest, MultipleWritesWithHash) { |
| 279 std::string hash; | 290 std::string hash; |
| 280 | 291 |
| 281 ASSERT_EQ(net::OK, base_file_->Initialize(true)); | 292 MakeFileWithHash(); |
| 293 ASSERT_EQ(net::OK, base_file_->Initialize()); | |
| 282 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 294 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 283 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | 295 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); |
| 284 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); | 296 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); |
| 285 // no hash before Finish() is called either. | 297 // No hash before Finish() is called. |
| 286 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); | 298 EXPECT_FALSE(base_file_->GetHash(&hash)); |
| 287 base_file_->Finish(); | 299 base_file_->Finish(); |
| 288 | 300 |
| 289 EXPECT_TRUE(base_file_->GetSha256Hash(&hash)); | 301 EXPECT_TRUE(base_file_->GetHash(&hash)); |
| 290 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8", | 302 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8", |
| 291 base::HexEncode(hash.data(), hash.size())); | 303 base::HexEncode(hash.data(), hash.size())); |
| 292 } | 304 } |
| 293 | 305 |
| 306 // Write data to the file multiple times, interrupt it, and continue using | |
| 307 // another file. Calculate the resulting combined sha256 hash. | |
| 308 TEST_F(BaseFileTest, MultipleWritesInterruptedWithHash) { | |
| 309 MakeFileWithHash(); | |
| 310 ASSERT_EQ(net::OK, base_file_->Initialize()); | |
| 311 // Write some data | |
| 312 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | |
| 313 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | |
| 314 // Get the hash state and file name. | |
| 315 std::string hash_state; | |
| 316 hash_state = base_file_->GetHashState(); | |
| 317 // Finish the file. | |
| 318 base_file_->Finish(); | |
| 319 | |
| 320 // Create another file | |
| 321 linked_ptr<net::FileStream> second_stream; | |
| 322 BaseFile second_file(FilePath(), | |
| 323 GURL(), | |
| 324 GURL(), | |
| 325 base_file_->bytes_so_far(), | |
| 326 true, | |
| 327 hash_state, | |
| 328 second_stream); | |
| 329 ASSERT_EQ(net::OK, second_file.Initialize()); | |
| 330 std::string data(kTestData3); | |
| 331 EXPECT_EQ(net::OK, second_file.AppendDataToFile(data.data(), data.size())); | |
| 332 second_file.Finish(); | |
| 333 | |
| 334 std::string hash; | |
| 335 EXPECT_TRUE(second_file.GetHash(&hash)); | |
| 336 // This will fail until getting the hash state is supported in SecureHash. | |
| 337 EXPECT_STREQ( | |
| 338 "CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8", | |
| 339 base::HexEncode(hash.data(), hash.size()).c_str()); | |
| 340 } | |
| 341 | |
| 294 // Rename the file after all writes to it. | 342 // Rename the file after all writes to it. |
| 295 TEST_F(BaseFileTest, WriteThenRename) { | 343 TEST_F(BaseFileTest, WriteThenRename) { |
| 296 ASSERT_EQ(net::OK, base_file_->Initialize(false)); | 344 ASSERT_EQ(net::OK, base_file_->Initialize()); |
| 297 | 345 |
| 298 FilePath initial_path(base_file_->full_path()); | 346 FilePath initial_path(base_file_->full_path()); |
| 299 EXPECT_TRUE(file_util::PathExists(initial_path)); | 347 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 300 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 348 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
| 301 EXPECT_FALSE(file_util::PathExists(new_path)); | 349 EXPECT_FALSE(file_util::PathExists(new_path)); |
| 302 | 350 |
| 303 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 351 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 304 | 352 |
| 305 EXPECT_EQ(net::OK, base_file_->Rename(new_path)); | 353 EXPECT_EQ(net::OK, base_file_->Rename(new_path)); |
| 306 EXPECT_FALSE(file_util::PathExists(initial_path)); | 354 EXPECT_FALSE(file_util::PathExists(initial_path)); |
| 307 EXPECT_TRUE(file_util::PathExists(new_path)); | 355 EXPECT_TRUE(file_util::PathExists(new_path)); |
| 308 | 356 |
| 309 base_file_->Finish(); | 357 base_file_->Finish(); |
| 310 } | 358 } |
| 311 | 359 |
| 312 // Rename the file while the download is still in progress. | 360 // Rename the file while the download is still in progress. |
| 313 TEST_F(BaseFileTest, RenameWhileInProgress) { | 361 TEST_F(BaseFileTest, RenameWhileInProgress) { |
| 314 ASSERT_EQ(net::OK, base_file_->Initialize(false)); | 362 ASSERT_EQ(net::OK, base_file_->Initialize()); |
| 315 | 363 |
| 316 FilePath initial_path(base_file_->full_path()); | 364 FilePath initial_path(base_file_->full_path()); |
| 317 EXPECT_TRUE(file_util::PathExists(initial_path)); | 365 EXPECT_TRUE(file_util::PathExists(initial_path)); |
| 318 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); | 366 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); |
| 319 EXPECT_FALSE(file_util::PathExists(new_path)); | 367 EXPECT_FALSE(file_util::PathExists(new_path)); |
| 320 | 368 |
| 321 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 369 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 322 | 370 |
| 323 EXPECT_TRUE(base_file_->in_progress()); | 371 EXPECT_TRUE(base_file_->in_progress()); |
| 324 EXPECT_EQ(net::OK, base_file_->Rename(new_path)); | 372 EXPECT_EQ(net::OK, base_file_->Rename(new_path)); |
| 325 EXPECT_FALSE(file_util::PathExists(initial_path)); | 373 EXPECT_FALSE(file_util::PathExists(initial_path)); |
| 326 EXPECT_TRUE(file_util::PathExists(new_path)); | 374 EXPECT_TRUE(file_util::PathExists(new_path)); |
| 327 | 375 |
| 328 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | 376 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); |
| 329 | 377 |
| 330 base_file_->Finish(); | 378 base_file_->Finish(); |
| 331 } | 379 } |
| 332 | 380 |
| 333 // Write data to the file multiple times. | 381 // Write data to the file multiple times. |
| 334 TEST_F(BaseFileTest, MultipleWritesWithError) { | 382 TEST_F(BaseFileTest, MultipleWritesWithError) { |
| 335 ASSERT_TRUE(OpenMockFileStream()); | 383 ASSERT_TRUE(OpenMockFileStream()); |
| 336 base_file_.reset(new BaseFile(mock_file_stream_->get_path(), | 384 base_file_.reset(new BaseFile(mock_file_stream_->get_path(), |
| 337 GURL(), GURL(), 0, mock_file_stream_)); | 385 GURL(), |
| 338 EXPECT_EQ(net::OK, base_file_->Initialize(false)); | 386 GURL(), |
| 387 0, | |
| 388 false, | |
| 389 "", | |
| 390 mock_file_stream_)); | |
| 391 EXPECT_EQ(net::OK, base_file_->Initialize()); | |
| 339 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 392 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 340 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | 393 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); |
| 341 ForceError(net::ERR_ACCESS_DENIED); | 394 ForceError(net::ERR_ACCESS_DENIED); |
| 342 ASSERT_NE(net::OK, AppendDataToFile(kTestData3)); | 395 ASSERT_NE(net::OK, AppendDataToFile(kTestData3)); |
| 343 std::string hash; | 396 std::string hash; |
| 344 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); | 397 EXPECT_FALSE(base_file_->GetHash(&hash)); |
| 345 base_file_->Finish(); | 398 base_file_->Finish(); |
| 346 } | 399 } |
| 347 | 400 |
| 348 // Try to write to uninitialized file. | 401 // Try to write to uninitialized file. |
| 349 TEST_F(BaseFileTest, UninitializedFile) { | 402 TEST_F(BaseFileTest, UninitializedFile) { |
| 350 expect_in_progress_ = false; | 403 expect_in_progress_ = false; |
| 351 EXPECT_EQ(net::ERR_INVALID_HANDLE, AppendDataToFile(kTestData1)); | 404 EXPECT_EQ(net::ERR_INVALID_HANDLE, AppendDataToFile(kTestData1)); |
| 352 } | 405 } |
| 353 | 406 |
| 354 // Create two |BaseFile|s with the same file, and attempt to write to both. | 407 // Create two |BaseFile|s with the same file, and attempt to write to both. |
| 355 // Overwrite base_file_ with another file with the same name and | 408 // Overwrite base_file_ with another file with the same name and |
| 356 // non-zero contents, and make sure the last file to close 'wins'. | 409 // non-zero contents, and make sure the last file to close 'wins'. |
| 357 TEST_F(BaseFileTest, DuplicateBaseFile) { | 410 TEST_F(BaseFileTest, DuplicateBaseFile) { |
| 358 EXPECT_EQ(net::OK, base_file_->Initialize(false)); | 411 EXPECT_EQ(net::OK, base_file_->Initialize()); |
| 359 | 412 |
| 360 // Create another |BaseFile| referring to the file that |base_file_| owns. | 413 // Create another |BaseFile| referring to the file that |base_file_| owns. |
| 361 CreateFileWithName(base_file_->full_path()); | 414 CreateFileWithName(base_file_->full_path()); |
| 362 | 415 |
| 363 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 416 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 364 base_file_->Finish(); | 417 base_file_->Finish(); |
| 365 } | 418 } |
| 366 | 419 |
| 367 // Create a file and append to it. | 420 // Create a file and append to it. |
| 368 TEST_F(BaseFileTest, AppendToBaseFile) { | 421 TEST_F(BaseFileTest, AppendToBaseFile) { |
| 369 // Create a new file. | 422 // Create a new file. |
| 370 FilePath existing_file_name = CreateTestFile(); | 423 FilePath existing_file_name = CreateTestFile(); |
| 371 | 424 |
| 372 set_expected_data(kTestData4); | 425 set_expected_data(kTestData4); |
| 373 | 426 |
| 374 // Use the file we've just created. | 427 // Use the file we've just created. |
| 375 base_file_.reset( | 428 base_file_.reset(new BaseFile(existing_file_name, |
| 376 new BaseFile(existing_file_name, GURL(), GURL(), kTestDataLength4, | 429 GURL(), |
| 377 file_stream_)); | 430 GURL(), |
| 431 kTestDataLength4, | |
| 432 false, | |
| 433 "", | |
| 434 file_stream_)); | |
| 378 | 435 |
| 379 EXPECT_EQ(net::OK, base_file_->Initialize(false)); | 436 EXPECT_EQ(net::OK, base_file_->Initialize()); |
| 380 | 437 |
| 381 const FilePath file_name = base_file_->full_path(); | 438 const FilePath file_name = base_file_->full_path(); |
| 382 EXPECT_NE(FilePath::StringType(), file_name.value()); | 439 EXPECT_NE(FilePath::StringType(), file_name.value()); |
| 383 | 440 |
| 384 // Write into the file. | 441 // Write into the file. |
| 385 EXPECT_EQ(net::OK, AppendDataToFile(kTestData1)); | 442 EXPECT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 386 | 443 |
| 387 base_file_->Finish(); | 444 base_file_->Finish(); |
| 388 base_file_->Detach(); | 445 base_file_->Detach(); |
| 389 expect_file_survives_ = true; | 446 expect_file_survives_ = true; |
| 390 } | 447 } |
| 391 | 448 |
| 392 // Create a read-only file and attempt to write to it. | 449 // Create a read-only file and attempt to write to it. |
| 393 TEST_F(BaseFileTest, ReadonlyBaseFile) { | 450 TEST_F(BaseFileTest, ReadonlyBaseFile) { |
| 394 // Create a new file. | 451 // Create a new file. |
| 395 FilePath readonly_file_name = CreateTestFile(); | 452 FilePath readonly_file_name = CreateTestFile(); |
| 396 | 453 |
| 397 // Make it read-only. | 454 // Make it read-only. |
| 398 EXPECT_TRUE(file_util::MakeFileUnwritable(readonly_file_name)); | 455 EXPECT_TRUE(file_util::MakeFileUnwritable(readonly_file_name)); |
| 399 | 456 |
| 400 // Try to overwrite it. | 457 // Try to overwrite it. |
| 401 base_file_.reset( | 458 base_file_.reset(new BaseFile(readonly_file_name, |
| 402 new BaseFile(readonly_file_name, GURL(), GURL(), 0, file_stream_)); | 459 GURL(), |
| 460 GURL(), | |
| 461 0, | |
| 462 false, | |
| 463 "", | |
| 464 file_stream_)); | |
| 403 | 465 |
| 404 expect_in_progress_ = false; | 466 expect_in_progress_ = false; |
| 405 | 467 |
| 406 int init_error = base_file_->Initialize(false); | 468 int init_error = base_file_->Initialize(); |
| 407 DVLOG(1) << " init_error = " << init_error; | 469 DVLOG(1) << " init_error = " << init_error; |
| 408 EXPECT_NE(net::OK, init_error); | 470 EXPECT_NE(net::OK, init_error); |
| 409 | 471 |
| 410 const FilePath file_name = base_file_->full_path(); | 472 const FilePath file_name = base_file_->full_path(); |
| 411 EXPECT_NE(FilePath::StringType(), file_name.value()); | 473 EXPECT_NE(FilePath::StringType(), file_name.value()); |
| 412 | 474 |
| 413 // Write into the file. | 475 // Write into the file. |
| 414 EXPECT_NE(net::OK, AppendDataToFile(kTestData1)); | 476 EXPECT_NE(net::OK, AppendDataToFile(kTestData1)); |
| 415 | 477 |
| 416 base_file_->Finish(); | 478 base_file_->Finish(); |
| 417 base_file_->Detach(); | 479 base_file_->Detach(); |
| 418 expect_file_survives_ = true; | 480 expect_file_survives_ = true; |
| 419 } | 481 } |
| 420 | 482 |
| 421 TEST_F(BaseFileTest, IsEmptySha256Hash) { | 483 TEST_F(BaseFileTest, IsEmptyHash) { |
| 422 std::string empty(BaseFile::kSha256HashLen, '\x00'); | 484 std::string empty(BaseFile::kSha256HashLen, '\x00'); |
| 423 EXPECT_TRUE(BaseFile::IsEmptySha256Hash(empty)); | 485 EXPECT_TRUE(BaseFile::IsEmptyHash(empty)); |
| 424 std::string not_empty(BaseFile::kSha256HashLen, '\x01'); | 486 std::string not_empty(BaseFile::kSha256HashLen, '\x01'); |
| 425 EXPECT_FALSE(BaseFile::IsEmptySha256Hash(not_empty)); | 487 EXPECT_FALSE(BaseFile::IsEmptyHash(not_empty)); |
| 426 EXPECT_FALSE(BaseFile::IsEmptySha256Hash("")); | 488 EXPECT_FALSE(BaseFile::IsEmptyHash("")); |
| 427 } | 489 } |
| 428 | 490 |
| 429 // Test that calculating speed after no writes. | 491 // Test that calculating speed after no writes. |
| 430 TEST_F(BaseFileTest, SpeedWithoutWrite) { | 492 TEST_F(BaseFileTest, SpeedWithoutWrite) { |
| 431 ASSERT_EQ(net::OK, base_file_->Initialize(false)); | 493 ASSERT_EQ(net::OK, base_file_->Initialize()); |
| 432 base::TimeTicks current = StartTick() + kElapsedTimeDelta; | 494 base::TimeTicks current = StartTick() + kElapsedTimeDelta; |
| 433 ASSERT_EQ(0, CurrentSpeedAtTime(current)); | 495 ASSERT_EQ(0, CurrentSpeedAtTime(current)); |
| 434 base_file_->Finish(); | 496 base_file_->Finish(); |
| 435 } | 497 } |
| 436 | 498 |
| 437 // Test that calculating speed after a single write. | 499 // Test that calculating speed after a single write. |
| 438 TEST_F(BaseFileTest, SpeedAfterSingleWrite) { | 500 TEST_F(BaseFileTest, SpeedAfterSingleWrite) { |
| 439 ASSERT_EQ(net::OK, base_file_->Initialize(false)); | 501 ASSERT_EQ(net::OK, base_file_->Initialize()); |
| 440 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 502 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 441 base::TimeTicks current = StartTick() + kElapsedTimeDelta; | 503 base::TimeTicks current = StartTick() + kElapsedTimeDelta; |
| 442 int64 expected_speed = kTestDataLength1 / kElapsedTimeSeconds; | 504 int64 expected_speed = kTestDataLength1 / kElapsedTimeSeconds; |
| 443 ASSERT_EQ(expected_speed, CurrentSpeedAtTime(current)); | 505 ASSERT_EQ(expected_speed, CurrentSpeedAtTime(current)); |
| 444 base_file_->Finish(); | 506 base_file_->Finish(); |
| 445 } | 507 } |
| 446 | 508 |
| 447 // Test that calculating speed after a multiple writes. | 509 // Test that calculating speed after a multiple writes. |
| 448 TEST_F(BaseFileTest, SpeedAfterMultipleWrite) { | 510 TEST_F(BaseFileTest, SpeedAfterMultipleWrite) { |
| 449 ASSERT_EQ(net::OK, base_file_->Initialize(false)); | 511 ASSERT_EQ(net::OK, base_file_->Initialize()); |
| 450 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 512 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 451 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); | 513 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); |
| 452 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); | 514 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); |
| 453 ASSERT_EQ(net::OK, AppendDataToFile(kTestData4)); | 515 ASSERT_EQ(net::OK, AppendDataToFile(kTestData4)); |
| 454 base::TimeTicks current = StartTick() + kElapsedTimeDelta; | 516 base::TimeTicks current = StartTick() + kElapsedTimeDelta; |
| 455 int64 expected_speed = (kTestDataLength1 + kTestDataLength2 + | 517 int64 expected_speed = (kTestDataLength1 + kTestDataLength2 + |
| 456 kTestDataLength3 + kTestDataLength4) / kElapsedTimeSeconds; | 518 kTestDataLength3 + kTestDataLength4) / kElapsedTimeSeconds; |
| 457 ASSERT_EQ(expected_speed, CurrentSpeedAtTime(current)); | 519 ASSERT_EQ(expected_speed, CurrentSpeedAtTime(current)); |
| 458 base_file_->Finish(); | 520 base_file_->Finish(); |
| 459 } | 521 } |
| 460 | 522 |
| 461 // Test that calculating speed after no delay - should not divide by 0. | 523 // Test that calculating speed after no delay - should not divide by 0. |
| 462 TEST_F(BaseFileTest, SpeedAfterNoElapsedTime) { | 524 TEST_F(BaseFileTest, SpeedAfterNoElapsedTime) { |
| 463 ASSERT_EQ(net::OK, base_file_->Initialize(false)); | 525 ASSERT_EQ(net::OK, base_file_->Initialize()); |
| 464 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); | 526 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); |
| 465 ASSERT_EQ(0, CurrentSpeedAtTime(StartTick())); | 527 ASSERT_EQ(0, CurrentSpeedAtTime(StartTick())); |
| 466 base_file_->Finish(); | 528 base_file_->Finish(); |
| 467 } | 529 } |
| OLD | NEW |