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