| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "base/platform_thread.h" | 8 #include "base/platform_thread.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "net/base/io_buffer.h" | |
| 11 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 12 #include "net/disk_cache/backend_impl.h" | 11 #include "net/disk_cache/backend_impl.h" |
| 13 #include "net/disk_cache/disk_cache_test_base.h" | 12 #include "net/disk_cache/disk_cache_test_base.h" |
| 14 #include "net/disk_cache/disk_cache_test_util.h" | 13 #include "net/disk_cache/disk_cache_test_util.h" |
| 15 #include "net/disk_cache/mapped_file.h" | 14 #include "net/disk_cache/mapped_file.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 16 |
| 18 using base::Time; | 17 using base::Time; |
| 19 | 18 |
| 20 namespace { | 19 namespace { |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 InitCache(); | 202 InitCache(); |
| 204 BackendKeying(); | 203 BackendKeying(); |
| 205 } | 204 } |
| 206 | 205 |
| 207 TEST_F(DiskCacheBackendTest, ExternalFiles) { | 206 TEST_F(DiskCacheBackendTest, ExternalFiles) { |
| 208 InitCache(); | 207 InitCache(); |
| 209 // First, lets create a file on the folder. | 208 // First, lets create a file on the folder. |
| 210 std::wstring filename = GetCachePath(); | 209 std::wstring filename = GetCachePath(); |
| 211 file_util::AppendToPath(&filename, L"f_000001"); | 210 file_util::AppendToPath(&filename, L"f_000001"); |
| 212 | 211 |
| 213 const int kSize = 50; | 212 const int kDataSize = 50; |
| 214 scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize); | 213 char data[kDataSize]; |
| 215 CacheTestFillBuffer(buffer1->data(), kSize, false); | 214 CacheTestFillBuffer(data, kDataSize, false); |
| 216 ASSERT_EQ(kSize, file_util::WriteFile(filename, buffer1->data(), kSize)); | 215 ASSERT_EQ(kDataSize, file_util::WriteFile(filename, data, kDataSize)); |
| 217 | 216 |
| 218 // Now let's create a file with the cache. | 217 // Now let's create a file with the cache. |
| 219 disk_cache::Entry* entry; | 218 disk_cache::Entry* entry; |
| 220 ASSERT_TRUE(cache_->CreateEntry("key", &entry)); | 219 ASSERT_TRUE(cache_->CreateEntry("key", &entry)); |
| 221 ASSERT_EQ(0, entry->WriteData(0, 20000, buffer1, 0, NULL, false)); | 220 ASSERT_EQ(0, entry->WriteData(0, 20000, data, 0, NULL, false)); |
| 222 entry->Close(); | 221 entry->Close(); |
| 223 | 222 |
| 224 // And verify that the first file is still there. | 223 // And verify that the first file is still there. |
| 225 scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kSize); | 224 char buffer[kDataSize]; |
| 226 ASSERT_EQ(kSize, file_util::ReadFile(filename, buffer2->data(), kSize)); | 225 ASSERT_EQ(kDataSize, file_util::ReadFile(filename, buffer, kDataSize)); |
| 227 EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kSize)); | 226 EXPECT_EQ(0, memcmp(data, buffer, kDataSize)); |
| 228 } | 227 } |
| 229 | 228 |
| 230 void DiskCacheBackendTest::BackendSetSize() { | 229 void DiskCacheBackendTest::BackendSetSize() { |
| 231 SetDirectMode(); | 230 SetDirectMode(); |
| 232 const int cache_size = 0x10000; // 64 kB | 231 const int cache_size = 0x10000; // 64 kB |
| 233 SetMaxSize(cache_size); | 232 SetMaxSize(cache_size); |
| 234 InitCache(); | 233 InitCache(); |
| 235 | 234 |
| 236 std::string first("some key"); | 235 std::string first("some key"); |
| 237 std::string second("something else"); | 236 std::string second("something else"); |
| 238 disk_cache::Entry* entry; | 237 disk_cache::Entry* entry; |
| 239 ASSERT_TRUE(cache_->CreateEntry(first, &entry)); | 238 ASSERT_TRUE(cache_->CreateEntry(first, &entry)); |
| 240 | 239 |
| 241 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(cache_size); | 240 char buffer[cache_size] = {0}; |
| 242 memset(buffer->data(), 0, cache_size); | |
| 243 EXPECT_EQ(cache_size / 10, entry->WriteData(0, 0, buffer, cache_size / 10, | 241 EXPECT_EQ(cache_size / 10, entry->WriteData(0, 0, buffer, cache_size / 10, |
| 244 NULL, false)) << "normal file"; | 242 NULL, false)) << "normal file"; |
| 245 | 243 |
| 246 EXPECT_EQ(net::ERR_FAILED, entry->WriteData(1, 0, buffer, cache_size / 5, | 244 EXPECT_EQ(net::ERR_FAILED, entry->WriteData(1, 0, buffer, cache_size / 5, |
| 247 NULL, false)) << "file size above the limit"; | 245 NULL, false)) << "file size above the limit"; |
| 248 | 246 |
| 249 // By doubling the total size, we make this file cacheable. | 247 // By doubling the total size, we make this file cacheable. |
| 250 SetMaxSize(cache_size * 2); | 248 SetMaxSize(cache_size * 2); |
| 251 EXPECT_EQ(cache_size / 5, entry->WriteData(1, 0, buffer, cache_size / 5, | 249 EXPECT_EQ(cache_size / 5, entry->WriteData(1, 0, buffer, cache_size / 5, |
| 252 NULL, false)); | 250 NULL, false)); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 | 331 |
| 334 // Before looking for invalid entries, let's check a valid entry. | 332 // Before looking for invalid entries, let's check a valid entry. |
| 335 TEST_F(DiskCacheBackendTest, ValidEntry) { | 333 TEST_F(DiskCacheBackendTest, ValidEntry) { |
| 336 SetDirectMode(); | 334 SetDirectMode(); |
| 337 InitCache(); | 335 InitCache(); |
| 338 | 336 |
| 339 std::string key("Some key"); | 337 std::string key("Some key"); |
| 340 disk_cache::Entry* entry1; | 338 disk_cache::Entry* entry1; |
| 341 ASSERT_TRUE(cache_->CreateEntry(key, &entry1)); | 339 ASSERT_TRUE(cache_->CreateEntry(key, &entry1)); |
| 342 | 340 |
| 343 const int kSize = 50; | 341 char data[] = "And the data to save"; |
| 344 scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize); | 342 EXPECT_TRUE(sizeof(data) == entry1->WriteData(0, 0, data, sizeof(data), NULL, |
| 345 base::strlcpy(buffer1->data(), "And the data to save", kSize); | 343 false)); |
| 346 EXPECT_EQ(kSize, entry1->WriteData(0, 0, buffer1, kSize, NULL, false)); | |
| 347 entry1->Close(); | 344 entry1->Close(); |
| 348 SimulateCrash(); | 345 SimulateCrash(); |
| 349 | 346 |
| 350 ASSERT_TRUE(cache_->OpenEntry(key, &entry1)); | 347 ASSERT_TRUE(cache_->OpenEntry(key, &entry1)); |
| 351 | 348 |
| 352 scoped_refptr<net::IOBuffer> buffer2 = new net::IOBuffer(kSize); | 349 char buffer[40]; |
| 353 memset(buffer2->data(), 0, kSize); | 350 memset(buffer, 0, sizeof(buffer)); |
| 354 EXPECT_EQ(kSize, entry1->ReadData(0, 0, buffer2, kSize, NULL)); | 351 EXPECT_TRUE(sizeof(data) == entry1->ReadData(0, 0, buffer, sizeof(data), |
| 352 NULL)); |
| 355 entry1->Close(); | 353 entry1->Close(); |
| 356 EXPECT_STREQ(buffer1->data(), buffer2->data()); | 354 EXPECT_STREQ(data, buffer); |
| 357 } | 355 } |
| 358 | 356 |
| 359 // The same logic of the previous test (ValidEntry), but this time force the | 357 // The same logic of the previous test (ValidEntry), but this time force the |
| 360 // entry to be invalid, simulating a crash in the middle. | 358 // entry to be invalid, simulating a crash in the middle. |
| 361 // We'll be leaking memory from this test. | 359 // We'll be leaking memory from this test. |
| 362 TEST_F(DiskCacheBackendTest, InvalidEntry) { | 360 TEST_F(DiskCacheBackendTest, InvalidEntry) { |
| 363 // Use the implementation directly... we need to simulate a crash. | 361 // Use the implementation directly... we need to simulate a crash. |
| 364 SetDirectMode(); | 362 SetDirectMode(); |
| 365 InitCache(); | 363 InitCache(); |
| 366 | 364 |
| 367 std::string key("Some key"); | 365 std::string key("Some key"); |
| 368 disk_cache::Entry* entry1; | 366 disk_cache::Entry* entry1; |
| 369 ASSERT_TRUE(cache_->CreateEntry(key, &entry1)); | 367 ASSERT_TRUE(cache_->CreateEntry(key, &entry1)); |
| 370 | 368 |
| 371 const int kSize = 50; | 369 char data[] = "And the data to save"; |
| 372 scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize); | 370 EXPECT_TRUE(sizeof(data) == entry1->WriteData(0, 0, data, sizeof(data), NULL, |
| 373 base::strlcpy(buffer1->data(), "And the data to save", kSize); | 371 false)); |
| 374 EXPECT_EQ(kSize, entry1->WriteData(0, 0, buffer1, kSize, NULL, false)); | |
| 375 SimulateCrash(); | 372 SimulateCrash(); |
| 376 | 373 |
| 377 EXPECT_FALSE(cache_->OpenEntry(key, &entry1)); | 374 EXPECT_FALSE(cache_->OpenEntry(key, &entry1)); |
| 378 EXPECT_EQ(0, cache_->GetEntryCount()); | 375 EXPECT_EQ(0, cache_->GetEntryCount()); |
| 379 } | 376 } |
| 380 | 377 |
| 381 // Almost the same test, but this time crash the cache after reading an entry. | 378 // Almost the same test, but this time crash the cache after reading an entry. |
| 382 // We'll be leaking memory from this test. | 379 // We'll be leaking memory from this test. |
| 383 TEST_F(DiskCacheBackendTest, InvalidEntryRead) { | 380 TEST_F(DiskCacheBackendTest, InvalidEntryRead) { |
| 384 // Use the implementation directly... we need to simulate a crash. | 381 // Use the implementation directly... we need to simulate a crash. |
| 385 SetDirectMode(); | 382 SetDirectMode(); |
| 386 InitCache(); | 383 InitCache(); |
| 387 | 384 |
| 388 std::string key("Some key"); | 385 std::string key("Some key"); |
| 389 disk_cache::Entry* entry1; | 386 disk_cache::Entry* entry1; |
| 390 ASSERT_TRUE(cache_->CreateEntry(key, &entry1)); | 387 ASSERT_TRUE(cache_->CreateEntry(key, &entry1)); |
| 391 | 388 |
| 392 const int kSize = 50; | 389 char data[] = "And the data to save"; |
| 393 scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize); | 390 EXPECT_TRUE(sizeof(data) == entry1->WriteData(0, 0, data, sizeof(data), NULL, |
| 394 base::strlcpy(buffer1->data(), "And the data to save", kSize); | 391 false)); |
| 395 EXPECT_EQ(kSize, entry1->WriteData(0, 0, buffer1, kSize, NULL, false)); | |
| 396 entry1->Close(); | 392 entry1->Close(); |
| 397 ASSERT_TRUE(cache_->OpenEntry(key, &entry1)); | 393 ASSERT_TRUE(cache_->OpenEntry(key, &entry1)); |
| 398 EXPECT_EQ(kSize, entry1->ReadData(0, 0, buffer1, kSize, NULL)); | 394 EXPECT_TRUE(sizeof(data) == entry1->ReadData(0, 0, data, sizeof(data), NULL)); |
| 399 | 395 |
| 400 SimulateCrash(); | 396 SimulateCrash(); |
| 401 | 397 |
| 402 EXPECT_FALSE(cache_->OpenEntry(key, &entry1)); | 398 EXPECT_FALSE(cache_->OpenEntry(key, &entry1)); |
| 403 EXPECT_EQ(0, cache_->GetEntryCount()); | 399 EXPECT_EQ(0, cache_->GetEntryCount()); |
| 404 } | 400 } |
| 405 | 401 |
| 406 // We'll be leaking memory from this test. | 402 // We'll be leaking memory from this test. |
| 407 TEST_F(DiskCacheBackendTest, InvalidEntryWithLoad) { | 403 TEST_F(DiskCacheBackendTest, InvalidEntryWithLoad) { |
| 408 // Work with a tiny index table (16 entries) | 404 // Work with a tiny index table (16 entries) |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 | 455 |
| 460 const int cache_size = 0x4000; // 16 kB | 456 const int cache_size = 0x4000; // 16 kB |
| 461 SetMaxSize(cache_size * 10); | 457 SetMaxSize(cache_size * 10); |
| 462 InitCache(); | 458 InitCache(); |
| 463 | 459 |
| 464 std::string first("some key"); | 460 std::string first("some key"); |
| 465 std::string second("something else"); | 461 std::string second("something else"); |
| 466 disk_cache::Entry* entry; | 462 disk_cache::Entry* entry; |
| 467 ASSERT_TRUE(cache_->CreateEntry(first, &entry)); | 463 ASSERT_TRUE(cache_->CreateEntry(first, &entry)); |
| 468 | 464 |
| 469 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(cache_size); | 465 char buffer[cache_size] = {0}; |
| 470 memset(buffer->data(), 0, cache_size); | |
| 471 EXPECT_EQ(cache_size * 19 / 20, entry->WriteData(0, 0, buffer, | 466 EXPECT_EQ(cache_size * 19 / 20, entry->WriteData(0, 0, buffer, |
| 472 cache_size * 19 / 20, NULL, false)); | 467 cache_size * 19 / 20, NULL, false)); |
| 473 | 468 |
| 474 // Simulate a crash. | 469 // Simulate a crash. |
| 475 SimulateCrash(); | 470 SimulateCrash(); |
| 476 | 471 |
| 477 ASSERT_TRUE(cache_->CreateEntry(second, &entry)); | 472 ASSERT_TRUE(cache_->CreateEntry(second, &entry)); |
| 478 EXPECT_EQ(cache_size / 10, entry->WriteData(0, 0, buffer, cache_size / 10, | 473 EXPECT_EQ(cache_size / 10, entry->WriteData(0, 0, buffer, cache_size / 10, |
| 479 NULL, false)) << "trim the cache"; | 474 NULL, false)) << "trim the cache"; |
| 480 entry->Close(); | 475 entry->Close(); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 // We'll be leaking memory from this test. | 542 // We'll be leaking memory from this test. |
| 548 TEST_F(DiskCacheBackendTest, InvalidEntryEnumeration) { | 543 TEST_F(DiskCacheBackendTest, InvalidEntryEnumeration) { |
| 549 // Use the implementation directly... we need to simulate a crash. | 544 // Use the implementation directly... we need to simulate a crash. |
| 550 SetDirectMode(); | 545 SetDirectMode(); |
| 551 InitCache(); | 546 InitCache(); |
| 552 | 547 |
| 553 std::string key("Some key"); | 548 std::string key("Some key"); |
| 554 disk_cache::Entry *entry, *entry1, *entry2; | 549 disk_cache::Entry *entry, *entry1, *entry2; |
| 555 ASSERT_TRUE(cache_->CreateEntry(key, &entry1)); | 550 ASSERT_TRUE(cache_->CreateEntry(key, &entry1)); |
| 556 | 551 |
| 557 const int kSize = 50; | 552 char data[] = "And the data to save"; |
| 558 scoped_refptr<net::IOBuffer> buffer1 = new net::IOBuffer(kSize); | 553 EXPECT_TRUE(sizeof(data) == entry1->WriteData(0, 0, data, sizeof(data), NULL, |
| 559 base::strlcpy(buffer1->data(), "And the data to save", kSize); | 554 false)); |
| 560 EXPECT_EQ(kSize, entry1->WriteData(0, 0, buffer1, kSize, NULL, false)); | |
| 561 entry1->Close(); | 555 entry1->Close(); |
| 562 ASSERT_TRUE(cache_->OpenEntry(key, &entry1)); | 556 ASSERT_TRUE(cache_->OpenEntry(key, &entry1)); |
| 563 EXPECT_EQ(kSize, entry1->ReadData(0, 0, buffer1, kSize, NULL)); | 557 EXPECT_TRUE(sizeof(data) == entry1->ReadData(0, 0, data, sizeof(data), NULL)); |
| 564 | 558 |
| 565 std::string key2("Another key"); | 559 std::string key2("Another key"); |
| 566 ASSERT_TRUE(cache_->CreateEntry(key2, &entry2)); | 560 ASSERT_TRUE(cache_->CreateEntry(key2, &entry2)); |
| 567 entry2->Close(); | 561 entry2->Close(); |
| 568 ASSERT_EQ(2, cache_->GetEntryCount()); | 562 ASSERT_EQ(2, cache_->GetEntryCount()); |
| 569 | 563 |
| 570 SimulateCrash(); | 564 SimulateCrash(); |
| 571 | 565 |
| 572 void* iter = NULL; | 566 void* iter = NULL; |
| 573 int count = 0; | 567 int count = 0; |
| (...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 976 InitCache(); | 970 InitCache(); |
| 977 BackendDoomAll(); | 971 BackendDoomAll(); |
| 978 } | 972 } |
| 979 | 973 |
| 980 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomAll) { | 974 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomAll) { |
| 981 SetMemoryOnlyMode(); | 975 SetMemoryOnlyMode(); |
| 982 InitCache(); | 976 InitCache(); |
| 983 BackendDoomAll(); | 977 BackendDoomAll(); |
| 984 } | 978 } |
| 985 | 979 |
| OLD | NEW |