| 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 #include "ios/net/cookies/cookie_store_ios_unittest_helper.h" |
| 6 |
| 7 #import <Foundation/Foundation.h> |
| 8 |
| 9 #include "base/run_loop.h" |
| 10 #import "ios/net/cookies/cookie_store_ios.h" |
| 11 #include "net/cookies/canonical_cookie.h" |
| 12 #include "net/cookies/cookie_options.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace net { |
| 16 |
| 17 namespace { |
| 18 |
| 19 void IgnoreBoolean(bool not_used) { |
| 20 } |
| 21 |
| 22 } // namespace |
| 23 |
| 24 #pragma mark - |
| 25 #pragma mark TestPersistentCookieStore |
| 26 |
| 27 TestPersistentCookieStore::TestPersistentCookieStore() |
| 28 : kTestCookieURL("http://foo.google.com/bar"), flushed_(false) {} |
| 29 |
| 30 TestPersistentCookieStore::~TestPersistentCookieStore() = default; |
| 31 |
| 32 #pragma mark - |
| 33 #pragma mark TestPersistentCookieStore methods |
| 34 |
| 35 void TestPersistentCookieStore::RunLoadedCallback() { |
| 36 std::vector<std::unique_ptr<net::CanonicalCookie>> cookies; |
| 37 net::CookieOptions options; |
| 38 options.set_include_httponly(); |
| 39 |
| 40 std::unique_ptr<net::CanonicalCookie> cookie(net::CanonicalCookie::Create( |
| 41 kTestCookieURL, "a=b", base::Time::Now(), options)); |
| 42 cookies.push_back(std::move(cookie)); |
| 43 |
| 44 std::unique_ptr<net::CanonicalCookie> bad_canonical_cookie( |
| 45 net::CanonicalCookie::Create( |
| 46 GURL("http://domain/"), "name", "\x81r\xe4\xbd\xa0\xe5\xa5\xbd", |
| 47 std::string(), "/path/", |
| 48 base::Time(), // creation |
| 49 base::Time(), // expires |
| 50 false, // secure |
| 51 false, // httponly |
| 52 net::CookieSameSite::DEFAULT_MODE, net::COOKIE_PRIORITY_DEFAULT)); |
| 53 cookies.push_back(std::move(bad_canonical_cookie)); |
| 54 loaded_callback_.Run(std::move(cookies)); |
| 55 } |
| 56 |
| 57 bool TestPersistentCookieStore::flushed() { |
| 58 return flushed_; |
| 59 } |
| 60 |
| 61 #pragma mark - |
| 62 #pragma mark Private methods |
| 63 |
| 64 void TestPersistentCookieStore::Load(const LoadedCallback& loaded_callback) { |
| 65 loaded_callback_ = loaded_callback; |
| 66 } |
| 67 |
| 68 void TestPersistentCookieStore::LoadCookiesForKey( |
| 69 const std::string& key, |
| 70 const LoadedCallback& loaded_callback) { |
| 71 loaded_callback_ = loaded_callback; |
| 72 } |
| 73 |
| 74 void TestPersistentCookieStore::AddCookie(const net::CanonicalCookie& cc) { |
| 75 } |
| 76 |
| 77 void TestPersistentCookieStore::UpdateCookieAccessTime( |
| 78 const net::CanonicalCookie& cc) { |
| 79 } |
| 80 |
| 81 void TestPersistentCookieStore::DeleteCookie(const net::CanonicalCookie& cc) { |
| 82 } |
| 83 |
| 84 void TestPersistentCookieStore::SetForceKeepSessionState() { |
| 85 } |
| 86 |
| 87 void TestPersistentCookieStore::Flush(const base::Closure& callback) { |
| 88 flushed_ = true; |
| 89 } |
| 90 |
| 91 #pragma mark - |
| 92 #pragma mark GetCookieCallback |
| 93 |
| 94 GetCookieCallback::GetCookieCallback() : did_run_(false) {} |
| 95 |
| 96 #pragma mark - |
| 97 #pragma mark GetCookieCallback methods |
| 98 |
| 99 bool GetCookieCallback::did_run() { |
| 100 return did_run_; |
| 101 } |
| 102 |
| 103 const std::string& GetCookieCallback::cookie_line() { |
| 104 return cookie_line_; |
| 105 } |
| 106 |
| 107 void GetCookieCallback::Run(const std::string& cookie_line) { |
| 108 ASSERT_FALSE(did_run_); |
| 109 did_run_ = true; |
| 110 cookie_line_ = cookie_line; |
| 111 } |
| 112 |
| 113 //------------------------------------------------------------------------------ |
| 114 |
| 115 void RecordCookieChanges(std::vector<net::CanonicalCookie>* out_cookies, |
| 116 std::vector<bool>* out_removes, |
| 117 const net::CanonicalCookie& cookie, |
| 118 net::CookieStore::ChangeCause cause) { |
| 119 DCHECK(out_cookies); |
| 120 out_cookies->push_back(cookie); |
| 121 if (out_removes) |
| 122 out_removes->push_back(net::CookieStore::ChangeCauseIsDeletion(cause)); |
| 123 } |
| 124 |
| 125 void SetCookie(const std::string& cookie_line, |
| 126 const GURL& url, |
| 127 net::CookieStore* store) { |
| 128 net::CookieOptions options; |
| 129 options.set_include_httponly(); |
| 130 store->SetCookieWithOptionsAsync(url, cookie_line, options, |
| 131 base::Bind(&IgnoreBoolean)); |
| 132 net::CookieStoreIOS::NotifySystemCookiesChanged(); |
| 133 // Wait until the flush is posted. |
| 134 base::RunLoop().RunUntilIdle(); |
| 135 } |
| 136 |
| 137 void ClearCookies() { |
| 138 NSHTTPCookieStorage* store = [NSHTTPCookieStorage sharedHTTPCookieStorage]; |
| 139 [store setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; |
| 140 NSArray* cookies = [store cookies]; |
| 141 for (NSHTTPCookie* cookie in cookies) |
| 142 [store deleteCookie:cookie]; |
| 143 EXPECT_EQ(0u, [[store cookies] count]); |
| 144 } |
| 145 |
| 146 } // namespace net |
| OLD | NEW |