Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(509)

Side by Side Diff: ios/net/cookies/cookie_store_ios_unittest.mm

Issue 2667753007: Divide Cookie Store IOS tests into different files (Closed)
Patch Set: rename unittest helper to test util and other renamings Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ios/net/cookies/cookie_store_ios_test_util.mm ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 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 "ios/net/cookies/cookie_store_ios.h" 5 #include "ios/net/cookies/cookie_store_ios.h"
6 6
7 #import <Foundation/Foundation.h> 7 #import <Foundation/Foundation.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h" 15 #include "base/run_loop.h"
16 #include "base/strings/sys_string_conversions.h" 16 #include "base/strings/sys_string_conversions.h"
17 #import "ios/net/cookies/cookie_store_ios_persistent.h" 17 #import "ios/net/cookies/cookie_store_ios_test_util.h"
18 #import "net/base/mac/url_conversions.h" 18 #import "net/base/mac/url_conversions.h"
19 #include "net/cookies/cookie_store_unittest.h" 19 #include "net/cookies/cookie_store_unittest.h"
20 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
21 21
22 namespace {
23 // Clears the underlying NSHTTPCookieStorage.
24 void ClearCookies() {
25 NSHTTPCookieStorage* store = [NSHTTPCookieStorage sharedHTTPCookieStorage];
26 [store setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
27 NSArray* cookies = [store cookies];
28 for (NSHTTPCookie* cookie in cookies)
29 [store deleteCookie:cookie];
30 EXPECT_EQ(0u, [[store cookies] count]);
31 }
32 } // namespace
33
34 namespace net { 22 namespace net {
35 23
36 struct CookieStoreIOSTestTraits { 24 struct CookieStoreIOSTestTraits {
37 static std::unique_ptr<net::CookieStore> Create() { 25 static std::unique_ptr<net::CookieStore> Create() {
38 ClearCookies(); 26 ClearCookies();
39 return base::MakeUnique<CookieStoreIOS>( 27 return base::MakeUnique<CookieStoreIOS>(
40 [NSHTTPCookieStorage sharedHTTPCookieStorage]); 28 [NSHTTPCookieStorage sharedHTTPCookieStorage]);
41 } 29 }
42 30
43 static const bool supports_http_only = false; 31 static const bool supports_http_only = false;
44 static const bool supports_non_dotted_domains = false; 32 static const bool supports_non_dotted_domains = false;
45 static const bool preserves_trailing_dots = false; 33 static const bool preserves_trailing_dots = false;
46 static const bool filters_schemes = false; 34 static const bool filters_schemes = false;
47 static const bool has_path_prefix_bug = true; 35 static const bool has_path_prefix_bug = true;
48 static const int creation_time_granularity_in_ms = 1000; 36 static const int creation_time_granularity_in_ms = 1000;
49 37
50 base::MessageLoop loop_; 38 base::MessageLoop loop_;
51 }; 39 };
52 40
53 struct InactiveCookieStoreIOSTestTraits {
54 static std::unique_ptr<net::CookieStore> Create() {
55 return base::MakeUnique<CookieStoreIOSPersistent>(nullptr);
56 }
57
58 static const bool is_cookie_monster = false;
59 static const bool supports_http_only = false;
60 static const bool supports_non_dotted_domains = true;
61 static const bool preserves_trailing_dots = true;
62 static const bool filters_schemes = false;
63 static const bool has_path_prefix_bug = false;
64 static const int creation_time_granularity_in_ms = 0;
65 static const int enforces_prefixes = true;
66 static const bool enforce_strict_secure = false;
67
68 base::MessageLoop loop_;
69 };
70
71 } // namespace net
72
73 namespace net {
74
75 INSTANTIATE_TYPED_TEST_CASE_P(CookieStoreIOS, 41 INSTANTIATE_TYPED_TEST_CASE_P(CookieStoreIOS,
76 CookieStoreTest, 42 CookieStoreTest,
77 CookieStoreIOSTestTraits); 43 CookieStoreIOSTestTraits);
78 44
79 INSTANTIATE_TYPED_TEST_CASE_P(InactiveCookieStoreIOS,
80 CookieStoreTest,
81 InactiveCookieStoreIOSTestTraits);
82
83 } // namespace net
84 45
85 namespace { 46 namespace {
86 47
87 // Test net::CookieMonster::PersistentCookieStore allowing to control when the
88 // initialization completes.
89 class TestPersistentCookieStore
90 : public net::CookieMonster::PersistentCookieStore {
91 public:
92 TestPersistentCookieStore()
93 : kTestCookieURL("http://foo.google.com/bar"), flushed_(false) {}
94
95 // Runs the completion callback with a "a=b" cookie.
96 void RunLoadedCallback() {
97 std::vector<std::unique_ptr<net::CanonicalCookie>> cookies;
98 net::CookieOptions options;
99 options.set_include_httponly();
100
101 std::unique_ptr<net::CanonicalCookie> cookie(net::CanonicalCookie::Create(
102 kTestCookieURL, "a=b", base::Time::Now(), options));
103 cookies.push_back(std::move(cookie));
104
105 // Some canonical cookies cannot be converted into System cookies, for
106 // example if value is not valid utf8. Such cookies are ignored.
107 std::unique_ptr<net::CanonicalCookie> bad_canonical_cookie(
108 net::CanonicalCookie::Create(GURL("http://domain/"), "name",
109 "\x81r\xe4\xbd\xa0\xe5\xa5\xbd",
110 std::string(), "/path/",
111 base::Time(), // creation
112 base::Time(), // expires
113 false, // secure
114 false, // httponly
115 net::CookieSameSite::DEFAULT_MODE,
116 net::COOKIE_PRIORITY_DEFAULT));
117 cookies.push_back(std::move(bad_canonical_cookie));
118 loaded_callback_.Run(std::move(cookies));
119 }
120
121 bool flushed() { return flushed_; }
122
123 private:
124 // net::CookieMonster::PersistentCookieStore implementation:
125 void Load(const LoadedCallback& loaded_callback) override {
126 loaded_callback_ = loaded_callback;
127 }
128
129 void LoadCookiesForKey(const std::string& key,
130 const LoadedCallback& loaded_callback) override {
131 loaded_callback_ = loaded_callback;
132 }
133
134 void AddCookie(const net::CanonicalCookie& cc) override {}
135 void UpdateCookieAccessTime(const net::CanonicalCookie& cc) override {}
136 void DeleteCookie(const net::CanonicalCookie& cc) override {}
137 void SetForceKeepSessionState() override {}
138 void Flush(const base::Closure& callback) override { flushed_ = true; }
139
140 private:
141 ~TestPersistentCookieStore() override {}
142
143 const GURL kTestCookieURL;
144 LoadedCallback loaded_callback_;
145 bool flushed_;
146 };
147
148 // Helper callback to be passed to CookieStore::GetCookiesWithOptionsAsync().
149 class GetCookieCallback {
150 public:
151 GetCookieCallback() : did_run_(false) {}
152
153 // Returns true if the callback has been run.
154 bool did_run() { return did_run_; }
155
156 // Returns the parameter of the callback.
157 const std::string& cookie_line() { return cookie_line_; }
158
159 void Run(const std::string& cookie_line) {
160 ASSERT_FALSE(did_run_);
161 did_run_ = true;
162 cookie_line_ = cookie_line;
163 }
164
165 private:
166 bool did_run_;
167 std::string cookie_line_;
168 };
169
170 // Helper callback to be passed to CookieStore::GetAllCookiesForURLAsync(). 48 // Helper callback to be passed to CookieStore::GetAllCookiesForURLAsync().
171 class GetAllCookiesCallback { 49 class GetAllCookiesCallback {
172 public: 50 public:
173 GetAllCookiesCallback() : did_run_(false) {} 51 GetAllCookiesCallback() : did_run_(false) {}
174 52
175 // Returns true if the callback has been run. 53 // Returns true if the callback has been run.
176 bool did_run() { return did_run_; } 54 bool did_run() { return did_run_; }
177 55
178 // Returns the parameter of the callback. 56 // Returns the parameter of the callback.
179 const net::CookieList& cookie_list() { return cookie_list_; } 57 const net::CookieList& cookie_list() { return cookie_list_; }
180 58
181 void Run(const net::CookieList& cookie_list) { 59 void Run(const net::CookieList& cookie_list) {
182 ASSERT_FALSE(did_run_); 60 ASSERT_FALSE(did_run_);
183 did_run_ = true; 61 did_run_ = true;
184 cookie_list_ = cookie_list; 62 cookie_list_ = cookie_list;
185 } 63 }
186 64
187 private: 65 private:
188 bool did_run_; 66 bool did_run_;
189 net::CookieList cookie_list_; 67 net::CookieList cookie_list_;
190 }; 68 };
191 69
192 namespace {
193
194 void RecordCookieChanges(std::vector<net::CanonicalCookie>* out_cookies,
195 std::vector<bool>* out_removes,
196 const net::CanonicalCookie& cookie,
197 net::CookieStore::ChangeCause cause) {
198 DCHECK(out_cookies);
199 out_cookies->push_back(cookie);
200 if (out_removes)
201 out_removes->push_back(net::CookieStore::ChangeCauseIsDeletion(cause));
202 }
203
204 void IgnoreBoolean(bool ignored) { 70 void IgnoreBoolean(bool ignored) {
205 } 71 }
206 72
207 void IgnoreString(const std::string& ignored) { 73 void IgnoreString(const std::string& ignored) {
208 } 74 }
209 75
210 } // namespace 76 } // namespace
211 77
212 // Sets a cookie.
213 void SetCookie(const std::string& cookie_line,
214 const GURL& url,
215 net::CookieStore* store) {
216 net::CookieOptions options;
217 options.set_include_httponly();
218 store->SetCookieWithOptionsAsync(url, cookie_line, options,
219 base::Bind(&IgnoreBoolean));
220 net::CookieStoreIOS::NotifySystemCookiesChanged();
221 // Wait until the flush is posted.
222 base::RunLoop().RunUntilIdle();
223 }
224
225 // Test fixture to exersize net::CookieStoreIOS created with
226 // TestPersistentCookieStore backend and not synchronized with
227 // NSHTTPCookieStorage.
228 class NotSynchronizedCookieStoreIOSWithBackend : public testing::Test {
229 public:
230 NotSynchronizedCookieStoreIOSWithBackend()
231 : kTestCookieURL("http://foo.google.com/bar"),
232 backend_(new TestPersistentCookieStore),
233 store_(base::MakeUnique<net::CookieStoreIOSPersistent>(backend_.get()))
234 {
235 cookie_changed_callback_ = store_->AddCallbackForCookie(
236 kTestCookieURL, "abc",
237 base::Bind(&RecordCookieChanges, &cookies_changed_, &cookies_removed_));
238 }
239
240 ~NotSynchronizedCookieStoreIOSWithBackend() override {}
241
242 // Gets the cookies. |callback| will be called on completion.
243 void GetCookies(const net::CookieStore::GetCookiesCallback& callback) {
244 net::CookieOptions options;
245 options.set_include_httponly();
246 store_->GetCookiesWithOptionsAsync(kTestCookieURL, options, callback);
247 }
248
249 // Sets a cookie.
250 void SetCookie(const std::string& cookie_line) {
251 ::SetCookie(cookie_line, kTestCookieURL, store_.get());
252 }
253
254 private:
255 const GURL kTestCookieURL;
256
257 protected:
258 base::MessageLoop loop_;
259 scoped_refptr<TestPersistentCookieStore> backend_;
260 std::unique_ptr<net::CookieStoreIOS> store_;
261 std::unique_ptr<net::CookieStore::CookieChangedSubscription>
262 cookie_changed_callback_;
263 std::vector<net::CanonicalCookie> cookies_changed_;
264 std::vector<bool> cookies_removed_;
265 };
266
267 // Test fixture to exersize net::CookieStoreIOS created without backend and 78 // Test fixture to exersize net::CookieStoreIOS created without backend and
268 // synchronized with |[NSHTTPCookieStorage sharedHTTPCookieStorage]|. 79 // synchronized with |[NSHTTPCookieStorage sharedHTTPCookieStorage]|.
269 class SynchronizedCookieStoreIOS : public testing::Test { 80 class CookieStoreIOSTest : public testing::Test {
270 public: 81 public:
271 SynchronizedCookieStoreIOS() 82 CookieStoreIOSTest()
272 : kTestCookieURL("http://foo.google.com/bar"), 83 : kTestCookieURL("http://foo.google.com/bar"),
273 kTestCookieURL2("http://foo.google.com/baz"), 84 kTestCookieURL2("http://foo.google.com/baz"),
274 kTestCookieURL3("http://foo.google.com"), 85 kTestCookieURL3("http://foo.google.com"),
275 kTestCookieURL4("http://bar.google.com/bar"), 86 kTestCookieURL4("http://bar.google.com/bar"),
276 backend_(new TestPersistentCookieStore), 87 backend_(new TestPersistentCookieStore),
277 store_(base::MakeUnique<net::CookieStoreIOS>( 88 store_(base::MakeUnique<net::CookieStoreIOS>(
278 [NSHTTPCookieStorage sharedHTTPCookieStorage])) { 89 [NSHTTPCookieStorage sharedHTTPCookieStorage])) {
279 cookie_changed_callback_ = store_->AddCallbackForCookie( 90 cookie_changed_callback_ = store_->AddCallbackForCookie(
280 kTestCookieURL, "abc", 91 kTestCookieURL, "abc",
281 base::Bind(&RecordCookieChanges, &cookies_changed_, &cookies_removed_)); 92 base::Bind(&RecordCookieChanges, &cookies_changed_, &cookies_removed_));
282 } 93 }
283 94
284 ~SynchronizedCookieStoreIOS() override {} 95 ~CookieStoreIOSTest() override {}
285 96
286 // Gets the cookies. |callback| will be called on completion. 97 // Gets the cookies. |callback| will be called on completion.
287 void GetCookies(const net::CookieStore::GetCookiesCallback& callback) { 98 void GetCookies(const net::CookieStore::GetCookiesCallback& callback) {
288 net::CookieOptions options; 99 net::CookieOptions options;
289 options.set_include_httponly(); 100 options.set_include_httponly();
290 store_->GetCookiesWithOptionsAsync(kTestCookieURL, options, callback); 101 store_->GetCookiesWithOptionsAsync(kTestCookieURL, options, callback);
291 } 102 }
292 103
293 // Sets a cookie. 104 // Sets a cookie.
294 void SetCookie(const std::string& cookie_line) { 105 void SetCookie(const std::string& cookie_line) {
295 ::SetCookie(cookie_line, kTestCookieURL, store_.get()); 106 net::SetCookie(cookie_line, kTestCookieURL, store_.get());
296 } 107 }
297 108
298 void SetSystemCookie(const GURL& url, 109 void SetSystemCookie(const GURL& url,
299 const std::string& name, 110 const std::string& name,
300 const std::string& value) { 111 const std::string& value) {
301 NSHTTPCookieStorage* storage = 112 NSHTTPCookieStorage* storage =
302 [NSHTTPCookieStorage sharedHTTPCookieStorage]; 113 [NSHTTPCookieStorage sharedHTTPCookieStorage];
303 [storage setCookie:[NSHTTPCookie cookieWithProperties:@{ 114 [storage setCookie:[NSHTTPCookie cookieWithProperties:@{
304 NSHTTPCookiePath : base::SysUTF8ToNSString(url.path()), 115 NSHTTPCookiePath : base::SysUTF8ToNSString(url.path()),
305 NSHTTPCookieName : base::SysUTF8ToNSString(name), 116 NSHTTPCookieName : base::SysUTF8ToNSString(name),
(...skipping 27 matching lines...) Expand all
333 144
334 base::MessageLoop loop_; 145 base::MessageLoop loop_;
335 scoped_refptr<TestPersistentCookieStore> backend_; 146 scoped_refptr<TestPersistentCookieStore> backend_;
336 std::unique_ptr<net::CookieStoreIOS> store_; 147 std::unique_ptr<net::CookieStoreIOS> store_;
337 std::unique_ptr<net::CookieStore::CookieChangedSubscription> 148 std::unique_ptr<net::CookieStore::CookieChangedSubscription>
338 cookie_changed_callback_; 149 cookie_changed_callback_;
339 std::vector<net::CanonicalCookie> cookies_changed_; 150 std::vector<net::CanonicalCookie> cookies_changed_;
340 std::vector<bool> cookies_removed_; 151 std::vector<bool> cookies_removed_;
341 }; 152 };
342 153
343 } // namespace 154 TEST_F(CookieStoreIOSTest, SetCookieCallsHookWhenSynchronized) {
344
345 namespace net {
346
347 TEST_F(NotSynchronizedCookieStoreIOSWithBackend, SetCookieCallsHook) {
348 ClearCookies();
349 SetCookie("abc=def");
350 EXPECT_EQ(0U, cookies_changed_.size());
351 EXPECT_EQ(0U, cookies_removed_.size());
352 backend_->RunLoadedCallback();
353 base::RunLoop().RunUntilIdle();
354 EXPECT_EQ(1U, cookies_changed_.size());
355 EXPECT_EQ(1U, cookies_removed_.size());
356 EXPECT_EQ("abc", cookies_changed_[0].Name());
357 EXPECT_EQ("def", cookies_changed_[0].Value());
358 EXPECT_FALSE(cookies_removed_[0]);
359
360 // Replacing an existing cookie is actually a two-phase delete + set
361 // operation, so we get an extra notification.
362 SetCookie("abc=ghi");
363 EXPECT_EQ(3U, cookies_changed_.size());
364 EXPECT_EQ(3U, cookies_removed_.size());
365 EXPECT_EQ("abc", cookies_changed_[1].Name());
366 EXPECT_EQ("def", cookies_changed_[1].Value());
367 EXPECT_TRUE(cookies_removed_[1]);
368 EXPECT_EQ("abc", cookies_changed_[2].Name());
369 EXPECT_EQ("ghi", cookies_changed_[2].Value());
370 EXPECT_FALSE(cookies_removed_[2]);
371 }
372
373 TEST_F(SynchronizedCookieStoreIOS, SetCookieCallsHookWhenSynchronized) {
374 GetCookies(base::Bind(&IgnoreString)); 155 GetCookies(base::Bind(&IgnoreString));
375 ClearCookies(); 156 ClearCookies();
376 SetCookie("abc=def"); 157 SetCookie("abc=def");
377 EXPECT_EQ(1U, cookies_changed_.size()); 158 EXPECT_EQ(1U, cookies_changed_.size());
378 EXPECT_EQ(1U, cookies_removed_.size()); 159 EXPECT_EQ(1U, cookies_removed_.size());
379 EXPECT_EQ("abc", cookies_changed_[0].Name()); 160 EXPECT_EQ("abc", cookies_changed_[0].Name());
380 EXPECT_EQ("def", cookies_changed_[0].Value()); 161 EXPECT_EQ("def", cookies_changed_[0].Value());
381 EXPECT_FALSE(cookies_removed_[0]); 162 EXPECT_FALSE(cookies_removed_[0]);
382 163
383 SetCookie("abc=ghi"); 164 SetCookie("abc=ghi");
384 EXPECT_EQ(3U, cookies_changed_.size()); 165 EXPECT_EQ(3U, cookies_changed_.size());
385 EXPECT_EQ(3U, cookies_removed_.size()); 166 EXPECT_EQ(3U, cookies_removed_.size());
386 EXPECT_EQ("abc", cookies_changed_[1].Name()); 167 EXPECT_EQ("abc", cookies_changed_[1].Name());
387 EXPECT_EQ("def", cookies_changed_[1].Value()); 168 EXPECT_EQ("def", cookies_changed_[1].Value());
388 EXPECT_TRUE(cookies_removed_[1]); 169 EXPECT_TRUE(cookies_removed_[1]);
389 EXPECT_EQ("abc", cookies_changed_[2].Name()); 170 EXPECT_EQ("abc", cookies_changed_[2].Name());
390 EXPECT_EQ("ghi", cookies_changed_[2].Value()); 171 EXPECT_EQ("ghi", cookies_changed_[2].Value());
391 EXPECT_FALSE(cookies_removed_[2]); 172 EXPECT_FALSE(cookies_removed_[2]);
392 DeleteSystemCookie(kTestCookieURL, "abc"); 173 DeleteSystemCookie(kTestCookieURL, "abc");
393 } 174 }
394 175
395 TEST_F(SynchronizedCookieStoreIOS, DeleteCallsHook) { 176 TEST_F(CookieStoreIOSTest, DeleteCallsHook) {
396 GetCookies(base::Bind(&IgnoreString)); 177 GetCookies(base::Bind(&IgnoreString));
397 ClearCookies(); 178 ClearCookies();
398 SetCookie("abc=def"); 179 SetCookie("abc=def");
399 EXPECT_EQ(1U, cookies_changed_.size()); 180 EXPECT_EQ(1U, cookies_changed_.size());
400 EXPECT_EQ(1U, cookies_removed_.size()); 181 EXPECT_EQ(1U, cookies_removed_.size());
401 store_->DeleteCookieAsync(kTestCookieURL, "abc", 182 store_->DeleteCookieAsync(kTestCookieURL, "abc",
402 base::Bind(&IgnoreBoolean, false)); 183 base::Bind(&IgnoreBoolean, false));
403 CookieStoreIOS::NotifySystemCookiesChanged(); 184 CookieStoreIOS::NotifySystemCookiesChanged();
404 base::RunLoop().RunUntilIdle(); 185 base::RunLoop().RunUntilIdle();
405 } 186 }
406 187
407 TEST_F(SynchronizedCookieStoreIOS, SameValueDoesNotCallHook) { 188 TEST_F(CookieStoreIOSTest, SameValueDoesNotCallHook) {
408 GetCookieCallback callback; 189 GetCookieCallback callback;
409 GetCookies(base::Bind(&IgnoreString)); 190 GetCookies(base::Bind(&IgnoreString));
410 ClearCookies(); 191 ClearCookies();
411 SetCookie("abc=def"); 192 SetCookie("abc=def");
412 EXPECT_EQ(1U, cookies_changed_.size()); 193 EXPECT_EQ(1U, cookies_changed_.size());
413 SetCookie("abc=def"); 194 SetCookie("abc=def");
414 EXPECT_EQ(1U, cookies_changed_.size()); 195 EXPECT_EQ(1U, cookies_changed_.size());
415 } 196 }
416 197
417 TEST(CookieStoreIOS, GetAllCookiesForURLAsync) { 198 TEST(CookieStoreIOS, GetAllCookiesForURLAsync) {
(...skipping 13 matching lines...) Expand all
431 cookie_store->GetAllCookiesForURLAsync( 212 cookie_store->GetAllCookiesForURLAsync(
432 kTestCookieURL, 213 kTestCookieURL,
433 base::Bind(&GetAllCookiesCallback::Run, base::Unretained(&callback))); 214 base::Bind(&GetAllCookiesCallback::Run, base::Unretained(&callback)));
434 EXPECT_TRUE(callback.did_run()); 215 EXPECT_TRUE(callback.did_run());
435 EXPECT_EQ(1u, callback.cookie_list().size()); 216 EXPECT_EQ(1u, callback.cookie_list().size());
436 net::CanonicalCookie cookie = callback.cookie_list()[0]; 217 net::CanonicalCookie cookie = callback.cookie_list()[0];
437 EXPECT_EQ("a", cookie.Name()); 218 EXPECT_EQ("a", cookie.Name());
438 EXPECT_EQ("b", cookie.Value()); 219 EXPECT_EQ("b", cookie.Value());
439 } 220 }
440 221
441 // Tests that cookies can be read before the backend is loaded. 222 TEST_F(CookieStoreIOSTest, NoInitialNotifyWithNoCookie) {
442 TEST_F(NotSynchronizedCookieStoreIOSWithBackend, NotSynchronized) {
443 // Start fetching the cookie.
444 GetCookieCallback callback;
445 GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback)));
446 // Backend loading completes.
447 backend_->RunLoadedCallback();
448 EXPECT_TRUE(callback.did_run());
449 EXPECT_EQ("a=b", callback.cookie_line());
450 }
451
452 TEST_F(SynchronizedCookieStoreIOS, NoInitialNotifyWithNoCookie) {
453 std::vector<net::CanonicalCookie> cookies; 223 std::vector<net::CanonicalCookie> cookies;
454 store_->AddCallbackForCookie( 224 store_->AddCallbackForCookie(
455 kTestCookieURL, "abc", 225 kTestCookieURL, "abc",
456 base::Bind(&RecordCookieChanges, &cookies, nullptr)); 226 base::Bind(&RecordCookieChanges, &cookies, nullptr));
457 EXPECT_EQ(0U, cookies.size()); 227 EXPECT_EQ(0U, cookies.size());
458 } 228 }
459 229
460 TEST_F(SynchronizedCookieStoreIOS, NoInitialNotifyWithSystemCookie) { 230 TEST_F(CookieStoreIOSTest, NoInitialNotifyWithSystemCookie) {
461 SetSystemCookie(kTestCookieURL, "abc", "def"); 231 SetSystemCookie(kTestCookieURL, "abc", "def");
462 std::vector<net::CanonicalCookie> cookies; 232 std::vector<net::CanonicalCookie> cookies;
463 store_->AddCallbackForCookie( 233 store_->AddCallbackForCookie(
464 kTestCookieURL, "abc", 234 kTestCookieURL, "abc",
465 base::Bind(&RecordCookieChanges, &cookies, nullptr)); 235 base::Bind(&RecordCookieChanges, &cookies, nullptr));
466 EXPECT_EQ(0U, cookies.size()); 236 EXPECT_EQ(0U, cookies.size());
467 DeleteSystemCookie(kTestCookieURL, "abc"); 237 DeleteSystemCookie(kTestCookieURL, "abc");
468 } 238 }
469 239
470 TEST_F(SynchronizedCookieStoreIOS, NotifyOnAdd) { 240 TEST_F(CookieStoreIOSTest, NotifyOnAdd) {
471 std::vector<net::CanonicalCookie> cookies; 241 std::vector<net::CanonicalCookie> cookies;
472 std::vector<bool> removes; 242 std::vector<bool> removes;
473 std::unique_ptr<net::CookieStore::CookieChangedSubscription> handle = 243 std::unique_ptr<net::CookieStore::CookieChangedSubscription> handle =
474 store_->AddCallbackForCookie( 244 store_->AddCallbackForCookie(
475 kTestCookieURL, "abc", 245 kTestCookieURL, "abc",
476 base::Bind(&RecordCookieChanges, &cookies, &removes)); 246 base::Bind(&RecordCookieChanges, &cookies, &removes));
477 EXPECT_EQ(0U, cookies.size()); 247 EXPECT_EQ(0U, cookies.size());
478 EXPECT_EQ(0U, removes.size()); 248 EXPECT_EQ(0U, removes.size());
479 SetSystemCookie(kTestCookieURL, "abc", "def"); 249 SetSystemCookie(kTestCookieURL, "abc", "def");
480 EXPECT_EQ(1U, cookies.size()); 250 EXPECT_EQ(1U, cookies.size());
481 EXPECT_EQ(1U, removes.size()); 251 EXPECT_EQ(1U, removes.size());
482 EXPECT_EQ("abc", cookies[0].Name()); 252 EXPECT_EQ("abc", cookies[0].Name());
483 EXPECT_EQ("def", cookies[0].Value()); 253 EXPECT_EQ("def", cookies[0].Value());
484 EXPECT_FALSE(removes[0]); 254 EXPECT_FALSE(removes[0]);
485 255
486 SetSystemCookie(kTestCookieURL, "ghi", "jkl"); 256 SetSystemCookie(kTestCookieURL, "ghi", "jkl");
487 EXPECT_EQ(1U, cookies.size()); 257 EXPECT_EQ(1U, cookies.size());
488 EXPECT_EQ(1U, removes.size()); 258 EXPECT_EQ(1U, removes.size());
489 259
490 DeleteSystemCookie(kTestCookieURL, "abc"); 260 DeleteSystemCookie(kTestCookieURL, "abc");
491 DeleteSystemCookie(kTestCookieURL, "ghi"); 261 DeleteSystemCookie(kTestCookieURL, "ghi");
492 } 262 }
493 263
494 TEST_F(SynchronizedCookieStoreIOS, NotifyOnChange) { 264 TEST_F(CookieStoreIOSTest, NotifyOnChange) {
495 std::vector<net::CanonicalCookie> cookies; 265 std::vector<net::CanonicalCookie> cookies;
496 std::vector<bool> removes; 266 std::vector<bool> removes;
497 std::unique_ptr<net::CookieStore::CookieChangedSubscription> handle = 267 std::unique_ptr<net::CookieStore::CookieChangedSubscription> handle =
498 store_->AddCallbackForCookie( 268 store_->AddCallbackForCookie(
499 kTestCookieURL, "abc", 269 kTestCookieURL, "abc",
500 base::Bind(&RecordCookieChanges, &cookies, &removes)); 270 base::Bind(&RecordCookieChanges, &cookies, &removes));
501 EXPECT_EQ(0U, cookies.size()); 271 EXPECT_EQ(0U, cookies.size());
502 SetSystemCookie(kTestCookieURL, "abc", "def"); 272 SetSystemCookie(kTestCookieURL, "abc", "def");
503 EXPECT_EQ(1U, cookies.size()); 273 EXPECT_EQ(1U, cookies.size());
504 SetSystemCookie(kTestCookieURL, "abc", "ghi"); 274 SetSystemCookie(kTestCookieURL, "abc", "ghi");
505 EXPECT_EQ(3U, cookies.size()); 275 EXPECT_EQ(3U, cookies.size());
506 EXPECT_EQ(3U, removes.size()); 276 EXPECT_EQ(3U, removes.size());
507 EXPECT_EQ("abc", cookies[1].Name()); 277 EXPECT_EQ("abc", cookies[1].Name());
508 EXPECT_EQ("def", cookies[1].Value()); 278 EXPECT_EQ("def", cookies[1].Value());
509 EXPECT_TRUE(removes[1]); 279 EXPECT_TRUE(removes[1]);
510 EXPECT_EQ("abc", cookies[2].Name()); 280 EXPECT_EQ("abc", cookies[2].Name());
511 EXPECT_EQ("ghi", cookies[2].Value()); 281 EXPECT_EQ("ghi", cookies[2].Value());
512 EXPECT_FALSE(removes[2]); 282 EXPECT_FALSE(removes[2]);
513 283
514 DeleteSystemCookie(kTestCookieURL, "abc"); 284 DeleteSystemCookie(kTestCookieURL, "abc");
515 } 285 }
516 286
517 TEST_F(SynchronizedCookieStoreIOS, NotifyOnDelete) { 287 TEST_F(CookieStoreIOSTest, NotifyOnDelete) {
518 std::vector<net::CanonicalCookie> cookies; 288 std::vector<net::CanonicalCookie> cookies;
519 std::vector<bool> removes; 289 std::vector<bool> removes;
520 SetSystemCookie(kTestCookieURL, "abc", "def"); 290 SetSystemCookie(kTestCookieURL, "abc", "def");
521 std::unique_ptr<net::CookieStore::CookieChangedSubscription> handle = 291 std::unique_ptr<net::CookieStore::CookieChangedSubscription> handle =
522 store_->AddCallbackForCookie( 292 store_->AddCallbackForCookie(
523 kTestCookieURL, "abc", 293 kTestCookieURL, "abc",
524 base::Bind(&RecordCookieChanges, &cookies, &removes)); 294 base::Bind(&RecordCookieChanges, &cookies, &removes));
525 EXPECT_EQ(0U, cookies.size()); 295 EXPECT_EQ(0U, cookies.size());
526 DeleteSystemCookie(kTestCookieURL, "abc"); 296 DeleteSystemCookie(kTestCookieURL, "abc");
527 EXPECT_EQ(1U, cookies.size()); 297 EXPECT_EQ(1U, cookies.size());
528 EXPECT_EQ(1U, removes.size()); 298 EXPECT_EQ(1U, removes.size());
529 EXPECT_TRUE(removes[0]); 299 EXPECT_TRUE(removes[0]);
530 SetSystemCookie(kTestCookieURL, "abc", "def"); 300 SetSystemCookie(kTestCookieURL, "abc", "def");
531 EXPECT_EQ(2U, cookies.size()); 301 EXPECT_EQ(2U, cookies.size());
532 EXPECT_EQ(2U, removes.size()); 302 EXPECT_EQ(2U, removes.size());
533 EXPECT_FALSE(removes[1]); 303 EXPECT_FALSE(removes[1]);
534 DeleteSystemCookie(kTestCookieURL, "abc"); 304 DeleteSystemCookie(kTestCookieURL, "abc");
535 } 305 }
536 306
537 TEST_F(SynchronizedCookieStoreIOS, NoNotifyOnNoChange) { 307 TEST_F(CookieStoreIOSTest, NoNotifyOnNoChange) {
538 std::vector<net::CanonicalCookie> cookies; 308 std::vector<net::CanonicalCookie> cookies;
539 std::unique_ptr<net::CookieStore::CookieChangedSubscription> handle = 309 std::unique_ptr<net::CookieStore::CookieChangedSubscription> handle =
540 store_->AddCallbackForCookie( 310 store_->AddCallbackForCookie(
541 kTestCookieURL, "abc", 311 kTestCookieURL, "abc",
542 base::Bind(&RecordCookieChanges, &cookies, nullptr)); 312 base::Bind(&RecordCookieChanges, &cookies, nullptr));
543 EXPECT_EQ(0U, cookies.size()); 313 EXPECT_EQ(0U, cookies.size());
544 SetSystemCookie(kTestCookieURL, "abc", "def"); 314 SetSystemCookie(kTestCookieURL, "abc", "def");
545 EXPECT_EQ(1U, cookies.size()); 315 EXPECT_EQ(1U, cookies.size());
546 SetSystemCookie(kTestCookieURL, "abc", "def"); 316 SetSystemCookie(kTestCookieURL, "abc", "def");
547 EXPECT_EQ(1U, cookies.size()); 317 EXPECT_EQ(1U, cookies.size());
548 DeleteSystemCookie(kTestCookieURL, "abc"); 318 DeleteSystemCookie(kTestCookieURL, "abc");
549 } 319 }
550 320
551 TEST_F(SynchronizedCookieStoreIOS, MultipleNotifies) { 321 TEST_F(CookieStoreIOSTest, MultipleNotifies) {
552 std::vector<net::CanonicalCookie> cookies; 322 std::vector<net::CanonicalCookie> cookies;
553 std::vector<net::CanonicalCookie> cookies2; 323 std::vector<net::CanonicalCookie> cookies2;
554 std::vector<net::CanonicalCookie> cookies3; 324 std::vector<net::CanonicalCookie> cookies3;
555 std::vector<net::CanonicalCookie> cookies4; 325 std::vector<net::CanonicalCookie> cookies4;
556 std::unique_ptr<net::CookieStore::CookieChangedSubscription> handle = 326 std::unique_ptr<net::CookieStore::CookieChangedSubscription> handle =
557 store_->AddCallbackForCookie( 327 store_->AddCallbackForCookie(
558 kTestCookieURL, "abc", 328 kTestCookieURL, "abc",
559 base::Bind(&RecordCookieChanges, &cookies, nullptr)); 329 base::Bind(&RecordCookieChanges, &cookies, nullptr));
560 std::unique_ptr<net::CookieStore::CookieChangedSubscription> handle2 = 330 std::unique_ptr<net::CookieStore::CookieChangedSubscription> handle2 =
561 store_->AddCallbackForCookie( 331 store_->AddCallbackForCookie(
(...skipping 14 matching lines...) Expand all
576 EXPECT_EQ(1U, cookies.size()); 346 EXPECT_EQ(1U, cookies.size());
577 EXPECT_EQ(1U, cookies2.size()); 347 EXPECT_EQ(1U, cookies2.size());
578 EXPECT_EQ(0U, cookies3.size()); 348 EXPECT_EQ(0U, cookies3.size());
579 EXPECT_EQ(1U, cookies4.size()); 349 EXPECT_EQ(1U, cookies4.size());
580 DeleteSystemCookie(kTestCookieURL, "abc"); 350 DeleteSystemCookie(kTestCookieURL, "abc");
581 DeleteSystemCookie(kTestCookieURL2, "abc"); 351 DeleteSystemCookie(kTestCookieURL2, "abc");
582 DeleteSystemCookie(kTestCookieURL3, "abc"); 352 DeleteSystemCookie(kTestCookieURL3, "abc");
583 DeleteSystemCookie(kTestCookieURL4, "abc"); 353 DeleteSystemCookie(kTestCookieURL4, "abc");
584 } 354 }
585 355
586 TEST_F(SynchronizedCookieStoreIOS, LessSpecificNestedCookie) { 356 TEST_F(CookieStoreIOSTest, LessSpecificNestedCookie) {
587 std::vector<net::CanonicalCookie> cookies; 357 std::vector<net::CanonicalCookie> cookies;
588 SetSystemCookie(kTestCookieURL2, "abc", "def"); 358 SetSystemCookie(kTestCookieURL2, "abc", "def");
589 std::unique_ptr<net::CookieStore::CookieChangedSubscription> handle = 359 std::unique_ptr<net::CookieStore::CookieChangedSubscription> handle =
590 store_->AddCallbackForCookie( 360 store_->AddCallbackForCookie(
591 kTestCookieURL2, "abc", 361 kTestCookieURL2, "abc",
592 base::Bind(&RecordCookieChanges, &cookies, nullptr)); 362 base::Bind(&RecordCookieChanges, &cookies, nullptr));
593 EXPECT_EQ(0U, cookies.size()); 363 EXPECT_EQ(0U, cookies.size());
594 SetSystemCookie(kTestCookieURL3, "abc", "ghi"); 364 SetSystemCookie(kTestCookieURL3, "abc", "ghi");
595 EXPECT_EQ(1U, cookies.size()); 365 EXPECT_EQ(1U, cookies.size());
596 DeleteSystemCookie(kTestCookieURL, "abc"); 366 DeleteSystemCookie(kTestCookieURL, "abc");
597 } 367 }
598 368
599 TEST_F(SynchronizedCookieStoreIOS, MoreSpecificNestedCookie) { 369 TEST_F(CookieStoreIOSTest, MoreSpecificNestedCookie) {
600 std::vector<net::CanonicalCookie> cookies; 370 std::vector<net::CanonicalCookie> cookies;
601 SetSystemCookie(kTestCookieURL3, "abc", "def"); 371 SetSystemCookie(kTestCookieURL3, "abc", "def");
602 std::unique_ptr<net::CookieStore::CookieChangedSubscription> handle = 372 std::unique_ptr<net::CookieStore::CookieChangedSubscription> handle =
603 store_->AddCallbackForCookie( 373 store_->AddCallbackForCookie(
604 kTestCookieURL2, "abc", 374 kTestCookieURL2, "abc",
605 base::Bind(&RecordCookieChanges, &cookies, nullptr)); 375 base::Bind(&RecordCookieChanges, &cookies, nullptr));
606 EXPECT_EQ(0U, cookies.size()); 376 EXPECT_EQ(0U, cookies.size());
607 SetSystemCookie(kTestCookieURL2, "abc", "ghi"); 377 SetSystemCookie(kTestCookieURL2, "abc", "ghi");
608 EXPECT_EQ(2U, cookies.size()); 378 EXPECT_EQ(2U, cookies.size());
609 DeleteSystemCookie(kTestCookieURL, "abc"); 379 DeleteSystemCookie(kTestCookieURL, "abc");
610 } 380 }
611 381
612 TEST_F(SynchronizedCookieStoreIOS, MoreSpecificNestedCookieWithSameValue) { 382 TEST_F(CookieStoreIOSTest, MoreSpecificNestedCookieWithSameValue) {
613 std::vector<net::CanonicalCookie> cookies; 383 std::vector<net::CanonicalCookie> cookies;
614 SetSystemCookie(kTestCookieURL3, "abc", "def"); 384 SetSystemCookie(kTestCookieURL3, "abc", "def");
615 std::unique_ptr<net::CookieStore::CookieChangedSubscription> handle = 385 std::unique_ptr<net::CookieStore::CookieChangedSubscription> handle =
616 store_->AddCallbackForCookie( 386 store_->AddCallbackForCookie(
617 kTestCookieURL2, "abc", 387 kTestCookieURL2, "abc",
618 base::Bind(&RecordCookieChanges, &cookies, nullptr)); 388 base::Bind(&RecordCookieChanges, &cookies, nullptr));
619 EXPECT_EQ(0U, cookies.size()); 389 EXPECT_EQ(0U, cookies.size());
620 SetSystemCookie(kTestCookieURL2, "abc", "def"); 390 SetSystemCookie(kTestCookieURL2, "abc", "def");
621 EXPECT_EQ(2U, cookies.size()); 391 EXPECT_EQ(2U, cookies.size());
622 DeleteSystemCookie(kTestCookieURL, "abc"); 392 DeleteSystemCookie(kTestCookieURL, "abc");
623 } 393 }
624 394
625 TEST_F(SynchronizedCookieStoreIOS, RemoveCallback) { 395 TEST_F(CookieStoreIOSTest, RemoveCallback) {
626 std::vector<net::CanonicalCookie> cookies; 396 std::vector<net::CanonicalCookie> cookies;
627 SetSystemCookie(kTestCookieURL, "abc", "def"); 397 SetSystemCookie(kTestCookieURL, "abc", "def");
628 std::unique_ptr<net::CookieStore::CookieChangedSubscription> handle = 398 std::unique_ptr<net::CookieStore::CookieChangedSubscription> handle =
629 store_->AddCallbackForCookie( 399 store_->AddCallbackForCookie(
630 kTestCookieURL, "abc", 400 kTestCookieURL, "abc",
631 base::Bind(&RecordCookieChanges, &cookies, nullptr)); 401 base::Bind(&RecordCookieChanges, &cookies, nullptr));
632 EXPECT_EQ(0U, cookies.size()); 402 EXPECT_EQ(0U, cookies.size());
633 SetSystemCookie(kTestCookieURL, "abc", "ghi"); 403 SetSystemCookie(kTestCookieURL, "abc", "ghi");
634 EXPECT_EQ(2U, cookies.size()); 404 EXPECT_EQ(2U, cookies.size());
635 // this deletes the callback 405 // this deletes the callback
636 handle.reset(); 406 handle.reset();
637 SetSystemCookie(kTestCookieURL, "abc", "jkl"); 407 SetSystemCookie(kTestCookieURL, "abc", "jkl");
638 EXPECT_EQ(2U, cookies.size()); 408 EXPECT_EQ(2U, cookies.size());
639 DeleteSystemCookie(kTestCookieURL, "abc"); 409 DeleteSystemCookie(kTestCookieURL, "abc");
640 } 410 }
641 411
642 } // namespace net 412 } // namespace net
OLDNEW
« no previous file with comments | « ios/net/cookies/cookie_store_ios_test_util.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698