1 // Copyright 2012 The Chromium Authors 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 <stdint.h> 14 15 #include <map> 16 #include <memory> 17 #include <string> 18 #include <utility> 19 #include <vector> 20 21 #include "net/cookies/canonical_cookie.h" 22 #include "net/cookies/cookie_monster.h" 23 #include "net/log/net_log_with_source.h" 24 25 class GURL; 26 27 namespace base { 28 class Time; 29 } 30 31 namespace net { 32 33 // Describes a call to one of the 5 functions of PersistentCookieStore. 34 struct CookieStoreCommand { 35 enum Type { 36 LOAD, 37 LOAD_COOKIES_FOR_KEY, 38 // UPDATE_ACCESS_TIME is not included in this list, because get cookie 39 // commands may or may not end updating the access time, unless they have 40 // the option set not to do so. 41 ADD, 42 REMOVE, 43 }; 44 45 // Constructor for LOAD and LOAD_COOKIES_FOR_KEY calls. |key| should be empty 46 // for LOAD_COOKIES_FOR_KEY. 47 CookieStoreCommand( 48 Type type, 49 CookieMonster::PersistentCookieStore::LoadedCallback loaded_callback, 50 const std::string& key); 51 52 // Constructor for ADD, UPDATE_ACCESS_TIME, and REMOVE calls. 53 CookieStoreCommand(Type type, const CanonicalCookie& cookie); 54 CookieStoreCommand(CookieStoreCommand&& other); 55 ~CookieStoreCommand(); 56 57 Type type; 58 59 // Only non-null for LOAD and LOAD_COOKIES_FOR_KEY. 60 CookieMonster::PersistentCookieStore::LoadedCallback loaded_callback; 61 62 // Only non-empty for LOAD_COOKIES_FOR_KEY. 63 std::string key; 64 65 // Only non-null for ADD, UPDATE_ACCESS_TIME, and REMOVE. 66 CanonicalCookie cookie; 67 }; 68 69 // Implementation of PersistentCookieStore that captures the 70 // received commands and saves them to a list. 71 // The result of calls to Load() can be configured using SetLoadExpectation(). 72 class MockPersistentCookieStore : public CookieMonster::PersistentCookieStore { 73 public: 74 typedef std::vector<CookieStoreCommand> CommandList; 75 76 MockPersistentCookieStore(); 77 78 MockPersistentCookieStore(const MockPersistentCookieStore&) = delete; 79 MockPersistentCookieStore& operator=(const MockPersistentCookieStore&) = 80 delete; 81 82 // When set, Load() and LoadCookiesForKey() calls are store in the command 83 // list, rather than being automatically executed. Defaults to false. set_store_load_commands(bool store_load_commands)84 void set_store_load_commands(bool store_load_commands) { 85 store_load_commands_ = store_load_commands; 86 } 87 88 void SetLoadExpectation(bool return_value, 89 std::vector<std::unique_ptr<CanonicalCookie>> result); 90 commands()91 const CommandList& commands() const { return commands_; } TakeCommands()92 CommandList TakeCommands() { return std::move(commands_); } TakeCallbackAt(size_t i)93 CookieMonster::PersistentCookieStore::LoadedCallback TakeCallbackAt( 94 size_t i) { 95 return std::move(commands_[i].loaded_callback); 96 } 97 98 void Load(LoadedCallback loaded_callback, 99 const NetLogWithSource& net_log) override; 100 101 void LoadCookiesForKey(const std::string& key, 102 LoadedCallback loaded_callback) override; 103 104 void AddCookie(const CanonicalCookie& cookie) override; 105 106 void UpdateCookieAccessTime(const CanonicalCookie& cookie) override; 107 108 void DeleteCookie(const CanonicalCookie& cookie) override; 109 110 void SetForceKeepSessionState() override; 111 112 void SetBeforeCommitCallback(base::RepeatingClosure callback) override; 113 114 void Flush(base::OnceClosure callback) override; 115 116 protected: 117 ~MockPersistentCookieStore() override; 118 119 private: 120 CommandList commands_; 121 122 bool store_load_commands_ = false; 123 124 // Deferred result to use when Load() is called. 125 bool load_return_value_ = true; 126 std::vector<std::unique_ptr<CanonicalCookie>> load_result_; 127 // Indicates if the store has been fully loaded to avoid returning duplicate 128 // cookies. 129 bool loaded_ = false; 130 }; 131 132 // Helper to build a single CanonicalCookie. 133 std::unique_ptr<CanonicalCookie> BuildCanonicalCookie( 134 const GURL& url, 135 const std::string& cookie_line, 136 const base::Time& creation_time); 137 138 // Helper to build a list of CanonicalCookie*s. 139 void AddCookieToList(const GURL& url, 140 const std::string& cookie_line, 141 const base::Time& creation_time, 142 std::vector<std::unique_ptr<CanonicalCookie>>* out_list); 143 144 // Just act like a backing database. Keep cookie information from 145 // Add/Update/Delete and regurgitate it when Load is called. 146 class MockSimplePersistentCookieStore 147 : public CookieMonster::PersistentCookieStore { 148 public: 149 MockSimplePersistentCookieStore(); 150 151 void Load(LoadedCallback loaded_callback, 152 const NetLogWithSource& net_log) override; 153 154 void LoadCookiesForKey(const std::string& key, 155 LoadedCallback loaded_callback) override; 156 157 void AddCookie(const CanonicalCookie& cookie) override; 158 159 void UpdateCookieAccessTime(const CanonicalCookie& cookie) override; 160 161 void DeleteCookie(const CanonicalCookie& cookie) override; 162 163 void SetForceKeepSessionState() override; 164 165 void SetBeforeCommitCallback(base::RepeatingClosure callback) override; 166 167 void Flush(base::OnceClosure callback) override; 168 169 protected: 170 ~MockSimplePersistentCookieStore() override; 171 172 private: 173 typedef std::map<CanonicalCookie::UniqueCookieKey, CanonicalCookie> 174 CanonicalCookieMap; 175 176 CanonicalCookieMap cookies_; 177 178 // Indicates if the store has been fully loaded to avoid return duplicate 179 // cookies in subsequent load requests 180 bool loaded_ = false; 181 }; 182 183 // Helper function for creating a CookieMonster backed by a 184 // MockSimplePersistentCookieStore for garbage collection testing. 185 // 186 // Fill the store through import with |num_*_cookies| cookies, 187 // |num_old_*_cookies| with access time Now()-days_old, the rest with access 188 // time Now(). Cookies made by |num_secure_cookies| and |num_non_secure_cookies| 189 // will be marked secure and non-secure, respectively. Do two SetCookies(). 190 // Return whether each of the two SetCookies() took longer than |gc_perf_micros| 191 // to complete, and how many cookie were left in the store afterwards. 192 std::unique_ptr<CookieMonster> CreateMonsterFromStoreForGC( 193 int num_secure_cookies, 194 int num_old_secure_cookies, 195 int num_non_secure_cookies, 196 int num_old_non_secure_cookies, 197 int days_old); 198 199 } // namespace net 200 201 #endif // NET_COOKIES_COOKIE_MONSTER_STORE_TEST_H_ 202