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