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 "chrome/browser/password_manager/password_store_mac.h" | 5 #include "chrome/browser/password_manager/password_store_mac.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/scoped_observer.h" | 9 #include "base/scoped_observer.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 using password_manager::LoginDatabase; | 30 using password_manager::LoginDatabase; |
31 using password_manager::PasswordStore; | 31 using password_manager::PasswordStore; |
32 using password_manager::PasswordStoreConsumer; | 32 using password_manager::PasswordStoreConsumer; |
33 using testing::_; | 33 using testing::_; |
34 using testing::DoAll; | 34 using testing::DoAll; |
35 using testing::Invoke; | 35 using testing::Invoke; |
36 using testing::WithArg; | 36 using testing::WithArg; |
37 | 37 |
38 namespace { | 38 namespace { |
39 | 39 |
40 ACTION(STLDeleteElements0) { | |
41 STLDeleteContainerPointers(arg0.begin(), arg0.end()); | |
42 } | |
43 | |
44 ACTION(QuitUIMessageLoop) { | 40 ACTION(QuitUIMessageLoop) { |
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
46 base::MessageLoop::current()->Quit(); | 42 base::MessageLoop::current()->Quit(); |
47 } | 43 } |
48 | 44 |
49 class MockPasswordStoreConsumer : public PasswordStoreConsumer { | 45 class MockPasswordStoreConsumer : public PasswordStoreConsumer { |
50 public: | 46 public: |
51 MOCK_METHOD1(OnGetPasswordStoreResults, | 47 MOCK_METHOD0(OnGetPasswordStoreResults, void()); |
52 void(const std::vector<autofill::PasswordForm*>&)); | |
53 | |
54 void CopyElements(const std::vector<autofill::PasswordForm*>& forms) { | |
55 last_result.clear(); | |
56 for (size_t i = 0; i < forms.size(); ++i) { | |
57 last_result.push_back(*forms[i]); | |
58 } | |
59 } | |
60 | 48 |
61 // Runs the current thread's message loop until OnGetPasswordStoreResults() | 49 // Runs the current thread's message loop until OnGetPasswordStoreResults() |
62 // is posted to it. This method should be called immediately after GetLogins, | 50 // is posted to it. This method should be called immediately after GetLogins, |
63 // without pumping the message loop in-between. | 51 // without pumping the message loop in-between. |
64 void WaitOnGetPasswordStoreResults() { | 52 void WaitOnGetPasswordStoreResults() { |
65 EXPECT_CALL(*this, OnGetPasswordStoreResults(_)).WillOnce(DoAll( | 53 EXPECT_CALL(*this, OnGetPasswordStoreResults()) |
66 WithArg<0>(Invoke(this, &MockPasswordStoreConsumer::CopyElements)), | 54 .WillOnce(QuitUIMessageLoop()); |
67 WithArg<0>(STLDeleteElements0()), | |
68 QuitUIMessageLoop())); | |
69 base::MessageLoop::current()->Run(); | 55 base::MessageLoop::current()->Run(); |
70 } | 56 } |
71 | |
72 std::vector<PasswordForm> last_result; | |
73 }; | 57 }; |
74 | 58 |
75 class MockPasswordStoreObserver : public PasswordStore::Observer { | 59 class MockPasswordStoreObserver : public PasswordStore::Observer { |
76 public: | 60 public: |
77 MOCK_METHOD1(OnLoginsChanged, | 61 MOCK_METHOD1(OnLoginsChanged, |
78 void(const password_manager::PasswordStoreChangeList& changes)); | 62 void(const password_manager::PasswordStoreChangeList& changes)); |
79 }; | 63 }; |
80 | 64 |
81 // A mock LoginDatabase that simulates a failing Init() method. | 65 // A mock LoginDatabase that simulates a failing Init() method. |
82 class BadLoginDatabase : public password_manager::LoginDatabase { | 66 class BadLoginDatabase : public password_manager::LoginDatabase { |
(...skipping 1187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1270 owned_keychain_adapter.AddPassword(*www_form); | 1254 owned_keychain_adapter.AddPassword(*www_form); |
1271 | 1255 |
1272 // 2. Get a password for m.facebook.com. | 1256 // 2. Get a password for m.facebook.com. |
1273 PasswordForm m_form(*www_form); | 1257 PasswordForm m_form(*www_form); |
1274 m_form.signon_realm = "http://m.facebook.com"; | 1258 m_form.signon_realm = "http://m.facebook.com"; |
1275 m_form.origin = GURL("http://m.facebook.com/index.html"); | 1259 m_form.origin = GURL("http://m.facebook.com/index.html"); |
1276 | 1260 |
1277 MockPasswordStoreConsumer consumer; | 1261 MockPasswordStoreConsumer consumer; |
1278 store_->GetLogins(m_form, PasswordStore::ALLOW_PROMPT, &consumer); | 1262 store_->GetLogins(m_form, PasswordStore::ALLOW_PROMPT, &consumer); |
1279 consumer.WaitOnGetPasswordStoreResults(); | 1263 consumer.WaitOnGetPasswordStoreResults(); |
1280 EXPECT_EQ(1u, consumer.last_result.size()); | 1264 EXPECT_EQ(1u, consumer.results()->size()); |
1281 | 1265 |
1282 // 3. Add the returned password for m.facebook.com. | 1266 // 3. Add the returned password for m.facebook.com. |
1283 login_db()->AddLogin(consumer.last_result[0]); | 1267 login_db()->AddLogin(*(*consumer.results())[0]); |
1284 owned_keychain_adapter.AddPassword(m_form); | 1268 owned_keychain_adapter.AddPassword(m_form); |
1285 | 1269 |
1286 // 4. Remove both passwords. | 1270 // 4. Remove both passwords. |
1287 store_->RemoveLogin(*www_form); | 1271 store_->RemoveLogin(*www_form); |
1288 store_->RemoveLogin(m_form); | 1272 store_->RemoveLogin(m_form); |
1289 FinishAsyncProcessing(); | 1273 FinishAsyncProcessing(); |
1290 | 1274 |
1291 // No trace of www.facebook.com. | 1275 // No trace of www.facebook.com. |
1292 ScopedVector<autofill::PasswordForm> matching_items = | 1276 ScopedVector<autofill::PasswordForm> matching_items = |
1293 owned_keychain_adapter.PasswordsFillingForm(www_form->signon_realm, | 1277 owned_keychain_adapter.PasswordsFillingForm(www_form->signon_realm, |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1509 scoped_ptr<PasswordForm> form(CreatePasswordFormFromData(www_form_data)); | 1493 scoped_ptr<PasswordForm> form(CreatePasswordFormFromData(www_form_data)); |
1510 store()->AddLogin(*form); | 1494 store()->AddLogin(*form); |
1511 | 1495 |
1512 MockPasswordStoreConsumer mock_consumer; | 1496 MockPasswordStoreConsumer mock_consumer; |
1513 store()->GetLogins(*form, PasswordStore::ALLOW_PROMPT, &mock_consumer); | 1497 store()->GetLogins(*form, PasswordStore::ALLOW_PROMPT, &mock_consumer); |
1514 | 1498 |
1515 // Now the read/write tasks are scheduled, let the DB initialization proceed. | 1499 // Now the read/write tasks are scheduled, let the DB initialization proceed. |
1516 event.Signal(); | 1500 event.Signal(); |
1517 | 1501 |
1518 mock_consumer.WaitOnGetPasswordStoreResults(); | 1502 mock_consumer.WaitOnGetPasswordStoreResults(); |
1519 EXPECT_EQ(1u, mock_consumer.last_result.size()); | 1503 EXPECT_EQ(1u, mock_consumer.results()->size()); |
1520 EXPECT_TRUE(login_db()); | 1504 EXPECT_TRUE(login_db()); |
1521 } | 1505 } |
1522 | 1506 |
1523 // Verify that operations on a PasswordStore with a bad database cause no | 1507 // Verify that operations on a PasswordStore with a bad database cause no |
1524 // explosions, but fail without side effect, return no data and trigger no | 1508 // explosions, but fail without side effect, return no data and trigger no |
1525 // notifications. | 1509 // notifications. |
1526 TEST_F(PasswordStoreMacTest, OperationsOnABadDatabaseSilentlyFail) { | 1510 TEST_F(PasswordStoreMacTest, OperationsOnABadDatabaseSilentlyFail) { |
1527 ClosePasswordStore(); | 1511 ClosePasswordStore(); |
1528 CreateAndInitPasswordStore( | 1512 CreateAndInitPasswordStore( |
1529 make_scoped_ptr<password_manager::LoginDatabase>(new BadLoginDatabase)); | 1513 make_scoped_ptr<password_manager::LoginDatabase>(new BadLoginDatabase)); |
(...skipping 15 matching lines...) Expand all Loading... |
1545 blacklisted_form->action = GURL("http://foo.example.com/action"); | 1529 blacklisted_form->action = GURL("http://foo.example.com/action"); |
1546 blacklisted_form->blacklisted_by_user = true; | 1530 blacklisted_form->blacklisted_by_user = true; |
1547 store()->AddLogin(*form); | 1531 store()->AddLogin(*form); |
1548 store()->AddLogin(*blacklisted_form); | 1532 store()->AddLogin(*blacklisted_form); |
1549 FinishAsyncProcessing(); | 1533 FinishAsyncProcessing(); |
1550 | 1534 |
1551 // Get all logins; autofillable logins; blacklisted logins. | 1535 // Get all logins; autofillable logins; blacklisted logins. |
1552 MockPasswordStoreConsumer mock_consumer; | 1536 MockPasswordStoreConsumer mock_consumer; |
1553 store()->GetLogins(*form, PasswordStore::DISALLOW_PROMPT, &mock_consumer); | 1537 store()->GetLogins(*form, PasswordStore::DISALLOW_PROMPT, &mock_consumer); |
1554 mock_consumer.WaitOnGetPasswordStoreResults(); | 1538 mock_consumer.WaitOnGetPasswordStoreResults(); |
1555 EXPECT_TRUE(mock_consumer.last_result.empty()); | 1539 EXPECT_TRUE(mock_consumer.results()->empty()); |
1556 store()->GetAutofillableLogins(&mock_consumer); | 1540 store()->GetAutofillableLogins(&mock_consumer); |
1557 mock_consumer.WaitOnGetPasswordStoreResults(); | 1541 mock_consumer.WaitOnGetPasswordStoreResults(); |
1558 EXPECT_TRUE(mock_consumer.last_result.empty()); | 1542 EXPECT_TRUE(mock_consumer.results()->empty()); |
1559 store()->GetBlacklistLogins(&mock_consumer); | 1543 store()->GetBlacklistLogins(&mock_consumer); |
1560 mock_consumer.WaitOnGetPasswordStoreResults(); | 1544 mock_consumer.WaitOnGetPasswordStoreResults(); |
1561 EXPECT_TRUE(mock_consumer.last_result.empty()); | 1545 EXPECT_TRUE(mock_consumer.results()->empty()); |
1562 | 1546 |
1563 // Report metrics. | 1547 // Report metrics. |
1564 store()->ReportMetrics("Test Username", true); | 1548 store()->ReportMetrics("Test Username", true); |
1565 FinishAsyncProcessing(); | 1549 FinishAsyncProcessing(); |
1566 | 1550 |
1567 // Change the login. | 1551 // Change the login. |
1568 form->password_value = base::ASCIIToUTF16("a different password"); | 1552 form->password_value = base::ASCIIToUTF16("a different password"); |
1569 store()->UpdateLogin(*form); | 1553 store()->UpdateLogin(*form); |
1570 FinishAsyncProcessing(); | 1554 FinishAsyncProcessing(); |
1571 | 1555 |
1572 // Delete one login; a range of logins. | 1556 // Delete one login; a range of logins. |
1573 store()->RemoveLogin(*form); | 1557 store()->RemoveLogin(*form); |
1574 store()->RemoveLoginsCreatedBetween(base::Time(), base::Time::Max()); | 1558 store()->RemoveLoginsCreatedBetween(base::Time(), base::Time::Max()); |
1575 store()->RemoveLoginsSyncedBetween(base::Time(), base::Time::Max()); | 1559 store()->RemoveLoginsSyncedBetween(base::Time(), base::Time::Max()); |
1576 FinishAsyncProcessing(); | 1560 FinishAsyncProcessing(); |
1577 | 1561 |
1578 // Verify no notifications are fired during shutdown either. | 1562 // Verify no notifications are fired during shutdown either. |
1579 ClosePasswordStore(); | 1563 ClosePasswordStore(); |
1580 } | 1564 } |
OLD | NEW |