OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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/chromeos/login/ownership_service.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/crypto/rsa_private_key.h" |
| 10 #include "base/file_path.h" |
| 11 #include "base/file_util.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/nss_util.h" |
| 14 #include "base/scoped_ptr.h" |
| 15 #include "base/scoped_temp_dir.h" |
| 16 #include "chrome/browser/chrome_thread.h" |
| 17 #include "chrome/browser/chromeos/login/mock_owner_key_utils.h" |
| 18 #include "chrome/browser/chromeos/login/owner_manager_unittest.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 using ::base::RSAPrivateKey; |
| 23 using ::testing::DoAll; |
| 24 using ::testing::Invoke; |
| 25 using ::testing::Return; |
| 26 using ::testing::SetArgumentPointee; |
| 27 using ::testing::_; |
| 28 |
| 29 |
| 30 namespace chromeos { |
| 31 |
| 32 class OwnershipServiceTest : public ::testing::Test { |
| 33 public: |
| 34 OwnershipServiceTest() |
| 35 : message_loop_(MessageLoop::TYPE_UI), |
| 36 ui_thread_(ChromeThread::UI, &message_loop_), |
| 37 file_thread_(ChromeThread::FILE), |
| 38 mock_(new MockKeyUtils), |
| 39 injector_(mock_) /* injector_ takes ownership of mock_ */ { |
| 40 } |
| 41 virtual ~OwnershipServiceTest() {} |
| 42 |
| 43 virtual void SetUp() { |
| 44 base::OpenPersistentNSSDB(); // TODO(cmasone): use test DB instead |
| 45 fake_private_key_.reset(RSAPrivateKey::Create(256)); |
| 46 ASSERT_TRUE(fake_private_key_->ExportPublicKey(&fake_public_key_)); |
| 47 |
| 48 // Mimic ownership. |
| 49 ASSERT_TRUE(tmpdir_.CreateUniqueTempDir()); |
| 50 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(tmpdir_.path(), &tmpfile_)); |
| 51 |
| 52 file_thread_.Start(); |
| 53 OwnerKeyUtils::set_factory(&injector_); |
| 54 service_.reset(new OwnershipService); // must happen AFTER set_factory(). |
| 55 |
| 56 } |
| 57 |
| 58 virtual void TearDown() { |
| 59 OwnerKeyUtils::set_factory(NULL); |
| 60 service_.reset(NULL); |
| 61 } |
| 62 |
| 63 void StartUnowned() { |
| 64 file_util::Delete(tmpfile_, false); |
| 65 } |
| 66 |
| 67 ScopedTempDir tmpdir_; |
| 68 FilePath tmpfile_; |
| 69 |
| 70 MessageLoop message_loop_; |
| 71 ChromeThread ui_thread_; |
| 72 ChromeThread file_thread_; |
| 73 |
| 74 std::vector<uint8> fake_public_key_; |
| 75 scoped_ptr<RSAPrivateKey> fake_private_key_; |
| 76 |
| 77 MockKeyUtils* mock_; |
| 78 MockInjector injector_; |
| 79 scoped_ptr<OwnershipService> service_; |
| 80 }; |
| 81 |
| 82 TEST_F(OwnershipServiceTest, IsOwned) { |
| 83 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) |
| 84 .WillRepeatedly(Return(tmpfile_)); |
| 85 EXPECT_TRUE(service_->IsAlreadyOwned()); |
| 86 } |
| 87 |
| 88 TEST_F(OwnershipServiceTest, IsUnowned) { |
| 89 StartUnowned(); |
| 90 |
| 91 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) |
| 92 .WillRepeatedly(Return(tmpfile_)); |
| 93 EXPECT_FALSE(service_->IsAlreadyOwned()); |
| 94 } |
| 95 |
| 96 TEST_F(OwnershipServiceTest, LoadOwnerKeyUnowned) { |
| 97 StartUnowned(); |
| 98 |
| 99 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) |
| 100 .WillRepeatedly(Return(tmpfile_)); |
| 101 EXPECT_FALSE(service_->StartLoadOwnerKeyAttempt()); |
| 102 } |
| 103 |
| 104 TEST_F(OwnershipServiceTest, LoadOwnerKeyFail) { |
| 105 MockKeyLoadObserver loader; |
| 106 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) |
| 107 .WillRepeatedly(Return(tmpfile_)); |
| 108 EXPECT_CALL(*mock_, ImportPublicKey(tmpfile_, _)) |
| 109 .WillOnce(Return(false)) |
| 110 .RetiresOnSaturation(); |
| 111 |
| 112 EXPECT_TRUE(service_->StartLoadOwnerKeyAttempt()); |
| 113 |
| 114 // Run remaining events, until ExportPublicKeyViaDbus(). |
| 115 message_loop_.Run(); |
| 116 } |
| 117 |
| 118 TEST_F(OwnershipServiceTest, LoadOwnerKey) { |
| 119 MockKeyLoadObserver loader; |
| 120 loader.ExpectKeyFetchSuccess(true); |
| 121 |
| 122 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) |
| 123 .WillRepeatedly(Return(tmpfile_)); |
| 124 EXPECT_CALL(*mock_, ImportPublicKey(tmpfile_, _)) |
| 125 .WillOnce(DoAll(SetArgumentPointee<1>(fake_public_key_), |
| 126 Return(true))) |
| 127 .RetiresOnSaturation(); |
| 128 EXPECT_TRUE(service_->StartLoadOwnerKeyAttempt()); |
| 129 |
| 130 message_loop_.Run(); |
| 131 } |
| 132 |
| 133 TEST_F(OwnershipServiceTest, TakeOwnershipAlreadyOwned) { |
| 134 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) |
| 135 .WillRepeatedly(Return(tmpfile_)); |
| 136 EXPECT_FALSE(service_->StartTakeOwnershipAttempt()); |
| 137 } |
| 138 |
| 139 TEST_F(OwnershipServiceTest, AttemptKeyGeneration) { |
| 140 // We really only care that we initiate key generation here; |
| 141 // actual key-generation paths are tested in owner_manager_unittest.cc |
| 142 StartUnowned(); |
| 143 MockKeyLoadObserver loader; |
| 144 loader.ExpectKeyFetchSuccess(false); |
| 145 |
| 146 EXPECT_CALL(*mock_, GenerateKeyPair()) |
| 147 .WillOnce(Return(reinterpret_cast<RSAPrivateKey*>(NULL))) |
| 148 .RetiresOnSaturation(); |
| 149 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) |
| 150 .WillRepeatedly(Return(tmpfile_)); |
| 151 |
| 152 EXPECT_TRUE(service_->StartTakeOwnershipAttempt()); |
| 153 |
| 154 message_loop_.Run(); |
| 155 } |
| 156 |
| 157 TEST_F(OwnershipServiceTest, NotYetOwnedVerify) { |
| 158 StartUnowned(); |
| 159 |
| 160 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) |
| 161 .WillRepeatedly(Return(tmpfile_)); |
| 162 MockKeyUser delegate(OwnerManager::KEY_UNAVAILABLE); |
| 163 EXPECT_FALSE(service_->StartVerifyAttempt("", "", &delegate)); |
| 164 } |
| 165 |
| 166 TEST_F(OwnershipServiceTest, GetKeyFailDuringVerify) { |
| 167 MockKeyLoadObserver loader; |
| 168 loader.ExpectKeyFetchSuccess(false); |
| 169 |
| 170 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) |
| 171 .WillRepeatedly(Return(tmpfile_)); |
| 172 EXPECT_CALL(*mock_, ImportPublicKey(tmpfile_, _)) |
| 173 .WillOnce(Return(false)) |
| 174 .RetiresOnSaturation(); |
| 175 |
| 176 MockKeyUser delegate(OwnerManager::KEY_UNAVAILABLE); |
| 177 EXPECT_TRUE(service_->StartVerifyAttempt("", "", &delegate)); |
| 178 |
| 179 message_loop_.Run(); |
| 180 } |
| 181 |
| 182 TEST_F(OwnershipServiceTest, GetKeyAndVerify) { |
| 183 MockKeyLoadObserver loader; |
| 184 loader.ExpectKeyFetchSuccess(true); |
| 185 loader.SetQuitOnKeyFetch(false); |
| 186 |
| 187 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) |
| 188 .WillRepeatedly(Return(tmpfile_)); |
| 189 EXPECT_CALL(*mock_, ImportPublicKey(tmpfile_, _)) |
| 190 .WillOnce(DoAll(SetArgumentPointee<1>(fake_public_key_), |
| 191 Return(true))) |
| 192 .RetiresOnSaturation(); |
| 193 |
| 194 MockKeyUser delegate(OwnerManager::SUCCESS); |
| 195 EXPECT_TRUE(service_->StartVerifyAttempt("", "", &delegate)); |
| 196 |
| 197 message_loop_.Run(); |
| 198 } |
| 199 |
| 200 } // namespace chromeos |
OLD | NEW |