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

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

Issue 2604763003: [ios] Cleaned up comments and tests for CookieStoreIOS synchronization. (Closed)
Patch Set: Removed more obsoleted tests Created 3 years, 12 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.h ('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
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 static const bool preserves_trailing_dots = true; 62 static const bool preserves_trailing_dots = true;
63 static const bool filters_schemes = false; 63 static const bool filters_schemes = false;
64 static const bool has_path_prefix_bug = false; 64 static const bool has_path_prefix_bug = false;
65 static const int creation_time_granularity_in_ms = 0; 65 static const int creation_time_granularity_in_ms = 0;
66 static const int enforces_prefixes = true; 66 static const int enforces_prefixes = true;
67 static const bool enforce_strict_secure = false; 67 static const bool enforce_strict_secure = false;
68 68
69 base::MessageLoop loop_; 69 base::MessageLoop loop_;
70 }; 70 };
71 71
72 // RoundTripTestCookieStore is un-synchronized and re-synchronized after all
73 // cookie operations. This means all system cookies are converted to Chrome
74 // cookies and converted back.
75 // The purpose of this class is to test that cookie conversions do not break the
76 // cookie store.
77 class RoundTripTestCookieStore : public net::CookieStore {
78 public:
79 RoundTripTestCookieStore()
80 : store_(new CookieStoreIOS(nullptr)),
81 dummy_store_(new CookieStoreIOS(nullptr)) {
82 store_->SetSynchronizedWithSystemStore(true);
83 }
84
85 ~RoundTripTestCookieStore() override {
86 store_->SetSynchronizedWithSystemStore(false);
87 }
88
89 // Inherited CookieStore methods.
90 void SetCookieWithOptionsAsync(const GURL& url,
91 const std::string& cookie_line,
92 const net::CookieOptions& options,
93 const SetCookiesCallback& callback) override {
94 RoundTrip();
95 store_->SetCookieWithOptionsAsync(url, cookie_line, options, callback);
96 }
97
98 void SetCookieWithDetailsAsync(const GURL& url,
99 const std::string& name,
100 const std::string& value,
101 const std::string& domain,
102 const std::string& path,
103 base::Time creation_time,
104 base::Time expiration_time,
105 base::Time last_access_time,
106 bool secure,
107 bool http_only,
108 CookieSameSite same_site,
109 bool enforce_strict_secure,
110 CookiePriority priority,
111 const SetCookiesCallback& callback) override {
112 RoundTrip();
113 store_->SetCookieWithDetailsAsync(
114 url, name, value, domain, path, creation_time, expiration_time,
115 last_access_time, secure, http_only, same_site, enforce_strict_secure,
116 priority, callback);
117 }
118
119 void GetCookiesWithOptionsAsync(const GURL& url,
120 const net::CookieOptions& options,
121 const GetCookiesCallback& callback) override {
122 RoundTrip();
123 store_->GetCookiesWithOptionsAsync(url, options, callback);
124 }
125
126 void GetCookieListWithOptionsAsync(
127 const GURL& url,
128 const net::CookieOptions& options,
129 const GetCookieListCallback& callback) override {
130 RoundTrip();
131 store_->GetCookieListWithOptionsAsync(url, options, callback);
132 }
133
134 void GetAllCookiesAsync(const GetCookieListCallback& callback) override {
135 RoundTrip();
136 store_->GetAllCookiesAsync(callback);
137 }
138
139 void DeleteCookieAsync(const GURL& url,
140 const std::string& cookie_name,
141 const base::Closure& callback) override {
142 RoundTrip();
143 store_->DeleteCookieAsync(url, cookie_name, callback);
144 }
145
146 void DeleteCanonicalCookieAsync(
147 const CanonicalCookie& cookie,
148 const DeleteCallback& callback) override {
149 RoundTrip();
150 store_->DeleteCanonicalCookieAsync(cookie, callback);
151 }
152
153 void DeleteAllCreatedBetweenAsync(const base::Time& delete_begin,
154 const base::Time& delete_end,
155 const DeleteCallback& callback) override {
156 RoundTrip();
157 store_->DeleteAllCreatedBetweenAsync(delete_begin, delete_end, callback);
158 }
159
160 void DeleteAllCreatedBetweenWithPredicateAsync(
161 const base::Time& delete_begin,
162 const base::Time& delete_end,
163 const CookiePredicate& predicate,
164 const DeleteCallback& callback) override {
165 RoundTrip();
166 store_->DeleteAllCreatedBetweenWithPredicateAsync(delete_begin, delete_end,
167 predicate, callback);
168 }
169
170 void DeleteSessionCookiesAsync(const DeleteCallback& callback) override {
171 RoundTrip();
172 store_->DeleteSessionCookiesAsync(callback);
173 }
174
175 void FlushStore(const base::Closure& callback) override {
176 RoundTrip();
177 store_->FlushStore(callback);
178 }
179
180 std::unique_ptr<CookieStore::CookieChangedSubscription> AddCallbackForCookie(
181 const GURL& url,
182 const std::string& name,
183 const CookieChangedCallback& callback) override {
184 return std::unique_ptr<CookieStore::CookieChangedSubscription>();
185 }
186
187 bool IsEphemeral() override {
188 return store_->IsEphemeral();
189 }
190
191 private:
192 void RoundTrip() {
193 store_->SetSynchronizedWithSystemStore(false);
194 dummy_store_->SetSynchronizedWithSystemStore(true);
195 // Check that the system store is empty, because it is synchronized with
196 // |dummy_store_| which is empty.
197 NSHTTPCookieStorage* store = [NSHTTPCookieStorage sharedHTTPCookieStorage];
198 EXPECT_EQ(0u, [[store cookies] count]);
199 dummy_store_->SetSynchronizedWithSystemStore(false);
200 store_->SetSynchronizedWithSystemStore(true);
201 }
202
203 std::unique_ptr<CookieStoreIOS> store_;
204 // |dummy_store_| is not directly used, but is needed to make |store_|
205 // inactive.
206 std::unique_ptr<CookieStoreIOS> dummy_store_;
207 };
208
209 struct RoundTripTestCookieStoreTraits {
210 static std::unique_ptr<net::CookieStore> Create() {
211 ClearCookies();
212 return base::MakeUnique<RoundTripTestCookieStore>();
213 }
214
215 static const bool is_cookie_monster = false;
216 static const bool supports_http_only = false;
217 static const bool supports_non_dotted_domains = false;
218 static const bool preserves_trailing_dots = false;
219 static const bool filters_schemes = false;
220 static const bool has_path_prefix_bug = true;
221 static const int creation_time_granularity_in_ms = 1000;
222 static const int enforces_prefixes = true;
223 static const bool enforce_strict_secure = false;
224 };
225
226 } // namespace net 72 } // namespace net
227 73
228 namespace net { 74 namespace net {
229 75
230 INSTANTIATE_TYPED_TEST_CASE_P(CookieStoreIOS, 76 INSTANTIATE_TYPED_TEST_CASE_P(CookieStoreIOS,
231 CookieStoreTest, 77 CookieStoreTest,
232 CookieStoreIOSTestTraits); 78 CookieStoreIOSTestTraits);
233 79
234 INSTANTIATE_TYPED_TEST_CASE_P(InactiveCookieStoreIOS, 80 INSTANTIATE_TYPED_TEST_CASE_P(InactiveCookieStoreIOS,
235 CookieStoreTest, 81 CookieStoreTest,
236 InactiveCookieStoreIOSTestTraits); 82 InactiveCookieStoreIOSTestTraits);
237 83
238 INSTANTIATE_TYPED_TEST_CASE_P(RoundTripTestCookieStore,
239 CookieStoreTest,
240 RoundTripTestCookieStoreTraits);
241
242 } // namespace net 84 } // namespace net
243 85
244 namespace { 86 namespace {
245 87
246 // Test net::CookieMonster::PersistentCookieStore allowing to control when the 88 // Test net::CookieMonster::PersistentCookieStore allowing to control when the
247 // initialization completes. 89 // initialization completes.
248 class TestPersistentCookieStore 90 class TestPersistentCookieStore
249 : public net::CookieMonster::PersistentCookieStore { 91 : public net::CookieMonster::PersistentCookieStore {
250 public: 92 public:
251 TestPersistentCookieStore() 93 TestPersistentCookieStore()
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 store_->SetSynchronizedWithSystemStore(true); 418 store_->SetSynchronizedWithSystemStore(true);
577 GetCookieCallback callback; 419 GetCookieCallback callback;
578 GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback))); 420 GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback)));
579 // Backend loading completes (end of synchronization). 421 // Backend loading completes (end of synchronization).
580 backend_->RunLoadedCallback(); 422 backend_->RunLoadedCallback();
581 EXPECT_TRUE(callback.did_run()); 423 EXPECT_TRUE(callback.did_run());
582 EXPECT_EQ("a=b", callback.cookie_line()); 424 EXPECT_EQ("a=b", callback.cookie_line());
583 store_->SetSynchronizedWithSystemStore(false); 425 store_->SetSynchronizedWithSystemStore(false);
584 } 426 }
585 427
586 // Tests that Synchronization can be "aborted" (i.e. the cookie store is
587 // unsynchronized while synchronization is in progress).
588 TEST_F(CookieStoreIOSWithBackend, SyncThenUnsync) {
589 ClearCookies();
590 std::unique_ptr<CookieStoreIOS> dummy_store(new CookieStoreIOS(nullptr));
591 // Switch back and forth before synchronization can complete.
592 store_->SetSynchronizedWithSystemStore(true);
593 store_->SetSynchronizedWithSystemStore(false);
594 dummy_store->SetSynchronizedWithSystemStore(true);
595 backend_->RunLoadedCallback();
596 // No cookie leak in the system store.
597 NSHTTPCookieStorage* store = [NSHTTPCookieStorage sharedHTTPCookieStorage];
598 EXPECT_EQ(0u, [[store cookies] count]);
599 // No cookie lost.
600 GetCookieCallback callback;
601 GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback)));
602 EXPECT_TRUE(callback.did_run());
603 EXPECT_EQ("a=b", callback.cookie_line());
604 dummy_store->SetSynchronizedWithSystemStore(false);
605 }
606
607 // Tests that Synchronization can be "aborted" while there are pending tasks
608 // (i.e. the cookie store is unsynchronized while synchronization is in progress
609 // and there are pending tasks).
610 TEST_F(CookieStoreIOSWithBackend, SyncThenUnsyncWithPendingTasks) {
611 ClearCookies();
612 std::unique_ptr<CookieStoreIOS> dummy_store(new CookieStoreIOS(nullptr));
613 // Start synchornization.
614 store_->SetSynchronizedWithSystemStore(true);
615 // Create a pending task while synchronization is in progress.
616 GetCookieCallback callback;
617 GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback)));
618 // Cancel the synchronization.
619 store_->SetSynchronizedWithSystemStore(false);
620 dummy_store->SetSynchronizedWithSystemStore(true);
621 // Synchronization completes after being cancelled.
622 backend_->RunLoadedCallback();
623 // The task is not lost.
624 EXPECT_TRUE(callback.did_run());
625 EXPECT_EQ("a=b", callback.cookie_line());
626 dummy_store->SetSynchronizedWithSystemStore(false);
627 }
628
629 TEST_F(CookieStoreIOSWithBackend, UnSynchronizeBeforeLoadComplete) {
630 ClearCookies();
631 // Switch back and forth before synchronization can complete.
632 store_->SetSynchronizedWithSystemStore(true);
633 store_->SetSynchronizedWithSystemStore(false);
634 backend_->RunLoadedCallback();
635 // No cookie lost.
636 GetCookieCallback callback;
637 GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback)));
638 EXPECT_TRUE(callback.did_run());
639 EXPECT_EQ("a=b", callback.cookie_line());
640 }
641
642 TEST_F(CookieStoreIOSWithBackend, UnSynchronize) {
643 ClearCookies();
644 store_->SetSynchronizedWithSystemStore(true);
645 backend_->RunLoadedCallback();
646 store_->SetSynchronizedWithSystemStore(false);
647 // No cookie lost.
648 GetCookieCallback callback;
649 GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback)));
650 EXPECT_TRUE(callback.did_run());
651 EXPECT_EQ("a=b", callback.cookie_line());
652 }
653
654 TEST_F(CookieStoreIOSWithBackend, FlushOnUnSynchronize) {
655 store_->SetSynchronizedWithSystemStore(true);
656 EXPECT_FALSE(backend_->flushed());
657 store_->SetSynchronizedWithSystemStore(false);
658 EXPECT_TRUE(backend_->flushed());
659 }
660
661 TEST_F(CookieStoreIOSWithBackend, FlushOnCookieChanged) { 428 TEST_F(CookieStoreIOSWithBackend, FlushOnCookieChanged) {
662 store_->SetSynchronizedWithSystemStore(true); 429 store_->SetSynchronizedWithSystemStore(true);
663 store_->set_flush_delay_for_testing(base::TimeDelta()); 430 store_->set_flush_delay_for_testing(base::TimeDelta());
664 backend_->RunLoadedCallback(); 431 backend_->RunLoadedCallback();
665 EXPECT_FALSE(backend_->flushed()); 432 EXPECT_FALSE(backend_->flushed());
666 433
667 // Set a cookie an check that it triggers a flush. 434 // Set a cookie an check that it triggers a flush.
668 SetCookie("x=y"); 435 SetCookie("x=y");
669 EXPECT_TRUE(backend_->flushed()); 436 EXPECT_TRUE(backend_->flushed());
670 437
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
898 EXPECT_EQ(2U, cookies.size()); 665 EXPECT_EQ(2U, cookies.size());
899 // this deletes the callback 666 // this deletes the callback
900 handle.reset(); 667 handle.reset();
901 SetSystemCookie(kTestCookieURL, "abc", "jkl"); 668 SetSystemCookie(kTestCookieURL, "abc", "jkl");
902 EXPECT_EQ(2U, cookies.size()); 669 EXPECT_EQ(2U, cookies.size());
903 DeleteSystemCookie(kTestCookieURL, "abc"); 670 DeleteSystemCookie(kTestCookieURL, "abc");
904 store_->SetSynchronizedWithSystemStore(false); 671 store_->SetSynchronizedWithSystemStore(false);
905 } 672 }
906 673
907 } // namespace net 674 } // namespace net
OLDNEW
« no previous file with comments | « ios/net/cookies/cookie_store_ios.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698