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

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: Incorporated Randy's comments. 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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 ASSERT_EQ(net::OK, base_file_->Initialize(false)); 244 ASSERT_EQ(net::OK, base_file_->Initialize(false));
244 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 245 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
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) {
Randy Smith (Not in Mondays) 2011/11/16 21:40:25 I'd think that we'd also want to confirm that if w
ahendrickson 2011/11/19 20:18:04 That sounds like a good idea. Done.
255 std::string partial_hash;
254 ASSERT_EQ(net::OK, base_file_->Initialize(true)); 256 ASSERT_EQ(net::OK, base_file_->Initialize(true));
257 // Can get partial hashes before Finish() is called.
258 EXPECT_TRUE(base_file_->GetSha256Hash(&partial_hash));
255 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 259 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
260 EXPECT_TRUE(base_file_->GetSha256Hash(&partial_hash));
256 base_file_->Finish(); 261 base_file_->Finish();
257 262
258 std::string hash; 263 std::string hash;
259 base_file_->GetSha256Hash(&hash); 264 base_file_->GetSha256Hash(&hash);
260 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", 265 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE",
261 base::HexEncode(hash.data(), hash.size())); 266 base::HexEncode(hash.data(), hash.size()));
262 } 267 }
263 268
264 // Write data to the file multiple times and calculate its sha256 hash. 269 // Write data to the file multiple times and calculate its sha256 hash.
265 TEST_F(BaseFileTest, MultipleWritesWithHash) { 270 TEST_F(BaseFileTest, MultipleWritesWithHash) {
266 std::string hash; 271 std::string hash;
267 272
268 ASSERT_EQ(net::OK, base_file_->Initialize(true)); 273 ASSERT_EQ(net::OK, base_file_->Initialize(true));
269 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 274 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
270 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); 275 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2));
271 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); 276 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3));
272 // no hash before Finish() is called either.
273 EXPECT_FALSE(base_file_->GetSha256Hash(&hash));
274 base_file_->Finish(); 277 base_file_->Finish();
275 278
276 EXPECT_TRUE(base_file_->GetSha256Hash(&hash)); 279 EXPECT_TRUE(base_file_->GetSha256Hash(&hash));
277 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8", 280 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8",
278 base::HexEncode(hash.data(), hash.size())); 281 base::HexEncode(hash.data(), hash.size()));
279 } 282 }
280 283
281 // Rename the file after all writes to it. 284 // Rename the file after all writes to it.
282 TEST_F(BaseFileTest, WriteThenRename) { 285 TEST_F(BaseFileTest, WriteThenRename) {
283 ASSERT_EQ(net::OK, base_file_->Initialize(false)); 286 ASSERT_EQ(net::OK, base_file_->Initialize(false));
(...skipping 30 matching lines...) Expand all
314 317
315 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); 318 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2));
316 319
317 base_file_->Finish(); 320 base_file_->Finish();
318 } 321 }
319 322
320 // Write data to the file multiple times. 323 // Write data to the file multiple times.
321 TEST_F(BaseFileTest, MultipleWritesWithError) { 324 TEST_F(BaseFileTest, MultipleWritesWithError) {
322 ASSERT_TRUE(OpenMockFileStream()); 325 ASSERT_TRUE(OpenMockFileStream());
323 base_file_.reset(new BaseFile(mock_file_stream_->get_path(), 326 base_file_.reset(new BaseFile(mock_file_stream_->get_path(),
324 GURL(), GURL(), 0, mock_file_stream_)); 327 GURL(), GURL(), 0, "", mock_file_stream_));
325 EXPECT_EQ(net::OK, base_file_->Initialize(false)); 328 EXPECT_EQ(net::OK, base_file_->Initialize(false));
326 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 329 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
327 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); 330 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2));
328 ForceError(net::ERR_ACCESS_DENIED); 331 ForceError(net::ERR_ACCESS_DENIED);
329 ASSERT_NE(net::OK, AppendDataToFile(kTestData3)); 332 ASSERT_NE(net::OK, AppendDataToFile(kTestData3));
330 std::string hash; 333 std::string hash;
331 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); 334 EXPECT_FALSE(base_file_->GetSha256Hash(&hash));
332 base_file_->Finish(); 335 base_file_->Finish();
333 } 336 }
334 337
(...skipping 19 matching lines...) Expand all
354 // Create a file and append to it. 357 // Create a file and append to it.
355 TEST_F(BaseFileTest, AppendToBaseFile) { 358 TEST_F(BaseFileTest, AppendToBaseFile) {
356 // Create a new file. 359 // Create a new file.
357 FilePath existing_file_name = CreateTestFile(); 360 FilePath existing_file_name = CreateTestFile();
358 361
359 set_expected_data(kTestData4); 362 set_expected_data(kTestData4);
360 363
361 // Use the file we've just created. 364 // Use the file we've just created.
362 base_file_.reset( 365 base_file_.reset(
363 new BaseFile(existing_file_name, GURL(), GURL(), kTestDataLength4, 366 new BaseFile(existing_file_name, GURL(), GURL(), kTestDataLength4,
364 file_stream_)); 367 "", file_stream_));
365 368
366 EXPECT_EQ(net::OK, base_file_->Initialize(false)); 369 EXPECT_EQ(net::OK, base_file_->Initialize(false));
367 370
368 const FilePath file_name = base_file_->full_path(); 371 const FilePath file_name = base_file_->full_path();
369 EXPECT_NE(FilePath::StringType(), file_name.value()); 372 EXPECT_NE(FilePath::StringType(), file_name.value());
370 373
371 // Write into the file. 374 // Write into the file.
372 EXPECT_EQ(net::OK, AppendDataToFile(kTestData1)); 375 EXPECT_EQ(net::OK, AppendDataToFile(kTestData1));
373 376
374 base_file_->Finish(); 377 base_file_->Finish();
375 base_file_->Detach(); 378 base_file_->Detach();
376 expect_file_survives_ = true; 379 expect_file_survives_ = true;
377 } 380 }
378 381
379 // Create a read-only file and attempt to write to it. 382 // Create a read-only file and attempt to write to it.
380 TEST_F(BaseFileTest, ReadonlyBaseFile) { 383 TEST_F(BaseFileTest, ReadonlyBaseFile) {
381 // Create a new file. 384 // Create a new file.
382 FilePath readonly_file_name = CreateTestFile(); 385 FilePath readonly_file_name = CreateTestFile();
383 386
384 // Make it read-only. 387 // Make it read-only.
385 EXPECT_TRUE(file_util::MakeFileUnwritable(readonly_file_name)); 388 EXPECT_TRUE(file_util::MakeFileUnwritable(readonly_file_name));
386 389
387 // Try to overwrite it. 390 // Try to overwrite it.
388 base_file_.reset( 391 base_file_.reset(
389 new BaseFile(readonly_file_name, GURL(), GURL(), 0, file_stream_)); 392 new BaseFile(readonly_file_name, GURL(), GURL(), 0, "", file_stream_));
390 393
391 expect_in_progress_ = false; 394 expect_in_progress_ = false;
392 395
393 int init_error = base_file_->Initialize(false); 396 int init_error = base_file_->Initialize(false);
394 DVLOG(1) << " init_error = " << init_error; 397 DVLOG(1) << " init_error = " << init_error;
395 EXPECT_NE(net::OK, init_error); 398 EXPECT_NE(net::OK, init_error);
396 399
397 const FilePath file_name = base_file_->full_path(); 400 const FilePath file_name = base_file_->full_path();
398 EXPECT_NE(FilePath::StringType(), file_name.value()); 401 EXPECT_NE(FilePath::StringType(), file_name.value());
399 402
400 // Write into the file. 403 // Write into the file.
401 EXPECT_NE(net::OK, AppendDataToFile(kTestData1)); 404 EXPECT_NE(net::OK, AppendDataToFile(kTestData1));
402 405
403 base_file_->Finish(); 406 base_file_->Finish();
404 base_file_->Detach(); 407 base_file_->Detach();
405 expect_file_survives_ = true; 408 expect_file_survives_ = true;
406 } 409 }
407 410
408 TEST_F(BaseFileTest, IsEmptySha256Hash) { 411 TEST_F(BaseFileTest, IsEmptySha256Hash) {
409 std::string empty(BaseFile::kSha256HashLen, '\x00'); 412 std::string empty(BaseFile::kSha256HashLen, '\x00');
410 EXPECT_TRUE(BaseFile::IsEmptySha256Hash(empty)); 413 EXPECT_TRUE(BaseFile::IsEmptySha256Hash(empty));
411 std::string not_empty(BaseFile::kSha256HashLen, '\x01'); 414 std::string not_empty(BaseFile::kSha256HashLen, '\x01');
412 EXPECT_FALSE(BaseFile::IsEmptySha256Hash(not_empty)); 415 EXPECT_FALSE(BaseFile::IsEmptySha256Hash(not_empty));
413 EXPECT_FALSE(BaseFile::IsEmptySha256Hash("")); 416 EXPECT_FALSE(BaseFile::IsEmptySha256Hash(""));
414 } 417 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698