OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/password_manager/simple_password_store_mac.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "components/os_crypt/os_crypt.h" |
| 11 #include "components/password_manager/core/browser/login_database.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "content/public/test/test_utils.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 using content::BrowserThread; |
| 19 using password_manager::PasswordStoreChange; |
| 20 using testing::ElementsAreArray; |
| 21 |
| 22 namespace { |
| 23 |
| 24 class MockPasswordStoreObserver |
| 25 : public password_manager::PasswordStore::Observer { |
| 26 public: |
| 27 MOCK_METHOD1(OnLoginsChanged, |
| 28 void(const password_manager::PasswordStoreChangeList& changes)); |
| 29 }; |
| 30 |
| 31 class SimplePasswordStoreMacTest : public testing::Test { |
| 32 public: |
| 33 SimplePasswordStoreMacTest() |
| 34 : thread_bundle_(content::TestBrowserThreadBundle::REAL_FILE_THREAD) {} |
| 35 |
| 36 void SetUp() override { |
| 37 ASSERT_TRUE(db_dir_.CreateUniqueTempDir()); |
| 38 |
| 39 // Ensure that LoginDatabase will use the mock keychain if it needs to |
| 40 // encrypt/decrypt a password. |
| 41 OSCrypt::UseMockKeychain(true); |
| 42 |
| 43 // Setup LoginDatabase. |
| 44 scoped_ptr<password_manager::LoginDatabase> login_db( |
| 45 new password_manager::LoginDatabase(test_login_db_file_path())); |
| 46 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner = |
| 47 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE); |
| 48 ASSERT_TRUE(file_task_runner); |
| 49 file_task_runner->PostTask( |
| 50 FROM_HERE, |
| 51 base::Bind(&InitOnBackgroundThread, base::Unretained(login_db.get()))); |
| 52 store_ = new SimplePasswordStoreMac( |
| 53 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), |
| 54 file_task_runner, login_db.Pass()); |
| 55 ASSERT_TRUE(store_->Init(syncer::SyncableService::StartSyncFlare())); |
| 56 // Make sure deferred initialization is finished. |
| 57 FinishAsyncProcessing(); |
| 58 } |
| 59 |
| 60 void TearDown() override { |
| 61 store_->Shutdown(); |
| 62 EXPECT_TRUE(store_->GetBackgroundTaskRunner()); |
| 63 store_ = nullptr; |
| 64 } |
| 65 |
| 66 base::FilePath test_login_db_file_path() const { |
| 67 return db_dir_.path().Append(FILE_PATH_LITERAL("login.db")); |
| 68 } |
| 69 |
| 70 scoped_refptr<SimplePasswordStoreMac> store() { return store_; } |
| 71 |
| 72 void FinishAsyncProcessing() { |
| 73 scoped_refptr<content::MessageLoopRunner> runner( |
| 74 new content::MessageLoopRunner); |
| 75 ASSERT_TRUE(BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, |
| 76 base::Bind(&Noop), |
| 77 runner->QuitClosure())); |
| 78 runner->Run(); |
| 79 } |
| 80 |
| 81 private: |
| 82 static void InitOnBackgroundThread(password_manager::LoginDatabase* db) { |
| 83 ASSERT_TRUE(db->Init()); |
| 84 } |
| 85 |
| 86 static void Noop() {} |
| 87 |
| 88 base::ScopedTempDir db_dir_; |
| 89 scoped_refptr<SimplePasswordStoreMac> store_; |
| 90 |
| 91 content::TestBrowserThreadBundle thread_bundle_; |
| 92 }; |
| 93 |
| 94 TEST_F(SimplePasswordStoreMacTest, Sanity) { |
| 95 autofill::PasswordForm form; |
| 96 form.origin = GURL("http://accounts.google.com/LoginAuth"); |
| 97 form.signon_realm = "http://accounts.google.com/"; |
| 98 form.username_value = base::ASCIIToUTF16("my_username"); |
| 99 form.password_value = base::ASCIIToUTF16("12345"); |
| 100 MockPasswordStoreObserver observer; |
| 101 store()->AddObserver(&observer); |
| 102 |
| 103 const PasswordStoreChange expected_add_changes[] = { |
| 104 PasswordStoreChange(PasswordStoreChange::ADD, form), |
| 105 }; |
| 106 EXPECT_CALL(observer, |
| 107 OnLoginsChanged(ElementsAreArray(expected_add_changes))); |
| 108 // Adding a login should trigger a notification. |
| 109 store()->AddLogin(form); |
| 110 FinishAsyncProcessing(); |
| 111 } |
| 112 |
| 113 } // namespace |
OLD | NEW |