| 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 #include "net/cookies/cookie_monster_store_test.h" | 5 #include "net/cookies/cookie_monster_store_test.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 11 #include "net/cookies/cookie_constants.h" | 11 #include "net/cookies/cookie_constants.h" |
| 12 #include "net/cookies/cookie_util.h" | 12 #include "net/cookies/cookie_util.h" |
| 13 #include "net/cookies/parsed_cookie.h" | 13 #include "net/cookies/parsed_cookie.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 16 | 16 |
| 17 namespace net { | 17 namespace net { |
| 18 LoadedCallbackTask::LoadedCallbackTask(LoadedCallback loaded_callback, | 18 LoadedCallbackTask::LoadedCallbackTask(LoadedCallback loaded_callback, |
| 19 std::vector<CanonicalCookie*> cookies) | 19 std::vector<CanonicalCookie*> cookies) |
| 20 : loaded_callback_(loaded_callback), | 20 : loaded_callback_(loaded_callback), cookies_(cookies) { |
| 21 cookies_(cookies) { | |
| 22 } | 21 } |
| 23 | 22 |
| 24 LoadedCallbackTask::~LoadedCallbackTask() {} | 23 LoadedCallbackTask::~LoadedCallbackTask() { |
| 24 } |
| 25 | 25 |
| 26 MockPersistentCookieStore::MockPersistentCookieStore() | 26 MockPersistentCookieStore::MockPersistentCookieStore() |
| 27 : load_return_value_(true), | 27 : load_return_value_(true), loaded_(false) { |
| 28 loaded_(false) { | |
| 29 } | 28 } |
| 30 | 29 |
| 31 void MockPersistentCookieStore::SetLoadExpectation( | 30 void MockPersistentCookieStore::SetLoadExpectation( |
| 32 bool return_value, | 31 bool return_value, |
| 33 const std::vector<CanonicalCookie*>& result) { | 32 const std::vector<CanonicalCookie*>& result) { |
| 34 load_return_value_ = return_value; | 33 load_return_value_ = return_value; |
| 35 load_result_ = result; | 34 load_result_ = result; |
| 36 } | 35 } |
| 37 | 36 |
| 38 void MockPersistentCookieStore::Load(const LoadedCallback& loaded_callback) { | 37 void MockPersistentCookieStore::Load(const LoadedCallback& loaded_callback) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 55 } else { | 54 } else { |
| 56 base::MessageLoop::current()->PostTask( | 55 base::MessageLoop::current()->PostTask( |
| 57 FROM_HERE, | 56 FROM_HERE, |
| 58 base::Bind(&LoadedCallbackTask::Run, | 57 base::Bind(&LoadedCallbackTask::Run, |
| 59 new LoadedCallbackTask(loaded_callback, | 58 new LoadedCallbackTask(loaded_callback, |
| 60 std::vector<CanonicalCookie*>()))); | 59 std::vector<CanonicalCookie*>()))); |
| 61 } | 60 } |
| 62 } | 61 } |
| 63 | 62 |
| 64 void MockPersistentCookieStore::AddCookie(const CanonicalCookie& cookie) { | 63 void MockPersistentCookieStore::AddCookie(const CanonicalCookie& cookie) { |
| 65 commands_.push_back( | 64 commands_.push_back(CookieStoreCommand(CookieStoreCommand::ADD, cookie)); |
| 66 CookieStoreCommand(CookieStoreCommand::ADD, cookie)); | |
| 67 } | 65 } |
| 68 | 66 |
| 69 void MockPersistentCookieStore::UpdateCookieAccessTime( | 67 void MockPersistentCookieStore::UpdateCookieAccessTime( |
| 70 const CanonicalCookie& cookie) { | 68 const CanonicalCookie& cookie) { |
| 71 commands_.push_back(CookieStoreCommand( | 69 commands_.push_back( |
| 72 CookieStoreCommand::UPDATE_ACCESS_TIME, cookie)); | 70 CookieStoreCommand(CookieStoreCommand::UPDATE_ACCESS_TIME, cookie)); |
| 73 } | 71 } |
| 74 | 72 |
| 75 void MockPersistentCookieStore::DeleteCookie(const CanonicalCookie& cookie) { | 73 void MockPersistentCookieStore::DeleteCookie(const CanonicalCookie& cookie) { |
| 76 commands_.push_back( | 74 commands_.push_back(CookieStoreCommand(CookieStoreCommand::REMOVE, cookie)); |
| 77 CookieStoreCommand(CookieStoreCommand::REMOVE, cookie)); | |
| 78 } | 75 } |
| 79 | 76 |
| 80 void MockPersistentCookieStore::Flush(const base::Closure& callback) { | 77 void MockPersistentCookieStore::Flush(const base::Closure& callback) { |
| 81 if (!callback.is_null()) | 78 if (!callback.is_null()) |
| 82 base::MessageLoop::current()->PostTask(FROM_HERE, callback); | 79 base::MessageLoop::current()->PostTask(FROM_HERE, callback); |
| 83 } | 80 } |
| 84 | 81 |
| 85 void MockPersistentCookieStore::SetForceKeepSessionState() { | 82 void MockPersistentCookieStore::SetForceKeepSessionState() { |
| 86 } | 83 } |
| 87 | 84 |
| 88 MockPersistentCookieStore::~MockPersistentCookieStore() {} | 85 MockPersistentCookieStore::~MockPersistentCookieStore() { |
| 86 } |
| 89 | 87 |
| 90 MockCookieMonsterDelegate::MockCookieMonsterDelegate() {} | 88 MockCookieMonsterDelegate::MockCookieMonsterDelegate() { |
| 89 } |
| 91 | 90 |
| 92 void MockCookieMonsterDelegate::OnCookieChanged( | 91 void MockCookieMonsterDelegate::OnCookieChanged( |
| 93 const CanonicalCookie& cookie, | 92 const CanonicalCookie& cookie, |
| 94 bool removed, | 93 bool removed, |
| 95 CookieMonster::Delegate::ChangeCause cause) { | 94 CookieMonster::Delegate::ChangeCause cause) { |
| 96 CookieNotification notification(cookie, removed); | 95 CookieNotification notification(cookie, removed); |
| 97 changes_.push_back(notification); | 96 changes_.push_back(notification); |
| 98 } | 97 } |
| 99 | 98 |
| 100 void MockCookieMonsterDelegate::OnLoaded() {} | 99 void MockCookieMonsterDelegate::OnLoaded() { |
| 100 } |
| 101 | 101 |
| 102 MockCookieMonsterDelegate::~MockCookieMonsterDelegate() {} | 102 MockCookieMonsterDelegate::~MockCookieMonsterDelegate() { |
| 103 } |
| 103 | 104 |
| 104 CanonicalCookie BuildCanonicalCookie(const std::string& key, | 105 CanonicalCookie BuildCanonicalCookie(const std::string& key, |
| 105 const std::string& cookie_line, | 106 const std::string& cookie_line, |
| 106 const base::Time& creation_time) { | 107 const base::Time& creation_time) { |
| 107 | |
| 108 // Parse the cookie line. | 108 // Parse the cookie line. |
| 109 ParsedCookie pc(cookie_line); | 109 ParsedCookie pc(cookie_line); |
| 110 EXPECT_TRUE(pc.IsValid()); | 110 EXPECT_TRUE(pc.IsValid()); |
| 111 | 111 |
| 112 // This helper is simplistic in interpreting a parsed cookie, in order to | 112 // This helper is simplistic in interpreting a parsed cookie, in order to |
| 113 // avoid duplicated CookieMonster's CanonPath() and CanonExpiration() | 113 // avoid duplicated CookieMonster's CanonPath() and CanonExpiration() |
| 114 // functions. Would be nice to export them, and re-use here. | 114 // functions. Would be nice to export them, and re-use here. |
| 115 EXPECT_FALSE(pc.HasMaxAge()); | 115 EXPECT_FALSE(pc.HasMaxAge()); |
| 116 EXPECT_TRUE(pc.HasPath()); | 116 EXPECT_TRUE(pc.HasPath()); |
| 117 base::Time cookie_expires = pc.HasExpires() ? | 117 base::Time cookie_expires = pc.HasExpires() |
| 118 cookie_util::ParseCookieTime(pc.Expires()) : base::Time(); | 118 ? cookie_util::ParseCookieTime(pc.Expires()) |
| 119 : base::Time(); |
| 119 std::string cookie_path = pc.Path(); | 120 std::string cookie_path = pc.Path(); |
| 120 | 121 |
| 121 return CanonicalCookie( | 122 return CanonicalCookie(GURL(), pc.Name(), pc.Value(), key, cookie_path, |
| 122 GURL(), pc.Name(), pc.Value(), key, cookie_path, | 123 creation_time, cookie_expires, creation_time, |
| 123 creation_time, cookie_expires, creation_time, | 124 pc.IsSecure(), pc.IsHttpOnly(), pc.Priority()); |
| 124 pc.IsSecure(), pc.IsHttpOnly(), pc.Priority()); | |
| 125 } | 125 } |
| 126 | 126 |
| 127 void AddCookieToList( | 127 void AddCookieToList(const std::string& key, |
| 128 const std::string& key, | 128 const std::string& cookie_line, |
| 129 const std::string& cookie_line, | 129 const base::Time& creation_time, |
| 130 const base::Time& creation_time, | 130 std::vector<CanonicalCookie*>* out_list) { |
| 131 std::vector<CanonicalCookie*>* out_list) { | 131 scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie( |
| 132 scoped_ptr<CanonicalCookie> cookie( | 132 BuildCanonicalCookie(key, cookie_line, creation_time))); |
| 133 new CanonicalCookie( | |
| 134 BuildCanonicalCookie(key, cookie_line, creation_time))); | |
| 135 | 133 |
| 136 out_list->push_back(cookie.release()); | 134 out_list->push_back(cookie.release()); |
| 137 } | 135 } |
| 138 | 136 |
| 139 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore() | 137 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore() |
| 140 : loaded_(false) { | 138 : loaded_(false) { |
| 141 } | 139 } |
| 142 | 140 |
| 143 void MockSimplePersistentCookieStore::Load( | 141 void MockSimplePersistentCookieStore::Load( |
| 144 const LoadedCallback& loaded_callback) { | 142 const LoadedCallback& loaded_callback) { |
| 145 std::vector<CanonicalCookie*> out_cookies; | 143 std::vector<CanonicalCookie*> out_cookies; |
| 146 | 144 |
| 147 for (CanonicalCookieMap::const_iterator it = cookies_.begin(); | 145 for (CanonicalCookieMap::const_iterator it = cookies_.begin(); |
| 148 it != cookies_.end(); it++) | 146 it != cookies_.end(); it++) |
| 149 out_cookies.push_back(new CanonicalCookie(it->second)); | 147 out_cookies.push_back(new CanonicalCookie(it->second)); |
| 150 | 148 |
| 151 base::MessageLoop::current()->PostTask( | 149 base::MessageLoop::current()->PostTask( |
| 152 FROM_HERE, | 150 FROM_HERE, |
| 153 base::Bind(&LoadedCallbackTask::Run, | 151 base::Bind(&LoadedCallbackTask::Run, |
| 154 new LoadedCallbackTask(loaded_callback, out_cookies))); | 152 new LoadedCallbackTask(loaded_callback, out_cookies))); |
| 155 loaded_ = true; | 153 loaded_ = true; |
| 156 } | 154 } |
| 157 | 155 |
| 158 void MockSimplePersistentCookieStore::LoadCookiesForKey(const std::string& key, | 156 void MockSimplePersistentCookieStore::LoadCookiesForKey( |
| 157 const std::string& key, |
| 159 const LoadedCallback& loaded_callback) { | 158 const LoadedCallback& loaded_callback) { |
| 160 if (!loaded_) { | 159 if (!loaded_) { |
| 161 Load(loaded_callback); | 160 Load(loaded_callback); |
| 162 } else { | 161 } else { |
| 163 base::MessageLoop::current()->PostTask( | 162 base::MessageLoop::current()->PostTask( |
| 164 FROM_HERE, | 163 FROM_HERE, |
| 165 base::Bind(&LoadedCallbackTask::Run, | 164 base::Bind(&LoadedCallbackTask::Run, |
| 166 new LoadedCallbackTask(loaded_callback, | 165 new LoadedCallbackTask(loaded_callback, |
| 167 std::vector<CanonicalCookie*>()))); | 166 std::vector<CanonicalCookie*>()))); |
| 168 } | 167 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 190 } | 189 } |
| 191 | 190 |
| 192 void MockSimplePersistentCookieStore::Flush(const base::Closure& callback) { | 191 void MockSimplePersistentCookieStore::Flush(const base::Closure& callback) { |
| 193 if (!callback.is_null()) | 192 if (!callback.is_null()) |
| 194 base::MessageLoop::current()->PostTask(FROM_HERE, callback); | 193 base::MessageLoop::current()->PostTask(FROM_HERE, callback); |
| 195 } | 194 } |
| 196 | 195 |
| 197 void MockSimplePersistentCookieStore::SetForceKeepSessionState() { | 196 void MockSimplePersistentCookieStore::SetForceKeepSessionState() { |
| 198 } | 197 } |
| 199 | 198 |
| 200 CookieMonster* CreateMonsterFromStoreForGC( | 199 CookieMonster* CreateMonsterFromStoreForGC(int num_cookies, |
| 201 int num_cookies, | 200 int num_old_cookies, |
| 202 int num_old_cookies, | 201 int days_old) { |
| 203 int days_old) { | |
| 204 base::Time current(base::Time::Now()); | 202 base::Time current(base::Time::Now()); |
| 205 base::Time past_creation(base::Time::Now() - base::TimeDelta::FromDays(1000)); | 203 base::Time past_creation(base::Time::Now() - base::TimeDelta::FromDays(1000)); |
| 206 scoped_refptr<MockSimplePersistentCookieStore> store( | 204 scoped_refptr<MockSimplePersistentCookieStore> store( |
| 207 new MockSimplePersistentCookieStore); | 205 new MockSimplePersistentCookieStore); |
| 208 // Must expire to be persistent | 206 // Must expire to be persistent |
| 209 for (int i = 0; i < num_cookies; i++) { | 207 for (int i = 0; i < num_cookies; i++) { |
| 210 base::Time creation_time = | 208 base::Time creation_time = |
| 211 past_creation + base::TimeDelta::FromMicroseconds(i); | 209 past_creation + base::TimeDelta::FromMicroseconds(i); |
| 212 base::Time expiration_time = current + base::TimeDelta::FromDays(30); | 210 base::Time expiration_time = current + base::TimeDelta::FromDays(30); |
| 213 base::Time last_access_time = | 211 base::Time last_access_time = |
| 214 (i < num_old_cookies) ? current - base::TimeDelta::FromDays(days_old) : | 212 (i < num_old_cookies) ? current - base::TimeDelta::FromDays(days_old) |
| 215 current; | 213 : current; |
| 216 | 214 |
| 217 CanonicalCookie cc( | 215 CanonicalCookie cc(GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i), |
| 218 GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i), "/path", | 216 "/path", creation_time, expiration_time, |
| 219 creation_time, expiration_time, last_access_time, false, false, | 217 last_access_time, false, false, COOKIE_PRIORITY_DEFAULT); |
| 220 COOKIE_PRIORITY_DEFAULT); | |
| 221 store->AddCookie(cc); | 218 store->AddCookie(cc); |
| 222 } | 219 } |
| 223 | 220 |
| 224 return new CookieMonster(store.get(), NULL); | 221 return new CookieMonster(store.get(), NULL); |
| 225 } | 222 } |
| 226 | 223 |
| 227 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {} | 224 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() { |
| 225 } |
| 228 | 226 |
| 229 } // namespace net | 227 } // namespace net |
| OLD | NEW |