OLD | NEW |
| (Empty) |
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 // This file contains test infrastructure for multiple files | |
6 // (current cookie_monster_unittest.cc and cookie_monster_perftest.cc) | |
7 // that need to test out CookieMonster interactions with the backing store. | |
8 // It should only be included by test code. | |
9 | |
10 #ifndef NET_COOKIES_COOKIE_MONSTER_STORE_TEST_H_ | |
11 #define NET_COOKIES_COOKIE_MONSTER_STORE_TEST_H_ | |
12 | |
13 #include <map> | |
14 #include <string> | |
15 #include <utility> | |
16 #include <vector> | |
17 #include "net/cookies/canonical_cookie.h" | |
18 #include "net/cookies/cookie_monster.h" | |
19 | |
20 namespace base { | |
21 class Time; | |
22 } | |
23 | |
24 namespace net { | |
25 | |
26 // Wrapper class for posting a loaded callback. Since the Callback class is not | |
27 // reference counted, we cannot post a callback to the message loop directly, | |
28 // instead we post a LoadedCallbackTask. | |
29 class LoadedCallbackTask | |
30 : public base::RefCountedThreadSafe<LoadedCallbackTask> { | |
31 public: | |
32 typedef CookieMonster::PersistentCookieStore::LoadedCallback LoadedCallback; | |
33 | |
34 LoadedCallbackTask(LoadedCallback loaded_callback, | |
35 std::vector<CanonicalCookie*> cookies); | |
36 | |
37 void Run() { | |
38 loaded_callback_.Run(cookies_); | |
39 } | |
40 | |
41 private: | |
42 friend class base::RefCountedThreadSafe<LoadedCallbackTask>; | |
43 ~LoadedCallbackTask(); | |
44 | |
45 LoadedCallback loaded_callback_; | |
46 std::vector<CanonicalCookie*> cookies_; | |
47 | |
48 DISALLOW_COPY_AND_ASSIGN(LoadedCallbackTask); | |
49 }; // Wrapper class LoadedCallbackTask | |
50 | |
51 // Describes a call to one of the 3 functions of PersistentCookieStore. | |
52 struct CookieStoreCommand { | |
53 enum Type { | |
54 ADD, | |
55 UPDATE_ACCESS_TIME, | |
56 REMOVE, | |
57 }; | |
58 | |
59 CookieStoreCommand(Type type, const CanonicalCookie& cookie) | |
60 : type(type), | |
61 cookie(cookie) {} | |
62 | |
63 Type type; | |
64 CanonicalCookie cookie; | |
65 }; | |
66 | |
67 // Implementation of PersistentCookieStore that captures the | |
68 // received commands and saves them to a list. | |
69 // The result of calls to Load() can be configured using SetLoadExpectation(). | |
70 class MockPersistentCookieStore | |
71 : public CookieMonster::PersistentCookieStore { | |
72 public: | |
73 typedef std::vector<CookieStoreCommand> CommandList; | |
74 | |
75 MockPersistentCookieStore(); | |
76 | |
77 void SetLoadExpectation( | |
78 bool return_value, | |
79 const std::vector<CanonicalCookie*>& result); | |
80 | |
81 const CommandList& commands() const { | |
82 return commands_; | |
83 } | |
84 | |
85 void Load(const LoadedCallback& loaded_callback) override; | |
86 | |
87 void LoadCookiesForKey(const std::string& key, | |
88 const LoadedCallback& loaded_callback) override; | |
89 | |
90 void AddCookie(const CanonicalCookie& cookie) override; | |
91 | |
92 void UpdateCookieAccessTime(const CanonicalCookie& cookie) override; | |
93 | |
94 void DeleteCookie(const CanonicalCookie& cookie) override; | |
95 | |
96 void Flush(const base::Closure& callback) override; | |
97 | |
98 void SetForceKeepSessionState() override; | |
99 | |
100 protected: | |
101 ~MockPersistentCookieStore() override; | |
102 | |
103 private: | |
104 CommandList commands_; | |
105 | |
106 // Deferred result to use when Load() is called. | |
107 bool load_return_value_; | |
108 std::vector<CanonicalCookie*> load_result_; | |
109 // Indicates if the store has been fully loaded to avoid returning duplicate | |
110 // cookies. | |
111 bool loaded_; | |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(MockPersistentCookieStore); | |
114 }; | |
115 | |
116 // Mock for CookieMonsterDelegate | |
117 class MockCookieMonsterDelegate : public CookieMonsterDelegate { | |
118 public: | |
119 typedef std::pair<CanonicalCookie, bool> | |
120 CookieNotification; | |
121 | |
122 MockCookieMonsterDelegate(); | |
123 | |
124 const std::vector<CookieNotification>& changes() const { return changes_; } | |
125 | |
126 void reset() { changes_.clear(); } | |
127 | |
128 void OnCookieChanged(const CanonicalCookie& cookie, | |
129 bool removed, | |
130 CookieMonsterDelegate::ChangeCause cause) override; | |
131 | |
132 void OnLoaded() override; | |
133 | |
134 private: | |
135 ~MockCookieMonsterDelegate() override; | |
136 | |
137 std::vector<CookieNotification> changes_; | |
138 | |
139 DISALLOW_COPY_AND_ASSIGN(MockCookieMonsterDelegate); | |
140 }; | |
141 | |
142 // Helper to build a single CanonicalCookie. | |
143 CanonicalCookie BuildCanonicalCookie(const std::string& key, | |
144 const std::string& cookie_line, | |
145 const base::Time& creation_time); | |
146 | |
147 // Helper to build a list of CanonicalCookie*s. | |
148 void AddCookieToList( | |
149 const std::string& key, | |
150 const std::string& cookie_line, | |
151 const base::Time& creation_time, | |
152 std::vector<CanonicalCookie*>* out_list); | |
153 | |
154 // Just act like a backing database. Keep cookie information from | |
155 // Add/Update/Delete and regurgitate it when Load is called. | |
156 class MockSimplePersistentCookieStore | |
157 : public CookieMonster::PersistentCookieStore { | |
158 public: | |
159 MockSimplePersistentCookieStore(); | |
160 | |
161 void Load(const LoadedCallback& loaded_callback) override; | |
162 | |
163 void LoadCookiesForKey(const std::string& key, | |
164 const LoadedCallback& loaded_callback) override; | |
165 | |
166 void AddCookie(const CanonicalCookie& cookie) override; | |
167 | |
168 void UpdateCookieAccessTime(const CanonicalCookie& cookie) override; | |
169 | |
170 void DeleteCookie(const CanonicalCookie& cookie) override; | |
171 | |
172 void Flush(const base::Closure& callback) override; | |
173 | |
174 void SetForceKeepSessionState() override; | |
175 | |
176 protected: | |
177 ~MockSimplePersistentCookieStore() override; | |
178 | |
179 private: | |
180 typedef std::map<int64, CanonicalCookie> CanonicalCookieMap; | |
181 | |
182 CanonicalCookieMap cookies_; | |
183 | |
184 // Indicates if the store has been fully loaded to avoid return duplicate | |
185 // cookies in subsequent load requests | |
186 bool loaded_; | |
187 }; | |
188 | |
189 // Helper function for creating a CookieMonster backed by a | |
190 // MockSimplePersistentCookieStore for garbage collection testing. | |
191 // | |
192 // Fill the store through import with |num_cookies| cookies, |num_old_cookies| | |
193 // with access time Now()-days_old, the rest with access time Now(). | |
194 // Do two SetCookies(). Return whether each of the two SetCookies() took | |
195 // longer than |gc_perf_micros| to complete, and how many cookie were | |
196 // left in the store afterwards. | |
197 CookieMonster* CreateMonsterFromStoreForGC( | |
198 int num_cookies, | |
199 int num_old_cookies, | |
200 int days_old); | |
201 | |
202 } // namespace net | |
203 | |
204 #endif // NET_COOKIES_COOKIE_MONSTER_STORE_TEST_H_ | |
OLD | NEW |