| Index: chrome/browser/chromeos/login/owner_manager_unittest.cc
|
| diff --git a/chrome/browser/chromeos/login/owner_manager_unittest.cc b/chrome/browser/chromeos/login/owner_manager_unittest.cc
|
| index fd5d6dd2d4157923b61edddb6069b6ab851eeb44..287b75a6058027a06a63d477c0f39c8de9afa55f 100644
|
| --- a/chrome/browser/chromeos/login/owner_manager_unittest.cc
|
| +++ b/chrome/browser/chromeos/login/owner_manager_unittest.cc
|
| @@ -2,28 +2,19 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| +#include "chrome/browser/chromeos/login/owner_manager_unittest.h"
|
| #include "chrome/browser/chromeos/login/owner_manager.h"
|
|
|
| -#include <cert.h>
|
| -#include <keyhi.h>
|
| -#include <keythi.h> // KeyType enum
|
| -#include <pk11pub.h>
|
| -#include <stdlib.h>
|
| -
|
| #include <string>
|
|
|
| #include "base/crypto/rsa_private_key.h"
|
| #include "base/file_path.h"
|
| #include "base/file_util.h"
|
| -#include "base/logging.h"
|
| -#include "base/nss_util_internal.h"
|
| #include "base/nss_util.h"
|
| +#include "base/logging.h"
|
| #include "base/scoped_temp_dir.h"
|
| #include "chrome/browser/chrome_thread.h"
|
| -#include "chrome/common/notification_observer.h"
|
| -#include "chrome/common/notification_registrar.h"
|
| -#include "chrome/common/notification_service.h"
|
| -#include "chrome/common/notification_type.h"
|
| +#include "chrome/browser/chromeos/login/mock_owner_key_utils.h"
|
| #include "testing/gmock/include/gmock/gmock.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| @@ -36,100 +27,14 @@ using ::testing::_;
|
|
|
| namespace chromeos {
|
|
|
| -namespace {
|
| -
|
| -class MockKeyUtils : public OwnerKeyUtils {
|
| - public:
|
| - MockKeyUtils() {}
|
| - ~MockKeyUtils() {}
|
| - MOCK_METHOD0(GenerateKeyPair, RSAPrivateKey*());
|
| - MOCK_METHOD1(ExportPublicKeyViaDbus, bool(RSAPrivateKey* pair));
|
| - MOCK_METHOD2(ExportPublicKeyToFile, bool(RSAPrivateKey* pair,
|
| - const FilePath& key_file));
|
| - MOCK_METHOD2(ImportPublicKey, bool(const FilePath& key_file,
|
| - std::vector<uint8>* output));
|
| - MOCK_METHOD1(FindPrivateKey, RSAPrivateKey*(const std::vector<uint8>& key));
|
| - MOCK_METHOD0(GetOwnerKeyFilePath, FilePath());
|
| - private:
|
| - DISALLOW_COPY_AND_ASSIGN(MockKeyUtils);
|
| -};
|
| -
|
| -class MockInjector : public OwnerKeyUtils::Factory {
|
| - public:
|
| - // Takes ownership of |mock|.
|
| - explicit MockInjector(MockKeyUtils* mock) :
|
| - transient_(mock),
|
| - delete_transient_(true) {
|
| - }
|
| -
|
| - virtual ~MockInjector() {
|
| - if (delete_transient_)
|
| - delete transient_;
|
| - }
|
| -
|
| - // If this is called, its caller takes ownership of |transient_|.
|
| - // If it's never called, |transient_| remains our problem.
|
| - OwnerKeyUtils* CreateOwnerKeyUtils() {
|
| - delete_transient_ = false;
|
| - return transient_;
|
| - }
|
| -
|
| - private:
|
| - MockKeyUtils* transient_;
|
| - bool delete_transient_;
|
| - DISALLOW_COPY_AND_ASSIGN(MockInjector);
|
| -};
|
| -
|
| -class KeyUser : public OwnerManager::Delegate {
|
| - public:
|
| - explicit KeyUser(const OwnerManager::KeyOpCode expected)
|
| - : expected_(expected) {
|
| - }
|
| -
|
| - virtual ~KeyUser() {}
|
| -
|
| - void OnKeyOpComplete(const OwnerManager::KeyOpCode return_code,
|
| - const std::string& payload) {
|
| - MessageLoop::current()->Quit();
|
| - EXPECT_EQ(expected_, return_code);
|
| - }
|
| -
|
| - const OwnerManager::KeyOpCode expected_;
|
| - private:
|
| - DISALLOW_COPY_AND_ASSIGN(KeyUser);
|
| -};
|
| -
|
| -static bool Win(RSAPrivateKey* key) {
|
| - MessageLoop::current()->Quit();
|
| - return true;
|
| -}
|
| -
|
| -static bool Fail(RSAPrivateKey* key) {
|
| - MessageLoop::current()->Quit();
|
| - return false;
|
| -}
|
| -
|
| -} // anonymous namespace
|
| -
|
| -class OwnerManagerTest : public ::testing::Test,
|
| - public NotificationObserver {
|
| +class OwnerManagerTest : public ::testing::Test {
|
| public:
|
| OwnerManagerTest()
|
| : message_loop_(MessageLoop::TYPE_UI),
|
| ui_thread_(ChromeThread::UI, &message_loop_),
|
| file_thread_(ChromeThread::FILE),
|
| - success_expected_(false),
|
| - quit_on_observe_(true),
|
| mock_(new MockKeyUtils),
|
| injector_(mock_) /* injector_ takes ownership of mock_ */ {
|
| - registrar_.Add(
|
| - this,
|
| - NotificationType::OWNER_KEY_FETCH_ATTEMPT_FAILED,
|
| - NotificationService::AllSources());
|
| - registrar_.Add(
|
| - this,
|
| - NotificationType::OWNER_KEY_FETCH_ATTEMPT_SUCCEEDED,
|
| - NotificationService::AllSources());
|
| }
|
| virtual ~OwnerManagerTest() {}
|
|
|
| @@ -159,27 +64,6 @@ class OwnerManagerTest : public ::testing::Test,
|
| manager->private_key_.reset(fake_private_key_.release());
|
| }
|
|
|
| - // NotificationObserver implementation.
|
| - virtual void Observe(NotificationType type,
|
| - const NotificationSource& source,
|
| - const NotificationDetails& details) {
|
| - if (type == NotificationType::OWNER_KEY_FETCH_ATTEMPT_SUCCEEDED) {
|
| - EXPECT_TRUE(success_expected_);
|
| - if (quit_on_observe_)
|
| - MessageLoop::current()->Quit();
|
| - } else if (type == NotificationType::OWNER_KEY_FETCH_ATTEMPT_FAILED) {
|
| - EXPECT_FALSE(success_expected_);
|
| - if (quit_on_observe_)
|
| - MessageLoop::current()->Quit();
|
| - }
|
| - }
|
| -
|
| - void ExpectKeyFetchSuccess(bool should_succeed) {
|
| - success_expected_ = should_succeed;
|
| - }
|
| -
|
| - void SetQuitOnKeyFetch(bool should_quit) { quit_on_observe_ = should_quit; }
|
| -
|
| ScopedTempDir tmpdir_;
|
| FilePath tmpfile_;
|
|
|
| @@ -190,40 +74,33 @@ class OwnerManagerTest : public ::testing::Test,
|
| std::vector<uint8> fake_public_key_;
|
| scoped_ptr<RSAPrivateKey> fake_private_key_;
|
|
|
| - NotificationRegistrar registrar_;
|
| - bool success_expected_;
|
| - bool quit_on_observe_;
|
| -
|
| MockKeyUtils* mock_;
|
| MockInjector injector_;
|
| +
|
| };
|
|
|
| -TEST_F(OwnerManagerTest, LoadKeyUnowned) {
|
| +TEST_F(OwnerManagerTest, LoadOwnerKeyFail) {
|
| StartUnowned();
|
| -
|
| - EXPECT_CALL(*mock_, GetOwnerKeyFilePath())
|
| - .WillRepeatedly(Return(tmpfile_));
|
| -
|
| + MockKeyLoadObserver loader;
|
| scoped_refptr<OwnerManager> manager(new OwnerManager);
|
| - EXPECT_FALSE(manager->StartLoadOwnerKeyAttempt());
|
| -}
|
|
|
| -TEST_F(OwnerManagerTest, LoadOwnerKeyFail) {
|
| EXPECT_CALL(*mock_, GetOwnerKeyFilePath())
|
| .WillRepeatedly(Return(tmpfile_));
|
| EXPECT_CALL(*mock_, ImportPublicKey(tmpfile_, _))
|
| .WillOnce(Return(false))
|
| .RetiresOnSaturation();
|
|
|
| - scoped_refptr<OwnerManager> manager(new OwnerManager);
|
| - EXPECT_TRUE(manager->StartLoadOwnerKeyAttempt());
|
| -
|
| - // Run remaining events, until ExportPublicKeyViaDbus().
|
| + ChromeThread::PostTask(
|
| + ChromeThread::FILE, FROM_HERE,
|
| + NewRunnableMethod(manager.get(),
|
| + &OwnerManager::LoadOwnerKey));
|
| message_loop_.Run();
|
| }
|
|
|
| TEST_F(OwnerManagerTest, LoadOwnerKey) {
|
| - ExpectKeyFetchSuccess(true);
|
| + MockKeyLoadObserver loader;
|
| + loader.ExpectKeyFetchSuccess(true);
|
| + scoped_refptr<OwnerManager> manager(new OwnerManager);
|
|
|
| EXPECT_CALL(*mock_, GetOwnerKeyFilePath())
|
| .WillRepeatedly(Return(tmpfile_));
|
| @@ -232,23 +109,19 @@ TEST_F(OwnerManagerTest, LoadOwnerKey) {
|
| Return(true)))
|
| .RetiresOnSaturation();
|
|
|
| - scoped_refptr<OwnerManager> manager(new OwnerManager);
|
| - EXPECT_TRUE(manager->StartLoadOwnerKeyAttempt());
|
| + ChromeThread::PostTask(
|
| + ChromeThread::FILE, FROM_HERE,
|
| + NewRunnableMethod(manager.get(),
|
| + &OwnerManager::LoadOwnerKey));
|
|
|
| - // Run remaining events, until ExportPublicKeyViaDbus().
|
| message_loop_.Run();
|
| }
|
|
|
| -TEST_F(OwnerManagerTest, TakeOwnershipAlreadyOwned) {
|
| - EXPECT_CALL(*mock_, GetOwnerKeyFilePath())
|
| - .WillRepeatedly(Return(tmpfile_));
|
| -
|
| - scoped_refptr<OwnerManager> manager(new OwnerManager);
|
| - EXPECT_FALSE(manager->StartTakeOwnershipAttempt());
|
| -}
|
| -
|
| TEST_F(OwnerManagerTest, KeyGenerationFail) {
|
| StartUnowned();
|
| + MockKeyLoadObserver loader;
|
| + loader.ExpectKeyFetchSuccess(false);
|
| + scoped_refptr<OwnerManager> manager(new OwnerManager);
|
|
|
| EXPECT_CALL(*mock_, GenerateKeyPair())
|
| .WillOnce(Return(reinterpret_cast<RSAPrivateKey*>(NULL)))
|
| @@ -256,17 +129,23 @@ TEST_F(OwnerManagerTest, KeyGenerationFail) {
|
| EXPECT_CALL(*mock_, GetOwnerKeyFilePath())
|
| .WillRepeatedly(Return(tmpfile_));
|
|
|
| - scoped_refptr<OwnerManager> manager(new OwnerManager);
|
| - EXPECT_TRUE(manager->StartTakeOwnershipAttempt());
|
| + ChromeThread::PostTask(
|
| + ChromeThread::FILE, FROM_HERE,
|
| + NewRunnableMethod(manager.get(),
|
| + &OwnerManager::GenerateKeysAndExportPublic));
|
|
|
| message_loop_.Run();
|
| }
|
|
|
| TEST_F(OwnerManagerTest, KeyExportFail) {
|
| StartUnowned();
|
| + MockKeyLoadObserver loader;
|
| + loader.ExpectKeyFetchSuccess(false);
|
| + scoped_refptr<OwnerManager> manager(new OwnerManager);
|
|
|
| - EXPECT_CALL(*mock_, ExportPublicKeyViaDbus(fake_private_key_.get()))
|
| - .WillOnce(Invoke(Fail))
|
| + EXPECT_CALL(*mock_, ExportPublicKeyViaDbus(fake_private_key_.get(),
|
| + manager.get()))
|
| + .WillOnce(Invoke(MockKeyUtils::ExportPublicKeyViaDbusFail))
|
| .RetiresOnSaturation();
|
| EXPECT_CALL(*mock_, GenerateKeyPair())
|
| .WillOnce(Return(fake_private_key_.release()))
|
| @@ -274,18 +153,23 @@ TEST_F(OwnerManagerTest, KeyExportFail) {
|
| EXPECT_CALL(*mock_, GetOwnerKeyFilePath())
|
| .WillRepeatedly(Return(tmpfile_));
|
|
|
| - scoped_refptr<OwnerManager> manager(new OwnerManager);
|
| - EXPECT_TRUE(manager->StartTakeOwnershipAttempt());
|
| + ChromeThread::PostTask(
|
| + ChromeThread::FILE, FROM_HERE,
|
| + NewRunnableMethod(manager.get(),
|
| + &OwnerManager::GenerateKeysAndExportPublic));
|
|
|
| message_loop_.Run();
|
| }
|
|
|
| TEST_F(OwnerManagerTest, TakeOwnership) {
|
| StartUnowned();
|
| - ExpectKeyFetchSuccess(true);
|
| + MockKeyLoadObserver loader;
|
| + loader.ExpectKeyFetchSuccess(true);
|
| + scoped_refptr<OwnerManager> manager(new OwnerManager);
|
|
|
| - EXPECT_CALL(*mock_, ExportPublicKeyViaDbus(fake_private_key_.get()))
|
| - .WillOnce(Invoke(Win))
|
| + EXPECT_CALL(*mock_, ExportPublicKeyViaDbus(fake_private_key_.get(),
|
| + manager.get()))
|
| + .WillOnce(Invoke(MockKeyUtils::ExportPublicKeyViaDbusWin))
|
| .RetiresOnSaturation();
|
| EXPECT_CALL(*mock_, GenerateKeyPair())
|
| .WillOnce(Return(fake_private_key_.release()))
|
| @@ -293,57 +177,64 @@ TEST_F(OwnerManagerTest, TakeOwnership) {
|
| EXPECT_CALL(*mock_, GetOwnerKeyFilePath())
|
| .WillRepeatedly(Return(tmpfile_));
|
|
|
| - scoped_refptr<OwnerManager> manager(new OwnerManager);
|
| - EXPECT_TRUE(manager->StartTakeOwnershipAttempt());
|
| + ChromeThread::PostTask(
|
| + ChromeThread::FILE, FROM_HERE,
|
| + NewRunnableMethod(manager.get(),
|
| + &OwnerManager::GenerateKeysAndExportPublic));
|
|
|
| message_loop_.Run();
|
| }
|
|
|
| -TEST_F(OwnerManagerTest, NotYetOwnedVerify) {
|
| +TEST_F(OwnerManagerTest, GetKeyFailDuringVerify) {
|
| StartUnowned();
|
| -
|
| - // Since this shouldn't happen, don't want it to end the test if it does.
|
| - SetQuitOnKeyFetch(false);
|
| -
|
| - EXPECT_CALL(*mock_, GetOwnerKeyFilePath())
|
| - .WillRepeatedly(Return(tmpfile_));
|
| -
|
| + MockKeyLoadObserver loader;
|
| + loader.ExpectKeyFetchSuccess(false);
|
| scoped_refptr<OwnerManager> manager(new OwnerManager);
|
| - KeyUser delegate(OwnerManager::KEY_UNAVAILABLE);
|
| - EXPECT_FALSE(manager->StartVerifyAttempt("", "", &delegate));
|
| -}
|
|
|
| -TEST_F(OwnerManagerTest, AlreadyHaveKeysVerify) {
|
| EXPECT_CALL(*mock_, GetOwnerKeyFilePath())
|
| .WillRepeatedly(Return(tmpfile_));
|
| + EXPECT_CALL(*mock_, ImportPublicKey(tmpfile_, _))
|
| + .WillOnce(Return(false))
|
| + .RetiresOnSaturation();
|
|
|
| - scoped_refptr<OwnerManager> manager(new OwnerManager);
|
| - InjectKeys(manager.get());
|
| - KeyUser delegate(OwnerManager::SUCCESS);
|
| - EXPECT_TRUE(manager->StartVerifyAttempt("", "", &delegate));
|
| + MockKeyUser delegate(OwnerManager::KEY_UNAVAILABLE);
|
|
|
| + ChromeThread::PostTask(
|
| + ChromeThread::FILE, FROM_HERE,
|
| + NewRunnableMethod(manager.get(),
|
| + &OwnerManager::Verify,
|
| + ChromeThread::UI,
|
| + std::string(),
|
| + std::string(),
|
| + &delegate));
|
| message_loop_.Run();
|
| }
|
|
|
| -TEST_F(OwnerManagerTest, GetKeyFailDuringVerify) {
|
| - ExpectKeyFetchSuccess(false);
|
| +TEST_F(OwnerManagerTest, AlreadyHaveKeysVerify) {
|
| + scoped_refptr<OwnerManager> manager(new OwnerManager);
|
|
|
| EXPECT_CALL(*mock_, GetOwnerKeyFilePath())
|
| .WillRepeatedly(Return(tmpfile_));
|
| - EXPECT_CALL(*mock_, ImportPublicKey(tmpfile_, _))
|
| - .WillOnce(Return(false))
|
| - .RetiresOnSaturation();
|
| -
|
| - scoped_refptr<OwnerManager> manager(new OwnerManager);
|
| - KeyUser delegate(OwnerManager::KEY_UNAVAILABLE);
|
| - EXPECT_TRUE(manager->StartVerifyAttempt("", "", &delegate));
|
|
|
| + InjectKeys(manager.get());
|
| + MockKeyUser delegate(OwnerManager::SUCCESS);
|
| +
|
| + ChromeThread::PostTask(
|
| + ChromeThread::FILE, FROM_HERE,
|
| + NewRunnableMethod(manager.get(),
|
| + &OwnerManager::Verify,
|
| + ChromeThread::UI,
|
| + std::string(),
|
| + std::string(),
|
| + &delegate));
|
| message_loop_.Run();
|
| }
|
|
|
| TEST_F(OwnerManagerTest, GetKeyAndVerify) {
|
| - ExpectKeyFetchSuccess(true);
|
| - SetQuitOnKeyFetch(false);
|
| + MockKeyLoadObserver loader;
|
| + loader.ExpectKeyFetchSuccess(true);
|
| + loader.SetQuitOnKeyFetch(false);
|
| + scoped_refptr<OwnerManager> manager(new OwnerManager);
|
|
|
| EXPECT_CALL(*mock_, GetOwnerKeyFilePath())
|
| .WillRepeatedly(Return(tmpfile_));
|
| @@ -352,10 +243,15 @@ TEST_F(OwnerManagerTest, GetKeyAndVerify) {
|
| Return(true)))
|
| .RetiresOnSaturation();
|
|
|
| - scoped_refptr<OwnerManager> manager(new OwnerManager);
|
| - KeyUser delegate(OwnerManager::SUCCESS);
|
| - EXPECT_TRUE(manager->StartVerifyAttempt("", "", &delegate));
|
| -
|
| + MockKeyUser delegate(OwnerManager::SUCCESS);
|
| + ChromeThread::PostTask(
|
| + ChromeThread::FILE, FROM_HERE,
|
| + NewRunnableMethod(manager.get(),
|
| + &OwnerManager::Verify,
|
| + ChromeThread::UI,
|
| + std::string(),
|
| + std::string(),
|
| + &delegate));
|
| message_loop_.Run();
|
| }
|
|
|
|
|