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

Side by Side Diff: net/disk_cache/entry_unittest.cc

Issue 23983005: SimpleCache: merge the first and second stream in one file (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed gavin's comments Created 7 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
« no previous file with comments | « net/disk_cache/backend_unittest.cc ('k') | net/disk_cache/simple/simple_entry_format.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 "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/bind.h" 6 #include "base/bind.h"
7 #include "base/bind_helpers.h" 7 #include "base/bind_helpers.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
11 #include "base/threading/platform_thread.h" 11 #include "base/threading/platform_thread.h"
12 #include "base/timer/timer.h" 12 #include "base/timer/timer.h"
13 #include "net/base/completion_callback.h" 13 #include "net/base/completion_callback.h"
14 #include "net/base/io_buffer.h" 14 #include "net/base/io_buffer.h"
15 #include "net/base/net_errors.h" 15 #include "net/base/net_errors.h"
16 #include "net/base/test_completion_callback.h" 16 #include "net/base/test_completion_callback.h"
17 #include "net/disk_cache/backend_impl.h" 17 #include "net/disk_cache/backend_impl.h"
18 #include "net/disk_cache/disk_cache_test_base.h" 18 #include "net/disk_cache/disk_cache_test_base.h"
19 #include "net/disk_cache/disk_cache_test_util.h" 19 #include "net/disk_cache/disk_cache_test_util.h"
20 #include "net/disk_cache/entry_impl.h" 20 #include "net/disk_cache/entry_impl.h"
21 #include "net/disk_cache/mem_entry_impl.h" 21 #include "net/disk_cache/mem_entry_impl.h"
22 #include "net/disk_cache/simple/simple_entry_format.h" 22 #include "net/disk_cache/simple/simple_entry_format.h"
23 #include "net/disk_cache/simple/simple_entry_impl.h" 23 #include "net/disk_cache/simple/simple_entry_impl.h"
24 #include "net/disk_cache/simple/simple_synchronous_entry.h"
24 #include "net/disk_cache/simple/simple_test_util.h" 25 #include "net/disk_cache/simple/simple_test_util.h"
25 #include "net/disk_cache/simple/simple_util.h" 26 #include "net/disk_cache/simple/simple_util.h"
26 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
27 28
28 using base::Time; 29 using base::Time;
29 using disk_cache::ScopedEntryPtr; 30 using disk_cache::ScopedEntryPtr;
30 31
31 // Tests that can run with different types of caches. 32 // Tests that can run with different types of caches.
32 class DiskCacheEntryTest : public DiskCacheTestWithCache { 33 class DiskCacheEntryTest : public DiskCacheTestWithCache {
33 public: 34 public:
(...skipping 21 matching lines...) Expand all
55 void DoomNormalEntry(); 56 void DoomNormalEntry();
56 void DoomEntryNextToOpenEntry(); 57 void DoomEntryNextToOpenEntry();
57 void DoomedEntry(); 58 void DoomedEntry();
58 void BasicSparseIO(); 59 void BasicSparseIO();
59 void HugeSparseIO(); 60 void HugeSparseIO();
60 void GetAvailableRange(); 61 void GetAvailableRange();
61 void CouldBeSparse(); 62 void CouldBeSparse();
62 void UpdateSparseEntry(); 63 void UpdateSparseEntry();
63 void DoomSparseEntry(); 64 void DoomSparseEntry();
64 void PartialSparseEntry(); 65 void PartialSparseEntry();
65 bool SimpleCacheMakeBadChecksumEntry(const char* key, int* data_size); 66 bool SimpleCacheMakeBadChecksumEntry(const std::string& key, int* data_size);
66 }; 67 };
67 68
68 // This part of the test runs on the background thread. 69 // This part of the test runs on the background thread.
69 void DiskCacheEntryTest::InternalSyncIOBackground(disk_cache::Entry* entry) { 70 void DiskCacheEntryTest::InternalSyncIOBackground(disk_cache::Entry* entry) {
70 const int kSize1 = 10; 71 const int kSize1 = 10;
71 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1)); 72 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
72 CacheTestFillBuffer(buffer1->data(), kSize1, false); 73 CacheTestFillBuffer(buffer1->data(), kSize1, false);
73 EXPECT_EQ( 74 EXPECT_EQ(
74 0, 75 0,
75 entry->ReadData(0, 0, buffer1.get(), kSize1, net::CompletionCallback())); 76 entry->ReadData(0, 0, buffer1.get(), kSize1, net::CompletionCallback()));
(...skipping 2331 matching lines...) Expand 10 before | Expand all | Expand 10 after
2407 } 2408 }
2408 2409
2409 TEST_F(DiskCacheEntryTest, SimpleCacheDoomedEntry) { 2410 TEST_F(DiskCacheEntryTest, SimpleCacheDoomedEntry) {
2410 SetSimpleCacheMode(); 2411 SetSimpleCacheMode();
2411 InitCache(); 2412 InitCache();
2412 DoomedEntry(); 2413 DoomedEntry();
2413 } 2414 }
2414 2415
2415 // Creates an entry with corrupted last byte in stream 0. 2416 // Creates an entry with corrupted last byte in stream 0.
2416 // Requires SimpleCacheMode. 2417 // Requires SimpleCacheMode.
2417 bool DiskCacheEntryTest::SimpleCacheMakeBadChecksumEntry(const char* key, 2418 bool DiskCacheEntryTest::SimpleCacheMakeBadChecksumEntry(const std::string& key,
2418 int* data_size) { 2419 int* data_size) {
2419 disk_cache::Entry* entry = NULL; 2420 disk_cache::Entry* entry = NULL;
2420 2421
2421 if (CreateEntry(key, &entry) != net::OK || !entry) { 2422 if (CreateEntry(key, &entry) != net::OK || !entry) {
2422 LOG(ERROR) << "Could not create entry"; 2423 LOG(ERROR) << "Could not create entry";
2423 return false; 2424 return false;
2424 } 2425 }
2425 2426
2426 const char data[] = "this is very good data"; 2427 const char data[] = "this is very good data";
2427 const int kDataSize = arraysize(data); 2428 const int kDataSize = arraysize(data);
2428 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kDataSize)); 2429 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kDataSize));
2429 base::strlcpy(buffer->data(), data, kDataSize); 2430 base::strlcpy(buffer->data(), data, kDataSize);
2430 2431
2431 EXPECT_EQ(kDataSize, WriteData(entry, 0, 0, buffer.get(), kDataSize, false)); 2432 EXPECT_EQ(kDataSize, WriteData(entry, 1, 0, buffer.get(), kDataSize, false));
2432 entry->Close(); 2433 entry->Close();
2433 entry = NULL; 2434 entry = NULL;
2434 2435
2435 // Corrupt the last byte of the data. 2436 // Corrupt the last byte of the data.
2436 base::FilePath entry_file0_path = cache_path_.AppendASCII( 2437 base::FilePath entry_file0_path = cache_path_.AppendASCII(
2437 disk_cache::simple_util::GetFilenameFromKeyAndIndex(key, 0)); 2438 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0));
2438 int flags = base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_OPEN; 2439 int flags = base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_OPEN;
2439 base::PlatformFile entry_file0 = 2440 base::PlatformFile entry_file0 =
2440 base::CreatePlatformFile(entry_file0_path, flags, NULL, NULL); 2441 base::CreatePlatformFile(entry_file0_path, flags, NULL, NULL);
2441 if (entry_file0 == base::kInvalidPlatformFileValue) 2442 if (entry_file0 == base::kInvalidPlatformFileValue)
2442 return false; 2443 return false;
2444
2443 int64 file_offset = 2445 int64 file_offset =
2444 disk_cache::simple_util::GetFileOffsetFromKeyAndDataOffset( 2446 sizeof(disk_cache::SimpleFileHeader) + key.size() + kDataSize - 2;
2445 key, kDataSize - 2);
2446 EXPECT_EQ(1, base::WritePlatformFile(entry_file0, file_offset, "X", 1)); 2447 EXPECT_EQ(1, base::WritePlatformFile(entry_file0, file_offset, "X", 1));
2447 if (!base::ClosePlatformFile(entry_file0)) 2448 if (!base::ClosePlatformFile(entry_file0))
2448 return false; 2449 return false;
2449 *data_size = kDataSize; 2450 *data_size = kDataSize;
2450 return true; 2451 return true;
2451 } 2452 }
2452 2453
2453 // Tests that the simple cache can detect entries that have bad data. 2454 // Tests that the simple cache can detect entries that have bad data.
2454 TEST_F(DiskCacheEntryTest, SimpleCacheBadChecksum) { 2455 TEST_F(DiskCacheEntryTest, SimpleCacheBadChecksum) {
2455 SetSimpleCacheMode(); 2456 SetSimpleCacheMode();
2456 InitCache(); 2457 InitCache();
2457 2458
2458 const char key[] = "the first key"; 2459 const char key[] = "the first key";
2459 int size_unused; 2460 int size_unused;
2460 ASSERT_TRUE(SimpleCacheMakeBadChecksumEntry(key, &size_unused)); 2461 ASSERT_TRUE(SimpleCacheMakeBadChecksumEntry(key, &size_unused));
2461 2462
2462 disk_cache::Entry* entry = NULL; 2463 disk_cache::Entry* entry = NULL;
2463 2464
2464 // Open the entry. 2465 // Open the entry.
2465 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); 2466 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
2466 ScopedEntryPtr entry_closer(entry); 2467 ScopedEntryPtr entry_closer(entry);
2467 2468
2468 const int kReadBufferSize = 200; 2469 const int kReadBufferSize = 200;
2469 EXPECT_GE(kReadBufferSize, entry->GetDataSize(0)); 2470 EXPECT_GE(kReadBufferSize, entry->GetDataSize(1));
2470 scoped_refptr<net::IOBuffer> read_buffer(new net::IOBuffer(kReadBufferSize)); 2471 scoped_refptr<net::IOBuffer> read_buffer(new net::IOBuffer(kReadBufferSize));
2471 EXPECT_EQ(net::ERR_CACHE_CHECKSUM_MISMATCH, 2472 EXPECT_EQ(net::ERR_CACHE_CHECKSUM_MISMATCH,
2472 ReadData(entry, 0, 0, read_buffer.get(), kReadBufferSize)); 2473 ReadData(entry, 1, 0, read_buffer.get(), kReadBufferSize));
2473 } 2474 }
2474 2475
2475 // Tests that an entry that has had an IO error occur can still be Doomed(). 2476 // Tests that an entry that has had an IO error occur can still be Doomed().
2476 TEST_F(DiskCacheEntryTest, SimpleCacheErrorThenDoom) { 2477 TEST_F(DiskCacheEntryTest, SimpleCacheErrorThenDoom) {
2477 SetSimpleCacheMode(); 2478 SetSimpleCacheMode();
2478 InitCache(); 2479 InitCache();
2479 2480
2480 const char key[] = "the first key"; 2481 const char key[] = "the first key";
2481 int size_unused; 2482 int size_unused;
2482 ASSERT_TRUE(SimpleCacheMakeBadChecksumEntry(key, &size_unused)); 2483 ASSERT_TRUE(SimpleCacheMakeBadChecksumEntry(key, &size_unused));
2483 2484
2484 disk_cache::Entry* entry = NULL; 2485 disk_cache::Entry* entry = NULL;
2485 2486
2486 // Open the entry, forcing an IO error. 2487 // Open the entry, forcing an IO error.
2487 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); 2488 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
2488 ScopedEntryPtr entry_closer(entry); 2489 ScopedEntryPtr entry_closer(entry);
2489 2490
2490 const int kReadBufferSize = 200; 2491 const int kReadBufferSize = 200;
2491 EXPECT_GE(kReadBufferSize, entry->GetDataSize(0)); 2492 EXPECT_GE(kReadBufferSize, entry->GetDataSize(1));
2492 scoped_refptr<net::IOBuffer> read_buffer(new net::IOBuffer(kReadBufferSize)); 2493 scoped_refptr<net::IOBuffer> read_buffer(new net::IOBuffer(kReadBufferSize));
2493 EXPECT_EQ(net::ERR_CACHE_CHECKSUM_MISMATCH, 2494 EXPECT_EQ(net::ERR_CACHE_CHECKSUM_MISMATCH,
2494 ReadData(entry, 0, 0, read_buffer.get(), kReadBufferSize)); 2495 ReadData(entry, 1, 0, read_buffer.get(), kReadBufferSize));
2495 2496
2496 entry->Doom(); // Should not crash. 2497 entry->Doom(); // Should not crash.
2497 } 2498 }
2498 2499
2499 bool TruncatePath(const base::FilePath& file_path, int64 length) { 2500 bool TruncatePath(const base::FilePath& file_path, int64 length) {
2500 const int flags = base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_OPEN; 2501 const int flags = base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_OPEN;
2501 base::PlatformFile file = 2502 base::PlatformFile file =
2502 base::CreatePlatformFile(file_path, flags, NULL, NULL); 2503 base::CreatePlatformFile(file_path, flags, NULL, NULL);
2503 if (base::kInvalidPlatformFileValue == file) 2504 if (base::kInvalidPlatformFileValue == file)
2504 return false; 2505 return false;
(...skipping 18 matching lines...) Expand all
2523 // Force the entry to flush to disk, so subsequent platform file operations 2524 // Force the entry to flush to disk, so subsequent platform file operations
2524 // succed. 2525 // succed.
2525 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); 2526 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
2526 entry->Close(); 2527 entry->Close();
2527 entry = NULL; 2528 entry = NULL;
2528 2529
2529 // Truncate the file such that the length isn't sufficient to have an EOF 2530 // Truncate the file such that the length isn't sufficient to have an EOF
2530 // record. 2531 // record.
2531 int kTruncationBytes = -implicit_cast<int>(sizeof(disk_cache::SimpleFileEOF)); 2532 int kTruncationBytes = -implicit_cast<int>(sizeof(disk_cache::SimpleFileEOF));
2532 const base::FilePath entry_path = cache_path_.AppendASCII( 2533 const base::FilePath entry_path = cache_path_.AppendASCII(
2533 disk_cache::simple_util::GetFilenameFromKeyAndIndex(key, 0)); 2534 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0));
2534 const int64 invalid_size = 2535 const int64 invalid_size =
2535 disk_cache::simple_util::GetFileSizeFromKeyAndDataSize(key, 2536 disk_cache::simple_util::GetFileSizeFromKeyAndDataSize(key,
2536 kTruncationBytes); 2537 kTruncationBytes);
2537 EXPECT_TRUE(TruncatePath(entry_path, invalid_size)); 2538 EXPECT_TRUE(TruncatePath(entry_path, invalid_size));
2538 EXPECT_EQ(net::ERR_FAILED, OpenEntry(key, &entry)); 2539 EXPECT_EQ(net::ERR_FAILED, OpenEntry(key, &entry));
2539 DisableIntegrityCheck(); 2540 DisableIntegrityCheck();
2540 } 2541 }
2541 2542
2542 TEST_F(DiskCacheEntryTest, SimpleCacheNonOptimisticOperationsBasic) { 2543 TEST_F(DiskCacheEntryTest, SimpleCacheNonOptimisticOperationsBasic) {
2543 // Test sequence: 2544 // Test sequence:
2544 // Create, Write, Read, Close. 2545 // Create, Write, Read, Close.
2545 SetCacheType(net::APP_CACHE); // APP_CACHE doesn't use optimistic operations. 2546 SetCacheType(net::APP_CACHE); // APP_CACHE doesn't use optimistic operations.
2546 SetSimpleCacheMode(); 2547 SetSimpleCacheMode();
2547 InitCache(); 2548 InitCache();
2548 disk_cache::Entry* const null_entry = NULL; 2549 disk_cache::Entry* const null_entry = NULL;
2549 2550
2550 disk_cache::Entry* entry = NULL; 2551 disk_cache::Entry* entry = NULL;
2551 EXPECT_EQ(net::OK, CreateEntry("my key", &entry)); 2552 EXPECT_EQ(net::OK, CreateEntry("my key", &entry));
2552 ASSERT_NE(null_entry, entry); 2553 ASSERT_NE(null_entry, entry);
2553 ScopedEntryPtr entry_closer(entry); 2554 ScopedEntryPtr entry_closer(entry);
2554 2555
2555 const int kBufferSize = 10; 2556 const int kBufferSize = 10;
2556 scoped_refptr<net::IOBufferWithSize> write_buffer( 2557 scoped_refptr<net::IOBufferWithSize> write_buffer(
2557 new net::IOBufferWithSize(kBufferSize)); 2558 new net::IOBufferWithSize(kBufferSize));
2558 CacheTestFillBuffer(write_buffer->data(), write_buffer->size(), false); 2559 CacheTestFillBuffer(write_buffer->data(), write_buffer->size(), false);
2559 EXPECT_EQ( 2560 EXPECT_EQ(
2560 write_buffer->size(), 2561 write_buffer->size(),
2561 WriteData(entry, 0, 0, write_buffer.get(), write_buffer->size(), false)); 2562 WriteData(entry, 1, 0, write_buffer.get(), write_buffer->size(), false));
2562 2563
2563 scoped_refptr<net::IOBufferWithSize> read_buffer( 2564 scoped_refptr<net::IOBufferWithSize> read_buffer(
2564 new net::IOBufferWithSize(kBufferSize)); 2565 new net::IOBufferWithSize(kBufferSize));
2565 EXPECT_EQ( 2566 EXPECT_EQ(read_buffer->size(),
2566 read_buffer->size(), 2567 ReadData(entry, 1, 0, read_buffer.get(), read_buffer->size()));
2567 ReadData(entry, 0, 0, read_buffer.get(), read_buffer->size()));
2568 } 2568 }
2569 2569
2570 TEST_F(DiskCacheEntryTest, SimpleCacheNonOptimisticOperationsDontBlock) { 2570 TEST_F(DiskCacheEntryTest, SimpleCacheNonOptimisticOperationsDontBlock) {
2571 // Test sequence: 2571 // Test sequence:
2572 // Create, Write, Close. 2572 // Create, Write, Close.
2573 SetCacheType(net::APP_CACHE); // APP_CACHE doesn't use optimistic operations. 2573 SetCacheType(net::APP_CACHE); // APP_CACHE doesn't use optimistic operations.
2574 SetSimpleCacheMode(); 2574 SetSimpleCacheMode();
2575 InitCache(); 2575 InitCache();
2576 disk_cache::Entry* const null_entry = NULL; 2576 disk_cache::Entry* const null_entry = NULL;
2577 2577
2578 MessageLoopHelper helper; 2578 MessageLoopHelper helper;
2579 CallbackTest create_callback(&helper, false); 2579 CallbackTest create_callback(&helper, false);
2580 2580
2581 int expected_callback_runs = 0; 2581 int expected_callback_runs = 0;
2582 const int kBufferSize = 10; 2582 const int kBufferSize = 10;
2583 scoped_refptr<net::IOBufferWithSize> write_buffer( 2583 scoped_refptr<net::IOBufferWithSize> write_buffer(
2584 new net::IOBufferWithSize(kBufferSize)); 2584 new net::IOBufferWithSize(kBufferSize));
2585 2585
2586 disk_cache::Entry* entry = NULL; 2586 disk_cache::Entry* entry = NULL;
2587 EXPECT_EQ(net::OK, CreateEntry("my key", &entry)); 2587 EXPECT_EQ(net::OK, CreateEntry("my key", &entry));
2588 ASSERT_NE(null_entry, entry); 2588 ASSERT_NE(null_entry, entry);
2589 ScopedEntryPtr entry_closer(entry); 2589 ScopedEntryPtr entry_closer(entry);
2590 2590
2591 CacheTestFillBuffer(write_buffer->data(), write_buffer->size(), false); 2591 CacheTestFillBuffer(write_buffer->data(), write_buffer->size(), false);
2592 CallbackTest write_callback(&helper, false); 2592 CallbackTest write_callback(&helper, false);
2593 int ret = entry->WriteData( 2593 int ret = entry->WriteData(
2594 0, 2594 1,
2595 0, 2595 0,
2596 write_buffer.get(), 2596 write_buffer.get(),
2597 write_buffer->size(), 2597 write_buffer->size(),
2598 base::Bind(&CallbackTest::Run, base::Unretained(&write_callback)), 2598 base::Bind(&CallbackTest::Run, base::Unretained(&write_callback)),
2599 false); 2599 false);
2600 ASSERT_EQ(net::ERR_IO_PENDING, ret); 2600 ASSERT_EQ(net::ERR_IO_PENDING, ret);
2601 helper.WaitUntilCacheIoFinished(++expected_callback_runs); 2601 helper.WaitUntilCacheIoFinished(++expected_callback_runs);
2602 } 2602 }
2603 2603
2604 TEST_F(DiskCacheEntryTest, 2604 TEST_F(DiskCacheEntryTest,
(...skipping 12 matching lines...) Expand all
2617 EXPECT_EQ(net::OK, CreateEntry("my key", &entry)); 2617 EXPECT_EQ(net::OK, CreateEntry("my key", &entry));
2618 ASSERT_NE(null_entry, entry); 2618 ASSERT_NE(null_entry, entry);
2619 ScopedEntryPtr entry_closer(entry); 2619 ScopedEntryPtr entry_closer(entry);
2620 2620
2621 const int kBufferSize = 10; 2621 const int kBufferSize = 10;
2622 scoped_refptr<net::IOBufferWithSize> write_buffer( 2622 scoped_refptr<net::IOBufferWithSize> write_buffer(
2623 new net::IOBufferWithSize(kBufferSize)); 2623 new net::IOBufferWithSize(kBufferSize));
2624 CacheTestFillBuffer(write_buffer->data(), write_buffer->size(), false); 2624 CacheTestFillBuffer(write_buffer->data(), write_buffer->size(), false);
2625 CallbackTest write_callback(&helper, false); 2625 CallbackTest write_callback(&helper, false);
2626 int ret = entry->WriteData( 2626 int ret = entry->WriteData(
2627 0, 2627 1,
2628 0, 2628 0,
2629 write_buffer.get(), 2629 write_buffer.get(),
2630 write_buffer->size(), 2630 write_buffer->size(),
2631 base::Bind(&CallbackTest::Run, base::Unretained(&write_callback)), 2631 base::Bind(&CallbackTest::Run, base::Unretained(&write_callback)),
2632 false); 2632 false);
2633 EXPECT_EQ(net::ERR_IO_PENDING, ret); 2633 EXPECT_EQ(net::ERR_IO_PENDING, ret);
2634 int expected_callback_runs = 1; 2634 int expected_callback_runs = 1;
2635 2635
2636 scoped_refptr<net::IOBufferWithSize> read_buffer( 2636 scoped_refptr<net::IOBufferWithSize> read_buffer(
2637 new net::IOBufferWithSize(kBufferSize)); 2637 new net::IOBufferWithSize(kBufferSize));
2638 CallbackTest read_callback(&helper, false); 2638 CallbackTest read_callback(&helper, false);
2639 ret = entry->ReadData( 2639 ret = entry->ReadData(
2640 0, 2640 1,
2641 0, 2641 0,
2642 read_buffer.get(), 2642 read_buffer.get(),
2643 read_buffer->size(), 2643 read_buffer->size(),
2644 base::Bind(&CallbackTest::Run, base::Unretained(&read_callback))); 2644 base::Bind(&CallbackTest::Run, base::Unretained(&read_callback)));
2645 EXPECT_EQ(net::ERR_IO_PENDING, ret); 2645 EXPECT_EQ(net::ERR_IO_PENDING, ret);
2646 ++expected_callback_runs; 2646 ++expected_callback_runs;
2647 2647
2648 helper.WaitUntilCacheIoFinished(expected_callback_runs); 2648 helper.WaitUntilCacheIoFinished(expected_callback_runs);
2649 ASSERT_EQ(read_buffer->size(), write_buffer->size()); 2649 ASSERT_EQ(read_buffer->size(), write_buffer->size());
2650 EXPECT_EQ( 2650 EXPECT_EQ(
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
2682 ASSERT_EQ(net::OK, 2682 ASSERT_EQ(net::OK,
2683 cache_->CreateEntry(key, &entry, 2683 cache_->CreateEntry(key, &entry,
2684 base::Bind(&CallbackTest::Run, 2684 base::Bind(&CallbackTest::Run,
2685 base::Unretained(&callback1)))); 2685 base::Unretained(&callback1))));
2686 EXPECT_NE(null, entry); 2686 EXPECT_NE(null, entry);
2687 ScopedEntryPtr entry_closer(entry); 2687 ScopedEntryPtr entry_closer(entry);
2688 2688
2689 // This write may or may not be optimistic (it depends if the previous 2689 // This write may or may not be optimistic (it depends if the previous
2690 // optimistic create already finished by the time we call the write here). 2690 // optimistic create already finished by the time we call the write here).
2691 int ret = entry->WriteData( 2691 int ret = entry->WriteData(
2692 0, 2692 1,
2693 0, 2693 0,
2694 buffer1.get(), 2694 buffer1.get(),
2695 kSize1, 2695 kSize1,
2696 base::Bind(&CallbackTest::Run, base::Unretained(&callback2)), 2696 base::Bind(&CallbackTest::Run, base::Unretained(&callback2)),
2697 false); 2697 false);
2698 EXPECT_TRUE(kSize1 == ret || net::ERR_IO_PENDING == ret); 2698 EXPECT_TRUE(kSize1 == ret || net::ERR_IO_PENDING == ret);
2699 if (net::ERR_IO_PENDING == ret) 2699 if (net::ERR_IO_PENDING == ret)
2700 expected++; 2700 expected++;
2701 2701
2702 // This Read must not be optimistic, since we don't support that yet. 2702 // This Read must not be optimistic, since we don't support that yet.
2703 EXPECT_EQ(net::ERR_IO_PENDING, 2703 EXPECT_EQ(net::ERR_IO_PENDING,
2704 entry->ReadData( 2704 entry->ReadData(
2705 0, 2705 1,
2706 0, 2706 0,
2707 buffer1_read.get(), 2707 buffer1_read.get(),
2708 kSize1, 2708 kSize1,
2709 base::Bind(&CallbackTest::Run, base::Unretained(&callback3)))); 2709 base::Bind(&CallbackTest::Run, base::Unretained(&callback3))));
2710 expected++; 2710 expected++;
2711 EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected)); 2711 EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
2712 EXPECT_EQ(0, memcmp(buffer1->data(), buffer1_read->data(), kSize1)); 2712 EXPECT_EQ(0, memcmp(buffer1->data(), buffer1_read->data(), kSize1));
2713 2713
2714 // At this point after waiting, the pending operations queue on the entry 2714 // At this point after waiting, the pending operations queue on the entry
2715 // should be empty, so the next Write operation must run as optimistic. 2715 // should be empty, so the next Write operation must run as optimistic.
2716 EXPECT_EQ(kSize2, 2716 EXPECT_EQ(kSize2,
2717 entry->WriteData( 2717 entry->WriteData(
2718 0, 2718 1,
2719 0, 2719 0,
2720 buffer2.get(), 2720 buffer2.get(),
2721 kSize2, 2721 kSize2,
2722 base::Bind(&CallbackTest::Run, base::Unretained(&callback4)), 2722 base::Bind(&CallbackTest::Run, base::Unretained(&callback4)),
2723 false)); 2723 false));
2724 2724
2725 // Lets do another read so we block until both the write and the read 2725 // Lets do another read so we block until both the write and the read
2726 // operation finishes and we can then test for HasOneRef() below. 2726 // operation finishes and we can then test for HasOneRef() below.
2727 EXPECT_EQ(net::ERR_IO_PENDING, 2727 EXPECT_EQ(net::ERR_IO_PENDING,
2728 entry->ReadData( 2728 entry->ReadData(
2729 0, 2729 1,
2730 0, 2730 0,
2731 buffer2_read.get(), 2731 buffer2_read.get(),
2732 kSize2, 2732 kSize2,
2733 base::Bind(&CallbackTest::Run, base::Unretained(&callback5)))); 2733 base::Bind(&CallbackTest::Run, base::Unretained(&callback5))));
2734 expected++; 2734 expected++;
2735 2735
2736 EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected)); 2736 EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
2737 EXPECT_EQ(0, memcmp(buffer2->data(), buffer2_read->data(), kSize2)); 2737 EXPECT_EQ(0, memcmp(buffer2->data(), buffer2_read->data(), kSize2));
2738 2738
2739 // Check that we are not leaking. 2739 // Check that we are not leaking.
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
2825 2825
2826 ASSERT_EQ(net::OK, 2826 ASSERT_EQ(net::OK,
2827 cache_->CreateEntry(key, &entry, net::CompletionCallback())); 2827 cache_->CreateEntry(key, &entry, net::CompletionCallback()));
2828 EXPECT_NE(null, entry); 2828 EXPECT_NE(null, entry);
2829 entry->Close(); 2829 entry->Close();
2830 2830
2831 // Lets do a Write so we block until both the Close and the Write 2831 // Lets do a Write so we block until both the Close and the Write
2832 // operation finishes. Write must fail since we are writing in a closed entry. 2832 // operation finishes. Write must fail since we are writing in a closed entry.
2833 EXPECT_EQ( 2833 EXPECT_EQ(
2834 net::ERR_IO_PENDING, 2834 net::ERR_IO_PENDING,
2835 entry->WriteData(0, 0, buffer1.get(), kSize1, cb.callback(), false)); 2835 entry->WriteData(1, 0, buffer1.get(), kSize1, cb.callback(), false));
2836 EXPECT_EQ(net::ERR_FAILED, cb.GetResult(net::ERR_IO_PENDING)); 2836 EXPECT_EQ(net::ERR_FAILED, cb.GetResult(net::ERR_IO_PENDING));
2837 2837
2838 // Finish running the pending tasks so that we fully complete the close 2838 // Finish running the pending tasks so that we fully complete the close
2839 // operation and destroy the entry object. 2839 // operation and destroy the entry object.
2840 base::MessageLoop::current()->RunUntilIdle(); 2840 base::MessageLoop::current()->RunUntilIdle();
2841 2841
2842 // At this point the |entry| must have been destroyed, and called 2842 // At this point the |entry| must have been destroyed, and called
2843 // RemoveSelfFromBackend(). 2843 // RemoveSelfFromBackend().
2844 disk_cache::Entry* entry2 = NULL; 2844 disk_cache::Entry* entry2 = NULL;
2845 ASSERT_EQ(net::ERR_IO_PENDING, 2845 ASSERT_EQ(net::ERR_IO_PENDING,
2846 cache_->OpenEntry(key, &entry2, cb.callback())); 2846 cache_->OpenEntry(key, &entry2, cb.callback()));
2847 ASSERT_EQ(net::OK, cb.GetResult(net::ERR_IO_PENDING)); 2847 ASSERT_EQ(net::OK, cb.GetResult(net::ERR_IO_PENDING));
2848 EXPECT_NE(null, entry2); 2848 EXPECT_NE(null, entry2);
2849 2849
2850 disk_cache::Entry* entry3 = NULL; 2850 disk_cache::Entry* entry3 = NULL;
2851 ASSERT_EQ(net::ERR_IO_PENDING, 2851 ASSERT_EQ(net::ERR_IO_PENDING,
2852 cache_->OpenEntry(key, &entry3, cb.callback())); 2852 cache_->OpenEntry(key, &entry3, cb.callback()));
2853 ASSERT_EQ(net::OK, cb.GetResult(net::ERR_IO_PENDING)); 2853 ASSERT_EQ(net::OK, cb.GetResult(net::ERR_IO_PENDING));
2854 EXPECT_NE(null, entry3); 2854 EXPECT_NE(null, entry3);
2855 EXPECT_EQ(entry2, entry3); 2855 EXPECT_EQ(entry2, entry3);
2856 entry3->Close(); 2856 entry3->Close();
2857 2857
2858 // The previous Close doesn't actually closes the entry since we opened it 2858 // The previous Close doesn't actually closes the entry since we opened it
2859 // twice, so the next Write operation must succeed and it must be able to 2859 // twice, so the next Write operation must succeed and it must be able to
2860 // perform it optimistically, since there is no operation running on this 2860 // perform it optimistically, since there is no operation running on this
2861 // entry. 2861 // entry.
2862 EXPECT_EQ(kSize1, 2862 EXPECT_EQ(kSize1,
2863 entry2->WriteData( 2863 entry2->WriteData(
2864 0, 0, buffer1.get(), kSize1, net::CompletionCallback(), false)); 2864 1, 0, buffer1.get(), kSize1, net::CompletionCallback(), false));
2865 2865
2866 // Lets do another read so we block until both the write and the read 2866 // Lets do another read so we block until both the write and the read
2867 // operation finishes and we can then test for HasOneRef() below. 2867 // operation finishes and we can then test for HasOneRef() below.
2868 EXPECT_EQ(net::ERR_IO_PENDING, 2868 EXPECT_EQ(net::ERR_IO_PENDING,
2869 entry2->ReadData(0, 0, buffer1.get(), kSize1, cb.callback())); 2869 entry2->ReadData(1, 0, buffer1.get(), kSize1, cb.callback()));
2870 EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING)); 2870 EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING));
2871 2871
2872 // Check that we are not leaking. 2872 // Check that we are not leaking.
2873 EXPECT_TRUE( 2873 EXPECT_TRUE(
2874 static_cast<disk_cache::SimpleEntryImpl*>(entry2)->HasOneRef()); 2874 static_cast<disk_cache::SimpleEntryImpl*>(entry2)->HasOneRef());
2875 entry2->Close(); 2875 entry2->Close();
2876 } 2876 }
2877 2877
2878 TEST_F(DiskCacheEntryTest, SimpleCacheOptimistic5) { 2878 TEST_F(DiskCacheEntryTest, SimpleCacheOptimistic5) {
2879 // Test sequence: 2879 // Test sequence:
(...skipping 10 matching lines...) Expand all
2890 disk_cache::Entry* entry = NULL; 2890 disk_cache::Entry* entry = NULL;
2891 2891
2892 ASSERT_EQ(net::OK, 2892 ASSERT_EQ(net::OK,
2893 cache_->CreateEntry(key, &entry, net::CompletionCallback())); 2893 cache_->CreateEntry(key, &entry, net::CompletionCallback()));
2894 EXPECT_NE(null, entry); 2894 EXPECT_NE(null, entry);
2895 ScopedEntryPtr entry_closer(entry); 2895 ScopedEntryPtr entry_closer(entry);
2896 entry->Doom(); 2896 entry->Doom();
2897 2897
2898 EXPECT_EQ( 2898 EXPECT_EQ(
2899 net::ERR_IO_PENDING, 2899 net::ERR_IO_PENDING,
2900 entry->WriteData(0, 0, buffer1.get(), kSize1, cb.callback(), false)); 2900 entry->WriteData(1, 0, buffer1.get(), kSize1, cb.callback(), false));
2901 EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING)); 2901 EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING));
2902 2902
2903 EXPECT_EQ(net::ERR_IO_PENDING, 2903 EXPECT_EQ(net::ERR_IO_PENDING,
2904 entry->ReadData(0, 0, buffer1.get(), kSize1, cb.callback())); 2904 entry->ReadData(1, 0, buffer1.get(), kSize1, cb.callback()));
2905 EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING)); 2905 EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING));
2906 2906
2907 // Check that we are not leaking. 2907 // Check that we are not leaking.
2908 EXPECT_TRUE( 2908 EXPECT_TRUE(
2909 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef()); 2909 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef());
2910 } 2910 }
2911 2911
2912 TEST_F(DiskCacheEntryTest, SimpleCacheOptimistic6) { 2912 TEST_F(DiskCacheEntryTest, SimpleCacheOptimistic6) {
2913 // Test sequence: 2913 // Test sequence:
2914 // Create, Write, Doom, Doom, Read, Doom, Close. 2914 // Create, Write, Doom, Doom, Read, Doom, Close.
2915 SetSimpleCacheMode(); 2915 SetSimpleCacheMode();
2916 InitCache(); 2916 InitCache();
2917 disk_cache::Entry* null = NULL; 2917 disk_cache::Entry* null = NULL;
2918 const char key[] = "the first key"; 2918 const char key[] = "the first key";
2919 2919
2920 net::TestCompletionCallback cb; 2920 net::TestCompletionCallback cb;
2921 const int kSize1 = 10; 2921 const int kSize1 = 10;
2922 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1)); 2922 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
2923 scoped_refptr<net::IOBuffer> buffer1_read(new net::IOBuffer(kSize1)); 2923 scoped_refptr<net::IOBuffer> buffer1_read(new net::IOBuffer(kSize1));
2924 CacheTestFillBuffer(buffer1->data(), kSize1, false); 2924 CacheTestFillBuffer(buffer1->data(), kSize1, false);
2925 disk_cache::Entry* entry = NULL; 2925 disk_cache::Entry* entry = NULL;
2926 2926
2927 ASSERT_EQ(net::OK, 2927 ASSERT_EQ(net::OK,
2928 cache_->CreateEntry(key, &entry, net::CompletionCallback())); 2928 cache_->CreateEntry(key, &entry, net::CompletionCallback()));
2929 EXPECT_NE(null, entry); 2929 EXPECT_NE(null, entry);
2930 ScopedEntryPtr entry_closer(entry); 2930 ScopedEntryPtr entry_closer(entry);
2931 2931
2932 EXPECT_EQ( 2932 EXPECT_EQ(
2933 net::ERR_IO_PENDING, 2933 net::ERR_IO_PENDING,
2934 entry->WriteData(0, 0, buffer1.get(), kSize1, cb.callback(), false)); 2934 entry->WriteData(1, 0, buffer1.get(), kSize1, cb.callback(), false));
2935 EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING)); 2935 EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING));
2936 2936
2937 entry->Doom(); 2937 entry->Doom();
2938 entry->Doom(); 2938 entry->Doom();
2939 2939
2940 // This Read must not be optimistic, since we don't support that yet. 2940 // This Read must not be optimistic, since we don't support that yet.
2941 EXPECT_EQ(net::ERR_IO_PENDING, 2941 EXPECT_EQ(net::ERR_IO_PENDING,
2942 entry->ReadData(0, 0, buffer1_read.get(), kSize1, cb.callback())); 2942 entry->ReadData(1, 0, buffer1_read.get(), kSize1, cb.callback()));
2943 EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING)); 2943 EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING));
2944 EXPECT_EQ(0, memcmp(buffer1->data(), buffer1_read->data(), kSize1)); 2944 EXPECT_EQ(0, memcmp(buffer1->data(), buffer1_read->data(), kSize1));
2945 2945
2946 entry->Doom(); 2946 entry->Doom();
2947 } 2947 }
2948 2948
2949 // Confirm that IO buffers are not referenced by the Simple Cache after a write 2949 // Confirm that IO buffers are not referenced by the Simple Cache after a write
2950 // completes. 2950 // completes.
2951 TEST_F(DiskCacheEntryTest, SimpleCacheOptimisticWriteReleases) { 2951 TEST_F(DiskCacheEntryTest, SimpleCacheOptimisticWriteReleases) {
2952 SetSimpleCacheMode(); 2952 SetSimpleCacheMode();
(...skipping 10 matching lines...) Expand all
2963 2963
2964 const int kWriteSize = 512; 2964 const int kWriteSize = 512;
2965 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kWriteSize)); 2965 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kWriteSize));
2966 EXPECT_TRUE(buffer1->HasOneRef()); 2966 EXPECT_TRUE(buffer1->HasOneRef());
2967 CacheTestFillBuffer(buffer1->data(), kWriteSize, false); 2967 CacheTestFillBuffer(buffer1->data(), kWriteSize, false);
2968 2968
2969 // An optimistic write happens only when there is an empty queue of pending 2969 // An optimistic write happens only when there is an empty queue of pending
2970 // operations. To ensure the queue is empty, we issue a write and wait until 2970 // operations. To ensure the queue is empty, we issue a write and wait until
2971 // it completes. 2971 // it completes.
2972 EXPECT_EQ(kWriteSize, 2972 EXPECT_EQ(kWriteSize,
2973 WriteData(entry, 0, 0, buffer1.get(), kWriteSize, false)); 2973 WriteData(entry, 1, 0, buffer1.get(), kWriteSize, false));
2974 EXPECT_TRUE(buffer1->HasOneRef()); 2974 EXPECT_TRUE(buffer1->HasOneRef());
2975 2975
2976 // Finally, we should perform an optimistic write and confirm that all 2976 // Finally, we should perform an optimistic write and confirm that all
2977 // references to the IO buffer have been released. 2977 // references to the IO buffer have been released.
2978 EXPECT_EQ( 2978 EXPECT_EQ(
2979 kWriteSize, 2979 kWriteSize,
2980 entry->WriteData( 2980 entry->WriteData(
2981 1, 0, buffer1.get(), kWriteSize, net::CompletionCallback(), false)); 2981 1, 0, buffer1.get(), kWriteSize, net::CompletionCallback(), false));
2982 EXPECT_TRUE(buffer1->HasOneRef()); 2982 EXPECT_TRUE(buffer1->HasOneRef());
2983 } 2983 }
(...skipping 24 matching lines...) Expand all
3008 entry->WriteData(0, 0, buffer1.get(), kSize1, cb.callback(), false)); 3008 entry->WriteData(0, 0, buffer1.get(), kSize1, cb.callback(), false));
3009 3009
3010 entry->Close(); 3010 entry->Close();
3011 3011
3012 // Finish running the pending tasks so that we fully complete the close 3012 // Finish running the pending tasks so that we fully complete the close
3013 // operation and destroy the entry object. 3013 // operation and destroy the entry object.
3014 base::MessageLoop::current()->RunUntilIdle(); 3014 base::MessageLoop::current()->RunUntilIdle();
3015 3015
3016 for (int i = 0; i < disk_cache::kSimpleEntryFileCount; ++i) { 3016 for (int i = 0; i < disk_cache::kSimpleEntryFileCount; ++i) {
3017 base::FilePath entry_file_path = cache_path_.AppendASCII( 3017 base::FilePath entry_file_path = cache_path_.AppendASCII(
3018 disk_cache::simple_util::GetFilenameFromKeyAndIndex(key, i)); 3018 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, i));
3019 base::PlatformFileInfo info; 3019 base::PlatformFileInfo info;
3020 EXPECT_FALSE(file_util::GetFileInfo(entry_file_path, &info)); 3020 EXPECT_FALSE(file_util::GetFileInfo(entry_file_path, &info));
3021 } 3021 }
3022 } 3022 }
3023 3023
3024 TEST_F(DiskCacheEntryTest, SimpleCacheDoomCreateRace) { 3024 TEST_F(DiskCacheEntryTest, SimpleCacheDoomCreateRace) {
3025 // This test runs as APP_CACHE to make operations more synchronous. Test 3025 // This test runs as APP_CACHE to make operations more synchronous. Test
3026 // sequence: 3026 // sequence:
3027 // Create, Doom, Create. 3027 // Create, Doom, Create.
3028 SetCacheType(net::APP_CACHE); 3028 SetCacheType(net::APP_CACHE);
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
3150 SetSimpleCacheMode(); 3150 SetSimpleCacheMode();
3151 SetMaxSize(kMaxSize); 3151 SetMaxSize(kMaxSize);
3152 InitCache(); 3152 InitCache();
3153 3153
3154 std::string key1("the first key"); 3154 std::string key1("the first key");
3155 disk_cache::Entry* entry; 3155 disk_cache::Entry* entry;
3156 ASSERT_EQ(net::OK, CreateEntry(key1, &entry)); 3156 ASSERT_EQ(net::OK, CreateEntry(key1, &entry));
3157 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kWriteSize)); 3157 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kWriteSize));
3158 CacheTestFillBuffer(buffer->data(), kWriteSize, false); 3158 CacheTestFillBuffer(buffer->data(), kWriteSize, false);
3159 EXPECT_EQ(kWriteSize, 3159 EXPECT_EQ(kWriteSize,
3160 WriteData(entry, 0, 0, buffer.get(), kWriteSize, false)); 3160 WriteData(entry, 1, 0, buffer.get(), kWriteSize, false));
3161 entry->Close(); 3161 entry->Close();
3162 AddDelay(); 3162 AddDelay();
3163 3163
3164 std::string key2("the key prefix"); 3164 std::string key2("the key prefix");
3165 for (int i = 0; i < kNumExtraEntries; i++) { 3165 for (int i = 0; i < kNumExtraEntries; i++) {
3166 ASSERT_EQ(net::OK, CreateEntry(key2 + base::StringPrintf("%d", i), &entry)); 3166 ASSERT_EQ(net::OK, CreateEntry(key2 + base::StringPrintf("%d", i), &entry));
3167 ScopedEntryPtr entry_closer(entry); 3167 ScopedEntryPtr entry_closer(entry);
3168 EXPECT_EQ(kWriteSize, 3168 EXPECT_EQ(kWriteSize,
3169 WriteData(entry, 0, 0, buffer.get(), kWriteSize, false)); 3169 WriteData(entry, 1, 0, buffer.get(), kWriteSize, false));
3170 } 3170 }
3171 3171
3172 // TODO(pasko): Find a way to wait for the eviction task(s) to finish by using 3172 // TODO(pasko): Find a way to wait for the eviction task(s) to finish by using
3173 // the internal knowledge about |SimpleBackendImpl|. 3173 // the internal knowledge about |SimpleBackendImpl|.
3174 ASSERT_NE(net::OK, OpenEntry(key1, &entry)) 3174 ASSERT_NE(net::OK, OpenEntry(key1, &entry))
3175 << "Should have evicted the old entry"; 3175 << "Should have evicted the old entry";
3176 for (int i = 0; i < 2; i++) { 3176 for (int i = 0; i < 2; i++) {
3177 int entry_no = kNumExtraEntries - i - 1; 3177 int entry_no = kNumExtraEntries - i - 1;
3178 // Generally there is no guarantee that at this point the backround eviction 3178 // Generally there is no guarantee that at this point the backround eviction
3179 // is finished. We are testing the positive case, i.e. when the eviction 3179 // is finished. We are testing the positive case, i.e. when the eviction
(...skipping 15 matching lines...) Expand all
3195 const char key[] = "the first key"; 3195 const char key[] = "the first key";
3196 3196
3197 const int kBufferSize = 1024; 3197 const int kBufferSize = 1024;
3198 scoped_refptr<net::IOBuffer> write_buffer(new net::IOBuffer(kBufferSize)); 3198 scoped_refptr<net::IOBuffer> write_buffer(new net::IOBuffer(kBufferSize));
3199 CacheTestFillBuffer(write_buffer->data(), kBufferSize, false); 3199 CacheTestFillBuffer(write_buffer->data(), kBufferSize, false);
3200 3200
3201 disk_cache::Entry* entry = NULL; 3201 disk_cache::Entry* entry = NULL;
3202 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 3202 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
3203 3203
3204 EXPECT_EQ(kBufferSize, 3204 EXPECT_EQ(kBufferSize,
3205 WriteData(entry, 0, 0, write_buffer.get(), kBufferSize, false)); 3205 WriteData(entry, 1, 0, write_buffer.get(), kBufferSize, false));
3206 entry->Close(); 3206 entry->Close();
3207 entry = NULL; 3207 entry = NULL;
3208 3208
3209 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); 3209 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
3210 ScopedEntryPtr entry_closer(entry); 3210 ScopedEntryPtr entry_closer(entry);
3211 3211
3212 MessageLoopHelper helper; 3212 MessageLoopHelper helper;
3213 int expected = 0; 3213 int expected = 0;
3214 3214
3215 // Make a short read. 3215 // Make a short read.
3216 const int kReadBufferSize = 512; 3216 const int kReadBufferSize = 512;
3217 scoped_refptr<net::IOBuffer> read_buffer(new net::IOBuffer(kReadBufferSize)); 3217 scoped_refptr<net::IOBuffer> read_buffer(new net::IOBuffer(kReadBufferSize));
3218 CallbackTest read_callback(&helper, false); 3218 CallbackTest read_callback(&helper, false);
3219 EXPECT_EQ(net::ERR_IO_PENDING, 3219 EXPECT_EQ(net::ERR_IO_PENDING,
3220 entry->ReadData(0, 3220 entry->ReadData(1,
3221 0, 3221 0,
3222 read_buffer.get(), 3222 read_buffer.get(),
3223 kReadBufferSize, 3223 kReadBufferSize,
3224 base::Bind(&CallbackTest::Run, 3224 base::Bind(&CallbackTest::Run,
3225 base::Unretained(&read_callback)))); 3225 base::Unretained(&read_callback))));
3226 ++expected; 3226 ++expected;
3227 3227
3228 // Truncate the entry to the length of that read. 3228 // Truncate the entry to the length of that read.
3229 scoped_refptr<net::IOBuffer> 3229 scoped_refptr<net::IOBuffer>
3230 truncate_buffer(new net::IOBuffer(kReadBufferSize)); 3230 truncate_buffer(new net::IOBuffer(kReadBufferSize));
3231 CacheTestFillBuffer(truncate_buffer->data(), kReadBufferSize, false); 3231 CacheTestFillBuffer(truncate_buffer->data(), kReadBufferSize, false);
3232 CallbackTest truncate_callback(&helper, false); 3232 CallbackTest truncate_callback(&helper, false);
3233 EXPECT_EQ(net::ERR_IO_PENDING, 3233 EXPECT_EQ(net::ERR_IO_PENDING,
3234 entry->WriteData(0, 3234 entry->WriteData(1,
3235 0, 3235 0,
3236 truncate_buffer.get(), 3236 truncate_buffer.get(),
3237 kReadBufferSize, 3237 kReadBufferSize,
3238 base::Bind(&CallbackTest::Run, 3238 base::Bind(&CallbackTest::Run,
3239 base::Unretained(&truncate_callback)), 3239 base::Unretained(&truncate_callback)),
3240 true)); 3240 true));
3241 ++expected; 3241 ++expected;
3242 3242
3243 // Wait for both the read and truncation to finish, and confirm that both 3243 // Wait for both the read and truncation to finish, and confirm that both
3244 // succeeded. 3244 // succeeded.
(...skipping 19 matching lines...) Expand all
3264 3264
3265 const int kBufferSize = 1024; 3265 const int kBufferSize = 1024;
3266 scoped_refptr<net::IOBuffer> write_buffer(new net::IOBuffer(kBufferSize)); 3266 scoped_refptr<net::IOBuffer> write_buffer(new net::IOBuffer(kBufferSize));
3267 CacheTestFillBuffer(write_buffer->data(), kBufferSize, false); 3267 CacheTestFillBuffer(write_buffer->data(), kBufferSize, false);
3268 3268
3269 MessageLoopHelper helper; 3269 MessageLoopHelper helper;
3270 int expected = 0; 3270 int expected = 0;
3271 3271
3272 CallbackTest write_callback(&helper, false); 3272 CallbackTest write_callback(&helper, false);
3273 EXPECT_EQ(net::ERR_IO_PENDING, 3273 EXPECT_EQ(net::ERR_IO_PENDING,
3274 entry->WriteData(0, 3274 entry->WriteData(1,
3275 0, 3275 0,
3276 write_buffer.get(), 3276 write_buffer.get(),
3277 kBufferSize, 3277 kBufferSize,
3278 base::Bind(&CallbackTest::Run, 3278 base::Bind(&CallbackTest::Run,
3279 base::Unretained(&write_callback)), 3279 base::Unretained(&write_callback)),
3280 true)); 3280 true));
3281 ++expected; 3281 ++expected;
3282 3282
3283 scoped_refptr<net::IOBuffer> read_buffer(new net::IOBuffer(kBufferSize)); 3283 scoped_refptr<net::IOBuffer> read_buffer(new net::IOBuffer(kBufferSize));
3284 CallbackTest read_callback(&helper, false); 3284 CallbackTest read_callback(&helper, false);
3285 EXPECT_EQ(net::ERR_IO_PENDING, 3285 EXPECT_EQ(net::ERR_IO_PENDING,
3286 entry->ReadData(0, 3286 entry->ReadData(1,
3287 0, 3287 0,
3288 read_buffer.get(), 3288 read_buffer.get(),
3289 kBufferSize, 3289 kBufferSize,
3290 base::Bind(&CallbackTest::Run, 3290 base::Bind(&CallbackTest::Run,
3291 base::Unretained(&read_callback)))); 3291 base::Unretained(&read_callback))));
3292 ++expected; 3292 ++expected;
3293 3293
3294 EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected)); 3294 EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
3295 EXPECT_EQ(kBufferSize, write_callback.last_result()); 3295 EXPECT_EQ(kBufferSize, write_callback.last_result());
3296 EXPECT_EQ(kBufferSize, read_callback.last_result()); 3296 EXPECT_EQ(kBufferSize, read_callback.last_result());
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
3360 int size; 3360 int size;
3361 ASSERT_TRUE(SimpleCacheMakeBadChecksumEntry(key, &size)); 3361 ASSERT_TRUE(SimpleCacheMakeBadChecksumEntry(key, &size));
3362 3362
3363 scoped_refptr<net::IOBuffer> read_buffer1(new net::IOBuffer(size)); 3363 scoped_refptr<net::IOBuffer> read_buffer1(new net::IOBuffer(size));
3364 scoped_refptr<net::IOBuffer> read_buffer2(new net::IOBuffer(size)); 3364 scoped_refptr<net::IOBuffer> read_buffer2(new net::IOBuffer(size));
3365 3365
3366 // Advance the first reader a little. 3366 // Advance the first reader a little.
3367 disk_cache::Entry* entry = NULL; 3367 disk_cache::Entry* entry = NULL;
3368 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); 3368 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
3369 ScopedEntryPtr entry_closer(entry); 3369 ScopedEntryPtr entry_closer(entry);
3370 EXPECT_EQ(1, ReadData(entry, 0, 0, read_buffer1.get(), 1)); 3370 EXPECT_EQ(1, ReadData(entry, 1, 0, read_buffer1.get(), 1));
3371 3371
3372 // Advance the 2nd reader by the same amount. 3372 // Advance the 2nd reader by the same amount.
3373 disk_cache::Entry* entry2 = NULL; 3373 disk_cache::Entry* entry2 = NULL;
3374 EXPECT_EQ(net::OK, OpenEntry(key, &entry2)); 3374 EXPECT_EQ(net::OK, OpenEntry(key, &entry2));
3375 ScopedEntryPtr entry2_closer(entry2); 3375 ScopedEntryPtr entry2_closer(entry2);
3376 EXPECT_EQ(1, ReadData(entry2, 0, 0, read_buffer2.get(), 1)); 3376 EXPECT_EQ(1, ReadData(entry2, 1, 0, read_buffer2.get(), 1));
3377 3377
3378 // Continue reading 1st. 3378 // Continue reading 1st.
3379 EXPECT_GT(0, ReadData(entry, 0, 1, read_buffer1.get(), size)); 3379 EXPECT_GT(0, ReadData(entry, 1, 1, read_buffer1.get(), size));
3380 3380
3381 // This read should fail as well because we have previous read failures. 3381 // This read should fail as well because we have previous read failures.
3382 EXPECT_GT(0, ReadData(entry2, 0, 1, read_buffer2.get(), 1)); 3382 EXPECT_GT(0, ReadData(entry2, 1, 1, read_buffer2.get(), 1));
3383 DisableIntegrityCheck(); 3383 DisableIntegrityCheck();
3384 } 3384 }
3385 3385
3386 // Test if we can sequentially read each subset of the data until all the data 3386 // Test if we can sequentially read each subset of the data until all the data
3387 // is read, then the CRC is calculated correctly and the reads are successful. 3387 // is read, then the CRC is calculated correctly and the reads are successful.
3388 TEST_F(DiskCacheEntryTest, SimpleCacheReadCombineCRC) { 3388 TEST_F(DiskCacheEntryTest, SimpleCacheReadCombineCRC) {
3389 // Test sequence: 3389 // Test sequence:
3390 // Create, Write, Read (first half of data), Read (second half of data), 3390 // Create, Write, Read (first half of data), Read (second half of data),
3391 // Close. 3391 // Close.
3392 SetSimpleCacheMode(); 3392 SetSimpleCacheMode();
3393 InitCache(); 3393 InitCache();
3394 disk_cache::Entry* null = NULL; 3394 disk_cache::Entry* null = NULL;
3395 const char key[] = "the first key"; 3395 const char key[] = "the first key";
3396 3396
3397 const int kHalfSize = 200; 3397 const int kHalfSize = 200;
3398 const int kSize = 2 * kHalfSize; 3398 const int kSize = 2 * kHalfSize;
3399 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); 3399 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
3400 CacheTestFillBuffer(buffer1->data(), kSize, false); 3400 CacheTestFillBuffer(buffer1->data(), kSize, false);
3401 disk_cache::Entry* entry = NULL; 3401 disk_cache::Entry* entry = NULL;
3402 3402
3403 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 3403 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
3404 EXPECT_NE(null, entry); 3404 EXPECT_NE(null, entry);
3405 3405
3406 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer1.get(), kSize, false)); 3406 EXPECT_EQ(kSize, WriteData(entry, 1, 0, buffer1.get(), kSize, false));
3407 entry->Close(); 3407 entry->Close();
3408 3408
3409 disk_cache::Entry* entry2 = NULL; 3409 disk_cache::Entry* entry2 = NULL;
3410 ASSERT_EQ(net::OK, OpenEntry(key, &entry2)); 3410 ASSERT_EQ(net::OK, OpenEntry(key, &entry2));
3411 EXPECT_EQ(entry, entry2); 3411 EXPECT_EQ(entry, entry2);
3412 3412
3413 // Read the first half of the data. 3413 // Read the first half of the data.
3414 int offset = 0; 3414 int offset = 0;
3415 int buf_len = kHalfSize; 3415 int buf_len = kHalfSize;
3416 scoped_refptr<net::IOBuffer> buffer1_read1(new net::IOBuffer(buf_len)); 3416 scoped_refptr<net::IOBuffer> buffer1_read1(new net::IOBuffer(buf_len));
3417 EXPECT_EQ(buf_len, ReadData(entry2, 0, offset, buffer1_read1.get(), buf_len)); 3417 EXPECT_EQ(buf_len, ReadData(entry2, 1, offset, buffer1_read1.get(), buf_len));
3418 EXPECT_EQ(0, memcmp(buffer1->data(), buffer1_read1->data(), buf_len)); 3418 EXPECT_EQ(0, memcmp(buffer1->data(), buffer1_read1->data(), buf_len));
3419 3419
3420 // Read the second half of the data. 3420 // Read the second half of the data.
3421 offset = buf_len; 3421 offset = buf_len;
3422 buf_len = kHalfSize; 3422 buf_len = kHalfSize;
3423 scoped_refptr<net::IOBuffer> buffer1_read2(new net::IOBuffer(buf_len)); 3423 scoped_refptr<net::IOBuffer> buffer1_read2(new net::IOBuffer(buf_len));
3424 EXPECT_EQ(buf_len, ReadData(entry2, 0, offset, buffer1_read2.get(), buf_len)); 3424 EXPECT_EQ(buf_len, ReadData(entry2, 1, offset, buffer1_read2.get(), buf_len));
3425 char* buffer1_data = buffer1->data() + offset; 3425 char* buffer1_data = buffer1->data() + offset;
3426 EXPECT_EQ(0, memcmp(buffer1_data, buffer1_read2->data(), buf_len)); 3426 EXPECT_EQ(0, memcmp(buffer1_data, buffer1_read2->data(), buf_len));
3427 3427
3428 // Check that we are not leaking. 3428 // Check that we are not leaking.
3429 EXPECT_NE(entry, null); 3429 EXPECT_NE(entry, null);
3430 EXPECT_TRUE( 3430 EXPECT_TRUE(
3431 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef()); 3431 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef());
3432 entry->Close(); 3432 entry->Close();
3433 entry = NULL; 3433 entry = NULL;
3434 } 3434 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
3476 EXPECT_EQ(0, memcmp(buffer1->data(), buffer1_read1->data(), kSize)); 3476 EXPECT_EQ(0, memcmp(buffer1->data(), buffer1_read1->data(), kSize));
3477 3477
3478 // Check that we are not leaking. 3478 // Check that we are not leaking.
3479 ASSERT_NE(entry, null); 3479 ASSERT_NE(entry, null);
3480 EXPECT_TRUE( 3480 EXPECT_TRUE(
3481 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef()); 3481 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef());
3482 entry->Close(); 3482 entry->Close();
3483 entry = NULL; 3483 entry = NULL;
3484 } 3484 }
3485 3485
3486 // Test that changing stream1 size does not affect stream0 (stream0 and stream1
3487 // are stored in the same file in Simple Cache).
3488 TEST_F(DiskCacheEntryTest, SimpleCacheStream1SizeChanges) {
3489 SetSimpleCacheMode();
3490 InitCache();
3491 disk_cache::Entry* entry = NULL;
3492 const char key[] = "the key";
3493 const int kSize = 100;
3494 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
3495 scoped_refptr<net::IOBuffer> buffer_read(new net::IOBuffer(kSize));
3496 CacheTestFillBuffer(buffer->data(), kSize, false);
3497
3498 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
3499 EXPECT_TRUE(entry);
3500
3501 // Write something into stream0.
3502 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false));
3503 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer_read.get(), kSize));
3504 EXPECT_EQ(0, memcmp(buffer->data(), buffer_read->data(), kSize));
3505 entry->Close();
3506
3507 // Extend stream1.
3508 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
3509 int stream1_size = 100;
3510 EXPECT_EQ(0, WriteData(entry, 1, stream1_size, buffer.get(), 0, false));
3511 EXPECT_EQ(stream1_size, entry->GetDataSize(1));
3512 entry->Close();
3513
3514 // Check that stream0 data has not been modified and that the EOF record for
3515 // stream 0 contains a crc.
3516 // The entry needs to be reopened before checking the crc: Open will perform
3517 // the synchronization with the previous Close. This ensures the EOF records
3518 // have been written to disk before we attempt to read them independently.
3519 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
3520 base::FilePath entry_file0_path = cache_path_.AppendASCII(
3521 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0));
3522 int flags = base::PLATFORM_FILE_READ | base::PLATFORM_FILE_OPEN;
3523 base::PlatformFile entry_file0 =
3524 base::CreatePlatformFile(entry_file0_path, flags, NULL, NULL);
3525 ASSERT_TRUE(entry_file0 != base::kInvalidPlatformFileValue);
3526
3527 int data_size[disk_cache::kSimpleEntryStreamCount] = {kSize, stream1_size, 0};
3528 disk_cache::SimpleEntryStat entry_stat(
3529 base::Time::Now(), base::Time::Now(), data_size);
3530 int eof_offset = entry_stat.GetEOFOffsetInFile(key, 0);
3531 disk_cache::SimpleFileEOF eof_record;
3532 ASSERT_EQ(static_cast<int>(sizeof(eof_record)), base::ReadPlatformFile(
3533 entry_file0,
3534 eof_offset,
3535 reinterpret_cast<char*>(&eof_record),
3536 sizeof(eof_record)));
3537 EXPECT_EQ(disk_cache::kSimpleFinalMagicNumber, eof_record.final_magic_number);
3538 EXPECT_TRUE((eof_record.flags & disk_cache::SimpleFileEOF::FLAG_HAS_CRC32) ==
3539 disk_cache::SimpleFileEOF::FLAG_HAS_CRC32);
3540
3541 buffer_read = new net::IOBuffer(kSize);
3542 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer_read.get(), kSize));
3543 EXPECT_EQ(0, memcmp(buffer->data(), buffer_read->data(), kSize));
3544
3545 // Shrink stream1.
3546 stream1_size = 50;
3547 EXPECT_EQ(0, WriteData(entry, 1, stream1_size, buffer.get(), 0, true));
3548 EXPECT_EQ(stream1_size, entry->GetDataSize(1));
3549 entry->Close();
3550
3551 // Check that stream0 data has not been modified.
3552 buffer_read = new net::IOBuffer(kSize);
3553 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
3554 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer_read.get(), kSize));
3555 EXPECT_EQ(0, memcmp(buffer->data(), buffer_read->data(), kSize));
3556 entry->Close();
3557 entry = NULL;
3558 }
3559
3486 #endif // defined(OS_POSIX) 3560 #endif // defined(OS_POSIX)
OLDNEW
« no previous file with comments | « net/disk_cache/backend_unittest.cc ('k') | net/disk_cache/simple/simple_entry_format.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698