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

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

Issue 15829004: Update net/ to use scoped_refptr<T>::get() rather than implicit "operator T*" (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: license twerk Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/disk_cache/backend_impl.cc ('k') | net/disk_cache/block_files.cc » ('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/file_util.h" 6 #include "base/file_util.h"
7 #include "base/port.h" 7 #include "base/port.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/stringprintf.h" 9 #include "base/stringprintf.h"
10 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" 10 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 base::FilePath filename = cache_path_.AppendASCII("f_000001"); 288 base::FilePath filename = cache_path_.AppendASCII("f_000001");
289 289
290 const int kSize = 50; 290 const int kSize = 50;
291 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); 291 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
292 CacheTestFillBuffer(buffer1->data(), kSize, false); 292 CacheTestFillBuffer(buffer1->data(), kSize, false);
293 ASSERT_EQ(kSize, file_util::WriteFile(filename, buffer1->data(), kSize)); 293 ASSERT_EQ(kSize, file_util::WriteFile(filename, buffer1->data(), kSize));
294 294
295 // Now let's create a file with the cache. 295 // Now let's create a file with the cache.
296 disk_cache::Entry* entry; 296 disk_cache::Entry* entry;
297 ASSERT_EQ(net::OK, CreateEntry("key", &entry)); 297 ASSERT_EQ(net::OK, CreateEntry("key", &entry));
298 ASSERT_EQ(0, WriteData(entry, 0, 20000, buffer1, 0, false)); 298 ASSERT_EQ(0, WriteData(entry, 0, 20000, buffer1.get(), 0, false));
299 entry->Close(); 299 entry->Close();
300 300
301 // And verify that the first file is still there. 301 // And verify that the first file is still there.
302 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize)); 302 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize));
303 ASSERT_EQ(kSize, file_util::ReadFile(filename, buffer2->data(), kSize)); 303 ASSERT_EQ(kSize, file_util::ReadFile(filename, buffer2->data(), kSize));
304 EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kSize)); 304 EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kSize));
305 } 305 }
306 306
307 // Tests that we deal with file-level pending operations at destruction time. 307 // Tests that we deal with file-level pending operations at destruction time.
308 void DiskCacheBackendTest::BackendShutdownWithPendingFileIO(bool fast) { 308 void DiskCacheBackendTest::BackendShutdownWithPendingFileIO(bool fast) {
(...skipping 20 matching lines...) Expand all
329 ASSERT_EQ(net::OK, cb.GetResult(rv)); 329 ASSERT_EQ(net::OK, cb.GetResult(rv));
330 330
331 const int kSize = 25000; 331 const int kSize = 25000;
332 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 332 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
333 CacheTestFillBuffer(buffer->data(), kSize, false); 333 CacheTestFillBuffer(buffer->data(), kSize, false);
334 334
335 for (int i = 0; i < 10 * 1024 * 1024; i += 64 * 1024) { 335 for (int i = 0; i < 10 * 1024 * 1024; i += 64 * 1024) {
336 // We are using the current thread as the cache thread because we want to 336 // We are using the current thread as the cache thread because we want to
337 // be able to call directly this method to make sure that the OS (instead 337 // be able to call directly this method to make sure that the OS (instead
338 // of us switching thread) is returning IO pending. 338 // of us switching thread) is returning IO pending.
339 rv = entry->WriteDataImpl(0, i, buffer, kSize, cb.callback(), false); 339 rv =
340 entry->WriteDataImpl(0, i, buffer.get(), kSize, cb.callback(), false);
340 if (rv == net::ERR_IO_PENDING) 341 if (rv == net::ERR_IO_PENDING)
341 break; 342 break;
342 EXPECT_EQ(kSize, rv); 343 EXPECT_EQ(kSize, rv);
343 } 344 }
344 345
345 // Don't call Close() to avoid going through the queue or we'll deadlock 346 // Don't call Close() to avoid going through the queue or we'll deadlock
346 // waiting for the operation to finish. 347 // waiting for the operation to finish.
347 entry->Release(); 348 entry->Release();
348 349
349 // The cache destructor will see one pending operation here. 350 // The cache destructor will see one pending operation here.
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 SetMaxSize(cache_size); 490 SetMaxSize(cache_size);
490 InitCache(); 491 InitCache();
491 492
492 std::string first("some key"); 493 std::string first("some key");
493 std::string second("something else"); 494 std::string second("something else");
494 disk_cache::Entry* entry; 495 disk_cache::Entry* entry;
495 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); 496 ASSERT_EQ(net::OK, CreateEntry(first, &entry));
496 497
497 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(cache_size)); 498 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(cache_size));
498 memset(buffer->data(), 0, cache_size); 499 memset(buffer->data(), 0, cache_size);
499 EXPECT_EQ(cache_size / 10, WriteData(entry, 0, 0, buffer, cache_size / 10, 500 EXPECT_EQ(cache_size / 10,
500 false)) << "normal file"; 501 WriteData(entry, 0, 0, buffer.get(), cache_size / 10, false))
502 << "normal file";
501 503
502 EXPECT_EQ(net::ERR_FAILED, WriteData(entry, 1, 0, buffer, cache_size / 5, 504 EXPECT_EQ(net::ERR_FAILED,
503 false)) << "file size above the limit"; 505 WriteData(entry, 1, 0, buffer.get(), cache_size / 5, false))
506 << "file size above the limit";
504 507
505 // By doubling the total size, we make this file cacheable. 508 // By doubling the total size, we make this file cacheable.
506 SetMaxSize(cache_size * 2); 509 SetMaxSize(cache_size * 2);
507 EXPECT_EQ(cache_size / 5, WriteData(entry, 1, 0, buffer, cache_size / 5, 510 EXPECT_EQ(cache_size / 5,
508 false)); 511 WriteData(entry, 1, 0, buffer.get(), cache_size / 5, false));
509 512
510 // Let's fill up the cache!. 513 // Let's fill up the cache!.
511 SetMaxSize(cache_size * 10); 514 SetMaxSize(cache_size * 10);
512 EXPECT_EQ(cache_size * 3 / 4, WriteData(entry, 0, 0, buffer, 515 EXPECT_EQ(cache_size * 3 / 4,
513 cache_size * 3 / 4, false)); 516 WriteData(entry, 0, 0, buffer.get(), cache_size * 3 / 4, false));
514 entry->Close(); 517 entry->Close();
515 FlushQueueForTest(); 518 FlushQueueForTest();
516 519
517 SetMaxSize(cache_size); 520 SetMaxSize(cache_size);
518 521
519 // The cache is 95% full. 522 // The cache is 95% full.
520 523
521 ASSERT_EQ(net::OK, CreateEntry(second, &entry)); 524 ASSERT_EQ(net::OK, CreateEntry(second, &entry));
522 EXPECT_EQ(cache_size / 10, WriteData(entry, 0, 0, buffer, cache_size / 10, 525 EXPECT_EQ(cache_size / 10,
523 false)); 526 WriteData(entry, 0, 0, buffer.get(), cache_size / 10, false));
524 527
525 disk_cache::Entry* entry2; 528 disk_cache::Entry* entry2;
526 ASSERT_EQ(net::OK, CreateEntry("an extra key", &entry2)); 529 ASSERT_EQ(net::OK, CreateEntry("an extra key", &entry2));
527 EXPECT_EQ(cache_size / 10, WriteData(entry2, 0, 0, buffer, cache_size / 10, 530 EXPECT_EQ(cache_size / 10,
528 false)); 531 WriteData(entry2, 0, 0, buffer.get(), cache_size / 10, false));
529 entry2->Close(); // This will trigger the cache trim. 532 entry2->Close(); // This will trigger the cache trim.
530 533
531 EXPECT_NE(net::OK, OpenEntry(first, &entry2)); 534 EXPECT_NE(net::OK, OpenEntry(first, &entry2));
532 535
533 FlushQueueForTest(); // Make sure that we are done trimming the cache. 536 FlushQueueForTest(); // Make sure that we are done trimming the cache.
534 FlushQueueForTest(); // We may have posted two tasks to evict stuff. 537 FlushQueueForTest(); // We may have posted two tasks to evict stuff.
535 538
536 entry->Close(); 539 entry->Close();
537 ASSERT_EQ(net::OK, OpenEntry(second, &entry)); 540 ASSERT_EQ(net::OK, OpenEntry(second, &entry));
538 EXPECT_EQ(cache_size / 10, entry->GetDataSize(0)); 541 EXPECT_EQ(cache_size / 10, entry->GetDataSize(0));
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 InitCache(); 692 InitCache();
690 693
691 std::string key("Some key"); 694 std::string key("Some key");
692 disk_cache::Entry* entry; 695 disk_cache::Entry* entry;
693 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 696 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
694 697
695 const int kSize = 50; 698 const int kSize = 50;
696 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); 699 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
697 memset(buffer1->data(), 0, kSize); 700 memset(buffer1->data(), 0, kSize);
698 base::strlcpy(buffer1->data(), "And the data to save", kSize); 701 base::strlcpy(buffer1->data(), "And the data to save", kSize);
699 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer1, kSize, false)); 702 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer1.get(), kSize, false));
700 entry->Close(); 703 entry->Close();
701 SimulateCrash(); 704 SimulateCrash();
702 705
703 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); 706 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
704 707
705 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize)); 708 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize));
706 memset(buffer2->data(), 0, kSize); 709 memset(buffer2->data(), 0, kSize);
707 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer2, kSize)); 710 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer2.get(), kSize));
708 entry->Close(); 711 entry->Close();
709 EXPECT_STREQ(buffer1->data(), buffer2->data()); 712 EXPECT_STREQ(buffer1->data(), buffer2->data());
710 } 713 }
711 714
712 TEST_F(DiskCacheBackendTest, ValidEntry) { 715 TEST_F(DiskCacheBackendTest, ValidEntry) {
713 BackendValidEntry(); 716 BackendValidEntry();
714 } 717 }
715 718
716 TEST_F(DiskCacheBackendTest, NewEvictionValidEntry) { 719 TEST_F(DiskCacheBackendTest, NewEvictionValidEntry) {
717 SetNewEviction(); 720 SetNewEviction();
718 BackendValidEntry(); 721 BackendValidEntry();
719 } 722 }
720 723
721 // The same logic of the previous test (ValidEntry), but this time force the 724 // The same logic of the previous test (ValidEntry), but this time force the
722 // entry to be invalid, simulating a crash in the middle. 725 // entry to be invalid, simulating a crash in the middle.
723 // We'll be leaking memory from this test. 726 // We'll be leaking memory from this test.
724 void DiskCacheBackendTest::BackendInvalidEntry() { 727 void DiskCacheBackendTest::BackendInvalidEntry() {
725 InitCache(); 728 InitCache();
726 729
727 std::string key("Some key"); 730 std::string key("Some key");
728 disk_cache::Entry* entry; 731 disk_cache::Entry* entry;
729 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 732 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
730 733
731 const int kSize = 50; 734 const int kSize = 50;
732 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 735 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
733 memset(buffer->data(), 0, kSize); 736 memset(buffer->data(), 0, kSize);
734 base::strlcpy(buffer->data(), "And the data to save", kSize); 737 base::strlcpy(buffer->data(), "And the data to save", kSize);
735 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, false)); 738 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false));
736 SimulateCrash(); 739 SimulateCrash();
737 740
738 EXPECT_NE(net::OK, OpenEntry(key, &entry)); 741 EXPECT_NE(net::OK, OpenEntry(key, &entry));
739 EXPECT_EQ(0, cache_->GetEntryCount()); 742 EXPECT_EQ(0, cache_->GetEntryCount());
740 } 743 }
741 744
742 // This and the other intentionally leaky tests below are excluded from 745 // This and the other intentionally leaky tests below are excluded from
743 // valgrind runs by naming them in the files 746 // valgrind runs by naming them in the files
744 // net/data/valgrind/net_unittests.gtest.txt 747 // net/data/valgrind/net_unittests.gtest.txt
745 // The scripts tools/valgrind/chrome_tests.sh 748 // The scripts tools/valgrind/chrome_tests.sh
(...skipping 26 matching lines...) Expand all
772 InitCache(); 775 InitCache();
773 776
774 std::string key("Some key"); 777 std::string key("Some key");
775 disk_cache::Entry* entry; 778 disk_cache::Entry* entry;
776 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 779 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
777 780
778 const int kSize = 50; 781 const int kSize = 50;
779 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 782 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
780 memset(buffer->data(), 0, kSize); 783 memset(buffer->data(), 0, kSize);
781 base::strlcpy(buffer->data(), "And the data to save", kSize); 784 base::strlcpy(buffer->data(), "And the data to save", kSize);
782 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, false)); 785 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false));
783 entry->Close(); 786 entry->Close();
784 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); 787 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
785 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer, kSize)); 788 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer.get(), kSize));
786 789
787 SimulateCrash(); 790 SimulateCrash();
788 791
789 if (type_ == net::APP_CACHE) { 792 if (type_ == net::APP_CACHE) {
790 // Reading an entry and crashing should not make it dirty. 793 // Reading an entry and crashing should not make it dirty.
791 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); 794 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
792 EXPECT_EQ(1, cache_->GetEntryCount()); 795 EXPECT_EQ(1, cache_->GetEntryCount());
793 entry->Close(); 796 entry->Close();
794 } else { 797 } else {
795 EXPECT_NE(net::OK, OpenEntry(key, &entry)); 798 EXPECT_NE(net::OK, OpenEntry(key, &entry));
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
898 SetMaxSize(kSize * 10); 901 SetMaxSize(kSize * 10);
899 InitCache(); 902 InitCache();
900 903
901 std::string first("some key"); 904 std::string first("some key");
902 std::string second("something else"); 905 std::string second("something else");
903 disk_cache::Entry* entry; 906 disk_cache::Entry* entry;
904 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); 907 ASSERT_EQ(net::OK, CreateEntry(first, &entry));
905 908
906 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 909 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
907 memset(buffer->data(), 0, kSize); 910 memset(buffer->data(), 0, kSize);
908 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, false)); 911 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false));
909 912
910 // Simulate a crash. 913 // Simulate a crash.
911 SimulateCrash(); 914 SimulateCrash();
912 915
913 ASSERT_EQ(net::OK, CreateEntry(second, &entry)); 916 ASSERT_EQ(net::OK, CreateEntry(second, &entry));
914 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, false)); 917 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false));
915 918
916 EXPECT_EQ(2, cache_->GetEntryCount()); 919 EXPECT_EQ(2, cache_->GetEntryCount());
917 SetMaxSize(kSize); 920 SetMaxSize(kSize);
918 entry->Close(); // Trim the cache. 921 entry->Close(); // Trim the cache.
919 FlushQueueForTest(); 922 FlushQueueForTest();
920 923
921 // If we evicted the entry in less than 20mS, we have one entry in the cache; 924 // If we evicted the entry in less than 20mS, we have one entry in the cache;
922 // if it took more than that, we posted a task and we'll delete the second 925 // if it took more than that, we posted a task and we'll delete the second
923 // entry too. 926 // entry too.
924 base::MessageLoop::current()->RunUntilIdle(); 927 base::MessageLoop::current()->RunUntilIdle();
(...skipping 28 matching lines...) Expand all
953 InitCache(); 956 InitCache();
954 957
955 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 958 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
956 memset(buffer->data(), 0, kSize); 959 memset(buffer->data(), 0, kSize);
957 disk_cache::Entry* entry; 960 disk_cache::Entry* entry;
958 961
959 // Writing 32 entries to this cache chains most of them. 962 // Writing 32 entries to this cache chains most of them.
960 for (int i = 0; i < 32; i++) { 963 for (int i = 0; i < 32; i++) {
961 std::string key(base::StringPrintf("some key %d", i)); 964 std::string key(base::StringPrintf("some key %d", i));
962 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 965 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
963 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, false)); 966 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false));
964 entry->Close(); 967 entry->Close();
965 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); 968 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
966 // Note that we are not closing the entries. 969 // Note that we are not closing the entries.
967 } 970 }
968 971
969 // Simulate a crash. 972 // Simulate a crash.
970 SimulateCrash(); 973 SimulateCrash();
971 974
972 ASSERT_EQ(net::OK, CreateEntry("Something else", &entry)); 975 ASSERT_EQ(net::OK, CreateEntry("Something else", &entry));
973 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, false)); 976 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false));
974 977
975 FlushQueueForTest(); 978 FlushQueueForTest();
976 EXPECT_EQ(33, cache_->GetEntryCount()); 979 EXPECT_EQ(33, cache_->GetEntryCount());
977 SetMaxSize(kSize); 980 SetMaxSize(kSize);
978 981
979 // For the new eviction code, all corrupt entries are on the second list so 982 // For the new eviction code, all corrupt entries are on the second list so
980 // they are not going away that easy. 983 // they are not going away that easy.
981 if (new_eviction_) { 984 if (new_eviction_) {
982 EXPECT_EQ(net::OK, DoomAllEntries()); 985 EXPECT_EQ(net::OK, DoomAllEntries());
983 } 986 }
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
1156 InitCache(); 1159 InitCache();
1157 const std::string first("first"); 1160 const std::string first("first");
1158 const std::string second("second"); 1161 const std::string second("second");
1159 disk_cache::Entry *entry1, *entry2; 1162 disk_cache::Entry *entry1, *entry2;
1160 const int kSize = 50; 1163 const int kSize = 50;
1161 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); 1164 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
1162 1165
1163 ASSERT_EQ(net::OK, CreateEntry(first, &entry1)); 1166 ASSERT_EQ(net::OK, CreateEntry(first, &entry1));
1164 memset(buffer1->data(), 0, kSize); 1167 memset(buffer1->data(), 0, kSize);
1165 base::strlcpy(buffer1->data(), "And the data to save", kSize); 1168 base::strlcpy(buffer1->data(), "And the data to save", kSize);
1166 EXPECT_EQ(kSize, WriteData(entry1, 0, 0, buffer1, kSize, false)); 1169 EXPECT_EQ(kSize, WriteData(entry1, 0, 0, buffer1.get(), kSize, false));
1167 1170
1168 ASSERT_EQ(net::OK, CreateEntry(second, &entry2)); 1171 ASSERT_EQ(net::OK, CreateEntry(second, &entry2));
1169 entry2->Close(); 1172 entry2->Close();
1170 1173
1171 FlushQueueForTest(); 1174 FlushQueueForTest();
1172 1175
1173 // Make sure that the timestamp is not the same. 1176 // Make sure that the timestamp is not the same.
1174 AddDelay(); 1177 AddDelay();
1175 1178
1176 // Read from the last item in the LRU. 1179 // Read from the last item in the LRU.
1177 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1, kSize)); 1180 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1.get(), kSize));
1178 entry1->Close(); 1181 entry1->Close();
1179 1182
1180 void* iter = NULL; 1183 void* iter = NULL;
1181 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2)); 1184 ASSERT_EQ(net::OK, OpenNextEntry(&iter, &entry2));
1182 EXPECT_EQ(entry2->GetKey(), second); 1185 EXPECT_EQ(entry2->GetKey(), second);
1183 entry2->Close(); 1186 entry2->Close();
1184 cache_->EndEnumeration(&iter); 1187 cache_->EndEnumeration(&iter);
1185 } 1188 }
1186 1189
1187 // Verify handling of invalid entries while doing enumerations. 1190 // Verify handling of invalid entries while doing enumerations.
1188 // We'll be leaking memory from this test. 1191 // We'll be leaking memory from this test.
1189 void DiskCacheBackendTest::BackendInvalidEntryEnumeration() { 1192 void DiskCacheBackendTest::BackendInvalidEntryEnumeration() {
1190 InitCache(); 1193 InitCache();
1191 1194
1192 std::string key("Some key"); 1195 std::string key("Some key");
1193 disk_cache::Entry *entry, *entry1, *entry2; 1196 disk_cache::Entry *entry, *entry1, *entry2;
1194 ASSERT_EQ(net::OK, CreateEntry(key, &entry1)); 1197 ASSERT_EQ(net::OK, CreateEntry(key, &entry1));
1195 1198
1196 const int kSize = 50; 1199 const int kSize = 50;
1197 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); 1200 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
1198 memset(buffer1->data(), 0, kSize); 1201 memset(buffer1->data(), 0, kSize);
1199 base::strlcpy(buffer1->data(), "And the data to save", kSize); 1202 base::strlcpy(buffer1->data(), "And the data to save", kSize);
1200 EXPECT_EQ(kSize, WriteData(entry1, 0, 0, buffer1, kSize, false)); 1203 EXPECT_EQ(kSize, WriteData(entry1, 0, 0, buffer1.get(), kSize, false));
1201 entry1->Close(); 1204 entry1->Close();
1202 ASSERT_EQ(net::OK, OpenEntry(key, &entry1)); 1205 ASSERT_EQ(net::OK, OpenEntry(key, &entry1));
1203 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1, kSize)); 1206 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1.get(), kSize));
1204 1207
1205 std::string key2("Another key"); 1208 std::string key2("Another key");
1206 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2)); 1209 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2));
1207 entry2->Close(); 1210 entry2->Close();
1208 ASSERT_EQ(2, cache_->GetEntryCount()); 1211 ASSERT_EQ(2, cache_->GetEntryCount());
1209 1212
1210 SimulateCrash(); 1213 SimulateCrash();
1211 1214
1212 void* iter = NULL; 1215 void* iter = NULL;
1213 int count = 0; 1216 int count = 0;
(...skipping 1180 matching lines...) Expand 10 before | Expand all | Expand 10 after
2394 CacheTestFillBuffer(key2, sizeof(key2), true); 2397 CacheTestFillBuffer(key2, sizeof(key2), true);
2395 CacheTestFillBuffer(key3, sizeof(key3), true); 2398 CacheTestFillBuffer(key3, sizeof(key3), true);
2396 key2[sizeof(key2) - 1] = '\0'; 2399 key2[sizeof(key2) - 1] = '\0';
2397 key3[sizeof(key3) - 1] = '\0'; 2400 key3[sizeof(key3) - 1] = '\0';
2398 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2)); 2401 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2));
2399 ASSERT_EQ(net::OK, CreateEntry(key3, &entry3)); 2402 ASSERT_EQ(net::OK, CreateEntry(key3, &entry3));
2400 2403
2401 const int kBufSize = 20000; 2404 const int kBufSize = 20000;
2402 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufSize)); 2405 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufSize));
2403 memset(buf->data(), 0, kBufSize); 2406 memset(buf->data(), 0, kBufSize);
2404 EXPECT_EQ(100, WriteData(entry2, 0, 0, buf, 100, false)); 2407 EXPECT_EQ(100, WriteData(entry2, 0, 0, buf.get(), 100, false));
2405 EXPECT_EQ(kBufSize, WriteData(entry3, 0, 0, buf, kBufSize, false)); 2408 EXPECT_EQ(kBufSize, WriteData(entry3, 0, 0, buf.get(), kBufSize, false));
2406 2409
2407 // This line should disable the cache but not delete it. 2410 // This line should disable the cache but not delete it.
2408 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry4)); 2411 EXPECT_NE(net::OK, OpenNextEntry(&iter, &entry4));
2409 EXPECT_EQ(0, cache_->GetEntryCount()); 2412 EXPECT_EQ(0, cache_->GetEntryCount());
2410 2413
2411 EXPECT_NE(net::OK, CreateEntry("cache is disabled", &entry4)); 2414 EXPECT_NE(net::OK, CreateEntry("cache is disabled", &entry4));
2412 2415
2413 EXPECT_EQ(100, ReadData(entry2, 0, 0, buf, 100)); 2416 EXPECT_EQ(100, ReadData(entry2, 0, 0, buf.get(), 100));
2414 EXPECT_EQ(100, WriteData(entry2, 0, 0, buf, 100, false)); 2417 EXPECT_EQ(100, WriteData(entry2, 0, 0, buf.get(), 100, false));
2415 EXPECT_EQ(100, WriteData(entry2, 1, 0, buf, 100, false)); 2418 EXPECT_EQ(100, WriteData(entry2, 1, 0, buf.get(), 100, false));
2416 2419
2417 EXPECT_EQ(kBufSize, ReadData(entry3, 0, 0, buf, kBufSize)); 2420 EXPECT_EQ(kBufSize, ReadData(entry3, 0, 0, buf.get(), kBufSize));
2418 EXPECT_EQ(kBufSize, WriteData(entry3, 0, 0, buf, kBufSize, false)); 2421 EXPECT_EQ(kBufSize, WriteData(entry3, 0, 0, buf.get(), kBufSize, false));
2419 EXPECT_EQ(kBufSize, WriteData(entry3, 1, 0, buf, kBufSize, false)); 2422 EXPECT_EQ(kBufSize, WriteData(entry3, 1, 0, buf.get(), kBufSize, false));
2420 2423
2421 std::string key = entry2->GetKey(); 2424 std::string key = entry2->GetKey();
2422 EXPECT_EQ(sizeof(key2) - 1, key.size()); 2425 EXPECT_EQ(sizeof(key2) - 1, key.size());
2423 key = entry3->GetKey(); 2426 key = entry3->GetKey();
2424 EXPECT_EQ(sizeof(key3) - 1, key.size()); 2427 EXPECT_EQ(sizeof(key3) - 1, key.size());
2425 2428
2426 entry1->Close(); 2429 entry1->Close();
2427 entry2->Close(); 2430 entry2->Close();
2428 entry3->Close(); 2431 entry3->Close();
2429 FlushQueueForTest(); // Flushing the Close posts a task to restart the cache. 2432 FlushQueueForTest(); // Flushing the Close posts a task to restart the cache.
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
2668 disk_cache::Entry* entry; 2671 disk_cache::Entry* entry;
2669 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 2672 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
2670 2673
2671 const int kSize = 200; 2674 const int kSize = 200;
2672 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 2675 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
2673 CacheTestFillBuffer(buffer->data(), kSize, true); 2676 CacheTestFillBuffer(buffer->data(), kSize, true);
2674 2677
2675 for (int i = 0; i < 10; i++) { 2678 for (int i = 0; i < 10; i++) {
2676 SCOPED_TRACE(i); 2679 SCOPED_TRACE(i);
2677 // Allocate 2MB for this entry. 2680 // Allocate 2MB for this entry.
2678 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer, kSize, true)); 2681 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, true));
2679 EXPECT_EQ(kSize, WriteData(entry, 1, 0, buffer, kSize, true)); 2682 EXPECT_EQ(kSize, WriteData(entry, 1, 0, buffer.get(), kSize, true));
2680 EXPECT_EQ(kSize, WriteData(entry, 0, 1024 * 1024, buffer, kSize, false)); 2683 EXPECT_EQ(kSize,
2681 EXPECT_EQ(kSize, WriteData(entry, 1, 1024 * 1024, buffer, kSize, false)); 2684 WriteData(entry, 0, 1024 * 1024, buffer.get(), kSize, false));
2685 EXPECT_EQ(kSize,
2686 WriteData(entry, 1, 1024 * 1024, buffer.get(), kSize, false));
2682 2687
2683 // Delete one of the buffers and truncate the other. 2688 // Delete one of the buffers and truncate the other.
2684 EXPECT_EQ(0, WriteData(entry, 0, 0, buffer, 0, true)); 2689 EXPECT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, true));
2685 EXPECT_EQ(0, WriteData(entry, 1, 10, buffer, 0, true)); 2690 EXPECT_EQ(0, WriteData(entry, 1, 10, buffer.get(), 0, true));
2686 2691
2687 // Delete the second buffer, writing 10 bytes to disk. 2692 // Delete the second buffer, writing 10 bytes to disk.
2688 entry->Close(); 2693 entry->Close();
2689 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); 2694 ASSERT_EQ(net::OK, OpenEntry(key, &entry));
2690 } 2695 }
2691 2696
2692 entry->Close(); 2697 entry->Close();
2693 EXPECT_EQ(0, cache_impl_->GetTotalBuffersSize()); 2698 EXPECT_EQ(0, cache_impl_->GetTotalBuffersSize());
2694 } 2699 }
2695 2700
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
2967 // Tests that the Simple Cache Backend fails to initialize with non-matching 2972 // Tests that the Simple Cache Backend fails to initialize with non-matching
2968 // file structure on disk. 2973 // file structure on disk.
2969 TEST_F(DiskCacheBackendTest, SimpleCacheOverBlockfileCache) { 2974 TEST_F(DiskCacheBackendTest, SimpleCacheOverBlockfileCache) {
2970 // Create a cache structure with the |BackendImpl|. 2975 // Create a cache structure with the |BackendImpl|.
2971 InitCache(); 2976 InitCache();
2972 disk_cache::Entry* entry; 2977 disk_cache::Entry* entry;
2973 const int kSize = 50; 2978 const int kSize = 50;
2974 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 2979 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
2975 CacheTestFillBuffer(buffer->data(), kSize, false); 2980 CacheTestFillBuffer(buffer->data(), kSize, false);
2976 ASSERT_EQ(net::OK, CreateEntry("key", &entry)); 2981 ASSERT_EQ(net::OK, CreateEntry("key", &entry));
2977 ASSERT_EQ(0, WriteData(entry, 0, 0, buffer, 0, false)); 2982 ASSERT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, false));
2978 entry->Close(); 2983 entry->Close();
2979 delete cache_; 2984 delete cache_;
2980 cache_ = NULL; 2985 cache_ = NULL;
2981 2986
2982 // Check that the |SimpleBackendImpl| does not favor this structure. 2987 // Check that the |SimpleBackendImpl| does not favor this structure.
2983 base::Thread cache_thread("CacheThread"); 2988 base::Thread cache_thread("CacheThread");
2984 ASSERT_TRUE(cache_thread.StartWithOptions( 2989 ASSERT_TRUE(cache_thread.StartWithOptions(
2985 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); 2990 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
2986 disk_cache::SimpleBackendImpl* simple_cache = 2991 disk_cache::SimpleBackendImpl* simple_cache =
2987 new disk_cache::SimpleBackendImpl(cache_path_, 0, net::DISK_CACHE, 2992 new disk_cache::SimpleBackendImpl(cache_path_, 0, net::DISK_CACHE,
(...skipping 10 matching lines...) Expand all
2998 // generated by the Simple Cache Backend. 3003 // generated by the Simple Cache Backend.
2999 TEST_F(DiskCacheBackendTest, BlockfileCacheOverSimpleCache) { 3004 TEST_F(DiskCacheBackendTest, BlockfileCacheOverSimpleCache) {
3000 // Create a cache structure with the |SimpleBackendImpl|. 3005 // Create a cache structure with the |SimpleBackendImpl|.
3001 SetSimpleCacheMode(); 3006 SetSimpleCacheMode();
3002 InitCache(); 3007 InitCache();
3003 disk_cache::Entry* entry; 3008 disk_cache::Entry* entry;
3004 const int kSize = 50; 3009 const int kSize = 50;
3005 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); 3010 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
3006 CacheTestFillBuffer(buffer->data(), kSize, false); 3011 CacheTestFillBuffer(buffer->data(), kSize, false);
3007 ASSERT_EQ(net::OK, CreateEntry("key", &entry)); 3012 ASSERT_EQ(net::OK, CreateEntry("key", &entry));
3008 ASSERT_EQ(0, WriteData(entry, 0, 0, buffer, 0, false)); 3013 ASSERT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, false));
3009 entry->Close(); 3014 entry->Close();
3010 delete cache_; 3015 delete cache_;
3011 cache_ = NULL; 3016 cache_ = NULL;
3012 3017
3013 // Check that the |BackendImpl| does not favor this structure. 3018 // Check that the |BackendImpl| does not favor this structure.
3014 base::Thread cache_thread("CacheThread"); 3019 base::Thread cache_thread("CacheThread");
3015 ASSERT_TRUE(cache_thread.StartWithOptions( 3020 ASSERT_TRUE(cache_thread.StartWithOptions(
3016 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); 3021 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
3017 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl( 3022 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl(
3018 cache_path_, base::MessageLoopProxy::current(), NULL); 3023 cache_path_, base::MessageLoopProxy::current(), NULL);
3019 cache->SetUnitTestMode(); 3024 cache->SetUnitTestMode();
3020 net::TestCompletionCallback cb; 3025 net::TestCompletionCallback cb;
3021 int rv = cache->Init(cb.callback()); 3026 int rv = cache->Init(cb.callback());
3022 EXPECT_NE(net::OK, cb.GetResult(rv)); 3027 EXPECT_NE(net::OK, cb.GetResult(rv));
3023 delete cache; 3028 delete cache;
3024 DisableIntegrityCheck(); 3029 DisableIntegrityCheck();
3025 } 3030 }
3026 3031
3027 #endif // !defined(OS_WIN) 3032 #endif // !defined(OS_WIN)
OLDNEW
« no previous file with comments | « net/disk_cache/backend_impl.cc ('k') | net/disk_cache/block_files.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698