Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(479)

Side by Side Diff: content/browser/download/base_file_unittest.cc

Issue 8404049: Added member data to classes to support download resumption. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: DownloadSaveInfo::offset is now int64. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 26 matching lines...) Expand all
37 BaseFileTest() 37 BaseFileTest()
38 : expect_file_survives_(false), 38 : expect_file_survives_(false),
39 expect_in_progress_(true), 39 expect_in_progress_(true),
40 expected_error_(false), 40 expected_error_(false),
41 file_thread_(BrowserThread::FILE, &message_loop_) { 41 file_thread_(BrowserThread::FILE, &message_loop_) {
42 } 42 }
43 43
44 virtual void SetUp() { 44 virtual void SetUp() {
45 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 45 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
46 base_file_.reset( 46 base_file_.reset(
47 new BaseFile(FilePath(), GURL(), GURL(), 0, file_stream_)); 47 new BaseFile(FilePath(), GURL(), GURL(), 0, "", file_stream_));
48 } 48 }
49 49
50 virtual void TearDown() { 50 virtual void TearDown() {
51 EXPECT_FALSE(base_file_->in_progress()); 51 EXPECT_FALSE(base_file_->in_progress());
52 if (!expected_error_) { 52 if (!expected_error_) {
53 EXPECT_EQ(static_cast<int64>(expected_data_.size()), 53 EXPECT_EQ(static_cast<int64>(expected_data_.size()),
54 base_file_->bytes_so_far()); 54 base_file_->bytes_so_far());
55 } 55 }
56 56
57 FilePath full_path = base_file_->full_path(); 57 FilePath full_path = base_file_->full_path();
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 return appended; 111 return appended;
112 } 112 }
113 113
114 void set_expected_data(const std::string& data) { expected_data_ = data; } 114 void set_expected_data(const std::string& data) { expected_data_ = data; }
115 115
116 // Helper functions. 116 // Helper functions.
117 // Create a file. Returns the complete file path. 117 // Create a file. Returns the complete file path.
118 static FilePath CreateTestFile() { 118 static FilePath CreateTestFile() {
119 FilePath file_name; 119 FilePath file_name;
120 linked_ptr<net::FileStream> dummy_file_stream; 120 linked_ptr<net::FileStream> dummy_file_stream;
121 BaseFile file(FilePath(), GURL(), GURL(), 0, dummy_file_stream); 121 BaseFile file(FilePath(), GURL(), GURL(), 0, "", dummy_file_stream);
122 122
123 EXPECT_EQ(net::OK, file.Initialize(false)); 123 EXPECT_EQ(net::OK, file.Initialize(false));
124 file_name = file.full_path(); 124 file_name = file.full_path();
125 EXPECT_NE(FilePath::StringType(), file_name.value()); 125 EXPECT_NE(FilePath::StringType(), file_name.value());
126 126
127 EXPECT_EQ(net::OK, file.AppendDataToFile(kTestData4, kTestDataLength4)); 127 EXPECT_EQ(net::OK, file.AppendDataToFile(kTestData4, kTestDataLength4));
128 128
129 // Keep the file from getting deleted when existing_file_name is deleted. 129 // Keep the file from getting deleted when existing_file_name is deleted.
130 file.Detach(); 130 file.Detach();
131 131
132 return file_name; 132 return file_name;
133 } 133 }
134 134
135 // Create a file with the specified file name. 135 // Create a file with the specified file name.
136 static void CreateFileWithName(const FilePath& file_name) { 136 static void CreateFileWithName(const FilePath& file_name) {
137 EXPECT_NE(FilePath::StringType(), file_name.value()); 137 EXPECT_NE(FilePath::StringType(), file_name.value());
138 linked_ptr<net::FileStream> dummy_file_stream; 138 linked_ptr<net::FileStream> dummy_file_stream;
139 BaseFile duplicate_file(file_name, GURL(), GURL(), 0, dummy_file_stream); 139 BaseFile duplicate_file(
140 file_name, GURL(), GURL(), 0, "", dummy_file_stream);
140 EXPECT_EQ(net::OK, duplicate_file.Initialize(false)); 141 EXPECT_EQ(net::OK, duplicate_file.Initialize(false));
141 // Write something into it. 142 // Write something into it.
142 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); 143 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4);
143 // Detach the file so it isn't deleted on destruction of |duplicate_file|. 144 // Detach the file so it isn't deleted on destruction of |duplicate_file|.
144 duplicate_file.Detach(); 145 duplicate_file.Detach();
145 } 146 }
146 147
147 protected: 148 protected:
148 linked_ptr<net::FileStream> file_stream_; 149 linked_ptr<net::FileStream> file_stream_;
149 linked_ptr<net::testing::MockFileStream> mock_file_stream_; 150 linked_ptr<net::testing::MockFileStream> mock_file_stream_;
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); 246 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2));
246 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); 247 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3));
247 std::string hash; 248 std::string hash;
248 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); 249 EXPECT_FALSE(base_file_->GetSha256Hash(&hash));
249 base_file_->Finish(); 250 base_file_->Finish();
250 } 251 }
251 252
252 // Write data to the file once and calculate its sha256 hash. 253 // Write data to the file once and calculate its sha256 hash.
253 TEST_F(BaseFileTest, SingleWriteWithHash) { 254 TEST_F(BaseFileTest, SingleWriteWithHash) {
254 ASSERT_EQ(net::OK, base_file_->Initialize(true)); 255 ASSERT_EQ(net::OK, base_file_->Initialize(true));
256 // Can get partial hash states before Finish() is called.
257 EXPECT_STRNE(std::string().c_str(), base_file_->GetSha256HashState().c_str());
255 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 258 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
259 EXPECT_STRNE(std::string().c_str(), base_file_->GetSha256HashState().c_str());
256 base_file_->Finish(); 260 base_file_->Finish();
257 261
258 std::string hash; 262 std::string hash;
259 base_file_->GetSha256Hash(&hash); 263 base_file_->GetSha256Hash(&hash);
260 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", 264 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE",
261 base::HexEncode(hash.data(), hash.size())); 265 base::HexEncode(hash.data(), hash.size()));
262 } 266 }
263 267
264 // Write data to the file multiple times and calculate its sha256 hash. 268 // Write data to the file multiple times and calculate its sha256 hash.
265 TEST_F(BaseFileTest, MultipleWritesWithHash) { 269 TEST_F(BaseFileTest, MultipleWritesWithHash) {
266 std::string hash; 270 std::string hash;
267 271
268 ASSERT_EQ(net::OK, base_file_->Initialize(true)); 272 ASSERT_EQ(net::OK, base_file_->Initialize(true));
269 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 273 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
270 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); 274 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2));
271 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); 275 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3));
272 // no hash before Finish() is called either. 276 // No hash before Finish() is called.
273 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); 277 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 std::string hash_state;
294 hash_state = base_file_->GetSha256HashState();
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
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 "",
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
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 "",
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 "",
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698