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 "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace net { | 14 namespace net { |
15 LoadedCallbackTask::LoadedCallbackTask(LoadedCallback loaded_callback, | 15 LoadedCallbackTask::LoadedCallbackTask(LoadedCallback loaded_callback, |
16 std::vector<CookieMonster::CanonicalCookie*> cookies) | 16 std::vector<CookieMonster::CanonicalCookie*> cookies) |
17 : loaded_callback_(loaded_callback), | 17 : loaded_callback_(loaded_callback), |
18 cookies_(cookies) { | 18 cookies_(cookies) { |
19 } | 19 } |
20 | 20 |
21 LoadedCallbackTask::~LoadedCallbackTask() {} | 21 LoadedCallbackTask::~LoadedCallbackTask() {} |
22 | 22 |
23 MockPersistentCookieStore::MockPersistentCookieStore() | 23 MockPersistentCookieStore::MockPersistentCookieStore() |
24 : load_return_value_(true), | 24 : load_return_value_(true), |
25 loaded_(false) { | 25 loaded_(false) { |
26 } | 26 } |
27 | 27 |
28 MockPersistentCookieStore::~MockPersistentCookieStore() {} | |
29 | |
30 void MockPersistentCookieStore::SetLoadExpectation( | 28 void MockPersistentCookieStore::SetLoadExpectation( |
31 bool return_value, | 29 bool return_value, |
32 const std::vector<CookieMonster::CanonicalCookie*>& result) { | 30 const std::vector<CookieMonster::CanonicalCookie*>& result) { |
33 load_return_value_ = return_value; | 31 load_return_value_ = return_value; |
34 load_result_ = result; | 32 load_result_ = result; |
35 } | 33 } |
36 | 34 |
37 void MockPersistentCookieStore::Load(const LoadedCallback& loaded_callback) { | 35 void MockPersistentCookieStore::Load(const LoadedCallback& loaded_callback) { |
38 std::vector<CookieMonster::CanonicalCookie*> out_cookies; | 36 std::vector<CookieMonster::CanonicalCookie*> out_cookies; |
39 if (load_return_value_) { | 37 if (load_return_value_) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 void MockPersistentCookieStore::Flush(const base::Closure& callback) { | 76 void MockPersistentCookieStore::Flush(const base::Closure& callback) { |
79 if (!callback.is_null()) | 77 if (!callback.is_null()) |
80 MessageLoop::current()->PostTask(FROM_HERE, callback); | 78 MessageLoop::current()->PostTask(FROM_HERE, callback); |
81 } | 79 } |
82 | 80 |
83 // No files are created so nothing to clear either | 81 // No files are created so nothing to clear either |
84 void | 82 void |
85 MockPersistentCookieStore::SetClearLocalStateOnExit(bool clear_local_state) { | 83 MockPersistentCookieStore::SetClearLocalStateOnExit(bool clear_local_state) { |
86 } | 84 } |
87 | 85 |
| 86 MockPersistentCookieStore::~MockPersistentCookieStore() {} |
| 87 |
88 MockCookieMonsterDelegate::MockCookieMonsterDelegate() {} | 88 MockCookieMonsterDelegate::MockCookieMonsterDelegate() {} |
89 | 89 |
90 void MockCookieMonsterDelegate::OnCookieChanged( | 90 void MockCookieMonsterDelegate::OnCookieChanged( |
91 const CookieMonster::CanonicalCookie& cookie, | 91 const CookieMonster::CanonicalCookie& cookie, |
92 bool removed, | 92 bool removed, |
93 CookieMonster::Delegate::ChangeCause cause) { | 93 CookieMonster::Delegate::ChangeCause cause) { |
94 CookieNotification notification(cookie, removed); | 94 CookieNotification notification(cookie, removed); |
95 changes_.push_back(notification); | 95 changes_.push_back(notification); |
96 } | 96 } |
97 | 97 |
98 MockCookieMonsterDelegate::~MockCookieMonsterDelegate() {} | 98 MockCookieMonsterDelegate::~MockCookieMonsterDelegate() {} |
99 | 99 |
100 void AddCookieToList( | |
101 const std::string& key, | |
102 const std::string& cookie_line, | |
103 const base::Time& creation_time, | |
104 std::vector<CookieMonster::CanonicalCookie*>* out_list) { | |
105 scoped_ptr<CookieMonster::CanonicalCookie> cookie( | |
106 new CookieMonster::CanonicalCookie( | |
107 BuildCanonicalCookie(key, cookie_line, creation_time))); | |
108 | |
109 out_list->push_back(cookie.release()); | |
110 } | |
111 | |
112 CookieMonster::CanonicalCookie BuildCanonicalCookie( | 100 CookieMonster::CanonicalCookie BuildCanonicalCookie( |
113 const std::string& key, | 101 const std::string& key, |
114 const std::string& cookie_line, | 102 const std::string& cookie_line, |
115 const base::Time& creation_time) { | 103 const base::Time& creation_time) { |
116 | 104 |
117 // Parse the cookie line. | 105 // Parse the cookie line. |
118 CookieMonster::ParsedCookie pc(cookie_line); | 106 CookieMonster::ParsedCookie pc(cookie_line); |
119 EXPECT_TRUE(pc.IsValid()); | 107 EXPECT_TRUE(pc.IsValid()); |
120 | 108 |
121 // This helper is simplistic in interpreting a parsed cookie, in order to | 109 // This helper is simplistic in interpreting a parsed cookie, in order to |
122 // avoid duplicated CookieMonster's CanonPath() and CanonExpiration() | 110 // avoid duplicated CookieMonster's CanonPath() and CanonExpiration() |
123 // functions. Would be nice to export them, and re-use here. | 111 // functions. Would be nice to export them, and re-use here. |
124 EXPECT_FALSE(pc.HasMaxAge()); | 112 EXPECT_FALSE(pc.HasMaxAge()); |
125 EXPECT_TRUE(pc.HasPath()); | 113 EXPECT_TRUE(pc.HasPath()); |
126 base::Time cookie_expires = pc.HasExpires() ? | 114 base::Time cookie_expires = pc.HasExpires() ? |
127 CookieMonster::ParseCookieTime(pc.Expires()) : base::Time(); | 115 CookieMonster::ParseCookieTime(pc.Expires()) : base::Time(); |
128 std::string cookie_path = pc.Path(); | 116 std::string cookie_path = pc.Path(); |
129 | 117 |
130 return CookieMonster::CanonicalCookie( | 118 return CookieMonster::CanonicalCookie( |
131 GURL(), pc.Name(), pc.Value(), key, cookie_path, | 119 GURL(), pc.Name(), pc.Value(), key, cookie_path, |
132 pc.MACKey(), pc.MACAlgorithm(), | 120 pc.MACKey(), pc.MACAlgorithm(), |
133 creation_time, creation_time, cookie_expires, | 121 creation_time, creation_time, cookie_expires, |
134 pc.IsSecure(), pc.IsHttpOnly(), | 122 pc.IsSecure(), pc.IsHttpOnly(), |
135 !cookie_expires.is_null(), !cookie_expires.is_null()); | 123 !cookie_expires.is_null(), !cookie_expires.is_null()); |
136 } | 124 } |
137 | 125 |
| 126 void AddCookieToList( |
| 127 const std::string& key, |
| 128 const std::string& cookie_line, |
| 129 const base::Time& creation_time, |
| 130 std::vector<CookieMonster::CanonicalCookie*>* out_list) { |
| 131 scoped_ptr<CookieMonster::CanonicalCookie> cookie( |
| 132 new CookieMonster::CanonicalCookie( |
| 133 BuildCanonicalCookie(key, cookie_line, creation_time))); |
| 134 |
| 135 out_list->push_back(cookie.release()); |
| 136 } |
| 137 |
138 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore() | 138 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore() |
139 : loaded_(false) {} | 139 : loaded_(false) { |
140 | 140 } |
141 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {} | |
142 | 141 |
143 void MockSimplePersistentCookieStore::Load( | 142 void MockSimplePersistentCookieStore::Load( |
144 const LoadedCallback& loaded_callback) { | 143 const LoadedCallback& loaded_callback) { |
145 std::vector<CookieMonster::CanonicalCookie*> out_cookies; | 144 std::vector<CookieMonster::CanonicalCookie*> out_cookies; |
146 | 145 |
147 for (CanonicalCookieMap::const_iterator it = cookies_.begin(); | 146 for (CanonicalCookieMap::const_iterator it = cookies_.begin(); |
148 it != cookies_.end(); it++) | 147 it != cookies_.end(); it++) |
149 out_cookies.push_back( | 148 out_cookies.push_back( |
150 new CookieMonster::CanonicalCookie(it->second)); | 149 new CookieMonster::CanonicalCookie(it->second)); |
151 | 150 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 CookieMonster::CanonicalCookie cc( | 220 CookieMonster::CanonicalCookie cc( |
222 GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i), "/path", | 221 GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i), "/path", |
223 mac_key, mac_algorithm, creation_time, expiration_time, | 222 mac_key, mac_algorithm, creation_time, expiration_time, |
224 last_access_time, false, false, true, true); | 223 last_access_time, false, false, true, true); |
225 store->AddCookie(cc); | 224 store->AddCookie(cc); |
226 } | 225 } |
227 | 226 |
228 return new CookieMonster(store, NULL); | 227 return new CookieMonster(store, NULL); |
229 } | 228 } |
230 | 229 |
| 230 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {} |
| 231 |
231 } // namespace net | 232 } // namespace net |
OLD | NEW |