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 <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 #include "net/disk_cache/cache_util.h" | 29 #include "net/disk_cache/cache_util.h" |
30 #include "net/disk_cache/disk_cache_test_base.h" | 30 #include "net/disk_cache/disk_cache_test_base.h" |
31 #include "net/disk_cache/disk_cache_test_util.h" | 31 #include "net/disk_cache/disk_cache_test_util.h" |
32 #include "net/disk_cache/memory/mem_backend_impl.h" | 32 #include "net/disk_cache/memory/mem_backend_impl.h" |
33 #include "net/disk_cache/simple/simple_backend_impl.h" | 33 #include "net/disk_cache/simple/simple_backend_impl.h" |
34 #include "net/disk_cache/simple/simple_entry_format.h" | 34 #include "net/disk_cache/simple/simple_entry_format.h" |
35 #include "net/disk_cache/simple/simple_index.h" | 35 #include "net/disk_cache/simple/simple_index.h" |
36 #include "net/disk_cache/simple/simple_synchronous_entry.h" | 36 #include "net/disk_cache/simple/simple_synchronous_entry.h" |
37 #include "net/disk_cache/simple/simple_test_util.h" | 37 #include "net/disk_cache/simple/simple_test_util.h" |
38 #include "net/disk_cache/simple/simple_util.h" | 38 #include "net/disk_cache/simple/simple_util.h" |
| 39 #include "net/test/gtest_util.h" |
| 40 #include "testing/gmock/include/gmock/gmock.h" |
39 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
40 | 42 |
| 43 using net::test::IsError; |
| 44 using net::test::IsOk; |
| 45 |
41 #if defined(OS_WIN) | 46 #if defined(OS_WIN) |
42 #include "base/win/scoped_handle.h" | 47 #include "base/win/scoped_handle.h" |
43 #endif | 48 #endif |
44 | 49 |
45 // Provide a BackendImpl object to macros from histogram_macros.h. | 50 // Provide a BackendImpl object to macros from histogram_macros.h. |
46 #define CACHE_UMA_BACKEND_IMPL_OBJ backend_ | 51 #define CACHE_UMA_BACKEND_IMPL_OBJ backend_ |
47 | 52 |
48 using base::Time; | 53 using base::Time; |
49 | 54 |
50 namespace { | 55 namespace { |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 // This must be greater then MemEntryImpl::kMaxSparseEntrySize. | 199 // This must be greater then MemEntryImpl::kMaxSparseEntrySize. |
195 const int kOffset = 10 + 1024 * 1024; | 200 const int kOffset = 10 + 1024 * 1024; |
196 | 201 |
197 disk_cache::Entry* entry0 = NULL; | 202 disk_cache::Entry* entry0 = NULL; |
198 disk_cache::Entry* entry1 = NULL; | 203 disk_cache::Entry* entry1 = NULL; |
199 disk_cache::Entry* entry2 = NULL; | 204 disk_cache::Entry* entry2 = NULL; |
200 | 205 |
201 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); | 206 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); |
202 CacheTestFillBuffer(buffer->data(), kSize, false); | 207 CacheTestFillBuffer(buffer->data(), kSize, false); |
203 | 208 |
204 ASSERT_EQ(net::OK, CreateEntry("zeroth", &entry0)); | 209 ASSERT_THAT(CreateEntry("zeroth", &entry0), IsOk()); |
205 ASSERT_EQ(kSize, WriteSparseData(entry0, 0, buffer.get(), kSize)); | 210 ASSERT_EQ(kSize, WriteSparseData(entry0, 0, buffer.get(), kSize)); |
206 ASSERT_EQ(kSize, | 211 ASSERT_EQ(kSize, |
207 WriteSparseData(entry0, kOffset + kSize, buffer.get(), kSize)); | 212 WriteSparseData(entry0, kOffset + kSize, buffer.get(), kSize)); |
208 entry0->Close(); | 213 entry0->Close(); |
209 | 214 |
210 FlushQueueForTest(); | 215 FlushQueueForTest(); |
211 AddDelay(); | 216 AddDelay(); |
212 if (doomed_start) | 217 if (doomed_start) |
213 *doomed_start = base::Time::Now(); | 218 *doomed_start = base::Time::Now(); |
214 | 219 |
215 // Order in rankings list: | 220 // Order in rankings list: |
216 // first_part1, first_part2, second_part1, second_part2 | 221 // first_part1, first_part2, second_part1, second_part2 |
217 ASSERT_EQ(net::OK, CreateEntry("first", &entry1)); | 222 ASSERT_THAT(CreateEntry("first", &entry1), IsOk()); |
218 ASSERT_EQ(kSize, WriteSparseData(entry1, 0, buffer.get(), kSize)); | 223 ASSERT_EQ(kSize, WriteSparseData(entry1, 0, buffer.get(), kSize)); |
219 ASSERT_EQ(kSize, | 224 ASSERT_EQ(kSize, |
220 WriteSparseData(entry1, kOffset + kSize, buffer.get(), kSize)); | 225 WriteSparseData(entry1, kOffset + kSize, buffer.get(), kSize)); |
221 entry1->Close(); | 226 entry1->Close(); |
222 | 227 |
223 ASSERT_EQ(net::OK, CreateEntry("second", &entry2)); | 228 ASSERT_THAT(CreateEntry("second", &entry2), IsOk()); |
224 ASSERT_EQ(kSize, WriteSparseData(entry2, 0, buffer.get(), kSize)); | 229 ASSERT_EQ(kSize, WriteSparseData(entry2, 0, buffer.get(), kSize)); |
225 ASSERT_EQ(kSize, | 230 ASSERT_EQ(kSize, |
226 WriteSparseData(entry2, kOffset + kSize, buffer.get(), kSize)); | 231 WriteSparseData(entry2, kOffset + kSize, buffer.get(), kSize)); |
227 entry2->Close(); | 232 entry2->Close(); |
228 | 233 |
229 FlushQueueForTest(); | 234 FlushQueueForTest(); |
230 AddDelay(); | 235 AddDelay(); |
231 if (doomed_end) | 236 if (doomed_end) |
232 *doomed_end = base::Time::Now(); | 237 *doomed_end = base::Time::Now(); |
233 | 238 |
234 // Order in rankings list: | 239 // Order in rankings list: |
235 // third_part1, fourth_part1, third_part2, fourth_part2 | 240 // third_part1, fourth_part1, third_part2, fourth_part2 |
236 disk_cache::Entry* entry3 = NULL; | 241 disk_cache::Entry* entry3 = NULL; |
237 disk_cache::Entry* entry4 = NULL; | 242 disk_cache::Entry* entry4 = NULL; |
238 ASSERT_EQ(net::OK, CreateEntry("third", &entry3)); | 243 ASSERT_THAT(CreateEntry("third", &entry3), IsOk()); |
239 ASSERT_EQ(kSize, WriteSparseData(entry3, 0, buffer.get(), kSize)); | 244 ASSERT_EQ(kSize, WriteSparseData(entry3, 0, buffer.get(), kSize)); |
240 ASSERT_EQ(net::OK, CreateEntry("fourth", &entry4)); | 245 ASSERT_THAT(CreateEntry("fourth", &entry4), IsOk()); |
241 ASSERT_EQ(kSize, WriteSparseData(entry4, 0, buffer.get(), kSize)); | 246 ASSERT_EQ(kSize, WriteSparseData(entry4, 0, buffer.get(), kSize)); |
242 ASSERT_EQ(kSize, | 247 ASSERT_EQ(kSize, |
243 WriteSparseData(entry3, kOffset + kSize, buffer.get(), kSize)); | 248 WriteSparseData(entry3, kOffset + kSize, buffer.get(), kSize)); |
244 ASSERT_EQ(kSize, | 249 ASSERT_EQ(kSize, |
245 WriteSparseData(entry4, kOffset + kSize, buffer.get(), kSize)); | 250 WriteSparseData(entry4, kOffset + kSize, buffer.get(), kSize)); |
246 entry3->Close(); | 251 entry3->Close(); |
247 entry4->Close(); | 252 entry4->Close(); |
248 | 253 |
249 FlushQueueForTest(); | 254 FlushQueueForTest(); |
250 AddDelay(); | 255 AddDelay(); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 // every stream. | 309 // every stream. |
305 return disk_cache::kSimpleEntryStreamCount * | 310 return disk_cache::kSimpleEntryStreamCount * |
306 (sizeof(disk_cache::SimpleFileHeader) + | 311 (sizeof(disk_cache::SimpleFileHeader) + |
307 sizeof(disk_cache::SimpleFileEOF) + key.size()); | 312 sizeof(disk_cache::SimpleFileEOF) + key.size()); |
308 } | 313 } |
309 | 314 |
310 void DiskCacheBackendTest::BackendBasics() { | 315 void DiskCacheBackendTest::BackendBasics() { |
311 InitCache(); | 316 InitCache(); |
312 disk_cache::Entry *entry1 = NULL, *entry2 = NULL; | 317 disk_cache::Entry *entry1 = NULL, *entry2 = NULL; |
313 EXPECT_NE(net::OK, OpenEntry("the first key", &entry1)); | 318 EXPECT_NE(net::OK, OpenEntry("the first key", &entry1)); |
314 ASSERT_EQ(net::OK, CreateEntry("the first key", &entry1)); | 319 ASSERT_THAT(CreateEntry("the first key", &entry1), IsOk()); |
315 ASSERT_TRUE(NULL != entry1); | 320 ASSERT_TRUE(NULL != entry1); |
316 entry1->Close(); | 321 entry1->Close(); |
317 entry1 = NULL; | 322 entry1 = NULL; |
318 | 323 |
319 ASSERT_EQ(net::OK, OpenEntry("the first key", &entry1)); | 324 ASSERT_THAT(OpenEntry("the first key", &entry1), IsOk()); |
320 ASSERT_TRUE(NULL != entry1); | 325 ASSERT_TRUE(NULL != entry1); |
321 entry1->Close(); | 326 entry1->Close(); |
322 entry1 = NULL; | 327 entry1 = NULL; |
323 | 328 |
324 EXPECT_NE(net::OK, CreateEntry("the first key", &entry1)); | 329 EXPECT_NE(net::OK, CreateEntry("the first key", &entry1)); |
325 ASSERT_EQ(net::OK, OpenEntry("the first key", &entry1)); | 330 ASSERT_THAT(OpenEntry("the first key", &entry1), IsOk()); |
326 EXPECT_NE(net::OK, OpenEntry("some other key", &entry2)); | 331 EXPECT_NE(net::OK, OpenEntry("some other key", &entry2)); |
327 ASSERT_EQ(net::OK, CreateEntry("some other key", &entry2)); | 332 ASSERT_THAT(CreateEntry("some other key", &entry2), IsOk()); |
328 ASSERT_TRUE(NULL != entry1); | 333 ASSERT_TRUE(NULL != entry1); |
329 ASSERT_TRUE(NULL != entry2); | 334 ASSERT_TRUE(NULL != entry2); |
330 EXPECT_EQ(2, cache_->GetEntryCount()); | 335 EXPECT_EQ(2, cache_->GetEntryCount()); |
331 | 336 |
332 disk_cache::Entry* entry3 = NULL; | 337 disk_cache::Entry* entry3 = NULL; |
333 ASSERT_EQ(net::OK, OpenEntry("some other key", &entry3)); | 338 ASSERT_THAT(OpenEntry("some other key", &entry3), IsOk()); |
334 ASSERT_TRUE(NULL != entry3); | 339 ASSERT_TRUE(NULL != entry3); |
335 EXPECT_TRUE(entry2 == entry3); | 340 EXPECT_TRUE(entry2 == entry3); |
336 EXPECT_EQ(2, cache_->GetEntryCount()); | 341 EXPECT_EQ(2, cache_->GetEntryCount()); |
337 | 342 |
338 EXPECT_EQ(net::OK, DoomEntry("some other key")); | 343 EXPECT_THAT(DoomEntry("some other key"), IsOk()); |
339 EXPECT_EQ(1, cache_->GetEntryCount()); | 344 EXPECT_EQ(1, cache_->GetEntryCount()); |
340 entry1->Close(); | 345 entry1->Close(); |
341 entry2->Close(); | 346 entry2->Close(); |
342 entry3->Close(); | 347 entry3->Close(); |
343 | 348 |
344 EXPECT_EQ(net::OK, DoomEntry("the first key")); | 349 EXPECT_THAT(DoomEntry("the first key"), IsOk()); |
345 EXPECT_EQ(0, cache_->GetEntryCount()); | 350 EXPECT_EQ(0, cache_->GetEntryCount()); |
346 | 351 |
347 ASSERT_EQ(net::OK, CreateEntry("the first key", &entry1)); | 352 ASSERT_THAT(CreateEntry("the first key", &entry1), IsOk()); |
348 ASSERT_EQ(net::OK, CreateEntry("some other key", &entry2)); | 353 ASSERT_THAT(CreateEntry("some other key", &entry2), IsOk()); |
349 entry1->Doom(); | 354 entry1->Doom(); |
350 entry1->Close(); | 355 entry1->Close(); |
351 EXPECT_EQ(net::OK, DoomEntry("some other key")); | 356 EXPECT_THAT(DoomEntry("some other key"), IsOk()); |
352 EXPECT_EQ(0, cache_->GetEntryCount()); | 357 EXPECT_EQ(0, cache_->GetEntryCount()); |
353 entry2->Close(); | 358 entry2->Close(); |
354 } | 359 } |
355 | 360 |
356 TEST_F(DiskCacheBackendTest, Basics) { | 361 TEST_F(DiskCacheBackendTest, Basics) { |
357 BackendBasics(); | 362 BackendBasics(); |
358 } | 363 } |
359 | 364 |
360 TEST_F(DiskCacheBackendTest, NewEvictionBasics) { | 365 TEST_F(DiskCacheBackendTest, NewEvictionBasics) { |
361 SetNewEviction(); | 366 SetNewEviction(); |
(...skipping 13 matching lines...) Expand all Loading... |
375 TEST_F(DiskCacheBackendTest, ShaderCacheBasics) { | 380 TEST_F(DiskCacheBackendTest, ShaderCacheBasics) { |
376 SetCacheType(net::SHADER_CACHE); | 381 SetCacheType(net::SHADER_CACHE); |
377 BackendBasics(); | 382 BackendBasics(); |
378 } | 383 } |
379 | 384 |
380 void DiskCacheBackendTest::BackendKeying() { | 385 void DiskCacheBackendTest::BackendKeying() { |
381 InitCache(); | 386 InitCache(); |
382 const char kName1[] = "the first key"; | 387 const char kName1[] = "the first key"; |
383 const char kName2[] = "the first Key"; | 388 const char kName2[] = "the first Key"; |
384 disk_cache::Entry *entry1, *entry2; | 389 disk_cache::Entry *entry1, *entry2; |
385 ASSERT_EQ(net::OK, CreateEntry(kName1, &entry1)); | 390 ASSERT_THAT(CreateEntry(kName1, &entry1), IsOk()); |
386 | 391 |
387 ASSERT_EQ(net::OK, CreateEntry(kName2, &entry2)); | 392 ASSERT_THAT(CreateEntry(kName2, &entry2), IsOk()); |
388 EXPECT_TRUE(entry1 != entry2) << "Case sensitive"; | 393 EXPECT_TRUE(entry1 != entry2) << "Case sensitive"; |
389 entry2->Close(); | 394 entry2->Close(); |
390 | 395 |
391 char buffer[30]; | 396 char buffer[30]; |
392 base::strlcpy(buffer, kName1, arraysize(buffer)); | 397 base::strlcpy(buffer, kName1, arraysize(buffer)); |
393 ASSERT_EQ(net::OK, OpenEntry(buffer, &entry2)); | 398 ASSERT_THAT(OpenEntry(buffer, &entry2), IsOk()); |
394 EXPECT_TRUE(entry1 == entry2); | 399 EXPECT_TRUE(entry1 == entry2); |
395 entry2->Close(); | 400 entry2->Close(); |
396 | 401 |
397 base::strlcpy(buffer + 1, kName1, arraysize(buffer) - 1); | 402 base::strlcpy(buffer + 1, kName1, arraysize(buffer) - 1); |
398 ASSERT_EQ(net::OK, OpenEntry(buffer + 1, &entry2)); | 403 ASSERT_THAT(OpenEntry(buffer + 1, &entry2), IsOk()); |
399 EXPECT_TRUE(entry1 == entry2); | 404 EXPECT_TRUE(entry1 == entry2); |
400 entry2->Close(); | 405 entry2->Close(); |
401 | 406 |
402 base::strlcpy(buffer + 3, kName1, arraysize(buffer) - 3); | 407 base::strlcpy(buffer + 3, kName1, arraysize(buffer) - 3); |
403 ASSERT_EQ(net::OK, OpenEntry(buffer + 3, &entry2)); | 408 ASSERT_THAT(OpenEntry(buffer + 3, &entry2), IsOk()); |
404 EXPECT_TRUE(entry1 == entry2); | 409 EXPECT_TRUE(entry1 == entry2); |
405 entry2->Close(); | 410 entry2->Close(); |
406 | 411 |
407 // Now verify long keys. | 412 // Now verify long keys. |
408 char buffer2[20000]; | 413 char buffer2[20000]; |
409 memset(buffer2, 's', sizeof(buffer2)); | 414 memset(buffer2, 's', sizeof(buffer2)); |
410 buffer2[1023] = '\0'; | 415 buffer2[1023] = '\0'; |
411 ASSERT_EQ(net::OK, CreateEntry(buffer2, &entry2)) << "key on block file"; | 416 ASSERT_EQ(net::OK, CreateEntry(buffer2, &entry2)) << "key on block file"; |
412 entry2->Close(); | 417 entry2->Close(); |
413 | 418 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 // Now test the public API. | 465 // Now test the public API. |
461 int rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, | 466 int rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, |
462 net::CACHE_BACKEND_DEFAULT, | 467 net::CACHE_BACKEND_DEFAULT, |
463 cache_path_, | 468 cache_path_, |
464 0, | 469 0, |
465 false, | 470 false, |
466 cache_thread.task_runner(), | 471 cache_thread.task_runner(), |
467 NULL, | 472 NULL, |
468 &cache, | 473 &cache, |
469 cb.callback()); | 474 cb.callback()); |
470 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 475 ASSERT_THAT(cb.GetResult(rv), IsOk()); |
471 ASSERT_TRUE(cache.get()); | 476 ASSERT_TRUE(cache.get()); |
472 cache.reset(); | 477 cache.reset(); |
473 | 478 |
474 rv = disk_cache::CreateCacheBackend(net::MEMORY_CACHE, | 479 rv = disk_cache::CreateCacheBackend(net::MEMORY_CACHE, |
475 net::CACHE_BACKEND_DEFAULT, | 480 net::CACHE_BACKEND_DEFAULT, |
476 base::FilePath(), 0, | 481 base::FilePath(), 0, |
477 false, NULL, NULL, &cache, | 482 false, NULL, NULL, &cache, |
478 cb.callback()); | 483 cb.callback()); |
479 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 484 ASSERT_THAT(cb.GetResult(rv), IsOk()); |
480 ASSERT_TRUE(cache.get()); | 485 ASSERT_TRUE(cache.get()); |
481 cache.reset(); | 486 cache.reset(); |
482 } | 487 } |
483 | 488 |
484 base::RunLoop().RunUntilIdle(); | 489 base::RunLoop().RunUntilIdle(); |
485 } | 490 } |
486 | 491 |
487 // Tests that |BackendImpl| fails to initialize with a missing file. | 492 // Tests that |BackendImpl| fails to initialize with a missing file. |
488 TEST_F(DiskCacheBackendTest, CreateBackend_MissingFile) { | 493 TEST_F(DiskCacheBackendTest, CreateBackend_MissingFile) { |
489 ASSERT_TRUE(CopyTestCache("bad_entry")); | 494 ASSERT_TRUE(CopyTestCache("bad_entry")); |
490 base::FilePath filename = cache_path_.AppendASCII("data_1"); | 495 base::FilePath filename = cache_path_.AppendASCII("data_1"); |
491 base::DeleteFile(filename, false); | 496 base::DeleteFile(filename, false); |
492 base::Thread cache_thread("CacheThread"); | 497 base::Thread cache_thread("CacheThread"); |
493 ASSERT_TRUE(cache_thread.StartWithOptions( | 498 ASSERT_TRUE(cache_thread.StartWithOptions( |
494 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 499 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
495 net::TestCompletionCallback cb; | 500 net::TestCompletionCallback cb; |
496 | 501 |
497 bool prev = base::ThreadRestrictions::SetIOAllowed(false); | 502 bool prev = base::ThreadRestrictions::SetIOAllowed(false); |
498 std::unique_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl( | 503 std::unique_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl( |
499 cache_path_, cache_thread.task_runner(), NULL)); | 504 cache_path_, cache_thread.task_runner(), NULL)); |
500 int rv = cache->Init(cb.callback()); | 505 int rv = cache->Init(cb.callback()); |
501 EXPECT_EQ(net::ERR_FAILED, cb.GetResult(rv)); | 506 EXPECT_THAT(cb.GetResult(rv), IsError(net::ERR_FAILED)); |
502 base::ThreadRestrictions::SetIOAllowed(prev); | 507 base::ThreadRestrictions::SetIOAllowed(prev); |
503 | 508 |
504 cache.reset(); | 509 cache.reset(); |
505 DisableIntegrityCheck(); | 510 DisableIntegrityCheck(); |
506 } | 511 } |
507 | 512 |
508 TEST_F(DiskCacheBackendTest, ExternalFiles) { | 513 TEST_F(DiskCacheBackendTest, ExternalFiles) { |
509 InitCache(); | 514 InitCache(); |
510 // First, let's create a file on the folder. | 515 // First, let's create a file on the folder. |
511 base::FilePath filename = cache_path_.AppendASCII("f_000001"); | 516 base::FilePath filename = cache_path_.AppendASCII("f_000001"); |
512 | 517 |
513 const int kSize = 50; | 518 const int kSize = 50; |
514 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); | 519 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); |
515 CacheTestFillBuffer(buffer1->data(), kSize, false); | 520 CacheTestFillBuffer(buffer1->data(), kSize, false); |
516 ASSERT_EQ(kSize, base::WriteFile(filename, buffer1->data(), kSize)); | 521 ASSERT_EQ(kSize, base::WriteFile(filename, buffer1->data(), kSize)); |
517 | 522 |
518 // Now let's create a file with the cache. | 523 // Now let's create a file with the cache. |
519 disk_cache::Entry* entry; | 524 disk_cache::Entry* entry; |
520 ASSERT_EQ(net::OK, CreateEntry("key", &entry)); | 525 ASSERT_THAT(CreateEntry("key", &entry), IsOk()); |
521 ASSERT_EQ(0, WriteData(entry, 0, 20000, buffer1.get(), 0, false)); | 526 ASSERT_EQ(0, WriteData(entry, 0, 20000, buffer1.get(), 0, false)); |
522 entry->Close(); | 527 entry->Close(); |
523 | 528 |
524 // And verify that the first file is still there. | 529 // And verify that the first file is still there. |
525 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize)); | 530 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize)); |
526 ASSERT_EQ(kSize, base::ReadFile(filename, buffer2->data(), kSize)); | 531 ASSERT_EQ(kSize, base::ReadFile(filename, buffer2->data(), kSize)); |
527 EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kSize)); | 532 EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kSize)); |
528 } | 533 } |
529 | 534 |
530 // Tests that we deal with file-level pending operations at destruction time. | 535 // Tests that we deal with file-level pending operations at destruction time. |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
591 std::unique_ptr<disk_cache::Backend> extra_cache; | 596 std::unique_ptr<disk_cache::Backend> extra_cache; |
592 int rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, | 597 int rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, |
593 net::CACHE_BACKEND_DEFAULT, | 598 net::CACHE_BACKEND_DEFAULT, |
594 store.path(), | 599 store.path(), |
595 0, | 600 0, |
596 false, | 601 false, |
597 base::ThreadTaskRunnerHandle::Get(), | 602 base::ThreadTaskRunnerHandle::Get(), |
598 NULL, | 603 NULL, |
599 &extra_cache, | 604 &extra_cache, |
600 cb.callback()); | 605 cb.callback()); |
601 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 606 ASSERT_THAT(cb.GetResult(rv), IsOk()); |
602 ASSERT_TRUE(extra_cache.get() != NULL); | 607 ASSERT_TRUE(extra_cache.get() != NULL); |
603 | 608 |
604 ASSERT_TRUE(CleanupCacheDir()); | 609 ASSERT_TRUE(CleanupCacheDir()); |
605 SetNewEviction(); // Match the expected behavior for integrity verification. | 610 SetNewEviction(); // Match the expected behavior for integrity verification. |
606 UseCurrentThread(); | 611 UseCurrentThread(); |
607 | 612 |
608 CreateBackend(disk_cache::kNoBuffering, NULL); | 613 CreateBackend(disk_cache::kNoBuffering, NULL); |
609 rv = GeneratePendingIO(&cb); | 614 rv = GeneratePendingIO(&cb); |
610 | 615 |
611 // cache_ has a pending operation, and extra_cache will go away. | 616 // cache_ has a pending operation, and extra_cache will go away. |
(...skipping 21 matching lines...) Expand all Loading... |
633 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 638 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
634 | 639 |
635 uint32_t flags = disk_cache::kNoBuffering; | 640 uint32_t flags = disk_cache::kNoBuffering; |
636 if (!fast) | 641 if (!fast) |
637 flags |= disk_cache::kNoRandom; | 642 flags |= disk_cache::kNoRandom; |
638 | 643 |
639 CreateBackend(flags, &cache_thread); | 644 CreateBackend(flags, &cache_thread); |
640 | 645 |
641 disk_cache::Entry* entry; | 646 disk_cache::Entry* entry; |
642 int rv = cache_->CreateEntry("some key", &entry, cb.callback()); | 647 int rv = cache_->CreateEntry("some key", &entry, cb.callback()); |
643 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 648 ASSERT_THAT(cb.GetResult(rv), IsOk()); |
644 | 649 |
645 entry->Close(); | 650 entry->Close(); |
646 | 651 |
647 // The cache destructor will see one pending operation here. | 652 // The cache destructor will see one pending operation here. |
648 cache_.reset(); | 653 cache_.reset(); |
649 } | 654 } |
650 | 655 |
651 base::RunLoop().RunUntilIdle(); | 656 base::RunLoop().RunUntilIdle(); |
652 EXPECT_FALSE(cb.have_result()); | 657 EXPECT_FALSE(cb.have_result()); |
653 } | 658 } |
(...skipping 21 matching lines...) Expand all Loading... |
675 base::Thread cache_thread("CacheThread"); | 680 base::Thread cache_thread("CacheThread"); |
676 ASSERT_TRUE(cache_thread.StartWithOptions( | 681 ASSERT_TRUE(cache_thread.StartWithOptions( |
677 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 682 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
678 | 683 |
679 disk_cache::BackendFlags flags = | 684 disk_cache::BackendFlags flags = |
680 fast ? disk_cache::kNone : disk_cache::kNoRandom; | 685 fast ? disk_cache::kNone : disk_cache::kNoRandom; |
681 CreateBackend(flags, &cache_thread); | 686 CreateBackend(flags, &cache_thread); |
682 | 687 |
683 disk_cache::Entry* entry; | 688 disk_cache::Entry* entry; |
684 int rv = cache_->CreateEntry("some key", &entry, cb.callback()); | 689 int rv = cache_->CreateEntry("some key", &entry, cb.callback()); |
685 ASSERT_EQ(net::ERR_IO_PENDING, rv); | 690 ASSERT_THAT(rv, IsError(net::ERR_IO_PENDING)); |
686 | 691 |
687 cache_.reset(); | 692 cache_.reset(); |
688 EXPECT_FALSE(cb.have_result()); | 693 EXPECT_FALSE(cb.have_result()); |
689 } | 694 } |
690 | 695 |
691 base::RunLoop().RunUntilIdle(); | 696 base::RunLoop().RunUntilIdle(); |
692 EXPECT_FALSE(cb.have_result()); | 697 EXPECT_FALSE(cb.have_result()); |
693 } | 698 } |
694 | 699 |
695 TEST_F(DiskCacheBackendTest, ShutdownWithPendingCreate) { | 700 TEST_F(DiskCacheBackendTest, ShutdownWithPendingCreate) { |
(...skipping 16 matching lines...) Expand all Loading... |
712 ASSERT_TRUE(CleanupCacheDir()); | 717 ASSERT_TRUE(CleanupCacheDir()); |
713 base::Thread cache_thread("CacheThread"); | 718 base::Thread cache_thread("CacheThread"); |
714 ASSERT_TRUE(cache_thread.StartWithOptions( | 719 ASSERT_TRUE(cache_thread.StartWithOptions( |
715 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 720 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
716 | 721 |
717 disk_cache::BackendFlags flags = disk_cache::kNoRandom; | 722 disk_cache::BackendFlags flags = disk_cache::kNoRandom; |
718 CreateBackend(flags, &cache_thread); | 723 CreateBackend(flags, &cache_thread); |
719 | 724 |
720 disk_cache::Entry* entry; | 725 disk_cache::Entry* entry; |
721 int rv = cache_->CreateEntry("some key", &entry, cb.callback()); | 726 int rv = cache_->CreateEntry("some key", &entry, cb.callback()); |
722 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 727 ASSERT_THAT(cb.GetResult(rv), IsOk()); |
723 entry->Close(); | 728 entry->Close(); |
724 entry = nullptr; | 729 entry = nullptr; |
725 | 730 |
726 rv = cache_->DoomEntry("some key", cb.callback()); | 731 rv = cache_->DoomEntry("some key", cb.callback()); |
727 ASSERT_EQ(net::ERR_IO_PENDING, rv); | 732 ASSERT_THAT(rv, IsError(net::ERR_IO_PENDING)); |
728 | 733 |
729 cache_.reset(); | 734 cache_.reset(); |
730 EXPECT_FALSE(cb.have_result()); | 735 EXPECT_FALSE(cb.have_result()); |
731 } | 736 } |
732 | 737 |
733 base::RunLoop().RunUntilIdle(); | 738 base::RunLoop().RunUntilIdle(); |
734 EXPECT_FALSE(cb.have_result()); | 739 EXPECT_FALSE(cb.have_result()); |
735 } | 740 } |
736 | 741 |
737 TEST_F(DiskCacheBackendTest, ShutdownWithPendingDoom) { | 742 TEST_F(DiskCacheBackendTest, ShutdownWithPendingDoom) { |
(...skipping 30 matching lines...) Expand all Loading... |
768 #endif | 773 #endif |
769 | 774 |
770 void DiskCacheBackendTest::BackendSetSize() { | 775 void DiskCacheBackendTest::BackendSetSize() { |
771 const int cache_size = 0x10000; // 64 kB | 776 const int cache_size = 0x10000; // 64 kB |
772 SetMaxSize(cache_size); | 777 SetMaxSize(cache_size); |
773 InitCache(); | 778 InitCache(); |
774 | 779 |
775 std::string first("some key"); | 780 std::string first("some key"); |
776 std::string second("something else"); | 781 std::string second("something else"); |
777 disk_cache::Entry* entry; | 782 disk_cache::Entry* entry; |
778 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); | 783 ASSERT_THAT(CreateEntry(first, &entry), IsOk()); |
779 | 784 |
780 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(cache_size)); | 785 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(cache_size)); |
781 memset(buffer->data(), 0, cache_size); | 786 memset(buffer->data(), 0, cache_size); |
782 EXPECT_EQ(cache_size / 10, | 787 EXPECT_EQ(cache_size / 10, |
783 WriteData(entry, 0, 0, buffer.get(), cache_size / 10, false)) | 788 WriteData(entry, 0, 0, buffer.get(), cache_size / 10, false)) |
784 << "normal file"; | 789 << "normal file"; |
785 | 790 |
786 EXPECT_EQ(net::ERR_FAILED, | 791 EXPECT_EQ(net::ERR_FAILED, |
787 WriteData(entry, 1, 0, buffer.get(), cache_size / 5, false)) | 792 WriteData(entry, 1, 0, buffer.get(), cache_size / 5, false)) |
788 << "file size above the limit"; | 793 << "file size above the limit"; |
789 | 794 |
790 // By doubling the total size, we make this file cacheable. | 795 // By doubling the total size, we make this file cacheable. |
791 SetMaxSize(cache_size * 2); | 796 SetMaxSize(cache_size * 2); |
792 EXPECT_EQ(cache_size / 5, | 797 EXPECT_EQ(cache_size / 5, |
793 WriteData(entry, 1, 0, buffer.get(), cache_size / 5, false)); | 798 WriteData(entry, 1, 0, buffer.get(), cache_size / 5, false)); |
794 | 799 |
795 // Let's fill up the cache!. | 800 // Let's fill up the cache!. |
796 SetMaxSize(cache_size * 10); | 801 SetMaxSize(cache_size * 10); |
797 EXPECT_EQ(cache_size * 3 / 4, | 802 EXPECT_EQ(cache_size * 3 / 4, |
798 WriteData(entry, 0, 0, buffer.get(), cache_size * 3 / 4, false)); | 803 WriteData(entry, 0, 0, buffer.get(), cache_size * 3 / 4, false)); |
799 entry->Close(); | 804 entry->Close(); |
800 FlushQueueForTest(); | 805 FlushQueueForTest(); |
801 | 806 |
802 SetMaxSize(cache_size); | 807 SetMaxSize(cache_size); |
803 | 808 |
804 // The cache is 95% full. | 809 // The cache is 95% full. |
805 | 810 |
806 ASSERT_EQ(net::OK, CreateEntry(second, &entry)); | 811 ASSERT_THAT(CreateEntry(second, &entry), IsOk()); |
807 EXPECT_EQ(cache_size / 10, | 812 EXPECT_EQ(cache_size / 10, |
808 WriteData(entry, 0, 0, buffer.get(), cache_size / 10, false)); | 813 WriteData(entry, 0, 0, buffer.get(), cache_size / 10, false)); |
809 | 814 |
810 disk_cache::Entry* entry2; | 815 disk_cache::Entry* entry2; |
811 ASSERT_EQ(net::OK, CreateEntry("an extra key", &entry2)); | 816 ASSERT_THAT(CreateEntry("an extra key", &entry2), IsOk()); |
812 EXPECT_EQ(cache_size / 10, | 817 EXPECT_EQ(cache_size / 10, |
813 WriteData(entry2, 0, 0, buffer.get(), cache_size / 10, false)); | 818 WriteData(entry2, 0, 0, buffer.get(), cache_size / 10, false)); |
814 entry2->Close(); // This will trigger the cache trim. | 819 entry2->Close(); // This will trigger the cache trim. |
815 | 820 |
816 EXPECT_NE(net::OK, OpenEntry(first, &entry2)); | 821 EXPECT_NE(net::OK, OpenEntry(first, &entry2)); |
817 | 822 |
818 FlushQueueForTest(); // Make sure that we are done trimming the cache. | 823 FlushQueueForTest(); // Make sure that we are done trimming the cache. |
819 FlushQueueForTest(); // We may have posted two tasks to evict stuff. | 824 FlushQueueForTest(); // We may have posted two tasks to evict stuff. |
820 | 825 |
821 entry->Close(); | 826 entry->Close(); |
822 ASSERT_EQ(net::OK, OpenEntry(second, &entry)); | 827 ASSERT_THAT(OpenEntry(second, &entry), IsOk()); |
823 EXPECT_EQ(cache_size / 10, entry->GetDataSize(0)); | 828 EXPECT_EQ(cache_size / 10, entry->GetDataSize(0)); |
824 entry->Close(); | 829 entry->Close(); |
825 } | 830 } |
826 | 831 |
827 TEST_F(DiskCacheBackendTest, SetSize) { | 832 TEST_F(DiskCacheBackendTest, SetSize) { |
828 BackendSetSize(); | 833 BackendSetSize(); |
829 } | 834 } |
830 | 835 |
831 TEST_F(DiskCacheBackendTest, NewEvictionSetSize) { | 836 TEST_F(DiskCacheBackendTest, NewEvictionSetSize) { |
832 SetNewEviction(); | 837 SetNewEviction(); |
833 BackendSetSize(); | 838 BackendSetSize(); |
834 } | 839 } |
835 | 840 |
836 TEST_F(DiskCacheBackendTest, MemoryOnlySetSize) { | 841 TEST_F(DiskCacheBackendTest, MemoryOnlySetSize) { |
837 SetMemoryOnlyMode(); | 842 SetMemoryOnlyMode(); |
838 BackendSetSize(); | 843 BackendSetSize(); |
839 } | 844 } |
840 | 845 |
841 void DiskCacheBackendTest::BackendLoad() { | 846 void DiskCacheBackendTest::BackendLoad() { |
842 InitCache(); | 847 InitCache(); |
843 int seed = static_cast<int>(Time::Now().ToInternalValue()); | 848 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
844 srand(seed); | 849 srand(seed); |
845 | 850 |
846 disk_cache::Entry* entries[100]; | 851 disk_cache::Entry* entries[100]; |
847 for (int i = 0; i < 100; i++) { | 852 for (int i = 0; i < 100; i++) { |
848 std::string key = GenerateKey(true); | 853 std::string key = GenerateKey(true); |
849 ASSERT_EQ(net::OK, CreateEntry(key, &entries[i])); | 854 ASSERT_THAT(CreateEntry(key, &entries[i]), IsOk()); |
850 } | 855 } |
851 EXPECT_EQ(100, cache_->GetEntryCount()); | 856 EXPECT_EQ(100, cache_->GetEntryCount()); |
852 | 857 |
853 for (int i = 0; i < 100; i++) { | 858 for (int i = 0; i < 100; i++) { |
854 int source1 = rand() % 100; | 859 int source1 = rand() % 100; |
855 int source2 = rand() % 100; | 860 int source2 = rand() % 100; |
856 disk_cache::Entry* temp = entries[source1]; | 861 disk_cache::Entry* temp = entries[source1]; |
857 entries[source1] = entries[source2]; | 862 entries[source1] = entries[source2]; |
858 entries[source2] = temp; | 863 entries[source2] = temp; |
859 } | 864 } |
860 | 865 |
861 for (int i = 0; i < 100; i++) { | 866 for (int i = 0; i < 100; i++) { |
862 disk_cache::Entry* entry; | 867 disk_cache::Entry* entry; |
863 ASSERT_EQ(net::OK, OpenEntry(entries[i]->GetKey(), &entry)); | 868 ASSERT_THAT(OpenEntry(entries[i]->GetKey(), &entry), IsOk()); |
864 EXPECT_TRUE(entry == entries[i]); | 869 EXPECT_TRUE(entry == entries[i]); |
865 entry->Close(); | 870 entry->Close(); |
866 entries[i]->Doom(); | 871 entries[i]->Doom(); |
867 entries[i]->Close(); | 872 entries[i]->Close(); |
868 } | 873 } |
869 FlushQueueForTest(); | 874 FlushQueueForTest(); |
870 EXPECT_EQ(0, cache_->GetEntryCount()); | 875 EXPECT_EQ(0, cache_->GetEntryCount()); |
871 } | 876 } |
872 | 877 |
873 TEST_F(DiskCacheBackendTest, Load) { | 878 TEST_F(DiskCacheBackendTest, Load) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
907 BackendLoad(); | 912 BackendLoad(); |
908 } | 913 } |
909 | 914 |
910 // Tests the chaining of an entry to the current head. | 915 // Tests the chaining of an entry to the current head. |
911 void DiskCacheBackendTest::BackendChain() { | 916 void DiskCacheBackendTest::BackendChain() { |
912 SetMask(0x1); // 2-entry table. | 917 SetMask(0x1); // 2-entry table. |
913 SetMaxSize(0x3000); // 12 kB. | 918 SetMaxSize(0x3000); // 12 kB. |
914 InitCache(); | 919 InitCache(); |
915 | 920 |
916 disk_cache::Entry* entry; | 921 disk_cache::Entry* entry; |
917 ASSERT_EQ(net::OK, CreateEntry("The first key", &entry)); | 922 ASSERT_THAT(CreateEntry("The first key", &entry), IsOk()); |
918 entry->Close(); | 923 entry->Close(); |
919 ASSERT_EQ(net::OK, CreateEntry("The Second key", &entry)); | 924 ASSERT_THAT(CreateEntry("The Second key", &entry), IsOk()); |
920 entry->Close(); | 925 entry->Close(); |
921 } | 926 } |
922 | 927 |
923 TEST_F(DiskCacheBackendTest, Chain) { | 928 TEST_F(DiskCacheBackendTest, Chain) { |
924 BackendChain(); | 929 BackendChain(); |
925 } | 930 } |
926 | 931 |
927 TEST_F(DiskCacheBackendTest, NewEvictionChain) { | 932 TEST_F(DiskCacheBackendTest, NewEvictionChain) { |
928 SetNewEviction(); | 933 SetNewEviction(); |
929 BackendChain(); | 934 BackendChain(); |
930 } | 935 } |
931 | 936 |
932 TEST_F(DiskCacheBackendTest, AppCacheChain) { | 937 TEST_F(DiskCacheBackendTest, AppCacheChain) { |
933 SetCacheType(net::APP_CACHE); | 938 SetCacheType(net::APP_CACHE); |
934 BackendChain(); | 939 BackendChain(); |
935 } | 940 } |
936 | 941 |
937 TEST_F(DiskCacheBackendTest, ShaderCacheChain) { | 942 TEST_F(DiskCacheBackendTest, ShaderCacheChain) { |
938 SetCacheType(net::SHADER_CACHE); | 943 SetCacheType(net::SHADER_CACHE); |
939 BackendChain(); | 944 BackendChain(); |
940 } | 945 } |
941 | 946 |
942 TEST_F(DiskCacheBackendTest, NewEvictionTrim) { | 947 TEST_F(DiskCacheBackendTest, NewEvictionTrim) { |
943 SetNewEviction(); | 948 SetNewEviction(); |
944 InitCache(); | 949 InitCache(); |
945 | 950 |
946 disk_cache::Entry* entry; | 951 disk_cache::Entry* entry; |
947 for (int i = 0; i < 100; i++) { | 952 for (int i = 0; i < 100; i++) { |
948 std::string name(base::StringPrintf("Key %d", i)); | 953 std::string name(base::StringPrintf("Key %d", i)); |
949 ASSERT_EQ(net::OK, CreateEntry(name, &entry)); | 954 ASSERT_THAT(CreateEntry(name, &entry), IsOk()); |
950 entry->Close(); | 955 entry->Close(); |
951 if (i < 90) { | 956 if (i < 90) { |
952 // Entries 0 to 89 are in list 1; 90 to 99 are in list 0. | 957 // Entries 0 to 89 are in list 1; 90 to 99 are in list 0. |
953 ASSERT_EQ(net::OK, OpenEntry(name, &entry)); | 958 ASSERT_THAT(OpenEntry(name, &entry), IsOk()); |
954 entry->Close(); | 959 entry->Close(); |
955 } | 960 } |
956 } | 961 } |
957 | 962 |
958 // The first eviction must come from list 1 (10% limit), the second must come | 963 // The first eviction must come from list 1 (10% limit), the second must come |
959 // from list 0. | 964 // from list 0. |
960 TrimForTest(false); | 965 TrimForTest(false); |
961 EXPECT_NE(net::OK, OpenEntry("Key 0", &entry)); | 966 EXPECT_NE(net::OK, OpenEntry("Key 0", &entry)); |
962 TrimForTest(false); | 967 TrimForTest(false); |
963 EXPECT_NE(net::OK, OpenEntry("Key 90", &entry)); | 968 EXPECT_NE(net::OK, OpenEntry("Key 90", &entry)); |
964 | 969 |
965 // Double check that we still have the list tails. | 970 // Double check that we still have the list tails. |
966 ASSERT_EQ(net::OK, OpenEntry("Key 1", &entry)); | 971 ASSERT_THAT(OpenEntry("Key 1", &entry), IsOk()); |
967 entry->Close(); | 972 entry->Close(); |
968 ASSERT_EQ(net::OK, OpenEntry("Key 91", &entry)); | 973 ASSERT_THAT(OpenEntry("Key 91", &entry), IsOk()); |
969 entry->Close(); | 974 entry->Close(); |
970 } | 975 } |
971 | 976 |
972 // Before looking for invalid entries, let's check a valid entry. | 977 // Before looking for invalid entries, let's check a valid entry. |
973 void DiskCacheBackendTest::BackendValidEntry() { | 978 void DiskCacheBackendTest::BackendValidEntry() { |
974 InitCache(); | 979 InitCache(); |
975 | 980 |
976 std::string key("Some key"); | 981 std::string key("Some key"); |
977 disk_cache::Entry* entry; | 982 disk_cache::Entry* entry; |
978 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 983 ASSERT_THAT(CreateEntry(key, &entry), IsOk()); |
979 | 984 |
980 const int kSize = 50; | 985 const int kSize = 50; |
981 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); | 986 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); |
982 memset(buffer1->data(), 0, kSize); | 987 memset(buffer1->data(), 0, kSize); |
983 base::strlcpy(buffer1->data(), "And the data to save", kSize); | 988 base::strlcpy(buffer1->data(), "And the data to save", kSize); |
984 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer1.get(), kSize, false)); | 989 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer1.get(), kSize, false)); |
985 entry->Close(); | 990 entry->Close(); |
986 SimulateCrash(); | 991 SimulateCrash(); |
987 | 992 |
988 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); | 993 ASSERT_THAT(OpenEntry(key, &entry), IsOk()); |
989 | 994 |
990 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize)); | 995 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize)); |
991 memset(buffer2->data(), 0, kSize); | 996 memset(buffer2->data(), 0, kSize); |
992 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer2.get(), kSize)); | 997 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer2.get(), kSize)); |
993 entry->Close(); | 998 entry->Close(); |
994 EXPECT_STREQ(buffer1->data(), buffer2->data()); | 999 EXPECT_STREQ(buffer1->data(), buffer2->data()); |
995 } | 1000 } |
996 | 1001 |
997 TEST_F(DiskCacheBackendTest, ValidEntry) { | 1002 TEST_F(DiskCacheBackendTest, ValidEntry) { |
998 BackendValidEntry(); | 1003 BackendValidEntry(); |
999 } | 1004 } |
1000 | 1005 |
1001 TEST_F(DiskCacheBackendTest, NewEvictionValidEntry) { | 1006 TEST_F(DiskCacheBackendTest, NewEvictionValidEntry) { |
1002 SetNewEviction(); | 1007 SetNewEviction(); |
1003 BackendValidEntry(); | 1008 BackendValidEntry(); |
1004 } | 1009 } |
1005 | 1010 |
1006 // The same logic of the previous test (ValidEntry), but this time force the | 1011 // The same logic of the previous test (ValidEntry), but this time force the |
1007 // entry to be invalid, simulating a crash in the middle. | 1012 // entry to be invalid, simulating a crash in the middle. |
1008 // We'll be leaking memory from this test. | 1013 // We'll be leaking memory from this test. |
1009 void DiskCacheBackendTest::BackendInvalidEntry() { | 1014 void DiskCacheBackendTest::BackendInvalidEntry() { |
1010 InitCache(); | 1015 InitCache(); |
1011 | 1016 |
1012 std::string key("Some key"); | 1017 std::string key("Some key"); |
1013 disk_cache::Entry* entry; | 1018 disk_cache::Entry* entry; |
1014 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 1019 ASSERT_THAT(CreateEntry(key, &entry), IsOk()); |
1015 | 1020 |
1016 const int kSize = 50; | 1021 const int kSize = 50; |
1017 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); | 1022 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); |
1018 memset(buffer->data(), 0, kSize); | 1023 memset(buffer->data(), 0, kSize); |
1019 base::strlcpy(buffer->data(), "And the data to save", kSize); | 1024 base::strlcpy(buffer->data(), "And the data to save", kSize); |
1020 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false)); | 1025 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false)); |
1021 SimulateCrash(); | 1026 SimulateCrash(); |
1022 | 1027 |
1023 EXPECT_NE(net::OK, OpenEntry(key, &entry)); | 1028 EXPECT_NE(net::OK, OpenEntry(key, &entry)); |
1024 EXPECT_EQ(0, cache_->GetEntryCount()); | 1029 EXPECT_EQ(0, cache_->GetEntryCount()); |
(...skipping 23 matching lines...) Expand all Loading... |
1048 BackendInvalidEntry(); | 1053 BackendInvalidEntry(); |
1049 } | 1054 } |
1050 | 1055 |
1051 // Almost the same test, but this time crash the cache after reading an entry. | 1056 // Almost the same test, but this time crash the cache after reading an entry. |
1052 // We'll be leaking memory from this test. | 1057 // We'll be leaking memory from this test. |
1053 void DiskCacheBackendTest::BackendInvalidEntryRead() { | 1058 void DiskCacheBackendTest::BackendInvalidEntryRead() { |
1054 InitCache(); | 1059 InitCache(); |
1055 | 1060 |
1056 std::string key("Some key"); | 1061 std::string key("Some key"); |
1057 disk_cache::Entry* entry; | 1062 disk_cache::Entry* entry; |
1058 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 1063 ASSERT_THAT(CreateEntry(key, &entry), IsOk()); |
1059 | 1064 |
1060 const int kSize = 50; | 1065 const int kSize = 50; |
1061 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); | 1066 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); |
1062 memset(buffer->data(), 0, kSize); | 1067 memset(buffer->data(), 0, kSize); |
1063 base::strlcpy(buffer->data(), "And the data to save", kSize); | 1068 base::strlcpy(buffer->data(), "And the data to save", kSize); |
1064 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false)); | 1069 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false)); |
1065 entry->Close(); | 1070 entry->Close(); |
1066 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); | 1071 ASSERT_THAT(OpenEntry(key, &entry), IsOk()); |
1067 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer.get(), kSize)); | 1072 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer.get(), kSize)); |
1068 | 1073 |
1069 SimulateCrash(); | 1074 SimulateCrash(); |
1070 | 1075 |
1071 if (type_ == net::APP_CACHE) { | 1076 if (type_ == net::APP_CACHE) { |
1072 // Reading an entry and crashing should not make it dirty. | 1077 // Reading an entry and crashing should not make it dirty. |
1073 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); | 1078 ASSERT_THAT(OpenEntry(key, &entry), IsOk()); |
1074 EXPECT_EQ(1, cache_->GetEntryCount()); | 1079 EXPECT_EQ(1, cache_->GetEntryCount()); |
1075 entry->Close(); | 1080 entry->Close(); |
1076 } else { | 1081 } else { |
1077 EXPECT_NE(net::OK, OpenEntry(key, &entry)); | 1082 EXPECT_NE(net::OK, OpenEntry(key, &entry)); |
1078 EXPECT_EQ(0, cache_->GetEntryCount()); | 1083 EXPECT_EQ(0, cache_->GetEntryCount()); |
1079 } | 1084 } |
1080 } | 1085 } |
1081 | 1086 |
1082 // We'll be leaking memory from this test. | 1087 // We'll be leaking memory from this test. |
1083 TEST_F(DiskCacheBackendTest, InvalidEntryRead) { | 1088 TEST_F(DiskCacheBackendTest, InvalidEntryRead) { |
(...skipping 25 matching lines...) Expand all Loading... |
1109 SetMaxSize(0x100000); | 1114 SetMaxSize(0x100000); |
1110 InitCache(); | 1115 InitCache(); |
1111 | 1116 |
1112 int seed = static_cast<int>(Time::Now().ToInternalValue()); | 1117 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
1113 srand(seed); | 1118 srand(seed); |
1114 | 1119 |
1115 const int kNumEntries = 100; | 1120 const int kNumEntries = 100; |
1116 disk_cache::Entry* entries[kNumEntries]; | 1121 disk_cache::Entry* entries[kNumEntries]; |
1117 for (int i = 0; i < kNumEntries; i++) { | 1122 for (int i = 0; i < kNumEntries; i++) { |
1118 std::string key = GenerateKey(true); | 1123 std::string key = GenerateKey(true); |
1119 ASSERT_EQ(net::OK, CreateEntry(key, &entries[i])); | 1124 ASSERT_THAT(CreateEntry(key, &entries[i]), IsOk()); |
1120 } | 1125 } |
1121 EXPECT_EQ(kNumEntries, cache_->GetEntryCount()); | 1126 EXPECT_EQ(kNumEntries, cache_->GetEntryCount()); |
1122 | 1127 |
1123 for (int i = 0; i < kNumEntries; i++) { | 1128 for (int i = 0; i < kNumEntries; i++) { |
1124 int source1 = rand() % kNumEntries; | 1129 int source1 = rand() % kNumEntries; |
1125 int source2 = rand() % kNumEntries; | 1130 int source2 = rand() % kNumEntries; |
1126 disk_cache::Entry* temp = entries[source1]; | 1131 disk_cache::Entry* temp = entries[source1]; |
1127 entries[source1] = entries[source2]; | 1132 entries[source1] = entries[source2]; |
1128 entries[source2] = temp; | 1133 entries[source2] = temp; |
1129 } | 1134 } |
1130 | 1135 |
1131 std::string keys[kNumEntries]; | 1136 std::string keys[kNumEntries]; |
1132 for (int i = 0; i < kNumEntries; i++) { | 1137 for (int i = 0; i < kNumEntries; i++) { |
1133 keys[i] = entries[i]->GetKey(); | 1138 keys[i] = entries[i]->GetKey(); |
1134 if (i < kNumEntries / 2) | 1139 if (i < kNumEntries / 2) |
1135 entries[i]->Close(); | 1140 entries[i]->Close(); |
1136 } | 1141 } |
1137 | 1142 |
1138 SimulateCrash(); | 1143 SimulateCrash(); |
1139 | 1144 |
1140 for (int i = kNumEntries / 2; i < kNumEntries; i++) { | 1145 for (int i = kNumEntries / 2; i < kNumEntries; i++) { |
1141 disk_cache::Entry* entry; | 1146 disk_cache::Entry* entry; |
1142 EXPECT_NE(net::OK, OpenEntry(keys[i], &entry)); | 1147 EXPECT_NE(net::OK, OpenEntry(keys[i], &entry)); |
1143 } | 1148 } |
1144 | 1149 |
1145 for (int i = 0; i < kNumEntries / 2; i++) { | 1150 for (int i = 0; i < kNumEntries / 2; i++) { |
1146 disk_cache::Entry* entry; | 1151 disk_cache::Entry* entry; |
1147 ASSERT_EQ(net::OK, OpenEntry(keys[i], &entry)); | 1152 ASSERT_THAT(OpenEntry(keys[i], &entry), IsOk()); |
1148 entry->Close(); | 1153 entry->Close(); |
1149 } | 1154 } |
1150 | 1155 |
1151 EXPECT_EQ(kNumEntries / 2, cache_->GetEntryCount()); | 1156 EXPECT_EQ(kNumEntries / 2, cache_->GetEntryCount()); |
1152 } | 1157 } |
1153 | 1158 |
1154 // We'll be leaking memory from this test. | 1159 // We'll be leaking memory from this test. |
1155 TEST_F(DiskCacheBackendTest, InvalidEntryWithLoad) { | 1160 TEST_F(DiskCacheBackendTest, InvalidEntryWithLoad) { |
1156 BackendInvalidEntryWithLoad(); | 1161 BackendInvalidEntryWithLoad(); |
1157 } | 1162 } |
(...skipping 18 matching lines...) Expand all Loading... |
1176 | 1181 |
1177 // We'll be leaking memory from this test. | 1182 // We'll be leaking memory from this test. |
1178 void DiskCacheBackendTest::BackendTrimInvalidEntry() { | 1183 void DiskCacheBackendTest::BackendTrimInvalidEntry() { |
1179 const int kSize = 0x3000; // 12 kB | 1184 const int kSize = 0x3000; // 12 kB |
1180 SetMaxSize(kSize * 10); | 1185 SetMaxSize(kSize * 10); |
1181 InitCache(); | 1186 InitCache(); |
1182 | 1187 |
1183 std::string first("some key"); | 1188 std::string first("some key"); |
1184 std::string second("something else"); | 1189 std::string second("something else"); |
1185 disk_cache::Entry* entry; | 1190 disk_cache::Entry* entry; |
1186 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); | 1191 ASSERT_THAT(CreateEntry(first, &entry), IsOk()); |
1187 | 1192 |
1188 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); | 1193 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); |
1189 memset(buffer->data(), 0, kSize); | 1194 memset(buffer->data(), 0, kSize); |
1190 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false)); | 1195 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false)); |
1191 | 1196 |
1192 // Simulate a crash. | 1197 // Simulate a crash. |
1193 SimulateCrash(); | 1198 SimulateCrash(); |
1194 | 1199 |
1195 ASSERT_EQ(net::OK, CreateEntry(second, &entry)); | 1200 ASSERT_THAT(CreateEntry(second, &entry), IsOk()); |
1196 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false)); | 1201 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false)); |
1197 | 1202 |
1198 EXPECT_EQ(2, cache_->GetEntryCount()); | 1203 EXPECT_EQ(2, cache_->GetEntryCount()); |
1199 SetMaxSize(kSize); | 1204 SetMaxSize(kSize); |
1200 entry->Close(); // Trim the cache. | 1205 entry->Close(); // Trim the cache. |
1201 FlushQueueForTest(); | 1206 FlushQueueForTest(); |
1202 | 1207 |
1203 // If we evicted the entry in less than 20mS, we have one entry in the cache; | 1208 // If we evicted the entry in less than 20mS, we have one entry in the cache; |
1204 // if it took more than that, we posted a task and we'll delete the second | 1209 // if it took more than that, we posted a task and we'll delete the second |
1205 // entry too. | 1210 // entry too. |
(...skipping 28 matching lines...) Expand all Loading... |
1234 SetMaxSize(kSize * 40); | 1239 SetMaxSize(kSize * 40); |
1235 InitCache(); | 1240 InitCache(); |
1236 | 1241 |
1237 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); | 1242 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); |
1238 memset(buffer->data(), 0, kSize); | 1243 memset(buffer->data(), 0, kSize); |
1239 disk_cache::Entry* entry; | 1244 disk_cache::Entry* entry; |
1240 | 1245 |
1241 // Writing 32 entries to this cache chains most of them. | 1246 // Writing 32 entries to this cache chains most of them. |
1242 for (int i = 0; i < 32; i++) { | 1247 for (int i = 0; i < 32; i++) { |
1243 std::string key(base::StringPrintf("some key %d", i)); | 1248 std::string key(base::StringPrintf("some key %d", i)); |
1244 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 1249 ASSERT_THAT(CreateEntry(key, &entry), IsOk()); |
1245 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false)); | 1250 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false)); |
1246 entry->Close(); | 1251 entry->Close(); |
1247 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); | 1252 ASSERT_THAT(OpenEntry(key, &entry), IsOk()); |
1248 // Note that we are not closing the entries. | 1253 // Note that we are not closing the entries. |
1249 } | 1254 } |
1250 | 1255 |
1251 // Simulate a crash. | 1256 // Simulate a crash. |
1252 SimulateCrash(); | 1257 SimulateCrash(); |
1253 | 1258 |
1254 ASSERT_EQ(net::OK, CreateEntry("Something else", &entry)); | 1259 ASSERT_THAT(CreateEntry("Something else", &entry), IsOk()); |
1255 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false)); | 1260 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false)); |
1256 | 1261 |
1257 FlushQueueForTest(); | 1262 FlushQueueForTest(); |
1258 EXPECT_EQ(33, cache_->GetEntryCount()); | 1263 EXPECT_EQ(33, cache_->GetEntryCount()); |
1259 SetMaxSize(kSize); | 1264 SetMaxSize(kSize); |
1260 | 1265 |
1261 // For the new eviction code, all corrupt entries are on the second list so | 1266 // For the new eviction code, all corrupt entries are on the second list so |
1262 // they are not going away that easy. | 1267 // they are not going away that easy. |
1263 if (new_eviction_) { | 1268 if (new_eviction_) { |
1264 EXPECT_EQ(net::OK, DoomAllEntries()); | 1269 EXPECT_THAT(DoomAllEntries(), IsOk()); |
1265 } | 1270 } |
1266 | 1271 |
1267 entry->Close(); // Trim the cache. | 1272 entry->Close(); // Trim the cache. |
1268 FlushQueueForTest(); | 1273 FlushQueueForTest(); |
1269 | 1274 |
1270 // We may abort the eviction before cleaning up everything. | 1275 // We may abort the eviction before cleaning up everything. |
1271 base::RunLoop().RunUntilIdle(); | 1276 base::RunLoop().RunUntilIdle(); |
1272 FlushQueueForTest(); | 1277 FlushQueueForTest(); |
1273 // If it's not clear enough: we may still have eviction tasks running at this | 1278 // If it's not clear enough: we may still have eviction tasks running at this |
1274 // time, so the number of entries is changing while we read it. | 1279 // time, so the number of entries is changing while we read it. |
(...skipping 15 matching lines...) Expand all Loading... |
1290 #endif // !defined(LEAK_SANITIZER) | 1295 #endif // !defined(LEAK_SANITIZER) |
1291 | 1296 |
1292 void DiskCacheBackendTest::BackendEnumerations() { | 1297 void DiskCacheBackendTest::BackendEnumerations() { |
1293 InitCache(); | 1298 InitCache(); |
1294 Time initial = Time::Now(); | 1299 Time initial = Time::Now(); |
1295 | 1300 |
1296 const int kNumEntries = 100; | 1301 const int kNumEntries = 100; |
1297 for (int i = 0; i < kNumEntries; i++) { | 1302 for (int i = 0; i < kNumEntries; i++) { |
1298 std::string key = GenerateKey(true); | 1303 std::string key = GenerateKey(true); |
1299 disk_cache::Entry* entry; | 1304 disk_cache::Entry* entry; |
1300 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 1305 ASSERT_THAT(CreateEntry(key, &entry), IsOk()); |
1301 entry->Close(); | 1306 entry->Close(); |
1302 } | 1307 } |
1303 EXPECT_EQ(kNumEntries, cache_->GetEntryCount()); | 1308 EXPECT_EQ(kNumEntries, cache_->GetEntryCount()); |
1304 Time final = Time::Now(); | 1309 Time final = Time::Now(); |
1305 | 1310 |
1306 disk_cache::Entry* entry; | 1311 disk_cache::Entry* entry; |
1307 std::unique_ptr<TestIterator> iter = CreateIterator(); | 1312 std::unique_ptr<TestIterator> iter = CreateIterator(); |
1308 int count = 0; | 1313 int count = 0; |
1309 Time last_modified[kNumEntries]; | 1314 Time last_modified[kNumEntries]; |
1310 Time last_used[kNumEntries]; | 1315 Time last_used[kNumEntries]; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1360 SetCacheType(net::APP_CACHE); | 1365 SetCacheType(net::APP_CACHE); |
1361 BackendEnumerations(); | 1366 BackendEnumerations(); |
1362 } | 1367 } |
1363 | 1368 |
1364 // Verifies enumerations while entries are open. | 1369 // Verifies enumerations while entries are open. |
1365 void DiskCacheBackendTest::BackendEnumerations2() { | 1370 void DiskCacheBackendTest::BackendEnumerations2() { |
1366 InitCache(); | 1371 InitCache(); |
1367 const std::string first("first"); | 1372 const std::string first("first"); |
1368 const std::string second("second"); | 1373 const std::string second("second"); |
1369 disk_cache::Entry *entry1, *entry2; | 1374 disk_cache::Entry *entry1, *entry2; |
1370 ASSERT_EQ(net::OK, CreateEntry(first, &entry1)); | 1375 ASSERT_THAT(CreateEntry(first, &entry1), IsOk()); |
1371 entry1->Close(); | 1376 entry1->Close(); |
1372 ASSERT_EQ(net::OK, CreateEntry(second, &entry2)); | 1377 ASSERT_THAT(CreateEntry(second, &entry2), IsOk()); |
1373 entry2->Close(); | 1378 entry2->Close(); |
1374 FlushQueueForTest(); | 1379 FlushQueueForTest(); |
1375 | 1380 |
1376 // Make sure that the timestamp is not the same. | 1381 // Make sure that the timestamp is not the same. |
1377 AddDelay(); | 1382 AddDelay(); |
1378 ASSERT_EQ(net::OK, OpenEntry(second, &entry1)); | 1383 ASSERT_THAT(OpenEntry(second, &entry1), IsOk()); |
1379 std::unique_ptr<TestIterator> iter = CreateIterator(); | 1384 std::unique_ptr<TestIterator> iter = CreateIterator(); |
1380 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry2)); | 1385 ASSERT_THAT(iter->OpenNextEntry(&entry2), IsOk()); |
1381 EXPECT_EQ(entry2->GetKey(), second); | 1386 EXPECT_EQ(entry2->GetKey(), second); |
1382 | 1387 |
1383 // Two entries and the iterator pointing at "first". | 1388 // Two entries and the iterator pointing at "first". |
1384 entry1->Close(); | 1389 entry1->Close(); |
1385 entry2->Close(); | 1390 entry2->Close(); |
1386 | 1391 |
1387 // The iterator should still be valid, so we should not crash. | 1392 // The iterator should still be valid, so we should not crash. |
1388 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry2)); | 1393 ASSERT_THAT(iter->OpenNextEntry(&entry2), IsOk()); |
1389 EXPECT_EQ(entry2->GetKey(), first); | 1394 EXPECT_EQ(entry2->GetKey(), first); |
1390 entry2->Close(); | 1395 entry2->Close(); |
1391 iter = CreateIterator(); | 1396 iter = CreateIterator(); |
1392 | 1397 |
1393 // Modify the oldest entry and get the newest element. | 1398 // Modify the oldest entry and get the newest element. |
1394 ASSERT_EQ(net::OK, OpenEntry(first, &entry1)); | 1399 ASSERT_THAT(OpenEntry(first, &entry1), IsOk()); |
1395 EXPECT_EQ(0, WriteData(entry1, 0, 200, NULL, 0, false)); | 1400 EXPECT_EQ(0, WriteData(entry1, 0, 200, NULL, 0, false)); |
1396 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry2)); | 1401 ASSERT_THAT(iter->OpenNextEntry(&entry2), IsOk()); |
1397 if (type_ == net::APP_CACHE) { | 1402 if (type_ == net::APP_CACHE) { |
1398 // The list is not updated. | 1403 // The list is not updated. |
1399 EXPECT_EQ(entry2->GetKey(), second); | 1404 EXPECT_EQ(entry2->GetKey(), second); |
1400 } else { | 1405 } else { |
1401 EXPECT_EQ(entry2->GetKey(), first); | 1406 EXPECT_EQ(entry2->GetKey(), first); |
1402 } | 1407 } |
1403 | 1408 |
1404 entry1->Close(); | 1409 entry1->Close(); |
1405 entry2->Close(); | 1410 entry2->Close(); |
1406 } | 1411 } |
(...skipping 26 matching lines...) Expand all Loading... |
1433 // when using the SHADER_CACHE type. | 1438 // when using the SHADER_CACHE type. |
1434 TEST_F(DiskCacheBackendTest, ShaderCacheEnumerationReadData) { | 1439 TEST_F(DiskCacheBackendTest, ShaderCacheEnumerationReadData) { |
1435 SetCacheType(net::SHADER_CACHE); | 1440 SetCacheType(net::SHADER_CACHE); |
1436 InitCache(); | 1441 InitCache(); |
1437 const std::string first("first"); | 1442 const std::string first("first"); |
1438 const std::string second("second"); | 1443 const std::string second("second"); |
1439 disk_cache::Entry *entry1, *entry2; | 1444 disk_cache::Entry *entry1, *entry2; |
1440 const int kSize = 50; | 1445 const int kSize = 50; |
1441 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); | 1446 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); |
1442 | 1447 |
1443 ASSERT_EQ(net::OK, CreateEntry(first, &entry1)); | 1448 ASSERT_THAT(CreateEntry(first, &entry1), IsOk()); |
1444 memset(buffer1->data(), 0, kSize); | 1449 memset(buffer1->data(), 0, kSize); |
1445 base::strlcpy(buffer1->data(), "And the data to save", kSize); | 1450 base::strlcpy(buffer1->data(), "And the data to save", kSize); |
1446 EXPECT_EQ(kSize, WriteData(entry1, 0, 0, buffer1.get(), kSize, false)); | 1451 EXPECT_EQ(kSize, WriteData(entry1, 0, 0, buffer1.get(), kSize, false)); |
1447 | 1452 |
1448 ASSERT_EQ(net::OK, CreateEntry(second, &entry2)); | 1453 ASSERT_THAT(CreateEntry(second, &entry2), IsOk()); |
1449 entry2->Close(); | 1454 entry2->Close(); |
1450 | 1455 |
1451 FlushQueueForTest(); | 1456 FlushQueueForTest(); |
1452 | 1457 |
1453 // Make sure that the timestamp is not the same. | 1458 // Make sure that the timestamp is not the same. |
1454 AddDelay(); | 1459 AddDelay(); |
1455 | 1460 |
1456 // Read from the last item in the LRU. | 1461 // Read from the last item in the LRU. |
1457 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1.get(), kSize)); | 1462 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1.get(), kSize)); |
1458 entry1->Close(); | 1463 entry1->Close(); |
1459 | 1464 |
1460 std::unique_ptr<TestIterator> iter = CreateIterator(); | 1465 std::unique_ptr<TestIterator> iter = CreateIterator(); |
1461 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry2)); | 1466 ASSERT_THAT(iter->OpenNextEntry(&entry2), IsOk()); |
1462 EXPECT_EQ(entry2->GetKey(), second); | 1467 EXPECT_EQ(entry2->GetKey(), second); |
1463 entry2->Close(); | 1468 entry2->Close(); |
1464 } | 1469 } |
1465 | 1470 |
1466 #if !defined(LEAK_SANITIZER) | 1471 #if !defined(LEAK_SANITIZER) |
1467 // Verify handling of invalid entries while doing enumerations. | 1472 // Verify handling of invalid entries while doing enumerations. |
1468 // We'll be leaking memory from this test. | 1473 // We'll be leaking memory from this test. |
1469 void DiskCacheBackendTest::BackendInvalidEntryEnumeration() { | 1474 void DiskCacheBackendTest::BackendInvalidEntryEnumeration() { |
1470 InitCache(); | 1475 InitCache(); |
1471 | 1476 |
1472 std::string key("Some key"); | 1477 std::string key("Some key"); |
1473 disk_cache::Entry *entry, *entry1, *entry2; | 1478 disk_cache::Entry *entry, *entry1, *entry2; |
1474 ASSERT_EQ(net::OK, CreateEntry(key, &entry1)); | 1479 ASSERT_THAT(CreateEntry(key, &entry1), IsOk()); |
1475 | 1480 |
1476 const int kSize = 50; | 1481 const int kSize = 50; |
1477 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); | 1482 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); |
1478 memset(buffer1->data(), 0, kSize); | 1483 memset(buffer1->data(), 0, kSize); |
1479 base::strlcpy(buffer1->data(), "And the data to save", kSize); | 1484 base::strlcpy(buffer1->data(), "And the data to save", kSize); |
1480 EXPECT_EQ(kSize, WriteData(entry1, 0, 0, buffer1.get(), kSize, false)); | 1485 EXPECT_EQ(kSize, WriteData(entry1, 0, 0, buffer1.get(), kSize, false)); |
1481 entry1->Close(); | 1486 entry1->Close(); |
1482 ASSERT_EQ(net::OK, OpenEntry(key, &entry1)); | 1487 ASSERT_THAT(OpenEntry(key, &entry1), IsOk()); |
1483 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1.get(), kSize)); | 1488 EXPECT_EQ(kSize, ReadData(entry1, 0, 0, buffer1.get(), kSize)); |
1484 | 1489 |
1485 std::string key2("Another key"); | 1490 std::string key2("Another key"); |
1486 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2)); | 1491 ASSERT_THAT(CreateEntry(key2, &entry2), IsOk()); |
1487 entry2->Close(); | 1492 entry2->Close(); |
1488 ASSERT_EQ(2, cache_->GetEntryCount()); | 1493 ASSERT_EQ(2, cache_->GetEntryCount()); |
1489 | 1494 |
1490 SimulateCrash(); | 1495 SimulateCrash(); |
1491 | 1496 |
1492 std::unique_ptr<TestIterator> iter = CreateIterator(); | 1497 std::unique_ptr<TestIterator> iter = CreateIterator(); |
1493 int count = 0; | 1498 int count = 0; |
1494 while (iter->OpenNextEntry(&entry) == net::OK) { | 1499 while (iter->OpenNextEntry(&entry) == net::OK) { |
1495 ASSERT_TRUE(NULL != entry); | 1500 ASSERT_TRUE(NULL != entry); |
1496 EXPECT_EQ(key2, entry->GetKey()); | 1501 EXPECT_EQ(key2, entry->GetKey()); |
(...skipping 21 matching lines...) Expand all Loading... |
1518 void DiskCacheBackendTest::BackendFixEnumerators() { | 1523 void DiskCacheBackendTest::BackendFixEnumerators() { |
1519 InitCache(); | 1524 InitCache(); |
1520 | 1525 |
1521 int seed = static_cast<int>(Time::Now().ToInternalValue()); | 1526 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
1522 srand(seed); | 1527 srand(seed); |
1523 | 1528 |
1524 const int kNumEntries = 10; | 1529 const int kNumEntries = 10; |
1525 for (int i = 0; i < kNumEntries; i++) { | 1530 for (int i = 0; i < kNumEntries; i++) { |
1526 std::string key = GenerateKey(true); | 1531 std::string key = GenerateKey(true); |
1527 disk_cache::Entry* entry; | 1532 disk_cache::Entry* entry; |
1528 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 1533 ASSERT_THAT(CreateEntry(key, &entry), IsOk()); |
1529 entry->Close(); | 1534 entry->Close(); |
1530 } | 1535 } |
1531 EXPECT_EQ(kNumEntries, cache_->GetEntryCount()); | 1536 EXPECT_EQ(kNumEntries, cache_->GetEntryCount()); |
1532 | 1537 |
1533 disk_cache::Entry *entry1, *entry2; | 1538 disk_cache::Entry *entry1, *entry2; |
1534 std::unique_ptr<TestIterator> iter1 = CreateIterator(), | 1539 std::unique_ptr<TestIterator> iter1 = CreateIterator(), |
1535 iter2 = CreateIterator(); | 1540 iter2 = CreateIterator(); |
1536 ASSERT_EQ(net::OK, iter1->OpenNextEntry(&entry1)); | 1541 ASSERT_THAT(iter1->OpenNextEntry(&entry1), IsOk()); |
1537 ASSERT_TRUE(NULL != entry1); | 1542 ASSERT_TRUE(NULL != entry1); |
1538 entry1->Close(); | 1543 entry1->Close(); |
1539 entry1 = NULL; | 1544 entry1 = NULL; |
1540 | 1545 |
1541 // Let's go to the middle of the list. | 1546 // Let's go to the middle of the list. |
1542 for (int i = 0; i < kNumEntries / 2; i++) { | 1547 for (int i = 0; i < kNumEntries / 2; i++) { |
1543 if (entry1) | 1548 if (entry1) |
1544 entry1->Close(); | 1549 entry1->Close(); |
1545 ASSERT_EQ(net::OK, iter1->OpenNextEntry(&entry1)); | 1550 ASSERT_THAT(iter1->OpenNextEntry(&entry1), IsOk()); |
1546 ASSERT_TRUE(NULL != entry1); | 1551 ASSERT_TRUE(NULL != entry1); |
1547 | 1552 |
1548 ASSERT_EQ(net::OK, iter2->OpenNextEntry(&entry2)); | 1553 ASSERT_THAT(iter2->OpenNextEntry(&entry2), IsOk()); |
1549 ASSERT_TRUE(NULL != entry2); | 1554 ASSERT_TRUE(NULL != entry2); |
1550 entry2->Close(); | 1555 entry2->Close(); |
1551 } | 1556 } |
1552 | 1557 |
1553 // Messing up with entry1 will modify entry2->next. | 1558 // Messing up with entry1 will modify entry2->next. |
1554 entry1->Doom(); | 1559 entry1->Doom(); |
1555 ASSERT_EQ(net::OK, iter2->OpenNextEntry(&entry2)); | 1560 ASSERT_THAT(iter2->OpenNextEntry(&entry2), IsOk()); |
1556 ASSERT_TRUE(NULL != entry2); | 1561 ASSERT_TRUE(NULL != entry2); |
1557 | 1562 |
1558 // The link entry2->entry1 should be broken. | 1563 // The link entry2->entry1 should be broken. |
1559 EXPECT_NE(entry2->GetKey(), entry1->GetKey()); | 1564 EXPECT_NE(entry2->GetKey(), entry1->GetKey()); |
1560 entry1->Close(); | 1565 entry1->Close(); |
1561 entry2->Close(); | 1566 entry2->Close(); |
1562 | 1567 |
1563 // And the second iterator should keep working. | 1568 // And the second iterator should keep working. |
1564 ASSERT_EQ(net::OK, iter2->OpenNextEntry(&entry2)); | 1569 ASSERT_THAT(iter2->OpenNextEntry(&entry2), IsOk()); |
1565 ASSERT_TRUE(NULL != entry2); | 1570 ASSERT_TRUE(NULL != entry2); |
1566 entry2->Close(); | 1571 entry2->Close(); |
1567 } | 1572 } |
1568 | 1573 |
1569 TEST_F(DiskCacheBackendTest, FixEnumerators) { | 1574 TEST_F(DiskCacheBackendTest, FixEnumerators) { |
1570 BackendFixEnumerators(); | 1575 BackendFixEnumerators(); |
1571 } | 1576 } |
1572 | 1577 |
1573 TEST_F(DiskCacheBackendTest, NewEvictionFixEnumerators) { | 1578 TEST_F(DiskCacheBackendTest, NewEvictionFixEnumerators) { |
1574 SetNewEviction(); | 1579 SetNewEviction(); |
1575 BackendFixEnumerators(); | 1580 BackendFixEnumerators(); |
1576 } | 1581 } |
1577 | 1582 |
1578 void DiskCacheBackendTest::BackendDoomRecent() { | 1583 void DiskCacheBackendTest::BackendDoomRecent() { |
1579 InitCache(); | 1584 InitCache(); |
1580 | 1585 |
1581 disk_cache::Entry *entry; | 1586 disk_cache::Entry *entry; |
1582 ASSERT_EQ(net::OK, CreateEntry("first", &entry)); | 1587 ASSERT_THAT(CreateEntry("first", &entry), IsOk()); |
1583 entry->Close(); | 1588 entry->Close(); |
1584 ASSERT_EQ(net::OK, CreateEntry("second", &entry)); | 1589 ASSERT_THAT(CreateEntry("second", &entry), IsOk()); |
1585 entry->Close(); | 1590 entry->Close(); |
1586 FlushQueueForTest(); | 1591 FlushQueueForTest(); |
1587 | 1592 |
1588 AddDelay(); | 1593 AddDelay(); |
1589 Time middle = Time::Now(); | 1594 Time middle = Time::Now(); |
1590 | 1595 |
1591 ASSERT_EQ(net::OK, CreateEntry("third", &entry)); | 1596 ASSERT_THAT(CreateEntry("third", &entry), IsOk()); |
1592 entry->Close(); | 1597 entry->Close(); |
1593 ASSERT_EQ(net::OK, CreateEntry("fourth", &entry)); | 1598 ASSERT_THAT(CreateEntry("fourth", &entry), IsOk()); |
1594 entry->Close(); | 1599 entry->Close(); |
1595 FlushQueueForTest(); | 1600 FlushQueueForTest(); |
1596 | 1601 |
1597 AddDelay(); | 1602 AddDelay(); |
1598 Time final = Time::Now(); | 1603 Time final = Time::Now(); |
1599 | 1604 |
1600 ASSERT_EQ(4, cache_->GetEntryCount()); | 1605 ASSERT_EQ(4, cache_->GetEntryCount()); |
1601 EXPECT_EQ(net::OK, DoomEntriesSince(final)); | 1606 EXPECT_THAT(DoomEntriesSince(final), IsOk()); |
1602 ASSERT_EQ(4, cache_->GetEntryCount()); | 1607 ASSERT_EQ(4, cache_->GetEntryCount()); |
1603 | 1608 |
1604 EXPECT_EQ(net::OK, DoomEntriesSince(middle)); | 1609 EXPECT_THAT(DoomEntriesSince(middle), IsOk()); |
1605 ASSERT_EQ(2, cache_->GetEntryCount()); | 1610 ASSERT_EQ(2, cache_->GetEntryCount()); |
1606 | 1611 |
1607 ASSERT_EQ(net::OK, OpenEntry("second", &entry)); | 1612 ASSERT_THAT(OpenEntry("second", &entry), IsOk()); |
1608 entry->Close(); | 1613 entry->Close(); |
1609 } | 1614 } |
1610 | 1615 |
1611 TEST_F(DiskCacheBackendTest, DoomRecent) { | 1616 TEST_F(DiskCacheBackendTest, DoomRecent) { |
1612 BackendDoomRecent(); | 1617 BackendDoomRecent(); |
1613 } | 1618 } |
1614 | 1619 |
1615 TEST_F(DiskCacheBackendTest, NewEvictionDoomRecent) { | 1620 TEST_F(DiskCacheBackendTest, NewEvictionDoomRecent) { |
1616 SetNewEviction(); | 1621 SetNewEviction(); |
1617 BackendDoomRecent(); | 1622 BackendDoomRecent(); |
(...skipping 18 matching lines...) Expand all Loading... |
1636 DoomEntriesSince(start); | 1641 DoomEntriesSince(start); |
1637 // NOTE: BackendImpl counts child entries in its GetEntryCount(), while | 1642 // NOTE: BackendImpl counts child entries in its GetEntryCount(), while |
1638 // MemBackendImpl does not. Thats why expected value differs here from | 1643 // MemBackendImpl does not. Thats why expected value differs here from |
1639 // MemoryOnlyDoomEntriesSinceSparse. | 1644 // MemoryOnlyDoomEntriesSinceSparse. |
1640 EXPECT_EQ(3, cache_->GetEntryCount()); | 1645 EXPECT_EQ(3, cache_->GetEntryCount()); |
1641 } | 1646 } |
1642 | 1647 |
1643 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomAllSparse) { | 1648 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomAllSparse) { |
1644 SetMemoryOnlyMode(); | 1649 SetMemoryOnlyMode(); |
1645 InitSparseCache(NULL, NULL); | 1650 InitSparseCache(NULL, NULL); |
1646 EXPECT_EQ(net::OK, DoomAllEntries()); | 1651 EXPECT_THAT(DoomAllEntries(), IsOk()); |
1647 EXPECT_EQ(0, cache_->GetEntryCount()); | 1652 EXPECT_EQ(0, cache_->GetEntryCount()); |
1648 } | 1653 } |
1649 | 1654 |
1650 TEST_F(DiskCacheBackendTest, DoomAllSparse) { | 1655 TEST_F(DiskCacheBackendTest, DoomAllSparse) { |
1651 InitSparseCache(NULL, NULL); | 1656 InitSparseCache(NULL, NULL); |
1652 EXPECT_EQ(net::OK, DoomAllEntries()); | 1657 EXPECT_THAT(DoomAllEntries(), IsOk()); |
1653 EXPECT_EQ(0, cache_->GetEntryCount()); | 1658 EXPECT_EQ(0, cache_->GetEntryCount()); |
1654 } | 1659 } |
1655 | 1660 |
1656 void DiskCacheBackendTest::BackendDoomBetween() { | 1661 void DiskCacheBackendTest::BackendDoomBetween() { |
1657 InitCache(); | 1662 InitCache(); |
1658 | 1663 |
1659 disk_cache::Entry *entry; | 1664 disk_cache::Entry *entry; |
1660 ASSERT_EQ(net::OK, CreateEntry("first", &entry)); | 1665 ASSERT_THAT(CreateEntry("first", &entry), IsOk()); |
1661 entry->Close(); | 1666 entry->Close(); |
1662 FlushQueueForTest(); | 1667 FlushQueueForTest(); |
1663 | 1668 |
1664 AddDelay(); | 1669 AddDelay(); |
1665 Time middle_start = Time::Now(); | 1670 Time middle_start = Time::Now(); |
1666 | 1671 |
1667 ASSERT_EQ(net::OK, CreateEntry("second", &entry)); | 1672 ASSERT_THAT(CreateEntry("second", &entry), IsOk()); |
1668 entry->Close(); | 1673 entry->Close(); |
1669 ASSERT_EQ(net::OK, CreateEntry("third", &entry)); | 1674 ASSERT_THAT(CreateEntry("third", &entry), IsOk()); |
1670 entry->Close(); | 1675 entry->Close(); |
1671 FlushQueueForTest(); | 1676 FlushQueueForTest(); |
1672 | 1677 |
1673 AddDelay(); | 1678 AddDelay(); |
1674 Time middle_end = Time::Now(); | 1679 Time middle_end = Time::Now(); |
1675 | 1680 |
1676 ASSERT_EQ(net::OK, CreateEntry("fourth", &entry)); | 1681 ASSERT_THAT(CreateEntry("fourth", &entry), IsOk()); |
1677 entry->Close(); | 1682 entry->Close(); |
1678 ASSERT_EQ(net::OK, OpenEntry("fourth", &entry)); | 1683 ASSERT_THAT(OpenEntry("fourth", &entry), IsOk()); |
1679 entry->Close(); | 1684 entry->Close(); |
1680 FlushQueueForTest(); | 1685 FlushQueueForTest(); |
1681 | 1686 |
1682 AddDelay(); | 1687 AddDelay(); |
1683 Time final = Time::Now(); | 1688 Time final = Time::Now(); |
1684 | 1689 |
1685 ASSERT_EQ(4, cache_->GetEntryCount()); | 1690 ASSERT_EQ(4, cache_->GetEntryCount()); |
1686 EXPECT_EQ(net::OK, DoomEntriesBetween(middle_start, middle_end)); | 1691 EXPECT_THAT(DoomEntriesBetween(middle_start, middle_end), IsOk()); |
1687 ASSERT_EQ(2, cache_->GetEntryCount()); | 1692 ASSERT_EQ(2, cache_->GetEntryCount()); |
1688 | 1693 |
1689 ASSERT_EQ(net::OK, OpenEntry("fourth", &entry)); | 1694 ASSERT_THAT(OpenEntry("fourth", &entry), IsOk()); |
1690 entry->Close(); | 1695 entry->Close(); |
1691 | 1696 |
1692 EXPECT_EQ(net::OK, DoomEntriesBetween(middle_start, final)); | 1697 EXPECT_THAT(DoomEntriesBetween(middle_start, final), IsOk()); |
1693 ASSERT_EQ(1, cache_->GetEntryCount()); | 1698 ASSERT_EQ(1, cache_->GetEntryCount()); |
1694 | 1699 |
1695 ASSERT_EQ(net::OK, OpenEntry("first", &entry)); | 1700 ASSERT_THAT(OpenEntry("first", &entry), IsOk()); |
1696 entry->Close(); | 1701 entry->Close(); |
1697 } | 1702 } |
1698 | 1703 |
1699 TEST_F(DiskCacheBackendTest, DoomBetween) { | 1704 TEST_F(DiskCacheBackendTest, DoomBetween) { |
1700 BackendDoomBetween(); | 1705 BackendDoomBetween(); |
1701 } | 1706 } |
1702 | 1707 |
1703 TEST_F(DiskCacheBackendTest, NewEvictionDoomBetween) { | 1708 TEST_F(DiskCacheBackendTest, NewEvictionDoomBetween) { |
1704 SetNewEviction(); | 1709 SetNewEviction(); |
1705 BackendDoomBetween(); | 1710 BackendDoomBetween(); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1747 CreateSetOfRandomEntries(&key_pool); | 1752 CreateSetOfRandomEntries(&key_pool); |
1748 | 1753 |
1749 int count = 0; | 1754 int count = 0; |
1750 for (std::string key : key_pool) { | 1755 for (std::string key : key_pool) { |
1751 std::string data(count, ' '); | 1756 std::string data(count, ' '); |
1752 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); | 1757 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); |
1753 | 1758 |
1754 // Alternate between writing to first two streams to test that we do not | 1759 // Alternate between writing to first two streams to test that we do not |
1755 // take only one stream into account. | 1760 // take only one stream into account. |
1756 disk_cache::Entry* entry; | 1761 disk_cache::Entry* entry; |
1757 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); | 1762 ASSERT_THAT(OpenEntry(key, &entry), IsOk()); |
1758 ASSERT_EQ(count, WriteData(entry, count % 2, 0, buffer.get(), count, true)); | 1763 ASSERT_EQ(count, WriteData(entry, count % 2, 0, buffer.get(), count, true)); |
1759 entry->Close(); | 1764 entry->Close(); |
1760 | 1765 |
1761 ++count; | 1766 ++count; |
1762 } | 1767 } |
1763 | 1768 |
1764 // The resulting size should be (0 + 1 + ... + count - 1) plus keys. | 1769 // The resulting size should be (0 + 1 + ... + count - 1) plus keys. |
1765 int result = CalculateSizeOfAllEntries(); | 1770 int result = CalculateSizeOfAllEntries(); |
1766 int total_metadata_size = 0; | 1771 int total_metadata_size = 0; |
1767 for (std::string key : key_pool) | 1772 for (std::string key : key_pool) |
1768 total_metadata_size += GetEntryMetadataSize(key); | 1773 total_metadata_size += GetEntryMetadataSize(key); |
1769 EXPECT_EQ((count - 1) * count / 2 + total_metadata_size, result); | 1774 EXPECT_EQ((count - 1) * count / 2 + total_metadata_size, result); |
1770 | 1775 |
1771 // Add another entry and test if the size is updated. Then remove it and test | 1776 // Add another entry and test if the size is updated. Then remove it and test |
1772 // if the size is back to original value. | 1777 // if the size is back to original value. |
1773 { | 1778 { |
1774 const int last_entry_size = 47; | 1779 const int last_entry_size = 47; |
1775 std::string data(last_entry_size, ' '); | 1780 std::string data(last_entry_size, ' '); |
1776 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); | 1781 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); |
1777 | 1782 |
1778 disk_cache::Entry* entry; | 1783 disk_cache::Entry* entry; |
1779 std::string key = GenerateKey(true); | 1784 std::string key = GenerateKey(true); |
1780 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 1785 ASSERT_THAT(CreateEntry(key, &entry), IsOk()); |
1781 ASSERT_EQ(last_entry_size, | 1786 ASSERT_EQ(last_entry_size, |
1782 WriteData(entry, 0, 0, buffer.get(), last_entry_size, true)); | 1787 WriteData(entry, 0, 0, buffer.get(), last_entry_size, true)); |
1783 entry->Close(); | 1788 entry->Close(); |
1784 | 1789 |
1785 int new_result = CalculateSizeOfAllEntries(); | 1790 int new_result = CalculateSizeOfAllEntries(); |
1786 EXPECT_EQ(result + last_entry_size + GetEntryMetadataSize(key), new_result); | 1791 EXPECT_EQ(result + last_entry_size + GetEntryMetadataSize(key), new_result); |
1787 | 1792 |
1788 DoomEntry(key); | 1793 DoomEntry(key); |
1789 new_result = CalculateSizeOfAllEntries(); | 1794 new_result = CalculateSizeOfAllEntries(); |
1790 EXPECT_EQ(result, new_result); | 1795 EXPECT_EQ(result, new_result); |
1791 } | 1796 } |
1792 | 1797 |
1793 // After dooming the entries, the size should be back to zero. | 1798 // After dooming the entries, the size should be back to zero. |
1794 ASSERT_EQ(net::OK, DoomAllEntries()); | 1799 ASSERT_THAT(DoomAllEntries(), IsOk()); |
1795 EXPECT_EQ(0, CalculateSizeOfAllEntries()); | 1800 EXPECT_EQ(0, CalculateSizeOfAllEntries()); |
1796 } | 1801 } |
1797 | 1802 |
1798 TEST_F(DiskCacheBackendTest, CalculateSizeOfAllEntries) { | 1803 TEST_F(DiskCacheBackendTest, CalculateSizeOfAllEntries) { |
1799 BackendCalculateSizeOfAllEntries(); | 1804 BackendCalculateSizeOfAllEntries(); |
1800 } | 1805 } |
1801 | 1806 |
1802 TEST_F(DiskCacheBackendTest, MemoryOnlyCalculateSizeOfAllEntries) { | 1807 TEST_F(DiskCacheBackendTest, MemoryOnlyCalculateSizeOfAllEntries) { |
1803 SetMemoryOnlyMode(); | 1808 SetMemoryOnlyMode(); |
1804 BackendCalculateSizeOfAllEntries(); | 1809 BackendCalculateSizeOfAllEntries(); |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1971 TEST_F(DiskCacheTest, WrongVersion) { | 1976 TEST_F(DiskCacheTest, WrongVersion) { |
1972 ASSERT_TRUE(CopyTestCache("wrong_version")); | 1977 ASSERT_TRUE(CopyTestCache("wrong_version")); |
1973 base::Thread cache_thread("CacheThread"); | 1978 base::Thread cache_thread("CacheThread"); |
1974 ASSERT_TRUE(cache_thread.StartWithOptions( | 1979 ASSERT_TRUE(cache_thread.StartWithOptions( |
1975 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 1980 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
1976 net::TestCompletionCallback cb; | 1981 net::TestCompletionCallback cb; |
1977 | 1982 |
1978 std::unique_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl( | 1983 std::unique_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl( |
1979 cache_path_, cache_thread.task_runner(), NULL)); | 1984 cache_path_, cache_thread.task_runner(), NULL)); |
1980 int rv = cache->Init(cb.callback()); | 1985 int rv = cache->Init(cb.callback()); |
1981 ASSERT_EQ(net::ERR_FAILED, cb.GetResult(rv)); | 1986 ASSERT_THAT(cb.GetResult(rv), IsError(net::ERR_FAILED)); |
1982 } | 1987 } |
1983 | 1988 |
1984 // Tests that the disk cache successfully joins the control group, dropping the | 1989 // Tests that the disk cache successfully joins the control group, dropping the |
1985 // existing cache in favour of a new empty cache. | 1990 // existing cache in favour of a new empty cache. |
1986 // Disabled on android since this test requires cache creator to create | 1991 // Disabled on android since this test requires cache creator to create |
1987 // blockfile caches. | 1992 // blockfile caches. |
1988 #if !defined(OS_ANDROID) | 1993 #if !defined(OS_ANDROID) |
1989 TEST_F(DiskCacheTest, SimpleCacheControlJoin) { | 1994 TEST_F(DiskCacheTest, SimpleCacheControlJoin) { |
1990 base::Thread cache_thread("CacheThread"); | 1995 base::Thread cache_thread("CacheThread"); |
1991 ASSERT_TRUE(cache_thread.StartWithOptions( | 1996 ASSERT_TRUE(cache_thread.StartWithOptions( |
(...skipping 13 matching lines...) Expand all Loading... |
2005 std::unique_ptr<disk_cache::Backend> base_cache; | 2010 std::unique_ptr<disk_cache::Backend> base_cache; |
2006 int rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, | 2011 int rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, |
2007 net::CACHE_BACKEND_BLOCKFILE, | 2012 net::CACHE_BACKEND_BLOCKFILE, |
2008 cache_path_, | 2013 cache_path_, |
2009 0, | 2014 0, |
2010 true, | 2015 true, |
2011 cache_thread.task_runner(), | 2016 cache_thread.task_runner(), |
2012 NULL, | 2017 NULL, |
2013 &base_cache, | 2018 &base_cache, |
2014 cb.callback()); | 2019 cb.callback()); |
2015 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 2020 ASSERT_THAT(cb.GetResult(rv), IsOk()); |
2016 EXPECT_EQ(0, base_cache->GetEntryCount()); | 2021 EXPECT_EQ(0, base_cache->GetEntryCount()); |
2017 } | 2022 } |
2018 #endif | 2023 #endif |
2019 | 2024 |
2020 // Tests that the disk cache can restart in the control group preserving | 2025 // Tests that the disk cache can restart in the control group preserving |
2021 // existing entries. | 2026 // existing entries. |
2022 TEST_F(DiskCacheTest, SimpleCacheControlRestart) { | 2027 TEST_F(DiskCacheTest, SimpleCacheControlRestart) { |
2023 // Instantiate the SimpleCacheTrial, forcing this run into the | 2028 // Instantiate the SimpleCacheTrial, forcing this run into the |
2024 // ExperimentControl group. | 2029 // ExperimentControl group. |
2025 base::FieldTrialList field_trial_list(new base::MockEntropyProvider()); | 2030 base::FieldTrialList field_trial_list(new base::MockEntropyProvider()); |
2026 base::FieldTrialList::CreateFieldTrial("SimpleCacheTrial", | 2031 base::FieldTrialList::CreateFieldTrial("SimpleCacheTrial", |
2027 "ExperimentControl"); | 2032 "ExperimentControl"); |
2028 | 2033 |
2029 base::Thread cache_thread("CacheThread"); | 2034 base::Thread cache_thread("CacheThread"); |
2030 ASSERT_TRUE(cache_thread.StartWithOptions( | 2035 ASSERT_TRUE(cache_thread.StartWithOptions( |
2031 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 2036 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
2032 | 2037 |
2033 std::unique_ptr<disk_cache::BackendImpl> cache = | 2038 std::unique_ptr<disk_cache::BackendImpl> cache = |
2034 CreateExistingEntryCache(cache_thread, cache_path_); | 2039 CreateExistingEntryCache(cache_thread, cache_path_); |
2035 ASSERT_TRUE(cache.get()); | 2040 ASSERT_TRUE(cache.get()); |
2036 | 2041 |
2037 net::TestCompletionCallback cb; | 2042 net::TestCompletionCallback cb; |
2038 | 2043 |
2039 const int kRestartCount = 5; | 2044 const int kRestartCount = 5; |
2040 for (int i = 0; i < kRestartCount; ++i) { | 2045 for (int i = 0; i < kRestartCount; ++i) { |
2041 cache.reset(new disk_cache::BackendImpl(cache_path_, | 2046 cache.reset(new disk_cache::BackendImpl(cache_path_, |
2042 cache_thread.task_runner(), NULL)); | 2047 cache_thread.task_runner(), NULL)); |
2043 int rv = cache->Init(cb.callback()); | 2048 int rv = cache->Init(cb.callback()); |
2044 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 2049 ASSERT_THAT(cb.GetResult(rv), IsOk()); |
2045 EXPECT_EQ(1, cache->GetEntryCount()); | 2050 EXPECT_EQ(1, cache->GetEntryCount()); |
2046 | 2051 |
2047 disk_cache::Entry* entry = NULL; | 2052 disk_cache::Entry* entry = NULL; |
2048 rv = cache->OpenEntry(kExistingEntryKey, &entry, cb.callback()); | 2053 rv = cache->OpenEntry(kExistingEntryKey, &entry, cb.callback()); |
2049 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 2054 ASSERT_THAT(cb.GetResult(rv), IsOk()); |
2050 EXPECT_NE(nullptr, entry); | 2055 EXPECT_NE(nullptr, entry); |
2051 entry->Close(); | 2056 entry->Close(); |
2052 } | 2057 } |
2053 } | 2058 } |
2054 | 2059 |
2055 // Tests that the disk cache can leave the control group preserving existing | 2060 // Tests that the disk cache can leave the control group preserving existing |
2056 // entries. | 2061 // entries. |
2057 TEST_F(DiskCacheTest, SimpleCacheControlLeave) { | 2062 TEST_F(DiskCacheTest, SimpleCacheControlLeave) { |
2058 base::Thread cache_thread("CacheThread"); | 2063 base::Thread cache_thread("CacheThread"); |
2059 ASSERT_TRUE(cache_thread.StartWithOptions( | 2064 ASSERT_TRUE(cache_thread.StartWithOptions( |
(...skipping 15 matching lines...) Expand all Loading... |
2075 // ExperimentNo group. | 2080 // ExperimentNo group. |
2076 base::FieldTrialList field_trial_list(new base::MockEntropyProvider()); | 2081 base::FieldTrialList field_trial_list(new base::MockEntropyProvider()); |
2077 base::FieldTrialList::CreateFieldTrial("SimpleCacheTrial", "ExperimentNo"); | 2082 base::FieldTrialList::CreateFieldTrial("SimpleCacheTrial", "ExperimentNo"); |
2078 net::TestCompletionCallback cb; | 2083 net::TestCompletionCallback cb; |
2079 | 2084 |
2080 const int kRestartCount = 5; | 2085 const int kRestartCount = 5; |
2081 for (int i = 0; i < kRestartCount; ++i) { | 2086 for (int i = 0; i < kRestartCount; ++i) { |
2082 std::unique_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl( | 2087 std::unique_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl( |
2083 cache_path_, cache_thread.task_runner(), NULL)); | 2088 cache_path_, cache_thread.task_runner(), NULL)); |
2084 int rv = cache->Init(cb.callback()); | 2089 int rv = cache->Init(cb.callback()); |
2085 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 2090 ASSERT_THAT(cb.GetResult(rv), IsOk()); |
2086 EXPECT_EQ(1, cache->GetEntryCount()); | 2091 EXPECT_EQ(1, cache->GetEntryCount()); |
2087 | 2092 |
2088 disk_cache::Entry* entry = NULL; | 2093 disk_cache::Entry* entry = NULL; |
2089 rv = cache->OpenEntry(kExistingEntryKey, &entry, cb.callback()); | 2094 rv = cache->OpenEntry(kExistingEntryKey, &entry, cb.callback()); |
2090 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 2095 ASSERT_THAT(cb.GetResult(rv), IsOk()); |
2091 EXPECT_NE(nullptr, entry); | 2096 EXPECT_NE(nullptr, entry); |
2092 entry->Close(); | 2097 entry->Close(); |
2093 } | 2098 } |
2094 } | 2099 } |
2095 | 2100 |
2096 // Tests that the cache is properly restarted on recovery error. | 2101 // Tests that the cache is properly restarted on recovery error. |
2097 // Disabled on android since this test requires cache creator to create | 2102 // Disabled on android since this test requires cache creator to create |
2098 // blockfile caches. | 2103 // blockfile caches. |
2099 #if !defined(OS_ANDROID) | 2104 #if !defined(OS_ANDROID) |
2100 TEST_F(DiskCacheBackendTest, DeleteOld) { | 2105 TEST_F(DiskCacheBackendTest, DeleteOld) { |
2101 ASSERT_TRUE(CopyTestCache("wrong_version")); | 2106 ASSERT_TRUE(CopyTestCache("wrong_version")); |
2102 SetNewEviction(); | 2107 SetNewEviction(); |
2103 base::Thread cache_thread("CacheThread"); | 2108 base::Thread cache_thread("CacheThread"); |
2104 ASSERT_TRUE(cache_thread.StartWithOptions( | 2109 ASSERT_TRUE(cache_thread.StartWithOptions( |
2105 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 2110 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
2106 | 2111 |
2107 net::TestCompletionCallback cb; | 2112 net::TestCompletionCallback cb; |
2108 bool prev = base::ThreadRestrictions::SetIOAllowed(false); | 2113 bool prev = base::ThreadRestrictions::SetIOAllowed(false); |
2109 base::FilePath path(cache_path_); | 2114 base::FilePath path(cache_path_); |
2110 int rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, | 2115 int rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, |
2111 net::CACHE_BACKEND_BLOCKFILE, | 2116 net::CACHE_BACKEND_BLOCKFILE, |
2112 path, | 2117 path, |
2113 0, | 2118 0, |
2114 true, | 2119 true, |
2115 cache_thread.task_runner(), | 2120 cache_thread.task_runner(), |
2116 NULL, | 2121 NULL, |
2117 &cache_, | 2122 &cache_, |
2118 cb.callback()); | 2123 cb.callback()); |
2119 path.clear(); // Make sure path was captured by the previous call. | 2124 path.clear(); // Make sure path was captured by the previous call. |
2120 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 2125 ASSERT_THAT(cb.GetResult(rv), IsOk()); |
2121 base::ThreadRestrictions::SetIOAllowed(prev); | 2126 base::ThreadRestrictions::SetIOAllowed(prev); |
2122 cache_.reset(); | 2127 cache_.reset(); |
2123 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); | 2128 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_)); |
2124 } | 2129 } |
2125 #endif | 2130 #endif |
2126 | 2131 |
2127 // We want to be able to deal with messed up entries on disk. | 2132 // We want to be able to deal with messed up entries on disk. |
2128 void DiskCacheBackendTest::BackendInvalidEntry2() { | 2133 void DiskCacheBackendTest::BackendInvalidEntry2() { |
2129 ASSERT_TRUE(CopyTestCache("bad_entry")); | 2134 ASSERT_TRUE(CopyTestCache("bad_entry")); |
2130 DisableFirstCleanup(); | 2135 DisableFirstCleanup(); |
2131 InitCache(); | 2136 InitCache(); |
2132 | 2137 |
2133 disk_cache::Entry *entry1, *entry2; | 2138 disk_cache::Entry *entry1, *entry2; |
2134 ASSERT_EQ(net::OK, OpenEntry("the first key", &entry1)); | 2139 ASSERT_THAT(OpenEntry("the first key", &entry1), IsOk()); |
2135 EXPECT_NE(net::OK, OpenEntry("some other key", &entry2)); | 2140 EXPECT_NE(net::OK, OpenEntry("some other key", &entry2)); |
2136 entry1->Close(); | 2141 entry1->Close(); |
2137 | 2142 |
2138 // CheckCacheIntegrity will fail at this point. | 2143 // CheckCacheIntegrity will fail at this point. |
2139 DisableIntegrityCheck(); | 2144 DisableIntegrityCheck(); |
2140 } | 2145 } |
2141 | 2146 |
2142 TEST_F(DiskCacheBackendTest, InvalidEntry2) { | 2147 TEST_F(DiskCacheBackendTest, InvalidEntry2) { |
2143 BackendInvalidEntry2(); | 2148 BackendInvalidEntry2(); |
2144 } | 2149 } |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2220 ASSERT_TRUE(CopyTestCache("list_loop2")); | 2225 ASSERT_TRUE(CopyTestCache("list_loop2")); |
2221 SetMask(0x1); // 2-entry table. | 2226 SetMask(0x1); // 2-entry table. |
2222 SetMaxSize(0x3000); // 12 kB. | 2227 SetMaxSize(0x3000); // 12 kB. |
2223 DisableFirstCleanup(); | 2228 DisableFirstCleanup(); |
2224 InitCache(); | 2229 InitCache(); |
2225 | 2230 |
2226 // The second entry points at itselft, and the first entry is not accessible | 2231 // The second entry points at itselft, and the first entry is not accessible |
2227 // though the index, but it is at the head of the LRU. | 2232 // though the index, but it is at the head of the LRU. |
2228 | 2233 |
2229 disk_cache::Entry* entry; | 2234 disk_cache::Entry* entry; |
2230 ASSERT_EQ(net::OK, CreateEntry("The first key", &entry)); | 2235 ASSERT_THAT(CreateEntry("The first key", &entry), IsOk()); |
2231 entry->Close(); | 2236 entry->Close(); |
2232 | 2237 |
2233 TrimForTest(false); | 2238 TrimForTest(false); |
2234 TrimForTest(false); | 2239 TrimForTest(false); |
2235 ASSERT_EQ(net::OK, OpenEntry("The first key", &entry)); | 2240 ASSERT_THAT(OpenEntry("The first key", &entry), IsOk()); |
2236 entry->Close(); | 2241 entry->Close(); |
2237 EXPECT_EQ(1, cache_->GetEntryCount()); | 2242 EXPECT_EQ(1, cache_->GetEntryCount()); |
2238 } | 2243 } |
2239 | 2244 |
2240 // Tests that we don't hang when there is a loop on the hash collision list. | 2245 // Tests that we don't hang when there is a loop on the hash collision list. |
2241 // The test cache could be a result of bug 69135. | 2246 // The test cache could be a result of bug 69135. |
2242 TEST_F(DiskCacheBackendTest, BadNextEntry2) { | 2247 TEST_F(DiskCacheBackendTest, BadNextEntry2) { |
2243 ASSERT_TRUE(CopyTestCache("list_loop3")); | 2248 ASSERT_TRUE(CopyTestCache("list_loop3")); |
2244 SetMask(0x1); // 2-entry table. | 2249 SetMask(0x1); // 2-entry table. |
2245 SetMaxSize(0x3000); // 12 kB. | 2250 SetMaxSize(0x3000); // 12 kB. |
2246 DisableFirstCleanup(); | 2251 DisableFirstCleanup(); |
2247 InitCache(); | 2252 InitCache(); |
2248 | 2253 |
2249 // There is a wide loop of 5 entries. | 2254 // There is a wide loop of 5 entries. |
2250 | 2255 |
2251 disk_cache::Entry* entry; | 2256 disk_cache::Entry* entry; |
2252 ASSERT_NE(net::OK, OpenEntry("Not present key", &entry)); | 2257 ASSERT_NE(net::OK, OpenEntry("Not present key", &entry)); |
2253 } | 2258 } |
2254 | 2259 |
2255 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry6) { | 2260 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry6) { |
2256 ASSERT_TRUE(CopyTestCache("bad_rankings3")); | 2261 ASSERT_TRUE(CopyTestCache("bad_rankings3")); |
2257 DisableFirstCleanup(); | 2262 DisableFirstCleanup(); |
2258 SetNewEviction(); | 2263 SetNewEviction(); |
2259 InitCache(); | 2264 InitCache(); |
2260 | 2265 |
2261 // The second entry is dirty, but removing it should not corrupt the list. | 2266 // The second entry is dirty, but removing it should not corrupt the list. |
2262 disk_cache::Entry* entry; | 2267 disk_cache::Entry* entry; |
2263 ASSERT_NE(net::OK, OpenEntry("the second key", &entry)); | 2268 ASSERT_NE(net::OK, OpenEntry("the second key", &entry)); |
2264 ASSERT_EQ(net::OK, OpenEntry("the first key", &entry)); | 2269 ASSERT_THAT(OpenEntry("the first key", &entry), IsOk()); |
2265 | 2270 |
2266 // This should not delete the cache. | 2271 // This should not delete the cache. |
2267 entry->Doom(); | 2272 entry->Doom(); |
2268 FlushQueueForTest(); | 2273 FlushQueueForTest(); |
2269 entry->Close(); | 2274 entry->Close(); |
2270 | 2275 |
2271 ASSERT_EQ(net::OK, OpenEntry("some other key", &entry)); | 2276 ASSERT_THAT(OpenEntry("some other key", &entry), IsOk()); |
2272 entry->Close(); | 2277 entry->Close(); |
2273 } | 2278 } |
2274 | 2279 |
2275 // Tests handling of corrupt entries by keeping the rankings node around, with | 2280 // Tests handling of corrupt entries by keeping the rankings node around, with |
2276 // a fatal failure. | 2281 // a fatal failure. |
2277 void DiskCacheBackendTest::BackendInvalidEntry7() { | 2282 void DiskCacheBackendTest::BackendInvalidEntry7() { |
2278 const int kSize = 0x3000; // 12 kB. | 2283 const int kSize = 0x3000; // 12 kB. |
2279 SetMaxSize(kSize * 10); | 2284 SetMaxSize(kSize * 10); |
2280 InitCache(); | 2285 InitCache(); |
2281 | 2286 |
2282 std::string first("some key"); | 2287 std::string first("some key"); |
2283 std::string second("something else"); | 2288 std::string second("something else"); |
2284 disk_cache::Entry* entry; | 2289 disk_cache::Entry* entry; |
2285 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); | 2290 ASSERT_THAT(CreateEntry(first, &entry), IsOk()); |
2286 entry->Close(); | 2291 entry->Close(); |
2287 ASSERT_EQ(net::OK, CreateEntry(second, &entry)); | 2292 ASSERT_THAT(CreateEntry(second, &entry), IsOk()); |
2288 | 2293 |
2289 // Corrupt this entry. | 2294 // Corrupt this entry. |
2290 disk_cache::EntryImpl* entry_impl = | 2295 disk_cache::EntryImpl* entry_impl = |
2291 static_cast<disk_cache::EntryImpl*>(entry); | 2296 static_cast<disk_cache::EntryImpl*>(entry); |
2292 | 2297 |
2293 entry_impl->rankings()->Data()->next = 0; | 2298 entry_impl->rankings()->Data()->next = 0; |
2294 entry_impl->rankings()->Store(); | 2299 entry_impl->rankings()->Store(); |
2295 entry->Close(); | 2300 entry->Close(); |
2296 FlushQueueForTest(); | 2301 FlushQueueForTest(); |
2297 EXPECT_EQ(2, cache_->GetEntryCount()); | 2302 EXPECT_EQ(2, cache_->GetEntryCount()); |
(...skipping 21 matching lines...) Expand all Loading... |
2319 // Tests handling of corrupt entries by keeping the rankings node around, with | 2324 // Tests handling of corrupt entries by keeping the rankings node around, with |
2320 // a non fatal failure. | 2325 // a non fatal failure. |
2321 void DiskCacheBackendTest::BackendInvalidEntry8() { | 2326 void DiskCacheBackendTest::BackendInvalidEntry8() { |
2322 const int kSize = 0x3000; // 12 kB | 2327 const int kSize = 0x3000; // 12 kB |
2323 SetMaxSize(kSize * 10); | 2328 SetMaxSize(kSize * 10); |
2324 InitCache(); | 2329 InitCache(); |
2325 | 2330 |
2326 std::string first("some key"); | 2331 std::string first("some key"); |
2327 std::string second("something else"); | 2332 std::string second("something else"); |
2328 disk_cache::Entry* entry; | 2333 disk_cache::Entry* entry; |
2329 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); | 2334 ASSERT_THAT(CreateEntry(first, &entry), IsOk()); |
2330 entry->Close(); | 2335 entry->Close(); |
2331 ASSERT_EQ(net::OK, CreateEntry(second, &entry)); | 2336 ASSERT_THAT(CreateEntry(second, &entry), IsOk()); |
2332 | 2337 |
2333 // Corrupt this entry. | 2338 // Corrupt this entry. |
2334 disk_cache::EntryImpl* entry_impl = | 2339 disk_cache::EntryImpl* entry_impl = |
2335 static_cast<disk_cache::EntryImpl*>(entry); | 2340 static_cast<disk_cache::EntryImpl*>(entry); |
2336 | 2341 |
2337 entry_impl->rankings()->Data()->contents = 0; | 2342 entry_impl->rankings()->Data()->contents = 0; |
2338 entry_impl->rankings()->Store(); | 2343 entry_impl->rankings()->Store(); |
2339 entry->Close(); | 2344 entry->Close(); |
2340 FlushQueueForTest(); | 2345 FlushQueueForTest(); |
2341 EXPECT_EQ(2, cache_->GetEntryCount()); | 2346 EXPECT_EQ(2, cache_->GetEntryCount()); |
2342 | 2347 |
2343 // This should detect the bad entry. | 2348 // This should detect the bad entry. |
2344 EXPECT_NE(net::OK, OpenEntry(second, &entry)); | 2349 EXPECT_NE(net::OK, OpenEntry(second, &entry)); |
2345 EXPECT_EQ(1, cache_->GetEntryCount()); | 2350 EXPECT_EQ(1, cache_->GetEntryCount()); |
2346 | 2351 |
2347 // We should not delete the cache. | 2352 // We should not delete the cache. |
2348 std::unique_ptr<TestIterator> iter = CreateIterator(); | 2353 std::unique_ptr<TestIterator> iter = CreateIterator(); |
2349 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry)); | 2354 ASSERT_THAT(iter->OpenNextEntry(&entry), IsOk()); |
2350 entry->Close(); | 2355 entry->Close(); |
2351 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry)); | 2356 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry)); |
2352 EXPECT_EQ(1, cache_->GetEntryCount()); | 2357 EXPECT_EQ(1, cache_->GetEntryCount()); |
2353 } | 2358 } |
2354 | 2359 |
2355 TEST_F(DiskCacheBackendTest, InvalidEntry8) { | 2360 TEST_F(DiskCacheBackendTest, InvalidEntry8) { |
2356 BackendInvalidEntry8(); | 2361 BackendInvalidEntry8(); |
2357 } | 2362 } |
2358 | 2363 |
2359 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry8) { | 2364 TEST_F(DiskCacheBackendTest, NewEvictionInvalidEntry8) { |
2360 SetNewEviction(); | 2365 SetNewEviction(); |
2361 BackendInvalidEntry8(); | 2366 BackendInvalidEntry8(); |
2362 } | 2367 } |
2363 | 2368 |
2364 // Tests handling of corrupt entries detected by enumerations. Note that these | 2369 // Tests handling of corrupt entries detected by enumerations. Note that these |
2365 // tests (xx9 to xx11) are basically just going though slightly different | 2370 // tests (xx9 to xx11) are basically just going though slightly different |
2366 // codepaths so they are tighlty coupled with the code, but that is better than | 2371 // codepaths so they are tighlty coupled with the code, but that is better than |
2367 // not testing error handling code. | 2372 // not testing error handling code. |
2368 void DiskCacheBackendTest::BackendInvalidEntry9(bool eviction) { | 2373 void DiskCacheBackendTest::BackendInvalidEntry9(bool eviction) { |
2369 const int kSize = 0x3000; // 12 kB. | 2374 const int kSize = 0x3000; // 12 kB. |
2370 SetMaxSize(kSize * 10); | 2375 SetMaxSize(kSize * 10); |
2371 InitCache(); | 2376 InitCache(); |
2372 | 2377 |
2373 std::string first("some key"); | 2378 std::string first("some key"); |
2374 std::string second("something else"); | 2379 std::string second("something else"); |
2375 disk_cache::Entry* entry; | 2380 disk_cache::Entry* entry; |
2376 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); | 2381 ASSERT_THAT(CreateEntry(first, &entry), IsOk()); |
2377 entry->Close(); | 2382 entry->Close(); |
2378 ASSERT_EQ(net::OK, CreateEntry(second, &entry)); | 2383 ASSERT_THAT(CreateEntry(second, &entry), IsOk()); |
2379 | 2384 |
2380 // Corrupt this entry. | 2385 // Corrupt this entry. |
2381 disk_cache::EntryImpl* entry_impl = | 2386 disk_cache::EntryImpl* entry_impl = |
2382 static_cast<disk_cache::EntryImpl*>(entry); | 2387 static_cast<disk_cache::EntryImpl*>(entry); |
2383 | 2388 |
2384 entry_impl->entry()->Data()->state = 0xbad; | 2389 entry_impl->entry()->Data()->state = 0xbad; |
2385 entry_impl->entry()->Store(); | 2390 entry_impl->entry()->Store(); |
2386 entry->Close(); | 2391 entry->Close(); |
2387 FlushQueueForTest(); | 2392 FlushQueueForTest(); |
2388 EXPECT_EQ(2, cache_->GetEntryCount()); | 2393 EXPECT_EQ(2, cache_->GetEntryCount()); |
2389 | 2394 |
2390 if (eviction) { | 2395 if (eviction) { |
2391 TrimForTest(false); | 2396 TrimForTest(false); |
2392 EXPECT_EQ(1, cache_->GetEntryCount()); | 2397 EXPECT_EQ(1, cache_->GetEntryCount()); |
2393 TrimForTest(false); | 2398 TrimForTest(false); |
2394 EXPECT_EQ(1, cache_->GetEntryCount()); | 2399 EXPECT_EQ(1, cache_->GetEntryCount()); |
2395 } else { | 2400 } else { |
2396 // We should detect the problem through the list, but we should not delete | 2401 // We should detect the problem through the list, but we should not delete |
2397 // the entry, just fail the iteration. | 2402 // the entry, just fail the iteration. |
2398 std::unique_ptr<TestIterator> iter = CreateIterator(); | 2403 std::unique_ptr<TestIterator> iter = CreateIterator(); |
2399 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry)); | 2404 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry)); |
2400 | 2405 |
2401 // Now a full iteration will work, and return one entry. | 2406 // Now a full iteration will work, and return one entry. |
2402 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry)); | 2407 ASSERT_THAT(iter->OpenNextEntry(&entry), IsOk()); |
2403 entry->Close(); | 2408 entry->Close(); |
2404 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry)); | 2409 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry)); |
2405 | 2410 |
2406 // This should detect what's left of the bad entry. | 2411 // This should detect what's left of the bad entry. |
2407 EXPECT_NE(net::OK, OpenEntry(second, &entry)); | 2412 EXPECT_NE(net::OK, OpenEntry(second, &entry)); |
2408 EXPECT_EQ(2, cache_->GetEntryCount()); | 2413 EXPECT_EQ(2, cache_->GetEntryCount()); |
2409 } | 2414 } |
2410 DisableIntegrityCheck(); | 2415 DisableIntegrityCheck(); |
2411 } | 2416 } |
2412 | 2417 |
(...skipping 18 matching lines...) Expand all Loading... |
2431 // Tests handling of corrupt entries detected by enumerations. | 2436 // Tests handling of corrupt entries detected by enumerations. |
2432 void DiskCacheBackendTest::BackendInvalidEntry10(bool eviction) { | 2437 void DiskCacheBackendTest::BackendInvalidEntry10(bool eviction) { |
2433 const int kSize = 0x3000; // 12 kB. | 2438 const int kSize = 0x3000; // 12 kB. |
2434 SetMaxSize(kSize * 10); | 2439 SetMaxSize(kSize * 10); |
2435 SetNewEviction(); | 2440 SetNewEviction(); |
2436 InitCache(); | 2441 InitCache(); |
2437 | 2442 |
2438 std::string first("some key"); | 2443 std::string first("some key"); |
2439 std::string second("something else"); | 2444 std::string second("something else"); |
2440 disk_cache::Entry* entry; | 2445 disk_cache::Entry* entry; |
2441 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); | 2446 ASSERT_THAT(CreateEntry(first, &entry), IsOk()); |
2442 entry->Close(); | 2447 entry->Close(); |
2443 ASSERT_EQ(net::OK, OpenEntry(first, &entry)); | 2448 ASSERT_THAT(OpenEntry(first, &entry), IsOk()); |
2444 EXPECT_EQ(0, WriteData(entry, 0, 200, NULL, 0, false)); | 2449 EXPECT_EQ(0, WriteData(entry, 0, 200, NULL, 0, false)); |
2445 entry->Close(); | 2450 entry->Close(); |
2446 ASSERT_EQ(net::OK, CreateEntry(second, &entry)); | 2451 ASSERT_THAT(CreateEntry(second, &entry), IsOk()); |
2447 | 2452 |
2448 // Corrupt this entry. | 2453 // Corrupt this entry. |
2449 disk_cache::EntryImpl* entry_impl = | 2454 disk_cache::EntryImpl* entry_impl = |
2450 static_cast<disk_cache::EntryImpl*>(entry); | 2455 static_cast<disk_cache::EntryImpl*>(entry); |
2451 | 2456 |
2452 entry_impl->entry()->Data()->state = 0xbad; | 2457 entry_impl->entry()->Data()->state = 0xbad; |
2453 entry_impl->entry()->Store(); | 2458 entry_impl->entry()->Store(); |
2454 entry->Close(); | 2459 entry->Close(); |
2455 ASSERT_EQ(net::OK, CreateEntry("third", &entry)); | 2460 ASSERT_THAT(CreateEntry("third", &entry), IsOk()); |
2456 entry->Close(); | 2461 entry->Close(); |
2457 EXPECT_EQ(3, cache_->GetEntryCount()); | 2462 EXPECT_EQ(3, cache_->GetEntryCount()); |
2458 | 2463 |
2459 // We have: | 2464 // We have: |
2460 // List 0: third -> second (bad). | 2465 // List 0: third -> second (bad). |
2461 // List 1: first. | 2466 // List 1: first. |
2462 | 2467 |
2463 if (eviction) { | 2468 if (eviction) { |
2464 // Detection order: second -> first -> third. | 2469 // Detection order: second -> first -> third. |
2465 TrimForTest(false); | 2470 TrimForTest(false); |
2466 EXPECT_EQ(3, cache_->GetEntryCount()); | 2471 EXPECT_EQ(3, cache_->GetEntryCount()); |
2467 TrimForTest(false); | 2472 TrimForTest(false); |
2468 EXPECT_EQ(2, cache_->GetEntryCount()); | 2473 EXPECT_EQ(2, cache_->GetEntryCount()); |
2469 TrimForTest(false); | 2474 TrimForTest(false); |
2470 EXPECT_EQ(1, cache_->GetEntryCount()); | 2475 EXPECT_EQ(1, cache_->GetEntryCount()); |
2471 } else { | 2476 } else { |
2472 // Detection order: third -> second -> first. | 2477 // Detection order: third -> second -> first. |
2473 // We should detect the problem through the list, but we should not delete | 2478 // We should detect the problem through the list, but we should not delete |
2474 // the entry. | 2479 // the entry. |
2475 std::unique_ptr<TestIterator> iter = CreateIterator(); | 2480 std::unique_ptr<TestIterator> iter = CreateIterator(); |
2476 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry)); | 2481 ASSERT_THAT(iter->OpenNextEntry(&entry), IsOk()); |
2477 entry->Close(); | 2482 entry->Close(); |
2478 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry)); | 2483 ASSERT_THAT(iter->OpenNextEntry(&entry), IsOk()); |
2479 EXPECT_EQ(first, entry->GetKey()); | 2484 EXPECT_EQ(first, entry->GetKey()); |
2480 entry->Close(); | 2485 entry->Close(); |
2481 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry)); | 2486 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry)); |
2482 } | 2487 } |
2483 DisableIntegrityCheck(); | 2488 DisableIntegrityCheck(); |
2484 } | 2489 } |
2485 | 2490 |
2486 TEST_F(DiskCacheBackendTest, InvalidEntry10) { | 2491 TEST_F(DiskCacheBackendTest, InvalidEntry10) { |
2487 BackendInvalidEntry10(false); | 2492 BackendInvalidEntry10(false); |
2488 } | 2493 } |
2489 | 2494 |
2490 TEST_F(DiskCacheBackendTest, TrimInvalidEntry10) { | 2495 TEST_F(DiskCacheBackendTest, TrimInvalidEntry10) { |
2491 BackendInvalidEntry10(true); | 2496 BackendInvalidEntry10(true); |
2492 } | 2497 } |
2493 | 2498 |
2494 // Tests handling of corrupt entries detected by enumerations. | 2499 // Tests handling of corrupt entries detected by enumerations. |
2495 void DiskCacheBackendTest::BackendInvalidEntry11(bool eviction) { | 2500 void DiskCacheBackendTest::BackendInvalidEntry11(bool eviction) { |
2496 const int kSize = 0x3000; // 12 kB. | 2501 const int kSize = 0x3000; // 12 kB. |
2497 SetMaxSize(kSize * 10); | 2502 SetMaxSize(kSize * 10); |
2498 SetNewEviction(); | 2503 SetNewEviction(); |
2499 InitCache(); | 2504 InitCache(); |
2500 | 2505 |
2501 std::string first("some key"); | 2506 std::string first("some key"); |
2502 std::string second("something else"); | 2507 std::string second("something else"); |
2503 disk_cache::Entry* entry; | 2508 disk_cache::Entry* entry; |
2504 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); | 2509 ASSERT_THAT(CreateEntry(first, &entry), IsOk()); |
2505 entry->Close(); | 2510 entry->Close(); |
2506 ASSERT_EQ(net::OK, OpenEntry(first, &entry)); | 2511 ASSERT_THAT(OpenEntry(first, &entry), IsOk()); |
2507 EXPECT_EQ(0, WriteData(entry, 0, 200, NULL, 0, false)); | 2512 EXPECT_EQ(0, WriteData(entry, 0, 200, NULL, 0, false)); |
2508 entry->Close(); | 2513 entry->Close(); |
2509 ASSERT_EQ(net::OK, CreateEntry(second, &entry)); | 2514 ASSERT_THAT(CreateEntry(second, &entry), IsOk()); |
2510 entry->Close(); | 2515 entry->Close(); |
2511 ASSERT_EQ(net::OK, OpenEntry(second, &entry)); | 2516 ASSERT_THAT(OpenEntry(second, &entry), IsOk()); |
2512 EXPECT_EQ(0, WriteData(entry, 0, 200, NULL, 0, false)); | 2517 EXPECT_EQ(0, WriteData(entry, 0, 200, NULL, 0, false)); |
2513 | 2518 |
2514 // Corrupt this entry. | 2519 // Corrupt this entry. |
2515 disk_cache::EntryImpl* entry_impl = | 2520 disk_cache::EntryImpl* entry_impl = |
2516 static_cast<disk_cache::EntryImpl*>(entry); | 2521 static_cast<disk_cache::EntryImpl*>(entry); |
2517 | 2522 |
2518 entry_impl->entry()->Data()->state = 0xbad; | 2523 entry_impl->entry()->Data()->state = 0xbad; |
2519 entry_impl->entry()->Store(); | 2524 entry_impl->entry()->Store(); |
2520 entry->Close(); | 2525 entry->Close(); |
2521 ASSERT_EQ(net::OK, CreateEntry("third", &entry)); | 2526 ASSERT_THAT(CreateEntry("third", &entry), IsOk()); |
2522 entry->Close(); | 2527 entry->Close(); |
2523 FlushQueueForTest(); | 2528 FlushQueueForTest(); |
2524 EXPECT_EQ(3, cache_->GetEntryCount()); | 2529 EXPECT_EQ(3, cache_->GetEntryCount()); |
2525 | 2530 |
2526 // We have: | 2531 // We have: |
2527 // List 0: third. | 2532 // List 0: third. |
2528 // List 1: second (bad) -> first. | 2533 // List 1: second (bad) -> first. |
2529 | 2534 |
2530 if (eviction) { | 2535 if (eviction) { |
2531 // Detection order: third -> first -> second. | 2536 // Detection order: third -> first -> second. |
2532 TrimForTest(false); | 2537 TrimForTest(false); |
2533 EXPECT_EQ(2, cache_->GetEntryCount()); | 2538 EXPECT_EQ(2, cache_->GetEntryCount()); |
2534 TrimForTest(false); | 2539 TrimForTest(false); |
2535 EXPECT_EQ(1, cache_->GetEntryCount()); | 2540 EXPECT_EQ(1, cache_->GetEntryCount()); |
2536 TrimForTest(false); | 2541 TrimForTest(false); |
2537 EXPECT_EQ(1, cache_->GetEntryCount()); | 2542 EXPECT_EQ(1, cache_->GetEntryCount()); |
2538 } else { | 2543 } else { |
2539 // Detection order: third -> second. | 2544 // Detection order: third -> second. |
2540 // We should detect the problem through the list, but we should not delete | 2545 // We should detect the problem through the list, but we should not delete |
2541 // the entry, just fail the iteration. | 2546 // the entry, just fail the iteration. |
2542 std::unique_ptr<TestIterator> iter = CreateIterator(); | 2547 std::unique_ptr<TestIterator> iter = CreateIterator(); |
2543 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry)); | 2548 ASSERT_THAT(iter->OpenNextEntry(&entry), IsOk()); |
2544 entry->Close(); | 2549 entry->Close(); |
2545 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry)); | 2550 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry)); |
2546 | 2551 |
2547 // Now a full iteration will work, and return two entries. | 2552 // Now a full iteration will work, and return two entries. |
2548 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry)); | 2553 ASSERT_THAT(iter->OpenNextEntry(&entry), IsOk()); |
2549 entry->Close(); | 2554 entry->Close(); |
2550 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry)); | 2555 ASSERT_THAT(iter->OpenNextEntry(&entry), IsOk()); |
2551 entry->Close(); | 2556 entry->Close(); |
2552 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry)); | 2557 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry)); |
2553 } | 2558 } |
2554 DisableIntegrityCheck(); | 2559 DisableIntegrityCheck(); |
2555 } | 2560 } |
2556 | 2561 |
2557 TEST_F(DiskCacheBackendTest, InvalidEntry11) { | 2562 TEST_F(DiskCacheBackendTest, InvalidEntry11) { |
2558 BackendInvalidEntry11(false); | 2563 BackendInvalidEntry11(false); |
2559 } | 2564 } |
2560 | 2565 |
2561 TEST_F(DiskCacheBackendTest, TrimInvalidEntry11) { | 2566 TEST_F(DiskCacheBackendTest, TrimInvalidEntry11) { |
2562 BackendInvalidEntry11(true); | 2567 BackendInvalidEntry11(true); |
2563 } | 2568 } |
2564 | 2569 |
2565 // Tests handling of corrupt entries in the middle of a long eviction run. | 2570 // Tests handling of corrupt entries in the middle of a long eviction run. |
2566 void DiskCacheBackendTest::BackendTrimInvalidEntry12() { | 2571 void DiskCacheBackendTest::BackendTrimInvalidEntry12() { |
2567 const int kSize = 0x3000; // 12 kB | 2572 const int kSize = 0x3000; // 12 kB |
2568 SetMaxSize(kSize * 10); | 2573 SetMaxSize(kSize * 10); |
2569 InitCache(); | 2574 InitCache(); |
2570 | 2575 |
2571 std::string first("some key"); | 2576 std::string first("some key"); |
2572 std::string second("something else"); | 2577 std::string second("something else"); |
2573 disk_cache::Entry* entry; | 2578 disk_cache::Entry* entry; |
2574 ASSERT_EQ(net::OK, CreateEntry(first, &entry)); | 2579 ASSERT_THAT(CreateEntry(first, &entry), IsOk()); |
2575 entry->Close(); | 2580 entry->Close(); |
2576 ASSERT_EQ(net::OK, CreateEntry(second, &entry)); | 2581 ASSERT_THAT(CreateEntry(second, &entry), IsOk()); |
2577 | 2582 |
2578 // Corrupt this entry. | 2583 // Corrupt this entry. |
2579 disk_cache::EntryImpl* entry_impl = | 2584 disk_cache::EntryImpl* entry_impl = |
2580 static_cast<disk_cache::EntryImpl*>(entry); | 2585 static_cast<disk_cache::EntryImpl*>(entry); |
2581 | 2586 |
2582 entry_impl->entry()->Data()->state = 0xbad; | 2587 entry_impl->entry()->Data()->state = 0xbad; |
2583 entry_impl->entry()->Store(); | 2588 entry_impl->entry()->Store(); |
2584 entry->Close(); | 2589 entry->Close(); |
2585 ASSERT_EQ(net::OK, CreateEntry("third", &entry)); | 2590 ASSERT_THAT(CreateEntry("third", &entry), IsOk()); |
2586 entry->Close(); | 2591 entry->Close(); |
2587 ASSERT_EQ(net::OK, CreateEntry("fourth", &entry)); | 2592 ASSERT_THAT(CreateEntry("fourth", &entry), IsOk()); |
2588 TrimForTest(true); | 2593 TrimForTest(true); |
2589 EXPECT_EQ(1, cache_->GetEntryCount()); | 2594 EXPECT_EQ(1, cache_->GetEntryCount()); |
2590 entry->Close(); | 2595 entry->Close(); |
2591 DisableIntegrityCheck(); | 2596 DisableIntegrityCheck(); |
2592 } | 2597 } |
2593 | 2598 |
2594 TEST_F(DiskCacheBackendTest, TrimInvalidEntry12) { | 2599 TEST_F(DiskCacheBackendTest, TrimInvalidEntry12) { |
2595 BackendTrimInvalidEntry12(); | 2600 BackendTrimInvalidEntry12(); |
2596 } | 2601 } |
2597 | 2602 |
2598 TEST_F(DiskCacheBackendTest, NewEvictionTrimInvalidEntry12) { | 2603 TEST_F(DiskCacheBackendTest, NewEvictionTrimInvalidEntry12) { |
2599 SetNewEviction(); | 2604 SetNewEviction(); |
2600 BackendTrimInvalidEntry12(); | 2605 BackendTrimInvalidEntry12(); |
2601 } | 2606 } |
2602 | 2607 |
2603 // We want to be able to deal with messed up entries on disk. | 2608 // We want to be able to deal with messed up entries on disk. |
2604 void DiskCacheBackendTest::BackendInvalidRankings2() { | 2609 void DiskCacheBackendTest::BackendInvalidRankings2() { |
2605 ASSERT_TRUE(CopyTestCache("bad_rankings")); | 2610 ASSERT_TRUE(CopyTestCache("bad_rankings")); |
2606 DisableFirstCleanup(); | 2611 DisableFirstCleanup(); |
2607 InitCache(); | 2612 InitCache(); |
2608 | 2613 |
2609 disk_cache::Entry *entry1, *entry2; | 2614 disk_cache::Entry *entry1, *entry2; |
2610 EXPECT_NE(net::OK, OpenEntry("the first key", &entry1)); | 2615 EXPECT_NE(net::OK, OpenEntry("the first key", &entry1)); |
2611 ASSERT_EQ(net::OK, OpenEntry("some other key", &entry2)); | 2616 ASSERT_THAT(OpenEntry("some other key", &entry2), IsOk()); |
2612 entry2->Close(); | 2617 entry2->Close(); |
2613 | 2618 |
2614 // CheckCacheIntegrity will fail at this point. | 2619 // CheckCacheIntegrity will fail at this point. |
2615 DisableIntegrityCheck(); | 2620 DisableIntegrityCheck(); |
2616 } | 2621 } |
2617 | 2622 |
2618 TEST_F(DiskCacheBackendTest, InvalidRankings2) { | 2623 TEST_F(DiskCacheBackendTest, InvalidRankings2) { |
2619 BackendInvalidRankings2(); | 2624 BackendInvalidRankings2(); |
2620 } | 2625 } |
2621 | 2626 |
2622 TEST_F(DiskCacheBackendTest, NewEvictionInvalidRankings2) { | 2627 TEST_F(DiskCacheBackendTest, NewEvictionInvalidRankings2) { |
2623 SetNewEviction(); | 2628 SetNewEviction(); |
2624 BackendInvalidRankings2(); | 2629 BackendInvalidRankings2(); |
2625 } | 2630 } |
2626 | 2631 |
2627 // If the LRU is corrupt, we delete the cache. | 2632 // If the LRU is corrupt, we delete the cache. |
2628 void DiskCacheBackendTest::BackendInvalidRankings() { | 2633 void DiskCacheBackendTest::BackendInvalidRankings() { |
2629 disk_cache::Entry* entry; | 2634 disk_cache::Entry* entry; |
2630 std::unique_ptr<TestIterator> iter = CreateIterator(); | 2635 std::unique_ptr<TestIterator> iter = CreateIterator(); |
2631 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry)); | 2636 ASSERT_THAT(iter->OpenNextEntry(&entry), IsOk()); |
2632 entry->Close(); | 2637 entry->Close(); |
2633 EXPECT_EQ(2, cache_->GetEntryCount()); | 2638 EXPECT_EQ(2, cache_->GetEntryCount()); |
2634 | 2639 |
2635 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry)); | 2640 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry)); |
2636 FlushQueueForTest(); // Allow the restart to finish. | 2641 FlushQueueForTest(); // Allow the restart to finish. |
2637 EXPECT_EQ(0, cache_->GetEntryCount()); | 2642 EXPECT_EQ(0, cache_->GetEntryCount()); |
2638 } | 2643 } |
2639 | 2644 |
2640 TEST_F(DiskCacheBackendTest, InvalidRankingsSuccess) { | 2645 TEST_F(DiskCacheBackendTest, InvalidRankingsSuccess) { |
2641 ASSERT_TRUE(CopyTestCache("bad_rankings")); | 2646 ASSERT_TRUE(CopyTestCache("bad_rankings")); |
(...skipping 24 matching lines...) Expand all Loading... |
2666 SetNewEviction(); | 2671 SetNewEviction(); |
2667 InitCache(); | 2672 InitCache(); |
2668 SetTestMode(); // Fail cache reinitialization. | 2673 SetTestMode(); // Fail cache reinitialization. |
2669 BackendInvalidRankings(); | 2674 BackendInvalidRankings(); |
2670 } | 2675 } |
2671 | 2676 |
2672 // If the LRU is corrupt and we have open entries, we disable the cache. | 2677 // If the LRU is corrupt and we have open entries, we disable the cache. |
2673 void DiskCacheBackendTest::BackendDisable() { | 2678 void DiskCacheBackendTest::BackendDisable() { |
2674 disk_cache::Entry *entry1, *entry2; | 2679 disk_cache::Entry *entry1, *entry2; |
2675 std::unique_ptr<TestIterator> iter = CreateIterator(); | 2680 std::unique_ptr<TestIterator> iter = CreateIterator(); |
2676 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry1)); | 2681 ASSERT_THAT(iter->OpenNextEntry(&entry1), IsOk()); |
2677 | 2682 |
2678 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry2)); | 2683 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry2)); |
2679 EXPECT_EQ(0, cache_->GetEntryCount()); | 2684 EXPECT_EQ(0, cache_->GetEntryCount()); |
2680 EXPECT_NE(net::OK, CreateEntry("Something new", &entry2)); | 2685 EXPECT_NE(net::OK, CreateEntry("Something new", &entry2)); |
2681 | 2686 |
2682 entry1->Close(); | 2687 entry1->Close(); |
2683 FlushQueueForTest(); // Flushing the Close posts a task to restart the cache. | 2688 FlushQueueForTest(); // Flushing the Close posts a task to restart the cache. |
2684 FlushQueueForTest(); // This one actually allows that task to complete. | 2689 FlushQueueForTest(); // This one actually allows that task to complete. |
2685 | 2690 |
2686 EXPECT_EQ(0, cache_->GetEntryCount()); | 2691 EXPECT_EQ(0, cache_->GetEntryCount()); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2766 InitCache(); | 2771 InitCache(); |
2767 SetTestMode(); // Fail cache reinitialization. | 2772 SetTestMode(); // Fail cache reinitialization. |
2768 BackendDisable2(); | 2773 BackendDisable2(); |
2769 } | 2774 } |
2770 | 2775 |
2771 // If the index size changes when we disable the cache, we should not crash. | 2776 // If the index size changes when we disable the cache, we should not crash. |
2772 void DiskCacheBackendTest::BackendDisable3() { | 2777 void DiskCacheBackendTest::BackendDisable3() { |
2773 disk_cache::Entry *entry1, *entry2; | 2778 disk_cache::Entry *entry1, *entry2; |
2774 std::unique_ptr<TestIterator> iter = CreateIterator(); | 2779 std::unique_ptr<TestIterator> iter = CreateIterator(); |
2775 EXPECT_EQ(2, cache_->GetEntryCount()); | 2780 EXPECT_EQ(2, cache_->GetEntryCount()); |
2776 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry1)); | 2781 ASSERT_THAT(iter->OpenNextEntry(&entry1), IsOk()); |
2777 entry1->Close(); | 2782 entry1->Close(); |
2778 | 2783 |
2779 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry2)); | 2784 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry2)); |
2780 FlushQueueForTest(); | 2785 FlushQueueForTest(); |
2781 | 2786 |
2782 ASSERT_EQ(net::OK, CreateEntry("Something new", &entry2)); | 2787 ASSERT_THAT(CreateEntry("Something new", &entry2), IsOk()); |
2783 entry2->Close(); | 2788 entry2->Close(); |
2784 | 2789 |
2785 EXPECT_EQ(1, cache_->GetEntryCount()); | 2790 EXPECT_EQ(1, cache_->GetEntryCount()); |
2786 } | 2791 } |
2787 | 2792 |
2788 TEST_F(DiskCacheBackendTest, DisableSuccess3) { | 2793 TEST_F(DiskCacheBackendTest, DisableSuccess3) { |
2789 ASSERT_TRUE(CopyTestCache("bad_rankings2")); | 2794 ASSERT_TRUE(CopyTestCache("bad_rankings2")); |
2790 DisableFirstCleanup(); | 2795 DisableFirstCleanup(); |
2791 SetMaxSize(20 * 1024 * 1024); | 2796 SetMaxSize(20 * 1024 * 1024); |
2792 InitCache(); | 2797 InitCache(); |
2793 BackendDisable3(); | 2798 BackendDisable3(); |
2794 } | 2799 } |
2795 | 2800 |
2796 TEST_F(DiskCacheBackendTest, NewEvictionDisableSuccess3) { | 2801 TEST_F(DiskCacheBackendTest, NewEvictionDisableSuccess3) { |
2797 ASSERT_TRUE(CopyTestCache("bad_rankings2")); | 2802 ASSERT_TRUE(CopyTestCache("bad_rankings2")); |
2798 DisableFirstCleanup(); | 2803 DisableFirstCleanup(); |
2799 SetMaxSize(20 * 1024 * 1024); | 2804 SetMaxSize(20 * 1024 * 1024); |
2800 SetNewEviction(); | 2805 SetNewEviction(); |
2801 InitCache(); | 2806 InitCache(); |
2802 BackendDisable3(); | 2807 BackendDisable3(); |
2803 } | 2808 } |
2804 | 2809 |
2805 // If we disable the cache, already open entries should work as far as possible. | 2810 // If we disable the cache, already open entries should work as far as possible. |
2806 void DiskCacheBackendTest::BackendDisable4() { | 2811 void DiskCacheBackendTest::BackendDisable4() { |
2807 disk_cache::Entry *entry1, *entry2, *entry3, *entry4; | 2812 disk_cache::Entry *entry1, *entry2, *entry3, *entry4; |
2808 std::unique_ptr<TestIterator> iter = CreateIterator(); | 2813 std::unique_ptr<TestIterator> iter = CreateIterator(); |
2809 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry1)); | 2814 ASSERT_THAT(iter->OpenNextEntry(&entry1), IsOk()); |
2810 | 2815 |
2811 char key2[2000]; | 2816 char key2[2000]; |
2812 char key3[20000]; | 2817 char key3[20000]; |
2813 CacheTestFillBuffer(key2, sizeof(key2), true); | 2818 CacheTestFillBuffer(key2, sizeof(key2), true); |
2814 CacheTestFillBuffer(key3, sizeof(key3), true); | 2819 CacheTestFillBuffer(key3, sizeof(key3), true); |
2815 key2[sizeof(key2) - 1] = '\0'; | 2820 key2[sizeof(key2) - 1] = '\0'; |
2816 key3[sizeof(key3) - 1] = '\0'; | 2821 key3[sizeof(key3) - 1] = '\0'; |
2817 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2)); | 2822 ASSERT_THAT(CreateEntry(key2, &entry2), IsOk()); |
2818 ASSERT_EQ(net::OK, CreateEntry(key3, &entry3)); | 2823 ASSERT_THAT(CreateEntry(key3, &entry3), IsOk()); |
2819 | 2824 |
2820 const int kBufSize = 20000; | 2825 const int kBufSize = 20000; |
2821 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufSize)); | 2826 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufSize)); |
2822 memset(buf->data(), 0, kBufSize); | 2827 memset(buf->data(), 0, kBufSize); |
2823 EXPECT_EQ(100, WriteData(entry2, 0, 0, buf.get(), 100, false)); | 2828 EXPECT_EQ(100, WriteData(entry2, 0, 0, buf.get(), 100, false)); |
2824 EXPECT_EQ(kBufSize, WriteData(entry3, 0, 0, buf.get(), kBufSize, false)); | 2829 EXPECT_EQ(kBufSize, WriteData(entry3, 0, 0, buf.get(), kBufSize, false)); |
2825 | 2830 |
2826 // This line should disable the cache but not delete it. | 2831 // This line should disable the cache but not delete it. |
2827 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry4)); | 2832 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry4)); |
2828 EXPECT_EQ(0, cache_->GetEntryCount()); | 2833 EXPECT_EQ(0, cache_->GetEntryCount()); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2866 BackendDisable4(); | 2871 BackendDisable4(); |
2867 } | 2872 } |
2868 | 2873 |
2869 // Tests the exposed API with a disabled cache. | 2874 // Tests the exposed API with a disabled cache. |
2870 void DiskCacheBackendTest::BackendDisabledAPI() { | 2875 void DiskCacheBackendTest::BackendDisabledAPI() { |
2871 cache_impl_->SetUnitTestMode(); // Simulate failure restarting the cache. | 2876 cache_impl_->SetUnitTestMode(); // Simulate failure restarting the cache. |
2872 | 2877 |
2873 disk_cache::Entry* entry1, *entry2; | 2878 disk_cache::Entry* entry1, *entry2; |
2874 std::unique_ptr<TestIterator> iter = CreateIterator(); | 2879 std::unique_ptr<TestIterator> iter = CreateIterator(); |
2875 EXPECT_EQ(2, cache_->GetEntryCount()); | 2880 EXPECT_EQ(2, cache_->GetEntryCount()); |
2876 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry1)); | 2881 ASSERT_THAT(iter->OpenNextEntry(&entry1), IsOk()); |
2877 entry1->Close(); | 2882 entry1->Close(); |
2878 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry2)); | 2883 EXPECT_NE(net::OK, iter->OpenNextEntry(&entry2)); |
2879 FlushQueueForTest(); | 2884 FlushQueueForTest(); |
2880 // The cache should be disabled. | 2885 // The cache should be disabled. |
2881 | 2886 |
2882 EXPECT_EQ(net::DISK_CACHE, cache_->GetCacheType()); | 2887 EXPECT_EQ(net::DISK_CACHE, cache_->GetCacheType()); |
2883 EXPECT_EQ(0, cache_->GetEntryCount()); | 2888 EXPECT_EQ(0, cache_->GetEntryCount()); |
2884 EXPECT_NE(net::OK, OpenEntry("First", &entry2)); | 2889 EXPECT_NE(net::OK, OpenEntry("First", &entry2)); |
2885 EXPECT_NE(net::OK, CreateEntry("Something new", &entry2)); | 2890 EXPECT_NE(net::OK, CreateEntry("Something new", &entry2)); |
2886 EXPECT_NE(net::OK, DoomEntry("First")); | 2891 EXPECT_NE(net::OK, DoomEntry("First")); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2925 SetMaxSize(kMaxSize); | 2930 SetMaxSize(kMaxSize); |
2926 InitSparseCache(nullptr, nullptr); | 2931 InitSparseCache(nullptr, nullptr); |
2927 | 2932 |
2928 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kWriteSize)); | 2933 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kWriteSize)); |
2929 CacheTestFillBuffer(buffer->data(), kWriteSize, false); | 2934 CacheTestFillBuffer(buffer->data(), kWriteSize, false); |
2930 | 2935 |
2931 std::string key_prefix("prefix"); | 2936 std::string key_prefix("prefix"); |
2932 for (int i = 0; i < kWriteEntryCount; ++i) { | 2937 for (int i = 0; i < kWriteEntryCount; ++i) { |
2933 AddDelay(); | 2938 AddDelay(); |
2934 disk_cache::Entry* entry = NULL; | 2939 disk_cache::Entry* entry = NULL; |
2935 ASSERT_EQ(net::OK, CreateEntry(key_prefix + base::IntToString(i), &entry)); | 2940 ASSERT_THAT(CreateEntry(key_prefix + base::IntToString(i), &entry), IsOk()); |
2936 disk_cache::ScopedEntryPtr entry_closer(entry); | 2941 disk_cache::ScopedEntryPtr entry_closer(entry); |
2937 EXPECT_EQ(kWriteSize, | 2942 EXPECT_EQ(kWriteSize, |
2938 WriteData(entry, 1, 0, buffer.get(), kWriteSize, false)); | 2943 WriteData(entry, 1, 0, buffer.get(), kWriteSize, false)); |
2939 } | 2944 } |
2940 | 2945 |
2941 int size = CalculateSizeOfAllEntries(); | 2946 int size = CalculateSizeOfAllEntries(); |
2942 EXPECT_GT(kMaxSize, size); | 2947 EXPECT_GT(kMaxSize, size); |
2943 } | 2948 } |
2944 | 2949 |
2945 TEST_F(DiskCacheBackendTest, BackendEviction) { | 2950 TEST_F(DiskCacheBackendTest, BackendEviction) { |
(...skipping 18 matching lines...) Expand all Loading... |
2964 const int kWriteSize = kMaxSize / kMaxEntryCount; | 2969 const int kWriteSize = kMaxSize / kMaxEntryCount; |
2965 | 2970 |
2966 SetMaxSize(kMaxSize); | 2971 SetMaxSize(kMaxSize); |
2967 InitCache(); | 2972 InitCache(); |
2968 | 2973 |
2969 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kWriteSize)); | 2974 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kWriteSize)); |
2970 CacheTestFillBuffer(buffer->data(), kWriteSize, false); | 2975 CacheTestFillBuffer(buffer->data(), kWriteSize, false); |
2971 | 2976 |
2972 // Create an entry to be our sparse entry that gets written later. | 2977 // Create an entry to be our sparse entry that gets written later. |
2973 disk_cache::Entry* entry; | 2978 disk_cache::Entry* entry; |
2974 ASSERT_EQ(net::OK, CreateEntry("first parent", &entry)); | 2979 ASSERT_THAT(CreateEntry("first parent", &entry), IsOk()); |
2975 disk_cache::ScopedEntryPtr first_parent(entry); | 2980 disk_cache::ScopedEntryPtr first_parent(entry); |
2976 | 2981 |
2977 // Create a ton of entries, and keep them open, to put the cache well above | 2982 // Create a ton of entries, and keep them open, to put the cache well above |
2978 // its eviction threshhold. | 2983 // its eviction threshhold. |
2979 const int kTooManyEntriesCount = kMaxEntryCount * 2; | 2984 const int kTooManyEntriesCount = kMaxEntryCount * 2; |
2980 std::list<disk_cache::ScopedEntryPtr> open_entries; | 2985 std::list<disk_cache::ScopedEntryPtr> open_entries; |
2981 std::string key_prefix("prefix"); | 2986 std::string key_prefix("prefix"); |
2982 for (int i = 0; i < kTooManyEntriesCount; ++i) { | 2987 for (int i = 0; i < kTooManyEntriesCount; ++i) { |
2983 ASSERT_EQ(net::OK, CreateEntry(key_prefix + base::IntToString(i), &entry)); | 2988 ASSERT_THAT(CreateEntry(key_prefix + base::IntToString(i), &entry), IsOk()); |
2984 EXPECT_EQ(kWriteSize, | 2989 EXPECT_EQ(kWriteSize, |
2985 WriteData(entry, 1, 0, buffer.get(), kWriteSize, false)); | 2990 WriteData(entry, 1, 0, buffer.get(), kWriteSize, false)); |
2986 open_entries.push_back(disk_cache::ScopedEntryPtr(entry)); | 2991 open_entries.push_back(disk_cache::ScopedEntryPtr(entry)); |
2987 } | 2992 } |
2988 EXPECT_LT(kMaxSize, CalculateSizeOfAllEntries()); | 2993 EXPECT_LT(kMaxSize, CalculateSizeOfAllEntries()); |
2989 | 2994 |
2990 // Writing this sparse data should not crash. | 2995 // Writing this sparse data should not crash. |
2991 EXPECT_EQ(1024, first_parent->WriteSparseData(32768, buffer.get(), 1024, | 2996 EXPECT_EQ(1024, first_parent->WriteSparseData(32768, buffer.get(), 1024, |
2992 net::CompletionCallback())); | 2997 net::CompletionCallback())); |
2993 } | 2998 } |
2994 | 2999 |
2995 TEST_F(DiskCacheTest, Backend_UsageStatsTimer) { | 3000 TEST_F(DiskCacheTest, Backend_UsageStatsTimer) { |
2996 MessageLoopHelper helper; | 3001 MessageLoopHelper helper; |
2997 | 3002 |
2998 ASSERT_TRUE(CleanupCacheDir()); | 3003 ASSERT_TRUE(CleanupCacheDir()); |
2999 std::unique_ptr<disk_cache::BackendImpl> cache; | 3004 std::unique_ptr<disk_cache::BackendImpl> cache; |
3000 cache.reset(new disk_cache::BackendImpl( | 3005 cache.reset(new disk_cache::BackendImpl( |
3001 cache_path_, base::ThreadTaskRunnerHandle::Get(), NULL)); | 3006 cache_path_, base::ThreadTaskRunnerHandle::Get(), NULL)); |
3002 ASSERT_TRUE(NULL != cache.get()); | 3007 ASSERT_TRUE(NULL != cache.get()); |
3003 cache->SetUnitTestMode(); | 3008 cache->SetUnitTestMode(); |
3004 ASSERT_EQ(net::OK, cache->SyncInit()); | 3009 ASSERT_THAT(cache->SyncInit(), IsOk()); |
3005 | 3010 |
3006 // Wait for a callback that never comes... about 2 secs :). The message loop | 3011 // Wait for a callback that never comes... about 2 secs :). The message loop |
3007 // has to run to allow invocation of the usage timer. | 3012 // has to run to allow invocation of the usage timer. |
3008 helper.WaitUntilCacheIoFinished(1); | 3013 helper.WaitUntilCacheIoFinished(1); |
3009 } | 3014 } |
3010 | 3015 |
3011 TEST_F(DiskCacheBackendTest, TimerNotCreated) { | 3016 TEST_F(DiskCacheBackendTest, TimerNotCreated) { |
3012 ASSERT_TRUE(CopyTestCache("wrong_version")); | 3017 ASSERT_TRUE(CopyTestCache("wrong_version")); |
3013 | 3018 |
3014 std::unique_ptr<disk_cache::BackendImpl> cache; | 3019 std::unique_ptr<disk_cache::BackendImpl> cache; |
3015 cache.reset(new disk_cache::BackendImpl( | 3020 cache.reset(new disk_cache::BackendImpl( |
3016 cache_path_, base::ThreadTaskRunnerHandle::Get(), NULL)); | 3021 cache_path_, base::ThreadTaskRunnerHandle::Get(), NULL)); |
3017 ASSERT_TRUE(NULL != cache.get()); | 3022 ASSERT_TRUE(NULL != cache.get()); |
3018 cache->SetUnitTestMode(); | 3023 cache->SetUnitTestMode(); |
3019 ASSERT_NE(net::OK, cache->SyncInit()); | 3024 ASSERT_NE(net::OK, cache->SyncInit()); |
3020 | 3025 |
3021 ASSERT_TRUE(NULL == cache->GetTimerForTest()); | 3026 ASSERT_TRUE(NULL == cache->GetTimerForTest()); |
3022 | 3027 |
3023 DisableIntegrityCheck(); | 3028 DisableIntegrityCheck(); |
3024 } | 3029 } |
3025 | 3030 |
3026 TEST_F(DiskCacheBackendTest, Backend_UsageStats) { | 3031 TEST_F(DiskCacheBackendTest, Backend_UsageStats) { |
3027 InitCache(); | 3032 InitCache(); |
3028 disk_cache::Entry* entry; | 3033 disk_cache::Entry* entry; |
3029 ASSERT_EQ(net::OK, CreateEntry("key", &entry)); | 3034 ASSERT_THAT(CreateEntry("key", &entry), IsOk()); |
3030 entry->Close(); | 3035 entry->Close(); |
3031 FlushQueueForTest(); | 3036 FlushQueueForTest(); |
3032 | 3037 |
3033 disk_cache::StatsItems stats; | 3038 disk_cache::StatsItems stats; |
3034 cache_->GetStats(&stats); | 3039 cache_->GetStats(&stats); |
3035 EXPECT_FALSE(stats.empty()); | 3040 EXPECT_FALSE(stats.empty()); |
3036 | 3041 |
3037 disk_cache::StatsItems::value_type hits("Create hit", "0x1"); | 3042 disk_cache::StatsItems::value_type hits("Create hit", "0x1"); |
3038 EXPECT_EQ(1, std::count(stats.begin(), stats.end(), hits)); | 3043 EXPECT_EQ(1, std::count(stats.begin(), stats.end(), hits)); |
3039 | 3044 |
3040 cache_.reset(); | 3045 cache_.reset(); |
3041 | 3046 |
3042 // Now open the cache and verify that the stats are still there. | 3047 // Now open the cache and verify that the stats are still there. |
3043 DisableFirstCleanup(); | 3048 DisableFirstCleanup(); |
3044 InitCache(); | 3049 InitCache(); |
3045 EXPECT_EQ(1, cache_->GetEntryCount()); | 3050 EXPECT_EQ(1, cache_->GetEntryCount()); |
3046 | 3051 |
3047 stats.clear(); | 3052 stats.clear(); |
3048 cache_->GetStats(&stats); | 3053 cache_->GetStats(&stats); |
3049 EXPECT_FALSE(stats.empty()); | 3054 EXPECT_FALSE(stats.empty()); |
3050 | 3055 |
3051 EXPECT_EQ(1, std::count(stats.begin(), stats.end(), hits)); | 3056 EXPECT_EQ(1, std::count(stats.begin(), stats.end(), hits)); |
3052 } | 3057 } |
3053 | 3058 |
3054 void DiskCacheBackendTest::BackendDoomAll() { | 3059 void DiskCacheBackendTest::BackendDoomAll() { |
3055 InitCache(); | 3060 InitCache(); |
3056 | 3061 |
3057 disk_cache::Entry *entry1, *entry2; | 3062 disk_cache::Entry *entry1, *entry2; |
3058 ASSERT_EQ(net::OK, CreateEntry("first", &entry1)); | 3063 ASSERT_THAT(CreateEntry("first", &entry1), IsOk()); |
3059 ASSERT_EQ(net::OK, CreateEntry("second", &entry2)); | 3064 ASSERT_THAT(CreateEntry("second", &entry2), IsOk()); |
3060 entry1->Close(); | 3065 entry1->Close(); |
3061 entry2->Close(); | 3066 entry2->Close(); |
3062 | 3067 |
3063 ASSERT_EQ(net::OK, CreateEntry("third", &entry1)); | 3068 ASSERT_THAT(CreateEntry("third", &entry1), IsOk()); |
3064 ASSERT_EQ(net::OK, CreateEntry("fourth", &entry2)); | 3069 ASSERT_THAT(CreateEntry("fourth", &entry2), IsOk()); |
3065 | 3070 |
3066 ASSERT_EQ(4, cache_->GetEntryCount()); | 3071 ASSERT_EQ(4, cache_->GetEntryCount()); |
3067 EXPECT_EQ(net::OK, DoomAllEntries()); | 3072 EXPECT_THAT(DoomAllEntries(), IsOk()); |
3068 ASSERT_EQ(0, cache_->GetEntryCount()); | 3073 ASSERT_EQ(0, cache_->GetEntryCount()); |
3069 | 3074 |
3070 // We should stop posting tasks at some point (if we post any). | 3075 // We should stop posting tasks at some point (if we post any). |
3071 base::RunLoop().RunUntilIdle(); | 3076 base::RunLoop().RunUntilIdle(); |
3072 | 3077 |
3073 disk_cache::Entry *entry3, *entry4; | 3078 disk_cache::Entry *entry3, *entry4; |
3074 EXPECT_NE(net::OK, OpenEntry("third", &entry3)); | 3079 EXPECT_NE(net::OK, OpenEntry("third", &entry3)); |
3075 ASSERT_EQ(net::OK, CreateEntry("third", &entry3)); | 3080 ASSERT_THAT(CreateEntry("third", &entry3), IsOk()); |
3076 ASSERT_EQ(net::OK, CreateEntry("fourth", &entry4)); | 3081 ASSERT_THAT(CreateEntry("fourth", &entry4), IsOk()); |
3077 | 3082 |
3078 EXPECT_EQ(net::OK, DoomAllEntries()); | 3083 EXPECT_THAT(DoomAllEntries(), IsOk()); |
3079 ASSERT_EQ(0, cache_->GetEntryCount()); | 3084 ASSERT_EQ(0, cache_->GetEntryCount()); |
3080 | 3085 |
3081 entry1->Close(); | 3086 entry1->Close(); |
3082 entry2->Close(); | 3087 entry2->Close(); |
3083 entry3->Doom(); // The entry should be already doomed, but this must work. | 3088 entry3->Doom(); // The entry should be already doomed, but this must work. |
3084 entry3->Close(); | 3089 entry3->Close(); |
3085 entry4->Close(); | 3090 entry4->Close(); |
3086 | 3091 |
3087 // Now try with all references released. | 3092 // Now try with all references released. |
3088 ASSERT_EQ(net::OK, CreateEntry("third", &entry1)); | 3093 ASSERT_THAT(CreateEntry("third", &entry1), IsOk()); |
3089 ASSERT_EQ(net::OK, CreateEntry("fourth", &entry2)); | 3094 ASSERT_THAT(CreateEntry("fourth", &entry2), IsOk()); |
3090 entry1->Close(); | 3095 entry1->Close(); |
3091 entry2->Close(); | 3096 entry2->Close(); |
3092 | 3097 |
3093 ASSERT_EQ(2, cache_->GetEntryCount()); | 3098 ASSERT_EQ(2, cache_->GetEntryCount()); |
3094 EXPECT_EQ(net::OK, DoomAllEntries()); | 3099 EXPECT_THAT(DoomAllEntries(), IsOk()); |
3095 ASSERT_EQ(0, cache_->GetEntryCount()); | 3100 ASSERT_EQ(0, cache_->GetEntryCount()); |
3096 | 3101 |
3097 EXPECT_EQ(net::OK, DoomAllEntries()); | 3102 EXPECT_THAT(DoomAllEntries(), IsOk()); |
3098 } | 3103 } |
3099 | 3104 |
3100 TEST_F(DiskCacheBackendTest, DoomAll) { | 3105 TEST_F(DiskCacheBackendTest, DoomAll) { |
3101 BackendDoomAll(); | 3106 BackendDoomAll(); |
3102 } | 3107 } |
3103 | 3108 |
3104 TEST_F(DiskCacheBackendTest, NewEvictionDoomAll) { | 3109 TEST_F(DiskCacheBackendTest, NewEvictionDoomAll) { |
3105 SetNewEviction(); | 3110 SetNewEviction(); |
3106 BackendDoomAll(); | 3111 BackendDoomAll(); |
3107 } | 3112 } |
3108 | 3113 |
3109 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomAll) { | 3114 TEST_F(DiskCacheBackendTest, MemoryOnlyDoomAll) { |
3110 SetMemoryOnlyMode(); | 3115 SetMemoryOnlyMode(); |
3111 BackendDoomAll(); | 3116 BackendDoomAll(); |
3112 } | 3117 } |
3113 | 3118 |
3114 TEST_F(DiskCacheBackendTest, AppCacheOnlyDoomAll) { | 3119 TEST_F(DiskCacheBackendTest, AppCacheOnlyDoomAll) { |
3115 SetCacheType(net::APP_CACHE); | 3120 SetCacheType(net::APP_CACHE); |
3116 BackendDoomAll(); | 3121 BackendDoomAll(); |
3117 } | 3122 } |
3118 | 3123 |
3119 TEST_F(DiskCacheBackendTest, ShaderCacheOnlyDoomAll) { | 3124 TEST_F(DiskCacheBackendTest, ShaderCacheOnlyDoomAll) { |
3120 SetCacheType(net::SHADER_CACHE); | 3125 SetCacheType(net::SHADER_CACHE); |
3121 BackendDoomAll(); | 3126 BackendDoomAll(); |
3122 } | 3127 } |
3123 | 3128 |
3124 // If the index size changes when we doom the cache, we should not crash. | 3129 // If the index size changes when we doom the cache, we should not crash. |
3125 void DiskCacheBackendTest::BackendDoomAll2() { | 3130 void DiskCacheBackendTest::BackendDoomAll2() { |
3126 EXPECT_EQ(2, cache_->GetEntryCount()); | 3131 EXPECT_EQ(2, cache_->GetEntryCount()); |
3127 EXPECT_EQ(net::OK, DoomAllEntries()); | 3132 EXPECT_THAT(DoomAllEntries(), IsOk()); |
3128 | 3133 |
3129 disk_cache::Entry* entry; | 3134 disk_cache::Entry* entry; |
3130 ASSERT_EQ(net::OK, CreateEntry("Something new", &entry)); | 3135 ASSERT_THAT(CreateEntry("Something new", &entry), IsOk()); |
3131 entry->Close(); | 3136 entry->Close(); |
3132 | 3137 |
3133 EXPECT_EQ(1, cache_->GetEntryCount()); | 3138 EXPECT_EQ(1, cache_->GetEntryCount()); |
3134 } | 3139 } |
3135 | 3140 |
3136 TEST_F(DiskCacheBackendTest, DoomAll2) { | 3141 TEST_F(DiskCacheBackendTest, DoomAll2) { |
3137 ASSERT_TRUE(CopyTestCache("bad_rankings2")); | 3142 ASSERT_TRUE(CopyTestCache("bad_rankings2")); |
3138 DisableFirstCleanup(); | 3143 DisableFirstCleanup(); |
3139 SetMaxSize(20 * 1024 * 1024); | 3144 SetMaxSize(20 * 1024 * 1024); |
3140 InitCache(); | 3145 InitCache(); |
(...skipping 26 matching lines...) Expand all Loading... |
3167 | 3172 |
3168 int rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, | 3173 int rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, |
3169 net::CACHE_BACKEND_DEFAULT, | 3174 net::CACHE_BACKEND_DEFAULT, |
3170 store1.path(), | 3175 store1.path(), |
3171 0, | 3176 0, |
3172 false, | 3177 false, |
3173 cache_thread.task_runner(), | 3178 cache_thread.task_runner(), |
3174 NULL, | 3179 NULL, |
3175 &cache[0], | 3180 &cache[0], |
3176 cb.callback()); | 3181 cb.callback()); |
3177 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 3182 ASSERT_THAT(cb.GetResult(rv), IsOk()); |
3178 rv = disk_cache::CreateCacheBackend(net::MEDIA_CACHE, | 3183 rv = disk_cache::CreateCacheBackend(net::MEDIA_CACHE, |
3179 net::CACHE_BACKEND_DEFAULT, | 3184 net::CACHE_BACKEND_DEFAULT, |
3180 store2.path(), | 3185 store2.path(), |
3181 0, | 3186 0, |
3182 false, | 3187 false, |
3183 cache_thread.task_runner(), | 3188 cache_thread.task_runner(), |
3184 NULL, | 3189 NULL, |
3185 &cache[1], | 3190 &cache[1], |
3186 cb.callback()); | 3191 cb.callback()); |
3187 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 3192 ASSERT_THAT(cb.GetResult(rv), IsOk()); |
3188 | 3193 |
3189 ASSERT_TRUE(cache[0].get() != NULL && cache[1].get() != NULL); | 3194 ASSERT_TRUE(cache[0].get() != NULL && cache[1].get() != NULL); |
3190 | 3195 |
3191 std::string key("the first key"); | 3196 std::string key("the first key"); |
3192 disk_cache::Entry* entry; | 3197 disk_cache::Entry* entry; |
3193 for (int i = 0; i < kNumberOfCaches; i++) { | 3198 for (int i = 0; i < kNumberOfCaches; i++) { |
3194 rv = cache[i]->CreateEntry(key, &entry, cb.callback()); | 3199 rv = cache[i]->CreateEntry(key, &entry, cb.callback()); |
3195 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 3200 ASSERT_THAT(cb.GetResult(rv), IsOk()); |
3196 entry->Close(); | 3201 entry->Close(); |
3197 } | 3202 } |
3198 } | 3203 } |
3199 | 3204 |
3200 // Test the six regions of the curve that determines the max cache size. | 3205 // Test the six regions of the curve that determines the max cache size. |
3201 TEST_F(DiskCacheTest, AutomaticMaxSize) { | 3206 TEST_F(DiskCacheTest, AutomaticMaxSize) { |
3202 using disk_cache::kDefaultCacheSize; | 3207 using disk_cache::kDefaultCacheSize; |
3203 int64_t large_size = kDefaultCacheSize; | 3208 int64_t large_size = kDefaultCacheSize; |
3204 | 3209 |
3205 // Region 1: expected = available * 0.8 | 3210 // Region 1: expected = available * 0.8 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3256 CACHE_UMA(HOURS, "FillupTime", i, 28); | 3261 CACHE_UMA(HOURS, "FillupTime", i, 28); |
3257 } | 3262 } |
3258 } | 3263 } |
3259 | 3264 |
3260 // Make sure that we keep the total memory used by the internal buffers under | 3265 // Make sure that we keep the total memory used by the internal buffers under |
3261 // control. | 3266 // control. |
3262 TEST_F(DiskCacheBackendTest, TotalBuffersSize1) { | 3267 TEST_F(DiskCacheBackendTest, TotalBuffersSize1) { |
3263 InitCache(); | 3268 InitCache(); |
3264 std::string key("the first key"); | 3269 std::string key("the first key"); |
3265 disk_cache::Entry* entry; | 3270 disk_cache::Entry* entry; |
3266 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 3271 ASSERT_THAT(CreateEntry(key, &entry), IsOk()); |
3267 | 3272 |
3268 const int kSize = 200; | 3273 const int kSize = 200; |
3269 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); | 3274 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); |
3270 CacheTestFillBuffer(buffer->data(), kSize, true); | 3275 CacheTestFillBuffer(buffer->data(), kSize, true); |
3271 | 3276 |
3272 for (int i = 0; i < 10; i++) { | 3277 for (int i = 0; i < 10; i++) { |
3273 SCOPED_TRACE(i); | 3278 SCOPED_TRACE(i); |
3274 // Allocate 2MB for this entry. | 3279 // Allocate 2MB for this entry. |
3275 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, true)); | 3280 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, true)); |
3276 EXPECT_EQ(kSize, WriteData(entry, 1, 0, buffer.get(), kSize, true)); | 3281 EXPECT_EQ(kSize, WriteData(entry, 1, 0, buffer.get(), kSize, true)); |
3277 EXPECT_EQ(kSize, | 3282 EXPECT_EQ(kSize, |
3278 WriteData(entry, 0, 1024 * 1024, buffer.get(), kSize, false)); | 3283 WriteData(entry, 0, 1024 * 1024, buffer.get(), kSize, false)); |
3279 EXPECT_EQ(kSize, | 3284 EXPECT_EQ(kSize, |
3280 WriteData(entry, 1, 1024 * 1024, buffer.get(), kSize, false)); | 3285 WriteData(entry, 1, 1024 * 1024, buffer.get(), kSize, false)); |
3281 | 3286 |
3282 // Delete one of the buffers and truncate the other. | 3287 // Delete one of the buffers and truncate the other. |
3283 EXPECT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, true)); | 3288 EXPECT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, true)); |
3284 EXPECT_EQ(0, WriteData(entry, 1, 10, buffer.get(), 0, true)); | 3289 EXPECT_EQ(0, WriteData(entry, 1, 10, buffer.get(), 0, true)); |
3285 | 3290 |
3286 // Delete the second buffer, writing 10 bytes to disk. | 3291 // Delete the second buffer, writing 10 bytes to disk. |
3287 entry->Close(); | 3292 entry->Close(); |
3288 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); | 3293 ASSERT_THAT(OpenEntry(key, &entry), IsOk()); |
3289 } | 3294 } |
3290 | 3295 |
3291 entry->Close(); | 3296 entry->Close(); |
3292 EXPECT_EQ(0, cache_impl_->GetTotalBuffersSize()); | 3297 EXPECT_EQ(0, cache_impl_->GetTotalBuffersSize()); |
3293 } | 3298 } |
3294 | 3299 |
3295 // This test assumes at least 150MB of system memory. | 3300 // This test assumes at least 150MB of system memory. |
3296 TEST_F(DiskCacheBackendTest, TotalBuffersSize2) { | 3301 TEST_F(DiskCacheBackendTest, TotalBuffersSize2) { |
3297 InitCache(); | 3302 InitCache(); |
3298 | 3303 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3358 EXPECT_TRUE(disk_cache::DeleteCacheFile(name)); | 3363 EXPECT_TRUE(disk_cache::DeleteCacheFile(name)); |
3359 } | 3364 } |
3360 | 3365 |
3361 TEST_F(DiskCacheBackendTest, UpdateRankForExternalCacheHit) { | 3366 TEST_F(DiskCacheBackendTest, UpdateRankForExternalCacheHit) { |
3362 InitCache(); | 3367 InitCache(); |
3363 | 3368 |
3364 disk_cache::Entry* entry; | 3369 disk_cache::Entry* entry; |
3365 | 3370 |
3366 for (int i = 0; i < 2; ++i) { | 3371 for (int i = 0; i < 2; ++i) { |
3367 std::string key = base::StringPrintf("key%d", i); | 3372 std::string key = base::StringPrintf("key%d", i); |
3368 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 3373 ASSERT_THAT(CreateEntry(key, &entry), IsOk()); |
3369 entry->Close(); | 3374 entry->Close(); |
3370 } | 3375 } |
3371 | 3376 |
3372 // Ping the oldest entry. | 3377 // Ping the oldest entry. |
3373 cache_->OnExternalCacheHit("key0"); | 3378 cache_->OnExternalCacheHit("key0"); |
3374 | 3379 |
3375 TrimForTest(false); | 3380 TrimForTest(false); |
3376 | 3381 |
3377 // Make sure the older key remains. | 3382 // Make sure the older key remains. |
3378 EXPECT_EQ(1, cache_->GetEntryCount()); | 3383 EXPECT_EQ(1, cache_->GetEntryCount()); |
3379 ASSERT_EQ(net::OK, OpenEntry("key0", &entry)); | 3384 ASSERT_THAT(OpenEntry("key0", &entry), IsOk()); |
3380 entry->Close(); | 3385 entry->Close(); |
3381 } | 3386 } |
3382 | 3387 |
3383 TEST_F(DiskCacheBackendTest, ShaderCacheUpdateRankForExternalCacheHit) { | 3388 TEST_F(DiskCacheBackendTest, ShaderCacheUpdateRankForExternalCacheHit) { |
3384 SetCacheType(net::SHADER_CACHE); | 3389 SetCacheType(net::SHADER_CACHE); |
3385 InitCache(); | 3390 InitCache(); |
3386 | 3391 |
3387 disk_cache::Entry* entry; | 3392 disk_cache::Entry* entry; |
3388 | 3393 |
3389 for (int i = 0; i < 2; ++i) { | 3394 for (int i = 0; i < 2; ++i) { |
3390 std::string key = base::StringPrintf("key%d", i); | 3395 std::string key = base::StringPrintf("key%d", i); |
3391 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 3396 ASSERT_THAT(CreateEntry(key, &entry), IsOk()); |
3392 entry->Close(); | 3397 entry->Close(); |
3393 } | 3398 } |
3394 | 3399 |
3395 // Ping the oldest entry. | 3400 // Ping the oldest entry. |
3396 cache_->OnExternalCacheHit("key0"); | 3401 cache_->OnExternalCacheHit("key0"); |
3397 | 3402 |
3398 TrimForTest(false); | 3403 TrimForTest(false); |
3399 | 3404 |
3400 // Make sure the older key remains. | 3405 // Make sure the older key remains. |
3401 EXPECT_EQ(1, cache_->GetEntryCount()); | 3406 EXPECT_EQ(1, cache_->GetEntryCount()); |
3402 ASSERT_EQ(net::OK, OpenEntry("key0", &entry)); | 3407 ASSERT_THAT(OpenEntry("key0", &entry), IsOk()); |
3403 entry->Close(); | 3408 entry->Close(); |
3404 } | 3409 } |
3405 | 3410 |
3406 TEST_F(DiskCacheBackendTest, SimpleCacheShutdownWithPendingCreate) { | 3411 TEST_F(DiskCacheBackendTest, SimpleCacheShutdownWithPendingCreate) { |
3407 SetCacheType(net::APP_CACHE); | 3412 SetCacheType(net::APP_CACHE); |
3408 SetSimpleCacheMode(); | 3413 SetSimpleCacheMode(); |
3409 BackendShutdownWithPendingCreate(false); | 3414 BackendShutdownWithPendingCreate(false); |
3410 } | 3415 } |
3411 | 3416 |
3412 TEST_F(DiskCacheBackendTest, SimpleCacheShutdownWithPendingDoom) { | 3417 TEST_F(DiskCacheBackendTest, SimpleCacheShutdownWithPendingDoom) { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3486 BackendDoomAll(); | 3491 BackendDoomAll(); |
3487 } | 3492 } |
3488 | 3493 |
3489 TEST_F(DiskCacheBackendTest, SimpleCacheOpenMissingFile) { | 3494 TEST_F(DiskCacheBackendTest, SimpleCacheOpenMissingFile) { |
3490 SetSimpleCacheMode(); | 3495 SetSimpleCacheMode(); |
3491 InitCache(); | 3496 InitCache(); |
3492 | 3497 |
3493 const char key[] = "the first key"; | 3498 const char key[] = "the first key"; |
3494 disk_cache::Entry* entry = NULL; | 3499 disk_cache::Entry* entry = NULL; |
3495 | 3500 |
3496 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 3501 ASSERT_THAT(CreateEntry(key, &entry), IsOk()); |
3497 ASSERT_TRUE(entry != NULL); | 3502 ASSERT_TRUE(entry != NULL); |
3498 entry->Close(); | 3503 entry->Close(); |
3499 entry = NULL; | 3504 entry = NULL; |
3500 | 3505 |
3501 // To make sure the file creation completed we need to call open again so that | 3506 // To make sure the file creation completed we need to call open again so that |
3502 // we block until it actually created the files. | 3507 // we block until it actually created the files. |
3503 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); | 3508 ASSERT_THAT(OpenEntry(key, &entry), IsOk()); |
3504 ASSERT_TRUE(entry != NULL); | 3509 ASSERT_TRUE(entry != NULL); |
3505 entry->Close(); | 3510 entry->Close(); |
3506 entry = NULL; | 3511 entry = NULL; |
3507 | 3512 |
3508 // Delete one of the files in the entry. | 3513 // Delete one of the files in the entry. |
3509 base::FilePath to_delete_file = cache_path_.AppendASCII( | 3514 base::FilePath to_delete_file = cache_path_.AppendASCII( |
3510 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); | 3515 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); |
3511 EXPECT_TRUE(base::PathExists(to_delete_file)); | 3516 EXPECT_TRUE(base::PathExists(to_delete_file)); |
3512 EXPECT_TRUE(disk_cache::DeleteCacheFile(to_delete_file)); | 3517 EXPECT_TRUE(disk_cache::DeleteCacheFile(to_delete_file)); |
3513 | 3518 |
3514 // Failing to open the entry should delete the rest of these files. | 3519 // Failing to open the entry should delete the rest of these files. |
3515 ASSERT_EQ(net::ERR_FAILED, OpenEntry(key, &entry)); | 3520 ASSERT_THAT(OpenEntry(key, &entry), IsError(net::ERR_FAILED)); |
3516 | 3521 |
3517 // Confirm the rest of the files are gone. | 3522 // Confirm the rest of the files are gone. |
3518 for (int i = 1; i < disk_cache::kSimpleEntryFileCount; ++i) { | 3523 for (int i = 1; i < disk_cache::kSimpleEntryFileCount; ++i) { |
3519 base::FilePath should_be_gone_file(cache_path_.AppendASCII( | 3524 base::FilePath should_be_gone_file(cache_path_.AppendASCII( |
3520 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, i))); | 3525 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, i))); |
3521 EXPECT_FALSE(base::PathExists(should_be_gone_file)); | 3526 EXPECT_FALSE(base::PathExists(should_be_gone_file)); |
3522 } | 3527 } |
3523 } | 3528 } |
3524 | 3529 |
3525 TEST_F(DiskCacheBackendTest, SimpleCacheOpenBadFile) { | 3530 TEST_F(DiskCacheBackendTest, SimpleCacheOpenBadFile) { |
3526 SetSimpleCacheMode(); | 3531 SetSimpleCacheMode(); |
3527 InitCache(); | 3532 InitCache(); |
3528 | 3533 |
3529 const char key[] = "the first key"; | 3534 const char key[] = "the first key"; |
3530 disk_cache::Entry* entry = NULL; | 3535 disk_cache::Entry* entry = NULL; |
3531 | 3536 |
3532 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 3537 ASSERT_THAT(CreateEntry(key, &entry), IsOk()); |
3533 disk_cache::Entry* null = NULL; | 3538 disk_cache::Entry* null = NULL; |
3534 ASSERT_NE(null, entry); | 3539 ASSERT_NE(null, entry); |
3535 entry->Close(); | 3540 entry->Close(); |
3536 entry = NULL; | 3541 entry = NULL; |
3537 | 3542 |
3538 // To make sure the file creation completed we need to call open again so that | 3543 // To make sure the file creation completed we need to call open again so that |
3539 // we block until it actually created the files. | 3544 // we block until it actually created the files. |
3540 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); | 3545 ASSERT_THAT(OpenEntry(key, &entry), IsOk()); |
3541 ASSERT_NE(null, entry); | 3546 ASSERT_NE(null, entry); |
3542 entry->Close(); | 3547 entry->Close(); |
3543 entry = NULL; | 3548 entry = NULL; |
3544 | 3549 |
3545 // The entry is being closed on the Simple Cache worker pool | 3550 // The entry is being closed on the Simple Cache worker pool |
3546 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting(); | 3551 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting(); |
3547 base::RunLoop().RunUntilIdle(); | 3552 base::RunLoop().RunUntilIdle(); |
3548 | 3553 |
3549 // Write an invalid header for stream 0 and stream 1. | 3554 // Write an invalid header for stream 0 and stream 1. |
3550 base::FilePath entry_file1_path = cache_path_.AppendASCII( | 3555 base::FilePath entry_file1_path = cache_path_.AppendASCII( |
3551 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); | 3556 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); |
3552 | 3557 |
3553 disk_cache::SimpleFileHeader header; | 3558 disk_cache::SimpleFileHeader header; |
3554 header.initial_magic_number = UINT64_C(0xbadf00d); | 3559 header.initial_magic_number = UINT64_C(0xbadf00d); |
3555 EXPECT_EQ(static_cast<int>(sizeof(header)), | 3560 EXPECT_EQ(static_cast<int>(sizeof(header)), |
3556 base::WriteFile(entry_file1_path, reinterpret_cast<char*>(&header), | 3561 base::WriteFile(entry_file1_path, reinterpret_cast<char*>(&header), |
3557 sizeof(header))); | 3562 sizeof(header))); |
3558 ASSERT_EQ(net::ERR_FAILED, OpenEntry(key, &entry)); | 3563 ASSERT_THAT(OpenEntry(key, &entry), IsError(net::ERR_FAILED)); |
3559 } | 3564 } |
3560 | 3565 |
3561 // Tests that the Simple Cache Backend fails to initialize with non-matching | 3566 // Tests that the Simple Cache Backend fails to initialize with non-matching |
3562 // file structure on disk. | 3567 // file structure on disk. |
3563 TEST_F(DiskCacheBackendTest, SimpleCacheOverBlockfileCache) { | 3568 TEST_F(DiskCacheBackendTest, SimpleCacheOverBlockfileCache) { |
3564 // Create a cache structure with the |BackendImpl|. | 3569 // Create a cache structure with the |BackendImpl|. |
3565 InitCache(); | 3570 InitCache(); |
3566 disk_cache::Entry* entry; | 3571 disk_cache::Entry* entry; |
3567 const int kSize = 50; | 3572 const int kSize = 50; |
3568 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); | 3573 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); |
3569 CacheTestFillBuffer(buffer->data(), kSize, false); | 3574 CacheTestFillBuffer(buffer->data(), kSize, false); |
3570 ASSERT_EQ(net::OK, CreateEntry("key", &entry)); | 3575 ASSERT_THAT(CreateEntry("key", &entry), IsOk()); |
3571 ASSERT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, false)); | 3576 ASSERT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, false)); |
3572 entry->Close(); | 3577 entry->Close(); |
3573 cache_.reset(); | 3578 cache_.reset(); |
3574 | 3579 |
3575 // Check that the |SimpleBackendImpl| does not favor this structure. | 3580 // Check that the |SimpleBackendImpl| does not favor this structure. |
3576 base::Thread cache_thread("CacheThread"); | 3581 base::Thread cache_thread("CacheThread"); |
3577 ASSERT_TRUE(cache_thread.StartWithOptions( | 3582 ASSERT_TRUE(cache_thread.StartWithOptions( |
3578 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 3583 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
3579 disk_cache::SimpleBackendImpl* simple_cache = | 3584 disk_cache::SimpleBackendImpl* simple_cache = |
3580 new disk_cache::SimpleBackendImpl( | 3585 new disk_cache::SimpleBackendImpl( |
3581 cache_path_, 0, net::DISK_CACHE, cache_thread.task_runner(), NULL); | 3586 cache_path_, 0, net::DISK_CACHE, cache_thread.task_runner(), NULL); |
3582 net::TestCompletionCallback cb; | 3587 net::TestCompletionCallback cb; |
3583 int rv = simple_cache->Init(cb.callback()); | 3588 int rv = simple_cache->Init(cb.callback()); |
3584 EXPECT_NE(net::OK, cb.GetResult(rv)); | 3589 EXPECT_NE(net::OK, cb.GetResult(rv)); |
3585 delete simple_cache; | 3590 delete simple_cache; |
3586 DisableIntegrityCheck(); | 3591 DisableIntegrityCheck(); |
3587 } | 3592 } |
3588 | 3593 |
3589 // Tests that the |BackendImpl| refuses to initialize on top of the files | 3594 // Tests that the |BackendImpl| refuses to initialize on top of the files |
3590 // generated by the Simple Cache Backend. | 3595 // generated by the Simple Cache Backend. |
3591 TEST_F(DiskCacheBackendTest, BlockfileCacheOverSimpleCache) { | 3596 TEST_F(DiskCacheBackendTest, BlockfileCacheOverSimpleCache) { |
3592 // Create a cache structure with the |SimpleBackendImpl|. | 3597 // Create a cache structure with the |SimpleBackendImpl|. |
3593 SetSimpleCacheMode(); | 3598 SetSimpleCacheMode(); |
3594 InitCache(); | 3599 InitCache(); |
3595 disk_cache::Entry* entry; | 3600 disk_cache::Entry* entry; |
3596 const int kSize = 50; | 3601 const int kSize = 50; |
3597 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); | 3602 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); |
3598 CacheTestFillBuffer(buffer->data(), kSize, false); | 3603 CacheTestFillBuffer(buffer->data(), kSize, false); |
3599 ASSERT_EQ(net::OK, CreateEntry("key", &entry)); | 3604 ASSERT_THAT(CreateEntry("key", &entry), IsOk()); |
3600 ASSERT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, false)); | 3605 ASSERT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, false)); |
3601 entry->Close(); | 3606 entry->Close(); |
3602 cache_.reset(); | 3607 cache_.reset(); |
3603 | 3608 |
3604 // Check that the |BackendImpl| does not favor this structure. | 3609 // Check that the |BackendImpl| does not favor this structure. |
3605 base::Thread cache_thread("CacheThread"); | 3610 base::Thread cache_thread("CacheThread"); |
3606 ASSERT_TRUE(cache_thread.StartWithOptions( | 3611 ASSERT_TRUE(cache_thread.StartWithOptions( |
3607 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 3612 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
3608 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl( | 3613 disk_cache::BackendImpl* cache = new disk_cache::BackendImpl( |
3609 cache_path_, base::ThreadTaskRunnerHandle::Get(), NULL); | 3614 cache_path_, base::ThreadTaskRunnerHandle::Get(), NULL); |
(...skipping 25 matching lines...) Expand all Loading... |
3635 ASSERT_TRUE(EnumerateAndMatchKeys(-1, iter.get(), &keys_to_match, &count)); | 3640 ASSERT_TRUE(EnumerateAndMatchKeys(-1, iter.get(), &keys_to_match, &count)); |
3636 iter.reset(); | 3641 iter.reset(); |
3637 EXPECT_EQ(key_pool.size(), count); | 3642 EXPECT_EQ(key_pool.size(), count); |
3638 EXPECT_TRUE(keys_to_match.empty()); | 3643 EXPECT_TRUE(keys_to_match.empty()); |
3639 | 3644 |
3640 // Check that opening entries does not affect enumeration. | 3645 // Check that opening entries does not affect enumeration. |
3641 keys_to_match = key_pool; | 3646 keys_to_match = key_pool; |
3642 iter = CreateIterator(); | 3647 iter = CreateIterator(); |
3643 count = 0; | 3648 count = 0; |
3644 disk_cache::Entry* entry_opened_before; | 3649 disk_cache::Entry* entry_opened_before; |
3645 ASSERT_EQ(net::OK, OpenEntry(*(key_pool.begin()), &entry_opened_before)); | 3650 ASSERT_THAT(OpenEntry(*(key_pool.begin()), &entry_opened_before), IsOk()); |
3646 ASSERT_TRUE(EnumerateAndMatchKeys(key_pool.size()/2, | 3651 ASSERT_TRUE(EnumerateAndMatchKeys(key_pool.size()/2, |
3647 iter.get(), | 3652 iter.get(), |
3648 &keys_to_match, | 3653 &keys_to_match, |
3649 &count)); | 3654 &count)); |
3650 | 3655 |
3651 disk_cache::Entry* entry_opened_middle; | 3656 disk_cache::Entry* entry_opened_middle; |
3652 ASSERT_EQ(net::OK, | 3657 ASSERT_EQ(net::OK, |
3653 OpenEntry(*(keys_to_match.begin()), &entry_opened_middle)); | 3658 OpenEntry(*(keys_to_match.begin()), &entry_opened_middle)); |
3654 ASSERT_TRUE(EnumerateAndMatchKeys(-1, iter.get(), &keys_to_match, &count)); | 3659 ASSERT_TRUE(EnumerateAndMatchKeys(-1, iter.get(), &keys_to_match, &count)); |
3655 iter.reset(); | 3660 iter.reset(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3691 // Tests that enumerations are not affected by corrupt files. | 3696 // Tests that enumerations are not affected by corrupt files. |
3692 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationCorruption) { | 3697 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationCorruption) { |
3693 SetSimpleCacheMode(); | 3698 SetSimpleCacheMode(); |
3694 InitCache(); | 3699 InitCache(); |
3695 // Create a corrupt entry. The write/read sequence ensures that the entry will | 3700 // Create a corrupt entry. The write/read sequence ensures that the entry will |
3696 // have been created before corrupting the platform files, in the case of | 3701 // have been created before corrupting the platform files, in the case of |
3697 // optimistic operations. | 3702 // optimistic operations. |
3698 const std::string key = "the key"; | 3703 const std::string key = "the key"; |
3699 disk_cache::Entry* corrupted_entry; | 3704 disk_cache::Entry* corrupted_entry; |
3700 | 3705 |
3701 ASSERT_EQ(net::OK, CreateEntry(key, &corrupted_entry)); | 3706 ASSERT_THAT(CreateEntry(key, &corrupted_entry), IsOk()); |
3702 ASSERT_TRUE(corrupted_entry); | 3707 ASSERT_TRUE(corrupted_entry); |
3703 const int kSize = 50; | 3708 const int kSize = 50; |
3704 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); | 3709 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); |
3705 CacheTestFillBuffer(buffer->data(), kSize, false); | 3710 CacheTestFillBuffer(buffer->data(), kSize, false); |
3706 ASSERT_EQ(kSize, | 3711 ASSERT_EQ(kSize, |
3707 WriteData(corrupted_entry, 0, 0, buffer.get(), kSize, false)); | 3712 WriteData(corrupted_entry, 0, 0, buffer.get(), kSize, false)); |
3708 ASSERT_EQ(kSize, ReadData(corrupted_entry, 0, 0, buffer.get(), kSize)); | 3713 ASSERT_EQ(kSize, ReadData(corrupted_entry, 0, 0, buffer.get(), kSize)); |
3709 corrupted_entry->Close(); | 3714 corrupted_entry->Close(); |
3710 | 3715 |
3711 std::set<std::string> key_pool; | 3716 std::set<std::string> key_pool; |
(...skipping 17 matching lines...) Expand all Loading... |
3729 // Tests that enumerations don't leak memory when the backend is destructed | 3734 // Tests that enumerations don't leak memory when the backend is destructed |
3730 // mid-enumeration. | 3735 // mid-enumeration. |
3731 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationDestruction) { | 3736 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationDestruction) { |
3732 SetSimpleCacheMode(); | 3737 SetSimpleCacheMode(); |
3733 InitCache(); | 3738 InitCache(); |
3734 std::set<std::string> key_pool; | 3739 std::set<std::string> key_pool; |
3735 ASSERT_TRUE(CreateSetOfRandomEntries(&key_pool)); | 3740 ASSERT_TRUE(CreateSetOfRandomEntries(&key_pool)); |
3736 | 3741 |
3737 std::unique_ptr<TestIterator> iter = CreateIterator(); | 3742 std::unique_ptr<TestIterator> iter = CreateIterator(); |
3738 disk_cache::Entry* entry = NULL; | 3743 disk_cache::Entry* entry = NULL; |
3739 ASSERT_EQ(net::OK, iter->OpenNextEntry(&entry)); | 3744 ASSERT_THAT(iter->OpenNextEntry(&entry), IsOk()); |
3740 EXPECT_TRUE(entry); | 3745 EXPECT_TRUE(entry); |
3741 disk_cache::ScopedEntryPtr entry_closer(entry); | 3746 disk_cache::ScopedEntryPtr entry_closer(entry); |
3742 | 3747 |
3743 cache_.reset(); | 3748 cache_.reset(); |
3744 // This test passes if we don't leak memory. | 3749 // This test passes if we don't leak memory. |
3745 } | 3750 } |
3746 | 3751 |
3747 // Tests that enumerations include entries with long keys. | 3752 // Tests that enumerations include entries with long keys. |
3748 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationLongKeys) { | 3753 TEST_F(DiskCacheBackendTest, SimpleCacheEnumerationLongKeys) { |
3749 SetSimpleCacheMode(); | 3754 SetSimpleCacheMode(); |
3750 InitCache(); | 3755 InitCache(); |
3751 std::set<std::string> key_pool; | 3756 std::set<std::string> key_pool; |
3752 ASSERT_TRUE(CreateSetOfRandomEntries(&key_pool)); | 3757 ASSERT_TRUE(CreateSetOfRandomEntries(&key_pool)); |
3753 | 3758 |
3754 const size_t long_key_length = | 3759 const size_t long_key_length = |
3755 disk_cache::SimpleSynchronousEntry::kInitialHeaderRead + 10; | 3760 disk_cache::SimpleSynchronousEntry::kInitialHeaderRead + 10; |
3756 std::string long_key(long_key_length, 'X'); | 3761 std::string long_key(long_key_length, 'X'); |
3757 key_pool.insert(long_key); | 3762 key_pool.insert(long_key); |
3758 disk_cache::Entry* entry = NULL; | 3763 disk_cache::Entry* entry = NULL; |
3759 ASSERT_EQ(net::OK, CreateEntry(long_key.c_str(), &entry)); | 3764 ASSERT_THAT(CreateEntry(long_key.c_str(), &entry), IsOk()); |
3760 entry->Close(); | 3765 entry->Close(); |
3761 | 3766 |
3762 std::unique_ptr<TestIterator> iter = CreateIterator(); | 3767 std::unique_ptr<TestIterator> iter = CreateIterator(); |
3763 size_t count = 0; | 3768 size_t count = 0; |
3764 EXPECT_TRUE(EnumerateAndMatchKeys(-1, iter.get(), &key_pool, &count)); | 3769 EXPECT_TRUE(EnumerateAndMatchKeys(-1, iter.get(), &key_pool, &count)); |
3765 EXPECT_TRUE(key_pool.empty()); | 3770 EXPECT_TRUE(key_pool.empty()); |
3766 } | 3771 } |
3767 | 3772 |
3768 // Tests that a SimpleCache doesn't crash when files are deleted very quickly | 3773 // Tests that a SimpleCache doesn't crash when files are deleted very quickly |
3769 // after closing. | 3774 // after closing. |
3770 // NOTE: IF THIS TEST IS FLAKY THEN IT IS FAILING. See https://crbug.com/416940 | 3775 // NOTE: IF THIS TEST IS FLAKY THEN IT IS FAILING. See https://crbug.com/416940 |
3771 TEST_F(DiskCacheBackendTest, SimpleCacheDeleteQuickly) { | 3776 TEST_F(DiskCacheBackendTest, SimpleCacheDeleteQuickly) { |
3772 SetSimpleCacheMode(); | 3777 SetSimpleCacheMode(); |
3773 for (int i = 0; i < 100; ++i) { | 3778 for (int i = 0; i < 100; ++i) { |
3774 InitCache(); | 3779 InitCache(); |
3775 cache_.reset(); | 3780 cache_.reset(); |
3776 EXPECT_TRUE(CleanupCacheDir()); | 3781 EXPECT_TRUE(CleanupCacheDir()); |
3777 } | 3782 } |
3778 } | 3783 } |
3779 | 3784 |
3780 TEST_F(DiskCacheBackendTest, SimpleCacheLateDoom) { | 3785 TEST_F(DiskCacheBackendTest, SimpleCacheLateDoom) { |
3781 SetSimpleCacheMode(); | 3786 SetSimpleCacheMode(); |
3782 InitCache(); | 3787 InitCache(); |
3783 | 3788 |
3784 disk_cache::Entry *entry1, *entry2; | 3789 disk_cache::Entry *entry1, *entry2; |
3785 ASSERT_EQ(net::OK, CreateEntry("first", &entry1)); | 3790 ASSERT_THAT(CreateEntry("first", &entry1), IsOk()); |
3786 ASSERT_EQ(net::OK, CreateEntry("second", &entry2)); | 3791 ASSERT_THAT(CreateEntry("second", &entry2), IsOk()); |
3787 entry1->Close(); | 3792 entry1->Close(); |
3788 | 3793 |
3789 // Ensure that the directory mtime is flushed to disk before serializing the | 3794 // Ensure that the directory mtime is flushed to disk before serializing the |
3790 // index. | 3795 // index. |
3791 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting(); | 3796 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting(); |
3792 base::RunLoop().RunUntilIdle(); | 3797 base::RunLoop().RunUntilIdle(); |
3793 #if defined(OS_POSIX) | 3798 #if defined(OS_POSIX) |
3794 base::File cache_dir(cache_path_, | 3799 base::File cache_dir(cache_path_, |
3795 base::File::FLAG_OPEN | base::File::FLAG_READ); | 3800 base::File::FLAG_OPEN | base::File::FLAG_READ); |
3796 EXPECT_TRUE(cache_dir.Flush()); | 3801 EXPECT_TRUE(cache_dir.Flush()); |
3797 #endif // defined(OS_POSIX) | 3802 #endif // defined(OS_POSIX) |
3798 cache_.reset(); | 3803 cache_.reset(); |
3799 | 3804 |
3800 // The index is now written. Dooming the last entry can't delete a file, | 3805 // The index is now written. Dooming the last entry can't delete a file, |
3801 // because that would advance the cache directory mtime and invalidate the | 3806 // because that would advance the cache directory mtime and invalidate the |
3802 // index. | 3807 // index. |
3803 entry2->Doom(); | 3808 entry2->Doom(); |
3804 entry2->Close(); | 3809 entry2->Close(); |
3805 | 3810 |
3806 DisableFirstCleanup(); | 3811 DisableFirstCleanup(); |
3807 InitCache(); | 3812 InitCache(); |
3808 EXPECT_EQ(disk_cache::SimpleIndex::INITIALIZE_METHOD_LOADED, | 3813 EXPECT_EQ(disk_cache::SimpleIndex::INITIALIZE_METHOD_LOADED, |
3809 simple_cache_impl_->index()->init_method()); | 3814 simple_cache_impl_->index()->init_method()); |
3810 } | 3815 } |
OLD | NEW |