OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/base/cookie_monster_store_test.h" |
| 6 |
| 7 #include "base/message_loop.h" |
| 8 #include "base/stringprintf.h" |
| 9 #include "base/time.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace net { |
| 13 |
| 14 MockPersistentCookieStore::MockPersistentCookieStore() |
| 15 : load_return_value_(true) { |
| 16 } |
| 17 |
| 18 MockPersistentCookieStore::~MockPersistentCookieStore() {} |
| 19 |
| 20 void MockPersistentCookieStore::SetLoadExpectation( |
| 21 bool return_value, |
| 22 const std::vector<net::CookieMonster::CanonicalCookie*>& result) { |
| 23 load_return_value_ = return_value; |
| 24 load_result_ = result; |
| 25 } |
| 26 |
| 27 bool MockPersistentCookieStore::Load( |
| 28 std::vector<net::CookieMonster::CanonicalCookie*>* out_cookies) { |
| 29 bool ok = load_return_value_; |
| 30 if (ok) |
| 31 *out_cookies = load_result_; |
| 32 return ok; |
| 33 } |
| 34 |
| 35 void MockPersistentCookieStore::AddCookie( |
| 36 const net::CookieMonster::CanonicalCookie& cookie) { |
| 37 commands_.push_back( |
| 38 CookieStoreCommand(CookieStoreCommand::ADD, cookie)); |
| 39 } |
| 40 |
| 41 void MockPersistentCookieStore::UpdateCookieAccessTime( |
| 42 const net::CookieMonster::CanonicalCookie& cookie) { |
| 43 commands_.push_back(CookieStoreCommand( |
| 44 CookieStoreCommand::UPDATE_ACCESS_TIME, cookie)); |
| 45 } |
| 46 |
| 47 void MockPersistentCookieStore::DeleteCookie( |
| 48 const net::CookieMonster::CanonicalCookie& cookie) { |
| 49 commands_.push_back( |
| 50 CookieStoreCommand(CookieStoreCommand::REMOVE, cookie)); |
| 51 } |
| 52 |
| 53 void MockPersistentCookieStore::Flush(Task* completion_task) { |
| 54 if (completion_task) |
| 55 MessageLoop::current()->PostTask(FROM_HERE, completion_task); |
| 56 } |
| 57 |
| 58 // No files are created so nothing to clear either |
| 59 void |
| 60 MockPersistentCookieStore::SetClearLocalStateOnExit(bool clear_local_state) { |
| 61 } |
| 62 |
| 63 MockCookieMonsterDelegate::MockCookieMonsterDelegate() {} |
| 64 |
| 65 void MockCookieMonsterDelegate::OnCookieChanged( |
| 66 const net::CookieMonster::CanonicalCookie& cookie, |
| 67 bool removed) { |
| 68 CookieNotification notification(cookie, removed); |
| 69 changes_.push_back(notification); |
| 70 } |
| 71 |
| 72 MockCookieMonsterDelegate::~MockCookieMonsterDelegate() {} |
| 73 |
| 74 void AddCookieToList( |
| 75 const std::string& key, |
| 76 const std::string& cookie_line, |
| 77 const base::Time& creation_time, |
| 78 std::vector<net::CookieMonster::CanonicalCookie*>* out_list) { |
| 79 |
| 80 // Parse the cookie line. |
| 81 net::CookieMonster::ParsedCookie pc(cookie_line); |
| 82 EXPECT_TRUE(pc.IsValid()); |
| 83 |
| 84 // This helper is simplistic in interpreting a parsed cookie, in order to |
| 85 // avoid duplicated CookieMonster's CanonPath() and CanonExpiration() |
| 86 // functions. Would be nice to export them, and re-use here. |
| 87 EXPECT_FALSE(pc.HasMaxAge()); |
| 88 EXPECT_TRUE(pc.HasPath()); |
| 89 base::Time cookie_expires = pc.HasExpires() ? |
| 90 net::CookieMonster::ParseCookieTime(pc.Expires()) : base::Time(); |
| 91 std::string cookie_path = pc.Path(); |
| 92 |
| 93 scoped_ptr<net::CookieMonster::CanonicalCookie> cookie( |
| 94 new net::CookieMonster::CanonicalCookie( |
| 95 pc.Name(), pc.Value(), key, cookie_path, |
| 96 pc.IsSecure(), pc.IsHttpOnly(), |
| 97 creation_time, creation_time, |
| 98 !cookie_expires.is_null(), |
| 99 cookie_expires)); |
| 100 |
| 101 out_list->push_back(cookie.release()); |
| 102 } |
| 103 |
| 104 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore() {} |
| 105 |
| 106 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {} |
| 107 |
| 108 bool MockSimplePersistentCookieStore::Load( |
| 109 std::vector<net::CookieMonster::CanonicalCookie*>* out_cookies) { |
| 110 for (CanonicalCookieMap::const_iterator it = cookies_.begin(); |
| 111 it != cookies_.end(); it++) |
| 112 out_cookies->push_back( |
| 113 new net::CookieMonster::CanonicalCookie(it->second)); |
| 114 return true; |
| 115 } |
| 116 |
| 117 void MockSimplePersistentCookieStore::AddCookie( |
| 118 const net::CookieMonster::CanonicalCookie& cookie) { |
| 119 int64 creation_time = cookie.CreationDate().ToInternalValue(); |
| 120 EXPECT_TRUE(cookies_.find(creation_time) == cookies_.end()); |
| 121 cookies_[creation_time] = cookie; |
| 122 } |
| 123 |
| 124 void MockSimplePersistentCookieStore::UpdateCookieAccessTime( |
| 125 const net::CookieMonster::CanonicalCookie& cookie) { |
| 126 int64 creation_time = cookie.CreationDate().ToInternalValue(); |
| 127 ASSERT_TRUE(cookies_.find(creation_time) != cookies_.end()); |
| 128 cookies_[creation_time].SetLastAccessDate(base::Time::Now()); |
| 129 } |
| 130 |
| 131 void MockSimplePersistentCookieStore::DeleteCookie( |
| 132 const net::CookieMonster::CanonicalCookie& cookie) { |
| 133 int64 creation_time = cookie.CreationDate().ToInternalValue(); |
| 134 CanonicalCookieMap::iterator it = cookies_.find(creation_time); |
| 135 ASSERT_TRUE(it != cookies_.end()); |
| 136 cookies_.erase(it); |
| 137 } |
| 138 |
| 139 void MockSimplePersistentCookieStore::Flush(Task* completion_task) { |
| 140 if (completion_task) |
| 141 MessageLoop::current()->PostTask(FROM_HERE, completion_task); |
| 142 } |
| 143 |
| 144 void MockSimplePersistentCookieStore::SetClearLocalStateOnExit( |
| 145 bool clear_local_state) { |
| 146 } |
| 147 |
| 148 net::CookieMonster* CreateMonsterFromStoreForGC( |
| 149 int num_cookies, |
| 150 int num_old_cookies, |
| 151 int days_old) { |
| 152 base::Time current(base::Time::Now()); |
| 153 base::Time past_creation(base::Time::Now() - base::TimeDelta::FromDays(1000)); |
| 154 scoped_refptr<MockSimplePersistentCookieStore> store( |
| 155 new MockSimplePersistentCookieStore); |
| 156 // Must expire to be persistent |
| 157 for (int i = 0; i < num_old_cookies; i++) { |
| 158 net::CookieMonster::CanonicalCookie cc( |
| 159 "a", "1", base::StringPrintf("h%05d.izzle", i), "/path", false, false, |
| 160 past_creation + base::TimeDelta::FromMicroseconds(i), |
| 161 current - base::TimeDelta::FromDays(days_old), |
| 162 true, current + base::TimeDelta::FromDays(30)); |
| 163 store->AddCookie(cc); |
| 164 } |
| 165 for (int i = num_old_cookies; i < num_cookies; i++) { |
| 166 net::CookieMonster::CanonicalCookie cc( |
| 167 "a", "1", base::StringPrintf("h%05d.izzle", i), "/path", false, false, |
| 168 past_creation + base::TimeDelta::FromMicroseconds(i), current, |
| 169 true, current + base::TimeDelta::FromDays(30)); |
| 170 store->AddCookie(cc); |
| 171 } |
| 172 |
| 173 return new net::CookieMonster(store, NULL); |
| 174 } |
| 175 |
| 176 } // namespace net |
OLD | NEW |