1 // Copyright 2014 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 CONTENT_BROWSER_APPCACHE_MOCK_APPCACHE_STORAGE_H_
6 #define CONTENT_BROWSER_APPCACHE_MOCK_APPCACHE_STORAGE_H_
7 
8 #include <stdint.h>
9 
10 #include <map>
11 #include <memory>
12 #include <unordered_map>
13 #include <vector>
14 
15 #include "base/callback.h"
16 #include "base/containers/circular_deque.h"
17 #include "base/gtest_prod_util.h"
18 #include "base/macros.h"
19 #include "base/memory/weak_ptr.h"
20 #include "content/browser/appcache/appcache.h"
21 #include "content/browser/appcache/appcache_disk_cache.h"
22 #include "content/browser/appcache/appcache_disk_cache_ops.h"
23 #include "content/browser/appcache/appcache_group.h"
24 #include "content/browser/appcache/appcache_storage.h"
25 #include "third_party/blink/public/mojom/appcache/appcache_info.mojom-forward.h"
26 
27 namespace content {
28 FORWARD_DECLARE_TEST(AppCacheServiceImplTest, DeleteAppCachesForOrigin);
29 FORWARD_DECLARE_TEST(MockAppCacheStorageTest, BasicFindMainResponse);
30 FORWARD_DECLARE_TEST(MockAppCacheStorageTest,
31                      BasicFindMainFallbackResponse);
32 FORWARD_DECLARE_TEST(MockAppCacheStorageTest, CreateGroup);
33 FORWARD_DECLARE_TEST(MockAppCacheStorageTest, FindMainResponseExclusions);
34 FORWARD_DECLARE_TEST(MockAppCacheStorageTest,
35                      FindMainResponseWithMultipleCandidates);
36 FORWARD_DECLARE_TEST(MockAppCacheStorageTest, LoadCache_FarHit);
37 FORWARD_DECLARE_TEST(MockAppCacheStorageTest, LoadGroupAndCache_FarHit);
38 FORWARD_DECLARE_TEST(MockAppCacheStorageTest, MakeGroupObsolete);
39 FORWARD_DECLARE_TEST(MockAppCacheStorageTest, StoreNewGroup);
40 FORWARD_DECLARE_TEST(MockAppCacheStorageTest, StoreExistingGroup);
41 FORWARD_DECLARE_TEST(MockAppCacheStorageTest,
42                      StoreExistingGroupExistingCache);
43 class AppCacheRequestHandlerTest;
44 class AppCacheServiceImplTest;
45 class MockAppCacheStorageTest;
46 
47 namespace appcache_update_job_unittest {
48 class AppCacheUpdateJobTest;
49 }
50 
51 // For use in unit tests.
52 // Note: This class is also being used to bootstrap our development efforts.
53 // We can get web tests up and running, and back fill with real storage
54 // somewhat in parallel.
55 class MockAppCacheStorage : public AppCacheStorage {
56  public:
57   explicit MockAppCacheStorage(AppCacheServiceImpl* service);
58   ~MockAppCacheStorage() override;
59 
60   void GetAllInfo(Delegate* delegate) override;
61   void LoadCache(int64_t id, Delegate* delegate) override;
62   void LoadOrCreateGroup(const GURL& manifest_url, Delegate* delegate) override;
63   void StoreGroupAndNewestCache(AppCacheGroup* group,
64                                 AppCache* newest_cache,
65                                 Delegate* delegate) override;
66   void FindResponseForMainRequest(const GURL& url,
67                                   const GURL& preferred_manifest_url,
68                                   Delegate* delegate) override;
69   void FindResponseForSubRequest(AppCache* cache,
70                                  const GURL& url,
71                                  AppCacheEntry* found_entry,
72                                  AppCacheEntry* found_fallback_entry,
73                                  bool* found_network_namespace) override;
74   void MarkEntryAsForeign(const GURL& entry_url, int64_t cache_id) override;
75   void MakeGroupObsolete(AppCacheGroup* group,
76                          Delegate* delegate,
77                          int response_code) override;
78   void StoreEvictionTimes(AppCacheGroup* group) override;
79   std::unique_ptr<AppCacheResponseReader> CreateResponseReader(
80       const GURL& manifest_url,
81       int64_t response_id) override;
82   std::unique_ptr<AppCacheResponseWriter> CreateResponseWriter(
83       const GURL& manifest_url) override;
84   std::unique_ptr<AppCacheResponseMetadataWriter> CreateResponseMetadataWriter(
85       int64_t response_id) override;
86   void DoomResponses(const GURL& manifest_url,
87                      const std::vector<int64_t>& response_ids) override;
88   void DeleteResponses(const GURL& manifest_url,
89                        const std::vector<int64_t>& response_ids) override;
90   bool IsInitialized() override;
91 
92  private:
93   friend class AppCacheRequestHandlerTest;
94   friend class AppCacheServiceImplTest;
95   friend class appcache_update_job_unittest::AppCacheUpdateJobTest;
96   friend class MockAppCacheStorageTest;
97 
98   using StoredCacheMap = std::unordered_map<int64_t, scoped_refptr<AppCache>>;
99   using StoredGroupMap = std::map<GURL, scoped_refptr<AppCacheGroup>>;
100   using DoomedResponseIds = std::set<int64_t>;
101   using StoredEvictionTimesMap =
102       std::map<int64_t, std::pair<base::Time, base::Time>>;
103 
104   void ProcessGetAllInfo(scoped_refptr<DelegateReference> delegate_ref);
105   void ProcessLoadCache(int64_t id,
106                         scoped_refptr<DelegateReference> delegate_ref);
107   void ProcessLoadOrCreateGroup(
108       const GURL& manifest_url, scoped_refptr<DelegateReference> delegate_ref);
109   void ProcessStoreGroupAndNewestCache(
110       scoped_refptr<AppCacheGroup> group, scoped_refptr<AppCache> newest_cache,
111       scoped_refptr<DelegateReference> delegate_ref);
112   void ProcessMakeGroupObsolete(scoped_refptr<AppCacheGroup> group,
113                                 scoped_refptr<DelegateReference> delegate_ref,
114                                 int response_code);
115   void ProcessFindResponseForMainRequest(
116       const GURL& url, scoped_refptr<DelegateReference> delegate_ref);
117 
118   void ScheduleTask(base::OnceClosure task);
119   void RunOnePendingTask();
120 
121   void AddStoredCache(AppCache* cache);
122   void RemoveStoredCache(AppCache* cache);
123   void RemoveStoredCaches(const std::vector<AppCache*>& caches);
IsCacheStored(const AppCache * cache)124   bool IsCacheStored(const AppCache* cache) {
125     return stored_caches_.find(cache->cache_id()) != stored_caches_.end();
126   }
127 
128   void AddStoredGroup(AppCacheGroup* group);
129   void RemoveStoredGroup(AppCacheGroup* group);
IsGroupStored(const AppCacheGroup * group)130   bool IsGroupStored(const AppCacheGroup* group) {
131     return IsGroupForManifestStored(group->manifest_url());
132   }
IsGroupForManifestStored(const GURL & manifest_url)133   bool IsGroupForManifestStored(const GURL& manifest_url) {
134     return stored_groups_.find(manifest_url) != stored_groups_.end();
135   }
136 
137   // These helpers determine when certain operations should complete
138   // asynchronously vs synchronously to faithfully mimic, or mock,
139   // the behavior of the real implemenation of the AppCacheStorage
140   // interface.
141   bool ShouldGroupLoadAppearAsync(const AppCacheGroup* group);
142   bool ShouldCacheLoadAppearAsync(const AppCache* cache);
143 
144   // Lazily constructed in-memory disk cache.
disk_cache()145   AppCacheDiskCache* disk_cache() {
146     if (!disk_cache_) {
147       const int kMaxCacheSize = 10 * 1024 * 1024;
148       disk_cache_ = std::make_unique<AppCacheDiskCache>();
149       disk_cache_->InitWithMemBackend(kMaxCacheSize,
150                                       net::CompletionOnceCallback());
151     }
152     return disk_cache_.get();
153   }
154 
155   // Simulate failures for testing. Once set all subsequent calls
156   // to MakeGroupObsolete or StorageGroupAndNewestCache will fail.
SimulateMakeGroupObsoleteFailure()157   void SimulateMakeGroupObsoleteFailure() {
158     simulate_make_group_obsolete_failure_ = true;
159   }
SimulateStoreGroupAndNewestCacheFailure()160   void SimulateStoreGroupAndNewestCacheFailure() {
161     simulate_store_group_and_newest_cache_failure_ = true;
162   }
163 
164   // Simulate FindResponseFor results for testing. These
165   // provided values will be return on the next call to
166   // the corresponding Find method, subsequent calls are
167   // unaffected.
SimulateFindMainResource(const AppCacheEntry & entry,const GURL & fallback_url,const AppCacheEntry & fallback_entry,int64_t cache_id,int64_t group_id,const GURL & manifest_url)168   void SimulateFindMainResource(const AppCacheEntry& entry,
169                                 const GURL& fallback_url,
170                                 const AppCacheEntry& fallback_entry,
171                                 int64_t cache_id,
172                                 int64_t group_id,
173                                 const GURL& manifest_url) {
174     simulate_find_main_resource_ = true;
175     simulate_find_sub_resource_ = false;
176     simulated_found_entry_ = entry;
177     simulated_found_fallback_url_ = fallback_url;
178     simulated_found_fallback_entry_ = fallback_entry;
179     simulated_found_cache_id_ = cache_id;
180     simulated_found_group_id_ = group_id;
181     simulated_found_manifest_url_ = manifest_url,
182     simulated_found_network_namespace_ = false;  // N/A to main resource loads
183   }
SimulateFindSubResource(const AppCacheEntry & entry,const AppCacheEntry & fallback_entry,bool network_namespace)184   void SimulateFindSubResource(
185       const AppCacheEntry& entry,
186       const AppCacheEntry& fallback_entry,
187       bool network_namespace) {
188     simulate_find_main_resource_ = false;
189     simulate_find_sub_resource_ = true;
190     simulated_found_entry_ = entry;
191     simulated_found_fallback_entry_ = fallback_entry;
192     simulated_found_cache_id_ =
193         blink::mojom::kAppCacheNoCacheId;    // N/A to sub resource loads
194     simulated_found_manifest_url_ = GURL();  // N/A to sub resource loads
195     simulated_found_group_id_ = 0;  // N/A to sub resource loads
196     simulated_found_network_namespace_ = network_namespace;
197   }
198 
SimulateGetAllInfo(scoped_refptr<AppCacheInfoCollection> info)199   void SimulateGetAllInfo(scoped_refptr<AppCacheInfoCollection> info) {
200     simulated_appcache_info_ = std::move(info);
201   }
202 
SimulateResponseReader(std::unique_ptr<AppCacheResponseReader> reader)203   void SimulateResponseReader(std::unique_ptr<AppCacheResponseReader> reader) {
204     simulated_reader_ = std::move(reader);
205   }
206 
207   StoredCacheMap stored_caches_;
208   StoredGroupMap stored_groups_;
209   StoredEvictionTimesMap stored_eviction_times_;
210   DoomedResponseIds doomed_response_ids_;
211   std::unique_ptr<AppCacheDiskCache> disk_cache_;
212   base::circular_deque<base::OnceClosure> pending_tasks_;
213 
214   bool simulate_make_group_obsolete_failure_;
215   bool simulate_store_group_and_newest_cache_failure_;
216 
217   bool simulate_find_main_resource_;
218   bool simulate_find_sub_resource_;
219   AppCacheEntry simulated_found_entry_;
220   AppCacheEntry simulated_found_fallback_entry_;
221   int64_t simulated_found_cache_id_;
222   int64_t simulated_found_group_id_;
223   GURL simulated_found_fallback_url_;
224   GURL simulated_found_manifest_url_;
225   bool simulated_found_network_namespace_;
226   scoped_refptr<AppCacheInfoCollection> simulated_appcache_info_;
227   std::unique_ptr<AppCacheResponseReader> simulated_reader_;
228 
229   base::WeakPtrFactory<MockAppCacheStorage> weak_factory_{this};
230 
231   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
232                            BasicFindMainResponse);
233   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
234                            BasicFindMainFallbackResponse);
235   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest, CreateGroup);
236   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
237                            FindMainResponseExclusions);
238   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
239                            FindMainResponseWithMultipleCandidates);
240   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest, LoadCache_FarHit);
241   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
242                            LoadGroupAndCache_FarHit);
243   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest, MakeGroupObsolete);
244   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest, StoreNewGroup);
245   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
246                            StoreExistingGroup);
247   FRIEND_TEST_ALL_PREFIXES(MockAppCacheStorageTest,
248                            StoreExistingGroupExistingCache);
249   FRIEND_TEST_ALL_PREFIXES(AppCacheServiceImplTest,
250                            DeleteAppCachesForOrigin);
251 
252   DISALLOW_COPY_AND_ASSIGN(MockAppCacheStorage);
253 };
254 
255 }  // namespace content
256 
257 #endif  // CONTENT_BROWSER_APPCACHE_MOCK_APPCACHE_STORAGE_H_
258