1// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/basictypes.h"
6#include "base/threading/platform_thread.h"
7#include "base/timer.h"
8#include "base/string_util.h"
9#include "net/base/io_buffer.h"
10#include "net/base/net_errors.h"
11#include "net/base/test_completion_callback.h"
12#include "net/disk_cache/backend_impl.h"
13#include "net/disk_cache/disk_cache_test_base.h"
14#include "net/disk_cache/disk_cache_test_util.h"
15#include "net/disk_cache/entry_impl.h"
16#include "net/disk_cache/mem_entry_impl.h"
17#include "testing/gtest/include/gtest/gtest.h"
18
19using base::Time;
20
21extern volatile int g_cache_tests_received;
22extern volatile bool g_cache_tests_error;
23
24// Tests that can run with different types of caches.
25class DiskCacheEntryTest : public DiskCacheTestWithCache {
26 public:
27  void InternalSyncIOBackground(disk_cache::Entry* entry);
28  void ExternalSyncIOBackground(disk_cache::Entry* entry);
29
30 protected:
31  void InternalSyncIO();
32  void InternalAsyncIO();
33  void ExternalSyncIO();
34  void ExternalAsyncIO();
35  void StreamAccess();
36  void GetKey();
37  void GetTimes();
38  void GrowData();
39  void TruncateData();
40  void ZeroLengthIO();
41  void Buffering();
42  void SizeChanges();
43  void ReuseEntry(int size);
44  void InvalidData();
45  void DoomNormalEntry();
46  void DoomedEntry();
47  void BasicSparseIO();
48  void HugeSparseIO();
49  void GetAvailableRange();
50  void CouldBeSparse();
51  void UpdateSparseEntry();
52  void DoomSparseEntry();
53  void PartialSparseEntry();
54};
55
56// Simple task to run part of a test from the cache thread.
57class SyncIOTask : public Task {
58 public:
59  SyncIOTask(DiskCacheEntryTest* test, disk_cache::Entry* entry)
60      : test_(test), entry_(entry) {}
61
62 protected:
63  DiskCacheEntryTest* test_;
64  disk_cache::Entry* entry_;
65};
66
67class InternalSyncIOTask : public SyncIOTask {
68 public:
69  InternalSyncIOTask(DiskCacheEntryTest* test, disk_cache::Entry* entry)
70      : SyncIOTask(test, entry) {}
71
72  virtual void Run() {
73    test_->InternalSyncIOBackground(entry_);
74  }
75};
76
77// This part of the test runs on the background thread.
78void DiskCacheEntryTest::InternalSyncIOBackground(disk_cache::Entry* entry) {
79  const int kSize1 = 10;
80  scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
81  CacheTestFillBuffer(buffer1->data(), kSize1, false);
82  EXPECT_EQ(0, entry->ReadData(0, 0, buffer1, kSize1, NULL));
83  base::strlcpy(buffer1->data(), "the data", kSize1);
84  EXPECT_EQ(10, entry->WriteData(0, 0, buffer1, kSize1, NULL, false));
85  memset(buffer1->data(), 0, kSize1);
86  EXPECT_EQ(10, entry->ReadData(0, 0, buffer1, kSize1, NULL));
87  EXPECT_STREQ("the data", buffer1->data());
88
89  const int kSize2 = 5000;
90  const int kSize3 = 10000;
91  scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize2));
92  scoped_refptr<net::IOBuffer> buffer3(new net::IOBuffer(kSize3));
93  memset(buffer3->data(), 0, kSize3);
94  CacheTestFillBuffer(buffer2->data(), kSize2, false);
95  base::strlcpy(buffer2->data(), "The really big data goes here", kSize2);
96  EXPECT_EQ(5000, entry->WriteData(1, 1500, buffer2, kSize2, NULL, false));
97  memset(buffer2->data(), 0, kSize2);
98  EXPECT_EQ(4989, entry->ReadData(1, 1511, buffer2, kSize2, NULL));
99  EXPECT_STREQ("big data goes here", buffer2->data());
100  EXPECT_EQ(5000, entry->ReadData(1, 0, buffer2, kSize2, NULL));
101  EXPECT_EQ(0, memcmp(buffer2->data(), buffer3->data(), 1500));
102  EXPECT_EQ(1500, entry->ReadData(1, 5000, buffer2, kSize2, NULL));
103
104  EXPECT_EQ(0, entry->ReadData(1, 6500, buffer2, kSize2, NULL));
105  EXPECT_EQ(6500, entry->ReadData(1, 0, buffer3, kSize3, NULL));
106  EXPECT_EQ(8192, entry->WriteData(1, 0, buffer3, 8192, NULL, false));
107  EXPECT_EQ(8192, entry->ReadData(1, 0, buffer3, kSize3, NULL));
108  EXPECT_EQ(8192, entry->GetDataSize(1));
109
110  // We need to delete the memory buffer on this thread.
111  EXPECT_EQ(0, entry->WriteData(0, 0, NULL, 0, NULL, true));
112  EXPECT_EQ(0, entry->WriteData(1, 0, NULL, 0, NULL, true));
113}
114
115// We need to support synchronous IO even though it is not a supported operation
116// from the point of view of the disk cache's public interface, because we use
117// it internally, not just by a few tests, but as part of the implementation
118// (see sparse_control.cc, for example).
119void DiskCacheEntryTest::InternalSyncIO() {
120  disk_cache::Entry* entry = NULL;
121  ASSERT_EQ(net::OK, CreateEntry("the first key", &entry));
122  ASSERT_TRUE(NULL != entry);
123
124  // The bulk of the test runs from within the task, on the cache thread.
125  RunTaskForTest(new InternalSyncIOTask(this, entry));
126
127  entry->Doom();
128  entry->Close();
129  FlushQueueForTest();
130  EXPECT_EQ(0, cache_->GetEntryCount());
131}
132
133TEST_F(DiskCacheEntryTest, InternalSyncIO) {
134  SetDirectMode();
135  InitCache();
136  InternalSyncIO();
137}
138
139TEST_F(DiskCacheEntryTest, MemoryOnlyInternalSyncIO) {
140  SetMemoryOnlyMode();
141  InitCache();
142  InternalSyncIO();
143}
144
145void DiskCacheEntryTest::InternalAsyncIO() {
146  disk_cache::Entry* entry = NULL;
147  ASSERT_EQ(net::OK, CreateEntry("the first key", &entry));
148  ASSERT_TRUE(NULL != entry);
149
150  // Avoid using internal buffers for the test. We have to write something to
151  // the entry and close it so that we flush the internal buffer to disk. After
152  // that, IO operations will be really hitting the disk. We don't care about
153  // the content, so just extending the entry is enough (all extensions zero-
154  // fill any holes).
155  EXPECT_EQ(0, WriteData(entry, 0, 15 * 1024, NULL, 0, false));
156  EXPECT_EQ(0, WriteData(entry, 1, 15 * 1024, NULL, 0, false));
157  entry->Close();
158  ASSERT_EQ(net::OK, OpenEntry("the first key", &entry));
159
160  // Let's verify that each IO goes to the right callback object.
161  CallbackTest callback1(false);
162  CallbackTest callback2(false);
163  CallbackTest callback3(false);
164  CallbackTest callback4(false);
165  CallbackTest callback5(false);
166  CallbackTest callback6(false);
167  CallbackTest callback7(false);
168  CallbackTest callback8(false);
169  CallbackTest callback9(false);
170  CallbackTest callback10(false);
171  CallbackTest callback11(false);
172  CallbackTest callback12(false);
173  CallbackTest callback13(false);
174
175  g_cache_tests_error = false;
176  g_cache_tests_received = 0;
177
178  MessageLoopHelper helper;
179
180  const int kSize1 = 10;
181  const int kSize2 = 5000;
182  const int kSize3 = 10000;
183  scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
184  scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize2));
185  scoped_refptr<net::IOBuffer> buffer3(new net::IOBuffer(kSize3));
186  CacheTestFillBuffer(buffer1->data(), kSize1, false);
187  CacheTestFillBuffer(buffer2->data(), kSize2, false);
188  CacheTestFillBuffer(buffer3->data(), kSize3, false);
189
190  EXPECT_EQ(0, entry->ReadData(0, 15 * 1024, buffer1, kSize1, &callback1));
191  base::strlcpy(buffer1->data(), "the data", kSize1);
192  int expected = 0;
193  int ret = entry->WriteData(0, 0, buffer1, kSize1, &callback2, false);
194  EXPECT_TRUE(10 == ret || net::ERR_IO_PENDING == ret);
195  if (net::ERR_IO_PENDING == ret)
196    expected++;
197
198  EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
199  memset(buffer2->data(), 0, kSize2);
200  ret = entry->ReadData(0, 0, buffer2, kSize1, &callback3);
201  EXPECT_TRUE(10 == ret || net::ERR_IO_PENDING == ret);
202  if (net::ERR_IO_PENDING == ret)
203    expected++;
204
205  EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
206  EXPECT_STREQ("the data", buffer2->data());
207
208  base::strlcpy(buffer2->data(), "The really big data goes here", kSize2);
209  ret = entry->WriteData(1, 1500, buffer2, kSize2, &callback4, true);
210  EXPECT_TRUE(5000 == ret || net::ERR_IO_PENDING == ret);
211  if (net::ERR_IO_PENDING == ret)
212    expected++;
213
214  EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
215  memset(buffer3->data(), 0, kSize3);
216  ret = entry->ReadData(1, 1511, buffer3, kSize2, &callback5);
217  EXPECT_TRUE(4989 == ret || net::ERR_IO_PENDING == ret);
218  if (net::ERR_IO_PENDING == ret)
219    expected++;
220
221  EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
222  EXPECT_STREQ("big data goes here", buffer3->data());
223  ret = entry->ReadData(1, 0, buffer2, kSize2, &callback6);
224  EXPECT_TRUE(5000 == ret || net::ERR_IO_PENDING == ret);
225  if (net::ERR_IO_PENDING == ret)
226    expected++;
227
228  memset(buffer3->data(), 0, kSize3);
229
230  EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
231  EXPECT_EQ(0, memcmp(buffer2->data(), buffer3->data(), 1500));
232  ret = entry->ReadData(1, 5000, buffer2, kSize2, &callback7);
233  EXPECT_TRUE(1500 == ret || net::ERR_IO_PENDING == ret);
234  if (net::ERR_IO_PENDING == ret)
235    expected++;
236
237  ret = entry->ReadData(1, 0, buffer3, kSize3, &callback9);
238  EXPECT_TRUE(6500 == ret || net::ERR_IO_PENDING == ret);
239  if (net::ERR_IO_PENDING == ret)
240    expected++;
241
242  ret = entry->WriteData(1, 0, buffer3, 8192, &callback10, true);
243  EXPECT_TRUE(8192 == ret || net::ERR_IO_PENDING == ret);
244  if (net::ERR_IO_PENDING == ret)
245    expected++;
246
247  EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
248  ret = entry->ReadData(1, 0, buffer3, kSize3, &callback11);
249  EXPECT_TRUE(8192 == ret || net::ERR_IO_PENDING == ret);
250  if (net::ERR_IO_PENDING == ret)
251    expected++;
252
253  EXPECT_EQ(8192, entry->GetDataSize(1));
254
255  ret = entry->ReadData(0, 0, buffer1, kSize1, &callback12);
256  EXPECT_TRUE(10 == ret || net::ERR_IO_PENDING == ret);
257  if (net::ERR_IO_PENDING == ret)
258    expected++;
259
260  ret = entry->ReadData(1, 0, buffer2, kSize2, &callback13);
261  EXPECT_TRUE(5000 == ret || net::ERR_IO_PENDING == ret);
262  if (net::ERR_IO_PENDING == ret)
263    expected++;
264
265  EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
266
267  EXPECT_FALSE(g_cache_tests_error);
268  EXPECT_EQ(expected, g_cache_tests_received);
269
270  entry->Doom();
271  entry->Close();
272  FlushQueueForTest();
273  EXPECT_EQ(0, cache_->GetEntryCount());
274}
275
276TEST_F(DiskCacheEntryTest, InternalAsyncIO) {
277  SetDirectMode();
278  InitCache();
279  InternalAsyncIO();
280}
281
282TEST_F(DiskCacheEntryTest, MemoryOnlyInternalAsyncIO) {
283  SetMemoryOnlyMode();
284  InitCache();
285  InternalAsyncIO();
286}
287
288class ExternalSyncIOTask : public SyncIOTask {
289 public:
290  ExternalSyncIOTask(DiskCacheEntryTest* test, disk_cache::Entry* entry)
291      : SyncIOTask(test, entry) {}
292
293  virtual void Run() {
294    test_->ExternalSyncIOBackground(entry_);
295  }
296};
297
298// This part of the test runs on the background thread.
299void DiskCacheEntryTest::ExternalSyncIOBackground(disk_cache::Entry* entry) {
300  const int kSize1 = 17000;
301  const int kSize2 = 25000;
302  scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
303  scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize2));
304  CacheTestFillBuffer(buffer1->data(), kSize1, false);
305  CacheTestFillBuffer(buffer2->data(), kSize2, false);
306  base::strlcpy(buffer1->data(), "the data", kSize1);
307  EXPECT_EQ(17000, entry->WriteData(0, 0, buffer1, kSize1, NULL, false));
308  memset(buffer1->data(), 0, kSize1);
309  EXPECT_EQ(17000, entry->ReadData(0, 0, buffer1, kSize1, NULL));
310  EXPECT_STREQ("the data", buffer1->data());
311
312  base::strlcpy(buffer2->data(), "The really big data goes here", kSize2);
313  EXPECT_EQ(25000, entry->WriteData(1, 10000, buffer2, kSize2, NULL, false));
314  memset(buffer2->data(), 0, kSize2);
315  EXPECT_EQ(24989, entry->ReadData(1, 10011, buffer2, kSize2, NULL));
316  EXPECT_STREQ("big data goes here", buffer2->data());
317  EXPECT_EQ(25000, entry->ReadData(1, 0, buffer2, kSize2, NULL));
318  EXPECT_EQ(0, memcmp(buffer2->data(), buffer2->data(), 10000));
319  EXPECT_EQ(5000, entry->ReadData(1, 30000, buffer2, kSize2, NULL));
320
321  EXPECT_EQ(0, entry->ReadData(1, 35000, buffer2, kSize2, NULL));
322  EXPECT_EQ(17000, entry->ReadData(1, 0, buffer1, kSize1, NULL));
323  EXPECT_EQ(17000, entry->WriteData(1, 20000, buffer1, kSize1, NULL, false));
324  EXPECT_EQ(37000, entry->GetDataSize(1));
325
326  // We need to delete the memory buffer on this thread.
327  EXPECT_EQ(0, entry->WriteData(0, 0, NULL, 0, NULL, true));
328  EXPECT_EQ(0, entry->WriteData(1, 0, NULL, 0, NULL, true));
329}
330
331void DiskCacheEntryTest::ExternalSyncIO() {
332  disk_cache::Entry* entry;
333  ASSERT_EQ(net::OK, CreateEntry("the first key", &entry));
334
335  // The bulk of the test runs from within the task, on the cache thread.
336  RunTaskForTest(new ExternalSyncIOTask(this, entry));
337
338  entry->Doom();
339  entry->Close();
340  FlushQueueForTest();
341  EXPECT_EQ(0, cache_->GetEntryCount());
342}
343
344TEST_F(DiskCacheEntryTest, ExternalSyncIO) {
345  SetDirectMode();
346  InitCache();
347  ExternalSyncIO();
348}
349
350TEST_F(DiskCacheEntryTest, ExternalSyncIONoBuffer) {
351  SetDirectMode();
352  InitCache();
353  cache_impl_->SetFlags(disk_cache::kNoBuffering);
354  ExternalSyncIO();
355}
356
357TEST_F(DiskCacheEntryTest, MemoryOnlyExternalSyncIO) {
358  SetMemoryOnlyMode();
359  InitCache();
360  ExternalSyncIO();
361}
362
363void DiskCacheEntryTest::ExternalAsyncIO() {
364  disk_cache::Entry* entry;
365  ASSERT_EQ(net::OK, CreateEntry("the first key", &entry));
366
367  // Let's verify that each IO goes to the right callback object.
368  CallbackTest callback1(false);
369  CallbackTest callback2(false);
370  CallbackTest callback3(false);
371  CallbackTest callback4(false);
372  CallbackTest callback5(false);
373  CallbackTest callback6(false);
374  CallbackTest callback7(false);
375  CallbackTest callback8(false);
376  CallbackTest callback9(false);
377
378  g_cache_tests_error = false;
379  g_cache_tests_received = 0;
380  int expected = 0;
381
382  MessageLoopHelper helper;
383
384  const int kSize1 = 17000;
385  const int kSize2 = 25000;
386  const int kSize3 = 25000;
387  scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
388  scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize2));
389  scoped_refptr<net::IOBuffer> buffer3(new net::IOBuffer(kSize3));
390  CacheTestFillBuffer(buffer1->data(), kSize1, false);
391  CacheTestFillBuffer(buffer2->data(), kSize2, false);
392  CacheTestFillBuffer(buffer3->data(), kSize3, false);
393  base::strlcpy(buffer1->data(), "the data", kSize1);
394  int ret = entry->WriteData(0, 0, buffer1, kSize1, &callback1, false);
395  EXPECT_TRUE(17000 == ret || net::ERR_IO_PENDING == ret);
396  if (net::ERR_IO_PENDING == ret)
397    expected++;
398
399  EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
400
401  memset(buffer2->data(), 0, kSize1);
402  ret = entry->ReadData(0, 0, buffer2, kSize1, &callback2);
403  EXPECT_TRUE(17000 == ret || net::ERR_IO_PENDING == ret);
404  if (net::ERR_IO_PENDING == ret)
405    expected++;
406
407  EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
408  EXPECT_STREQ("the data", buffer1->data());
409
410  base::strlcpy(buffer2->data(), "The really big data goes here", kSize2);
411  ret = entry->WriteData(1, 10000, buffer2, kSize2, &callback3, false);
412  EXPECT_TRUE(25000 == ret || net::ERR_IO_PENDING == ret);
413  if (net::ERR_IO_PENDING == ret)
414    expected++;
415
416  EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
417
418  memset(buffer3->data(), 0, kSize3);
419  ret = entry->ReadData(1, 10011, buffer3, kSize3, &callback4);
420  EXPECT_TRUE(24989 == ret || net::ERR_IO_PENDING == ret);
421  if (net::ERR_IO_PENDING == ret)
422    expected++;
423
424  EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
425  EXPECT_STREQ("big data goes here", buffer3->data());
426  ret = entry->ReadData(1, 0, buffer2, kSize2, &callback5);
427  EXPECT_TRUE(25000 == ret || net::ERR_IO_PENDING == ret);
428  if (net::ERR_IO_PENDING == ret)
429    expected++;
430
431  EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
432  EXPECT_EQ(0, memcmp(buffer2->data(), buffer2->data(), 10000));
433  ret = entry->ReadData(1, 30000, buffer2, kSize2, &callback6);
434  EXPECT_TRUE(5000 == ret || net::ERR_IO_PENDING == ret);
435  if (net::ERR_IO_PENDING == ret)
436    expected++;
437
438  EXPECT_EQ(0, entry->ReadData(1, 35000, buffer2, kSize2, &callback7));
439  ret = entry->ReadData(1, 0, buffer1, kSize1, &callback8);
440  EXPECT_TRUE(17000 == ret || net::ERR_IO_PENDING == ret);
441  if (net::ERR_IO_PENDING == ret)
442    expected++;
443  ret = entry->WriteData(1, 20000, buffer1, kSize1, &callback9, false);
444  EXPECT_TRUE(17000 == ret || net::ERR_IO_PENDING == ret);
445  if (net::ERR_IO_PENDING == ret)
446    expected++;
447
448  EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
449  EXPECT_EQ(37000, entry->GetDataSize(1));
450
451  EXPECT_FALSE(g_cache_tests_error);
452  EXPECT_EQ(expected, g_cache_tests_received);
453
454  entry->Doom();
455  entry->Close();
456  FlushQueueForTest();
457  EXPECT_EQ(0, cache_->GetEntryCount());
458}
459
460TEST_F(DiskCacheEntryTest, ExternalAsyncIO) {
461  SetDirectMode();
462  InitCache();
463  ExternalAsyncIO();
464}
465
466TEST_F(DiskCacheEntryTest, ExternalAsyncIONoBuffer) {
467  SetDirectMode();
468  InitCache();
469  cache_impl_->SetFlags(disk_cache::kNoBuffering);
470  ExternalAsyncIO();
471}
472
473TEST_F(DiskCacheEntryTest, MemoryOnlyExternalAsyncIO) {
474  SetMemoryOnlyMode();
475  InitCache();
476  ExternalAsyncIO();
477}
478
479void DiskCacheEntryTest::StreamAccess() {
480  disk_cache::Entry* entry = NULL;
481  ASSERT_EQ(net::OK, CreateEntry("the first key", &entry));
482  ASSERT_TRUE(NULL != entry);
483
484  const int kBufferSize = 1024;
485  scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kBufferSize));
486  scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kBufferSize));
487
488  const int kNumStreams = 3;
489  for (int i = 0; i < kNumStreams; i++) {
490    CacheTestFillBuffer(buffer1->data(), kBufferSize, false);
491    EXPECT_EQ(kBufferSize, WriteData(entry, i, 0, buffer1, kBufferSize, false));
492    memset(buffer2->data(), 0, kBufferSize);
493    EXPECT_EQ(kBufferSize, ReadData(entry, i, 0, buffer2, kBufferSize));
494    EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kBufferSize));
495  }
496
497  EXPECT_EQ(net::ERR_INVALID_ARGUMENT,
498            ReadData(entry, kNumStreams, 0, buffer1, kBufferSize));
499  entry->Close();
500}
501
502TEST_F(DiskCacheEntryTest, StreamAccess) {
503  InitCache();
504  StreamAccess();
505}
506
507TEST_F(DiskCacheEntryTest, MemoryOnlyStreamAccess) {
508  SetMemoryOnlyMode();
509  InitCache();
510  StreamAccess();
511}
512
513void DiskCacheEntryTest::GetKey() {
514  std::string key("the first key");
515  disk_cache::Entry* entry;
516  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
517  EXPECT_EQ(key, entry->GetKey()) << "short key";
518  entry->Close();
519
520  int seed = static_cast<int>(Time::Now().ToInternalValue());
521  srand(seed);
522  char key_buffer[20000];
523
524  CacheTestFillBuffer(key_buffer, 3000, true);
525  key_buffer[1000] = '\0';
526
527  key = key_buffer;
528  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
529  EXPECT_TRUE(key == entry->GetKey()) << "1000 bytes key";
530  entry->Close();
531
532  key_buffer[1000] = 'p';
533  key_buffer[3000] = '\0';
534  key = key_buffer;
535  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
536  EXPECT_TRUE(key == entry->GetKey()) << "medium size key";
537  entry->Close();
538
539  CacheTestFillBuffer(key_buffer, sizeof(key_buffer), true);
540  key_buffer[19999] = '\0';
541
542  key = key_buffer;
543  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
544  EXPECT_TRUE(key == entry->GetKey()) << "long key";
545  entry->Close();
546}
547
548TEST_F(DiskCacheEntryTest, GetKey) {
549  InitCache();
550  GetKey();
551}
552
553TEST_F(DiskCacheEntryTest, MemoryOnlyGetKey) {
554  SetMemoryOnlyMode();
555  InitCache();
556  GetKey();
557}
558
559void DiskCacheEntryTest::GetTimes() {
560  std::string key("the first key");
561  disk_cache::Entry* entry;
562
563  Time t1 = Time::Now();
564  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
565  EXPECT_TRUE(entry->GetLastModified() >= t1);
566  EXPECT_TRUE(entry->GetLastModified() == entry->GetLastUsed());
567
568  base::PlatformThread::Sleep(20);
569  Time t2 = Time::Now();
570  EXPECT_TRUE(t2 > t1);
571  EXPECT_EQ(0, WriteData(entry, 0, 200, NULL, 0, false));
572  if (type_ == net::APP_CACHE) {
573    EXPECT_TRUE(entry->GetLastModified() < t2);
574  } else {
575    EXPECT_TRUE(entry->GetLastModified() >= t2);
576  }
577  EXPECT_TRUE(entry->GetLastModified() == entry->GetLastUsed());
578
579  base::PlatformThread::Sleep(20);
580  Time t3 = Time::Now();
581  EXPECT_TRUE(t3 > t2);
582  const int kSize = 200;
583  scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
584  EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer, kSize));
585  if (type_ == net::APP_CACHE) {
586    EXPECT_TRUE(entry->GetLastUsed() < t2);
587    EXPECT_TRUE(entry->GetLastModified() < t2);
588  } else {
589    EXPECT_TRUE(entry->GetLastUsed() >= t3);
590    EXPECT_TRUE(entry->GetLastModified() < t3);
591  }
592  entry->Close();
593}
594
595TEST_F(DiskCacheEntryTest, GetTimes) {
596  InitCache();
597  GetTimes();
598}
599
600TEST_F(DiskCacheEntryTest, MemoryOnlyGetTimes) {
601  SetMemoryOnlyMode();
602  InitCache();
603  GetTimes();
604}
605
606TEST_F(DiskCacheEntryTest, AppCacheGetTimes) {
607  SetCacheType(net::APP_CACHE);
608  InitCache();
609  GetTimes();
610}
611
612void DiskCacheEntryTest::GrowData() {
613  std::string key1("the first key");
614  disk_cache::Entry* entry;
615  ASSERT_EQ(net::OK, CreateEntry(key1, &entry));
616
617  const int kSize = 20000;
618  scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
619  scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize));
620  CacheTestFillBuffer(buffer1->data(), kSize, false);
621  memset(buffer2->data(), 0, kSize);
622
623  base::strlcpy(buffer1->data(), "the data", kSize);
624  EXPECT_EQ(10, WriteData(entry, 0, 0, buffer1, 10, false));
625  EXPECT_EQ(10, ReadData(entry, 0, 0, buffer2, 10));
626  EXPECT_STREQ("the data", buffer2->data());
627  EXPECT_EQ(10, entry->GetDataSize(0));
628
629  EXPECT_EQ(2000, WriteData(entry, 0, 0, buffer1, 2000, false));
630  EXPECT_EQ(2000, entry->GetDataSize(0));
631  EXPECT_EQ(2000, ReadData(entry, 0, 0, buffer2, 2000));
632  EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), 2000));
633
634  EXPECT_EQ(20000, WriteData(entry, 0, 0, buffer1, kSize, false));
635  EXPECT_EQ(20000, entry->GetDataSize(0));
636  EXPECT_EQ(20000, ReadData(entry, 0, 0, buffer2, kSize));
637  EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), kSize));
638  entry->Close();
639
640  memset(buffer2->data(), 0, kSize);
641  std::string key2("Second key");
642  ASSERT_EQ(net::OK, CreateEntry(key2, &entry));
643  EXPECT_EQ(10, WriteData(entry, 0, 0, buffer1, 10, false));
644  EXPECT_EQ(10, entry->GetDataSize(0));
645  entry->Close();
646
647  // Go from an internal address to a bigger block size.
648  ASSERT_EQ(net::OK, OpenEntry(key2, &entry));
649  EXPECT_EQ(2000, WriteData(entry, 0, 0, buffer1, 2000, false));
650  EXPECT_EQ(2000, entry->GetDataSize(0));
651  EXPECT_EQ(2000, ReadData(entry, 0, 0, buffer2, 2000));
652  EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), 2000));
653  entry->Close();
654  memset(buffer2->data(), 0, kSize);
655
656  // Go from an internal address to an external one.
657  ASSERT_EQ(net::OK, OpenEntry(key2, &entry));
658  EXPECT_EQ(20000, WriteData(entry, 0, 0, buffer1, kSize, false));
659  EXPECT_EQ(20000, entry->GetDataSize(0));
660  EXPECT_EQ(20000, ReadData(entry, 0, 0, buffer2, kSize));
661  EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), kSize));
662  entry->Close();
663
664  // Double check the size from disk.
665  ASSERT_EQ(net::OK, OpenEntry(key2, &entry));
666  EXPECT_EQ(20000, entry->GetDataSize(0));
667
668  // Now extend the entry without actual data.
669  EXPECT_EQ(0, WriteData(entry, 0, 45500, buffer1, 0, false));
670  entry->Close();
671
672  // And check again from disk.
673  ASSERT_EQ(net::OK, OpenEntry(key2, &entry));
674  EXPECT_EQ(45500, entry->GetDataSize(0));
675  entry->Close();
676}
677
678TEST_F(DiskCacheEntryTest, GrowData) {
679  InitCache();
680  GrowData();
681}
682
683TEST_F(DiskCacheEntryTest, GrowDataNoBuffer) {
684  SetDirectMode();
685  InitCache();
686  cache_impl_->SetFlags(disk_cache::kNoBuffering);
687  GrowData();
688}
689
690TEST_F(DiskCacheEntryTest, MemoryOnlyGrowData) {
691  SetMemoryOnlyMode();
692  InitCache();
693  GrowData();
694}
695
696void DiskCacheEntryTest::TruncateData() {
697  std::string key("the first key");
698  disk_cache::Entry* entry;
699  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
700
701  const int kSize1 = 20000;
702  const int kSize2 = 20000;
703  scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
704  scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize2));
705
706  CacheTestFillBuffer(buffer1->data(), kSize1, false);
707  memset(buffer2->data(), 0, kSize2);
708
709  // Simple truncation:
710  EXPECT_EQ(200, WriteData(entry, 0, 0, buffer1, 200, false));
711  EXPECT_EQ(200, entry->GetDataSize(0));
712  EXPECT_EQ(100, WriteData(entry, 0, 0, buffer1, 100, false));
713  EXPECT_EQ(200, entry->GetDataSize(0));
714  EXPECT_EQ(100, WriteData(entry, 0, 0, buffer1, 100, true));
715  EXPECT_EQ(100, entry->GetDataSize(0));
716  EXPECT_EQ(0, WriteData(entry, 0, 50, buffer1, 0, true));
717  EXPECT_EQ(50, entry->GetDataSize(0));
718  EXPECT_EQ(0, WriteData(entry, 0, 0, buffer1, 0, true));
719  EXPECT_EQ(0, entry->GetDataSize(0));
720  entry->Close();
721  ASSERT_EQ(net::OK, OpenEntry(key, &entry));
722
723  // Go to an external file.
724  EXPECT_EQ(20000, WriteData(entry, 0, 0, buffer1, 20000, true));
725  EXPECT_EQ(20000, entry->GetDataSize(0));
726  EXPECT_EQ(20000, ReadData(entry, 0, 0, buffer2, 20000));
727  EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), 20000));
728  memset(buffer2->data(), 0, kSize2);
729
730  // External file truncation
731  EXPECT_EQ(18000, WriteData(entry, 0, 0, buffer1, 18000, false));
732  EXPECT_EQ(20000, entry->GetDataSize(0));
733  EXPECT_EQ(18000, WriteData(entry, 0, 0, buffer1, 18000, true));
734  EXPECT_EQ(18000, entry->GetDataSize(0));
735  EXPECT_EQ(0, WriteData(entry, 0, 17500, buffer1, 0, true));
736  EXPECT_EQ(17500, entry->GetDataSize(0));
737
738  // And back to an internal block.
739  EXPECT_EQ(600, WriteData(entry, 0, 1000, buffer1, 600, true));
740  EXPECT_EQ(1600, entry->GetDataSize(0));
741  EXPECT_EQ(600, ReadData(entry, 0, 1000, buffer2, 600));
742  EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), 600));
743  EXPECT_EQ(1000, ReadData(entry, 0, 0, buffer2, 1000));
744  EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), 1000)) <<
745      "Preserves previous data";
746
747  // Go from external file to zero length.
748  EXPECT_EQ(20000, WriteData(entry, 0, 0, buffer1, 20000, true));
749  EXPECT_EQ(20000, entry->GetDataSize(0));
750  EXPECT_EQ(0, WriteData(entry, 0, 0, buffer1, 0, true));
751  EXPECT_EQ(0, entry->GetDataSize(0));
752
753  entry->Close();
754}
755
756TEST_F(DiskCacheEntryTest, TruncateData) {
757  InitCache();
758  TruncateData();
759}
760
761TEST_F(DiskCacheEntryTest, TruncateDataNoBuffer) {
762  SetDirectMode();
763  InitCache();
764  cache_impl_->SetFlags(disk_cache::kNoBuffering);
765  TruncateData();
766}
767
768TEST_F(DiskCacheEntryTest, MemoryOnlyTruncateData) {
769  SetMemoryOnlyMode();
770  InitCache();
771  TruncateData();
772}
773
774void DiskCacheEntryTest::ZeroLengthIO() {
775  std::string key("the first key");
776  disk_cache::Entry* entry;
777  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
778
779  EXPECT_EQ(0, ReadData(entry, 0, 0, NULL, 0));
780  EXPECT_EQ(0, WriteData(entry, 0, 0, NULL, 0, false));
781
782  // This write should extend the entry.
783  EXPECT_EQ(0, WriteData(entry, 0, 1000, NULL, 0, false));
784  EXPECT_EQ(0, ReadData(entry, 0, 500, NULL, 0));
785  EXPECT_EQ(0, ReadData(entry, 0, 2000, NULL, 0));
786  EXPECT_EQ(1000, entry->GetDataSize(0));
787
788  EXPECT_EQ(0, WriteData(entry, 0, 100000, NULL, 0, true));
789  EXPECT_EQ(0, ReadData(entry, 0, 50000, NULL, 0));
790  EXPECT_EQ(100000, entry->GetDataSize(0));
791
792  // Let's verify the actual content.
793  const int kSize = 20;
794  const char zeros[kSize] = {};
795  scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
796
797  CacheTestFillBuffer(buffer->data(), kSize, false);
798  EXPECT_EQ(kSize, ReadData(entry, 0, 500, buffer, kSize));
799  EXPECT_TRUE(!memcmp(buffer->data(), zeros, kSize));
800
801  CacheTestFillBuffer(buffer->data(), kSize, false);
802  EXPECT_EQ(kSize, ReadData(entry, 0, 5000, buffer, kSize));
803  EXPECT_TRUE(!memcmp(buffer->data(), zeros, kSize));
804
805  CacheTestFillBuffer(buffer->data(), kSize, false);
806  EXPECT_EQ(kSize, ReadData(entry, 0, 50000, buffer, kSize));
807  EXPECT_TRUE(!memcmp(buffer->data(), zeros, kSize));
808
809  entry->Close();
810}
811
812TEST_F(DiskCacheEntryTest, ZeroLengthIO) {
813  InitCache();
814  ZeroLengthIO();
815}
816
817TEST_F(DiskCacheEntryTest, ZeroLengthIONoBuffer) {
818  SetDirectMode();
819  InitCache();
820  cache_impl_->SetFlags(disk_cache::kNoBuffering);
821  ZeroLengthIO();
822}
823
824TEST_F(DiskCacheEntryTest, MemoryOnlyZeroLengthIO) {
825  SetMemoryOnlyMode();
826  InitCache();
827  ZeroLengthIO();
828}
829
830// Tests that we handle the content correctly when buffering.
831void DiskCacheEntryTest::Buffering() {
832  std::string key("the first key");
833  disk_cache::Entry* entry;
834  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
835
836  const int kSize = 200;
837  scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
838  scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize));
839  CacheTestFillBuffer(buffer1->data(), kSize, true);
840  CacheTestFillBuffer(buffer2->data(), kSize, true);
841
842  EXPECT_EQ(kSize, WriteData(entry, 1, 0, buffer1, kSize, false));
843  entry->Close();
844
845  // Write a little more and read what we wrote before.
846  ASSERT_EQ(net::OK, OpenEntry(key, &entry));
847  EXPECT_EQ(kSize, WriteData(entry, 1, 5000, buffer1, kSize, false));
848  EXPECT_EQ(kSize, ReadData(entry, 1, 0, buffer2, kSize));
849  EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data(), kSize));
850
851  // Now go to an external file.
852  EXPECT_EQ(kSize, WriteData(entry, 1, 18000, buffer1, kSize, false));
853  entry->Close();
854
855  // Write something else and verify old data.
856  ASSERT_EQ(net::OK, OpenEntry(key, &entry));
857  EXPECT_EQ(kSize, WriteData(entry, 1, 10000, buffer1, kSize, false));
858  CacheTestFillBuffer(buffer2->data(), kSize, true);
859  EXPECT_EQ(kSize, ReadData(entry, 1, 5000, buffer2, kSize));
860  EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data(), kSize));
861  CacheTestFillBuffer(buffer2->data(), kSize, true);
862  EXPECT_EQ(kSize, ReadData(entry, 1, 0, buffer2, kSize));
863  EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data(), kSize));
864  CacheTestFillBuffer(buffer2->data(), kSize, true);
865  EXPECT_EQ(kSize, ReadData(entry, 1, 18000, buffer2, kSize));
866  EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data(), kSize));
867
868  // Extend the file some more.
869  EXPECT_EQ(kSize, WriteData(entry, 1, 23000, buffer1, kSize, false));
870  entry->Close();
871
872  // And now make sure that we can deal with data in both places (ram/disk).
873  ASSERT_EQ(net::OK, OpenEntry(key, &entry));
874  EXPECT_EQ(kSize, WriteData(entry, 1, 17000, buffer1, kSize, false));
875
876  // We should not overwrite the data at 18000 with this.
877  EXPECT_EQ(kSize, WriteData(entry, 1, 19000, buffer1, kSize, false));
878  CacheTestFillBuffer(buffer2->data(), kSize, true);
879  EXPECT_EQ(kSize, ReadData(entry, 1, 18000, buffer2, kSize));
880  EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data(), kSize));
881  CacheTestFillBuffer(buffer2->data(), kSize, true);
882  EXPECT_EQ(kSize, ReadData(entry, 1, 17000, buffer2, kSize));
883  EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data(), kSize));
884
885  EXPECT_EQ(kSize, WriteData(entry, 1, 22900, buffer1, kSize, false));
886  CacheTestFillBuffer(buffer2->data(), kSize, true);
887  EXPECT_EQ(100, ReadData(entry, 1, 23000, buffer2, kSize));
888  EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data() + 100, 100));
889
890  CacheTestFillBuffer(buffer2->data(), kSize, true);
891  EXPECT_EQ(100, ReadData(entry, 1, 23100, buffer2, kSize));
892  EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data() + 100, 100));
893
894  // Extend the file again and read before without closing the entry.
895  EXPECT_EQ(kSize, WriteData(entry, 1, 25000, buffer1, kSize, false));
896  EXPECT_EQ(kSize, WriteData(entry, 1, 45000, buffer1, kSize, false));
897  CacheTestFillBuffer(buffer2->data(), kSize, true);
898  EXPECT_EQ(kSize, ReadData(entry, 1, 25000, buffer2, kSize));
899  EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data(), kSize));
900  CacheTestFillBuffer(buffer2->data(), kSize, true);
901  EXPECT_EQ(kSize, ReadData(entry, 1, 45000, buffer2, kSize));
902  EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data(), kSize));
903
904  entry->Close();
905}
906
907TEST_F(DiskCacheEntryTest, Buffering) {
908  InitCache();
909  Buffering();
910}
911
912TEST_F(DiskCacheEntryTest, BufferingNoBuffer) {
913  SetDirectMode();
914  InitCache();
915  cache_impl_->SetFlags(disk_cache::kNoBuffering);
916  Buffering();
917}
918
919// Some extra tests to make sure that buffering works properly when changing
920// the entry size.
921void DiskCacheEntryTest::SizeChanges() {
922  std::string key("the first key");
923  disk_cache::Entry* entry;
924  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
925
926  const int kSize = 200;
927  const char zeros[kSize] = {};
928  scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
929  scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize));
930  CacheTestFillBuffer(buffer1->data(), kSize, true);
931  CacheTestFillBuffer(buffer2->data(), kSize, true);
932
933  EXPECT_EQ(kSize, WriteData(entry, 1, 0, buffer1, kSize, true));
934  EXPECT_EQ(kSize, WriteData(entry, 1, 17000, buffer1, kSize, true));
935  EXPECT_EQ(kSize, WriteData(entry, 1, 23000, buffer1, kSize, true));
936  entry->Close();
937
938  // Extend the file and read between the old size and the new write.
939  ASSERT_EQ(net::OK, OpenEntry(key, &entry));
940  EXPECT_EQ(23000 + kSize, entry->GetDataSize(1));
941  EXPECT_EQ(kSize, WriteData(entry, 1, 25000, buffer1, kSize, true));
942  EXPECT_EQ(25000 + kSize, entry->GetDataSize(1));
943  EXPECT_EQ(kSize, ReadData(entry, 1, 24000, buffer2, kSize));
944  EXPECT_TRUE(!memcmp(buffer2->data(), zeros, kSize));
945
946  // Read at the end of the old file size.
947  EXPECT_EQ(kSize, ReadData(entry, 1, 23000 + kSize - 35, buffer2, kSize));
948  EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data() + kSize - 35, 35));
949
950  // Read slightly before the last write.
951  CacheTestFillBuffer(buffer2->data(), kSize, true);
952  EXPECT_EQ(kSize, ReadData(entry, 1, 24900, buffer2, kSize));
953  EXPECT_TRUE(!memcmp(buffer2->data(), zeros, 100));
954  EXPECT_TRUE(!memcmp(buffer2->data() + 100, buffer1->data(), kSize - 100));
955
956  // Extend the entry a little more.
957  EXPECT_EQ(kSize, WriteData(entry, 1, 26000, buffer1, kSize, true));
958  EXPECT_EQ(26000 + kSize, entry->GetDataSize(1));
959  CacheTestFillBuffer(buffer2->data(), kSize, true);
960  EXPECT_EQ(kSize, ReadData(entry, 1, 25900, buffer2, kSize));
961  EXPECT_TRUE(!memcmp(buffer2->data(), zeros, 100));
962  EXPECT_TRUE(!memcmp(buffer2->data() + 100, buffer1->data(), kSize - 100));
963
964  // And now reduce the size.
965  EXPECT_EQ(kSize, WriteData(entry, 1, 25000, buffer1, kSize, true));
966  EXPECT_EQ(25000 + kSize, entry->GetDataSize(1));
967  EXPECT_EQ(28, ReadData(entry, 1, 25000 + kSize - 28, buffer2, kSize));
968  EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data() + kSize - 28, 28));
969
970  // Reduce the size with a buffer that is not extending the size.
971  EXPECT_EQ(kSize, WriteData(entry, 1, 24000, buffer1, kSize, false));
972  EXPECT_EQ(25000 + kSize, entry->GetDataSize(1));
973  EXPECT_EQ(kSize, WriteData(entry, 1, 24500, buffer1, kSize, true));
974  EXPECT_EQ(24500 + kSize, entry->GetDataSize(1));
975  EXPECT_EQ(kSize, ReadData(entry, 1, 23900, buffer2, kSize));
976  EXPECT_TRUE(!memcmp(buffer2->data(), zeros, 100));
977  EXPECT_TRUE(!memcmp(buffer2->data() + 100, buffer1->data(), kSize - 100));
978
979  // And now reduce the size below the old size.
980  EXPECT_EQ(kSize, WriteData(entry, 1, 19000, buffer1, kSize, true));
981  EXPECT_EQ(19000 + kSize, entry->GetDataSize(1));
982  EXPECT_EQ(kSize, ReadData(entry, 1, 18900, buffer2, kSize));
983  EXPECT_TRUE(!memcmp(buffer2->data(), zeros, 100));
984  EXPECT_TRUE(!memcmp(buffer2->data() + 100, buffer1->data(), kSize - 100));
985
986  // Verify that the actual file is truncated.
987  entry->Close();
988  ASSERT_EQ(net::OK, OpenEntry(key, &entry));
989  EXPECT_EQ(19000 + kSize, entry->GetDataSize(1));
990
991  entry->Close();
992}
993
994TEST_F(DiskCacheEntryTest, SizeChanges) {
995  InitCache();
996  SizeChanges();
997}
998
999TEST_F(DiskCacheEntryTest, SizeChangesNoBuffer) {
1000  SetDirectMode();
1001  InitCache();
1002  cache_impl_->SetFlags(disk_cache::kNoBuffering);
1003  SizeChanges();
1004}
1005
1006// Write more than the total cache capacity but to a single entry. |size| is the
1007// amount of bytes to write each time.
1008void DiskCacheEntryTest::ReuseEntry(int size) {
1009  std::string key1("the first key");
1010  disk_cache::Entry* entry;
1011  ASSERT_EQ(net::OK, CreateEntry(key1, &entry));
1012
1013  entry->Close();
1014  std::string key2("the second key");
1015  ASSERT_EQ(net::OK, CreateEntry(key2, &entry));
1016
1017  scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(size));
1018  CacheTestFillBuffer(buffer->data(), size, false);
1019
1020  for (int i = 0; i < 15; i++) {
1021    EXPECT_EQ(0, WriteData(entry, 0, 0, buffer, 0, true));
1022    EXPECT_EQ(size, WriteData(entry, 0, 0, buffer, size, false));
1023    entry->Close();
1024    ASSERT_EQ(net::OK, OpenEntry(key2, &entry));
1025  }
1026
1027  entry->Close();
1028  ASSERT_EQ(net::OK, OpenEntry(key1, &entry)) << "have not evicted this entry";
1029  entry->Close();
1030}
1031
1032TEST_F(DiskCacheEntryTest, ReuseExternalEntry) {
1033  SetDirectMode();
1034  SetMaxSize(200 * 1024);
1035  InitCache();
1036  ReuseEntry(20 * 1024);
1037}
1038
1039TEST_F(DiskCacheEntryTest, MemoryOnlyReuseExternalEntry) {
1040  SetDirectMode();
1041  SetMemoryOnlyMode();
1042  SetMaxSize(200 * 1024);
1043  InitCache();
1044  ReuseEntry(20 * 1024);
1045}
1046
1047TEST_F(DiskCacheEntryTest, ReuseInternalEntry) {
1048  SetDirectMode();
1049  SetMaxSize(100 * 1024);
1050  InitCache();
1051  ReuseEntry(10 * 1024);
1052}
1053
1054TEST_F(DiskCacheEntryTest, MemoryOnlyReuseInternalEntry) {
1055  SetDirectMode();
1056  SetMemoryOnlyMode();
1057  SetMaxSize(100 * 1024);
1058  InitCache();
1059  ReuseEntry(10 * 1024);
1060}
1061
1062// Reading somewhere that was not written should return zeros.
1063void DiskCacheEntryTest::InvalidData() {
1064  std::string key("the first key");
1065  disk_cache::Entry* entry;
1066  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1067
1068  const int kSize1 = 20000;
1069  const int kSize2 = 20000;
1070  const int kSize3 = 20000;
1071  scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
1072  scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize2));
1073  scoped_refptr<net::IOBuffer> buffer3(new net::IOBuffer(kSize3));
1074
1075  CacheTestFillBuffer(buffer1->data(), kSize1, false);
1076  memset(buffer2->data(), 0, kSize2);
1077
1078  // Simple data grow:
1079  EXPECT_EQ(200, WriteData(entry, 0, 400, buffer1, 200, false));
1080  EXPECT_EQ(600, entry->GetDataSize(0));
1081  EXPECT_EQ(100, ReadData(entry, 0, 300, buffer3, 100));
1082  EXPECT_TRUE(!memcmp(buffer3->data(), buffer2->data(), 100));
1083  entry->Close();
1084  ASSERT_EQ(net::OK, OpenEntry(key, &entry));
1085
1086  // The entry is now on disk. Load it and extend it.
1087  EXPECT_EQ(200, WriteData(entry, 0, 800, buffer1, 200, false));
1088  EXPECT_EQ(1000, entry->GetDataSize(0));
1089  EXPECT_EQ(100, ReadData(entry, 0, 700, buffer3, 100));
1090  EXPECT_TRUE(!memcmp(buffer3->data(), buffer2->data(), 100));
1091  entry->Close();
1092  ASSERT_EQ(net::OK, OpenEntry(key, &entry));
1093
1094  // This time using truncate.
1095  EXPECT_EQ(200, WriteData(entry, 0, 1800, buffer1, 200, true));
1096  EXPECT_EQ(2000, entry->GetDataSize(0));
1097  EXPECT_EQ(100, ReadData(entry, 0, 1500, buffer3, 100));
1098  EXPECT_TRUE(!memcmp(buffer3->data(), buffer2->data(), 100));
1099
1100  // Go to an external file.
1101  EXPECT_EQ(200, WriteData(entry, 0, 19800, buffer1, 200, false));
1102  EXPECT_EQ(20000, entry->GetDataSize(0));
1103  EXPECT_EQ(4000, ReadData(entry, 0, 14000, buffer3, 4000));
1104  EXPECT_TRUE(!memcmp(buffer3->data(), buffer2->data(), 4000));
1105
1106  // And back to an internal block.
1107  EXPECT_EQ(600, WriteData(entry, 0, 1000, buffer1, 600, true));
1108  EXPECT_EQ(1600, entry->GetDataSize(0));
1109  EXPECT_EQ(600, ReadData(entry, 0, 1000, buffer3, 600));
1110  EXPECT_TRUE(!memcmp(buffer3->data(), buffer1->data(), 600));
1111
1112  // Extend it again.
1113  EXPECT_EQ(600, WriteData(entry, 0, 2000, buffer1, 600, false));
1114  EXPECT_EQ(2600, entry->GetDataSize(0));
1115  EXPECT_EQ(200, ReadData(entry, 0, 1800, buffer3, 200));
1116  EXPECT_TRUE(!memcmp(buffer3->data(), buffer2->data(), 200));
1117
1118  // And again (with truncation flag).
1119  EXPECT_EQ(600, WriteData(entry, 0, 3000, buffer1, 600, true));
1120  EXPECT_EQ(3600, entry->GetDataSize(0));
1121  EXPECT_EQ(200, ReadData(entry, 0, 2800, buffer3, 200));
1122  EXPECT_TRUE(!memcmp(buffer3->data(), buffer2->data(), 200));
1123
1124  entry->Close();
1125}
1126
1127TEST_F(DiskCacheEntryTest, InvalidData) {
1128  InitCache();
1129  InvalidData();
1130}
1131
1132TEST_F(DiskCacheEntryTest, InvalidDataNoBuffer) {
1133  SetDirectMode();
1134  InitCache();
1135  cache_impl_->SetFlags(disk_cache::kNoBuffering);
1136  InvalidData();
1137}
1138
1139TEST_F(DiskCacheEntryTest, MemoryOnlyInvalidData) {
1140  SetMemoryOnlyMode();
1141  InitCache();
1142  InvalidData();
1143}
1144
1145// Tests that the cache preserves the buffer of an IO operation.
1146TEST_F(DiskCacheEntryTest, ReadWriteDestroyBuffer) {
1147  InitCache();
1148  std::string key("the first key");
1149  disk_cache::Entry* entry;
1150  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1151
1152  const int kSize = 200;
1153  scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
1154  CacheTestFillBuffer(buffer->data(), kSize, false);
1155
1156  TestCompletionCallback cb;
1157  EXPECT_EQ(net::ERR_IO_PENDING,
1158            entry->WriteData(0, 0, buffer, kSize, &cb, false));
1159
1160  // Release our reference to the buffer.
1161  buffer = NULL;
1162  EXPECT_EQ(kSize, cb.WaitForResult());
1163
1164  // And now test with a Read().
1165  buffer = new net::IOBuffer(kSize);
1166  CacheTestFillBuffer(buffer->data(), kSize, false);
1167
1168  EXPECT_EQ(net::ERR_IO_PENDING, entry->ReadData(0, 0, buffer, kSize, &cb));
1169  buffer = NULL;
1170  EXPECT_EQ(kSize, cb.WaitForResult());
1171
1172  entry->Close();
1173}
1174
1175void DiskCacheEntryTest::DoomNormalEntry() {
1176  std::string key("the first key");
1177  disk_cache::Entry* entry;
1178  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1179  entry->Doom();
1180  entry->Close();
1181
1182  const int kSize = 20000;
1183  scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
1184  CacheTestFillBuffer(buffer->data(), kSize, true);
1185  buffer->data()[19999] = '\0';
1186
1187  key = buffer->data();
1188  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1189  EXPECT_EQ(20000, WriteData(entry, 0, 0, buffer, kSize, false));
1190  EXPECT_EQ(20000, WriteData(entry, 1, 0, buffer, kSize, false));
1191  entry->Doom();
1192  entry->Close();
1193
1194  FlushQueueForTest();
1195  EXPECT_EQ(0, cache_->GetEntryCount());
1196}
1197
1198TEST_F(DiskCacheEntryTest, DoomEntry) {
1199  SetDirectMode();
1200  InitCache();
1201  DoomNormalEntry();
1202}
1203
1204TEST_F(DiskCacheEntryTest, MemoryOnlyDoomEntry) {
1205  SetMemoryOnlyMode();
1206  InitCache();
1207  DoomNormalEntry();
1208}
1209
1210// Verify that basic operations work as expected with doomed entries.
1211void DiskCacheEntryTest::DoomedEntry() {
1212  std::string key("the first key");
1213  disk_cache::Entry* entry;
1214  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1215  entry->Doom();
1216
1217  FlushQueueForTest();
1218  EXPECT_EQ(0, cache_->GetEntryCount());
1219  Time initial = Time::Now();
1220  base::PlatformThread::Sleep(20);
1221
1222  const int kSize1 = 2000;
1223  const int kSize2 = 2000;
1224  scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
1225  scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize2));
1226  CacheTestFillBuffer(buffer1->data(), kSize1, false);
1227  memset(buffer2->data(), 0, kSize2);
1228
1229  EXPECT_EQ(2000, WriteData(entry, 0, 0, buffer1, 2000, false));
1230  EXPECT_EQ(2000, ReadData(entry, 0, 0, buffer2, 2000));
1231  EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kSize1));
1232  EXPECT_EQ(key, entry->GetKey());
1233  EXPECT_TRUE(initial < entry->GetLastModified());
1234  EXPECT_TRUE(initial < entry->GetLastUsed());
1235
1236  entry->Close();
1237}
1238
1239TEST_F(DiskCacheEntryTest, DoomedEntry) {
1240  SetDirectMode();
1241  InitCache();
1242  DoomedEntry();
1243}
1244
1245TEST_F(DiskCacheEntryTest, MemoryOnlyDoomedEntry) {
1246  SetMemoryOnlyMode();
1247  InitCache();
1248  DoomedEntry();
1249}
1250
1251// Test that child entries in a memory cache backend are not visible from
1252// enumerations.
1253TEST_F(DiskCacheEntryTest, MemoryOnlyEnumerationWithSparseEntries) {
1254  SetMemoryOnlyMode();
1255  InitCache();
1256
1257  const int kSize = 4096;
1258  scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize));
1259  CacheTestFillBuffer(buf->data(), kSize, false);
1260
1261  std::string key("the first key");
1262  disk_cache::Entry* parent_entry;
1263  ASSERT_EQ(net::OK, CreateEntry(key, &parent_entry));
1264
1265  // Writes to the parent entry.
1266  EXPECT_EQ(kSize, parent_entry->WriteSparseData(0, buf, kSize, NULL));
1267
1268  // This write creates a child entry and writes to it.
1269  EXPECT_EQ(kSize, parent_entry->WriteSparseData(8192, buf, kSize, NULL));
1270
1271  parent_entry->Close();
1272
1273  // Perform the enumerations.
1274  void* iter = NULL;
1275  disk_cache::Entry* entry = NULL;
1276  int count = 0;
1277  while (OpenNextEntry(&iter, &entry) == net::OK) {
1278    ASSERT_TRUE(entry != NULL);
1279    ++count;
1280    disk_cache::MemEntryImpl* mem_entry =
1281        reinterpret_cast<disk_cache::MemEntryImpl*>(entry);
1282    EXPECT_EQ(disk_cache::MemEntryImpl::kParentEntry, mem_entry->type());
1283    mem_entry->Close();
1284  }
1285  EXPECT_EQ(1, count);
1286}
1287
1288// Writes |buf_1| to offset and reads it back as |buf_2|.
1289void VerifySparseIO(disk_cache::Entry* entry, int64 offset,
1290                    net::IOBuffer* buf_1, int size, net::IOBuffer* buf_2) {
1291  TestCompletionCallback cb;
1292
1293  memset(buf_2->data(), 0, size);
1294  int ret = entry->ReadSparseData(offset, buf_2, size, &cb);
1295  EXPECT_EQ(0, cb.GetResult(ret));
1296
1297  ret = entry->WriteSparseData(offset, buf_1, size, &cb);
1298  EXPECT_EQ(size, cb.GetResult(ret));
1299
1300  ret = entry->ReadSparseData(offset, buf_2, size, &cb);
1301  EXPECT_EQ(size, cb.GetResult(ret));
1302
1303  EXPECT_EQ(0, memcmp(buf_1->data(), buf_2->data(), size));
1304}
1305
1306// Reads |size| bytes from |entry| at |offset| and verifies that they are the
1307// same as the content of the provided |buffer|.
1308void VerifyContentSparseIO(disk_cache::Entry* entry, int64 offset, char* buffer,
1309                           int size) {
1310  TestCompletionCallback cb;
1311
1312  scoped_refptr<net::IOBuffer> buf_1(new net::IOBuffer(size));
1313  memset(buf_1->data(), 0, size);
1314  int ret = entry->ReadSparseData(offset, buf_1, size, &cb);
1315  EXPECT_EQ(size, cb.GetResult(ret));
1316
1317  EXPECT_EQ(0, memcmp(buf_1->data(), buffer, size));
1318}
1319
1320void DiskCacheEntryTest::BasicSparseIO() {
1321  std::string key("the first key");
1322  disk_cache::Entry* entry;
1323  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1324
1325  const int kSize = 2048;
1326  scoped_refptr<net::IOBuffer> buf_1(new net::IOBuffer(kSize));
1327  scoped_refptr<net::IOBuffer> buf_2(new net::IOBuffer(kSize));
1328  CacheTestFillBuffer(buf_1->data(), kSize, false);
1329
1330  // Write at offset 0.
1331  VerifySparseIO(entry, 0, buf_1, kSize, buf_2);
1332
1333  // Write at offset 0x400000 (4 MB).
1334  VerifySparseIO(entry, 0x400000, buf_1, kSize, buf_2);
1335
1336  // Write at offset 0x800000000 (32 GB).
1337  VerifySparseIO(entry, 0x800000000LL, buf_1, kSize, buf_2);
1338
1339  entry->Close();
1340
1341  // Check everything again.
1342  ASSERT_EQ(net::OK, OpenEntry(key, &entry));
1343  VerifyContentSparseIO(entry, 0, buf_1->data(), kSize);
1344  VerifyContentSparseIO(entry, 0x400000, buf_1->data(), kSize);
1345  VerifyContentSparseIO(entry, 0x800000000LL, buf_1->data(), kSize);
1346  entry->Close();
1347}
1348
1349TEST_F(DiskCacheEntryTest, BasicSparseIO) {
1350  InitCache();
1351  BasicSparseIO();
1352}
1353
1354TEST_F(DiskCacheEntryTest, MemoryOnlyBasicSparseIO) {
1355  SetMemoryOnlyMode();
1356  InitCache();
1357  BasicSparseIO();
1358}
1359
1360void DiskCacheEntryTest::HugeSparseIO() {
1361  std::string key("the first key");
1362  disk_cache::Entry* entry;
1363  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1364
1365  // Write 1.2 MB so that we cover multiple entries.
1366  const int kSize = 1200 * 1024;
1367  scoped_refptr<net::IOBuffer> buf_1(new net::IOBuffer(kSize));
1368  scoped_refptr<net::IOBuffer> buf_2(new net::IOBuffer(kSize));
1369  CacheTestFillBuffer(buf_1->data(), kSize, false);
1370
1371  // Write at offset 0x20F0000 (33 MB - 64 KB).
1372  VerifySparseIO(entry, 0x20F0000, buf_1, kSize, buf_2);
1373  entry->Close();
1374
1375  // Check it again.
1376  ASSERT_EQ(net::OK, OpenEntry(key, &entry));
1377  VerifyContentSparseIO(entry, 0x20F0000, buf_1->data(), kSize);
1378  entry->Close();
1379}
1380
1381TEST_F(DiskCacheEntryTest, HugeSparseIO) {
1382  InitCache();
1383  HugeSparseIO();
1384}
1385
1386TEST_F(DiskCacheEntryTest, MemoryOnlyHugeSparseIO) {
1387  SetMemoryOnlyMode();
1388  InitCache();
1389  HugeSparseIO();
1390}
1391
1392void DiskCacheEntryTest::GetAvailableRange() {
1393  std::string key("the first key");
1394  disk_cache::Entry* entry;
1395  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1396
1397  const int kSize = 16 * 1024;
1398  scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize));
1399  CacheTestFillBuffer(buf->data(), kSize, false);
1400
1401  // Write at offset 0x20F0000 (33 MB - 64 KB), and 0x20F4400 (33 MB - 47 KB).
1402  EXPECT_EQ(kSize, WriteSparseData(entry, 0x20F0000, buf, kSize));
1403  EXPECT_EQ(kSize, WriteSparseData(entry, 0x20F4400, buf, kSize));
1404
1405  // We stop at the first empty block.
1406  int64 start;
1407  TestCompletionCallback cb;
1408  int rv = entry->GetAvailableRange(0x20F0000, kSize * 2, &start, &cb);
1409  EXPECT_EQ(kSize, cb.GetResult(rv));
1410  EXPECT_EQ(0x20F0000, start);
1411
1412  start = 0;
1413  rv = entry->GetAvailableRange(0, kSize, &start, &cb);
1414  EXPECT_EQ(0, cb.GetResult(rv));
1415  rv = entry->GetAvailableRange(0x20F0000 - kSize, kSize, &start, &cb);
1416  EXPECT_EQ(0, cb.GetResult(rv));
1417  rv = entry->GetAvailableRange(0, 0x2100000, &start, &cb);
1418  EXPECT_EQ(kSize, cb.GetResult(rv));
1419  EXPECT_EQ(0x20F0000, start);
1420
1421  // We should be able to Read based on the results of GetAvailableRange.
1422  start = -1;
1423  rv = entry->GetAvailableRange(0x2100000, kSize, &start, &cb);
1424  EXPECT_EQ(0, cb.GetResult(rv));
1425  rv = entry->ReadSparseData(start, buf, kSize, &cb);
1426  EXPECT_EQ(0, cb.GetResult(rv));
1427
1428  start = 0;
1429  rv = entry->GetAvailableRange(0x20F2000, kSize, &start, &cb);
1430  EXPECT_EQ(0x2000, cb.GetResult(rv));
1431  EXPECT_EQ(0x20F2000, start);
1432  EXPECT_EQ(0x2000, ReadSparseData(entry, start, buf, kSize));
1433
1434  // Make sure that we respect the |len| argument.
1435  start = 0;
1436  rv = entry->GetAvailableRange(0x20F0001 - kSize, kSize, &start, &cb);
1437  EXPECT_EQ(1, cb.GetResult(rv));
1438  EXPECT_EQ(0x20F0000, start);
1439
1440  entry->Close();
1441}
1442
1443TEST_F(DiskCacheEntryTest, GetAvailableRange) {
1444  InitCache();
1445  GetAvailableRange();
1446}
1447
1448TEST_F(DiskCacheEntryTest, MemoryOnlyGetAvailableRange) {
1449  SetMemoryOnlyMode();
1450  InitCache();
1451  GetAvailableRange();
1452}
1453
1454void DiskCacheEntryTest::CouldBeSparse() {
1455  std::string key("the first key");
1456  disk_cache::Entry* entry;
1457  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1458
1459  const int kSize = 16 * 1024;
1460  scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize));
1461  CacheTestFillBuffer(buf->data(), kSize, false);
1462
1463  // Write at offset 0x20F0000 (33 MB - 64 KB).
1464  EXPECT_EQ(kSize, WriteSparseData(entry, 0x20F0000, buf, kSize));
1465
1466  EXPECT_TRUE(entry->CouldBeSparse());
1467  entry->Close();
1468
1469  ASSERT_EQ(net::OK, OpenEntry(key, &entry));
1470  EXPECT_TRUE(entry->CouldBeSparse());
1471  entry->Close();
1472
1473  // Now verify a regular entry.
1474  key.assign("another key");
1475  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1476  EXPECT_FALSE(entry->CouldBeSparse());
1477
1478  EXPECT_EQ(kSize, WriteData(entry, 0, 0, buf, kSize, false));
1479  EXPECT_EQ(kSize, WriteData(entry, 1, 0, buf, kSize, false));
1480  EXPECT_EQ(kSize, WriteData(entry, 2, 0, buf, kSize, false));
1481
1482  EXPECT_FALSE(entry->CouldBeSparse());
1483  entry->Close();
1484
1485  ASSERT_EQ(net::OK, OpenEntry(key, &entry));
1486  EXPECT_FALSE(entry->CouldBeSparse());
1487  entry->Close();
1488}
1489
1490TEST_F(DiskCacheEntryTest, CouldBeSparse) {
1491  InitCache();
1492  CouldBeSparse();
1493}
1494
1495TEST_F(DiskCacheEntryTest, MemoryCouldBeSparse) {
1496  SetMemoryOnlyMode();
1497  InitCache();
1498  CouldBeSparse();
1499}
1500
1501TEST_F(DiskCacheEntryTest, MemoryOnlyMisalignedSparseIO) {
1502  SetMemoryOnlyMode();
1503  InitCache();
1504
1505  const int kSize = 8192;
1506  scoped_refptr<net::IOBuffer> buf_1(new net::IOBuffer(kSize));
1507  scoped_refptr<net::IOBuffer> buf_2(new net::IOBuffer(kSize));
1508  CacheTestFillBuffer(buf_1->data(), kSize, false);
1509
1510  std::string key("the first key");
1511  disk_cache::Entry* entry;
1512  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1513
1514  // This loop writes back to back starting from offset 0 and 9000.
1515  for (int i = 0; i < kSize; i += 1024) {
1516    scoped_refptr<net::WrappedIOBuffer> buf_3(
1517      new net::WrappedIOBuffer(buf_1->data() + i));
1518    VerifySparseIO(entry, i, buf_3, 1024, buf_2);
1519    VerifySparseIO(entry, 9000 + i, buf_3, 1024, buf_2);
1520  }
1521
1522  // Make sure we have data written.
1523  VerifyContentSparseIO(entry, 0, buf_1->data(), kSize);
1524  VerifyContentSparseIO(entry, 9000, buf_1->data(), kSize);
1525
1526  // This tests a large write that spans 3 entries from a misaligned offset.
1527  VerifySparseIO(entry, 20481, buf_1, 8192, buf_2);
1528
1529  entry->Close();
1530}
1531
1532TEST_F(DiskCacheEntryTest, MemoryOnlyMisalignedGetAvailableRange) {
1533  SetMemoryOnlyMode();
1534  InitCache();
1535
1536  const int kSize = 8192;
1537  scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize));
1538  CacheTestFillBuffer(buf->data(), kSize, false);
1539
1540  disk_cache::Entry* entry;
1541  std::string key("the first key");
1542  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1543
1544  // Writes in the middle of an entry.
1545  EXPECT_EQ(1024, entry->WriteSparseData(0, buf, 1024, NULL));
1546  EXPECT_EQ(1024, entry->WriteSparseData(5120, buf, 1024, NULL));
1547  EXPECT_EQ(1024, entry->WriteSparseData(10000, buf, 1024, NULL));
1548
1549  // Writes in the middle of an entry and spans 2 child entries.
1550  EXPECT_EQ(8192, entry->WriteSparseData(50000, buf, 8192, NULL));
1551
1552  int64 start;
1553  TestCompletionCallback cb;
1554  // Test that we stop at a discontinuous child at the second block.
1555  int rv = entry->GetAvailableRange(0, 10000, &start, &cb);
1556  EXPECT_EQ(1024, cb.GetResult(rv));
1557  EXPECT_EQ(0, start);
1558
1559  // Test that number of bytes is reported correctly when we start from the
1560  // middle of a filled region.
1561  rv = entry->GetAvailableRange(512, 10000, &start, &cb);
1562  EXPECT_EQ(512, cb.GetResult(rv));
1563  EXPECT_EQ(512, start);
1564
1565  // Test that we found bytes in the child of next block.
1566  rv = entry->GetAvailableRange(1024, 10000, &start, &cb);
1567  EXPECT_EQ(1024, cb.GetResult(rv));
1568  EXPECT_EQ(5120, start);
1569
1570  // Test that the desired length is respected. It starts within a filled
1571  // region.
1572  rv = entry->GetAvailableRange(5500, 512, &start, &cb);
1573  EXPECT_EQ(512, cb.GetResult(rv));
1574  EXPECT_EQ(5500, start);
1575
1576  // Test that the desired length is respected. It starts before a filled
1577  // region.
1578  rv = entry->GetAvailableRange(5000, 620, &start, &cb);
1579  EXPECT_EQ(500, cb.GetResult(rv));
1580  EXPECT_EQ(5120, start);
1581
1582  // Test that multiple blocks are scanned.
1583  rv = entry->GetAvailableRange(40000, 20000, &start, &cb);
1584  EXPECT_EQ(8192, cb.GetResult(rv));
1585  EXPECT_EQ(50000, start);
1586
1587  entry->Close();
1588}
1589
1590void DiskCacheEntryTest::UpdateSparseEntry() {
1591  std::string key("the first key");
1592  disk_cache::Entry* entry1;
1593  ASSERT_EQ(net::OK, CreateEntry(key, &entry1));
1594
1595  const int kSize = 2048;
1596  scoped_refptr<net::IOBuffer> buf_1(new net::IOBuffer(kSize));
1597  scoped_refptr<net::IOBuffer> buf_2(new net::IOBuffer(kSize));
1598  CacheTestFillBuffer(buf_1->data(), kSize, false);
1599
1600  // Write at offset 0.
1601  VerifySparseIO(entry1, 0, buf_1, kSize, buf_2);
1602  entry1->Close();
1603
1604  // Write at offset 2048.
1605  ASSERT_EQ(net::OK, OpenEntry(key, &entry1));
1606  VerifySparseIO(entry1, 2048, buf_1, kSize, buf_2);
1607
1608  disk_cache::Entry* entry2;
1609  ASSERT_EQ(net::OK, CreateEntry("the second key", &entry2));
1610
1611  entry1->Close();
1612  entry2->Close();
1613  FlushQueueForTest();
1614  if (memory_only_)
1615    EXPECT_EQ(2, cache_->GetEntryCount());
1616  else
1617    EXPECT_EQ(3, cache_->GetEntryCount());
1618}
1619
1620TEST_F(DiskCacheEntryTest, UpdateSparseEntry) {
1621  SetDirectMode();
1622  SetCacheType(net::MEDIA_CACHE);
1623  InitCache();
1624  UpdateSparseEntry();
1625}
1626
1627TEST_F(DiskCacheEntryTest, MemoryOnlyUpdateSparseEntry) {
1628  SetMemoryOnlyMode();
1629  SetCacheType(net::MEDIA_CACHE);
1630  InitCache();
1631  UpdateSparseEntry();
1632}
1633
1634void DiskCacheEntryTest::DoomSparseEntry() {
1635  std::string key1("the first key");
1636  std::string key2("the second key");
1637  disk_cache::Entry *entry1, *entry2;
1638  ASSERT_EQ(net::OK, CreateEntry(key1, &entry1));
1639  ASSERT_EQ(net::OK, CreateEntry(key2, &entry2));
1640
1641  const int kSize = 4 * 1024;
1642  scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize));
1643  CacheTestFillBuffer(buf->data(), kSize, false);
1644
1645  int64 offset = 1024;
1646  // Write to a bunch of ranges.
1647  for (int i = 0; i < 12; i++) {
1648    EXPECT_EQ(kSize, entry1->WriteSparseData(offset, buf, kSize, NULL));
1649    // Keep the second map under the default size.
1650    if (i < 9)
1651      EXPECT_EQ(kSize, entry2->WriteSparseData(offset, buf, kSize, NULL));
1652    offset *= 4;
1653  }
1654
1655  if (memory_only_)
1656    EXPECT_EQ(2, cache_->GetEntryCount());
1657  else
1658    EXPECT_EQ(15, cache_->GetEntryCount());
1659
1660  // Doom the first entry while it's still open.
1661  entry1->Doom();
1662  entry1->Close();
1663  entry2->Close();
1664
1665  // Doom the second entry after it's fully saved.
1666  EXPECT_EQ(net::OK, DoomEntry(key2));
1667
1668  // Make sure we do all needed work. This may fail for entry2 if between Close
1669  // and DoomEntry the system decides to remove all traces of the file from the
1670  // system cache so we don't see that there is pending IO.
1671  MessageLoop::current()->RunAllPending();
1672
1673  if (memory_only_) {
1674    EXPECT_EQ(0, cache_->GetEntryCount());
1675  } else {
1676    if (5 == cache_->GetEntryCount()) {
1677      // Most likely we are waiting for the result of reading the sparse info
1678      // (it's always async on Posix so it is easy to miss). Unfortunately we
1679      // don't have any signal to watch for so we can only wait.
1680      base::PlatformThread::Sleep(500);
1681      MessageLoop::current()->RunAllPending();
1682    }
1683    EXPECT_EQ(0, cache_->GetEntryCount());
1684  }
1685}
1686
1687TEST_F(DiskCacheEntryTest, DoomSparseEntry) {
1688  SetDirectMode();
1689  UseCurrentThread();
1690  InitCache();
1691  DoomSparseEntry();
1692}
1693
1694TEST_F(DiskCacheEntryTest, MemoryOnlyDoomSparseEntry) {
1695  SetMemoryOnlyMode();
1696  InitCache();
1697  DoomSparseEntry();
1698}
1699
1700// A CompletionCallback that deletes the cache from within the callback. The way
1701// a TestCompletionCallback works means that all tasks (even new ones) are
1702// executed by the message loop before returning to the caller so the only way
1703// to simulate a race is to execute what we want on the callback.
1704class SparseTestCompletionCallback : public TestCompletionCallback {
1705 public:
1706  explicit SparseTestCompletionCallback(disk_cache::Backend* cache)
1707      : cache_(cache) {}
1708
1709  virtual void RunWithParams(const Tuple1<int>& params) {
1710    delete cache_;
1711    TestCompletionCallback::RunWithParams(params);
1712  }
1713 private:
1714  disk_cache::Backend* cache_;
1715  DISALLOW_COPY_AND_ASSIGN(SparseTestCompletionCallback);
1716};
1717
1718// Tests that we don't crash when the backend is deleted while we are working
1719// deleting the sub-entries of a sparse entry.
1720TEST_F(DiskCacheEntryTest, DoomSparseEntry2) {
1721  SetDirectMode();
1722  UseCurrentThread();
1723  InitCache();
1724  std::string key("the key");
1725  disk_cache::Entry* entry;
1726  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1727
1728  const int kSize = 4 * 1024;
1729  scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize));
1730  CacheTestFillBuffer(buf->data(), kSize, false);
1731
1732  int64 offset = 1024;
1733  // Write to a bunch of ranges.
1734  for (int i = 0; i < 12; i++) {
1735    EXPECT_EQ(kSize, entry->WriteSparseData(offset, buf, kSize, NULL));
1736    offset *= 4;
1737  }
1738  EXPECT_EQ(9, cache_->GetEntryCount());
1739
1740  entry->Close();
1741  SparseTestCompletionCallback cb(cache_);
1742  int rv = cache_->DoomEntry(key, &cb);
1743  EXPECT_EQ(net::ERR_IO_PENDING, rv);
1744  EXPECT_EQ(net::OK, cb.WaitForResult());
1745
1746  // TearDown will attempt to delete the cache_.
1747  cache_ = NULL;
1748}
1749
1750void DiskCacheEntryTest::PartialSparseEntry() {
1751  std::string key("the first key");
1752  disk_cache::Entry* entry;
1753  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1754
1755  // We should be able to deal with IO that is not aligned to the block size
1756  // of a sparse entry, at least to write a big range without leaving holes.
1757  const int kSize = 4 * 1024;
1758  const int kSmallSize = 128;
1759  scoped_refptr<net::IOBuffer> buf1(new net::IOBuffer(kSize));
1760  CacheTestFillBuffer(buf1->data(), kSize, false);
1761
1762  // The first write is just to extend the entry. The third write occupies
1763  // a 1KB block partially, it may not be written internally depending on the
1764  // implementation.
1765  EXPECT_EQ(kSize, WriteSparseData(entry, 20000, buf1, kSize));
1766  EXPECT_EQ(kSize, WriteSparseData(entry, 500, buf1, kSize));
1767  EXPECT_EQ(kSmallSize, WriteSparseData(entry, 1080321, buf1, kSmallSize));
1768  entry->Close();
1769  ASSERT_EQ(net::OK, OpenEntry(key, &entry));
1770
1771  scoped_refptr<net::IOBuffer> buf2(new net::IOBuffer(kSize));
1772  memset(buf2->data(), 0, kSize);
1773  EXPECT_EQ(0, ReadSparseData(entry, 8000, buf2, kSize));
1774
1775  EXPECT_EQ(500, ReadSparseData(entry, kSize, buf2, kSize));
1776  EXPECT_EQ(0, memcmp(buf2->data(), buf1->data() + kSize - 500, 500));
1777  EXPECT_EQ(0, ReadSparseData(entry, 0, buf2, kSize));
1778
1779  // This read should not change anything.
1780  EXPECT_EQ(96, ReadSparseData(entry, 24000, buf2, kSize));
1781  EXPECT_EQ(500, ReadSparseData(entry, kSize, buf2, kSize));
1782  EXPECT_EQ(0, ReadSparseData(entry, 99, buf2, kSize));
1783
1784  int rv;
1785  int64 start;
1786  TestCompletionCallback cb;
1787  if (memory_only_) {
1788    rv = entry->GetAvailableRange(0, 600, &start, &cb);
1789    EXPECT_EQ(100, cb.GetResult(rv));
1790    EXPECT_EQ(500, start);
1791  } else {
1792    rv = entry->GetAvailableRange(0, 2048, &start, &cb);
1793    EXPECT_EQ(1024, cb.GetResult(rv));
1794    EXPECT_EQ(1024, start);
1795  }
1796  rv = entry->GetAvailableRange(kSize, kSize, &start, &cb);
1797  EXPECT_EQ(500, cb.GetResult(rv));
1798  EXPECT_EQ(kSize, start);
1799  rv = entry->GetAvailableRange(20 * 1024, 10000, &start, &cb);
1800  EXPECT_EQ(3616, cb.GetResult(rv));
1801  EXPECT_EQ(20 * 1024, start);
1802
1803  // 1. Query before a filled 1KB block.
1804  // 2. Query within a filled 1KB block.
1805  // 3. Query beyond a filled 1KB block.
1806  if (memory_only_) {
1807    rv = entry->GetAvailableRange(19400, kSize, &start, &cb);
1808    EXPECT_EQ(3496, cb.GetResult(rv));
1809    EXPECT_EQ(20000, start);
1810  } else {
1811    rv = entry->GetAvailableRange(19400, kSize, &start, &cb);
1812    EXPECT_EQ(3016, cb.GetResult(rv));
1813    EXPECT_EQ(20480, start);
1814  }
1815  rv = entry->GetAvailableRange(3073, kSize, &start, &cb);
1816  EXPECT_EQ(1523, cb.GetResult(rv));
1817  EXPECT_EQ(3073, start);
1818  rv = entry->GetAvailableRange(4600, kSize, &start, &cb);
1819  EXPECT_EQ(0, cb.GetResult(rv));
1820  EXPECT_EQ(4600, start);
1821
1822  // Now make another write and verify that there is no hole in between.
1823  EXPECT_EQ(kSize, WriteSparseData(entry, 500 + kSize, buf1, kSize));
1824  rv = entry->GetAvailableRange(1024, 10000, &start, &cb);
1825  EXPECT_EQ(7 * 1024 + 500, cb.GetResult(rv));
1826  EXPECT_EQ(1024, start);
1827  EXPECT_EQ(kSize, ReadSparseData(entry, kSize, buf2, kSize));
1828  EXPECT_EQ(0, memcmp(buf2->data(), buf1->data() + kSize - 500, 500));
1829  EXPECT_EQ(0, memcmp(buf2->data() + 500, buf1->data(), kSize - 500));
1830
1831  entry->Close();
1832}
1833
1834TEST_F(DiskCacheEntryTest, PartialSparseEntry) {
1835  InitCache();
1836  PartialSparseEntry();
1837}
1838
1839TEST_F(DiskCacheEntryTest, MemoryPartialSparseEntry) {
1840  SetMemoryOnlyMode();
1841  InitCache();
1842  PartialSparseEntry();
1843}
1844
1845// Tests that corrupt sparse children are removed automatically.
1846TEST_F(DiskCacheEntryTest, CleanupSparseEntry) {
1847  InitCache();
1848  std::string key("the first key");
1849  disk_cache::Entry* entry;
1850  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1851
1852  const int kSize = 4 * 1024;
1853  scoped_refptr<net::IOBuffer> buf1(new net::IOBuffer(kSize));
1854  CacheTestFillBuffer(buf1->data(), kSize, false);
1855
1856  const int k1Meg = 1024 * 1024;
1857  EXPECT_EQ(kSize, WriteSparseData(entry, 8192, buf1, kSize));
1858  EXPECT_EQ(kSize, WriteSparseData(entry, k1Meg + 8192, buf1, kSize));
1859  EXPECT_EQ(kSize, WriteSparseData(entry, 2 * k1Meg + 8192, buf1, kSize));
1860  entry->Close();
1861  EXPECT_EQ(4, cache_->GetEntryCount());
1862
1863  void* iter = NULL;
1864  int count = 0;
1865  std::string child_key[2];
1866  while (OpenNextEntry(&iter, &entry) == net::OK) {
1867    ASSERT_TRUE(entry != NULL);
1868    // Writing to an entry will alter the LRU list and invalidate the iterator.
1869    if (entry->GetKey() != key && count < 2)
1870      child_key[count++] = entry->GetKey();
1871    entry->Close();
1872  }
1873  for (int i = 0; i < 2; i++) {
1874    ASSERT_EQ(net::OK, OpenEntry(child_key[i], &entry));
1875    // Overwrite the header's magic and signature.
1876    EXPECT_EQ(12, WriteData(entry, 2, 0, buf1, 12, false));
1877    entry->Close();
1878  }
1879
1880  EXPECT_EQ(4, cache_->GetEntryCount());
1881  ASSERT_EQ(net::OK, OpenEntry(key, &entry));
1882
1883  // Two children should be gone. One while reading and one while writing.
1884  EXPECT_EQ(0, ReadSparseData(entry, 2 * k1Meg + 8192, buf1, kSize));
1885  EXPECT_EQ(kSize, WriteSparseData(entry, k1Meg + 16384, buf1, kSize));
1886  EXPECT_EQ(0, ReadSparseData(entry, k1Meg + 8192, buf1, kSize));
1887
1888  // We never touched this one.
1889  EXPECT_EQ(kSize, ReadSparseData(entry, 8192, buf1, kSize));
1890  entry->Close();
1891
1892  // We re-created one of the corrupt children.
1893  EXPECT_EQ(3, cache_->GetEntryCount());
1894}
1895
1896TEST_F(DiskCacheEntryTest, CancelSparseIO) {
1897  UseCurrentThread();
1898  InitCache();
1899  std::string key("the first key");
1900  disk_cache::Entry* entry;
1901  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1902
1903  const int kSize = 40 * 1024;
1904  scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize));
1905  CacheTestFillBuffer(buf->data(), kSize, false);
1906
1907  // This will open and write two "real" entries.
1908  TestCompletionCallback cb1, cb2, cb3, cb4, cb5;
1909  int rv = entry->WriteSparseData(1024 * 1024 - 4096, buf, kSize, &cb1);
1910  EXPECT_EQ(net::ERR_IO_PENDING, rv);
1911
1912  int64 offset = 0;
1913  rv = entry->GetAvailableRange(offset, kSize, &offset, &cb5);
1914  rv = cb5.GetResult(rv);
1915  if (!cb1.have_result()) {
1916    // We may or may not have finished writing to the entry. If we have not,
1917    // we cannot start another operation at this time.
1918    EXPECT_EQ(net::ERR_CACHE_OPERATION_NOT_SUPPORTED, rv);
1919  }
1920
1921  // We cancel the pending operation, and register multiple notifications.
1922  entry->CancelSparseIO();
1923  EXPECT_EQ(net::ERR_IO_PENDING, entry->ReadyForSparseIO(&cb2));
1924  EXPECT_EQ(net::ERR_IO_PENDING, entry->ReadyForSparseIO(&cb3));
1925  entry->CancelSparseIO();  // Should be a no op at this point.
1926  EXPECT_EQ(net::ERR_IO_PENDING, entry->ReadyForSparseIO(&cb4));
1927
1928  if (!cb1.have_result()) {
1929    EXPECT_EQ(net::ERR_CACHE_OPERATION_NOT_SUPPORTED,
1930              entry->ReadSparseData(offset, buf, kSize, NULL));
1931    EXPECT_EQ(net::ERR_CACHE_OPERATION_NOT_SUPPORTED,
1932              entry->WriteSparseData(offset, buf, kSize, NULL));
1933  }
1934
1935  // Now see if we receive all notifications. Note that we should not be able
1936  // to write everything (unless the timing of the system is really weird).
1937  rv = cb1.WaitForResult();
1938  EXPECT_TRUE(rv == 4096 || rv == kSize);
1939  EXPECT_EQ(net::OK, cb2.WaitForResult());
1940  EXPECT_EQ(net::OK, cb3.WaitForResult());
1941  EXPECT_EQ(net::OK, cb4.WaitForResult());
1942
1943  rv = entry->GetAvailableRange(offset, kSize, &offset, &cb5);
1944  EXPECT_EQ(0, cb5.GetResult(rv));
1945  entry->Close();
1946}
1947
1948// Tests that we perform sanity checks on an entry's key. Note that there are
1949// other tests that exercise sanity checks by using saved corrupt files.
1950TEST_F(DiskCacheEntryTest, KeySanityCheck) {
1951  UseCurrentThread();
1952  InitCache();
1953  std::string key("the first key");
1954  disk_cache::Entry* entry;
1955  ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1956
1957  disk_cache::EntryImpl* entry_impl =
1958      static_cast<disk_cache::EntryImpl*>(entry);
1959  disk_cache::EntryStore* store = entry_impl->entry()->Data();
1960
1961  // We have reserved space for a short key (one block), let's say that the key
1962  // takes more than one block, and remove the NULLs after the actual key.
1963  store->key_len = 800;
1964  memset(store->key + key.size(), 'k', sizeof(store->key) - key.size());
1965  entry_impl->entry()->set_modified();
1966  entry->Close();
1967
1968  // We have a corrupt entry. Now reload it. We should NOT read beyond the
1969  // allocated buffer here.
1970  ASSERT_NE(net::OK, OpenEntry(key, &entry));
1971  DisableIntegrityCheck();
1972}
1973