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