Home | History | Annotate | Download | only in cookies
      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 {
     18 LoadedCallbackTask::LoadedCallbackTask(LoadedCallback loaded_callback,
     19                                        std::vector<CanonicalCookie*> cookies)
     20     : loaded_callback_(loaded_callback),
     21       cookies_(cookies) {
     22 }
     23 
     24 LoadedCallbackTask::~LoadedCallbackTask() {}
     25 
     26 MockPersistentCookieStore::MockPersistentCookieStore()
     27     : load_return_value_(true),
     28       loaded_(false) {
     29 }
     30 
     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 
     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 
     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 
     64 void MockPersistentCookieStore::AddCookie(const CanonicalCookie& cookie) {
     65   commands_.push_back(
     66       CookieStoreCommand(CookieStoreCommand::ADD, cookie));
     67 }
     68 
     69 void MockPersistentCookieStore::UpdateCookieAccessTime(
     70     const CanonicalCookie& cookie) {
     71   commands_.push_back(CookieStoreCommand(
     72       CookieStoreCommand::UPDATE_ACCESS_TIME, cookie));
     73 }
     74 
     75 void MockPersistentCookieStore::DeleteCookie(const CanonicalCookie& cookie) {
     76   commands_.push_back(
     77       CookieStoreCommand(CookieStoreCommand::REMOVE, cookie));
     78 }
     79 
     80 void MockPersistentCookieStore::Flush(const base::Closure& callback) {
     81   if (!callback.is_null())
     82     base::MessageLoop::current()->PostTask(FROM_HERE, callback);
     83 }
     84 
     85 void MockPersistentCookieStore::SetForceKeepSessionState() {
     86 }
     87 
     88 MockPersistentCookieStore::~MockPersistentCookieStore() {}
     89 
     90 MockCookieMonsterDelegate::MockCookieMonsterDelegate() {}
     91 
     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 
    100 MockCookieMonsterDelegate::~MockCookieMonsterDelegate() {}
    101 
    102 CanonicalCookie BuildCanonicalCookie(const std::string& key,
    103                                      const std::string& cookie_line,
    104                                      const base::Time& creation_time) {
    105 
    106   // Parse the cookie line.
    107   ParsedCookie pc(cookie_line);
    108   EXPECT_TRUE(pc.IsValid());
    109 
    110   // This helper is simplistic in interpreting a parsed cookie, in order to
    111   // avoid duplicated CookieMonster's CanonPath() and CanonExpiration()
    112   // functions. Would be nice to export them, and re-use here.
    113   EXPECT_FALSE(pc.HasMaxAge());
    114   EXPECT_TRUE(pc.HasPath());
    115   base::Time cookie_expires = pc.HasExpires() ?
    116       cookie_util::ParseCookieTime(pc.Expires()) : base::Time();
    117   std::string cookie_path = pc.Path();
    118 
    119   return CanonicalCookie(
    120       GURL(), pc.Name(), pc.Value(), key, cookie_path,
    121       creation_time, cookie_expires, creation_time,
    122       pc.IsSecure(), pc.IsHttpOnly(), pc.Priority());
    123 }
    124 
    125 void AddCookieToList(
    126     const std::string& key,
    127     const std::string& cookie_line,
    128     const base::Time& creation_time,
    129     std::vector<CanonicalCookie*>* out_list) {
    130   scoped_ptr<CanonicalCookie> cookie(
    131       new CanonicalCookie(
    132           BuildCanonicalCookie(key, cookie_line, creation_time)));
    133 
    134   out_list->push_back(cookie.release());
    135 }
    136 
    137 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore()
    138     : loaded_(false) {
    139 }
    140 
    141 void MockSimplePersistentCookieStore::Load(
    142     const LoadedCallback& loaded_callback) {
    143   std::vector<CanonicalCookie*> out_cookies;
    144 
    145   for (CanonicalCookieMap::const_iterator it = cookies_.begin();
    146        it != cookies_.end(); it++)
    147     out_cookies.push_back(new CanonicalCookie(it->second));
    148 
    149   base::MessageLoop::current()->PostTask(
    150       FROM_HERE,
    151       base::Bind(&LoadedCallbackTask::Run,
    152                  new LoadedCallbackTask(loaded_callback, out_cookies)));
    153   loaded_ = true;
    154 }
    155 
    156 void MockSimplePersistentCookieStore::LoadCookiesForKey(const std::string& key,
    157     const LoadedCallback& loaded_callback) {
    158   if (!loaded_) {
    159     Load(loaded_callback);
    160   } else {
    161     base::MessageLoop::current()->PostTask(
    162         FROM_HERE,
    163         base::Bind(&LoadedCallbackTask::Run,
    164                    new LoadedCallbackTask(loaded_callback,
    165                                           std::vector<CanonicalCookie*>())));
    166   }
    167 }
    168 
    169 void MockSimplePersistentCookieStore::AddCookie(const CanonicalCookie& cookie) {
    170   int64 creation_time = cookie.CreationDate().ToInternalValue();
    171   EXPECT_TRUE(cookies_.find(creation_time) == cookies_.end());
    172   cookies_[creation_time] = cookie;
    173 }
    174 
    175 void MockSimplePersistentCookieStore::UpdateCookieAccessTime(
    176     const CanonicalCookie& cookie) {
    177   int64 creation_time = cookie.CreationDate().ToInternalValue();
    178   ASSERT_TRUE(cookies_.find(creation_time) != cookies_.end());
    179   cookies_[creation_time].SetLastAccessDate(base::Time::Now());
    180 }
    181 
    182 void MockSimplePersistentCookieStore::DeleteCookie(
    183     const CanonicalCookie& cookie) {
    184   int64 creation_time = cookie.CreationDate().ToInternalValue();
    185   CanonicalCookieMap::iterator it = cookies_.find(creation_time);
    186   ASSERT_TRUE(it != cookies_.end());
    187   cookies_.erase(it);
    188 }
    189 
    190 void MockSimplePersistentCookieStore::Flush(const base::Closure& callback) {
    191   if (!callback.is_null())
    192     base::MessageLoop::current()->PostTask(FROM_HERE, callback);
    193 }
    194 
    195 void MockSimplePersistentCookieStore::SetForceKeepSessionState() {
    196 }
    197 
    198 CookieMonster* CreateMonsterFromStoreForGC(
    199     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;
    214 
    215     CanonicalCookie cc(
    216         GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i), "/path",
    217         creation_time, expiration_time, last_access_time, false, false,
    218         COOKIE_PRIORITY_DEFAULT);
    219     store->AddCookie(cc);
    220   }
    221 
    222   return new CookieMonster(store.get(), NULL);
    223 }
    224 
    225 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {}
    226 
    227 }  // namespace net
    228