| OLD | NEW |
| 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 |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/strings/sys_string_conversions.h" | 16 #include "base/strings/sys_string_conversions.h" |
| 17 #import "ios/net/cookies/cookie_store_ios_persistent.h" |
| 17 #import "net/base/mac/url_conversions.h" | 18 #import "net/base/mac/url_conversions.h" |
| 18 #include "net/cookies/cookie_store_unittest.h" | 19 #include "net/cookies/cookie_store_unittest.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 // Clears the underlying NSHTTPCookieStorage. | 23 // Clears the underlying NSHTTPCookieStorage. |
| 23 void ClearCookies() { | 24 void ClearCookies() { |
| 24 NSHTTPCookieStorage* store = [NSHTTPCookieStorage sharedHTTPCookieStorage]; | 25 NSHTTPCookieStorage* store = [NSHTTPCookieStorage sharedHTTPCookieStorage]; |
| 25 [store setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; | 26 [store setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; |
| 26 NSArray* cookies = [store cookies]; | 27 NSArray* cookies = [store cookies]; |
| 27 for (NSHTTPCookie* cookie in cookies) | 28 for (NSHTTPCookie* cookie in cookies) |
| 28 [store deleteCookie:cookie]; | 29 [store deleteCookie:cookie]; |
| 29 EXPECT_EQ(0u, [[store cookies] count]); | 30 EXPECT_EQ(0u, [[store cookies] count]); |
| 30 } | 31 } |
| 31 } // namespace | 32 } // namespace |
| 32 | 33 |
| 33 namespace net { | 34 namespace net { |
| 34 | 35 |
| 35 struct CookieStoreIOSTestTraits { | 36 struct CookieStoreIOSTestTraits { |
| 36 static std::unique_ptr<net::CookieStore> Create() { | 37 static std::unique_ptr<net::CookieStore> Create() { |
| 37 ClearCookies(); | 38 ClearCookies(); |
| 38 std::unique_ptr<CookieStoreIOS> store(new CookieStoreIOS(nullptr)); | 39 return base::MakeUnique<CookieStoreIOS>( |
| 39 store->synchronization_state_ = CookieStoreIOS::SYNCHRONIZED; | 40 [NSHTTPCookieStorage sharedHTTPCookieStorage]); |
| 40 return std::move(store); | |
| 41 } | 41 } |
| 42 | 42 |
| 43 static const bool supports_http_only = false; | 43 static const bool supports_http_only = false; |
| 44 static const bool supports_non_dotted_domains = false; | 44 static const bool supports_non_dotted_domains = false; |
| 45 static const bool preserves_trailing_dots = false; | 45 static const bool preserves_trailing_dots = false; |
| 46 static const bool filters_schemes = false; | 46 static const bool filters_schemes = false; |
| 47 static const bool has_path_prefix_bug = true; | 47 static const bool has_path_prefix_bug = true; |
| 48 static const int creation_time_granularity_in_ms = 1000; | 48 static const int creation_time_granularity_in_ms = 1000; |
| 49 | 49 |
| 50 base::MessageLoop loop_; | 50 base::MessageLoop loop_; |
| 51 }; | 51 }; |
| 52 | 52 |
| 53 struct InactiveCookieStoreIOSTestTraits { | 53 struct InactiveCookieStoreIOSTestTraits { |
| 54 static std::unique_ptr<net::CookieStore> Create() { | 54 static std::unique_ptr<net::CookieStore> Create() { |
| 55 return base::MakeUnique<CookieStoreIOS>(nullptr); | 55 return base::MakeUnique<CookieStoreIOSPersistent>(nullptr); |
| 56 } | 56 } |
| 57 | 57 |
| 58 static const bool is_cookie_monster = false; | 58 static const bool is_cookie_monster = false; |
| 59 static const bool supports_http_only = false; | 59 static const bool supports_http_only = false; |
| 60 static const bool supports_non_dotted_domains = true; | 60 static const bool supports_non_dotted_domains = true; |
| 61 static const bool preserves_trailing_dots = true; | 61 static const bool preserves_trailing_dots = true; |
| 62 static const bool filters_schemes = false; | 62 static const bool filters_schemes = false; |
| 63 static const bool has_path_prefix_bug = false; | 63 static const bool has_path_prefix_bug = false; |
| 64 static const int creation_time_granularity_in_ms = 0; | 64 static const int creation_time_granularity_in_ms = 0; |
| 65 static const int enforces_prefixes = true; | 65 static const int enforces_prefixes = true; |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 } | 223 } |
| 224 | 224 |
| 225 // Test fixture to exersize net::CookieStoreIOS created with | 225 // Test fixture to exersize net::CookieStoreIOS created with |
| 226 // TestPersistentCookieStore backend and not synchronized with | 226 // TestPersistentCookieStore backend and not synchronized with |
| 227 // NSHTTPCookieStorage. | 227 // NSHTTPCookieStorage. |
| 228 class NotSynchronizedCookieStoreIOSWithBackend : public testing::Test { | 228 class NotSynchronizedCookieStoreIOSWithBackend : public testing::Test { |
| 229 public: | 229 public: |
| 230 NotSynchronizedCookieStoreIOSWithBackend() | 230 NotSynchronizedCookieStoreIOSWithBackend() |
| 231 : kTestCookieURL("http://foo.google.com/bar"), | 231 : kTestCookieURL("http://foo.google.com/bar"), |
| 232 backend_(new TestPersistentCookieStore), | 232 backend_(new TestPersistentCookieStore), |
| 233 store_(new net::CookieStoreIOS(backend_.get())) { | 233 store_(base::MakeUnique<net::CookieStoreIOSPersistent>(backend_.get())) |
| 234 { |
| 234 cookie_changed_callback_ = store_->AddCallbackForCookie( | 235 cookie_changed_callback_ = store_->AddCallbackForCookie( |
| 235 kTestCookieURL, "abc", | 236 kTestCookieURL, "abc", |
| 236 base::Bind(&RecordCookieChanges, &cookies_changed_, &cookies_removed_)); | 237 base::Bind(&RecordCookieChanges, &cookies_changed_, &cookies_removed_)); |
| 237 } | 238 } |
| 238 | 239 |
| 239 ~NotSynchronizedCookieStoreIOSWithBackend() override {} | 240 ~NotSynchronizedCookieStoreIOSWithBackend() override {} |
| 240 | 241 |
| 241 // Gets the cookies. |callback| will be called on completion. | 242 // Gets the cookies. |callback| will be called on completion. |
| 242 void GetCookies(const net::CookieStore::GetCookiesCallback& callback) { | 243 void GetCookies(const net::CookieStore::GetCookiesCallback& callback) { |
| 243 net::CookieOptions options; | 244 net::CookieOptions options; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 266 // Test fixture to exersize net::CookieStoreIOS created without backend and | 267 // Test fixture to exersize net::CookieStoreIOS created without backend and |
| 267 // synchronized with |[NSHTTPCookieStorage sharedHTTPCookieStorage]|. | 268 // synchronized with |[NSHTTPCookieStorage sharedHTTPCookieStorage]|. |
| 268 class SynchronizedCookieStoreIOS : public testing::Test { | 269 class SynchronizedCookieStoreIOS : public testing::Test { |
| 269 public: | 270 public: |
| 270 SynchronizedCookieStoreIOS() | 271 SynchronizedCookieStoreIOS() |
| 271 : kTestCookieURL("http://foo.google.com/bar"), | 272 : kTestCookieURL("http://foo.google.com/bar"), |
| 272 kTestCookieURL2("http://foo.google.com/baz"), | 273 kTestCookieURL2("http://foo.google.com/baz"), |
| 273 kTestCookieURL3("http://foo.google.com"), | 274 kTestCookieURL3("http://foo.google.com"), |
| 274 kTestCookieURL4("http://bar.google.com/bar"), | 275 kTestCookieURL4("http://bar.google.com/bar"), |
| 275 backend_(new TestPersistentCookieStore), | 276 backend_(new TestPersistentCookieStore), |
| 276 store_(net::CookieStoreIOS::CreateCookieStore( | 277 store_(base::MakeUnique<net::CookieStoreIOS>( |
| 277 [NSHTTPCookieStorage sharedHTTPCookieStorage])) { | 278 [NSHTTPCookieStorage sharedHTTPCookieStorage])) { |
| 278 cookie_changed_callback_ = store_->AddCallbackForCookie( | 279 cookie_changed_callback_ = store_->AddCallbackForCookie( |
| 279 kTestCookieURL, "abc", | 280 kTestCookieURL, "abc", |
| 280 base::Bind(&RecordCookieChanges, &cookies_changed_, &cookies_removed_)); | 281 base::Bind(&RecordCookieChanges, &cookies_changed_, &cookies_removed_)); |
| 281 } | 282 } |
| 282 | 283 |
| 283 ~SynchronizedCookieStoreIOS() override {} | 284 ~SynchronizedCookieStoreIOS() override {} |
| 284 | 285 |
| 285 // Gets the cookies. |callback| will be called on completion. | 286 // Gets the cookies. |callback| will be called on completion. |
| 286 void GetCookies(const net::CookieStore::GetCookiesCallback& callback) { | 287 void GetCookies(const net::CookieStore::GetCookiesCallback& callback) { |
| 287 net::CookieOptions options; | 288 net::CookieOptions options; |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 SetCookie("abc=def"); | 411 SetCookie("abc=def"); |
| 411 EXPECT_EQ(1U, cookies_changed_.size()); | 412 EXPECT_EQ(1U, cookies_changed_.size()); |
| 412 SetCookie("abc=def"); | 413 SetCookie("abc=def"); |
| 413 EXPECT_EQ(1U, cookies_changed_.size()); | 414 EXPECT_EQ(1U, cookies_changed_.size()); |
| 414 } | 415 } |
| 415 | 416 |
| 416 TEST(CookieStoreIOS, GetAllCookiesForURLAsync) { | 417 TEST(CookieStoreIOS, GetAllCookiesForURLAsync) { |
| 417 base::MessageLoop loop; | 418 base::MessageLoop loop; |
| 418 const GURL kTestCookieURL("http://foo.google.com/bar"); | 419 const GURL kTestCookieURL("http://foo.google.com/bar"); |
| 419 ClearCookies(); | 420 ClearCookies(); |
| 420 std::unique_ptr<CookieStoreIOS> cookie_store( | 421 std::unique_ptr<CookieStoreIOS> cookie_store(base::MakeUnique<CookieStoreIOS>( |
| 421 CookieStoreIOS::CreateCookieStore( | 422 [NSHTTPCookieStorage sharedHTTPCookieStorage])); |
| 422 [NSHTTPCookieStorage sharedHTTPCookieStorage])); | 423 |
| 423 // Add a cookie. | 424 // Add a cookie. |
| 424 net::CookieOptions options; | 425 net::CookieOptions options; |
| 425 options.set_include_httponly(); | 426 options.set_include_httponly(); |
| 426 cookie_store->SetCookieWithOptionsAsync( | 427 cookie_store->SetCookieWithOptionsAsync( |
| 427 kTestCookieURL, "a=b", options, net::CookieStore::SetCookiesCallback()); | 428 kTestCookieURL, "a=b", options, net::CookieStore::SetCookiesCallback()); |
| 428 // Check we can get the cookie. | 429 // Check we can get the cookie. |
| 429 GetAllCookiesCallback callback; | 430 GetAllCookiesCallback callback; |
| 430 cookie_store->GetAllCookiesForURLAsync( | 431 cookie_store->GetAllCookiesForURLAsync( |
| 431 kTestCookieURL, | 432 kTestCookieURL, |
| 432 base::Bind(&GetAllCookiesCallback::Run, base::Unretained(&callback))); | 433 base::Bind(&GetAllCookiesCallback::Run, base::Unretained(&callback))); |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 632 SetSystemCookie(kTestCookieURL, "abc", "ghi"); | 633 SetSystemCookie(kTestCookieURL, "abc", "ghi"); |
| 633 EXPECT_EQ(2U, cookies.size()); | 634 EXPECT_EQ(2U, cookies.size()); |
| 634 // this deletes the callback | 635 // this deletes the callback |
| 635 handle.reset(); | 636 handle.reset(); |
| 636 SetSystemCookie(kTestCookieURL, "abc", "jkl"); | 637 SetSystemCookie(kTestCookieURL, "abc", "jkl"); |
| 637 EXPECT_EQ(2U, cookies.size()); | 638 EXPECT_EQ(2U, cookies.size()); |
| 638 DeleteSystemCookie(kTestCookieURL, "abc"); | 639 DeleteSystemCookie(kTestCookieURL, "abc"); |
| 639 } | 640 } |
| 640 | 641 |
| 641 } // namespace net | 642 } // namespace net |
| OLD | NEW |