DevTools: cut host and port from webSocketDebuggerUrl in addition to ws:// prefix
[chromium-blink-merge.git] / net / cookies / cookie_monster_store_test.cc
blobbf422eddc88b40b325106118aa8d49cb8c530aa1
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.
5 #include "net/cookies/cookie_monster_store_test.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/time/time.h"
13 #include "net/cookies/cookie_constants.h"
14 #include "net/cookies/cookie_util.h"
15 #include "net/cookies/parsed_cookie.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "url/gurl.h"
19 namespace net {
20 LoadedCallbackTask::LoadedCallbackTask(LoadedCallback loaded_callback,
21 std::vector<CanonicalCookie*> cookies)
22 : loaded_callback_(loaded_callback), cookies_(cookies) {
25 LoadedCallbackTask::~LoadedCallbackTask() {
28 MockPersistentCookieStore::MockPersistentCookieStore()
29 : load_return_value_(true), loaded_(false) {
32 void MockPersistentCookieStore::SetLoadExpectation(
33 bool return_value,
34 const std::vector<CanonicalCookie*>& result) {
35 load_return_value_ = return_value;
36 load_result_ = result;
39 void MockPersistentCookieStore::Load(const LoadedCallback& loaded_callback) {
40 std::vector<CanonicalCookie*> out_cookies;
41 if (load_return_value_) {
42 out_cookies = load_result_;
43 loaded_ = true;
45 base::ThreadTaskRunnerHandle::Get()->PostTask(
46 FROM_HERE,
47 base::Bind(&LoadedCallbackTask::Run,
48 new LoadedCallbackTask(loaded_callback, out_cookies)));
51 void MockPersistentCookieStore::LoadCookiesForKey(
52 const std::string& key,
53 const LoadedCallback& loaded_callback) {
54 if (!loaded_) {
55 Load(loaded_callback);
56 } else {
57 base::ThreadTaskRunnerHandle::Get()->PostTask(
58 FROM_HERE,
59 base::Bind(&LoadedCallbackTask::Run,
60 new LoadedCallbackTask(loaded_callback,
61 std::vector<CanonicalCookie*>())));
65 void MockPersistentCookieStore::AddCookie(const CanonicalCookie& cookie) {
66 commands_.push_back(CookieStoreCommand(CookieStoreCommand::ADD, cookie));
69 void MockPersistentCookieStore::UpdateCookieAccessTime(
70 const CanonicalCookie& cookie) {
71 commands_.push_back(
72 CookieStoreCommand(CookieStoreCommand::UPDATE_ACCESS_TIME, cookie));
75 void MockPersistentCookieStore::DeleteCookie(const CanonicalCookie& cookie) {
76 commands_.push_back(CookieStoreCommand(CookieStoreCommand::REMOVE, cookie));
79 void MockPersistentCookieStore::Flush(const base::Closure& callback) {
80 if (!callback.is_null())
81 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
84 void MockPersistentCookieStore::SetForceKeepSessionState() {
87 MockPersistentCookieStore::~MockPersistentCookieStore() {
90 MockCookieMonsterDelegate::MockCookieMonsterDelegate() {
93 void MockCookieMonsterDelegate::OnCookieChanged(
94 const CanonicalCookie& cookie,
95 bool removed,
96 CookieMonster::Delegate::ChangeCause cause) {
97 CookieNotification notification(cookie, removed);
98 changes_.push_back(notification);
101 MockCookieMonsterDelegate::~MockCookieMonsterDelegate() {
104 CanonicalCookie BuildCanonicalCookie(const std::string& key,
105 const std::string& cookie_line,
106 const base::Time& creation_time) {
107 // Parse the cookie line.
108 ParsedCookie pc(cookie_line);
109 EXPECT_TRUE(pc.IsValid());
111 // This helper is simplistic in interpreting a parsed cookie, in order to
112 // avoid duplicated CookieMonster's CanonPath() and CanonExpiration()
113 // functions. Would be nice to export them, and re-use here.
114 EXPECT_FALSE(pc.HasMaxAge());
115 EXPECT_TRUE(pc.HasPath());
116 base::Time cookie_expires = pc.HasExpires()
117 ? cookie_util::ParseCookieTime(pc.Expires())
118 : base::Time();
119 std::string cookie_path = pc.Path();
121 return CanonicalCookie(GURL(), pc.Name(), pc.Value(), key, cookie_path,
122 creation_time, cookie_expires, creation_time,
123 pc.IsSecure(), pc.IsHttpOnly(), pc.IsFirstPartyOnly(),
124 pc.Priority());
127 void AddCookieToList(const std::string& key,
128 const std::string& cookie_line,
129 const base::Time& creation_time,
130 std::vector<CanonicalCookie*>* out_list) {
131 scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie(
132 BuildCanonicalCookie(key, cookie_line, creation_time)));
134 out_list->push_back(cookie.release());
137 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore()
138 : loaded_(false) {
141 void MockSimplePersistentCookieStore::Load(
142 const LoadedCallback& loaded_callback) {
143 std::vector<CanonicalCookie*> out_cookies;
145 for (CanonicalCookieMap::const_iterator it = cookies_.begin();
146 it != cookies_.end(); it++)
147 out_cookies.push_back(new CanonicalCookie(it->second));
149 base::ThreadTaskRunnerHandle::Get()->PostTask(
150 FROM_HERE,
151 base::Bind(&LoadedCallbackTask::Run,
152 new LoadedCallbackTask(loaded_callback, out_cookies)));
153 loaded_ = true;
156 void MockSimplePersistentCookieStore::LoadCookiesForKey(
157 const std::string& key,
158 const LoadedCallback& loaded_callback) {
159 if (!loaded_) {
160 Load(loaded_callback);
161 } else {
162 base::ThreadTaskRunnerHandle::Get()->PostTask(
163 FROM_HERE,
164 base::Bind(&LoadedCallbackTask::Run,
165 new LoadedCallbackTask(loaded_callback,
166 std::vector<CanonicalCookie*>())));
170 void MockSimplePersistentCookieStore::AddCookie(const CanonicalCookie& cookie) {
171 int64 creation_time = cookie.CreationDate().ToInternalValue();
172 EXPECT_TRUE(cookies_.find(creation_time) == cookies_.end());
173 cookies_[creation_time] = cookie;
176 void MockSimplePersistentCookieStore::UpdateCookieAccessTime(
177 const CanonicalCookie& cookie) {
178 int64 creation_time = cookie.CreationDate().ToInternalValue();
179 ASSERT_TRUE(cookies_.find(creation_time) != cookies_.end());
180 cookies_[creation_time].SetLastAccessDate(base::Time::Now());
183 void MockSimplePersistentCookieStore::DeleteCookie(
184 const CanonicalCookie& cookie) {
185 int64 creation_time = cookie.CreationDate().ToInternalValue();
186 CanonicalCookieMap::iterator it = cookies_.find(creation_time);
187 ASSERT_TRUE(it != cookies_.end());
188 cookies_.erase(it);
191 void MockSimplePersistentCookieStore::Flush(const base::Closure& callback) {
192 if (!callback.is_null())
193 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
196 void MockSimplePersistentCookieStore::SetForceKeepSessionState() {
199 CookieMonster* CreateMonsterFromStoreForGC(int num_cookies,
200 int num_old_cookies,
201 int days_old) {
202 base::Time current(base::Time::Now());
203 base::Time past_creation(base::Time::Now() - base::TimeDelta::FromDays(1000));
204 scoped_refptr<MockSimplePersistentCookieStore> store(
205 new MockSimplePersistentCookieStore);
206 // Must expire to be persistent
207 for (int i = 0; i < num_cookies; i++) {
208 base::Time creation_time =
209 past_creation + base::TimeDelta::FromMicroseconds(i);
210 base::Time expiration_time = current + base::TimeDelta::FromDays(30);
211 base::Time last_access_time =
212 (i < num_old_cookies) ? current - base::TimeDelta::FromDays(days_old)
213 : current;
215 CanonicalCookie cc(GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i),
216 "/path", creation_time, expiration_time,
217 last_access_time, false, false, false,
218 COOKIE_PRIORITY_DEFAULT);
219 store->AddCookie(cc);
222 return new CookieMonster(store.get(), NULL);
225 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {
228 } // namespace net