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