| 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 // Tests common functionality used by the Chrome Extensions Cookies API | 5 // Tests common functionality used by the Chrome Extensions Cookies API |
| 6 // implementation. | 6 // implementation. |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 namespace GetAll = extensions::api::cookies::GetAll; | 28 namespace GetAll = extensions::api::cookies::GetAll; |
| 29 | 29 |
| 30 namespace extensions { | 30 namespace extensions { |
| 31 | 31 |
| 32 namespace keys = cookies_api_constants; | 32 namespace keys = cookies_api_constants; |
| 33 | 33 |
| 34 namespace { | 34 namespace { |
| 35 | 35 |
| 36 struct DomainMatchCase { | 36 struct DomainMatchCase { |
| 37 const char* filter; | 37 const char* filter; |
| 38 const char* url; | |
| 39 const char* domain; | 38 const char* domain; |
| 40 const bool matches; | 39 const bool matches; |
| 41 }; | 40 }; |
| 42 | 41 |
| 43 } // namespace | 42 } // namespace |
| 44 | 43 |
| 45 class ExtensionCookiesTest : public testing::Test { | 44 class ExtensionCookiesTest : public testing::Test { |
| 46 private: | 45 private: |
| 47 content::TestBrowserThreadBundle thread_bundle_; | 46 content::TestBrowserThreadBundle thread_bundle_; |
| 48 }; | 47 }; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 cookies_helpers::ChooseProfileFromStoreId( | 80 cookies_helpers::ChooseProfileFromStoreId( |
| 82 "1", profile->GetOffTheRecordProfile(), true)); | 81 "1", profile->GetOffTheRecordProfile(), true)); |
| 83 EXPECT_EQ(profile->GetOffTheRecordProfile(), | 82 EXPECT_EQ(profile->GetOffTheRecordProfile(), |
| 84 cookies_helpers::ChooseProfileFromStoreId( | 83 cookies_helpers::ChooseProfileFromStoreId( |
| 85 "1", profile->GetOffTheRecordProfile(), false)); | 84 "1", profile->GetOffTheRecordProfile(), false)); |
| 86 } | 85 } |
| 87 | 86 |
| 88 TEST_F(ExtensionCookiesTest, ExtensionTypeCreation) { | 87 TEST_F(ExtensionCookiesTest, ExtensionTypeCreation) { |
| 89 std::unique_ptr<net::CanonicalCookie> canonical_cookie1( | 88 std::unique_ptr<net::CanonicalCookie> canonical_cookie1( |
| 90 net::CanonicalCookie::Create( | 89 net::CanonicalCookie::Create( |
| 91 GURL("http://www.example.com"), "ABC", "DEF", std::string(), "/", | 90 "ABC", "DEF", "www.example.com", "/", base::Time(), base::Time(), |
| 92 base::Time(), base::Time(), false, false, | 91 base::Time(), false, false, net::CookieSameSite::DEFAULT_MODE, |
| 93 net::CookieSameSite::DEFAULT_MODE, net::COOKIE_PRIORITY_DEFAULT)); | 92 net::COOKIE_PRIORITY_DEFAULT)); |
| 94 ASSERT_NE(nullptr, canonical_cookie1.get()); | 93 ASSERT_NE(nullptr, canonical_cookie1.get()); |
| 95 Cookie cookie1 = | 94 Cookie cookie1 = |
| 96 cookies_helpers::CreateCookie(*canonical_cookie1, "some cookie store"); | 95 cookies_helpers::CreateCookie(*canonical_cookie1, "some cookie store"); |
| 97 EXPECT_EQ("ABC", cookie1.name); | 96 EXPECT_EQ("ABC", cookie1.name); |
| 98 EXPECT_EQ("DEF", cookie1.value); | 97 EXPECT_EQ("DEF", cookie1.value); |
| 99 EXPECT_EQ("www.example.com", cookie1.domain); | 98 EXPECT_EQ("www.example.com", cookie1.domain); |
| 100 EXPECT_TRUE(cookie1.host_only); | 99 EXPECT_TRUE(cookie1.host_only); |
| 101 EXPECT_EQ("/", cookie1.path); | 100 EXPECT_EQ("/", cookie1.path); |
| 102 EXPECT_FALSE(cookie1.secure); | 101 EXPECT_FALSE(cookie1.secure); |
| 103 EXPECT_FALSE(cookie1.http_only); | 102 EXPECT_FALSE(cookie1.http_only); |
| 104 EXPECT_EQ(api::cookies::SAME_SITE_STATUS_NO_RESTRICTION, cookie1.same_site); | 103 EXPECT_EQ(api::cookies::SAME_SITE_STATUS_NO_RESTRICTION, cookie1.same_site); |
| 105 EXPECT_TRUE(cookie1.session); | 104 EXPECT_TRUE(cookie1.session); |
| 106 EXPECT_FALSE(cookie1.expiration_date.get()); | 105 EXPECT_FALSE(cookie1.expiration_date.get()); |
| 107 EXPECT_EQ("some cookie store", cookie1.store_id); | 106 EXPECT_EQ("some cookie store", cookie1.store_id); |
| 108 | 107 |
| 109 std::unique_ptr<net::CanonicalCookie> canonical_cookie2( | 108 std::unique_ptr<net::CanonicalCookie> canonical_cookie2( |
| 110 net::CanonicalCookie::Create( | 109 net::CanonicalCookie::Create( |
| 111 GURL("http://example.com"), "ABC", "DEF", ".example.com", "/", | 110 "ABC", "DEF", ".example.com", "/", base::Time(), |
| 112 base::Time(), base::Time::FromDoubleT(10000), false, false, | 111 base::Time::FromDoubleT(10000), base::Time(), false, false, |
| 113 net::CookieSameSite::STRICT_MODE, net::COOKIE_PRIORITY_DEFAULT)); | 112 net::CookieSameSite::STRICT_MODE, net::COOKIE_PRIORITY_DEFAULT)); |
| 114 ASSERT_NE(nullptr, canonical_cookie2.get()); | 113 ASSERT_NE(nullptr, canonical_cookie2.get()); |
| 115 Cookie cookie2 = | 114 Cookie cookie2 = |
| 116 cookies_helpers::CreateCookie(*canonical_cookie2, "some cookie store"); | 115 cookies_helpers::CreateCookie(*canonical_cookie2, "some cookie store"); |
| 117 EXPECT_FALSE(cookie2.host_only); | 116 EXPECT_FALSE(cookie2.host_only); |
| 118 EXPECT_FALSE(cookie2.session); | 117 EXPECT_FALSE(cookie2.session); |
| 119 EXPECT_EQ(api::cookies::SAME_SITE_STATUS_STRICT, cookie2.same_site); | 118 EXPECT_EQ(api::cookies::SAME_SITE_STATUS_STRICT, cookie2.same_site); |
| 120 ASSERT_TRUE(cookie2.expiration_date.get()); | 119 ASSERT_TRUE(cookie2.expiration_date.get()); |
| 121 EXPECT_EQ(10000, *cookie2.expiration_date); | 120 EXPECT_EQ(10000, *cookie2.expiration_date); |
| 122 | 121 |
| 123 TestingProfile profile; | 122 TestingProfile profile; |
| 124 auto tab_ids_list = base::MakeUnique<base::ListValue>(); | 123 auto tab_ids_list = base::MakeUnique<base::ListValue>(); |
| 125 std::vector<int> tab_ids; | 124 std::vector<int> tab_ids; |
| 126 CookieStore cookie_store = | 125 CookieStore cookie_store = |
| 127 cookies_helpers::CreateCookieStore(&profile, std::move(tab_ids_list)); | 126 cookies_helpers::CreateCookieStore(&profile, std::move(tab_ids_list)); |
| 128 EXPECT_EQ("0", cookie_store.id); | 127 EXPECT_EQ("0", cookie_store.id); |
| 129 EXPECT_EQ(tab_ids, cookie_store.tab_ids); | 128 EXPECT_EQ(tab_ids, cookie_store.tab_ids); |
| 130 } | 129 } |
| 131 | 130 |
| 132 TEST_F(ExtensionCookiesTest, GetURLFromCanonicalCookie) { | 131 TEST_F(ExtensionCookiesTest, GetURLFromCanonicalCookie) { |
| 133 std::unique_ptr<net::CanonicalCookie> cookie1(net::CanonicalCookie::Create( | 132 std::unique_ptr<net::CanonicalCookie> cookie1(net::CanonicalCookie::Create( |
| 134 GURL("http://example.com"), "ABC", "DEF", "example.com", "/", | 133 "ABC", "DEF", "example.com", "/", base::Time(), base::Time(), |
| 135 base::Time(), base::Time(), false, false, | 134 base::Time(), false, false, net::CookieSameSite::DEFAULT_MODE, |
| 136 net::CookieSameSite::DEFAULT_MODE, net::COOKIE_PRIORITY_DEFAULT)); | 135 net::COOKIE_PRIORITY_DEFAULT)); |
| 137 ASSERT_NE(nullptr, cookie1.get()); | 136 ASSERT_NE(nullptr, cookie1.get()); |
| 138 EXPECT_EQ("http://example.com/", | 137 EXPECT_EQ("http://example.com/", |
| 139 cookies_helpers::GetURLFromCanonicalCookie(*cookie1).spec()); | 138 cookies_helpers::GetURLFromCanonicalCookie(*cookie1).spec()); |
| 140 | 139 |
| 141 std::unique_ptr<net::CanonicalCookie> cookie2(net::CanonicalCookie::Create( | 140 std::unique_ptr<net::CanonicalCookie> cookie2(net::CanonicalCookie::Create( |
| 142 GURL("https://helloworld.com"), "ABC", "DEF", ".helloworld.com", "/", | 141 "ABC", "DEF", ".helloworld.com", "/", base::Time(), base::Time(), |
| 143 base::Time(), base::Time(), true, false, | 142 base::Time(), true, false, net::CookieSameSite::DEFAULT_MODE, |
| 144 net::CookieSameSite::DEFAULT_MODE, net::COOKIE_PRIORITY_DEFAULT)); | 143 net::COOKIE_PRIORITY_DEFAULT)); |
| 145 ASSERT_NE(nullptr, cookie2.get()); | 144 ASSERT_NE(nullptr, cookie2.get()); |
| 146 EXPECT_EQ("https://helloworld.com/", | 145 EXPECT_EQ("https://helloworld.com/", |
| 147 cookies_helpers::GetURLFromCanonicalCookie(*cookie2).spec()); | 146 cookies_helpers::GetURLFromCanonicalCookie(*cookie2).spec()); |
| 148 } | 147 } |
| 149 | 148 |
| 150 TEST_F(ExtensionCookiesTest, EmptyDictionary) { | 149 TEST_F(ExtensionCookiesTest, EmptyDictionary) { |
| 151 base::DictionaryValue dict; | 150 base::DictionaryValue dict; |
| 152 GetAll::Params::Details details; | 151 GetAll::Params::Details details; |
| 153 bool rv = GetAll::Params::Details::Populate(dict, &details); | 152 bool rv = GetAll::Params::Details::Populate(dict, &details); |
| 154 ASSERT_TRUE(rv); | 153 ASSERT_TRUE(rv); |
| 155 cookies_helpers::MatchFilter filter(&details); | 154 cookies_helpers::MatchFilter filter(&details); |
| 156 net::CanonicalCookie cookie; | 155 net::CanonicalCookie cookie; |
| 157 EXPECT_TRUE(filter.MatchesCookie(cookie)); | 156 EXPECT_TRUE(filter.MatchesCookie(cookie)); |
| 158 } | 157 } |
| 159 | 158 |
| 160 TEST_F(ExtensionCookiesTest, DomainMatching) { | 159 TEST_F(ExtensionCookiesTest, DomainMatching) { |
| 161 const DomainMatchCase tests[] = { | 160 const DomainMatchCase tests[] = { |
| 162 {"bar.com", "http://bar.com", "", true}, | 161 {"bar.com", "bar.com", true}, {".bar.com", "bar.com", true}, |
| 163 {".bar.com", "http://bar.com", "bar.com", true}, | 162 {"bar.com", "food.bar.com", true}, {"bar.com", "bar.foo.com", false}, |
| 164 {"bar.com", "http://foo.bar.com", "", true}, | 163 {".bar.com", ".foo.bar.com", true}, {".bar.com", "baz.foo.bar.com", true}, |
| 165 {"bar.com", "http://bar.foo.com", "", false}, | 164 {"foo.bar.com", ".bar.com", false}}; |
| 166 {".bar.com", "http://foo.bar.com", ".foo.bar.com", true}, | |
| 167 {".bar.com", "http://baz.foo.bar.com", "", true}, | |
| 168 {"foo.bar.com", "http://bar.com", ".bar.com", false}}; | |
| 169 | 165 |
| 170 for (size_t i = 0; i < arraysize(tests); ++i) { | 166 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 171 // Build up the Params struct. | 167 // Build up the Params struct. |
| 172 base::ListValue args; | 168 base::ListValue args; |
| 173 auto dict = base::MakeUnique<base::DictionaryValue>(); | 169 auto dict = base::MakeUnique<base::DictionaryValue>(); |
| 174 dict->SetString(keys::kDomainKey, std::string(tests[i].filter)); | 170 dict->SetString(keys::kDomainKey, std::string(tests[i].filter)); |
| 175 args.Set(0, std::move(dict)); | 171 args.Set(0, std::move(dict)); |
| 176 std::unique_ptr<GetAll::Params> params(GetAll::Params::Create(args)); | 172 std::unique_ptr<GetAll::Params> params(GetAll::Params::Create(args)); |
| 177 | 173 |
| 178 cookies_helpers::MatchFilter filter(¶ms->details); | 174 cookies_helpers::MatchFilter filter(¶ms->details); |
| 179 std::unique_ptr<net::CanonicalCookie> cookie(net::CanonicalCookie::Create( | 175 std::unique_ptr<net::CanonicalCookie> cookie(net::CanonicalCookie::Create( |
| 180 GURL(tests[i].url), std::string(), std::string(), tests[i].domain, | 176 std::string(), std::string(), tests[i].domain, std::string(), |
| 181 std::string(), base::Time(), base::Time(), false, false, | 177 base::Time(), base::Time(), base::Time(), false, false, |
| 182 net::CookieSameSite::DEFAULT_MODE, net::COOKIE_PRIORITY_DEFAULT)); | 178 net::CookieSameSite::DEFAULT_MODE, net::COOKIE_PRIORITY_DEFAULT)); |
| 183 ASSERT_NE(nullptr, cookie.get()); | 179 ASSERT_NE(nullptr, cookie.get()); |
| 184 EXPECT_EQ(tests[i].matches, filter.MatchesCookie(*cookie)); | 180 EXPECT_EQ(tests[i].matches, filter.MatchesCookie(*cookie)) << " test " << i; |
| 185 } | 181 } |
| 186 } | 182 } |
| 187 | 183 |
| 188 TEST_F(ExtensionCookiesTest, DecodeUTF8WithErrorHandling) { | 184 TEST_F(ExtensionCookiesTest, DecodeUTF8WithErrorHandling) { |
| 189 std::unique_ptr<net::CanonicalCookie> canonical_cookie( | 185 std::unique_ptr<net::CanonicalCookie> canonical_cookie( |
| 190 net::CanonicalCookie::Create(GURL("http://test.com"), | 186 net::CanonicalCookie::Create(GURL("http://test.com"), |
| 191 "=011Q255bNX_1!yd\203e+;path=/path\203", | 187 "=011Q255bNX_1!yd\203e+;path=/path\203", |
| 192 base::Time(), net::CookieOptions())); | 188 base::Time(), net::CookieOptions())); |
| 193 ASSERT_NE(nullptr, canonical_cookie.get()); | 189 ASSERT_NE(nullptr, canonical_cookie.get()); |
| 194 Cookie cookie = | 190 Cookie cookie = |
| 195 cookies_helpers::CreateCookie(*canonical_cookie, "some cookie store"); | 191 cookies_helpers::CreateCookie(*canonical_cookie, "some cookie store"); |
| 196 EXPECT_EQ(std::string("011Q255bNX_1!yd\xEF\xBF\xBD" | 192 EXPECT_EQ(std::string("011Q255bNX_1!yd\xEF\xBF\xBD" |
| 197 "e+"), | 193 "e+"), |
| 198 cookie.value); | 194 cookie.value); |
| 199 EXPECT_EQ(std::string(), cookie.path); | 195 EXPECT_EQ(std::string(), cookie.path); |
| 200 } | 196 } |
| 201 | 197 |
| 202 } // namespace extensions | 198 } // namespace extensions |
| OLD | NEW |