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

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

Issue 9223019: Added net logging to BaseFile. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Minor cleanup Created 8 years, 10 months 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) 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/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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 BaseFileTest() 44 BaseFileTest()
45 : expect_file_survives_(false), 45 : expect_file_survives_(false),
46 expect_in_progress_(true), 46 expect_in_progress_(true),
47 expected_error_(false), 47 expected_error_(false),
48 file_thread_(BrowserThread::FILE, &message_loop_) { 48 file_thread_(BrowserThread::FILE, &message_loop_) {
49 } 49 }
50 50
51 virtual void SetUp() { 51 virtual void SetUp() {
52 ResetHash(); 52 ResetHash();
53 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 53 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
54 base_file_.reset( 54 base_file_.reset(new BaseFile(FilePath(),
55 new BaseFile(FilePath(), GURL(), GURL(), 0, false, "", file_stream_)); 55 GURL(),
56 GURL(),
57 0,
58 false,
59 "",
60 file_stream_,
61 net::BoundNetLog()));
56 } 62 }
57 63
58 virtual void TearDown() { 64 virtual void TearDown() {
59 EXPECT_FALSE(base_file_->in_progress()); 65 EXPECT_FALSE(base_file_->in_progress());
60 if (!expected_error_) { 66 if (!expected_error_) {
61 EXPECT_EQ(static_cast<int64>(expected_data_.size()), 67 EXPECT_EQ(static_cast<int64>(expected_data_.size()),
62 base_file_->bytes_so_far()); 68 base_file_->bytes_so_far());
63 } 69 }
64 70
65 FilePath full_path = base_file_->full_path(); 71 FilePath full_path = base_file_->full_path();
(...skipping 23 matching lines...) Expand all
89 95
90 std::string GetFinalHash() { 96 std::string GetFinalHash() {
91 std::string hash; 97 std::string hash;
92 secure_hash_->Finish(sha256_hash_, kSha256HashLen); 98 secure_hash_->Finish(sha256_hash_, kSha256HashLen);
93 hash.assign(reinterpret_cast<const char*>(sha256_hash_), 99 hash.assign(reinterpret_cast<const char*>(sha256_hash_),
94 sizeof(sha256_hash_)); 100 sizeof(sha256_hash_));
95 return hash; 101 return hash;
96 } 102 }
97 103
98 void MakeFileWithHash() { 104 void MakeFileWithHash() {
99 base_file_.reset( 105 base_file_.reset(new BaseFile(FilePath(),
100 new BaseFile(FilePath(), GURL(), GURL(), 0, true, "", file_stream_)); 106 GURL(),
107 GURL(),
108 0,
109 true,
110 "",
111 file_stream_,
112 net::BoundNetLog()));
101 } 113 }
102 114
103 bool OpenMockFileStream() { 115 bool OpenMockFileStream() {
104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
105 117
106 FilePath path; 118 FilePath path;
107 if (!file_util::CreateTemporaryFile(&path)) 119 if (!file_util::CreateTemporaryFile(&path))
108 return false; 120 return false;
109 121
110 // Create a new file stream. 122 // Create a new file stream.
(...skipping 30 matching lines...) Expand all
141 return appended; 153 return appended;
142 } 154 }
143 155
144 void set_expected_data(const std::string& data) { expected_data_ = data; } 156 void set_expected_data(const std::string& data) { expected_data_ = data; }
145 157
146 // Helper functions. 158 // Helper functions.
147 // Create a file. Returns the complete file path. 159 // Create a file. Returns the complete file path.
148 static FilePath CreateTestFile() { 160 static FilePath CreateTestFile() {
149 FilePath file_name; 161 FilePath file_name;
150 linked_ptr<net::FileStream> dummy_file_stream; 162 linked_ptr<net::FileStream> dummy_file_stream;
151 BaseFile file(FilePath(), GURL(), GURL(), 0, false, "", dummy_file_stream); 163 BaseFile file(FilePath(),
164 GURL(),
165 GURL(),
166 0,
167 false,
168 "",
169 dummy_file_stream,
170 net::BoundNetLog());
152 171
153 EXPECT_EQ(net::OK, file.Initialize()); 172 EXPECT_EQ(net::OK, file.Initialize());
154 file_name = file.full_path(); 173 file_name = file.full_path();
155 EXPECT_NE(FilePath::StringType(), file_name.value()); 174 EXPECT_NE(FilePath::StringType(), file_name.value());
156 175
157 EXPECT_EQ(net::OK, file.AppendDataToFile(kTestData4, kTestDataLength4)); 176 EXPECT_EQ(net::OK, file.AppendDataToFile(kTestData4, kTestDataLength4));
158 177
159 // Keep the file from getting deleted when existing_file_name is deleted. 178 // Keep the file from getting deleted when existing_file_name is deleted.
160 file.Detach(); 179 file.Detach();
161 180
162 return file_name; 181 return file_name;
163 } 182 }
164 183
165 // Create a file with the specified file name. 184 // Create a file with the specified file name.
166 static void CreateFileWithName(const FilePath& file_name) { 185 static void CreateFileWithName(const FilePath& file_name) {
167 EXPECT_NE(FilePath::StringType(), file_name.value()); 186 EXPECT_NE(FilePath::StringType(), file_name.value());
168 linked_ptr<net::FileStream> dummy_file_stream; 187 linked_ptr<net::FileStream> dummy_file_stream;
169 BaseFile duplicate_file( 188 BaseFile duplicate_file(file_name,
170 file_name, GURL(), GURL(), 0, false, "", dummy_file_stream); 189 GURL(),
190 GURL(),
191 0,
192 false,
193 "",
194 dummy_file_stream,
195 net::BoundNetLog());
171 EXPECT_EQ(net::OK, duplicate_file.Initialize()); 196 EXPECT_EQ(net::OK, duplicate_file.Initialize());
172 // Write something into it. 197 // Write something into it.
173 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); 198 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4);
174 // Detach the file so it isn't deleted on destruction of |duplicate_file|. 199 // Detach the file so it isn't deleted on destruction of |duplicate_file|.
175 duplicate_file.Detach(); 200 duplicate_file.Detach();
176 } 201 }
177 202
178 int64 CurrentSpeedAtTime(base::TimeTicks current_time) { 203 int64 CurrentSpeedAtTime(base::TimeTicks current_time) {
179 EXPECT_TRUE(base_file_.get()); 204 EXPECT_TRUE(base_file_.get());
180 return base_file_->CurrentSpeedAtTime(current_time); 205 return base_file_->CurrentSpeedAtTime(current_time);
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 base_file_->Finish(); 405 base_file_->Finish();
381 406
382 // Create another file 407 // Create another file
383 linked_ptr<net::FileStream> second_stream; 408 linked_ptr<net::FileStream> second_stream;
384 BaseFile second_file(FilePath(), 409 BaseFile second_file(FilePath(),
385 GURL(), 410 GURL(),
386 GURL(), 411 GURL(),
387 base_file_->bytes_so_far(), 412 base_file_->bytes_so_far(),
388 true, 413 true,
389 hash_state, 414 hash_state,
390 second_stream); 415 second_stream,
416 net::BoundNetLog());
391 ASSERT_EQ(net::OK, second_file.Initialize()); 417 ASSERT_EQ(net::OK, second_file.Initialize());
392 std::string data(kTestData3); 418 std::string data(kTestData3);
393 EXPECT_EQ(net::OK, second_file.AppendDataToFile(data.data(), data.size())); 419 EXPECT_EQ(net::OK, second_file.AppendDataToFile(data.data(), data.size()));
394 second_file.Finish(); 420 second_file.Finish();
395 421
396 std::string hash; 422 std::string hash;
397 EXPECT_TRUE(second_file.GetHash(&hash)); 423 EXPECT_TRUE(second_file.GetHash(&hash));
398 // This will fail until getting the hash state is supported in SecureHash. 424 // This will fail until getting the hash state is supported in SecureHash.
399 EXPECT_STREQ(expected_hash_hex.c_str(), 425 EXPECT_STREQ(expected_hash_hex.c_str(),
400 base::HexEncode(hash.data(), hash.size()).c_str()); 426 base::HexEncode(hash.data(), hash.size()).c_str());
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 467
442 // Write data to the file multiple times. 468 // Write data to the file multiple times.
443 TEST_F(BaseFileTest, MultipleWritesWithError) { 469 TEST_F(BaseFileTest, MultipleWritesWithError) {
444 ASSERT_TRUE(OpenMockFileStream()); 470 ASSERT_TRUE(OpenMockFileStream());
445 base_file_.reset(new BaseFile(mock_file_stream_->get_path(), 471 base_file_.reset(new BaseFile(mock_file_stream_->get_path(),
446 GURL(), 472 GURL(),
447 GURL(), 473 GURL(),
448 0, 474 0,
449 false, 475 false,
450 "", 476 "",
451 mock_file_stream_)); 477 mock_file_stream_,
478 net::BoundNetLog()));
452 EXPECT_EQ(net::OK, base_file_->Initialize()); 479 EXPECT_EQ(net::OK, base_file_->Initialize());
453 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 480 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
454 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); 481 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2));
455 ForceError(net::ERR_ACCESS_DENIED); 482 ForceError(net::ERR_ACCESS_DENIED);
456 ASSERT_NE(net::OK, AppendDataToFile(kTestData3)); 483 ASSERT_NE(net::OK, AppendDataToFile(kTestData3));
457 std::string hash; 484 std::string hash;
458 EXPECT_FALSE(base_file_->GetHash(&hash)); 485 EXPECT_FALSE(base_file_->GetHash(&hash));
459 base_file_->Finish(); 486 base_file_->Finish();
460 } 487 }
461 488
(...skipping 23 matching lines...) Expand all
485 512
486 set_expected_data(kTestData4); 513 set_expected_data(kTestData4);
487 514
488 // Use the file we've just created. 515 // Use the file we've just created.
489 base_file_.reset(new BaseFile(existing_file_name, 516 base_file_.reset(new BaseFile(existing_file_name,
490 GURL(), 517 GURL(),
491 GURL(), 518 GURL(),
492 kTestDataLength4, 519 kTestDataLength4,
493 false, 520 false,
494 "", 521 "",
495 file_stream_)); 522 file_stream_,
523 net::BoundNetLog()));
496 524
497 EXPECT_EQ(net::OK, base_file_->Initialize()); 525 EXPECT_EQ(net::OK, base_file_->Initialize());
498 526
499 const FilePath file_name = base_file_->full_path(); 527 const FilePath file_name = base_file_->full_path();
500 EXPECT_NE(FilePath::StringType(), file_name.value()); 528 EXPECT_NE(FilePath::StringType(), file_name.value());
501 529
502 // Write into the file. 530 // Write into the file.
503 EXPECT_EQ(net::OK, AppendDataToFile(kTestData1)); 531 EXPECT_EQ(net::OK, AppendDataToFile(kTestData1));
504 532
505 base_file_->Finish(); 533 base_file_->Finish();
506 base_file_->Detach(); 534 base_file_->Detach();
507 expect_file_survives_ = true; 535 expect_file_survives_ = true;
508 } 536 }
509 537
510 // Create a read-only file and attempt to write to it. 538 // Create a read-only file and attempt to write to it.
511 TEST_F(BaseFileTest, ReadonlyBaseFile) { 539 TEST_F(BaseFileTest, ReadonlyBaseFile) {
512 // Create a new file. 540 // Create a new file.
513 FilePath readonly_file_name = CreateTestFile(); 541 FilePath readonly_file_name = CreateTestFile();
514 542
515 // Make it read-only. 543 // Make it read-only.
516 EXPECT_TRUE(file_util::MakeFileUnwritable(readonly_file_name)); 544 EXPECT_TRUE(file_util::MakeFileUnwritable(readonly_file_name));
517 545
518 // Try to overwrite it. 546 // Try to overwrite it.
519 base_file_.reset(new BaseFile(readonly_file_name, 547 base_file_.reset(new BaseFile(readonly_file_name,
520 GURL(), 548 GURL(),
521 GURL(), 549 GURL(),
522 0, 550 0,
523 false, 551 false,
524 "", 552 "",
525 file_stream_)); 553 file_stream_,
554 net::BoundNetLog()));
526 555
527 expect_in_progress_ = false; 556 expect_in_progress_ = false;
528 557
529 int init_error = base_file_->Initialize(); 558 int init_error = base_file_->Initialize();
530 DVLOG(1) << " init_error = " << init_error; 559 DVLOG(1) << " init_error = " << init_error;
531 EXPECT_NE(net::OK, init_error); 560 EXPECT_NE(net::OK, init_error);
532 561
533 const FilePath file_name = base_file_->full_path(); 562 const FilePath file_name = base_file_->full_path();
534 EXPECT_NE(FilePath::StringType(), file_name.value()); 563 EXPECT_NE(FilePath::StringType(), file_name.value());
535 564
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
581 base_file_->Finish(); 610 base_file_->Finish();
582 } 611 }
583 612
584 // Test that calculating speed after no delay - should not divide by 0. 613 // Test that calculating speed after no delay - should not divide by 0.
585 TEST_F(BaseFileTest, SpeedAfterNoElapsedTime) { 614 TEST_F(BaseFileTest, SpeedAfterNoElapsedTime) {
586 ASSERT_EQ(net::OK, base_file_->Initialize()); 615 ASSERT_EQ(net::OK, base_file_->Initialize());
587 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 616 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
588 ASSERT_EQ(0, CurrentSpeedAtTime(StartTick())); 617 ASSERT_EQ(0, CurrentSpeedAtTime(StartTick()));
589 base_file_->Finish(); 618 base_file_->Finish();
590 } 619 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698