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

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: fix comment 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
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_unittest_helper.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 SynchronizedCookieStoreIOS : public testing::Test {
Eugene But (OOO till 7-30) 2017/02/01 17:35:54 How about s/SynchronizedCookieStoreIOS/CookieStore
maksims (do not use this acc) 2017/02/02 06:47:04 Done.
270 public: 81 public:
271 SynchronizedCookieStoreIOS() 82 SynchronizedCookieStoreIOS()
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 ~SynchronizedCookieStoreIOS() 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
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) { 154 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
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
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.
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) { 222 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(SynchronizedCookieStoreIOS, NoInitialNotifyWithSystemCookie) {
461 SetSystemCookie(kTestCookieURL, "abc", "def"); 231 SetSystemCookie(kTestCookieURL, "abc", "def");
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
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

Powered by Google App Engine
This is Rietveld 408576698