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 <stdarg.h> | 5 #include <stdarg.h> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "chrome/browser/password_manager/native_backend_gnome_x.h" | 14 #include "chrome/browser/password_manager/native_backend_gnome_x.h" |
| 15 #include "chrome/browser/password_manager/psl_matching_helper.h" |
15 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
16 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
17 #include "components/autofill/core/common/password_form.h" | 18 #include "components/autofill/core/common/password_form.h" |
18 #include "content/public/test/test_browser_thread.h" | 19 #include "content/public/test/test_browser_thread.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 | 21 |
21 using autofill::PasswordForm; | 22 using autofill::PasswordForm; |
22 using base::UTF8ToUTF16; | 23 using base::UTF8ToUTF16; |
23 using base::UTF16ToUTF8; | 24 using base::UTF16ToUTF8; |
24 using content::BrowserThread; | 25 using content::BrowserThread; |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 | 292 |
292 ASSERT_TRUE(MockGnomeKeyringLoader::LoadMockGnomeKeyring()); | 293 ASSERT_TRUE(MockGnomeKeyringLoader::LoadMockGnomeKeyring()); |
293 | 294 |
294 form_google_.origin = GURL("http://www.google.com/"); | 295 form_google_.origin = GURL("http://www.google.com/"); |
295 form_google_.action = GURL("http://www.google.com/login"); | 296 form_google_.action = GURL("http://www.google.com/login"); |
296 form_google_.username_element = UTF8ToUTF16("user"); | 297 form_google_.username_element = UTF8ToUTF16("user"); |
297 form_google_.username_value = UTF8ToUTF16("joeschmoe"); | 298 form_google_.username_value = UTF8ToUTF16("joeschmoe"); |
298 form_google_.password_element = UTF8ToUTF16("pass"); | 299 form_google_.password_element = UTF8ToUTF16("pass"); |
299 form_google_.password_value = UTF8ToUTF16("seekrit"); | 300 form_google_.password_value = UTF8ToUTF16("seekrit"); |
300 form_google_.submit_element = UTF8ToUTF16("submit"); | 301 form_google_.submit_element = UTF8ToUTF16("submit"); |
301 form_google_.signon_realm = "Google"; | 302 form_google_.signon_realm = "http://www.google.com/"; |
| 303 |
| 304 form_facebook_.origin = GURL("http://www.facebook.com/"); |
| 305 form_facebook_.action = GURL("http://www.facebook.com/login"); |
| 306 form_facebook_.username_element = UTF8ToUTF16("user"); |
| 307 form_facebook_.username_value = UTF8ToUTF16("a"); |
| 308 form_facebook_.password_element = UTF8ToUTF16("password"); |
| 309 form_facebook_.password_value = UTF8ToUTF16("b"); |
| 310 form_facebook_.submit_element = UTF8ToUTF16("submit"); |
| 311 form_facebook_.signon_realm = "http://www.facebook.com/"; |
302 | 312 |
303 form_isc_.origin = GURL("http://www.isc.org/"); | 313 form_isc_.origin = GURL("http://www.isc.org/"); |
304 form_isc_.action = GURL("http://www.isc.org/auth"); | 314 form_isc_.action = GURL("http://www.isc.org/auth"); |
305 form_isc_.username_element = UTF8ToUTF16("id"); | 315 form_isc_.username_element = UTF8ToUTF16("id"); |
306 form_isc_.username_value = UTF8ToUTF16("janedoe"); | 316 form_isc_.username_value = UTF8ToUTF16("janedoe"); |
307 form_isc_.password_element = UTF8ToUTF16("passwd"); | 317 form_isc_.password_element = UTF8ToUTF16("passwd"); |
308 form_isc_.password_value = UTF8ToUTF16("ihazabukkit"); | 318 form_isc_.password_value = UTF8ToUTF16("ihazabukkit"); |
309 form_isc_.submit_element = UTF8ToUTF16("login"); | 319 form_isc_.submit_element = UTF8ToUTF16("login"); |
310 form_isc_.signon_realm = "ISC"; | 320 form_isc_.signon_realm = "http://www.isc.org/"; |
311 } | 321 } |
312 | 322 |
313 virtual void TearDown() { | 323 virtual void TearDown() { |
314 base::MessageLoop::current()->PostTask(FROM_HERE, | 324 base::MessageLoop::current()->PostTask(FROM_HERE, |
315 base::MessageLoop::QuitClosure()); | 325 base::MessageLoop::QuitClosure()); |
316 base::MessageLoop::current()->Run(); | 326 base::MessageLoop::current()->Run(); |
317 db_thread_.Stop(); | 327 db_thread_.Stop(); |
318 } | 328 } |
319 | 329 |
320 void RunBothThreads() { | 330 void RunBothThreads() { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 UTF16ToUTF8(form.submit_element)); | 386 UTF16ToUTF8(form.submit_element)); |
377 CheckStringAttribute(item, "signon_realm", form.signon_realm); | 387 CheckStringAttribute(item, "signon_realm", form.signon_realm); |
378 CheckUint32Attribute(item, "ssl_valid", form.ssl_valid); | 388 CheckUint32Attribute(item, "ssl_valid", form.ssl_valid); |
379 CheckUint32Attribute(item, "preferred", form.preferred); | 389 CheckUint32Attribute(item, "preferred", form.preferred); |
380 // We don't check the date created. It varies. | 390 // We don't check the date created. It varies. |
381 CheckUint32Attribute(item, "blacklisted_by_user", form.blacklisted_by_user); | 391 CheckUint32Attribute(item, "blacklisted_by_user", form.blacklisted_by_user); |
382 CheckUint32Attribute(item, "scheme", form.scheme); | 392 CheckUint32Attribute(item, "scheme", form.scheme); |
383 CheckStringAttribute(item, "application", app_string); | 393 CheckStringAttribute(item, "application", app_string); |
384 } | 394 } |
385 | 395 |
| 396 // Checks (using EXPECT_* macros), that |credentials| are accessible for |
| 397 // filling in for a page with |origin| iff |
| 398 // |should_credential_be_available_to_url| is true. |
| 399 void CheckCredentialAvailability(const PasswordForm& credentials, |
| 400 const std::string& url, |
| 401 bool should_credential_be_available_to_url) { |
| 402 PSLMatchingHelper helper; |
| 403 ASSERT_TRUE(helper.IsMatchingEnabled()) |
| 404 << "PSL matching needs to be enabled."; |
| 405 |
| 406 NativeBackendGnome backend(321, profile_.GetPrefs()); |
| 407 backend.Init(); |
| 408 |
| 409 BrowserThread::PostTask( |
| 410 BrowserThread::DB, |
| 411 FROM_HERE, |
| 412 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin), |
| 413 base::Unretained(&backend), |
| 414 credentials)); |
| 415 |
| 416 PasswordForm target_form; |
| 417 target_form.origin = GURL(url); |
| 418 target_form.signon_realm = url; |
| 419 std::vector<PasswordForm*> form_list; |
| 420 BrowserThread::PostTask( |
| 421 BrowserThread::DB, |
| 422 FROM_HERE, |
| 423 base::Bind(base::IgnoreResult(&NativeBackendGnome::GetLogins), |
| 424 base::Unretained(&backend), |
| 425 target_form, |
| 426 &form_list)); |
| 427 |
| 428 RunBothThreads(); |
| 429 |
| 430 EXPECT_EQ(1u, mock_keyring_items.size()); |
| 431 if (mock_keyring_items.size() > 0) |
| 432 CheckMockKeyringItem(&mock_keyring_items[0], credentials, "chrome-321"); |
| 433 |
| 434 if (should_credential_be_available_to_url) |
| 435 EXPECT_EQ(1u, form_list.size()); |
| 436 else |
| 437 EXPECT_EQ(0u, form_list.size()); |
| 438 STLDeleteElements(&form_list); |
| 439 } |
| 440 |
386 base::MessageLoopForUI message_loop_; | 441 base::MessageLoopForUI message_loop_; |
387 content::TestBrowserThread ui_thread_; | 442 content::TestBrowserThread ui_thread_; |
388 content::TestBrowserThread db_thread_; | 443 content::TestBrowserThread db_thread_; |
389 | 444 |
390 TestingProfile profile_; | 445 TestingProfile profile_; |
391 | 446 |
392 // Provide some test forms to avoid having to set them up in each test. | 447 // Provide some test forms to avoid having to set them up in each test. |
393 PasswordForm form_google_; | 448 PasswordForm form_google_; |
| 449 PasswordForm form_facebook_; |
394 PasswordForm form_isc_; | 450 PasswordForm form_isc_; |
395 }; | 451 }; |
396 | 452 |
397 TEST_F(NativeBackendGnomeTest, BasicAddLogin) { | 453 TEST_F(NativeBackendGnomeTest, BasicAddLogin) { |
398 // Pretend that the migration has already taken place. | 454 // Pretend that the migration has already taken place. |
399 profile_.GetPrefs()->SetBoolean(prefs::kPasswordsUseLocalProfileId, true); | 455 profile_.GetPrefs()->SetBoolean(prefs::kPasswordsUseLocalProfileId, true); |
400 | 456 |
401 NativeBackendGnome backend(42, profile_.GetPrefs()); | 457 NativeBackendGnome backend(42, profile_.GetPrefs()); |
402 backend.Init(); | 458 backend.Init(); |
403 | 459 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 | 492 |
437 // Quick check that we got something back. | 493 // Quick check that we got something back. |
438 EXPECT_EQ(1u, form_list.size()); | 494 EXPECT_EQ(1u, form_list.size()); |
439 STLDeleteElements(&form_list); | 495 STLDeleteElements(&form_list); |
440 | 496 |
441 EXPECT_EQ(1u, mock_keyring_items.size()); | 497 EXPECT_EQ(1u, mock_keyring_items.size()); |
442 if (mock_keyring_items.size() > 0) | 498 if (mock_keyring_items.size() > 0) |
443 CheckMockKeyringItem(&mock_keyring_items[0], form_google_, "chrome-42"); | 499 CheckMockKeyringItem(&mock_keyring_items[0], form_google_, "chrome-42"); |
444 } | 500 } |
445 | 501 |
| 502 // Save a password for www.facebook.com and see it suggested for m.facebook.com. |
| 503 TEST_F(NativeBackendGnomeTest, PSLMatchingPositive) { |
| 504 CheckCredentialAvailability(form_facebook_, |
| 505 "http://m.facebook.com/", |
| 506 /*should_credential_be_available_to_url=*/true); |
| 507 } |
| 508 |
| 509 // Save a password for www.facebook.com and see it not suggested for |
| 510 // m-facebook.com. |
| 511 TEST_F(NativeBackendGnomeTest, PSLMatchingNegativeDomainMismatch) { |
| 512 CheckCredentialAvailability(form_facebook_, |
| 513 "http://m-facebook.com/", |
| 514 /*should_credential_be_available_to_url=*/false); |
| 515 } |
| 516 |
| 517 // Test PSL matching is off for domains excluded from it. |
| 518 TEST_F(NativeBackendGnomeTest, PSLMatchingDisabledDomains) { |
| 519 CheckCredentialAvailability(form_google_, |
| 520 "http://one.google.com/", |
| 521 /*should_credential_be_available_to_url=*/false); |
| 522 } |
| 523 |
446 TEST_F(NativeBackendGnomeTest, BasicRemoveLogin) { | 524 TEST_F(NativeBackendGnomeTest, BasicRemoveLogin) { |
447 // Pretend that the migration has already taken place. | 525 // Pretend that the migration has already taken place. |
448 profile_.GetPrefs()->SetBoolean(prefs::kPasswordsUseLocalProfileId, true); | 526 profile_.GetPrefs()->SetBoolean(prefs::kPasswordsUseLocalProfileId, true); |
449 | 527 |
450 NativeBackendGnome backend(42, profile_.GetPrefs()); | 528 NativeBackendGnome backend(42, profile_.GetPrefs()); |
451 backend.Init(); | 529 backend.Init(); |
452 | 530 |
453 BrowserThread::PostTask( | 531 BrowserThread::PostTask( |
454 BrowserThread::DB, FROM_HERE, | 532 BrowserThread::DB, FROM_HERE, |
455 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin), | 533 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin), |
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
901 RunBothThreads(); | 979 RunBothThreads(); |
902 | 980 |
903 // The other two copies of the password in different profiles should remain. | 981 // The other two copies of the password in different profiles should remain. |
904 EXPECT_EQ(2u, mock_keyring_items.size()); | 982 EXPECT_EQ(2u, mock_keyring_items.size()); |
905 if (mock_keyring_items.size() > 0) | 983 if (mock_keyring_items.size() > 0) |
906 CheckMockKeyringItem(&mock_keyring_items[0], form_google_, "chrome"); | 984 CheckMockKeyringItem(&mock_keyring_items[0], form_google_, "chrome"); |
907 if (mock_keyring_items.size() > 1) | 985 if (mock_keyring_items.size() > 1) |
908 CheckMockKeyringItem(&mock_keyring_items[1], form_google_, "chrome-42"); | 986 CheckMockKeyringItem(&mock_keyring_items[1], form_google_, "chrome-42"); |
909 } | 987 } |
910 } | 988 } |
OLD | NEW |