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

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

Issue 10905284: Use the user's preferred downloads directory for creating the initial download file. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix Windows path check Created 8 years, 3 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
« no previous file with comments | « content/browser/download/base_file.cc ('k') | content/browser/download/download_create_info.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 base_file_->bytes_so_far()); 150 base_file_->bytes_so_far());
151 } 151 }
152 } 152 }
153 return appended; 153 return appended;
154 } 154 }
155 155
156 void set_expected_data(const std::string& data) { expected_data_ = data; } 156 void set_expected_data(const std::string& data) { expected_data_ = data; }
157 157
158 // Helper functions. 158 // Helper functions.
159 // Create a file. Returns the complete file path. 159 // Create a file. Returns the complete file path.
160 static FilePath CreateTestFile() { 160 FilePath CreateTestFile() {
161 FilePath file_name; 161 FilePath file_name;
162 linked_ptr<net::FileStream> dummy_file_stream; 162 linked_ptr<net::FileStream> dummy_file_stream;
163 BaseFile file(FilePath(), 163 BaseFile file(FilePath(),
164 GURL(), 164 GURL(),
165 GURL(), 165 GURL(),
166 0, 166 0,
167 false, 167 false,
168 "", 168 "",
169 dummy_file_stream, 169 dummy_file_stream,
170 net::BoundNetLog()); 170 net::BoundNetLog());
171 171
172 EXPECT_EQ(net::OK, file.Initialize()); 172 EXPECT_EQ(net::OK, file.Initialize(temp_dir_.path()));
173 file_name = file.full_path(); 173 file_name = file.full_path();
174 EXPECT_NE(FilePath::StringType(), file_name.value()); 174 EXPECT_NE(FilePath::StringType(), file_name.value());
175 175
176 EXPECT_EQ(net::OK, file.AppendDataToFile(kTestData4, kTestDataLength4)); 176 EXPECT_EQ(net::OK, file.AppendDataToFile(kTestData4, kTestDataLength4));
177 177
178 // 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.
179 file.Detach(); 179 file.Detach();
180 180
181 return file_name; 181 return file_name;
182 } 182 }
183 183
184 // Create a file with the specified file name. 184 // Create a file with the specified file name.
185 static void CreateFileWithName(const FilePath& file_name) { 185 void CreateFileWithName(const FilePath& file_name) {
186 EXPECT_NE(FilePath::StringType(), file_name.value()); 186 EXPECT_NE(FilePath::StringType(), file_name.value());
187 linked_ptr<net::FileStream> dummy_file_stream; 187 linked_ptr<net::FileStream> dummy_file_stream;
188 BaseFile duplicate_file(file_name, 188 BaseFile duplicate_file(file_name,
189 GURL(), 189 GURL(),
190 GURL(), 190 GURL(),
191 0, 191 0,
192 false, 192 false,
193 "", 193 "",
194 dummy_file_stream, 194 dummy_file_stream,
195 net::BoundNetLog()); 195 net::BoundNetLog());
196 EXPECT_EQ(net::OK, duplicate_file.Initialize()); 196 EXPECT_EQ(net::OK, duplicate_file.Initialize(temp_dir_.path()));
197 // Write something into it. 197 // Write something into it.
198 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); 198 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4);
199 // 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|.
200 duplicate_file.Detach(); 200 duplicate_file.Detach();
201 } 201 }
202 202
203 int64 CurrentSpeedAtTime(base::TimeTicks current_time) { 203 int64 CurrentSpeedAtTime(base::TimeTicks current_time) {
204 EXPECT_TRUE(base_file_.get()); 204 EXPECT_TRUE(base_file_.get());
205 return base_file_->CurrentSpeedAtTime(current_time); 205 return base_file_->CurrentSpeedAtTime(current_time);
206 } 206 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 246
247 // Test the most basic scenario: just create the object and do a sanity check 247 // Test the most basic scenario: just create the object and do a sanity check
248 // on all its accessors. This is actually a case that rarely happens 248 // on all its accessors. This is actually a case that rarely happens
249 // in production, where we would at least Initialize it. 249 // in production, where we would at least Initialize it.
250 TEST_F(BaseFileTest, CreateDestroy) { 250 TEST_F(BaseFileTest, CreateDestroy) {
251 EXPECT_EQ(FilePath().value(), base_file_->full_path().value()); 251 EXPECT_EQ(FilePath().value(), base_file_->full_path().value());
252 } 252 }
253 253
254 // Cancel the download explicitly. 254 // Cancel the download explicitly.
255 TEST_F(BaseFileTest, Cancel) { 255 TEST_F(BaseFileTest, Cancel) {
256 ASSERT_EQ(net::OK, base_file_->Initialize()); 256 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path()));
257 EXPECT_TRUE(file_util::PathExists(base_file_->full_path())); 257 EXPECT_TRUE(file_util::PathExists(base_file_->full_path()));
258 base_file_->Cancel(); 258 base_file_->Cancel();
259 EXPECT_FALSE(file_util::PathExists(base_file_->full_path())); 259 EXPECT_FALSE(file_util::PathExists(base_file_->full_path()));
260 EXPECT_NE(FilePath().value(), base_file_->full_path().value()); 260 EXPECT_NE(FilePath().value(), base_file_->full_path().value());
261 } 261 }
262 262
263 // Write data to the file and detach it, so it doesn't get deleted 263 // Write data to the file and detach it, so it doesn't get deleted
264 // automatically when base_file_ is destructed. 264 // automatically when base_file_ is destructed.
265 TEST_F(BaseFileTest, WriteAndDetach) { 265 TEST_F(BaseFileTest, WriteAndDetach) {
266 ASSERT_EQ(net::OK, base_file_->Initialize()); 266 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path()));
267 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 267 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
268 base_file_->Finish(); 268 base_file_->Finish();
269 base_file_->Detach(); 269 base_file_->Detach();
270 expect_file_survives_ = true; 270 expect_file_survives_ = true;
271 } 271 }
272 272
273 // Write data to the file and detach it, and calculate its sha256 hash. 273 // Write data to the file and detach it, and calculate its sha256 hash.
274 TEST_F(BaseFileTest, WriteWithHashAndDetach) { 274 TEST_F(BaseFileTest, WriteWithHashAndDetach) {
275 // Calculate the final hash. 275 // Calculate the final hash.
276 ResetHash(); 276 ResetHash();
277 UpdateHash(kTestData1, kTestDataLength1); 277 UpdateHash(kTestData1, kTestDataLength1);
278 std::string expected_hash = GetFinalHash(); 278 std::string expected_hash = GetFinalHash();
279 std::string expected_hash_hex = 279 std::string expected_hash_hex =
280 base::HexEncode(expected_hash.data(), expected_hash.size()); 280 base::HexEncode(expected_hash.data(), expected_hash.size());
281 281
282 MakeFileWithHash(); 282 MakeFileWithHash();
283 ASSERT_EQ(net::OK, base_file_->Initialize()); 283 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path()));
284 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 284 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
285 base_file_->Finish(); 285 base_file_->Finish();
286 286
287 std::string hash; 287 std::string hash;
288 base_file_->GetHash(&hash); 288 base_file_->GetHash(&hash);
289 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", 289 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE",
290 expected_hash_hex); 290 expected_hash_hex);
291 EXPECT_EQ(expected_hash_hex, base::HexEncode(hash.data(), hash.size())); 291 EXPECT_EQ(expected_hash_hex, base::HexEncode(hash.data(), hash.size()));
292 292
293 base_file_->Detach(); 293 base_file_->Detach();
294 expect_file_survives_ = true; 294 expect_file_survives_ = true;
295 } 295 }
296 296
297 // Rename the file after writing to it, then detach. 297 // Rename the file after writing to it, then detach.
298 TEST_F(BaseFileTest, WriteThenRenameAndDetach) { 298 TEST_F(BaseFileTest, WriteThenRenameAndDetach) {
299 ASSERT_EQ(net::OK, base_file_->Initialize()); 299 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path()));
300 300
301 FilePath initial_path(base_file_->full_path()); 301 FilePath initial_path(base_file_->full_path());
302 EXPECT_TRUE(file_util::PathExists(initial_path)); 302 EXPECT_TRUE(file_util::PathExists(initial_path));
303 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); 303 FilePath new_path(temp_dir_.path().AppendASCII("NewFile"));
304 EXPECT_FALSE(file_util::PathExists(new_path)); 304 EXPECT_FALSE(file_util::PathExists(new_path));
305 305
306 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 306 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
307 307
308 EXPECT_EQ(net::OK, base_file_->Rename(new_path)); 308 EXPECT_EQ(net::OK, base_file_->Rename(new_path));
309 EXPECT_FALSE(file_util::PathExists(initial_path)); 309 EXPECT_FALSE(file_util::PathExists(initial_path));
310 EXPECT_TRUE(file_util::PathExists(new_path)); 310 EXPECT_TRUE(file_util::PathExists(new_path));
311 311
312 base_file_->Finish(); 312 base_file_->Finish();
313 base_file_->Detach(); 313 base_file_->Detach();
314 expect_file_survives_ = true; 314 expect_file_survives_ = true;
315 } 315 }
316 316
317 // Write data to the file once. 317 // Write data to the file once.
318 TEST_F(BaseFileTest, SingleWrite) { 318 TEST_F(BaseFileTest, SingleWrite) {
319 ASSERT_EQ(net::OK, base_file_->Initialize()); 319 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path()));
320 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 320 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
321 base_file_->Finish(); 321 base_file_->Finish();
322 } 322 }
323 323
324 // Write data to the file multiple times. 324 // Write data to the file multiple times.
325 TEST_F(BaseFileTest, MultipleWrites) { 325 TEST_F(BaseFileTest, MultipleWrites) {
326 ASSERT_EQ(net::OK, base_file_->Initialize()); 326 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path()));
327 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 327 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
328 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); 328 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2));
329 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); 329 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3));
330 std::string hash; 330 std::string hash;
331 EXPECT_FALSE(base_file_->GetHash(&hash)); 331 EXPECT_FALSE(base_file_->GetHash(&hash));
332 base_file_->Finish(); 332 base_file_->Finish();
333 } 333 }
334 334
335 // Write data to the file once and calculate its sha256 hash. 335 // Write data to the file once and calculate its sha256 hash.
336 TEST_F(BaseFileTest, SingleWriteWithHash) { 336 TEST_F(BaseFileTest, SingleWriteWithHash) {
337 // Calculate the final hash. 337 // Calculate the final hash.
338 ResetHash(); 338 ResetHash();
339 UpdateHash(kTestData1, kTestDataLength1); 339 UpdateHash(kTestData1, kTestDataLength1);
340 std::string expected_hash = GetFinalHash(); 340 std::string expected_hash = GetFinalHash();
341 std::string expected_hash_hex = 341 std::string expected_hash_hex =
342 base::HexEncode(expected_hash.data(), expected_hash.size()); 342 base::HexEncode(expected_hash.data(), expected_hash.size());
343 343
344 MakeFileWithHash(); 344 MakeFileWithHash();
345 ASSERT_EQ(net::OK, base_file_->Initialize()); 345 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path()));
346 // Can get partial hash states before Finish() is called. 346 // Can get partial hash states before Finish() is called.
347 EXPECT_STRNE(std::string().c_str(), base_file_->GetHashState().c_str()); 347 EXPECT_STRNE(std::string().c_str(), base_file_->GetHashState().c_str());
348 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 348 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
349 EXPECT_STRNE(std::string().c_str(), base_file_->GetHashState().c_str()); 349 EXPECT_STRNE(std::string().c_str(), base_file_->GetHashState().c_str());
350 base_file_->Finish(); 350 base_file_->Finish();
351 351
352 std::string hash; 352 std::string hash;
353 base_file_->GetHash(&hash); 353 base_file_->GetHash(&hash);
354 EXPECT_EQ(expected_hash_hex, base::HexEncode(hash.data(), hash.size())); 354 EXPECT_EQ(expected_hash_hex, base::HexEncode(hash.data(), hash.size()));
355 } 355 }
356 356
357 // Write data to the file multiple times and calculate its sha256 hash. 357 // Write data to the file multiple times and calculate its sha256 hash.
358 TEST_F(BaseFileTest, MultipleWritesWithHash) { 358 TEST_F(BaseFileTest, MultipleWritesWithHash) {
359 // Calculate the final hash. 359 // Calculate the final hash.
360 ResetHash(); 360 ResetHash();
361 UpdateHash(kTestData1, kTestDataLength1); 361 UpdateHash(kTestData1, kTestDataLength1);
362 UpdateHash(kTestData2, kTestDataLength2); 362 UpdateHash(kTestData2, kTestDataLength2);
363 UpdateHash(kTestData3, kTestDataLength3); 363 UpdateHash(kTestData3, kTestDataLength3);
364 std::string expected_hash = GetFinalHash(); 364 std::string expected_hash = GetFinalHash();
365 std::string expected_hash_hex = 365 std::string expected_hash_hex =
366 base::HexEncode(expected_hash.data(), expected_hash.size()); 366 base::HexEncode(expected_hash.data(), expected_hash.size());
367 367
368 std::string hash; 368 std::string hash;
369 MakeFileWithHash(); 369 MakeFileWithHash();
370 ASSERT_EQ(net::OK, base_file_->Initialize()); 370 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path()));
371 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 371 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
372 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); 372 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2));
373 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); 373 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3));
374 // No hash before Finish() is called. 374 // No hash before Finish() is called.
375 EXPECT_FALSE(base_file_->GetHash(&hash)); 375 EXPECT_FALSE(base_file_->GetHash(&hash));
376 base_file_->Finish(); 376 base_file_->Finish();
377 377
378 EXPECT_TRUE(base_file_->GetHash(&hash)); 378 EXPECT_TRUE(base_file_->GetHash(&hash));
379 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8", 379 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8",
380 expected_hash_hex); 380 expected_hash_hex);
381 EXPECT_EQ(expected_hash_hex, base::HexEncode(hash.data(), hash.size())); 381 EXPECT_EQ(expected_hash_hex, base::HexEncode(hash.data(), hash.size()));
382 } 382 }
383 383
384 // Write data to the file multiple times, interrupt it, and continue using 384 // Write data to the file multiple times, interrupt it, and continue using
385 // another file. Calculate the resulting combined sha256 hash. 385 // another file. Calculate the resulting combined sha256 hash.
386 TEST_F(BaseFileTest, MultipleWritesInterruptedWithHash) { 386 TEST_F(BaseFileTest, MultipleWritesInterruptedWithHash) {
387 // Calculate the final hash. 387 // Calculate the final hash.
388 ResetHash(); 388 ResetHash();
389 UpdateHash(kTestData1, kTestDataLength1); 389 UpdateHash(kTestData1, kTestDataLength1);
390 UpdateHash(kTestData2, kTestDataLength2); 390 UpdateHash(kTestData2, kTestDataLength2);
391 UpdateHash(kTestData3, kTestDataLength3); 391 UpdateHash(kTestData3, kTestDataLength3);
392 std::string expected_hash = GetFinalHash(); 392 std::string expected_hash = GetFinalHash();
393 std::string expected_hash_hex = 393 std::string expected_hash_hex =
394 base::HexEncode(expected_hash.data(), expected_hash.size()); 394 base::HexEncode(expected_hash.data(), expected_hash.size());
395 395
396 MakeFileWithHash(); 396 MakeFileWithHash();
397 ASSERT_EQ(net::OK, base_file_->Initialize()); 397 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path()));
398 // Write some data 398 // Write some data
399 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 399 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
400 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); 400 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2));
401 // Get the hash state and file name. 401 // Get the hash state and file name.
402 std::string hash_state; 402 std::string hash_state;
403 hash_state = base_file_->GetHashState(); 403 hash_state = base_file_->GetHashState();
404 // Finish the file. 404 // Finish the file.
405 base_file_->Finish(); 405 base_file_->Finish();
406 406
407 // Create another file 407 // Create another file
408 linked_ptr<net::FileStream> second_stream; 408 linked_ptr<net::FileStream> second_stream;
409 BaseFile second_file(FilePath(), 409 BaseFile second_file(FilePath(),
410 GURL(), 410 GURL(),
411 GURL(), 411 GURL(),
412 base_file_->bytes_so_far(), 412 base_file_->bytes_so_far(),
413 true, 413 true,
414 hash_state, 414 hash_state,
415 second_stream, 415 second_stream,
416 net::BoundNetLog()); 416 net::BoundNetLog());
417 ASSERT_EQ(net::OK, second_file.Initialize()); 417 ASSERT_EQ(net::OK, second_file.Initialize(temp_dir_.path()));
418 std::string data(kTestData3); 418 std::string data(kTestData3);
419 EXPECT_EQ(net::OK, second_file.AppendDataToFile(data.data(), data.size())); 419 EXPECT_EQ(net::OK, second_file.AppendDataToFile(data.data(), data.size()));
420 second_file.Finish(); 420 second_file.Finish();
421 421
422 std::string hash; 422 std::string hash;
423 EXPECT_TRUE(second_file.GetHash(&hash)); 423 EXPECT_TRUE(second_file.GetHash(&hash));
424 // 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.
425 EXPECT_STREQ(expected_hash_hex.c_str(), 425 EXPECT_STREQ(expected_hash_hex.c_str(),
426 base::HexEncode(hash.data(), hash.size()).c_str()); 426 base::HexEncode(hash.data(), hash.size()).c_str());
427 } 427 }
428 428
429 // Rename the file after all writes to it. 429 // Rename the file after all writes to it.
430 TEST_F(BaseFileTest, WriteThenRename) { 430 TEST_F(BaseFileTest, WriteThenRename) {
431 ASSERT_EQ(net::OK, base_file_->Initialize()); 431 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path()));
432 432
433 FilePath initial_path(base_file_->full_path()); 433 FilePath initial_path(base_file_->full_path());
434 EXPECT_TRUE(file_util::PathExists(initial_path)); 434 EXPECT_TRUE(file_util::PathExists(initial_path));
435 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); 435 FilePath new_path(temp_dir_.path().AppendASCII("NewFile"));
436 EXPECT_FALSE(file_util::PathExists(new_path)); 436 EXPECT_FALSE(file_util::PathExists(new_path));
437 437
438 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 438 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
439 439
440 EXPECT_EQ(net::OK, base_file_->Rename(new_path)); 440 EXPECT_EQ(net::OK, base_file_->Rename(new_path));
441 EXPECT_FALSE(file_util::PathExists(initial_path)); 441 EXPECT_FALSE(file_util::PathExists(initial_path));
442 EXPECT_TRUE(file_util::PathExists(new_path)); 442 EXPECT_TRUE(file_util::PathExists(new_path));
443 443
444 base_file_->Finish(); 444 base_file_->Finish();
445 } 445 }
446 446
447 // Rename the file while the download is still in progress. 447 // Rename the file while the download is still in progress.
448 TEST_F(BaseFileTest, RenameWhileInProgress) { 448 TEST_F(BaseFileTest, RenameWhileInProgress) {
449 ASSERT_EQ(net::OK, base_file_->Initialize()); 449 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path()));
450 450
451 FilePath initial_path(base_file_->full_path()); 451 FilePath initial_path(base_file_->full_path());
452 EXPECT_TRUE(file_util::PathExists(initial_path)); 452 EXPECT_TRUE(file_util::PathExists(initial_path));
453 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); 453 FilePath new_path(temp_dir_.path().AppendASCII("NewFile"));
454 EXPECT_FALSE(file_util::PathExists(new_path)); 454 EXPECT_FALSE(file_util::PathExists(new_path));
455 455
456 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 456 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
457 457
458 EXPECT_TRUE(base_file_->in_progress()); 458 EXPECT_TRUE(base_file_->in_progress());
459 EXPECT_EQ(net::OK, base_file_->Rename(new_path)); 459 EXPECT_EQ(net::OK, base_file_->Rename(new_path));
460 EXPECT_FALSE(file_util::PathExists(initial_path)); 460 EXPECT_FALSE(file_util::PathExists(initial_path));
461 EXPECT_TRUE(file_util::PathExists(new_path)); 461 EXPECT_TRUE(file_util::PathExists(new_path));
462 462
463 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); 463 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2));
464 464
465 base_file_->Finish(); 465 base_file_->Finish();
466 } 466 }
467 467
468 // Test that a failed rename reports the correct error. 468 // Test that a failed rename reports the correct error.
469 TEST_F(BaseFileTest, RenameWithError) { 469 TEST_F(BaseFileTest, RenameWithError) {
470 ASSERT_EQ(net::OK, base_file_->Initialize()); 470 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path()));
471 471
472 // TestDir is a subdirectory in |temp_dir_| that we will make read-only so 472 // TestDir is a subdirectory in |temp_dir_| that we will make read-only so
473 // that the rename will fail. 473 // that the rename will fail.
474 FilePath test_dir(temp_dir_.path().AppendASCII("TestDir")); 474 FilePath test_dir(temp_dir_.path().AppendASCII("TestDir"));
475 ASSERT_TRUE(file_util::CreateDirectory(test_dir)); 475 ASSERT_TRUE(file_util::CreateDirectory(test_dir));
476 476
477 FilePath new_path(test_dir.AppendASCII("TestFile")); 477 FilePath new_path(test_dir.AppendASCII("TestFile"));
478 EXPECT_FALSE(file_util::PathExists(new_path)); 478 EXPECT_FALSE(file_util::PathExists(new_path));
479 479
480 { 480 {
481 file_util::PermissionRestorer restore_permissions_for(test_dir); 481 file_util::PermissionRestorer restore_permissions_for(test_dir);
482 ASSERT_TRUE(file_util::MakeFileUnwritable(test_dir)); 482 ASSERT_TRUE(file_util::MakeFileUnwritable(test_dir));
483 EXPECT_EQ(net::ERR_ACCESS_DENIED, base_file_->Rename(new_path)); 483 EXPECT_EQ(net::ERR_ACCESS_DENIED, base_file_->Rename(new_path));
484 } 484 }
485 485
486 base_file_->Finish(); 486 base_file_->Finish();
487 } 487 }
488 488
489 // Write data to the file multiple times. 489 // Write data to the file multiple times.
490 TEST_F(BaseFileTest, MultipleWritesWithError) { 490 TEST_F(BaseFileTest, MultipleWritesWithError) {
491 ASSERT_TRUE(OpenMockFileStream()); 491 ASSERT_TRUE(OpenMockFileStream());
492 base_file_.reset(new BaseFile(mock_file_stream_->get_path(), 492 base_file_.reset(new BaseFile(mock_file_stream_->get_path(),
493 GURL(), 493 GURL(),
494 GURL(), 494 GURL(),
495 0, 495 0,
496 false, 496 false,
497 "", 497 "",
498 mock_file_stream_, 498 mock_file_stream_,
499 net::BoundNetLog())); 499 net::BoundNetLog()));
500 EXPECT_EQ(net::OK, base_file_->Initialize()); 500 EXPECT_EQ(net::OK, base_file_->Initialize(temp_dir_.path()));
501 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 501 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
502 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); 502 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2));
503 ForceError(net::ERR_ACCESS_DENIED); 503 ForceError(net::ERR_ACCESS_DENIED);
504 ASSERT_NE(net::OK, AppendDataToFile(kTestData3)); 504 ASSERT_NE(net::OK, AppendDataToFile(kTestData3));
505 std::string hash; 505 std::string hash;
506 EXPECT_FALSE(base_file_->GetHash(&hash)); 506 EXPECT_FALSE(base_file_->GetHash(&hash));
507 base_file_->Finish(); 507 base_file_->Finish();
508 } 508 }
509 509
510 // Try to write to uninitialized file. 510 // Try to write to uninitialized file.
511 TEST_F(BaseFileTest, UninitializedFile) { 511 TEST_F(BaseFileTest, UninitializedFile) {
512 expect_in_progress_ = false; 512 expect_in_progress_ = false;
513 EXPECT_EQ(net::ERR_INVALID_HANDLE, AppendDataToFile(kTestData1)); 513 EXPECT_EQ(net::ERR_INVALID_HANDLE, AppendDataToFile(kTestData1));
514 } 514 }
515 515
516 // Create two |BaseFile|s with the same file, and attempt to write to both. 516 // Create two |BaseFile|s with the same file, and attempt to write to both.
517 // Overwrite base_file_ with another file with the same name and 517 // Overwrite base_file_ with another file with the same name and
518 // non-zero contents, and make sure the last file to close 'wins'. 518 // non-zero contents, and make sure the last file to close 'wins'.
519 TEST_F(BaseFileTest, DuplicateBaseFile) { 519 TEST_F(BaseFileTest, DuplicateBaseFile) {
520 EXPECT_EQ(net::OK, base_file_->Initialize()); 520 EXPECT_EQ(net::OK, base_file_->Initialize(temp_dir_.path()));
521 521
522 // Create another |BaseFile| referring to the file that |base_file_| owns. 522 // Create another |BaseFile| referring to the file that |base_file_| owns.
523 CreateFileWithName(base_file_->full_path()); 523 CreateFileWithName(base_file_->full_path());
524 524
525 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 525 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
526 base_file_->Finish(); 526 base_file_->Finish();
527 } 527 }
528 528
529 // Create a file and append to it. 529 // Create a file and append to it.
530 TEST_F(BaseFileTest, AppendToBaseFile) { 530 TEST_F(BaseFileTest, AppendToBaseFile) {
531 // Create a new file. 531 // Create a new file.
532 FilePath existing_file_name = CreateTestFile(); 532 FilePath existing_file_name = CreateTestFile();
533 533
534 set_expected_data(kTestData4); 534 set_expected_data(kTestData4);
535 535
536 // Use the file we've just created. 536 // Use the file we've just created.
537 base_file_.reset(new BaseFile(existing_file_name, 537 base_file_.reset(new BaseFile(existing_file_name,
538 GURL(), 538 GURL(),
539 GURL(), 539 GURL(),
540 kTestDataLength4, 540 kTestDataLength4,
541 false, 541 false,
542 "", 542 "",
543 file_stream_, 543 file_stream_,
544 net::BoundNetLog())); 544 net::BoundNetLog()));
545 545
546 EXPECT_EQ(net::OK, base_file_->Initialize()); 546 EXPECT_EQ(net::OK, base_file_->Initialize(temp_dir_.path()));
547 547
548 const FilePath file_name = base_file_->full_path(); 548 const FilePath file_name = base_file_->full_path();
549 EXPECT_NE(FilePath::StringType(), file_name.value()); 549 EXPECT_NE(FilePath::StringType(), file_name.value());
550 550
551 // Write into the file. 551 // Write into the file.
552 EXPECT_EQ(net::OK, AppendDataToFile(kTestData1)); 552 EXPECT_EQ(net::OK, AppendDataToFile(kTestData1));
553 553
554 base_file_->Finish(); 554 base_file_->Finish();
555 base_file_->Detach(); 555 base_file_->Detach();
556 expect_file_survives_ = true; 556 expect_file_survives_ = true;
(...skipping 15 matching lines...) Expand all
572 GURL(), 572 GURL(),
573 GURL(), 573 GURL(),
574 0, 574 0,
575 false, 575 false,
576 "", 576 "",
577 file_stream_, 577 file_stream_,
578 net::BoundNetLog())); 578 net::BoundNetLog()));
579 579
580 expect_in_progress_ = false; 580 expect_in_progress_ = false;
581 581
582 int init_error = base_file_->Initialize(); 582 int init_error = base_file_->Initialize(temp_dir_.path());
583 DVLOG(1) << " init_error = " << init_error; 583 DVLOG(1) << " init_error = " << init_error;
584 EXPECT_NE(net::OK, init_error); 584 EXPECT_NE(net::OK, init_error);
585 585
586 const FilePath file_name = base_file_->full_path(); 586 const FilePath file_name = base_file_->full_path();
587 EXPECT_NE(FilePath::StringType(), file_name.value()); 587 EXPECT_NE(FilePath::StringType(), file_name.value());
588 588
589 // Write into the file. 589 // Write into the file.
590 EXPECT_NE(net::OK, AppendDataToFile(kTestData1)); 590 EXPECT_NE(net::OK, AppendDataToFile(kTestData1));
591 591
592 base_file_->Finish(); 592 base_file_->Finish();
593 base_file_->Detach(); 593 base_file_->Detach();
594 expect_file_survives_ = true; 594 expect_file_survives_ = true;
595 } 595 }
596 596
597 TEST_F(BaseFileTest, IsEmptyHash) { 597 TEST_F(BaseFileTest, IsEmptyHash) {
598 std::string empty(BaseFile::kSha256HashLen, '\x00'); 598 std::string empty(BaseFile::kSha256HashLen, '\x00');
599 EXPECT_TRUE(BaseFile::IsEmptyHash(empty)); 599 EXPECT_TRUE(BaseFile::IsEmptyHash(empty));
600 std::string not_empty(BaseFile::kSha256HashLen, '\x01'); 600 std::string not_empty(BaseFile::kSha256HashLen, '\x01');
601 EXPECT_FALSE(BaseFile::IsEmptyHash(not_empty)); 601 EXPECT_FALSE(BaseFile::IsEmptyHash(not_empty));
602 EXPECT_FALSE(BaseFile::IsEmptyHash("")); 602 EXPECT_FALSE(BaseFile::IsEmptyHash(""));
603 } 603 }
604 604
605 // Test that calculating speed after no writes. 605 // Test that calculating speed after no writes.
606 TEST_F(BaseFileTest, SpeedWithoutWrite) { 606 TEST_F(BaseFileTest, SpeedWithoutWrite) {
607 ASSERT_EQ(net::OK, base_file_->Initialize()); 607 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path()));
608 base::TimeTicks current = StartTick() + kElapsedTimeDelta; 608 base::TimeTicks current = StartTick() + kElapsedTimeDelta;
609 ASSERT_EQ(0, CurrentSpeedAtTime(current)); 609 ASSERT_EQ(0, CurrentSpeedAtTime(current));
610 base_file_->Finish(); 610 base_file_->Finish();
611 } 611 }
612 612
613 // Test that calculating speed after a single write. 613 // Test that calculating speed after a single write.
614 TEST_F(BaseFileTest, SpeedAfterSingleWrite) { 614 TEST_F(BaseFileTest, SpeedAfterSingleWrite) {
615 ASSERT_EQ(net::OK, base_file_->Initialize()); 615 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path()));
616 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 616 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
617 base::TimeTicks current = StartTick() + kElapsedTimeDelta; 617 base::TimeTicks current = StartTick() + kElapsedTimeDelta;
618 int64 expected_speed = kTestDataLength1 / kElapsedTimeSeconds; 618 int64 expected_speed = kTestDataLength1 / kElapsedTimeSeconds;
619 ASSERT_EQ(expected_speed, CurrentSpeedAtTime(current)); 619 ASSERT_EQ(expected_speed, CurrentSpeedAtTime(current));
620 base_file_->Finish(); 620 base_file_->Finish();
621 } 621 }
622 622
623 // Test that calculating speed after a multiple writes. 623 // Test that calculating speed after a multiple writes.
624 TEST_F(BaseFileTest, SpeedAfterMultipleWrite) { 624 TEST_F(BaseFileTest, SpeedAfterMultipleWrite) {
625 ASSERT_EQ(net::OK, base_file_->Initialize()); 625 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path()));
626 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 626 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
627 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2)); 627 ASSERT_EQ(net::OK, AppendDataToFile(kTestData2));
628 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3)); 628 ASSERT_EQ(net::OK, AppendDataToFile(kTestData3));
629 ASSERT_EQ(net::OK, AppendDataToFile(kTestData4)); 629 ASSERT_EQ(net::OK, AppendDataToFile(kTestData4));
630 base::TimeTicks current = StartTick() + kElapsedTimeDelta; 630 base::TimeTicks current = StartTick() + kElapsedTimeDelta;
631 int64 expected_speed = (kTestDataLength1 + kTestDataLength2 + 631 int64 expected_speed = (kTestDataLength1 + kTestDataLength2 +
632 kTestDataLength3 + kTestDataLength4) / kElapsedTimeSeconds; 632 kTestDataLength3 + kTestDataLength4) / kElapsedTimeSeconds;
633 ASSERT_EQ(expected_speed, CurrentSpeedAtTime(current)); 633 ASSERT_EQ(expected_speed, CurrentSpeedAtTime(current));
634 base_file_->Finish(); 634 base_file_->Finish();
635 } 635 }
636 636
637 // Test that calculating speed after no delay - should not divide by 0. 637 // Test that calculating speed after no delay - should not divide by 0.
638 TEST_F(BaseFileTest, SpeedAfterNoElapsedTime) { 638 TEST_F(BaseFileTest, SpeedAfterNoElapsedTime) {
639 ASSERT_EQ(net::OK, base_file_->Initialize()); 639 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path()));
640 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1)); 640 ASSERT_EQ(net::OK, AppendDataToFile(kTestData1));
641 ASSERT_EQ(0, CurrentSpeedAtTime(StartTick())); 641 ASSERT_EQ(0, CurrentSpeedAtTime(StartTick()));
642 base_file_->Finish(); 642 base_file_->Finish();
643 } 643 }
644
645 // Test that a temporary file is created in the default download directory.
646 TEST_F(BaseFileTest, CreatedInDefaultDirectory) {
647 ASSERT_TRUE(base_file_->full_path().empty());
648 ASSERT_EQ(net::OK, base_file_->Initialize(temp_dir_.path()));
649 EXPECT_FALSE(base_file_->full_path().empty());
650
651 // On Windows, CreateTemporaryFileInDir() will cause a path with short names
652 // to be expanded into a path with long names. Thus temp_dir.path() might not
653 // be a string-wise match to base_file_->full_path().DirName() even though
654 // they are in the same directory.
655 FilePath temp_file;
656 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(),
657 &temp_file));
658 ASSERT_FALSE(temp_file.empty());
659 EXPECT_STREQ(temp_file.DirName().value().c_str(),
660 base_file_->full_path().DirName().value().c_str());
661 base_file_->Finish();
662 }
OLDNEW
« no previous file with comments | « content/browser/download/base_file.cc ('k') | content/browser/download/download_create_info.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698