| 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 <windows.h> | 5 #include <windows.h> |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "components/password_manager/core/common/password_manager_pref_names.h" | 26 #include "components/password_manager/core/common/password_manager_pref_names.h" |
| 27 #include "components/webdata/common/web_database_service.h" | 27 #include "components/webdata/common/web_database_service.h" |
| 28 #include "content/public/test/test_browser_thread.h" | 28 #include "content/public/test/test_browser_thread.h" |
| 29 #include "crypto/wincrypt_shim.h" | 29 #include "crypto/wincrypt_shim.h" |
| 30 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 32 | 32 |
| 33 using autofill::PasswordForm; | 33 using autofill::PasswordForm; |
| 34 using base::WaitableEvent; | 34 using base::WaitableEvent; |
| 35 using content::BrowserThread; | 35 using content::BrowserThread; |
| 36 using password_manager::ContainsSamePasswordForms; |
| 36 using password_manager::LoginDatabase; | 37 using password_manager::LoginDatabase; |
| 37 using password_manager::ContainsAllPasswordForms; | |
| 38 using password_manager::PasswordFormData; | 38 using password_manager::PasswordFormData; |
| 39 using password_manager::PasswordStore; | 39 using password_manager::PasswordStore; |
| 40 using password_manager::PasswordStoreConsumer; | 40 using password_manager::PasswordStoreConsumer; |
| 41 using testing::_; | 41 using testing::_; |
| 42 using testing::DoAll; | 42 using testing::DoAll; |
| 43 using testing::IsEmpty; |
| 43 using testing::WithArg; | 44 using testing::WithArg; |
| 44 | 45 |
| 45 namespace { | 46 namespace { |
| 46 | 47 |
| 47 class MockPasswordStoreConsumer : public PasswordStoreConsumer { | 48 class MockPasswordStoreConsumer : public PasswordStoreConsumer { |
| 48 public: | 49 public: |
| 49 MOCK_METHOD1(OnGetPasswordStoreResults, | 50 MOCK_METHOD1(OnGetPasswordStoreResultsConstRef, |
| 50 void(const std::vector<autofill::PasswordForm*>&)); | 51 void(const std::vector<PasswordForm*>&)); |
| 52 |
| 53 // GMock cannot mock methods with move-only args. |
| 54 void OnGetPasswordStoreResults(ScopedVector<PasswordForm> results) override { |
| 55 OnGetPasswordStoreResultsConstRef(results.get()); |
| 56 } |
| 51 }; | 57 }; |
| 52 | 58 |
| 53 class MockWebDataServiceConsumer : public WebDataServiceConsumer { | 59 class MockWebDataServiceConsumer : public WebDataServiceConsumer { |
| 54 public: | 60 public: |
| 55 MOCK_METHOD2(OnWebDataServiceRequestDone, | 61 MOCK_METHOD2(OnWebDataServiceRequestDone, |
| 56 void(PasswordWebDataService::Handle, const WDTypedResult*)); | 62 void(PasswordWebDataService::Handle, const WDTypedResult*)); |
| 57 }; | 63 }; |
| 58 | 64 |
| 59 } // anonymous namespace | 65 } // anonymous namespace |
| 60 | 66 |
| 61 typedef std::vector<PasswordForm*> VectorOfForms; | |
| 62 | |
| 63 class PasswordStoreWinTest : public testing::Test { | 67 class PasswordStoreWinTest : public testing::Test { |
| 64 protected: | 68 protected: |
| 65 PasswordStoreWinTest() | 69 PasswordStoreWinTest() |
| 66 : ui_thread_(BrowserThread::UI, &message_loop_), | 70 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 67 db_thread_(BrowserThread::DB) { | 71 db_thread_(BrowserThread::DB) { |
| 68 } | 72 } |
| 69 | 73 |
| 70 bool CreateIE7PasswordInfo(const std::wstring& url, const base::Time& created, | 74 bool CreateIE7PasswordInfo(const std::wstring& url, const base::Time& created, |
| 71 IE7PasswordInfo* info) { | 75 IE7PasswordInfo* info) { |
| 72 // Copied from chrome/browser/importer/importer_unittest.cc | 76 // Copied from chrome/browser/importer/importer_unittest.cc |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | 208 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
| 205 base::Bind(&WaitableEvent::Signal, base::Unretained(&done))); | 209 base::Bind(&WaitableEvent::Signal, base::Unretained(&done))); |
| 206 done.Wait(); | 210 done.Wait(); |
| 207 | 211 |
| 208 store_ = CreatePasswordStore(); | 212 store_ = CreatePasswordStore(); |
| 209 EXPECT_TRUE(store_->Init(syncer::SyncableService::StartSyncFlare())); | 213 EXPECT_TRUE(store_->Init(syncer::SyncableService::StartSyncFlare())); |
| 210 | 214 |
| 211 MockPasswordStoreConsumer consumer; | 215 MockPasswordStoreConsumer consumer; |
| 212 | 216 |
| 213 // Make sure we quit the MessageLoop even if the test fails. | 217 // Make sure we quit the MessageLoop even if the test fails. |
| 214 ON_CALL(consumer, OnGetPasswordStoreResults(_)) | 218 ON_CALL(consumer, OnGetPasswordStoreResultsConstRef(_)) |
| 215 .WillByDefault(QuitUIMessageLoop()); | 219 .WillByDefault(QuitUIMessageLoop()); |
| 216 | 220 |
| 217 PasswordFormData form_data = { | 221 PasswordFormData form_data = { |
| 218 PasswordForm::SCHEME_HTML, | 222 PasswordForm::SCHEME_HTML, |
| 219 "http://example.com/", | 223 "http://example.com/", |
| 220 "http://example.com/origin", | 224 "http://example.com/origin", |
| 221 "http://example.com/action", | 225 "http://example.com/action", |
| 222 L"submit_element", | 226 L"submit_element", |
| 223 L"username_element", | 227 L"username_element", |
| 224 L"password_element", | 228 L"password_element", |
| 225 L"", | 229 L"", |
| 226 L"", | 230 L"", |
| 227 true, false, 1, | 231 true, false, 1, |
| 228 }; | 232 }; |
| 229 scoped_ptr<PasswordForm> form(CreatePasswordFormFromData(form_data)); | 233 scoped_ptr<PasswordForm> form = CreatePasswordFormFromData(form_data); |
| 230 | 234 |
| 231 // The returned form will not have 'action' or '*_element' fields set. This | 235 // The returned form will not have 'action' or '*_element' fields set. This |
| 232 // is because credentials imported from IE don't have this information. | 236 // is because credentials imported from IE don't have this information. |
| 233 PasswordFormData expected_form_data = { | 237 PasswordFormData expected_form_data = { |
| 234 PasswordForm::SCHEME_HTML, | 238 PasswordForm::SCHEME_HTML, |
| 235 "http://example.com/", | 239 "http://example.com/", |
| 236 "http://example.com/origin", | 240 "http://example.com/origin", |
| 237 "", | 241 "", |
| 238 L"", | 242 L"", |
| 239 L"", | 243 L"", |
| 240 L"", | 244 L"", |
| 241 L"abcdefgh", | 245 L"abcdefgh", |
| 242 L"abcdefghijkl", | 246 L"abcdefghijkl", |
| 243 true, false, 1, | 247 true, false, 1, |
| 244 }; | 248 }; |
| 245 std::vector<PasswordForm*> forms; | 249 ScopedVector<autofill::PasswordForm> expected_forms; |
| 246 forms.push_back(CreatePasswordFormFromData(expected_form_data)); | 250 expected_forms.push_back(CreatePasswordFormFromData(expected_form_data)); |
| 247 | 251 |
| 248 // The IE7 password should be returned. | 252 // The IE7 password should be returned. |
| 249 EXPECT_CALL(consumer, | 253 EXPECT_CALL(consumer, OnGetPasswordStoreResultsConstRef( |
| 250 OnGetPasswordStoreResults(ContainsAllPasswordForms(forms))) | 254 ContainsSamePasswordForms(expected_forms.get()))); |
| 251 .WillOnce(QuitUIMessageLoop()); | |
| 252 | 255 |
| 253 store_->GetLogins(*form, PasswordStore::DISALLOW_PROMPT, &consumer); | 256 store_->GetLogins(*form, PasswordStore::DISALLOW_PROMPT, &consumer); |
| 254 base::MessageLoop::current()->Run(); | 257 base::MessageLoop::current()->Run(); |
| 255 | |
| 256 STLDeleteElements(&forms); | |
| 257 } | 258 } |
| 258 | 259 |
| 259 // Crashy. http://crbug.com/86558 | 260 // Crashy. http://crbug.com/86558 |
| 260 TEST_F(PasswordStoreWinTest, DISABLED_OutstandingWDSQueries) { | 261 TEST_F(PasswordStoreWinTest, DISABLED_OutstandingWDSQueries) { |
| 261 store_ = CreatePasswordStore(); | 262 store_ = CreatePasswordStore(); |
| 262 EXPECT_TRUE(store_->Init(syncer::SyncableService::StartSyncFlare())); | 263 EXPECT_TRUE(store_->Init(syncer::SyncableService::StartSyncFlare())); |
| 263 | 264 |
| 264 PasswordFormData form_data = { | 265 PasswordFormData form_data = { |
| 265 PasswordForm::SCHEME_HTML, | 266 PasswordForm::SCHEME_HTML, |
| 266 "http://example.com/", | 267 "http://example.com/", |
| 267 "http://example.com/origin", | 268 "http://example.com/origin", |
| 268 "http://example.com/action", | 269 "http://example.com/action", |
| 269 L"submit_element", | 270 L"submit_element", |
| 270 L"username_element", | 271 L"username_element", |
| 271 L"password_element", | 272 L"password_element", |
| 272 L"", | 273 L"", |
| 273 L"", | 274 L"", |
| 274 true, false, 1, | 275 true, false, 1, |
| 275 }; | 276 }; |
| 276 scoped_ptr<PasswordForm> form(CreatePasswordFormFromData(form_data)); | 277 scoped_ptr<PasswordForm> form = CreatePasswordFormFromData(form_data); |
| 277 | 278 |
| 278 MockPasswordStoreConsumer consumer; | 279 MockPasswordStoreConsumer consumer; |
| 279 store_->GetLogins(*form, PasswordStore::DISALLOW_PROMPT, &consumer); | 280 store_->GetLogins(*form, PasswordStore::DISALLOW_PROMPT, &consumer); |
| 280 | 281 |
| 281 // Release the PSW and the WDS before the query can return. | 282 // Release the PSW and the WDS before the query can return. |
| 282 store_->Shutdown(); | 283 store_->Shutdown(); |
| 283 store_ = NULL; | 284 store_ = NULL; |
| 284 wds_ = NULL; | 285 wds_ = NULL; |
| 285 | 286 |
| 286 base::MessageLoop::current()->RunUntilIdle(); | 287 base::MessageLoop::current()->RunUntilIdle(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 299 WaitableEvent done(false, false); | 300 WaitableEvent done(false, false); |
| 300 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | 301 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
| 301 base::Bind(&WaitableEvent::Signal, base::Unretained(&done))); | 302 base::Bind(&WaitableEvent::Signal, base::Unretained(&done))); |
| 302 done.Wait(); | 303 done.Wait(); |
| 303 | 304 |
| 304 store_ = CreatePasswordStore(); | 305 store_ = CreatePasswordStore(); |
| 305 EXPECT_TRUE(store_->Init(syncer::SyncableService::StartSyncFlare())); | 306 EXPECT_TRUE(store_->Init(syncer::SyncableService::StartSyncFlare())); |
| 306 | 307 |
| 307 MockPasswordStoreConsumer password_consumer; | 308 MockPasswordStoreConsumer password_consumer; |
| 308 // Make sure we quit the MessageLoop even if the test fails. | 309 // Make sure we quit the MessageLoop even if the test fails. |
| 309 ON_CALL(password_consumer, OnGetPasswordStoreResults(_)) | 310 ON_CALL(password_consumer, OnGetPasswordStoreResultsConstRef(_)) |
| 310 .WillByDefault(QuitUIMessageLoop()); | 311 .WillByDefault(QuitUIMessageLoop()); |
| 311 | 312 |
| 312 PasswordFormData form_data = { | 313 PasswordFormData form_data = { |
| 313 PasswordForm::SCHEME_HTML, | 314 PasswordForm::SCHEME_HTML, |
| 314 "http://example.com/", | 315 "http://example.com/", |
| 315 "http://example.com/origin", | 316 "http://example.com/origin", |
| 316 "http://example.com/action", | 317 "http://example.com/action", |
| 317 L"submit_element", | 318 L"submit_element", |
| 318 L"username_element", | 319 L"username_element", |
| 319 L"password_element", | 320 L"password_element", |
| 320 L"", | 321 L"", |
| 321 L"", | 322 L"", |
| 322 true, false, 1, | 323 true, false, 1, |
| 323 }; | 324 }; |
| 324 scoped_ptr<PasswordForm> form(CreatePasswordFormFromData(form_data)); | 325 scoped_ptr<PasswordForm> form = CreatePasswordFormFromData(form_data); |
| 325 | 326 |
| 326 PasswordFormData expected_form_data = { | 327 PasswordFormData expected_form_data = { |
| 327 PasswordForm::SCHEME_HTML, | 328 PasswordForm::SCHEME_HTML, |
| 328 "http://example.com/", | 329 "http://example.com/", |
| 329 "http://example.com/origin", | 330 "http://example.com/origin", |
| 330 "http://example.com/action", | 331 "http://example.com/action", |
| 331 L"submit_element", | 332 L"submit_element", |
| 332 L"username_element", | 333 L"username_element", |
| 333 L"password_element", | 334 L"password_element", |
| 334 L"abcdefgh", | 335 L"abcdefgh", |
| 335 L"abcdefghijkl", | 336 L"abcdefghijkl", |
| 336 true, false, 1, | 337 true, false, 1, |
| 337 }; | 338 }; |
| 338 std::vector<PasswordForm*> forms; | 339 ScopedVector<autofill::PasswordForm> expected_forms; |
| 339 forms.push_back(CreatePasswordFormFromData(expected_form_data)); | 340 expected_forms.push_back(CreatePasswordFormFromData(expected_form_data)); |
| 340 | 341 |
| 341 // The IE7 password should be returned. | 342 // The IE7 password should be returned. |
| 342 EXPECT_CALL(password_consumer, | 343 EXPECT_CALL(password_consumer, |
| 343 OnGetPasswordStoreResults(ContainsAllPasswordForms(forms))) | 344 OnGetPasswordStoreResultsConstRef( |
| 344 .WillOnce(QuitUIMessageLoop()); | 345 ContainsSamePasswordForms(expected_forms.get()))); |
| 345 | 346 |
| 346 store_->GetLogins(*form, PasswordStore::DISALLOW_PROMPT, &password_consumer); | 347 store_->GetLogins(*form, PasswordStore::DISALLOW_PROMPT, &password_consumer); |
| 347 | 348 |
| 348 MockWebDataServiceConsumer wds_consumer; | 349 MockWebDataServiceConsumer wds_consumer; |
| 349 | 350 |
| 350 EXPECT_CALL(wds_consumer, | 351 EXPECT_CALL(wds_consumer, OnWebDataServiceRequestDone(_, _)) |
| 351 OnWebDataServiceRequestDone(_, _)) | |
| 352 .WillOnce(QuitUIMessageLoop()); | 352 .WillOnce(QuitUIMessageLoop()); |
| 353 | 353 |
| 354 wds_->GetIE7Login(password_info, &wds_consumer); | 354 wds_->GetIE7Login(password_info, &wds_consumer); |
| 355 | 355 |
| 356 // Run the MessageLoop twice: once for the GetIE7Login that PasswordStoreWin | 356 // Run the MessageLoop twice: once for the GetIE7Login that PasswordStoreWin |
| 357 // schedules on the DB thread and once for the one we just scheduled on the UI | 357 // schedules on the DB thread and once for the one we just scheduled on the UI |
| 358 // thread. | 358 // thread. |
| 359 base::MessageLoop::current()->Run(); | 359 base::MessageLoop::current()->Run(); |
| 360 base::MessageLoop::current()->Run(); | 360 base::MessageLoop::current()->Run(); |
| 361 | |
| 362 STLDeleteElements(&forms); | |
| 363 } | 361 } |
| 364 | 362 |
| 365 TEST_F(PasswordStoreWinTest, EmptyLogins) { | 363 TEST_F(PasswordStoreWinTest, EmptyLogins) { |
| 366 store_ = CreatePasswordStore(); | 364 store_ = CreatePasswordStore(); |
| 367 store_->Init(syncer::SyncableService::StartSyncFlare()); | 365 store_->Init(syncer::SyncableService::StartSyncFlare()); |
| 368 | 366 |
| 369 PasswordFormData form_data = { | 367 PasswordFormData form_data = { |
| 370 PasswordForm::SCHEME_HTML, | 368 PasswordForm::SCHEME_HTML, |
| 371 "http://example.com/", | 369 "http://example.com/", |
| 372 "http://example.com/origin", | 370 "http://example.com/origin", |
| 373 "http://example.com/action", | 371 "http://example.com/action", |
| 374 L"submit_element", | 372 L"submit_element", |
| 375 L"username_element", | 373 L"username_element", |
| 376 L"password_element", | 374 L"password_element", |
| 377 L"", | 375 L"", |
| 378 L"", | 376 L"", |
| 379 true, false, 1, | 377 true, false, 1, |
| 380 }; | 378 }; |
| 381 scoped_ptr<PasswordForm> form(CreatePasswordFormFromData(form_data)); | 379 scoped_ptr<PasswordForm> form = CreatePasswordFormFromData(form_data); |
| 382 | 380 |
| 383 MockPasswordStoreConsumer consumer; | 381 MockPasswordStoreConsumer consumer; |
| 384 | 382 |
| 385 // Make sure we quit the MessageLoop even if the test fails. | 383 // Make sure we quit the MessageLoop even if the test fails. |
| 386 ON_CALL(consumer, OnGetPasswordStoreResults(_)) | 384 ON_CALL(consumer, OnGetPasswordStoreResultsConstRef(_)) |
| 387 .WillByDefault(QuitUIMessageLoop()); | 385 .WillByDefault(QuitUIMessageLoop()); |
| 388 | 386 |
| 389 VectorOfForms expect_none; | 387 EXPECT_CALL(consumer, OnGetPasswordStoreResultsConstRef(IsEmpty())); |
| 390 // expect that we get no results; | |
| 391 EXPECT_CALL(consumer, | |
| 392 OnGetPasswordStoreResults(ContainsAllPasswordForms(expect_none))) | |
| 393 .WillOnce(DoAll(WithArg<0>(STLDeleteElements0()), QuitUIMessageLoop())); | |
| 394 | 388 |
| 395 store_->GetLogins(*form, PasswordStore::DISALLOW_PROMPT, &consumer); | 389 store_->GetLogins(*form, PasswordStore::DISALLOW_PROMPT, &consumer); |
| 396 base::MessageLoop::current()->Run(); | 390 base::MessageLoop::current()->Run(); |
| 397 } | 391 } |
| 398 | 392 |
| 399 TEST_F(PasswordStoreWinTest, EmptyBlacklistLogins) { | 393 TEST_F(PasswordStoreWinTest, EmptyBlacklistLogins) { |
| 400 store_ = CreatePasswordStore(); | 394 store_ = CreatePasswordStore(); |
| 401 store_->Init(syncer::SyncableService::StartSyncFlare()); | 395 store_->Init(syncer::SyncableService::StartSyncFlare()); |
| 402 | 396 |
| 403 MockPasswordStoreConsumer consumer; | 397 MockPasswordStoreConsumer consumer; |
| 404 | 398 |
| 405 // Make sure we quit the MessageLoop even if the test fails. | 399 // Make sure we quit the MessageLoop even if the test fails. |
| 406 ON_CALL(consumer, OnGetPasswordStoreResults(_)) | 400 ON_CALL(consumer, OnGetPasswordStoreResultsConstRef(_)) |
| 407 .WillByDefault(QuitUIMessageLoop()); | 401 .WillByDefault(QuitUIMessageLoop()); |
| 408 | 402 |
| 409 VectorOfForms expect_none; | 403 EXPECT_CALL(consumer, OnGetPasswordStoreResultsConstRef(IsEmpty())); |
| 410 // expect that we get no results; | |
| 411 EXPECT_CALL( | |
| 412 consumer, | |
| 413 OnGetPasswordStoreResults(ContainsAllPasswordForms(expect_none))) | |
| 414 .WillOnce(DoAll(WithArg<0>(STLDeleteElements0()), QuitUIMessageLoop())); | |
| 415 | 404 |
| 416 store_->GetBlacklistLogins(&consumer); | 405 store_->GetBlacklistLogins(&consumer); |
| 417 base::MessageLoop::current()->Run(); | 406 base::MessageLoop::current()->Run(); |
| 418 } | 407 } |
| 419 | 408 |
| 420 TEST_F(PasswordStoreWinTest, EmptyAutofillableLogins) { | 409 TEST_F(PasswordStoreWinTest, EmptyAutofillableLogins) { |
| 421 store_ = CreatePasswordStore(); | 410 store_ = CreatePasswordStore(); |
| 422 store_->Init(syncer::SyncableService::StartSyncFlare()); | 411 store_->Init(syncer::SyncableService::StartSyncFlare()); |
| 423 | 412 |
| 424 MockPasswordStoreConsumer consumer; | 413 MockPasswordStoreConsumer consumer; |
| 425 | 414 |
| 426 // Make sure we quit the MessageLoop even if the test fails. | 415 // Make sure we quit the MessageLoop even if the test fails. |
| 427 ON_CALL(consumer, OnGetPasswordStoreResults(_)) | 416 ON_CALL(consumer, OnGetPasswordStoreResultsConstRef(_)) |
| 428 .WillByDefault(QuitUIMessageLoop()); | 417 .WillByDefault(QuitUIMessageLoop()); |
| 429 | 418 |
| 430 VectorOfForms expect_none; | 419 EXPECT_CALL(consumer, OnGetPasswordStoreResultsConstRef(IsEmpty())); |
| 431 // expect that we get no results; | |
| 432 EXPECT_CALL( | |
| 433 consumer, | |
| 434 OnGetPasswordStoreResults(ContainsAllPasswordForms(expect_none))) | |
| 435 .WillOnce(DoAll(WithArg<0>(STLDeleteElements0()), QuitUIMessageLoop())); | |
| 436 | 420 |
| 437 store_->GetAutofillableLogins(&consumer); | 421 store_->GetAutofillableLogins(&consumer); |
| 438 base::MessageLoop::current()->Run(); | 422 base::MessageLoop::current()->Run(); |
| 439 } | 423 } |
| OLD | NEW |