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

Side by Side Diff: net/cookies/cookie_monster_unittest.cc

Issue 1000103002: Add a method to override all the cookies in CookieMonster (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Review comments Created 5 years, 9 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 | « net/cookies/cookie_monster.cc ('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 (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_store_unittest.h" 5 #include "net/cookies/cookie_store_unittest.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 cm->SetCookieWithDetailsAsync( 159 cm->SetCookieWithDetailsAsync(
160 url, name, value, domain, path, expiration_time, secure, http_only, 160 url, name, value, domain, path, expiration_time, secure, http_only,
161 first_party_only, priority, 161 first_party_only, priority,
162 base::Bind(&ResultSavingCookieCallback<bool>::Run, 162 base::Bind(&ResultSavingCookieCallback<bool>::Run,
163 base::Unretained(&callback))); 163 base::Unretained(&callback)));
164 RunFor(kTimeout); 164 RunFor(kTimeout);
165 EXPECT_TRUE(callback.did_run()); 165 EXPECT_TRUE(callback.did_run());
166 return callback.result(); 166 return callback.result();
167 } 167 }
168 168
169 bool SetAllCookies(CookieMonster* cm, const CookieList& list) {
170 DCHECK(cm);
171 ResultSavingCookieCallback<bool> callback;
172 cm->SetAllCookiesAsync(list,
173 base::Bind(&ResultSavingCookieCallback<bool>::Run,
174 base::Unretained(&callback)));
175 RunFor(kTimeout);
176 EXPECT_TRUE(callback.did_run());
177 return callback.result();
178 }
179
169 int DeleteAll(CookieMonster* cm) { 180 int DeleteAll(CookieMonster* cm) {
170 DCHECK(cm); 181 DCHECK(cm);
171 ResultSavingCookieCallback<int> callback; 182 ResultSavingCookieCallback<int> callback;
172 cm->DeleteAllAsync(base::Bind(&ResultSavingCookieCallback<int>::Run, 183 cm->DeleteAllAsync(base::Bind(&ResultSavingCookieCallback<int>::Run,
173 base::Unretained(&callback))); 184 base::Unretained(&callback)));
174 RunFor(kTimeout); 185 RunFor(kTimeout);
175 EXPECT_TRUE(callback.did_run()); 186 EXPECT_TRUE(callback.did_run());
176 return callback.result(); 187 return callback.result();
177 } 188 }
178 189
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after
535 546
536 // Function for creating a CM with a number of cookies in it, 547 // Function for creating a CM with a number of cookies in it,
537 // no store (and hence no ability to affect access time). 548 // no store (and hence no ability to affect access time).
538 CookieMonster* CreateMonsterForGC(int num_cookies) { 549 CookieMonster* CreateMonsterForGC(int num_cookies) {
539 CookieMonster* cm(new CookieMonster(NULL, NULL)); 550 CookieMonster* cm(new CookieMonster(NULL, NULL));
540 for (int i = 0; i < num_cookies; i++) { 551 for (int i = 0; i < num_cookies; i++) {
541 SetCookie(cm, GURL(base::StringPrintf("http://h%05d.izzle", i)), "a=1"); 552 SetCookie(cm, GURL(base::StringPrintf("http://h%05d.izzle", i)), "a=1");
542 } 553 }
543 return cm; 554 return cm;
544 } 555 }
556
557 bool IsCookieInList(const CanonicalCookie& cookie, const CookieList& list) {
558 for (CookieList::const_iterator it = list.begin(); it != list.end(); ++it) {
559 if (it->Source() == cookie.Source() && it->Name() == cookie.Name() &&
560 it->Value() == cookie.Value() && it->Domain() == cookie.Domain() &&
561 it->Path() == cookie.Path() &&
562 it->CreationDate() == cookie.CreationDate() &&
563 it->ExpiryDate() == cookie.ExpiryDate() &&
564 it->LastAccessDate() == cookie.LastAccessDate() &&
565 it->IsSecure() == cookie.IsSecure() &&
566 it->IsHttpOnly() == cookie.IsHttpOnly() &&
567 it->Priority() == cookie.Priority()) {
568 return true;
569 }
570 }
571
572 return false;
573 }
545 }; 574 };
546 575
547 // TODO(erikwright): Replace the other callbacks and synchronous helper methods 576 // TODO(erikwright): Replace the other callbacks and synchronous helper methods
548 // in this test suite with these Mocks. 577 // in this test suite with these Mocks.
549 template <typename T, typename C> 578 template <typename T, typename C>
550 class MockCookieCallback { 579 class MockCookieCallback {
551 public: 580 public:
552 C AsCallback() { 581 C AsCallback() {
553 return base::Bind(&T::Invoke, base::Unretained(static_cast<T*>(this))); 582 return base::Bind(&T::Invoke, base::Unretained(static_cast<T*>(this)));
554 } 583 }
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
618 cookie_monster->DeleteCookieAsync(url, name, callback->AsCallback()); 647 cookie_monster->DeleteCookieAsync(url, name, callback->AsCallback());
619 } 648 }
620 ACTION_P3(GetCookiesAction, cookie_monster, url, callback) { 649 ACTION_P3(GetCookiesAction, cookie_monster, url, callback) {
621 cookie_monster->GetCookiesWithOptionsAsync(url, CookieOptions(), 650 cookie_monster->GetCookiesWithOptionsAsync(url, CookieOptions(),
622 callback->AsCallback()); 651 callback->AsCallback());
623 } 652 }
624 ACTION_P4(SetCookieAction, cookie_monster, url, cookie_line, callback) { 653 ACTION_P4(SetCookieAction, cookie_monster, url, cookie_line, callback) {
625 cookie_monster->SetCookieWithOptionsAsync(url, cookie_line, CookieOptions(), 654 cookie_monster->SetCookieWithOptionsAsync(url, cookie_line, CookieOptions(),
626 callback->AsCallback()); 655 callback->AsCallback());
627 } 656 }
657 ACTION_P3(SetAllCookiesAction, cookie_monster, list, callback) {
658 cookie_monster->SetAllCookiesAsync(list, callback->AsCallback());
659 }
628 ACTION_P4(DeleteAllCreatedBetweenAction, 660 ACTION_P4(DeleteAllCreatedBetweenAction,
629 cookie_monster, 661 cookie_monster,
630 delete_begin, 662 delete_begin,
631 delete_end, 663 delete_end,
632 callback) { 664 callback) {
633 cookie_monster->DeleteAllCreatedBetweenAsync(delete_begin, delete_end, 665 cookie_monster->DeleteAllCreatedBetweenAsync(delete_begin, delete_end,
634 callback->AsCallback()); 666 callback->AsCallback());
635 } 667 }
636 ACTION_P3(SetCookieWithDetailsAction, cookie_monster, cc, callback) { 668 ACTION_P3(SetCookieWithDetailsAction, cookie_monster, cc, callback) {
637 cookie_monster->SetCookieWithDetailsAsync( 669 cookie_monster->SetCookieWithDetailsAsync(
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
817 849
818 EXPECT_CALL(set_cookies_callback, Invoke(true)) 850 EXPECT_CALL(set_cookies_callback, Invoke(true))
819 .WillOnce(SetCookieAction(&cookie_monster(), url_google_, "X=Y", 851 .WillOnce(SetCookieAction(&cookie_monster(), url_google_, "X=Y",
820 &set_cookies_callback)); 852 &set_cookies_callback));
821 EXPECT_CALL(set_cookies_callback, Invoke(true)) 853 EXPECT_CALL(set_cookies_callback, Invoke(true))
822 .WillOnce(QuitCurrentMessageLoop()); 854 .WillOnce(QuitCurrentMessageLoop());
823 855
824 CompleteLoadingAndWait(); 856 CompleteLoadingAndWait();
825 } 857 }
826 858
859 TEST_F(DeferredCookieTaskTest, DeferredSetAllCookies) {
860 MockSetCookiesCallback set_cookies_callback;
861 CookieList list;
862 list.push_back(CanonicalCookie(url_google_, "A", "B", "google.izzle", "/",
863 base::Time::Now(), base::Time(), base::Time(),
864 false, true, false, COOKIE_PRIORITY_DEFAULT));
865 list.push_back(CanonicalCookie(url_google_, "C", "D", "google.izzle", "/",
866 base::Time::Now(), base::Time(), base::Time(),
867 false, true, false, COOKIE_PRIORITY_DEFAULT));
868
869 BeginWith(
870 SetAllCookiesAction(&cookie_monster(), list, &set_cookies_callback));
871
872 WaitForLoadCall();
873
874 EXPECT_CALL(set_cookies_callback, Invoke(true))
875 .WillOnce(
876 SetAllCookiesAction(&cookie_monster(), list, &set_cookies_callback));
877 EXPECT_CALL(set_cookies_callback, Invoke(true))
878 .WillOnce(QuitCurrentMessageLoop());
879
880 CompleteLoadingAndWait();
881 }
882
827 TEST_F(DeferredCookieTaskTest, DeferredDeleteCookie) { 883 TEST_F(DeferredCookieTaskTest, DeferredDeleteCookie) {
828 MockClosure delete_cookie_callback; 884 MockClosure delete_cookie_callback;
829 885
830 BeginWithForDomainKey("google.izzle", 886 BeginWithForDomainKey("google.izzle",
831 DeleteCookieAction(&cookie_monster(), url_google_, "A", 887 DeleteCookieAction(&cookie_monster(), url_google_, "A",
832 &delete_cookie_callback)); 888 &delete_cookie_callback));
833 889
834 WaitForLoadCall(); 890 WaitForLoadCall();
835 891
836 EXPECT_CALL(delete_cookie_callback, Invoke()) 892 EXPECT_CALL(delete_cookie_callback, Invoke())
(...skipping 1262 matching lines...) Expand 10 before | Expand all | Expand 10 after
2099 base::MessageLoop::current()->RunUntilIdle(); 2155 base::MessageLoop::current()->RunUntilIdle();
2100 2156
2101 ASSERT_EQ(2, counter->callback_count()); 2157 ASSERT_EQ(2, counter->callback_count());
2102 2158
2103 cm->FlushStore(base::Bind(&CallbackCounter::Callback, counter.get())); 2159 cm->FlushStore(base::Bind(&CallbackCounter::Callback, counter.get()));
2104 base::MessageLoop::current()->RunUntilIdle(); 2160 base::MessageLoop::current()->RunUntilIdle();
2105 2161
2106 ASSERT_EQ(3, counter->callback_count()); 2162 ASSERT_EQ(3, counter->callback_count());
2107 } 2163 }
2108 2164
2165 TEST_F(CookieMonsterTest, SetAllCookies) {
2166 scoped_refptr<FlushablePersistentStore> store(new FlushablePersistentStore());
2167 scoped_refptr<CookieMonster> cm(new CookieMonster(store.get(), NULL));
2168 cm->SetPersistSessionCookies(true);
2169
2170 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "U=V; path=/"));
2171 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "W=X; path=/foo"));
2172 EXPECT_TRUE(SetCookie(cm.get(), url_google_, "Y=Z; path=/"));
2173
2174 CookieList list;
2175 list.push_back(CanonicalCookie(url_google_, "A", "B", url_google_.host(), "/",
2176 base::Time::Now(), base::Time(), base::Time(),
2177 false, false, false, COOKIE_PRIORITY_DEFAULT));
2178 list.push_back(CanonicalCookie(url_google_, "W", "X", url_google_.host(),
2179 "/bar", base::Time::Now(), base::Time(),
2180 base::Time(), false, false, false,
2181 COOKIE_PRIORITY_DEFAULT));
2182 list.push_back(CanonicalCookie(url_google_, "Y", "Z", url_google_.host(), "/",
2183 base::Time::Now(), base::Time(), base::Time(),
2184 false, false, false, COOKIE_PRIORITY_DEFAULT));
2185
2186 // SetAllCookies must not flush.
erikwright (departed) 2015/03/17 17:46:16 It should, presumably, still update the backend, n
droger 2015/03/17 17:48:57 Yes.
2187 ASSERT_EQ(0, store->flush_count());
2188 EXPECT_TRUE(SetAllCookies(cm.get(), list));
2189 EXPECT_EQ(0, store->flush_count());
2190
2191 CookieList cookies = GetAllCookies(cm.get());
2192 size_t expected_size = 3; // "A", "W" and "Y". "U" is gone.
2193 EXPECT_EQ(expected_size, cookies.size());
2194 CookieList::iterator it = cookies.begin();
2195
2196 ASSERT_TRUE(it != cookies.end());
2197 EXPECT_EQ("W", it->Name());
2198 EXPECT_EQ("X", it->Value());
2199 EXPECT_EQ("/bar", it->Path()); // The path has been updated.
2200
2201 ASSERT_TRUE(++it != cookies.end());
2202 EXPECT_EQ("A", it->Name());
2203 EXPECT_EQ("B", it->Value());
2204
2205 ASSERT_TRUE(++it != cookies.end());
2206 EXPECT_EQ("Y", it->Name());
2207 EXPECT_EQ("Z", it->Value());
2208 }
2209
2210 TEST_F(CookieMonsterTest, ComputeCookieDiff) {
2211 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2212
2213 base::Time now = base::Time::Now();
2214 base::Time creation_time = now - base::TimeDelta::FromSeconds(1);
2215
2216 CanonicalCookie cookie1(url_google_, "A", "B", url_google_.host(), "/",
2217 creation_time, base::Time(), base::Time(), false,
2218 false, false, COOKIE_PRIORITY_DEFAULT);
2219 CanonicalCookie cookie2(url_google_, "C", "D", url_google_.host(), "/",
2220 creation_time, base::Time(), base::Time(), false,
2221 false, false, COOKIE_PRIORITY_DEFAULT);
2222 CanonicalCookie cookie3(url_google_, "E", "F", url_google_.host(), "/",
2223 creation_time, base::Time(), base::Time(), false,
2224 false, false, COOKIE_PRIORITY_DEFAULT);
2225 CanonicalCookie cookie4(url_google_, "G", "H", url_google_.host(), "/",
2226 creation_time, base::Time(), base::Time(), false,
2227 false, false, COOKIE_PRIORITY_DEFAULT);
2228 CanonicalCookie cookie4_with_new_value(
2229 url_google_, "G", "iamnew", url_google_.host(), "/", creation_time,
2230 base::Time(), base::Time(), false, false, false, COOKIE_PRIORITY_DEFAULT);
2231 CanonicalCookie cookie5(url_google_, "I", "J", url_google_.host(), "/",
2232 creation_time, base::Time(), base::Time(), false,
2233 false, false, COOKIE_PRIORITY_DEFAULT);
2234 CanonicalCookie cookie5_with_new_creation_time(
2235 url_google_, "I", "J", url_google_.host(), "/", now, base::Time(),
2236 base::Time(), false, false, false, COOKIE_PRIORITY_DEFAULT);
2237 CanonicalCookie cookie6(url_google_, "K", "L", url_google_.host(), "/foo",
2238 creation_time, base::Time(), base::Time(), false,
2239 false, false, COOKIE_PRIORITY_DEFAULT);
2240 CanonicalCookie cookie6_with_new_path(
2241 url_google_, "K", "L", url_google_.host(), "/bar", creation_time,
2242 base::Time(), base::Time(), false, false, false, COOKIE_PRIORITY_DEFAULT);
2243 CanonicalCookie cookie7(url_google_, "M", "N", url_google_.host(), "/foo",
2244 creation_time, base::Time(), base::Time(), false,
2245 false, false, COOKIE_PRIORITY_DEFAULT);
2246 CanonicalCookie cookie7_with_new_path(
2247 url_google_, "M", "N", url_google_.host(), "/bar", creation_time,
2248 base::Time(), base::Time(), false, false, false, COOKIE_PRIORITY_DEFAULT);
2249
2250 CookieList old_cookies;
2251 old_cookies.push_back(cookie1);
2252 old_cookies.push_back(cookie2);
2253 old_cookies.push_back(cookie4);
2254 old_cookies.push_back(cookie5);
2255 old_cookies.push_back(cookie6);
2256 old_cookies.push_back(cookie7);
2257
2258 CookieList new_cookies;
2259 new_cookies.push_back(cookie1);
2260 new_cookies.push_back(cookie3);
2261 new_cookies.push_back(cookie4_with_new_value);
2262 new_cookies.push_back(cookie5_with_new_creation_time);
2263 new_cookies.push_back(cookie6_with_new_path);
2264 new_cookies.push_back(cookie7);
2265 new_cookies.push_back(cookie7_with_new_path);
2266
2267 CookieList cookies_to_add;
2268 CookieList cookies_to_delete;
2269
2270 cm->ComputeCookieDiff(&old_cookies, &new_cookies, &cookies_to_add,
2271 &cookies_to_delete);
2272
2273 // |cookie1| has not changed.
2274 EXPECT_FALSE(IsCookieInList(cookie1, cookies_to_add));
2275 EXPECT_FALSE(IsCookieInList(cookie1, cookies_to_delete));
2276
2277 // |cookie2| has been deleted.
2278 EXPECT_FALSE(IsCookieInList(cookie2, cookies_to_add));
2279 EXPECT_TRUE(IsCookieInList(cookie2, cookies_to_delete));
2280
2281 // |cookie3| has been added.
2282 EXPECT_TRUE(IsCookieInList(cookie3, cookies_to_add));
2283 EXPECT_FALSE(IsCookieInList(cookie3, cookies_to_delete));
2284
2285 // |cookie4| has a new value: new cookie overrides the old one (which does not
2286 // need to be explicitly removed).
2287 EXPECT_FALSE(IsCookieInList(cookie4, cookies_to_add));
2288 EXPECT_FALSE(IsCookieInList(cookie4, cookies_to_delete));
2289 EXPECT_TRUE(IsCookieInList(cookie4_with_new_value, cookies_to_add));
2290 EXPECT_FALSE(IsCookieInList(cookie4_with_new_value, cookies_to_delete));
2291
2292 // |cookie5| has a new creation time: new cookie overrides the old one (which
2293 // does not need to be explicitly removed).
2294 EXPECT_FALSE(IsCookieInList(cookie5, cookies_to_add));
2295 EXPECT_FALSE(IsCookieInList(cookie5, cookies_to_delete));
2296 EXPECT_TRUE(IsCookieInList(cookie5_with_new_creation_time, cookies_to_add));
2297 EXPECT_FALSE(
2298 IsCookieInList(cookie5_with_new_creation_time, cookies_to_delete));
2299
2300 // |cookie6| has a new path: the new cookie does not overrides the old one,
2301 // which needs to be explicitly removed.
2302 EXPECT_FALSE(IsCookieInList(cookie6, cookies_to_add));
2303 EXPECT_TRUE(IsCookieInList(cookie6, cookies_to_delete));
2304 EXPECT_TRUE(IsCookieInList(cookie6_with_new_path, cookies_to_add));
2305 EXPECT_FALSE(IsCookieInList(cookie6_with_new_path, cookies_to_delete));
2306
2307 // |cookie7| is kept and |cookie7_with_new_path| is added as a new cookie.
2308 EXPECT_FALSE(IsCookieInList(cookie7, cookies_to_add));
2309 EXPECT_FALSE(IsCookieInList(cookie7, cookies_to_delete));
2310 EXPECT_TRUE(IsCookieInList(cookie7_with_new_path, cookies_to_add));
2311 EXPECT_FALSE(IsCookieInList(cookie7_with_new_path, cookies_to_delete));
2312 }
2313
2109 TEST_F(CookieMonsterTest, HistogramCheck) { 2314 TEST_F(CookieMonsterTest, HistogramCheck) {
2110 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL)); 2315 scoped_refptr<CookieMonster> cm(new CookieMonster(NULL, NULL));
2111 // Should match call in InitializeHistograms, but doesn't really matter 2316 // Should match call in InitializeHistograms, but doesn't really matter
2112 // since the histogram should have been initialized by the CM construction 2317 // since the histogram should have been initialized by the CM construction
2113 // above. 2318 // above.
2114 base::HistogramBase* expired_histogram = base::Histogram::FactoryGet( 2319 base::HistogramBase* expired_histogram = base::Histogram::FactoryGet(
2115 "Cookie.ExpirationDurationMinutes", 1, 10 * 365 * 24 * 60, 50, 2320 "Cookie.ExpirationDurationMinutes", 1, 10 * 365 * 24 * 60, 50,
2116 base::Histogram::kUmaTargetedHistogramFlag); 2321 base::Histogram::kUmaTargetedHistogramFlag);
2117 2322
2118 scoped_ptr<base::HistogramSamples> samples1( 2323 scoped_ptr<base::HistogramSamples> samples1(
(...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after
2743 monster()->AddCallbackForCookie( 2948 monster()->AddCallbackForCookie(
2744 test_url_, "abc", 2949 test_url_, "abc",
2745 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); 2950 base::Bind(&RecordCookieChanges, &cookies1, nullptr)));
2746 SetCookie(monster(), test_url_, "abc=def"); 2951 SetCookie(monster(), test_url_, "abc=def");
2747 base::MessageLoop::current()->RunUntilIdle(); 2952 base::MessageLoop::current()->RunUntilIdle();
2748 EXPECT_EQ(1U, cookies0.size()); 2953 EXPECT_EQ(1U, cookies0.size());
2749 EXPECT_EQ(1U, cookies0.size()); 2954 EXPECT_EQ(1U, cookies0.size());
2750 } 2955 }
2751 2956
2752 } // namespace net 2957 } // namespace net
OLDNEW
« no previous file with comments | « net/cookies/cookie_monster.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698