blob: 9759a2b3049855e263f510b20a0584d30b7ddec2 [file] [log] [blame]
[email protected]30f02c432011-05-10 22:27:061// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
[email protected]4d5e0362008-08-28 00:59:065#ifndef NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_
6#define NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
initial.commit586acc5fe2008-07-26 22:42:528
9#include "base/basictypes.h"
[email protected]79b3fec2011-11-15 22:33:4710#include "base/file_path.h"
11#include "base/memory/scoped_ptr.h"
[email protected]34b99632011-01-01 01:01:0612#include "base/threading/thread.h"
[email protected]67b09ec2010-08-27 17:49:4213#include "net/base/cache_type.h"
initial.commit586acc5fe2008-07-26 22:42:5214#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1515#include "testing/platform_test.h"
initial.commit586acc5fe2008-07-26 22:42:5216
[email protected]e1fcf142010-08-23 18:47:2517namespace net {
18
19class IOBuffer;
20
21} // namespace net
22
initial.commit586acc5fe2008-07-26 22:42:5223namespace disk_cache {
24
25class Backend;
26class BackendImpl;
[email protected]bc4fb8e2010-03-18 23:58:1727class Entry;
initial.commit586acc5fe2008-07-26 22:42:5228class MemBackendImpl;
29
[email protected]4d5e0362008-08-28 00:59:0630} // namespace disk_cache
initial.commit586acc5fe2008-07-26 22:42:5231
[email protected]17b89142008-11-07 21:52:1532// 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 {
[email protected]79b3fec2011-11-15 22:33:4737 protected:
38 DiskCacheTest();
39 virtual ~DiskCacheTest();
40
41 // Copies a set of cache files from the data folder to the test folder.
42 bool CopyTestCache(const std::string& name);
43
44 // Deletes the contents of |cache_path_|.
45 bool CleanupCacheDir();
46
[email protected]17b89142008-11-07 21:52:1547 virtual void TearDown();
[email protected]79b3fec2011-11-15 22:33:4748
49 FilePath cache_path_;
50
51 private:
52 scoped_ptr<MessageLoop> message_loop_;
[email protected]17b89142008-11-07 21:52:1553};
54
initial.commit586acc5fe2008-07-26 22:42:5255// Provides basic support for cache related tests.
[email protected]4d5e0362008-08-28 00:59:0656class DiskCacheTestWithCache : public DiskCacheTest {
initial.commit586acc5fe2008-07-26 22:42:5257 protected:
[email protected]7aefb152011-01-21 23:46:4958 DiskCacheTestWithCache();
59 virtual ~DiskCacheTestWithCache();
initial.commit586acc5fe2008-07-26 22:42:5260
61 void InitCache();
initial.commit586acc5fe2008-07-26 22:42:5262 void SimulateCrash();
[email protected]a9da16d2008-07-30 21:41:5463 void SetTestMode();
initial.commit586acc5fe2008-07-26 22:42:5264
65 void SetMemoryOnlyMode() {
66 memory_only_ = true;
67 }
68
69 // Use the implementation directly instead of the factory provided object.
70 void SetDirectMode() {
71 implementation_ = true;
72 }
73
74 void SetMask(uint32 mask) {
75 mask_ = mask;
76 }
77
78 void SetMaxSize(int size);
79
80 // Deletes and re-creates the files on initialization errors.
81 void SetForceCreation() {
82 force_creation_ = true;
83 }
84
[email protected]220d71002009-04-17 00:50:0485 void SetNewEviction() {
86 new_eviction_ = true;
87 }
88
[email protected]a9da16d2008-07-30 21:41:5489 void DisableFirstCleanup() {
90 first_cleanup_ = false;
91 }
92
[email protected]220d71002009-04-17 00:50:0493 void DisableIntegrityCheck() {
94 integrity_ = false;
95 }
96
[email protected]fb2622f2010-07-13 18:00:5697 void UseCurrentThread() {
98 use_current_thread_ = true;
99 }
100
[email protected]67b09ec2010-08-27 17:49:42101 void SetCacheType(net::CacheType type) {
102 type_ = type;
103 }
104
[email protected]bc4fb8e2010-03-18 23:58:17105 // Utility methods to access the cache and wait for each operation to finish.
106 int OpenEntry(const std::string& key, disk_cache::Entry** entry);
107 int CreateEntry(const std::string& key, disk_cache::Entry** entry);
108 int DoomEntry(const std::string& key);
109 int DoomAllEntries();
110 int DoomEntriesBetween(const base::Time initial_time,
111 const base::Time end_time);
112 int DoomEntriesSince(const base::Time initial_time);
113 int OpenNextEntry(void** iter, disk_cache::Entry** next_entry);
[email protected]fb2622f2010-07-13 18:00:56114 void FlushQueueForTest();
[email protected]65188eb2010-09-16 20:59:29115 void RunTaskForTest(Task* task);
[email protected]e1fcf142010-08-23 18:47:25116 int ReadData(disk_cache::Entry* entry, int index, int offset,
[email protected]67b09ec2010-08-27 17:49:42117 net::IOBuffer* buf, int len);
[email protected]e1fcf142010-08-23 18:47:25118 int WriteData(disk_cache::Entry* entry, int index, int offset,
119 net::IOBuffer* buf, int len, bool truncate);
120 int ReadSparseData(disk_cache::Entry* entry, int64 offset, net::IOBuffer* buf,
121 int len);
122 int WriteSparseData(disk_cache::Entry* entry, int64 offset,
123 net::IOBuffer* buf, int len);
[email protected]bc4fb8e2010-03-18 23:58:17124
[email protected]30f02c432011-05-10 22:27:06125 // Asks the cache to trim an entry. If |empty| is true, the whole cache is
[email protected]ceb61da32011-01-25 23:52:02126 // deleted.
127 void TrimForTest(bool empty);
128
[email protected]30f02c432011-05-10 22:27:06129 // Asks the cache to trim an entry from the deleted list. If |empty| is
130 // true, the whole list is deleted.
[email protected]ceb61da32011-01-25 23:52:02131 void TrimDeletedListForTest(bool empty);
132
[email protected]7aefb152011-01-21 23:46:49133 // DiskCacheTest:
134 virtual void TearDown();
135
initial.commit586acc5fe2008-07-26 22:42:52136 // cache_ will always have a valid object, regardless of how the cache was
137 // initialized. The implementation pointers can be NULL.
138 disk_cache::Backend* cache_;
139 disk_cache::BackendImpl* cache_impl_;
140 disk_cache::MemBackendImpl* mem_cache_;
141
142 uint32 mask_;
143 int size_;
[email protected]67b09ec2010-08-27 17:49:42144 net::CacheType type_;
initial.commit586acc5fe2008-07-26 22:42:52145 bool memory_only_;
146 bool implementation_;
147 bool force_creation_;
[email protected]220d71002009-04-17 00:50:04148 bool new_eviction_;
[email protected]a9da16d2008-07-30 21:41:54149 bool first_cleanup_;
[email protected]220d71002009-04-17 00:50:04150 bool integrity_;
[email protected]fb2622f2010-07-13 18:00:56151 bool use_current_thread_;
[email protected]220d71002009-04-17 00:50:04152 // This is intentionally left uninitialized, to be used by any test.
153 bool success_;
initial.commit586acc5fe2008-07-26 22:42:52154
155 private:
156 void InitMemoryCache();
157 void InitDiskCache();
[email protected]79b3fec2011-11-15 22:33:47158 void InitDiskCacheImpl();
[email protected]ec44a9a2010-06-15 19:31:51159
160 base::Thread cache_thread_;
161 DISALLOW_COPY_AND_ASSIGN(DiskCacheTestWithCache);
initial.commit586acc5fe2008-07-26 22:42:52162};
163
[email protected]4d5e0362008-08-28 00:59:06164#endif // NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_