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 #include "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 18 matching lines...) Expand all Loading... | |
29 } | 29 } |
30 | 30 |
31 Pickle SerializeVector(const std::vector<string16>& vec) const { | 31 Pickle SerializeVector(const std::vector<string16>& vec) const { |
32 return db_.SerializeVector(vec); | 32 return db_.SerializeVector(vec); |
33 } | 33 } |
34 | 34 |
35 std::vector<string16> DeserializeVector(const Pickle& pickle) const { | 35 std::vector<string16> DeserializeVector(const Pickle& pickle) const { |
36 return db_.DeserializeVector(pickle); | 36 return db_.DeserializeVector(pickle); |
37 } | 37 } |
38 | 38 |
39 void SetPublicSuffixMatching(bool enabled) { | |
40 db_.public_suffix_domain_matching_ = enabled; | |
41 } | |
42 | |
39 LoginDatabase db_; | 43 LoginDatabase db_; |
40 base::FilePath file_; | 44 base::FilePath file_; |
41 base::ScopedTempDir temp_dir_; | 45 base::ScopedTempDir temp_dir_; |
42 }; | 46 }; |
43 | 47 |
44 TEST_F(LoginDatabaseTest, Logins) { | 48 TEST_F(LoginDatabaseTest, Logins) { |
45 std::vector<PasswordForm*> result; | 49 std::vector<PasswordForm*> result; |
46 | 50 |
47 // Verify the database is empty. | 51 // Verify the database is empty. |
48 EXPECT_TRUE(db_.GetAutofillableLogins(&result)); | 52 EXPECT_TRUE(db_.GetAutofillableLogins(&result)); |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 EXPECT_TRUE(form6.preferred); | 174 EXPECT_TRUE(form6.preferred); |
171 delete result[0]; | 175 delete result[0]; |
172 result.clear(); | 176 result.clear(); |
173 | 177 |
174 // Make sure everything can disappear. | 178 // Make sure everything can disappear. |
175 EXPECT_TRUE(db_.RemoveLogin(form4)); | 179 EXPECT_TRUE(db_.RemoveLogin(form4)); |
176 EXPECT_TRUE(db_.GetAutofillableLogins(&result)); | 180 EXPECT_TRUE(db_.GetAutofillableLogins(&result)); |
177 EXPECT_EQ(0U, result.size()); | 181 EXPECT_EQ(0U, result.size()); |
178 } | 182 } |
179 | 183 |
184 TEST_F(LoginDatabaseTest, TestPublicSuffixDomainMatching) { | |
185 SetPublicSuffixMatching(true); | |
186 std::vector<PasswordForm*> result; | |
187 | |
188 // Verify the database is empty. | |
189 EXPECT_TRUE(db_.GetAutofillableLogins(&result)); | |
190 EXPECT_EQ(0U, result.size()); | |
191 | |
192 // Example password form. | |
193 PasswordForm form; | |
194 form.origin = GURL("https://foo.com/"); | |
195 form.action = GURL("https://foo.com/login"); | |
196 form.username_element = ASCIIToUTF16("username"); | |
197 form.username_value = ASCIIToUTF16("test@gmail.com"); | |
198 form.password_element = ASCIIToUTF16("password"); | |
199 form.password_value = ASCIIToUTF16("test"); | |
200 form.submit_element = ASCIIToUTF16(""); | |
201 form.signon_realm = "https://foo.com/"; | |
202 form.ssl_valid = true; | |
203 form.preferred = false; | |
204 form.scheme = PasswordForm::SCHEME_HTML; | |
205 | |
206 // Add it and make sure it is there. | |
207 EXPECT_TRUE(db_.AddLogin(form)); | |
208 EXPECT_TRUE(db_.GetAutofillableLogins(&result)); | |
209 EXPECT_EQ(1U, result.size()); | |
210 delete result[0]; | |
211 result.clear(); | |
212 | |
213 // Match against an exact copy. | |
214 EXPECT_TRUE(db_.GetLogins(form, &result)); | |
215 EXPECT_EQ(1U, result.size()); | |
216 delete result[0]; | |
217 result.clear(); | |
218 | |
219 // We go to the mobile site. | |
220 PasswordForm form2(form); | |
221 form2.origin = GURL("https://mobile.foo.com/"); | |
222 form2.action = GURL("https://mobile.foo.com/login"); | |
223 form2.signon_realm = "https://mobile.foo.com/"; | |
224 | |
225 // Match against the mobile site. | |
226 EXPECT_TRUE(db_.GetLogins(form2, &result)); | |
227 EXPECT_EQ(1U, result.size()); | |
228 EXPECT_EQ("https://mobile.foo.com/", result[0]->signon_realm); | |
229 EXPECT_EQ("https://foo.com/", result[0]->original_signon_realm); | |
230 delete result[0]; | |
231 result.clear(); | |
232 } | |
233 | |
234 // This test fails if the implementation of GetLogins uses GetCachedStatement | |
235 // instead of GetUniqueStatement, since REGEXP is in use. See | |
236 // http://crbug.com/248608. | |
237 TEST_F(LoginDatabaseTest, TestPublicSuffixDomainMatchingDifferentSites) { | |
238 SetPublicSuffixMatching(true); | |
239 std::vector<PasswordForm*> result; | |
240 | |
241 // Verify the database is empty. | |
242 EXPECT_TRUE(db_.GetAutofillableLogins(&result)); | |
243 EXPECT_EQ(0U, result.size()); | |
244 | |
245 // Example password form. | |
246 PasswordForm form; | |
247 form.origin = GURL("https://foo.com/"); | |
248 form.action = GURL("https://foo.com/login"); | |
249 form.username_element = ASCIIToUTF16("username"); | |
250 form.username_value = ASCIIToUTF16("test@gmail.com"); | |
251 form.password_element = ASCIIToUTF16("password"); | |
252 form.password_value = ASCIIToUTF16("test"); | |
253 form.submit_element = ASCIIToUTF16(""); | |
254 form.signon_realm = "https://foo.com/"; | |
255 form.ssl_valid = true; | |
256 form.preferred = false; | |
257 form.scheme = PasswordForm::SCHEME_HTML; | |
258 | |
259 // Add it and make sure it is there. | |
260 EXPECT_TRUE(db_.AddLogin(form)); | |
261 EXPECT_TRUE(db_.GetAutofillableLogins(&result)); | |
262 EXPECT_EQ(1U, result.size()); | |
263 delete result[0]; | |
264 result.clear(); | |
265 | |
266 // Match against an exact copy. | |
267 EXPECT_TRUE(db_.GetLogins(form, &result)); | |
268 EXPECT_EQ(1U, result.size()); | |
269 delete result[0]; | |
270 result.clear(); | |
271 | |
272 // We go to the mobile site. | |
273 PasswordForm form2(form); | |
274 form2.origin = GURL("https://mobile.foo.com/"); | |
275 form2.action = GURL("https://mobile.foo.com/login"); | |
276 form2.signon_realm = "https://mobile.foo.com/"; | |
277 | |
278 // Match against the mobile site. | |
279 EXPECT_TRUE(db_.GetLogins(form2, &result)); | |
280 EXPECT_EQ(1U, result.size()); | |
281 EXPECT_EQ("https://mobile.foo.com/", result[0]->signon_realm); | |
282 EXPECT_EQ("https://foo.com/", result[0]->original_signon_realm); | |
283 delete result[0]; | |
284 result.clear(); | |
285 | |
286 // Add baz.com desktop site. | |
287 form.origin = GURL("https://baz.com/login/"); | |
288 form.action = GURL("https://baz.com/login/"); | |
289 form.username_element = ASCIIToUTF16("email"); | |
290 form.username_value = ASCIIToUTF16("test@gmail.com"); | |
291 form.password_element = ASCIIToUTF16("password"); | |
292 form.password_value = ASCIIToUTF16("test"); | |
293 form.submit_element = ASCIIToUTF16(""); | |
294 form.signon_realm = "https://baz.com/"; | |
295 form.ssl_valid = true; | |
296 form.preferred = false; | |
297 form.scheme = PasswordForm::SCHEME_HTML; | |
298 | |
299 // Add it and make sure it is there. | |
300 EXPECT_TRUE(db_.AddLogin(form)); | |
301 EXPECT_TRUE(db_.GetAutofillableLogins(&result)); | |
302 EXPECT_EQ(2U, result.size()); | |
303 delete result[0]; | |
304 delete result[1]; | |
305 result.clear(); | |
306 | |
307 // We go to the mobile site of baz.com. | |
308 PasswordForm form3(form); | |
309 form3.origin = GURL("https://m.baz.com/login/"); | |
310 form3.action = GURL("https://m.baz.com/login/"); | |
311 form3.signon_realm = "https://m.baz.com/"; | |
312 | |
313 // Match against the mobile site of baz.com. | |
314 EXPECT_TRUE(db_.GetLogins(form3, &result)); | |
315 EXPECT_EQ(1U, result.size()); | |
316 EXPECT_EQ("https://m.baz.com/", result[0]->signon_realm); | |
317 EXPECT_EQ("https://baz.com/", result[0]->original_signon_realm); | |
318 delete result[0]; | |
319 result.clear(); | |
320 } | |
321 | |
322 PasswordForm GetFormWithNewSignonRealm(PasswordForm form, | |
323 std::string signon_realm) { | |
324 PasswordForm form2(form); | |
325 form2.origin = GURL(signon_realm); | |
326 form2.action = GURL(signon_realm); | |
327 form2.signon_realm = signon_realm; | |
328 return form2; | |
329 } | |
330 | |
331 TEST_F(LoginDatabaseTest, TestPublicSuffixDomainMatchingRegexp) { | |
332 SetPublicSuffixMatching(true); | |
333 std::vector<PasswordForm*> result; | |
334 | |
335 // Verify the database is empty. | |
336 EXPECT_TRUE(db_.GetAutofillableLogins(&result)); | |
337 EXPECT_EQ(0U, result.size()); | |
338 | |
339 // Example password form. | |
340 PasswordForm form; | |
341 form.origin = GURL("http://foo.com/"); | |
342 form.action = GURL("http://foo.com/login"); | |
343 form.username_element = ASCIIToUTF16("username"); | |
344 form.username_value = ASCIIToUTF16("test@gmail.com"); | |
345 form.password_element = ASCIIToUTF16("password"); | |
346 form.password_value = ASCIIToUTF16("test"); | |
347 form.submit_element = ASCIIToUTF16(""); | |
348 form.signon_realm = "http://foo.com/"; | |
349 form.ssl_valid = false; | |
350 form.preferred = false; | |
351 form.scheme = PasswordForm::SCHEME_HTML; | |
352 | |
353 // Add it and make sure it is there. | |
354 EXPECT_TRUE(db_.AddLogin(form)); | |
355 EXPECT_TRUE(db_.GetAutofillableLogins(&result)); | |
356 EXPECT_EQ(1U, result.size()); | |
357 delete result[0]; | |
358 result.clear(); | |
359 | |
360 // Match against an exact copy. | |
361 EXPECT_TRUE(db_.GetLogins(form, &result)); | |
362 EXPECT_EQ(1U, result.size()); | |
363 delete result[0]; | |
364 result.clear(); | |
365 | |
366 // www.foo.com should match. | |
367 PasswordForm form2 = GetFormWithNewSignonRealm(form, "http://www.foo.com/"); | |
368 EXPECT_TRUE(db_.GetLogins(form2, &result)); | |
369 EXPECT_EQ(1U, result.size()); | |
370 delete result[0]; | |
371 result.clear(); | |
372 | |
373 // a.b.foo.com should match. | |
374 form2 = GetFormWithNewSignonRealm(form, "http://a.b.foo.com/"); | |
375 EXPECT_TRUE(db_.GetLogins(form2, &result)); | |
376 EXPECT_EQ(1U, result.size()); | |
377 delete result[0]; | |
378 result.clear(); | |
379 | |
380 // foo.com with port 1337 should not match. | |
381 form2 = GetFormWithNewSignonRealm(form, "http://foo.com:1337/"); | |
382 EXPECT_TRUE(db_.GetLogins(form2, &result)); | |
383 EXPECT_EQ(0U, result.size()); | |
384 | |
385 // http://foo.com should match since the scheme is wrong. | |
palmer
2013/06/15 00:03:27
Typo: "should not match"
nyquist
2013/06/17 23:20:11
Done.
| |
386 form2 = GetFormWithNewSignonRealm(form, "https://foo.com/"); | |
387 EXPECT_TRUE(db_.GetLogins(form2, &result)); | |
388 EXPECT_EQ(0U, result.size()); | |
389 | |
390 // notfoo.com should not match. | |
391 form2 = GetFormWithNewSignonRealm(form, "http://notfoo.com/"); | |
392 EXPECT_TRUE(db_.GetLogins(form2, &result)); | |
393 EXPECT_EQ(0U, result.size()); | |
394 | |
395 // baz.com should not match. | |
396 form2 = GetFormWithNewSignonRealm(form, "http://baz.com/"); | |
397 EXPECT_TRUE(db_.GetLogins(form2, &result)); | |
398 EXPECT_EQ(0U, result.size()); | |
399 } | |
400 | |
180 static bool AddTimestampedLogin(LoginDatabase* db, std::string url, | 401 static bool AddTimestampedLogin(LoginDatabase* db, std::string url, |
181 const std::string& unique_string, | 402 const std::string& unique_string, |
182 const base::Time& time) { | 403 const base::Time& time) { |
183 // Example password form. | 404 // Example password form. |
184 PasswordForm form; | 405 PasswordForm form; |
185 form.origin = GURL(url + std::string("/LoginAuth")); | 406 form.origin = GURL(url + std::string("/LoginAuth")); |
186 form.username_element = ASCIIToUTF16(unique_string); | 407 form.username_element = ASCIIToUTF16(unique_string); |
187 form.username_value = ASCIIToUTF16(unique_string); | 408 form.username_value = ASCIIToUTF16(unique_string); |
188 form.password_element = ASCIIToUTF16(unique_string); | 409 form.password_element = ASCIIToUTF16(unique_string); |
189 form.submit_element = ASCIIToUTF16("signIn"); | 410 form.submit_element = ASCIIToUTF16("signIn"); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
287 | 508 |
288 // Normal data. | 509 // Normal data. |
289 vec.push_back(ASCIIToUTF16("first")); | 510 vec.push_back(ASCIIToUTF16("first")); |
290 vec.push_back(ASCIIToUTF16("second")); | 511 vec.push_back(ASCIIToUTF16("second")); |
291 vec.push_back(ASCIIToUTF16("third")); | 512 vec.push_back(ASCIIToUTF16("third")); |
292 | 513 |
293 temp = SerializeVector(vec); | 514 temp = SerializeVector(vec); |
294 output = DeserializeVector(temp); | 515 output = DeserializeVector(temp); |
295 EXPECT_THAT(output, Eq(vec)); | 516 EXPECT_THAT(output, Eq(vec)); |
296 } | 517 } |
OLD | NEW |