| 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 "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/extensions/api/cookies/cookies_api_constants.h" | 11 #include "chrome/browser/extensions/api/cookies/cookies_api_constants.h" |
| 12 #include "chrome/browser/extensions/api/cookies/cookies_helpers.h" | 12 #include "chrome/browser/extensions/api/cookies/cookies_helpers.h" |
| 13 #include "chrome/common/extensions/api/cookies.h" |
| 13 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
| 14 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 15 | 16 |
| 17 using extensions::api::cookies::Cookie; |
| 18 using extensions::api::cookies::CookieStore; |
| 19 |
| 20 namespace GetAll = extensions::api::cookies::GetAll; |
| 21 |
| 16 namespace extensions { | 22 namespace extensions { |
| 17 | 23 |
| 18 namespace keys = cookies_api_constants; | 24 namespace keys = cookies_api_constants; |
| 19 | 25 |
| 20 namespace { | 26 namespace { |
| 21 | 27 |
| 22 struct DomainMatchCase { | 28 struct DomainMatchCase { |
| 23 const char* filter; | 29 const char* filter; |
| 24 const char* domain; | 30 const char* domain; |
| 25 const bool matches; | 31 const bool matches; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 "0", &otrProfile, false)); | 103 "0", &otrProfile, false)); |
| 98 EXPECT_EQ(&otrProfile, | 104 EXPECT_EQ(&otrProfile, |
| 99 cookies_helpers::ChooseProfileFromStoreId( | 105 cookies_helpers::ChooseProfileFromStoreId( |
| 100 "1", &otrProfile, true)); | 106 "1", &otrProfile, true)); |
| 101 EXPECT_EQ(&otrProfile, | 107 EXPECT_EQ(&otrProfile, |
| 102 cookies_helpers::ChooseProfileFromStoreId( | 108 cookies_helpers::ChooseProfileFromStoreId( |
| 103 "1", &otrProfile, false)); | 109 "1", &otrProfile, false)); |
| 104 } | 110 } |
| 105 | 111 |
| 106 TEST_F(ExtensionCookiesTest, ExtensionTypeCreation) { | 112 TEST_F(ExtensionCookiesTest, ExtensionTypeCreation) { |
| 107 std::string string_value; | 113 net::CookieMonster::CanonicalCookie canonical_cookie1( |
| 108 bool boolean_value; | |
| 109 double double_value; | |
| 110 Value* value; | |
| 111 | |
| 112 net::CookieMonster::CanonicalCookie cookie1( | |
| 113 GURL(), "ABC", "DEF", "www.foobar.com", "/", | 114 GURL(), "ABC", "DEF", "www.foobar.com", "/", |
| 114 std::string(), std::string(), | 115 std::string(), std::string(), |
| 115 base::Time(), base::Time(), base::Time(), | 116 base::Time(), base::Time(), base::Time(), |
| 116 false, false, false, false); | 117 false, false, false, false); |
| 117 scoped_ptr<DictionaryValue> cookie_value1( | 118 scoped_ptr<Cookie> cookie1( |
| 118 cookies_helpers::CreateCookieValue( | 119 cookies_helpers::CreateCookie( |
| 119 cookie1, "some cookie store")); | 120 canonical_cookie1, "some cookie store")); |
| 120 EXPECT_TRUE(cookie_value1->GetString(keys::kNameKey, &string_value)); | 121 EXPECT_EQ("ABC", cookie1->name); |
| 121 EXPECT_EQ("ABC", string_value); | 122 EXPECT_EQ("DEF", cookie1->value); |
| 122 EXPECT_TRUE(cookie_value1->GetString(keys::kValueKey, &string_value)); | 123 EXPECT_EQ("www.foobar.com", cookie1->domain); |
| 123 EXPECT_EQ("DEF", string_value); | 124 EXPECT_TRUE(cookie1->host_only); |
| 124 EXPECT_TRUE(cookie_value1->GetString(keys::kDomainKey, &string_value)); | 125 EXPECT_EQ("/", cookie1->path); |
| 125 EXPECT_EQ("www.foobar.com", string_value); | 126 EXPECT_FALSE(cookie1->secure); |
| 126 EXPECT_TRUE(cookie_value1->GetBoolean(keys::kHostOnlyKey, &boolean_value)); | 127 EXPECT_FALSE(cookie1->http_only); |
| 127 EXPECT_TRUE(boolean_value); | 128 EXPECT_TRUE(cookie1->session); |
| 128 EXPECT_TRUE(cookie_value1->GetString(keys::kPathKey, &string_value)); | 129 EXPECT_FALSE(cookie1->expiration_date.get()); |
| 129 EXPECT_EQ("/", string_value); | 130 EXPECT_EQ("some cookie store", cookie1->store_id); |
| 130 EXPECT_TRUE(cookie_value1->GetBoolean(keys::kSecureKey, &boolean_value)); | |
| 131 EXPECT_FALSE(boolean_value); | |
| 132 EXPECT_TRUE(cookie_value1->GetBoolean(keys::kHttpOnlyKey, &boolean_value)); | |
| 133 EXPECT_FALSE(boolean_value); | |
| 134 EXPECT_TRUE(cookie_value1->GetBoolean(keys::kSessionKey, &boolean_value)); | |
| 135 EXPECT_TRUE(boolean_value); | |
| 136 EXPECT_FALSE( | |
| 137 cookie_value1->GetDouble(keys::kExpirationDateKey, &double_value)); | |
| 138 EXPECT_TRUE(cookie_value1->GetString(keys::kStoreIdKey, &string_value)); | |
| 139 EXPECT_EQ("some cookie store", string_value); | |
| 140 | 131 |
| 141 net::CookieMonster::CanonicalCookie cookie2( | 132 net::CookieMonster::CanonicalCookie canonical_cookie2( |
| 142 GURL(), "ABC", "DEF", ".foobar.com", "/", std::string(), std::string(), | 133 GURL(), "ABC", "DEF", ".foobar.com", "/", std::string(), std::string(), |
| 143 base::Time(), base::Time::FromDoubleT(10000), base::Time(), | 134 base::Time(), base::Time::FromDoubleT(10000), base::Time(), |
| 144 false, false, true, true); | 135 false, false, true, true); |
| 145 scoped_ptr<DictionaryValue> cookie_value2( | 136 scoped_ptr<Cookie> cookie2( |
| 146 cookies_helpers::CreateCookieValue( | 137 cookies_helpers::CreateCookie( |
| 147 cookie2, "some cookie store")); | 138 canonical_cookie2, "some cookie store")); |
| 148 EXPECT_TRUE(cookie_value2->GetBoolean(keys::kHostOnlyKey, &boolean_value)); | 139 EXPECT_FALSE(cookie2->host_only); |
| 149 EXPECT_FALSE(boolean_value); | 140 EXPECT_FALSE(cookie2->session); |
| 150 EXPECT_TRUE(cookie_value2->GetBoolean(keys::kSessionKey, &boolean_value)); | 141 ASSERT_TRUE(cookie2->expiration_date.get()); |
| 151 EXPECT_FALSE(boolean_value); | 142 EXPECT_EQ(10000, *cookie2->expiration_date); |
| 152 EXPECT_TRUE( | |
| 153 cookie_value2->GetDouble(keys::kExpirationDateKey, &double_value)); | |
| 154 EXPECT_EQ(10000, double_value); | |
| 155 | 143 |
| 156 TestingProfile profile; | 144 TestingProfile profile; |
| 157 ListValue* tab_ids = new ListValue(); | 145 ListValue* tab_ids_list = new ListValue(); |
| 158 scoped_ptr<DictionaryValue> cookie_store_value( | 146 std::vector<int> tab_ids; |
| 159 cookies_helpers::CreateCookieStoreValue(&profile, tab_ids)); | 147 scoped_ptr<CookieStore> cookie_store( |
| 160 EXPECT_TRUE(cookie_store_value->GetString(keys::kIdKey, &string_value)); | 148 cookies_helpers::CreateCookieStore(&profile, tab_ids_list)); |
| 161 EXPECT_EQ("0", string_value); | 149 EXPECT_EQ("0", cookie_store->id); |
| 162 EXPECT_TRUE(cookie_store_value->Get(keys::kTabIdsKey, &value)); | 150 EXPECT_EQ(tab_ids, cookie_store->tab_ids); |
| 163 EXPECT_EQ(tab_ids, value); | |
| 164 } | 151 } |
| 165 | 152 |
| 166 TEST_F(ExtensionCookiesTest, GetURLFromCanonicalCookie) { | 153 TEST_F(ExtensionCookiesTest, GetURLFromCanonicalCookie) { |
| 167 net::CookieMonster::CanonicalCookie cookie1( | 154 net::CookieMonster::CanonicalCookie cookie1( |
| 168 GURL(), "ABC", "DEF", "www.foobar.com", "/", | 155 GURL(), "ABC", "DEF", "www.foobar.com", "/", |
| 169 std::string(), std::string(), | 156 std::string(), std::string(), |
| 170 base::Time(), base::Time(), base::Time(), | 157 base::Time(), base::Time(), base::Time(), |
| 171 false, false, false, false); | 158 false, false, false, false); |
| 172 EXPECT_EQ("http://www.foobar.com/", | 159 EXPECT_EQ("http://www.foobar.com/", |
| 173 cookies_helpers::GetURLFromCanonicalCookie( | 160 cookies_helpers::GetURLFromCanonicalCookie( |
| 174 cookie1).spec()); | 161 cookie1).spec()); |
| 175 | 162 |
| 176 net::CookieMonster::CanonicalCookie cookie2( | 163 net::CookieMonster::CanonicalCookie cookie2( |
| 177 GURL(), "ABC", "DEF", ".helloworld.com", "/", | 164 GURL(), "ABC", "DEF", ".helloworld.com", "/", |
| 178 std::string(), std::string(), | 165 std::string(), std::string(), |
| 179 base::Time(), base::Time(), base::Time(), | 166 base::Time(), base::Time(), base::Time(), |
| 180 true, false, false, false); | 167 true, false, false, false); |
| 181 EXPECT_EQ("https://helloworld.com/", | 168 EXPECT_EQ("https://helloworld.com/", |
| 182 cookies_helpers::GetURLFromCanonicalCookie( | 169 cookies_helpers::GetURLFromCanonicalCookie( |
| 183 cookie2).spec()); | 170 cookie2).spec()); |
| 184 } | 171 } |
| 185 | 172 |
| 186 TEST_F(ExtensionCookiesTest, EmptyDictionary) { | 173 TEST_F(ExtensionCookiesTest, EmptyDictionary) { |
| 187 scoped_ptr<DictionaryValue> details(new DictionaryValue()); | 174 DictionaryValue dict; |
| 188 cookies_helpers::MatchFilter filter(details.get()); | 175 GetAll::Params::Details details; |
| 189 std::string domain; | 176 bool rv = GetAll::Params::Details::Populate(dict, &details); |
| 177 ASSERT_TRUE(rv); |
| 178 cookies_helpers::MatchFilter filter(&details); |
| 190 net::CookieMonster::CanonicalCookie cookie; | 179 net::CookieMonster::CanonicalCookie cookie; |
| 191 | |
| 192 EXPECT_TRUE(filter.MatchesCookie(cookie)); | 180 EXPECT_TRUE(filter.MatchesCookie(cookie)); |
| 193 } | 181 } |
| 194 | 182 |
| 195 TEST_F(ExtensionCookiesTest, DomainMatching) { | 183 TEST_F(ExtensionCookiesTest, DomainMatching) { |
| 196 const DomainMatchCase tests[] = { | 184 const DomainMatchCase tests[] = { |
| 197 { "bar.com", "bar.com", true }, | 185 { "bar.com", "bar.com", true }, |
| 198 { ".bar.com", "bar.com", true }, | 186 { ".bar.com", "bar.com", true }, |
| 199 { "bar.com", "foo.bar.com", true }, | 187 { "bar.com", "foo.bar.com", true }, |
| 200 { "bar.com", "bar.foo.com", false }, | 188 { "bar.com", "bar.foo.com", false }, |
| 201 { ".bar.com", ".foo.bar.com", true }, | 189 { ".bar.com", ".foo.bar.com", true }, |
| 202 { ".bar.com", "baz.foo.bar.com", true }, | 190 { ".bar.com", "baz.foo.bar.com", true }, |
| 203 { "foo.bar.com", ".bar.com", false } | 191 { "foo.bar.com", ".bar.com", false } |
| 204 }; | 192 }; |
| 205 | 193 |
| 206 scoped_ptr<DictionaryValue> details(new DictionaryValue()); | |
| 207 for (size_t i = 0; i < arraysize(tests); ++i) { | 194 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 208 details->SetString(keys::kDomainKey, std::string(tests[i].filter)); | 195 // Build up the Params struct. |
| 209 cookies_helpers::MatchFilter filter(details.get()); | 196 ListValue args; |
| 197 DictionaryValue* dict = new DictionaryValue(); |
| 198 dict->SetString(keys::kDomainKey, std::string(tests[i].filter)); |
| 199 args.Set(0, dict); |
| 200 scoped_ptr<GetAll::Params> params(GetAll::Params::Create(args)); |
| 201 |
| 202 cookies_helpers::MatchFilter filter(¶ms->details); |
| 210 net::CookieMonster::CanonicalCookie cookie(GURL(), "", "", tests[i].domain, | 203 net::CookieMonster::CanonicalCookie cookie(GURL(), "", "", tests[i].domain, |
| 211 "", "", "", base::Time(), | 204 "", "", "", base::Time(), |
| 212 base::Time(), base::Time(), | 205 base::Time(), base::Time(), |
| 213 false, false, false, false); | 206 false, false, false, false); |
| 214 EXPECT_EQ(tests[i].matches, filter.MatchesCookie(cookie)); | 207 EXPECT_EQ(tests[i].matches, filter.MatchesCookie(cookie)); |
| 215 } | 208 } |
| 216 } | 209 } |
| 217 | 210 |
| 218 TEST_F(ExtensionCookiesTest, DecodeUTF8WithErrorHandling) { | 211 TEST_F(ExtensionCookiesTest, DecodeUTF8WithErrorHandling) { |
| 219 net::CookieMonster::CanonicalCookie cookie(GURL(), "", | 212 net::CookieMonster::CanonicalCookie canonical_cookie( |
| 220 "011Q255bNX_1!yd\203e+", | 213 GURL(), "", "011Q255bNX_1!yd\203e+", "test.com", "/path\203", "", "", |
| 221 "test.com", | 214 base::Time(), base::Time(), base::Time(), false, false, false, false); |
| 222 "/path\203", "", "", base::Time(), | 215 scoped_ptr<Cookie> cookie( |
| 223 base::Time(), base::Time(), | 216 cookies_helpers::CreateCookie( |
| 224 false, false, false, false); | 217 canonical_cookie, "some cookie store")); |
| 225 scoped_ptr<DictionaryValue> cookie_value( | 218 EXPECT_EQ(std::string("011Q255bNX_1!yd\xEF\xBF\xBD" "e+"), cookie->value); |
| 226 cookies_helpers::CreateCookieValue( | 219 EXPECT_EQ(std::string(""), cookie->path); |
| 227 cookie, "some cookie store")); | |
| 228 std::string string_value; | |
| 229 EXPECT_TRUE(cookie_value->GetString(keys::kValueKey, &string_value)); | |
| 230 EXPECT_EQ(std::string("011Q255bNX_1!yd\xEF\xBF\xBD" "e+"), string_value); | |
| 231 EXPECT_TRUE(cookie_value->GetString(keys::kPathKey, &string_value)); | |
| 232 EXPECT_EQ(std::string(""), string_value); | |
| 233 } | 220 } |
| 234 | 221 |
| 235 } // namespace extensions | 222 } // namespace extensions |
| OLD | NEW |