• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
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 
8 #include "base/basictypes.h"
9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/threading/thread.h"
13 #include "net/base/cache_type.h"
14 #include "net/disk_cache/disk_cache.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/platform_test.h"
17 
18 namespace net {
19 
20 class IOBuffer;
21 
22 }  // namespace net
23 
24 namespace disk_cache {
25 
26 class Backend;
27 class BackendImpl;
28 class Entry;
29 class MemBackendImpl;
30 class SimpleBackendImpl;
31 
32 }  // namespace disk_cache
33 
34 // These tests can use the path service, which uses autoreleased objects on the
35 // Mac, so this needs to be a PlatformTest.  Even tests that do not require a
36 // cache (and that do not need to be a DiskCacheTestWithCache) are susceptible
37 // to this problem; all such tests should use TEST_F(DiskCacheTest, ...).
38 class DiskCacheTest : public PlatformTest {
39  protected:
40   DiskCacheTest();
41   virtual ~DiskCacheTest();
42 
43   // Copies a set of cache files from the data folder to the test folder.
44   bool CopyTestCache(const std::string& name);
45 
46   // Deletes the contents of |cache_path_|.
47   bool CleanupCacheDir();
48 
49   virtual void TearDown() OVERRIDE;
50 
51   base::FilePath cache_path_;
52 
53  private:
54   base::ScopedTempDir temp_dir_;
55   scoped_ptr<base::MessageLoop> message_loop_;
56 };
57 
58 // Provides basic support for cache related tests.
59 class DiskCacheTestWithCache : public DiskCacheTest {
60  protected:
61   class TestIterator {
62    public:
63     explicit TestIterator(scoped_ptr<disk_cache::Backend::Iterator> iterator);
64     ~TestIterator();
65 
66     int OpenNextEntry(disk_cache::Entry** next_entry);
67 
68    private:
69     scoped_ptr<disk_cache::Backend::Iterator> iterator_;
70   };
71 
72   DiskCacheTestWithCache();
73   virtual ~DiskCacheTestWithCache();
74 
75   void CreateBackend(uint32 flags, base::Thread* thread);
76 
77   void InitCache();
78   void SimulateCrash();
79   void SetTestMode();
80 
SetMemoryOnlyMode()81   void SetMemoryOnlyMode() {
82     memory_only_ = true;
83   }
84 
SetSimpleCacheMode()85   void SetSimpleCacheMode() {
86     simple_cache_mode_ = true;
87   }
88 
SetMask(uint32 mask)89   void SetMask(uint32 mask) {
90     mask_ = mask;
91   }
92 
93   void SetMaxSize(int size);
94 
95   // Deletes and re-creates the files on initialization errors.
SetForceCreation()96   void SetForceCreation() {
97     force_creation_ = true;
98   }
99 
SetNewEviction()100   void SetNewEviction() {
101     new_eviction_ = true;
102   }
103 
DisableSimpleCacheWaitForIndex()104   void DisableSimpleCacheWaitForIndex() {
105     simple_cache_wait_for_index_ = false;
106   }
107 
DisableFirstCleanup()108   void DisableFirstCleanup() {
109     first_cleanup_ = false;
110   }
111 
DisableIntegrityCheck()112   void DisableIntegrityCheck() {
113     integrity_ = false;
114   }
115 
UseCurrentThread()116   void UseCurrentThread() {
117     use_current_thread_ = true;
118   }
119 
SetCacheType(net::CacheType type)120   void SetCacheType(net::CacheType type) {
121     type_ = type;
122   }
123 
124   // Utility methods to access the cache and wait for each operation to finish.
125   int OpenEntry(const std::string& key, disk_cache::Entry** entry);
126   int CreateEntry(const std::string& key, disk_cache::Entry** entry);
127   int DoomEntry(const std::string& key);
128   int DoomAllEntries();
129   int DoomEntriesBetween(const base::Time initial_time,
130                          const base::Time end_time);
131   int DoomEntriesSince(const base::Time initial_time);
132   scoped_ptr<TestIterator> CreateIterator();
133   void FlushQueueForTest();
134   void RunTaskForTest(const base::Closure& closure);
135   int ReadData(disk_cache::Entry* entry, int index, int offset,
136                net::IOBuffer* buf, int len);
137   int WriteData(disk_cache::Entry* entry, int index, int offset,
138                 net::IOBuffer* buf, int len, bool truncate);
139   int ReadSparseData(disk_cache::Entry* entry, int64 offset, net::IOBuffer* buf,
140                      int len);
141   int WriteSparseData(disk_cache::Entry* entry, int64 offset,
142                       net::IOBuffer* buf, int len);
143 
144   // Asks the cache to trim an entry. If |empty| is true, the whole cache is
145   // deleted.
146   void TrimForTest(bool empty);
147 
148   // Asks the cache to trim an entry from the deleted list. If |empty| is
149   // true, the whole list is deleted.
150   void TrimDeletedListForTest(bool empty);
151 
152   // Makes sure that some time passes before continuing the test. Time::Now()
153   // before and after this method will not be the same.
154   void AddDelay();
155 
156   // DiskCacheTest:
157   virtual void TearDown() OVERRIDE;
158 
159   // cache_ will always have a valid object, regardless of how the cache was
160   // initialized. The implementation pointers can be NULL.
161   scoped_ptr<disk_cache::Backend> cache_;
162   disk_cache::BackendImpl* cache_impl_;
163   disk_cache::SimpleBackendImpl* simple_cache_impl_;
164   disk_cache::MemBackendImpl* mem_cache_;
165 
166   uint32 mask_;
167   int size_;
168   net::CacheType type_;
169   bool memory_only_;
170   bool simple_cache_mode_;
171   bool simple_cache_wait_for_index_;
172   bool force_creation_;
173   bool new_eviction_;
174   bool first_cleanup_;
175   bool integrity_;
176   bool use_current_thread_;
177   // This is intentionally left uninitialized, to be used by any test.
178   bool success_;
179 
180  private:
181   void InitMemoryCache();
182   void InitDiskCache();
183 
184   base::Thread cache_thread_;
185   DISALLOW_COPY_AND_ASSIGN(DiskCacheTestWithCache);
186 };
187 
188 #endif  // NET_DISK_CACHE_DISK_CACHE_TEST_BASE_H_
189