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