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 // Example password form that has - in the domain name. |
| 361 PasswordForm form_dash = |
| 362 GetFormWithNewSignonRealm(form, "http://www.foo-bar.com/"); |
| 363 |
| 364 // Add it and make sure it is there. |
| 365 EXPECT_TRUE(db_.AddLogin(form_dash)); |
| 366 EXPECT_TRUE(db_.GetAutofillableLogins(&result)); |
| 367 EXPECT_EQ(2U, result.size()); |
| 368 delete result[0]; |
| 369 delete result[1]; |
| 370 result.clear(); |
| 371 |
| 372 // Match against an exact copy. |
| 373 EXPECT_TRUE(db_.GetLogins(form, &result)); |
| 374 EXPECT_EQ(1U, result.size()); |
| 375 delete result[0]; |
| 376 result.clear(); |
| 377 |
| 378 // www.foo.com should match. |
| 379 PasswordForm form2 = GetFormWithNewSignonRealm(form, "http://www.foo.com/"); |
| 380 EXPECT_TRUE(db_.GetLogins(form2, &result)); |
| 381 EXPECT_EQ(1U, result.size()); |
| 382 delete result[0]; |
| 383 result.clear(); |
| 384 |
| 385 // a.b.foo.com should match. |
| 386 form2 = GetFormWithNewSignonRealm(form, "http://a.b.foo.com/"); |
| 387 EXPECT_TRUE(db_.GetLogins(form2, &result)); |
| 388 EXPECT_EQ(1U, result.size()); |
| 389 delete result[0]; |
| 390 result.clear(); |
| 391 |
| 392 // a-b.foo.com should match. |
| 393 form2 = GetFormWithNewSignonRealm(form, "http://a-b.foo.com/"); |
| 394 EXPECT_TRUE(db_.GetLogins(form2, &result)); |
| 395 EXPECT_EQ(1U, result.size()); |
| 396 delete result[0]; |
| 397 result.clear(); |
| 398 |
| 399 // foo-bar.com should match. |
| 400 form2 = GetFormWithNewSignonRealm(form, "http://foo-bar.com/"); |
| 401 EXPECT_TRUE(db_.GetLogins(form2, &result)); |
| 402 EXPECT_EQ(1U, result.size()); |
| 403 delete result[0]; |
| 404 result.clear(); |
| 405 |
| 406 // www.foo-bar.com should match. |
| 407 form2 = GetFormWithNewSignonRealm(form, "http://www.foo-bar.com/"); |
| 408 EXPECT_TRUE(db_.GetLogins(form2, &result)); |
| 409 EXPECT_EQ(1U, result.size()); |
| 410 delete result[0]; |
| 411 result.clear(); |
| 412 |
| 413 // a.b.foo-bar.com should match. |
| 414 form2 = GetFormWithNewSignonRealm(form, "http://a.b.foo-bar.com/"); |
| 415 EXPECT_TRUE(db_.GetLogins(form2, &result)); |
| 416 EXPECT_EQ(1U, result.size()); |
| 417 delete result[0]; |
| 418 result.clear(); |
| 419 |
| 420 // a-b.foo-bar.com should match. |
| 421 form2 = GetFormWithNewSignonRealm(form, "http://a-b.foo-bar.com/"); |
| 422 EXPECT_TRUE(db_.GetLogins(form2, &result)); |
| 423 EXPECT_EQ(1U, result.size()); |
| 424 delete result[0]; |
| 425 result.clear(); |
| 426 |
| 427 // foo.com with port 1337 should not match. |
| 428 form2 = GetFormWithNewSignonRealm(form, "http://foo.com:1337/"); |
| 429 EXPECT_TRUE(db_.GetLogins(form2, &result)); |
| 430 EXPECT_EQ(0U, result.size()); |
| 431 |
| 432 // http://foo.com should not match since the scheme is wrong. |
| 433 form2 = GetFormWithNewSignonRealm(form, "https://foo.com/"); |
| 434 EXPECT_TRUE(db_.GetLogins(form2, &result)); |
| 435 EXPECT_EQ(0U, result.size()); |
| 436 |
| 437 // notfoo.com should not match. |
| 438 form2 = GetFormWithNewSignonRealm(form, "http://notfoo.com/"); |
| 439 EXPECT_TRUE(db_.GetLogins(form2, &result)); |
| 440 EXPECT_EQ(0U, result.size()); |
| 441 |
| 442 // baz.com should not match. |
| 443 form2 = GetFormWithNewSignonRealm(form, "http://baz.com/"); |
| 444 EXPECT_TRUE(db_.GetLogins(form2, &result)); |
| 445 EXPECT_EQ(0U, result.size()); |
| 446 |
| 447 // foo-baz.com should not match. |
| 448 form2 = GetFormWithNewSignonRealm(form, "http://foo-baz.com/"); |
| 449 EXPECT_TRUE(db_.GetLogins(form2, &result)); |
| 450 EXPECT_EQ(0U, result.size()); |
| 451 } |
| 452 |
180 static bool AddTimestampedLogin(LoginDatabase* db, std::string url, | 453 static bool AddTimestampedLogin(LoginDatabase* db, std::string url, |
181 const std::string& unique_string, | 454 const std::string& unique_string, |
182 const base::Time& time) { | 455 const base::Time& time) { |
183 // Example password form. | 456 // Example password form. |
184 PasswordForm form; | 457 PasswordForm form; |
185 form.origin = GURL(url + std::string("/LoginAuth")); | 458 form.origin = GURL(url + std::string("/LoginAuth")); |
186 form.username_element = ASCIIToUTF16(unique_string); | 459 form.username_element = ASCIIToUTF16(unique_string); |
187 form.username_value = ASCIIToUTF16(unique_string); | 460 form.username_value = ASCIIToUTF16(unique_string); |
188 form.password_element = ASCIIToUTF16(unique_string); | 461 form.password_element = ASCIIToUTF16(unique_string); |
189 form.submit_element = ASCIIToUTF16("signIn"); | 462 form.submit_element = ASCIIToUTF16("signIn"); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 | 560 |
288 // Normal data. | 561 // Normal data. |
289 vec.push_back(ASCIIToUTF16("first")); | 562 vec.push_back(ASCIIToUTF16("first")); |
290 vec.push_back(ASCIIToUTF16("second")); | 563 vec.push_back(ASCIIToUTF16("second")); |
291 vec.push_back(ASCIIToUTF16("third")); | 564 vec.push_back(ASCIIToUTF16("third")); |
292 | 565 |
293 temp = SerializeVector(vec); | 566 temp = SerializeVector(vec); |
294 output = DeserializeVector(temp); | 567 output = DeserializeVector(temp); |
295 EXPECT_THAT(output, Eq(vec)); | 568 EXPECT_THAT(output, Eq(vec)); |
296 } | 569 } |
OLD | NEW |