| 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.h" | 5 #include "net/cookies/cookie_monster.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 3344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3355 | 3355 |
| 3356 protected: | 3356 protected: |
| 3357 const GURL test_url_; | 3357 const GURL test_url_; |
| 3358 | 3358 |
| 3359 private: | 3359 private: |
| 3360 scoped_refptr<MockPersistentCookieStore> store_; | 3360 scoped_refptr<MockPersistentCookieStore> store_; |
| 3361 std::unique_ptr<CookieMonster> monster_; | 3361 std::unique_ptr<CookieMonster> monster_; |
| 3362 }; | 3362 }; |
| 3363 | 3363 |
| 3364 void RecordCookieChanges(std::vector<CanonicalCookie>* out_cookies, | 3364 void RecordCookieChanges(std::vector<CanonicalCookie>* out_cookies, |
| 3365 std::vector<bool>* out_removes, | 3365 std::vector<CookieStore::ChangeCause>* out_causes, |
| 3366 const CanonicalCookie& cookie, | 3366 const CanonicalCookie& cookie, |
| 3367 bool removed) { | 3367 CookieStore::ChangeCause cause) { |
| 3368 DCHECK(out_cookies); | 3368 DCHECK(out_cookies); |
| 3369 out_cookies->push_back(cookie); | 3369 out_cookies->push_back(cookie); |
| 3370 if (out_removes) | 3370 if (out_causes) |
| 3371 out_removes->push_back(removed); | 3371 out_causes->push_back(cause); |
| 3372 } | 3372 } |
| 3373 | 3373 |
| 3374 TEST_F(CookieMonsterNotificationTest, NoNotifyWithNoCookie) { | 3374 TEST_F(CookieMonsterNotificationTest, NoNotifyWithNoCookie) { |
| 3375 std::vector<CanonicalCookie> cookies; | 3375 std::vector<CanonicalCookie> cookies; |
| 3376 std::unique_ptr<CookieStore::CookieChangedSubscription> sub( | 3376 std::unique_ptr<CookieStore::CookieChangedSubscription> sub( |
| 3377 monster()->AddCallbackForCookie( | 3377 monster()->AddCallbackForCookie( |
| 3378 test_url_, "abc", | 3378 test_url_, "abc", |
| 3379 base::Bind(&RecordCookieChanges, &cookies, nullptr))); | 3379 base::Bind(&RecordCookieChanges, &cookies, nullptr))); |
| 3380 base::RunLoop().RunUntilIdle(); | 3380 base::RunLoop().RunUntilIdle(); |
| 3381 EXPECT_EQ(0U, cookies.size()); | 3381 EXPECT_EQ(0U, cookies.size()); |
| 3382 } | 3382 } |
| 3383 | 3383 |
| 3384 TEST_F(CookieMonsterNotificationTest, NoNotifyWithInitialCookie) { | 3384 TEST_F(CookieMonsterNotificationTest, NoNotifyWithInitialCookie) { |
| 3385 std::vector<CanonicalCookie> cookies; | 3385 std::vector<CanonicalCookie> cookies; |
| 3386 SetCookie(monster(), test_url_, "abc=def"); | 3386 SetCookie(monster(), test_url_, "abc=def"); |
| 3387 base::RunLoop().RunUntilIdle(); | 3387 base::RunLoop().RunUntilIdle(); |
| 3388 std::unique_ptr<CookieStore::CookieChangedSubscription> sub( | 3388 std::unique_ptr<CookieStore::CookieChangedSubscription> sub( |
| 3389 monster()->AddCallbackForCookie( | 3389 monster()->AddCallbackForCookie( |
| 3390 test_url_, "abc", | 3390 test_url_, "abc", |
| 3391 base::Bind(&RecordCookieChanges, &cookies, nullptr))); | 3391 base::Bind(&RecordCookieChanges, &cookies, nullptr))); |
| 3392 base::RunLoop().RunUntilIdle(); | 3392 base::RunLoop().RunUntilIdle(); |
| 3393 EXPECT_EQ(0U, cookies.size()); | 3393 EXPECT_EQ(0U, cookies.size()); |
| 3394 } | 3394 } |
| 3395 | 3395 |
| 3396 TEST_F(CookieMonsterNotificationTest, NotifyOnSet) { | 3396 TEST_F(CookieMonsterNotificationTest, NotifyOnSet) { |
| 3397 std::vector<CanonicalCookie> cookies; | 3397 std::vector<CanonicalCookie> cookies; |
| 3398 std::vector<bool> removes; | 3398 std::vector<CookieStore::ChangeCause> causes; |
| 3399 std::unique_ptr<CookieStore::CookieChangedSubscription> sub( | 3399 std::unique_ptr<CookieStore::CookieChangedSubscription> sub( |
| 3400 monster()->AddCallbackForCookie( | 3400 monster()->AddCallbackForCookie( |
| 3401 test_url_, "abc", | 3401 test_url_, "abc", |
| 3402 base::Bind(&RecordCookieChanges, &cookies, &removes))); | 3402 base::Bind(&RecordCookieChanges, &cookies, &causes))); |
| 3403 SetCookie(monster(), test_url_, "abc=def"); | 3403 SetCookie(monster(), test_url_, "abc=def"); |
| 3404 base::RunLoop().RunUntilIdle(); | 3404 base::RunLoop().RunUntilIdle(); |
| 3405 EXPECT_EQ(1U, cookies.size()); | 3405 EXPECT_EQ(1U, cookies.size()); |
| 3406 EXPECT_EQ(1U, removes.size()); | 3406 EXPECT_EQ(1U, causes.size()); |
| 3407 | 3407 |
| 3408 EXPECT_EQ("abc", cookies[0].Name()); | 3408 EXPECT_EQ("abc", cookies[0].Name()); |
| 3409 EXPECT_EQ("def", cookies[0].Value()); | 3409 EXPECT_EQ("def", cookies[0].Value()); |
| 3410 EXPECT_FALSE(removes[0]); | 3410 EXPECT_EQ(CookieStore::ChangeCause::INSERTED, causes[0]); |
| 3411 } | 3411 } |
| 3412 | 3412 |
| 3413 TEST_F(CookieMonsterNotificationTest, NotifyOnDelete) { | 3413 TEST_F(CookieMonsterNotificationTest, NotifyOnDelete) { |
| 3414 std::vector<CanonicalCookie> cookies; | 3414 std::vector<CanonicalCookie> cookies; |
| 3415 std::vector<bool> removes; | 3415 std::vector<CookieStore::ChangeCause> causes; |
| 3416 std::unique_ptr<CookieStore::CookieChangedSubscription> sub( | 3416 std::unique_ptr<CookieStore::CookieChangedSubscription> sub( |
| 3417 monster()->AddCallbackForCookie( | 3417 monster()->AddCallbackForCookie( |
| 3418 test_url_, "abc", | 3418 test_url_, "abc", |
| 3419 base::Bind(&RecordCookieChanges, &cookies, &removes))); | 3419 base::Bind(&RecordCookieChanges, &cookies, &causes))); |
| 3420 SetCookie(monster(), test_url_, "abc=def"); | 3420 SetCookie(monster(), test_url_, "abc=def"); |
| 3421 base::RunLoop().RunUntilIdle(); | 3421 base::RunLoop().RunUntilIdle(); |
| 3422 EXPECT_EQ(1U, cookies.size()); | 3422 EXPECT_EQ(1U, cookies.size()); |
| 3423 EXPECT_EQ(1U, removes.size()); | 3423 EXPECT_EQ(1U, causes.size()); |
| 3424 | 3424 |
| 3425 DeleteCookie(monster(), test_url_, "abc"); | 3425 DeleteCookie(monster(), test_url_, "abc"); |
| 3426 base::RunLoop().RunUntilIdle(); | 3426 base::RunLoop().RunUntilIdle(); |
| 3427 EXPECT_EQ(2U, cookies.size()); | 3427 EXPECT_EQ(2U, cookies.size()); |
| 3428 EXPECT_EQ(2U, removes.size()); | 3428 EXPECT_EQ(2U, causes.size()); |
| 3429 | 3429 |
| 3430 EXPECT_EQ("abc", cookies[1].Name()); | 3430 EXPECT_EQ("abc", cookies[1].Name()); |
| 3431 EXPECT_EQ("def", cookies[1].Value()); | 3431 EXPECT_EQ("def", cookies[1].Value()); |
| 3432 EXPECT_TRUE(removes[1]); | 3432 EXPECT_EQ(CookieStore::ChangeCause::EXPLICIT, causes[1]); |
| 3433 } | 3433 } |
| 3434 | 3434 |
| 3435 TEST_F(CookieMonsterNotificationTest, NotifyOnUpdate) { | 3435 TEST_F(CookieMonsterNotificationTest, NotifyOnUpdate) { |
| 3436 std::vector<CanonicalCookie> cookies; | 3436 std::vector<CanonicalCookie> cookies; |
| 3437 std::vector<bool> removes; | 3437 std::vector<CookieStore::ChangeCause> causes; |
| 3438 std::unique_ptr<CookieStore::CookieChangedSubscription> sub( | 3438 std::unique_ptr<CookieStore::CookieChangedSubscription> sub( |
| 3439 monster()->AddCallbackForCookie( | 3439 monster()->AddCallbackForCookie( |
| 3440 test_url_, "abc", | 3440 test_url_, "abc", |
| 3441 base::Bind(&RecordCookieChanges, &cookies, &removes))); | 3441 base::Bind(&RecordCookieChanges, &cookies, &causes))); |
| 3442 SetCookie(monster(), test_url_, "abc=def"); | 3442 SetCookie(monster(), test_url_, "abc=def"); |
| 3443 base::RunLoop().RunUntilIdle(); | 3443 base::RunLoop().RunUntilIdle(); |
| 3444 EXPECT_EQ(1U, cookies.size()); | 3444 EXPECT_EQ(1U, cookies.size()); |
| 3445 | 3445 |
| 3446 // Replacing an existing cookie is actually a two-phase delete + set | 3446 // Replacing an existing cookie is actually a two-phase delete + set |
| 3447 // operation, so we get an extra notification. | 3447 // operation, so we get an extra notification. |
| 3448 SetCookie(monster(), test_url_, "abc=ghi"); | 3448 SetCookie(monster(), test_url_, "abc=ghi"); |
| 3449 base::RunLoop().RunUntilIdle(); | 3449 base::RunLoop().RunUntilIdle(); |
| 3450 | 3450 |
| 3451 EXPECT_EQ(3U, cookies.size()); | 3451 EXPECT_EQ(3U, cookies.size()); |
| 3452 EXPECT_EQ(3U, removes.size()); | 3452 EXPECT_EQ(3U, causes.size()); |
| 3453 | 3453 |
| 3454 EXPECT_EQ("abc", cookies[1].Name()); | 3454 EXPECT_EQ("abc", cookies[1].Name()); |
| 3455 EXPECT_EQ("def", cookies[1].Value()); | 3455 EXPECT_EQ("def", cookies[1].Value()); |
| 3456 EXPECT_TRUE(removes[1]); | 3456 EXPECT_EQ(CookieStore::ChangeCause::OVERWRITE, causes[1]); |
| 3457 | 3457 |
| 3458 EXPECT_EQ("abc", cookies[2].Name()); | 3458 EXPECT_EQ("abc", cookies[2].Name()); |
| 3459 EXPECT_EQ("ghi", cookies[2].Value()); | 3459 EXPECT_EQ("ghi", cookies[2].Value()); |
| 3460 EXPECT_FALSE(removes[2]); | 3460 EXPECT_EQ(CookieStore::ChangeCause::INSERTED, causes[2]); |
| 3461 } | 3461 } |
| 3462 | 3462 |
| 3463 TEST_F(CookieMonsterNotificationTest, MultipleNotifies) { | 3463 TEST_F(CookieMonsterNotificationTest, MultipleNotifies) { |
| 3464 std::vector<CanonicalCookie> cookies0; | 3464 std::vector<CanonicalCookie> cookies0; |
| 3465 std::vector<CanonicalCookie> cookies1; | 3465 std::vector<CanonicalCookie> cookies1; |
| 3466 std::unique_ptr<CookieStore::CookieChangedSubscription> sub0( | 3466 std::unique_ptr<CookieStore::CookieChangedSubscription> sub0( |
| 3467 monster()->AddCallbackForCookie( | 3467 monster()->AddCallbackForCookie( |
| 3468 test_url_, "abc", | 3468 test_url_, "abc", |
| 3469 base::Bind(&RecordCookieChanges, &cookies0, nullptr))); | 3469 base::Bind(&RecordCookieChanges, &cookies0, nullptr))); |
| 3470 std::unique_ptr<CookieStore::CookieChangedSubscription> sub1( | 3470 std::unique_ptr<CookieStore::CookieChangedSubscription> sub1( |
| (...skipping 21 matching lines...) Expand all Loading... |
| 3492 monster()->AddCallbackForCookie( | 3492 monster()->AddCallbackForCookie( |
| 3493 test_url_, "abc", | 3493 test_url_, "abc", |
| 3494 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); | 3494 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); |
| 3495 SetCookie(monster(), test_url_, "abc=def"); | 3495 SetCookie(monster(), test_url_, "abc=def"); |
| 3496 base::RunLoop().RunUntilIdle(); | 3496 base::RunLoop().RunUntilIdle(); |
| 3497 EXPECT_EQ(1U, cookies0.size()); | 3497 EXPECT_EQ(1U, cookies0.size()); |
| 3498 EXPECT_EQ(1U, cookies0.size()); | 3498 EXPECT_EQ(1U, cookies0.size()); |
| 3499 } | 3499 } |
| 3500 | 3500 |
| 3501 } // namespace net | 3501 } // namespace net |
| OLD | NEW |