blob: ef6306b57c6e44b5e1ebcd15415bc32eaf5c9fc4 [file] [log] [blame]
[email protected]05f8087d2012-06-29 18:58:371// Copyright (c) 2012 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_
initial.commit586acc5fe2008-07-26 22:42:527
8#include "base/basictypes.h"
[email protected]57999812013-02-24 05:40:529#include "base/files/file_path.h"
[email protected]ea1a3f62012-11-16 20:34:2310#include "base/files/scoped_temp_dir.h"
[email protected]79b3fec2011-11-15 22:33:4711#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;
[email protected]1ed95752013-04-23 00:12:3629class SimpleBackendImpl;
initial.commit586acc5fe2008-07-26 22:42:5230
[email protected]4d5e0362008-08-28 00:59:0631} // namespace disk_cache
initial.commit586acc5fe2008-07-26 22:42:5232
[email protected]17b89142008-11-07 21:52:1533// These tests can use the path service, which uses autoreleased objects on the
34// Mac, so this needs to be a PlatformTest. Even tests that do not require a
35// cache (and that do not need to be a DiskCacheTestWithCache) are susceptible
36// to this problem; all such tests should use TEST_F(DiskCacheTest, ...).
37class DiskCacheTest : public PlatformTest {
[email protected]79b3fec2011-11-15 22:33:4738 protected:
39 DiskCacheTest();
40 virtual ~DiskCacheTest();
41
42 // Copies a set of cache files from the data folder to the test folder.
43 bool CopyTestCache(const std::string& name);
44
45 // Deletes the contents of |cache_path_|.
46 bool CleanupCacheDir();
47
[email protected]f2cbbc82011-11-16 01:10:2948 virtual void TearDown() OVERRIDE;
[email protected]79b3fec2011-11-15 22:33:4749
[email protected]6cdfd7f2013-02-08 20:40:1550 base::FilePath cache_path_;
[email protected]79b3fec2011-11-15 22:33:4751
52 private:
[email protected]ea1a3f62012-11-16 20:34:2353 base::ScopedTempDir temp_dir_;
[email protected]79b3fec2011-11-15 22:33:4754 scoped_ptr<MessageLoop> message_loop_;
[email protected]17b89142008-11-07 21:52:1555};
56
initial.commit586acc5fe2008-07-26 22:42:5257// Provides basic support for cache related tests.
[email protected]4d5e0362008-08-28 00:59:0658class DiskCacheTestWithCache : public DiskCacheTest {
initial.commit586acc5fe2008-07-26 22:42:5259 protected:
[email protected]7aefb152011-01-21 23:46:4960 DiskCacheTestWithCache();
61 virtual ~DiskCacheTestWithCache();
initial.commit586acc5fe2008-07-26 22:42:5262
[email protected]398ad132013-04-02 15:11:0163 void CreateBackend(uint32 flags, base::Thread* thread);
64
initial.commit586acc5fe2008-07-26 22:42:5265 void InitCache();
initial.commit586acc5fe2008-07-26 22:42:5266 void SimulateCrash();
[email protected]a9da16d2008-07-30 21:41:5467 void SetTestMode();
initial.commit586acc5fe2008-07-26 22:42:5268
69 void SetMemoryOnlyMode() {
70 memory_only_ = true;
71 }
72
[email protected]398ad132013-04-02 15:11:0173 void SetSimpleCacheMode() {
74 simple_cache_mode_ = true;
initial.commit586acc5fe2008-07-26 22:42:5275 }
76
77 void SetMask(uint32 mask) {
78 mask_ = mask;
79 }
80
81 void SetMaxSize(int size);
82
83 // Deletes and re-creates the files on initialization errors.
84 void SetForceCreation() {
85 force_creation_ = true;
86 }
87
[email protected]220d71002009-04-17 00:50:0488 void SetNewEviction() {
89 new_eviction_ = true;
90 }
91
[email protected]a9da16d2008-07-30 21:41:5492 void DisableFirstCleanup() {
93 first_cleanup_ = false;
94 }
95
[email protected]220d71002009-04-17 00:50:0496 void DisableIntegrityCheck() {
97 integrity_ = false;
98 }
99
[email protected]fb2622f2010-07-13 18:00:56100 void UseCurrentThread() {
101 use_current_thread_ = true;
102 }
103
[email protected]67b09ec2010-08-27 17:49:42104 void SetCacheType(net::CacheType type) {
105 type_ = type;
106 }
107
[email protected]bc4fb8e2010-03-18 23:58:17108 // Utility methods to access the cache and wait for each operation to finish.
109 int OpenEntry(const std::string& key, disk_cache::Entry** entry);
110 int CreateEntry(const std::string& key, disk_cache::Entry** entry);
111 int DoomEntry(const std::string& key);
112 int DoomAllEntries();
113 int DoomEntriesBetween(const base::Time initial_time,
114 const base::Time end_time);
115 int DoomEntriesSince(const base::Time initial_time);
116 int OpenNextEntry(void** iter, disk_cache::Entry** next_entry);
[email protected]fb2622f2010-07-13 18:00:56117 void FlushQueueForTest();
[email protected]f27bbe002011-12-22 11:29:34118 void RunTaskForTest(const base::Closure& closure);
[email protected]e1fcf142010-08-23 18:47:25119 int ReadData(disk_cache::Entry* entry, int index, int offset,
[email protected]67b09ec2010-08-27 17:49:42120 net::IOBuffer* buf, int len);
[email protected]e1fcf142010-08-23 18:47:25121 int WriteData(disk_cache::Entry* entry, int index, int offset,
122 net::IOBuffer* buf, int len, bool truncate);
123 int ReadSparseData(disk_cache::Entry* entry, int64 offset, net::IOBuffer* buf,
124 int len);
125 int WriteSparseData(disk_cache::Entry* entry, int64 offset,
126 net::IOBuffer* buf, int len);
[email protected]bc4fb8e2010-03-18 23:58:17127
[email protected]30f02c432011-05-10 22:27:06128 // Asks the cache to trim an entry. If |empty| is true, the whole cache is
[email protected]ceb61da32011-01-25 23:52:02129 // deleted.
130 void TrimForTest(bool empty);
131
[email protected]30f02c432011-05-10 22:27:06132 // Asks the cache to trim an entry from the deleted list. If |empty| is
133 // true, the whole list is deleted.
[email protected]ceb61da32011-01-25 23:52:02134 void TrimDeletedListForTest(bool empty);
135
[email protected]05f8087d2012-06-29 18:58:37136 // Makes sure that some time passes before continuing the test. Time::Now()
137 // before and after this method will not be the same.
138 void AddDelay();
139
[email protected]7aefb152011-01-21 23:46:49140 // DiskCacheTest:
[email protected]f2cbbc82011-11-16 01:10:29141 virtual void TearDown() OVERRIDE;
[email protected]7aefb152011-01-21 23:46:49142
initial.commit586acc5fe2008-07-26 22:42:52143 // cache_ will always have a valid object, regardless of how the cache was
144 // initialized. The implementation pointers can be NULL.
145 disk_cache::Backend* cache_;
146 disk_cache::BackendImpl* cache_impl_;
[email protected]1ed95752013-04-23 00:12:36147 disk_cache::SimpleBackendImpl* simple_cache_impl_;
initial.commit586acc5fe2008-07-26 22:42:52148 disk_cache::MemBackendImpl* mem_cache_;
149
150 uint32 mask_;
151 int size_;
[email protected]67b09ec2010-08-27 17:49:42152 net::CacheType type_;
initial.commit586acc5fe2008-07-26 22:42:52153 bool memory_only_;
[email protected]398ad132013-04-02 15:11:01154 bool simple_cache_mode_;
initial.commit586acc5fe2008-07-26 22:42:52155 bool force_creation_;
[email protected]220d71002009-04-17 00:50:04156 bool new_eviction_;
[email protected]a9da16d2008-07-30 21:41:54157 bool first_cleanup_;
[email protected]220d71002009-04-17 00:50:04158 bool integrity_;
[email protected]fb2622f2010-07-13 18:00:56159 bool use_current_thread_;
[email protected]220d71002009-04-17 00:50:04160 // This is intentionally left uninitialized, to be used by any test.
161 bool success_;
initial.commit586acc5fe2008-07-26 22:42:52162
163 private:
164 void InitMemoryCache();
165 void InitDiskCache();
[email protected]ec44a9a2010-06-15 19:31:51166
167 base::Thread cache_thread_;
168 DISALLOW_COPY_AND_ASSIGN(DiskCacheTestWithCache);
initial.commit586acc5fe2008-07-26 22:42:52169};
170
[email protected]4d5e0362008-08-28 00:59:06171#endif // NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_