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