| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/net/cookies/cookie_store_ios_persistent.h" |
| 6 |
| 7 #import <Foundation/Foundation.h> |
| 8 |
| 9 #include <memory> |
| 10 |
| 11 #include "base/bind_helpers.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" |
| 16 #import "ios/net/cookies/cookie_store_ios_test_util.h" |
| 17 #include "net/cookies/cookie_store_unittest.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 namespace net { |
| 21 |
| 22 struct InactiveCookieStoreIOSTestTraits { |
| 23 static std::unique_ptr<net::CookieStore> Create() { |
| 24 return base::MakeUnique<CookieStoreIOSPersistent>(nullptr); |
| 25 } |
| 26 |
| 27 static const bool is_cookie_monster = false; |
| 28 static const bool supports_http_only = false; |
| 29 static const bool supports_non_dotted_domains = true; |
| 30 static const bool preserves_trailing_dots = true; |
| 31 static const bool filters_schemes = false; |
| 32 static const bool has_path_prefix_bug = false; |
| 33 static const int creation_time_granularity_in_ms = 0; |
| 34 static const int enforces_prefixes = true; |
| 35 static const bool enforce_strict_secure = false; |
| 36 |
| 37 base::MessageLoop loop_; |
| 38 }; |
| 39 |
| 40 INSTANTIATE_TYPED_TEST_CASE_P(InactiveCookieStoreIOS, |
| 41 CookieStoreTest, |
| 42 InactiveCookieStoreIOSTestTraits); |
| 43 |
| 44 namespace { |
| 45 |
| 46 // Test fixture to exercise net::CookieStoreIOSPersistent created with |
| 47 // TestPersistentCookieStore back-end and not synchronized with |
| 48 // NSHTTPCookieStorage. |
| 49 class CookieStoreIOSPersistentTest : public testing::Test { |
| 50 public: |
| 51 CookieStoreIOSPersistentTest() |
| 52 : kTestCookieURL("http://foo.google.com/bar"), |
| 53 backend_(new net::TestPersistentCookieStore), |
| 54 store_( |
| 55 base::MakeUnique<net::CookieStoreIOSPersistent>(backend_.get())) { |
| 56 cookie_changed_callback_ = store_->AddCallbackForCookie( |
| 57 kTestCookieURL, "abc", |
| 58 base::Bind(&net::RecordCookieChanges, &cookies_changed_, |
| 59 &cookies_removed_)); |
| 60 } |
| 61 |
| 62 ~CookieStoreIOSPersistentTest() override {} |
| 63 |
| 64 // Gets the cookies. |callback| will be called on completion. |
| 65 void GetCookies(const net::CookieStore::GetCookiesCallback& callback) { |
| 66 net::CookieOptions options; |
| 67 options.set_include_httponly(); |
| 68 store_->GetCookiesWithOptionsAsync(kTestCookieURL, options, callback); |
| 69 } |
| 70 |
| 71 // Sets a cookie. |
| 72 void SetCookie(const std::string& cookie_line) { |
| 73 net::SetCookie(cookie_line, kTestCookieURL, store_.get()); |
| 74 } |
| 75 |
| 76 private: |
| 77 const GURL kTestCookieURL; |
| 78 |
| 79 protected: |
| 80 base::MessageLoop loop_; |
| 81 scoped_refptr<net::TestPersistentCookieStore> backend_; |
| 82 std::unique_ptr<net::CookieStoreIOS> store_; |
| 83 std::unique_ptr<net::CookieStore::CookieChangedSubscription> |
| 84 cookie_changed_callback_; |
| 85 std::vector<net::CanonicalCookie> cookies_changed_; |
| 86 std::vector<bool> cookies_removed_; |
| 87 }; |
| 88 |
| 89 } // namespace |
| 90 |
| 91 TEST_F(CookieStoreIOSPersistentTest, SetCookieCallsHook) { |
| 92 ClearCookies(); |
| 93 SetCookie("abc=def"); |
| 94 EXPECT_EQ(0U, cookies_changed_.size()); |
| 95 EXPECT_EQ(0U, cookies_removed_.size()); |
| 96 backend_->RunLoadedCallback(); |
| 97 base::RunLoop().RunUntilIdle(); |
| 98 EXPECT_EQ(1U, cookies_changed_.size()); |
| 99 EXPECT_EQ(1U, cookies_removed_.size()); |
| 100 EXPECT_EQ("abc", cookies_changed_[0].Name()); |
| 101 EXPECT_EQ("def", cookies_changed_[0].Value()); |
| 102 EXPECT_FALSE(cookies_removed_[0]); |
| 103 |
| 104 // Replacing an existing cookie is actually a two-phase delete + set |
| 105 // operation, so we get an extra notification. |
| 106 SetCookie("abc=ghi"); |
| 107 EXPECT_EQ(3U, cookies_changed_.size()); |
| 108 EXPECT_EQ(3U, cookies_removed_.size()); |
| 109 EXPECT_EQ("abc", cookies_changed_[1].Name()); |
| 110 EXPECT_EQ("def", cookies_changed_[1].Value()); |
| 111 EXPECT_TRUE(cookies_removed_[1]); |
| 112 EXPECT_EQ("abc", cookies_changed_[2].Name()); |
| 113 EXPECT_EQ("ghi", cookies_changed_[2].Value()); |
| 114 EXPECT_FALSE(cookies_removed_[2]); |
| 115 } |
| 116 |
| 117 // Tests that cookies can be read before the backend is loaded. |
| 118 TEST_F(CookieStoreIOSPersistentTest, NotSynchronized) { |
| 119 // Start fetching the cookie. |
| 120 GetCookieCallback callback; |
| 121 GetCookies(base::Bind(&GetCookieCallback::Run, base::Unretained(&callback))); |
| 122 // Backend loading completes. |
| 123 backend_->RunLoadedCallback(); |
| 124 EXPECT_TRUE(callback.did_run()); |
| 125 EXPECT_EQ("a=b", callback.cookie_line()); |
| 126 } |
| 127 |
| 128 } // namespace net |
| OLD | NEW |