1// Copyright (c) 2011 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#ifndef NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_
6#define NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_
7#pragma once
8
9#include "base/basictypes.h"
10#include "base/threading/thread.h"
11#include "net/base/cache_type.h"
12#include "testing/gtest/include/gtest/gtest.h"
13#include "testing/platform_test.h"
14
15class FilePath;
16
17namespace net {
18
19class IOBuffer;
20
21}  // namespace net
22
23namespace disk_cache {
24
25class Backend;
26class BackendImpl;
27class Entry;
28class MemBackendImpl;
29
30}  // namespace disk_cache
31
32// These tests can use the path service, which uses autoreleased objects on the
33// Mac, so this needs to be a PlatformTest.  Even tests that do not require a
34// cache (and that do not need to be a DiskCacheTestWithCache) are susceptible
35// to this problem; all such tests should use TEST_F(DiskCacheTest, ...).
36class DiskCacheTest : public PlatformTest {
37  virtual void TearDown();
38};
39
40// Provides basic support for cache related tests.
41class DiskCacheTestWithCache : public DiskCacheTest {
42 protected:
43  DiskCacheTestWithCache();
44  virtual ~DiskCacheTestWithCache();
45
46  void InitCache();
47  void SimulateCrash();
48  void SetTestMode();
49
50  void SetMemoryOnlyMode() {
51    memory_only_ = true;
52  }
53
54  // Use the implementation directly instead of the factory provided object.
55  void SetDirectMode() {
56    implementation_ = true;
57  }
58
59  void SetMask(uint32 mask) {
60    mask_ = mask;
61  }
62
63  void SetMaxSize(int size);
64
65  // Deletes and re-creates the files on initialization errors.
66  void SetForceCreation() {
67    force_creation_ = true;
68  }
69
70  void SetNewEviction() {
71    new_eviction_ = true;
72  }
73
74  void DisableFirstCleanup() {
75    first_cleanup_ = false;
76  }
77
78  void DisableIntegrityCheck() {
79    integrity_ = false;
80  }
81
82  void UseCurrentThread() {
83    use_current_thread_ = true;
84  }
85
86  void SetCacheType(net::CacheType type) {
87    type_ = type;
88  }
89
90  // Utility methods to access the cache and wait for each operation to finish.
91  int OpenEntry(const std::string& key, disk_cache::Entry** entry);
92  int CreateEntry(const std::string& key, disk_cache::Entry** entry);
93  int DoomEntry(const std::string& key);
94  int DoomAllEntries();
95  int DoomEntriesBetween(const base::Time initial_time,
96                         const base::Time end_time);
97  int DoomEntriesSince(const base::Time initial_time);
98  int OpenNextEntry(void** iter, disk_cache::Entry** next_entry);
99  void FlushQueueForTest();
100  void RunTaskForTest(Task* task);
101  int ReadData(disk_cache::Entry* entry, int index, int offset,
102               net::IOBuffer* buf, int len);
103  int WriteData(disk_cache::Entry* entry, int index, int offset,
104                net::IOBuffer* buf, int len, bool truncate);
105  int ReadSparseData(disk_cache::Entry* entry, int64 offset, net::IOBuffer* buf,
106                     int len);
107  int WriteSparseData(disk_cache::Entry* entry, int64 offset,
108                      net::IOBuffer* buf, int len);
109
110  // Asks the cache to trim an entry. If |empty| is true, the whole cache is
111  // deleted.
112  void TrimForTest(bool empty);
113
114  // Asks the cache to trim an entry from the deleted list. If |empty| is
115  // true, the whole list is deleted.
116  void TrimDeletedListForTest(bool empty);
117
118  // DiskCacheTest:
119  virtual void TearDown();
120
121  // cache_ will always have a valid object, regardless of how the cache was
122  // initialized. The implementation pointers can be NULL.
123  disk_cache::Backend* cache_;
124  disk_cache::BackendImpl* cache_impl_;
125  disk_cache::MemBackendImpl* mem_cache_;
126
127  uint32 mask_;
128  int size_;
129  net::CacheType type_;
130  bool memory_only_;
131  bool implementation_;
132  bool force_creation_;
133  bool new_eviction_;
134  bool first_cleanup_;
135  bool integrity_;
136  bool use_current_thread_;
137  // This is intentionally left uninitialized, to be used by any test.
138  bool success_;
139
140 private:
141  void InitMemoryCache();
142  void InitDiskCache();
143  void InitDiskCacheImpl(const FilePath& path);
144
145  base::Thread cache_thread_;
146  DISALLOW_COPY_AND_ASSIGN(DiskCacheTestWithCache);
147};
148
149#endif  // NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_
150