• 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 // 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 
Run()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 
CookieStoreCommandCookieStoreCommand59   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 
commands()81   const CommandList& commands() const {
82     return commands_;
83   }
84 
85   virtual void Load(const LoadedCallback& loaded_callback) OVERRIDE;
86 
87   virtual void LoadCookiesForKey(const std::string& key,
88     const LoadedCallback& loaded_callback) OVERRIDE;
89 
90   virtual void AddCookie(const CanonicalCookie& cookie) OVERRIDE;
91 
92   virtual void UpdateCookieAccessTime(
93       const CanonicalCookie& cookie) OVERRIDE;
94 
95   virtual void DeleteCookie(
96       const CanonicalCookie& cookie) OVERRIDE;
97 
98   virtual void Flush(const base::Closure& callback) OVERRIDE;
99 
100   virtual void SetForceKeepSessionState() OVERRIDE;
101 
102  protected:
103   virtual ~MockPersistentCookieStore();
104 
105  private:
106   CommandList commands_;
107 
108   // Deferred result to use when Load() is called.
109   bool load_return_value_;
110   std::vector<CanonicalCookie*> load_result_;
111   // Indicates if the store has been fully loaded to avoid returning duplicate
112   // cookies.
113   bool loaded_;
114 
115   DISALLOW_COPY_AND_ASSIGN(MockPersistentCookieStore);
116 };
117 
118 // Mock for CookieMonsterDelegate
119 class MockCookieMonsterDelegate : public CookieMonsterDelegate {
120  public:
121   typedef std::pair<CanonicalCookie, bool>
122       CookieNotification;
123 
124   MockCookieMonsterDelegate();
125 
changes()126   const std::vector<CookieNotification>& changes() const { return changes_; }
127 
reset()128   void reset() { changes_.clear(); }
129 
130   virtual void OnCookieChanged(
131       const CanonicalCookie& cookie,
132       bool removed,
133       CookieMonsterDelegate::ChangeCause cause) OVERRIDE;
134 
135   virtual void OnLoaded() OVERRIDE;
136 
137  private:
138   virtual ~MockCookieMonsterDelegate();
139 
140   std::vector<CookieNotification> changes_;
141 
142   DISALLOW_COPY_AND_ASSIGN(MockCookieMonsterDelegate);
143 };
144 
145 // Helper to build a single CanonicalCookie.
146 CanonicalCookie BuildCanonicalCookie(const std::string& key,
147                                      const std::string& cookie_line,
148                                      const base::Time& creation_time);
149 
150 // Helper to build a list of CanonicalCookie*s.
151 void AddCookieToList(
152     const std::string& key,
153     const std::string& cookie_line,
154     const base::Time& creation_time,
155     std::vector<CanonicalCookie*>* out_list);
156 
157 // Just act like a backing database.  Keep cookie information from
158 // Add/Update/Delete and regurgitate it when Load is called.
159 class MockSimplePersistentCookieStore
160     : public CookieMonster::PersistentCookieStore {
161  public:
162   MockSimplePersistentCookieStore();
163 
164   virtual void Load(const LoadedCallback& loaded_callback) OVERRIDE;
165 
166   virtual void LoadCookiesForKey(const std::string& key,
167       const LoadedCallback& loaded_callback) OVERRIDE;
168 
169   virtual void AddCookie(const CanonicalCookie& cookie) OVERRIDE;
170 
171   virtual void UpdateCookieAccessTime(const CanonicalCookie& cookie) OVERRIDE;
172 
173   virtual void DeleteCookie(const CanonicalCookie& cookie) OVERRIDE;
174 
175   virtual void Flush(const base::Closure& callback) OVERRIDE;
176 
177   virtual void SetForceKeepSessionState() OVERRIDE;
178 
179  protected:
180   virtual ~MockSimplePersistentCookieStore();
181 
182  private:
183   typedef std::map<int64, CanonicalCookie> CanonicalCookieMap;
184 
185   CanonicalCookieMap cookies_;
186 
187   // Indicates if the store has been fully loaded to avoid return duplicate
188   // cookies in subsequent load requests
189   bool loaded_;
190 };
191 
192 // Helper function for creating a CookieMonster backed by a
193 // MockSimplePersistentCookieStore for garbage collection testing.
194 //
195 // Fill the store through import with |num_cookies| cookies, |num_old_cookies|
196 // with access time Now()-days_old, the rest with access time Now().
197 // Do two SetCookies().  Return whether each of the two SetCookies() took
198 // longer than |gc_perf_micros| to complete, and how many cookie were
199 // left in the store afterwards.
200 CookieMonster* CreateMonsterFromStoreForGC(
201     int num_cookies,
202     int num_old_cookies,
203     int days_old);
204 
205 }  // namespace net
206 
207 #endif  // NET_COOKIES_COOKIE_MONSTER_STORE_TEST_H_
208