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> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/message_loop/message_loop.h" | |
17 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
18 #include "base/metrics/histogram_samples.h" | 17 #include "base/metrics/histogram_samples.h" |
| 18 #include "base/run_loop.h" |
19 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
20 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
21 #include "base/strings/string_piece.h" | 21 #include "base/strings/string_piece.h" |
22 #include "base/strings/string_split.h" | 22 #include "base/strings/string_split.h" |
23 #include "base/strings/string_tokenizer.h" | 23 #include "base/strings/string_tokenizer.h" |
24 #include "base/strings/stringprintf.h" | 24 #include "base/strings/stringprintf.h" |
25 #include "base/test/histogram_tester.h" | 25 #include "base/test/histogram_tester.h" |
26 #include "base/threading/thread.h" | 26 #include "base/threading/thread.h" |
27 #include "base/threading/thread_task_runner_handle.h" | 27 #include "base/threading/thread_task_runner_handle.h" |
28 #include "base/time/time.h" | 28 #include "base/time/time.h" |
(...skipping 2499 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2528 TEST_F(CookieMonsterTest, FlushStore) { | 2528 TEST_F(CookieMonsterTest, FlushStore) { |
2529 scoped_refptr<CallbackCounter> counter(new CallbackCounter()); | 2529 scoped_refptr<CallbackCounter> counter(new CallbackCounter()); |
2530 scoped_refptr<FlushablePersistentStore> store(new FlushablePersistentStore()); | 2530 scoped_refptr<FlushablePersistentStore> store(new FlushablePersistentStore()); |
2531 std::unique_ptr<CookieMonster> cm(new CookieMonster(store.get(), nullptr)); | 2531 std::unique_ptr<CookieMonster> cm(new CookieMonster(store.get(), nullptr)); |
2532 | 2532 |
2533 ASSERT_EQ(0, store->flush_count()); | 2533 ASSERT_EQ(0, store->flush_count()); |
2534 ASSERT_EQ(0, counter->callback_count()); | 2534 ASSERT_EQ(0, counter->callback_count()); |
2535 | 2535 |
2536 // Before initialization, FlushStore() should just run the callback. | 2536 // Before initialization, FlushStore() should just run the callback. |
2537 cm->FlushStore(base::Bind(&CallbackCounter::Callback, counter.get())); | 2537 cm->FlushStore(base::Bind(&CallbackCounter::Callback, counter.get())); |
2538 base::MessageLoop::current()->RunUntilIdle(); | 2538 base::RunLoop().RunUntilIdle(); |
2539 | 2539 |
2540 ASSERT_EQ(0, store->flush_count()); | 2540 ASSERT_EQ(0, store->flush_count()); |
2541 ASSERT_EQ(1, counter->callback_count()); | 2541 ASSERT_EQ(1, counter->callback_count()); |
2542 | 2542 |
2543 // NULL callback is safe. | 2543 // NULL callback is safe. |
2544 cm->FlushStore(base::Closure()); | 2544 cm->FlushStore(base::Closure()); |
2545 base::MessageLoop::current()->RunUntilIdle(); | 2545 base::RunLoop().RunUntilIdle(); |
2546 | 2546 |
2547 ASSERT_EQ(0, store->flush_count()); | 2547 ASSERT_EQ(0, store->flush_count()); |
2548 ASSERT_EQ(1, counter->callback_count()); | 2548 ASSERT_EQ(1, counter->callback_count()); |
2549 | 2549 |
2550 // After initialization, FlushStore() should delegate to the store. | 2550 // After initialization, FlushStore() should delegate to the store. |
2551 GetAllCookies(cm.get()); // Force init. | 2551 GetAllCookies(cm.get()); // Force init. |
2552 cm->FlushStore(base::Bind(&CallbackCounter::Callback, counter.get())); | 2552 cm->FlushStore(base::Bind(&CallbackCounter::Callback, counter.get())); |
2553 base::MessageLoop::current()->RunUntilIdle(); | 2553 base::RunLoop().RunUntilIdle(); |
2554 | 2554 |
2555 ASSERT_EQ(1, store->flush_count()); | 2555 ASSERT_EQ(1, store->flush_count()); |
2556 ASSERT_EQ(2, counter->callback_count()); | 2556 ASSERT_EQ(2, counter->callback_count()); |
2557 | 2557 |
2558 // NULL callback is still safe. | 2558 // NULL callback is still safe. |
2559 cm->FlushStore(base::Closure()); | 2559 cm->FlushStore(base::Closure()); |
2560 base::MessageLoop::current()->RunUntilIdle(); | 2560 base::RunLoop().RunUntilIdle(); |
2561 | 2561 |
2562 ASSERT_EQ(2, store->flush_count()); | 2562 ASSERT_EQ(2, store->flush_count()); |
2563 ASSERT_EQ(2, counter->callback_count()); | 2563 ASSERT_EQ(2, counter->callback_count()); |
2564 | 2564 |
2565 // If there's no backing store, FlushStore() is always a safe no-op. | 2565 // If there's no backing store, FlushStore() is always a safe no-op. |
2566 cm.reset(new CookieMonster(nullptr, nullptr)); | 2566 cm.reset(new CookieMonster(nullptr, nullptr)); |
2567 GetAllCookies(cm.get()); // Force init. | 2567 GetAllCookies(cm.get()); // Force init. |
2568 cm->FlushStore(base::Closure()); | 2568 cm->FlushStore(base::Closure()); |
2569 base::MessageLoop::current()->RunUntilIdle(); | 2569 base::RunLoop().RunUntilIdle(); |
2570 | 2570 |
2571 ASSERT_EQ(2, counter->callback_count()); | 2571 ASSERT_EQ(2, counter->callback_count()); |
2572 | 2572 |
2573 cm->FlushStore(base::Bind(&CallbackCounter::Callback, counter.get())); | 2573 cm->FlushStore(base::Bind(&CallbackCounter::Callback, counter.get())); |
2574 base::MessageLoop::current()->RunUntilIdle(); | 2574 base::RunLoop().RunUntilIdle(); |
2575 | 2575 |
2576 ASSERT_EQ(3, counter->callback_count()); | 2576 ASSERT_EQ(3, counter->callback_count()); |
2577 } | 2577 } |
2578 | 2578 |
2579 TEST_F(CookieMonsterTest, SetAllCookies) { | 2579 TEST_F(CookieMonsterTest, SetAllCookies) { |
2580 scoped_refptr<FlushablePersistentStore> store(new FlushablePersistentStore()); | 2580 scoped_refptr<FlushablePersistentStore> store(new FlushablePersistentStore()); |
2581 std::unique_ptr<CookieMonster> cm(new CookieMonster(store.get(), nullptr)); | 2581 std::unique_ptr<CookieMonster> cm(new CookieMonster(store.get(), nullptr)); |
2582 cm->SetPersistSessionCookies(true); | 2582 cm->SetPersistSessionCookies(true); |
2583 | 2583 |
2584 EXPECT_TRUE(SetCookie(cm.get(), http_www_google_.url(), "U=V; path=/")); | 2584 EXPECT_TRUE(SetCookie(cm.get(), http_www_google_.url(), "U=V; path=/")); |
(...skipping 777 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3362 if (out_removes) | 3362 if (out_removes) |
3363 out_removes->push_back(removed); | 3363 out_removes->push_back(removed); |
3364 } | 3364 } |
3365 | 3365 |
3366 TEST_F(CookieMonsterNotificationTest, NoNotifyWithNoCookie) { | 3366 TEST_F(CookieMonsterNotificationTest, NoNotifyWithNoCookie) { |
3367 std::vector<CanonicalCookie> cookies; | 3367 std::vector<CanonicalCookie> cookies; |
3368 std::unique_ptr<CookieStore::CookieChangedSubscription> sub( | 3368 std::unique_ptr<CookieStore::CookieChangedSubscription> sub( |
3369 monster()->AddCallbackForCookie( | 3369 monster()->AddCallbackForCookie( |
3370 test_url_, "abc", | 3370 test_url_, "abc", |
3371 base::Bind(&RecordCookieChanges, &cookies, nullptr))); | 3371 base::Bind(&RecordCookieChanges, &cookies, nullptr))); |
3372 base::MessageLoop::current()->RunUntilIdle(); | 3372 base::RunLoop().RunUntilIdle(); |
3373 EXPECT_EQ(0U, cookies.size()); | 3373 EXPECT_EQ(0U, cookies.size()); |
3374 } | 3374 } |
3375 | 3375 |
3376 TEST_F(CookieMonsterNotificationTest, NoNotifyWithInitialCookie) { | 3376 TEST_F(CookieMonsterNotificationTest, NoNotifyWithInitialCookie) { |
3377 std::vector<CanonicalCookie> cookies; | 3377 std::vector<CanonicalCookie> cookies; |
3378 SetCookie(monster(), test_url_, "abc=def"); | 3378 SetCookie(monster(), test_url_, "abc=def"); |
3379 base::MessageLoop::current()->RunUntilIdle(); | 3379 base::RunLoop().RunUntilIdle(); |
3380 std::unique_ptr<CookieStore::CookieChangedSubscription> sub( | 3380 std::unique_ptr<CookieStore::CookieChangedSubscription> sub( |
3381 monster()->AddCallbackForCookie( | 3381 monster()->AddCallbackForCookie( |
3382 test_url_, "abc", | 3382 test_url_, "abc", |
3383 base::Bind(&RecordCookieChanges, &cookies, nullptr))); | 3383 base::Bind(&RecordCookieChanges, &cookies, nullptr))); |
3384 base::MessageLoop::current()->RunUntilIdle(); | 3384 base::RunLoop().RunUntilIdle(); |
3385 EXPECT_EQ(0U, cookies.size()); | 3385 EXPECT_EQ(0U, cookies.size()); |
3386 } | 3386 } |
3387 | 3387 |
3388 TEST_F(CookieMonsterNotificationTest, NotifyOnSet) { | 3388 TEST_F(CookieMonsterNotificationTest, NotifyOnSet) { |
3389 std::vector<CanonicalCookie> cookies; | 3389 std::vector<CanonicalCookie> cookies; |
3390 std::vector<bool> removes; | 3390 std::vector<bool> removes; |
3391 std::unique_ptr<CookieStore::CookieChangedSubscription> sub( | 3391 std::unique_ptr<CookieStore::CookieChangedSubscription> sub( |
3392 monster()->AddCallbackForCookie( | 3392 monster()->AddCallbackForCookie( |
3393 test_url_, "abc", | 3393 test_url_, "abc", |
3394 base::Bind(&RecordCookieChanges, &cookies, &removes))); | 3394 base::Bind(&RecordCookieChanges, &cookies, &removes))); |
3395 SetCookie(monster(), test_url_, "abc=def"); | 3395 SetCookie(monster(), test_url_, "abc=def"); |
3396 base::MessageLoop::current()->RunUntilIdle(); | 3396 base::RunLoop().RunUntilIdle(); |
3397 EXPECT_EQ(1U, cookies.size()); | 3397 EXPECT_EQ(1U, cookies.size()); |
3398 EXPECT_EQ(1U, removes.size()); | 3398 EXPECT_EQ(1U, removes.size()); |
3399 | 3399 |
3400 EXPECT_EQ("abc", cookies[0].Name()); | 3400 EXPECT_EQ("abc", cookies[0].Name()); |
3401 EXPECT_EQ("def", cookies[0].Value()); | 3401 EXPECT_EQ("def", cookies[0].Value()); |
3402 EXPECT_FALSE(removes[0]); | 3402 EXPECT_FALSE(removes[0]); |
3403 } | 3403 } |
3404 | 3404 |
3405 TEST_F(CookieMonsterNotificationTest, NotifyOnDelete) { | 3405 TEST_F(CookieMonsterNotificationTest, NotifyOnDelete) { |
3406 std::vector<CanonicalCookie> cookies; | 3406 std::vector<CanonicalCookie> cookies; |
3407 std::vector<bool> removes; | 3407 std::vector<bool> removes; |
3408 std::unique_ptr<CookieStore::CookieChangedSubscription> sub( | 3408 std::unique_ptr<CookieStore::CookieChangedSubscription> sub( |
3409 monster()->AddCallbackForCookie( | 3409 monster()->AddCallbackForCookie( |
3410 test_url_, "abc", | 3410 test_url_, "abc", |
3411 base::Bind(&RecordCookieChanges, &cookies, &removes))); | 3411 base::Bind(&RecordCookieChanges, &cookies, &removes))); |
3412 SetCookie(monster(), test_url_, "abc=def"); | 3412 SetCookie(monster(), test_url_, "abc=def"); |
3413 base::MessageLoop::current()->RunUntilIdle(); | 3413 base::RunLoop().RunUntilIdle(); |
3414 EXPECT_EQ(1U, cookies.size()); | 3414 EXPECT_EQ(1U, cookies.size()); |
3415 EXPECT_EQ(1U, removes.size()); | 3415 EXPECT_EQ(1U, removes.size()); |
3416 | 3416 |
3417 DeleteCookie(monster(), test_url_, "abc"); | 3417 DeleteCookie(monster(), test_url_, "abc"); |
3418 base::MessageLoop::current()->RunUntilIdle(); | 3418 base::RunLoop().RunUntilIdle(); |
3419 EXPECT_EQ(2U, cookies.size()); | 3419 EXPECT_EQ(2U, cookies.size()); |
3420 EXPECT_EQ(2U, removes.size()); | 3420 EXPECT_EQ(2U, removes.size()); |
3421 | 3421 |
3422 EXPECT_EQ("abc", cookies[1].Name()); | 3422 EXPECT_EQ("abc", cookies[1].Name()); |
3423 EXPECT_EQ("def", cookies[1].Value()); | 3423 EXPECT_EQ("def", cookies[1].Value()); |
3424 EXPECT_TRUE(removes[1]); | 3424 EXPECT_TRUE(removes[1]); |
3425 } | 3425 } |
3426 | 3426 |
3427 TEST_F(CookieMonsterNotificationTest, NotifyOnUpdate) { | 3427 TEST_F(CookieMonsterNotificationTest, NotifyOnUpdate) { |
3428 std::vector<CanonicalCookie> cookies; | 3428 std::vector<CanonicalCookie> cookies; |
3429 std::vector<bool> removes; | 3429 std::vector<bool> removes; |
3430 std::unique_ptr<CookieStore::CookieChangedSubscription> sub( | 3430 std::unique_ptr<CookieStore::CookieChangedSubscription> sub( |
3431 monster()->AddCallbackForCookie( | 3431 monster()->AddCallbackForCookie( |
3432 test_url_, "abc", | 3432 test_url_, "abc", |
3433 base::Bind(&RecordCookieChanges, &cookies, &removes))); | 3433 base::Bind(&RecordCookieChanges, &cookies, &removes))); |
3434 SetCookie(monster(), test_url_, "abc=def"); | 3434 SetCookie(monster(), test_url_, "abc=def"); |
3435 base::MessageLoop::current()->RunUntilIdle(); | 3435 base::RunLoop().RunUntilIdle(); |
3436 EXPECT_EQ(1U, cookies.size()); | 3436 EXPECT_EQ(1U, cookies.size()); |
3437 | 3437 |
3438 // Replacing an existing cookie is actually a two-phase delete + set | 3438 // Replacing an existing cookie is actually a two-phase delete + set |
3439 // operation, so we get an extra notification. | 3439 // operation, so we get an extra notification. |
3440 SetCookie(monster(), test_url_, "abc=ghi"); | 3440 SetCookie(monster(), test_url_, "abc=ghi"); |
3441 base::MessageLoop::current()->RunUntilIdle(); | 3441 base::RunLoop().RunUntilIdle(); |
3442 | 3442 |
3443 EXPECT_EQ(3U, cookies.size()); | 3443 EXPECT_EQ(3U, cookies.size()); |
3444 EXPECT_EQ(3U, removes.size()); | 3444 EXPECT_EQ(3U, removes.size()); |
3445 | 3445 |
3446 EXPECT_EQ("abc", cookies[1].Name()); | 3446 EXPECT_EQ("abc", cookies[1].Name()); |
3447 EXPECT_EQ("def", cookies[1].Value()); | 3447 EXPECT_EQ("def", cookies[1].Value()); |
3448 EXPECT_TRUE(removes[1]); | 3448 EXPECT_TRUE(removes[1]); |
3449 | 3449 |
3450 EXPECT_EQ("abc", cookies[2].Name()); | 3450 EXPECT_EQ("abc", cookies[2].Name()); |
3451 EXPECT_EQ("ghi", cookies[2].Value()); | 3451 EXPECT_EQ("ghi", cookies[2].Value()); |
3452 EXPECT_FALSE(removes[2]); | 3452 EXPECT_FALSE(removes[2]); |
3453 } | 3453 } |
3454 | 3454 |
3455 TEST_F(CookieMonsterNotificationTest, MultipleNotifies) { | 3455 TEST_F(CookieMonsterNotificationTest, MultipleNotifies) { |
3456 std::vector<CanonicalCookie> cookies0; | 3456 std::vector<CanonicalCookie> cookies0; |
3457 std::vector<CanonicalCookie> cookies1; | 3457 std::vector<CanonicalCookie> cookies1; |
3458 std::unique_ptr<CookieStore::CookieChangedSubscription> sub0( | 3458 std::unique_ptr<CookieStore::CookieChangedSubscription> sub0( |
3459 monster()->AddCallbackForCookie( | 3459 monster()->AddCallbackForCookie( |
3460 test_url_, "abc", | 3460 test_url_, "abc", |
3461 base::Bind(&RecordCookieChanges, &cookies0, nullptr))); | 3461 base::Bind(&RecordCookieChanges, &cookies0, nullptr))); |
3462 std::unique_ptr<CookieStore::CookieChangedSubscription> sub1( | 3462 std::unique_ptr<CookieStore::CookieChangedSubscription> sub1( |
3463 monster()->AddCallbackForCookie( | 3463 monster()->AddCallbackForCookie( |
3464 test_url_, "def", | 3464 test_url_, "def", |
3465 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); | 3465 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); |
3466 SetCookie(monster(), test_url_, "abc=def"); | 3466 SetCookie(monster(), test_url_, "abc=def"); |
3467 base::MessageLoop::current()->RunUntilIdle(); | 3467 base::RunLoop().RunUntilIdle(); |
3468 EXPECT_EQ(1U, cookies0.size()); | 3468 EXPECT_EQ(1U, cookies0.size()); |
3469 EXPECT_EQ(0U, cookies1.size()); | 3469 EXPECT_EQ(0U, cookies1.size()); |
3470 SetCookie(monster(), test_url_, "def=abc"); | 3470 SetCookie(monster(), test_url_, "def=abc"); |
3471 base::MessageLoop::current()->RunUntilIdle(); | 3471 base::RunLoop().RunUntilIdle(); |
3472 EXPECT_EQ(1U, cookies0.size()); | 3472 EXPECT_EQ(1U, cookies0.size()); |
3473 EXPECT_EQ(1U, cookies1.size()); | 3473 EXPECT_EQ(1U, cookies1.size()); |
3474 } | 3474 } |
3475 | 3475 |
3476 TEST_F(CookieMonsterNotificationTest, MultipleSameNotifies) { | 3476 TEST_F(CookieMonsterNotificationTest, MultipleSameNotifies) { |
3477 std::vector<CanonicalCookie> cookies0; | 3477 std::vector<CanonicalCookie> cookies0; |
3478 std::vector<CanonicalCookie> cookies1; | 3478 std::vector<CanonicalCookie> cookies1; |
3479 std::unique_ptr<CookieStore::CookieChangedSubscription> sub0( | 3479 std::unique_ptr<CookieStore::CookieChangedSubscription> sub0( |
3480 monster()->AddCallbackForCookie( | 3480 monster()->AddCallbackForCookie( |
3481 test_url_, "abc", | 3481 test_url_, "abc", |
3482 base::Bind(&RecordCookieChanges, &cookies0, nullptr))); | 3482 base::Bind(&RecordCookieChanges, &cookies0, nullptr))); |
3483 std::unique_ptr<CookieStore::CookieChangedSubscription> sub1( | 3483 std::unique_ptr<CookieStore::CookieChangedSubscription> sub1( |
3484 monster()->AddCallbackForCookie( | 3484 monster()->AddCallbackForCookie( |
3485 test_url_, "abc", | 3485 test_url_, "abc", |
3486 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); | 3486 base::Bind(&RecordCookieChanges, &cookies1, nullptr))); |
3487 SetCookie(monster(), test_url_, "abc=def"); | 3487 SetCookie(monster(), test_url_, "abc=def"); |
3488 base::MessageLoop::current()->RunUntilIdle(); | 3488 base::RunLoop().RunUntilIdle(); |
3489 EXPECT_EQ(1U, cookies0.size()); | 3489 EXPECT_EQ(1U, cookies0.size()); |
3490 EXPECT_EQ(1U, cookies0.size()); | 3490 EXPECT_EQ(1U, cookies0.size()); |
3491 } | 3491 } |
3492 | 3492 |
3493 } // namespace net | 3493 } // namespace net |
OLD | NEW |