| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "base/message_loop.h" | |
| 11 #include "base/time.h" | |
| 12 #include "net/base/cookie_monster.h" | 10 #include "net/base/cookie_monster.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | 11 |
| 15 namespace { | 12 namespace base { |
| 13 class Time; |
| 14 } |
| 15 |
| 16 namespace net { |
| 16 | 17 |
| 17 // Describes a call to one of the 3 functions of PersistentCookieStore. | 18 // Describes a call to one of the 3 functions of PersistentCookieStore. |
| 18 struct CookieStoreCommand { | 19 struct CookieStoreCommand { |
| 19 enum Type { | 20 enum Type { |
| 20 ADD, | 21 ADD, |
| 21 UPDATE_ACCESS_TIME, | 22 UPDATE_ACCESS_TIME, |
| 22 REMOVE, | 23 REMOVE, |
| 23 }; | 24 }; |
| 24 | 25 |
| 25 CookieStoreCommand(Type type, | 26 CookieStoreCommand(Type type, |
| 26 const net::CookieMonster::CanonicalCookie& cookie) | 27 const net::CookieMonster::CanonicalCookie& cookie) |
| 27 : type(type), | 28 : type(type), |
| 28 cookie(cookie) {} | 29 cookie(cookie) {} |
| 29 | 30 |
| 30 Type type; | 31 Type type; |
| 31 net::CookieMonster::CanonicalCookie cookie; | 32 net::CookieMonster::CanonicalCookie cookie; |
| 32 }; | 33 }; |
| 33 | 34 |
| 34 // Implementation of PersistentCookieStore that captures the | 35 // Implementation of PersistentCookieStore that captures the |
| 35 // received commands and saves them to a list. | 36 // received commands and saves them to a list. |
| 36 // The result of calls to Load() can be configured using SetLoadExpectation(). | 37 // The result of calls to Load() can be configured using SetLoadExpectation(). |
| 37 class MockPersistentCookieStore | 38 class MockPersistentCookieStore |
| 38 : public net::CookieMonster::PersistentCookieStore { | 39 : public net::CookieMonster::PersistentCookieStore { |
| 39 public: | 40 public: |
| 40 typedef std::vector<CookieStoreCommand> CommandList; | 41 typedef std::vector<CookieStoreCommand> CommandList; |
| 41 | 42 |
| 42 MockPersistentCookieStore() : load_return_value_(true) { | 43 MockPersistentCookieStore(); |
| 43 } | 44 virtual ~MockPersistentCookieStore(); |
| 44 | |
| 45 virtual bool Load( | |
| 46 std::vector<net::CookieMonster::CanonicalCookie*>* out_cookies) { | |
| 47 bool ok = load_return_value_; | |
| 48 if (ok) | |
| 49 *out_cookies = load_result_; | |
| 50 return ok; | |
| 51 } | |
| 52 | |
| 53 virtual void AddCookie(const net::CookieMonster::CanonicalCookie& cookie) { | |
| 54 commands_.push_back( | |
| 55 CookieStoreCommand(CookieStoreCommand::ADD, cookie)); | |
| 56 } | |
| 57 | |
| 58 virtual void UpdateCookieAccessTime( | |
| 59 const net::CookieMonster::CanonicalCookie& cookie) { | |
| 60 commands_.push_back(CookieStoreCommand( | |
| 61 CookieStoreCommand::UPDATE_ACCESS_TIME, cookie)); | |
| 62 } | |
| 63 | |
| 64 virtual void DeleteCookie( | |
| 65 const net::CookieMonster::CanonicalCookie& cookie) { | |
| 66 commands_.push_back( | |
| 67 CookieStoreCommand(CookieStoreCommand::REMOVE, cookie)); | |
| 68 } | |
| 69 | |
| 70 virtual void Flush(Task* completion_task) { | |
| 71 if (completion_task) | |
| 72 MessageLoop::current()->PostTask(FROM_HERE, completion_task); | |
| 73 } | |
| 74 | |
| 75 // No files are created so nothing to clear either | |
| 76 virtual void SetClearLocalStateOnExit(bool clear_local_state) {} | |
| 77 | 45 |
| 78 void SetLoadExpectation( | 46 void SetLoadExpectation( |
| 79 bool return_value, | 47 bool return_value, |
| 80 const std::vector<net::CookieMonster::CanonicalCookie*>& result) { | 48 const std::vector<net::CookieMonster::CanonicalCookie*>& result); |
| 81 load_return_value_ = return_value; | |
| 82 load_result_ = result; | |
| 83 } | |
| 84 | 49 |
| 85 const CommandList& commands() const { | 50 const CommandList& commands() const { |
| 86 return commands_; | 51 return commands_; |
| 87 } | 52 } |
| 88 | 53 |
| 54 virtual bool Load( |
| 55 std::vector<net::CookieMonster::CanonicalCookie*>* out_cookies); |
| 56 |
| 57 virtual void AddCookie(const net::CookieMonster::CanonicalCookie& cookie); |
| 58 |
| 59 virtual void UpdateCookieAccessTime( |
| 60 const net::CookieMonster::CanonicalCookie& cookie); |
| 61 |
| 62 virtual void DeleteCookie( |
| 63 const net::CookieMonster::CanonicalCookie& cookie); |
| 64 |
| 65 virtual void Flush(Task* completion_task); |
| 66 |
| 67 // No files are created so nothing to clear either |
| 68 virtual void SetClearLocalStateOnExit(bool clear_local_state); |
| 69 |
| 89 private: | 70 private: |
| 90 CommandList commands_; | 71 CommandList commands_; |
| 91 | 72 |
| 92 // Deferred result to use when Load() is called. | 73 // Deferred result to use when Load() is called. |
| 93 bool load_return_value_; | 74 bool load_return_value_; |
| 94 std::vector<net::CookieMonster::CanonicalCookie*> load_result_; | 75 std::vector<net::CookieMonster::CanonicalCookie*> load_result_; |
| 95 | 76 |
| 96 DISALLOW_COPY_AND_ASSIGN(MockPersistentCookieStore); | 77 DISALLOW_COPY_AND_ASSIGN(MockPersistentCookieStore); |
| 97 }; | 78 }; |
| 98 | 79 |
| 99 // Mock for CookieMonster::Delegate | 80 // Mock for CookieMonster::Delegate |
| 100 class MockCookieMonsterDelegate : public net::CookieMonster::Delegate { | 81 class MockCookieMonsterDelegate : public net::CookieMonster::Delegate { |
| 101 public: | 82 public: |
| 102 typedef std::pair<net::CookieMonster::CanonicalCookie, bool> | 83 typedef std::pair<net::CookieMonster::CanonicalCookie, bool> |
| 103 CookieNotification; | 84 CookieNotification; |
| 104 | 85 |
| 105 MockCookieMonsterDelegate() {} | 86 MockCookieMonsterDelegate(); |
| 106 | |
| 107 virtual void OnCookieChanged( | |
| 108 const net::CookieMonster::CanonicalCookie& cookie, | |
| 109 bool removed) { | |
| 110 CookieNotification notification(cookie, removed); | |
| 111 changes_.push_back(notification); | |
| 112 } | |
| 113 | 87 |
| 114 const std::vector<CookieNotification>& changes() const { return changes_; } | 88 const std::vector<CookieNotification>& changes() const { return changes_; } |
| 115 | 89 |
| 116 void reset() { changes_.clear(); } | 90 void reset() { changes_.clear(); } |
| 117 | 91 |
| 92 virtual void OnCookieChanged( |
| 93 const net::CookieMonster::CanonicalCookie& cookie, |
| 94 bool removed); |
| 95 |
| 118 private: | 96 private: |
| 119 virtual ~MockCookieMonsterDelegate() {} | 97 virtual ~MockCookieMonsterDelegate(); |
| 120 | 98 |
| 121 std::vector<CookieNotification> changes_; | 99 std::vector<CookieNotification> changes_; |
| 122 | 100 |
| 123 DISALLOW_COPY_AND_ASSIGN(MockCookieMonsterDelegate); | 101 DISALLOW_COPY_AND_ASSIGN(MockCookieMonsterDelegate); |
| 124 }; | 102 }; |
| 125 | 103 |
| 126 // Helper to build a list of CanonicalCookie*s. | 104 // Helper to build a list of CanonicalCookie*s. |
| 127 static void AddCookieToList( | 105 void AddCookieToList( |
| 128 const std::string& key, | 106 const std::string& key, |
| 129 const std::string& cookie_line, | 107 const std::string& cookie_line, |
| 130 const base::Time& creation_time, | 108 const base::Time& creation_time, |
| 131 std::vector<net::CookieMonster::CanonicalCookie*>* out_list) { | 109 std::vector<net::CookieMonster::CanonicalCookie*>* out_list); |
| 132 | |
| 133 // Parse the cookie line. | |
| 134 net::CookieMonster::ParsedCookie pc(cookie_line); | |
| 135 EXPECT_TRUE(pc.IsValid()); | |
| 136 | |
| 137 // This helper is simplistic in interpreting a parsed cookie, in order to | |
| 138 // avoid duplicated CookieMonster's CanonPath() and CanonExpiration() | |
| 139 // functions. Would be nice to export them, and re-use here. | |
| 140 EXPECT_FALSE(pc.HasMaxAge()); | |
| 141 EXPECT_TRUE(pc.HasPath()); | |
| 142 base::Time cookie_expires = pc.HasExpires() ? | |
| 143 net::CookieMonster::ParseCookieTime(pc.Expires()) : base::Time(); | |
| 144 std::string cookie_path = pc.Path(); | |
| 145 | |
| 146 scoped_ptr<net::CookieMonster::CanonicalCookie> cookie( | |
| 147 new net::CookieMonster::CanonicalCookie( | |
| 148 pc.Name(), pc.Value(), key, cookie_path, | |
| 149 pc.IsSecure(), pc.IsHttpOnly(), | |
| 150 creation_time, creation_time, | |
| 151 !cookie_expires.is_null(), | |
| 152 cookie_expires)); | |
| 153 | |
| 154 out_list->push_back(cookie.release()); | |
| 155 } | |
| 156 | 110 |
| 157 // Just act like a backing database. Keep cookie information from | 111 // Just act like a backing database. Keep cookie information from |
| 158 // Add/Update/Delete and regurgitate it when Load is called. | 112 // Add/Update/Delete and regurgitate it when Load is called. |
| 159 class MockSimplePersistentCookieStore | 113 class MockSimplePersistentCookieStore |
| 160 : public net::CookieMonster::PersistentCookieStore { | 114 : public net::CookieMonster::PersistentCookieStore { |
| 115 public: |
| 116 MockSimplePersistentCookieStore(); |
| 117 virtual ~MockSimplePersistentCookieStore(); |
| 118 |
| 119 virtual bool Load( |
| 120 std::vector<net::CookieMonster::CanonicalCookie*>* out_cookies); |
| 121 |
| 122 virtual void AddCookie( |
| 123 const net::CookieMonster::CanonicalCookie& cookie); |
| 124 |
| 125 virtual void UpdateCookieAccessTime( |
| 126 const net::CookieMonster::CanonicalCookie& cookie); |
| 127 |
| 128 virtual void DeleteCookie( |
| 129 const net::CookieMonster::CanonicalCookie& cookie); |
| 130 |
| 131 virtual void Flush(Task* completion_task); |
| 132 |
| 133 virtual void SetClearLocalStateOnExit(bool clear_local_state); |
| 134 |
| 161 private: | 135 private: |
| 162 typedef std::map<int64, net::CookieMonster::CanonicalCookie> | 136 typedef std::map<int64, net::CookieMonster::CanonicalCookie> |
| 163 CanonicalCookieMap; | 137 CanonicalCookieMap; |
| 164 | 138 |
| 165 public: | |
| 166 virtual bool Load( | |
| 167 std::vector<net::CookieMonster::CanonicalCookie*>* out_cookies) { | |
| 168 for (CanonicalCookieMap::const_iterator it = cookies_.begin(); | |
| 169 it != cookies_.end(); it++) | |
| 170 out_cookies->push_back( | |
| 171 new net::CookieMonster::CanonicalCookie(it->second)); | |
| 172 return true; | |
| 173 } | |
| 174 | |
| 175 virtual void AddCookie( | |
| 176 const net::CookieMonster::CanonicalCookie& cookie) { | |
| 177 int64 creation_time = cookie.CreationDate().ToInternalValue(); | |
| 178 EXPECT_TRUE(cookies_.find(creation_time) == cookies_.end()); | |
| 179 cookies_[creation_time] = cookie; | |
| 180 } | |
| 181 | |
| 182 virtual void UpdateCookieAccessTime( | |
| 183 const net::CookieMonster::CanonicalCookie& cookie) { | |
| 184 int64 creation_time = cookie.CreationDate().ToInternalValue(); | |
| 185 ASSERT_TRUE(cookies_.find(creation_time) != cookies_.end()); | |
| 186 cookies_[creation_time].SetLastAccessDate(base::Time::Now()); | |
| 187 } | |
| 188 | |
| 189 virtual void DeleteCookie( | |
| 190 const net::CookieMonster::CanonicalCookie& cookie) { | |
| 191 int64 creation_time = cookie.CreationDate().ToInternalValue(); | |
| 192 CanonicalCookieMap::iterator it = cookies_.find(creation_time); | |
| 193 ASSERT_TRUE(it != cookies_.end()); | |
| 194 cookies_.erase(it); | |
| 195 } | |
| 196 | |
| 197 virtual void Flush(Task* completion_task) { | |
| 198 if (completion_task) | |
| 199 MessageLoop::current()->PostTask(FROM_HERE, completion_task); | |
| 200 } | |
| 201 | |
| 202 virtual void SetClearLocalStateOnExit(bool clear_local_state) {} | |
| 203 | |
| 204 private: | |
| 205 CanonicalCookieMap cookies_; | 139 CanonicalCookieMap cookies_; |
| 206 }; | 140 }; |
| 207 | 141 |
| 208 // Helper function for creating a CookieMonster backed by a | 142 // Helper function for creating a CookieMonster backed by a |
| 209 // MockSimplePersistentCookieStore for garbage collection testing. | 143 // MockSimplePersistentCookieStore for garbage collection testing. |
| 210 // | 144 // |
| 211 // Fill the store through import with |num_cookies| cookies, |num_old_cookies| | 145 // Fill the store through import with |num_cookies| cookies, |num_old_cookies| |
| 212 // with access time Now()-days_old, the rest with access time Now(). | 146 // with access time Now()-days_old, the rest with access time Now(). |
| 213 // Do two SetCookies(). Return whether each of the two SetCookies() took | 147 // Do two SetCookies(). Return whether each of the two SetCookies() took |
| 214 // longer than |gc_perf_micros| to complete, and how many cookie were | 148 // longer than |gc_perf_micros| to complete, and how many cookie were |
| 215 // left in the store afterwards. | 149 // left in the store afterwards. |
| 216 static net::CookieMonster* CreateMonsterFromStoreForGC( | 150 net::CookieMonster* CreateMonsterFromStoreForGC( |
| 217 int num_cookies, | 151 int num_cookies, |
| 218 int num_old_cookies, | 152 int num_old_cookies, |
| 219 int days_old) { | 153 int days_old); |
| 220 base::Time current(base::Time::Now()); | |
| 221 base::Time past_creation(base::Time::Now() - base::TimeDelta::FromDays(1000)); | |
| 222 scoped_refptr<MockSimplePersistentCookieStore> store( | |
| 223 new MockSimplePersistentCookieStore); | |
| 224 // Must expire to be persistent | |
| 225 for (int i = 0; i < num_old_cookies; i++) { | |
| 226 net::CookieMonster::CanonicalCookie cc( | |
| 227 "a", "1", StringPrintf("h%05d.izzle", i), "/path", false, false, | |
| 228 past_creation + base::TimeDelta::FromMicroseconds(i), | |
| 229 current - base::TimeDelta::FromDays(days_old), | |
| 230 true, current + base::TimeDelta::FromDays(30)); | |
| 231 store->AddCookie(cc); | |
| 232 } | |
| 233 for (int i = num_old_cookies; i < num_cookies; i++) { | |
| 234 net::CookieMonster::CanonicalCookie cc( | |
| 235 "a", "1", StringPrintf("h%05d.izzle", i), "/path", false, false, | |
| 236 past_creation + base::TimeDelta::FromMicroseconds(i), current, | |
| 237 true, current + base::TimeDelta::FromDays(30)); | |
| 238 store->AddCookie(cc); | |
| 239 } | |
| 240 | 154 |
| 241 return new net::CookieMonster(store, NULL); | 155 } // namespace net |
| 242 } | |
| 243 | |
| 244 } // namespace | |
| OLD | NEW |