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