Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(445)

Side by Side Diff: chrome/browser/extensions/api/cookies/cookies_unittest.cc

Issue 10785017: Move CanonicalCookie into separate files (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added missing include Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/test/base/testing_profile.h" 13 #include "chrome/test/base/testing_profile.h"
14 #include "googleurl/src/gurl.h" 14 #include "googleurl/src/gurl.h"
15 #include "net/cookies/canonical_cookie.h"
15 16
16 namespace extensions { 17 namespace extensions {
17 18
18 namespace keys = cookies_api_constants; 19 namespace keys = cookies_api_constants;
19 20
20 namespace { 21 namespace {
21 22
22 struct DomainMatchCase { 23 struct DomainMatchCase {
23 const char* filter; 24 const char* filter;
24 const char* domain; 25 const char* domain;
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 cookies_helpers::ChooseProfileFromStoreId( 103 cookies_helpers::ChooseProfileFromStoreId(
103 "1", &otrProfile, false)); 104 "1", &otrProfile, false));
104 } 105 }
105 106
106 TEST_F(ExtensionCookiesTest, ExtensionTypeCreation) { 107 TEST_F(ExtensionCookiesTest, ExtensionTypeCreation) {
107 std::string string_value; 108 std::string string_value;
108 bool boolean_value; 109 bool boolean_value;
109 double double_value; 110 double double_value;
110 Value* value; 111 Value* value;
111 112
112 net::CookieMonster::CanonicalCookie cookie1( 113 net::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); 117 false, false);
117 scoped_ptr<DictionaryValue> cookie_value1( 118 scoped_ptr<DictionaryValue> cookie_value1(
118 cookies_helpers::CreateCookieValue( 119 cookies_helpers::CreateCookieValue(
119 cookie1, "some cookie store")); 120 cookie1, "some cookie store"));
120 EXPECT_TRUE(cookie_value1->GetString(keys::kNameKey, &string_value)); 121 EXPECT_TRUE(cookie_value1->GetString(keys::kNameKey, &string_value));
121 EXPECT_EQ("ABC", string_value); 122 EXPECT_EQ("ABC", string_value);
122 EXPECT_TRUE(cookie_value1->GetString(keys::kValueKey, &string_value)); 123 EXPECT_TRUE(cookie_value1->GetString(keys::kValueKey, &string_value));
123 EXPECT_EQ("DEF", string_value); 124 EXPECT_EQ("DEF", string_value);
124 EXPECT_TRUE(cookie_value1->GetString(keys::kDomainKey, &string_value)); 125 EXPECT_TRUE(cookie_value1->GetString(keys::kDomainKey, &string_value));
125 EXPECT_EQ("www.foobar.com", string_value); 126 EXPECT_EQ("www.foobar.com", string_value);
126 EXPECT_TRUE(cookie_value1->GetBoolean(keys::kHostOnlyKey, &boolean_value)); 127 EXPECT_TRUE(cookie_value1->GetBoolean(keys::kHostOnlyKey, &boolean_value));
127 EXPECT_TRUE(boolean_value); 128 EXPECT_TRUE(boolean_value);
128 EXPECT_TRUE(cookie_value1->GetString(keys::kPathKey, &string_value)); 129 EXPECT_TRUE(cookie_value1->GetString(keys::kPathKey, &string_value));
129 EXPECT_EQ("/", string_value); 130 EXPECT_EQ("/", string_value);
130 EXPECT_TRUE(cookie_value1->GetBoolean(keys::kSecureKey, &boolean_value)); 131 EXPECT_TRUE(cookie_value1->GetBoolean(keys::kSecureKey, &boolean_value));
131 EXPECT_FALSE(boolean_value); 132 EXPECT_FALSE(boolean_value);
132 EXPECT_TRUE(cookie_value1->GetBoolean(keys::kHttpOnlyKey, &boolean_value)); 133 EXPECT_TRUE(cookie_value1->GetBoolean(keys::kHttpOnlyKey, &boolean_value));
133 EXPECT_FALSE(boolean_value); 134 EXPECT_FALSE(boolean_value);
134 EXPECT_TRUE(cookie_value1->GetBoolean(keys::kSessionKey, &boolean_value)); 135 EXPECT_TRUE(cookie_value1->GetBoolean(keys::kSessionKey, &boolean_value));
135 EXPECT_TRUE(boolean_value); 136 EXPECT_TRUE(boolean_value);
136 EXPECT_FALSE( 137 EXPECT_FALSE(
137 cookie_value1->GetDouble(keys::kExpirationDateKey, &double_value)); 138 cookie_value1->GetDouble(keys::kExpirationDateKey, &double_value));
138 EXPECT_TRUE(cookie_value1->GetString(keys::kStoreIdKey, &string_value)); 139 EXPECT_TRUE(cookie_value1->GetString(keys::kStoreIdKey, &string_value));
139 EXPECT_EQ("some cookie store", string_value); 140 EXPECT_EQ("some cookie store", string_value);
140 141
141 net::CookieMonster::CanonicalCookie cookie2( 142 net::CanonicalCookie cookie2(
142 GURL(), "ABC", "DEF", ".foobar.com", "/", std::string(), std::string(), 143 GURL(), "ABC", "DEF", ".foobar.com", "/", std::string(), std::string(),
143 base::Time(), base::Time::FromDoubleT(10000), base::Time(), 144 base::Time(), base::Time::FromDoubleT(10000), base::Time(),
144 false, false); 145 false, false);
145 scoped_ptr<DictionaryValue> cookie_value2( 146 scoped_ptr<DictionaryValue> cookie_value2(
146 cookies_helpers::CreateCookieValue( 147 cookies_helpers::CreateCookieValue(
147 cookie2, "some cookie store")); 148 cookie2, "some cookie store"));
148 EXPECT_TRUE(cookie_value2->GetBoolean(keys::kHostOnlyKey, &boolean_value)); 149 EXPECT_TRUE(cookie_value2->GetBoolean(keys::kHostOnlyKey, &boolean_value));
149 EXPECT_FALSE(boolean_value); 150 EXPECT_FALSE(boolean_value);
150 EXPECT_TRUE(cookie_value2->GetBoolean(keys::kSessionKey, &boolean_value)); 151 EXPECT_TRUE(cookie_value2->GetBoolean(keys::kSessionKey, &boolean_value));
151 EXPECT_FALSE(boolean_value); 152 EXPECT_FALSE(boolean_value);
152 EXPECT_TRUE( 153 EXPECT_TRUE(
153 cookie_value2->GetDouble(keys::kExpirationDateKey, &double_value)); 154 cookie_value2->GetDouble(keys::kExpirationDateKey, &double_value));
154 EXPECT_EQ(10000, double_value); 155 EXPECT_EQ(10000, double_value);
155 156
156 TestingProfile profile; 157 TestingProfile profile;
157 ListValue* tab_ids = new ListValue(); 158 ListValue* tab_ids = new ListValue();
158 scoped_ptr<DictionaryValue> cookie_store_value( 159 scoped_ptr<DictionaryValue> cookie_store_value(
159 cookies_helpers::CreateCookieStoreValue(&profile, tab_ids)); 160 cookies_helpers::CreateCookieStoreValue(&profile, tab_ids));
160 EXPECT_TRUE(cookie_store_value->GetString(keys::kIdKey, &string_value)); 161 EXPECT_TRUE(cookie_store_value->GetString(keys::kIdKey, &string_value));
161 EXPECT_EQ("0", string_value); 162 EXPECT_EQ("0", string_value);
162 EXPECT_TRUE(cookie_store_value->Get(keys::kTabIdsKey, &value)); 163 EXPECT_TRUE(cookie_store_value->Get(keys::kTabIdsKey, &value));
163 EXPECT_EQ(tab_ids, value); 164 EXPECT_EQ(tab_ids, value);
164 } 165 }
165 166
166 TEST_F(ExtensionCookiesTest, GetURLFromCanonicalCookie) { 167 TEST_F(ExtensionCookiesTest, GetURLFromCanonicalCookie) {
167 net::CookieMonster::CanonicalCookie cookie1( 168 net::CanonicalCookie cookie1(
168 GURL(), "ABC", "DEF", "www.foobar.com", "/", 169 GURL(), "ABC", "DEF", "www.foobar.com", "/",
169 std::string(), std::string(), 170 std::string(), std::string(),
170 base::Time(), base::Time(), base::Time(), 171 base::Time(), base::Time(), base::Time(),
171 false, false); 172 false, false);
172 EXPECT_EQ("http://www.foobar.com/", 173 EXPECT_EQ("http://www.foobar.com/",
173 cookies_helpers::GetURLFromCanonicalCookie( 174 cookies_helpers::GetURLFromCanonicalCookie(
174 cookie1).spec()); 175 cookie1).spec());
175 176
176 net::CookieMonster::CanonicalCookie cookie2( 177 net::CanonicalCookie cookie2(
177 GURL(), "ABC", "DEF", ".helloworld.com", "/", 178 GURL(), "ABC", "DEF", ".helloworld.com", "/",
178 std::string(), std::string(), 179 std::string(), std::string(),
179 base::Time(), base::Time(), base::Time(), 180 base::Time(), base::Time(), base::Time(),
180 true, false); 181 true, false);
181 EXPECT_EQ("https://helloworld.com/", 182 EXPECT_EQ("https://helloworld.com/",
182 cookies_helpers::GetURLFromCanonicalCookie( 183 cookies_helpers::GetURLFromCanonicalCookie(
183 cookie2).spec()); 184 cookie2).spec());
184 } 185 }
185 186
186 TEST_F(ExtensionCookiesTest, EmptyDictionary) { 187 TEST_F(ExtensionCookiesTest, EmptyDictionary) {
187 scoped_ptr<DictionaryValue> details(new DictionaryValue()); 188 scoped_ptr<DictionaryValue> details(new DictionaryValue());
188 cookies_helpers::MatchFilter filter(details.get()); 189 cookies_helpers::MatchFilter filter(details.get());
189 std::string domain; 190 std::string domain;
190 net::CookieMonster::CanonicalCookie cookie; 191 net::CanonicalCookie cookie;
191 192
192 EXPECT_TRUE(filter.MatchesCookie(cookie)); 193 EXPECT_TRUE(filter.MatchesCookie(cookie));
193 } 194 }
194 195
195 TEST_F(ExtensionCookiesTest, DomainMatching) { 196 TEST_F(ExtensionCookiesTest, DomainMatching) {
196 const DomainMatchCase tests[] = { 197 const DomainMatchCase tests[] = {
197 { "bar.com", "bar.com", true }, 198 { "bar.com", "bar.com", true },
198 { ".bar.com", "bar.com", true }, 199 { ".bar.com", "bar.com", true },
199 { "bar.com", "foo.bar.com", true }, 200 { "bar.com", "foo.bar.com", true },
200 { "bar.com", "bar.foo.com", false }, 201 { "bar.com", "bar.foo.com", false },
201 { ".bar.com", ".foo.bar.com", true }, 202 { ".bar.com", ".foo.bar.com", true },
202 { ".bar.com", "baz.foo.bar.com", true }, 203 { ".bar.com", "baz.foo.bar.com", true },
203 { "foo.bar.com", ".bar.com", false } 204 { "foo.bar.com", ".bar.com", false }
204 }; 205 };
205 206
206 scoped_ptr<DictionaryValue> details(new DictionaryValue()); 207 scoped_ptr<DictionaryValue> details(new DictionaryValue());
207 for (size_t i = 0; i < arraysize(tests); ++i) { 208 for (size_t i = 0; i < arraysize(tests); ++i) {
208 details->SetString(keys::kDomainKey, std::string(tests[i].filter)); 209 details->SetString(keys::kDomainKey, std::string(tests[i].filter));
209 cookies_helpers::MatchFilter filter(details.get()); 210 cookies_helpers::MatchFilter filter(details.get());
210 net::CookieMonster::CanonicalCookie cookie(GURL(), "", "", tests[i].domain, 211 net::CanonicalCookie cookie(GURL(), "", "", tests[i].domain, "", "", "",
211 "", "", "", base::Time(), 212 base::Time(), base::Time(), base::Time(), false,
212 base::Time(), base::Time(), 213 false);
213 false, false);
214 EXPECT_EQ(tests[i].matches, filter.MatchesCookie(cookie)); 214 EXPECT_EQ(tests[i].matches, filter.MatchesCookie(cookie));
215 } 215 }
216 } 216 }
217 217
218 TEST_F(ExtensionCookiesTest, DecodeUTF8WithErrorHandling) { 218 TEST_F(ExtensionCookiesTest, DecodeUTF8WithErrorHandling) {
219 net::CookieMonster::CanonicalCookie cookie(GURL(), "", 219 net::CanonicalCookie cookie(GURL(), "", "011Q255bNX_1!yd\203e+", "test.com",
220 "011Q255bNX_1!yd\203e+", 220 "/path\203", "", "", base::Time(), base::Time(),
221 "test.com", 221 base::Time(), false, false);
222 "/path\203", "", "", base::Time(),
223 base::Time(), base::Time(),
224 false, false);
225 scoped_ptr<DictionaryValue> cookie_value( 222 scoped_ptr<DictionaryValue> cookie_value(
226 cookies_helpers::CreateCookieValue( 223 cookies_helpers::CreateCookieValue(
227 cookie, "some cookie store")); 224 cookie, "some cookie store"));
228 std::string string_value; 225 std::string string_value;
229 EXPECT_TRUE(cookie_value->GetString(keys::kValueKey, &string_value)); 226 EXPECT_TRUE(cookie_value->GetString(keys::kValueKey, &string_value));
230 EXPECT_EQ(std::string("011Q255bNX_1!yd\xEF\xBF\xBD" "e+"), string_value); 227 EXPECT_EQ(std::string("011Q255bNX_1!yd\xEF\xBF\xBD" "e+"), string_value);
231 EXPECT_TRUE(cookie_value->GetString(keys::kPathKey, &string_value)); 228 EXPECT_TRUE(cookie_value->GetString(keys::kPathKey, &string_value));
232 EXPECT_EQ(std::string(""), string_value); 229 EXPECT_EQ(std::string(""), string_value);
233 } 230 }
234 231
235 } // namespace extensions 232 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698