| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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/owner_manager.h" | |
| 6 #include "chrome/browser/chromeos/login/owner_manager_unittest.h" | |
| 7 | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/file_path.h" | |
| 12 #include "base/file_util.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/message_loop.h" | |
| 15 #include "base/scoped_temp_dir.h" | |
| 16 #include "chrome/browser/chromeos/login/mock_owner_key_utils.h" | |
| 17 #include "chrome/common/chrome_notification_types.h" | |
| 18 #include "content/public/browser/notification_service.h" | |
| 19 #include "content/public/test/test_browser_thread.h" | |
| 20 #include "crypto/nss_util.h" | |
| 21 #include "crypto/rsa_private_key.h" | |
| 22 #include "testing/gmock/include/gmock/gmock.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | |
| 24 | |
| 25 using content::BrowserThread; | |
| 26 using ::crypto::RSAPrivateKey; | |
| 27 using ::testing::DoAll; | |
| 28 using ::testing::Eq; | |
| 29 using ::testing::Invoke; | |
| 30 using ::testing::Return; | |
| 31 using ::testing::SetArgumentPointee; | |
| 32 using ::testing::_; | |
| 33 | |
| 34 namespace chromeos { | |
| 35 | |
| 36 //////////////////////////////////////////////////////////////////////////////// | |
| 37 // MockKeyLoadObserver | |
| 38 MockKeyLoadObserver::MockKeyLoadObserver(base::WaitableEvent* e) | |
| 39 : success_expected_(false), | |
| 40 event_(e), | |
| 41 observed_(false) { | |
| 42 registrar_.Add( | |
| 43 this, | |
| 44 chrome::NOTIFICATION_OWNER_KEY_FETCH_ATTEMPT_FAILED, | |
| 45 content::NotificationService::AllSources()); | |
| 46 registrar_.Add( | |
| 47 this, | |
| 48 chrome::NOTIFICATION_OWNER_KEY_FETCH_ATTEMPT_SUCCEEDED, | |
| 49 content::NotificationService::AllSources()); | |
| 50 } | |
| 51 | |
| 52 MockKeyLoadObserver::~MockKeyLoadObserver() { | |
| 53 DCHECK(observed_); | |
| 54 } | |
| 55 | |
| 56 void MockKeyLoadObserver::Observe(int type, | |
| 57 const content::NotificationSource& source, | |
| 58 const content::NotificationDetails& details) { | |
| 59 LOG(INFO) << "Observed key fetch event"; | |
| 60 if (type == chrome::NOTIFICATION_OWNER_KEY_FETCH_ATTEMPT_SUCCEEDED) { | |
| 61 DCHECK(success_expected_); | |
| 62 observed_ = true; | |
| 63 if (event_) | |
| 64 event_->Signal(); | |
| 65 } else if (type == chrome::NOTIFICATION_OWNER_KEY_FETCH_ATTEMPT_FAILED) { | |
| 66 DCHECK(!success_expected_); | |
| 67 observed_ = true; | |
| 68 if (event_) | |
| 69 event_->Signal(); | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 void MockKeyLoadObserver::ExpectKeyFetchSuccess(bool should_succeed) { | |
| 74 success_expected_ = should_succeed; | |
| 75 } | |
| 76 | |
| 77 //////////////////////////////////////////////////////////////////////////////// | |
| 78 // MockKeyUser | |
| 79 MockKeyUser::MockKeyUser(const OwnerManager::KeyOpCode expected, | |
| 80 base::WaitableEvent* e) | |
| 81 : expected_(expected), | |
| 82 event_(e) { | |
| 83 } | |
| 84 | |
| 85 void MockKeyUser::OnKeyOpComplete(const OwnerManager::KeyOpCode return_code, | |
| 86 const std::vector<uint8>& payload) { | |
| 87 DCHECK_EQ(expected_, return_code); | |
| 88 if (event_) | |
| 89 event_->Signal(); | |
| 90 } | |
| 91 | |
| 92 //////////////////////////////////////////////////////////////////////////////// | |
| 93 // MockKeyUpdateUser | |
| 94 | |
| 95 void MockKeyUpdateUser::OnKeyUpdated() { | |
| 96 if (event_) | |
| 97 event_->Signal(); | |
| 98 } | |
| 99 | |
| 100 //////////////////////////////////////////////////////////////////////////////// | |
| 101 // MockSigner | |
| 102 | |
| 103 MockSigner::MockSigner(const OwnerManager::KeyOpCode expected, | |
| 104 const std::vector<uint8>& sig, | |
| 105 base::WaitableEvent* e) | |
| 106 : expected_code_(expected), | |
| 107 expected_sig_(sig), | |
| 108 event_(e) { | |
| 109 } | |
| 110 | |
| 111 MockSigner::~MockSigner() {} | |
| 112 | |
| 113 void MockSigner::OnKeyOpComplete(const OwnerManager::KeyOpCode return_code, | |
| 114 const std::vector<uint8>& payload) { | |
| 115 DCHECK_EQ(expected_code_, return_code); | |
| 116 for (uint32 i = 0; i < payload.size(); ++i) | |
| 117 DCHECK_EQ(expected_sig_[i], payload[i]); | |
| 118 if (event_) | |
| 119 event_->Signal(); | |
| 120 } | |
| 121 | |
| 122 //////////////////////////////////////////////////////////////////////////////// | |
| 123 // OwnerManagerTest | |
| 124 | |
| 125 class OwnerManagerTest : public testing::Test { | |
| 126 public: | |
| 127 OwnerManagerTest() | |
| 128 : message_loop_(MessageLoop::TYPE_UI), | |
| 129 ui_thread_(BrowserThread::UI, &message_loop_), | |
| 130 file_thread_(BrowserThread::FILE), | |
| 131 mock_(new MockKeyUtils), | |
| 132 injector_(mock_) /* injector_ takes ownership of mock_ */ { | |
| 133 } | |
| 134 virtual ~OwnerManagerTest() {} | |
| 135 | |
| 136 virtual void SetUp() { | |
| 137 crypto::OpenPersistentNSSDB(); // TODO(cmasone): use test DB instead | |
| 138 fake_private_key_.reset(RSAPrivateKey::Create(256)); | |
| 139 ASSERT_TRUE(fake_private_key_->ExportPublicKey(&fake_public_key_)); | |
| 140 | |
| 141 // Mimic ownership. | |
| 142 ASSERT_TRUE(tmpdir_.CreateUniqueTempDir()); | |
| 143 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(tmpdir_.path(), &tmpfile_)); | |
| 144 | |
| 145 file_thread_.Start(); | |
| 146 OwnerKeyUtils::set_factory(&injector_); | |
| 147 } | |
| 148 | |
| 149 virtual void TearDown() { | |
| 150 OwnerKeyUtils::set_factory(NULL); | |
| 151 } | |
| 152 | |
| 153 void StartUnowned() { | |
| 154 file_util::Delete(tmpfile_, false); | |
| 155 } | |
| 156 | |
| 157 void InjectKeys(OwnerManager* manager) { | |
| 158 manager->public_key_ = fake_public_key_; | |
| 159 manager->private_key_.reset(fake_private_key_.release()); | |
| 160 } | |
| 161 | |
| 162 ScopedTempDir tmpdir_; | |
| 163 FilePath tmpfile_; | |
| 164 | |
| 165 MessageLoop message_loop_; | |
| 166 content::TestBrowserThread ui_thread_; | |
| 167 content::TestBrowserThread file_thread_; | |
| 168 | |
| 169 std::vector<uint8> fake_public_key_; | |
| 170 scoped_ptr<RSAPrivateKey> fake_private_key_; | |
| 171 | |
| 172 MockKeyUtils* mock_; | |
| 173 MockInjector injector_; | |
| 174 }; | |
| 175 | |
| 176 TEST_F(OwnerManagerTest, UpdateOwnerKey) { | |
| 177 scoped_refptr<OwnerManager> manager(new OwnerManager); | |
| 178 | |
| 179 base::WaitableEvent event(true, false); | |
| 180 MockKeyUpdateUser delegate(&event); | |
| 181 BrowserThread::PostTask( | |
| 182 BrowserThread::FILE, FROM_HERE, | |
| 183 base::Bind(&OwnerManager::UpdateOwnerKey, manager.get(), | |
| 184 BrowserThread::UI, std::vector<uint8>(), &delegate)); | |
| 185 while (!event.IsSignaled()) | |
| 186 message_loop_.RunAllPending(); | |
| 187 } | |
| 188 | |
| 189 TEST_F(OwnerManagerTest, LoadOwnerKeyFail) { | |
| 190 StartUnowned(); | |
| 191 base::WaitableEvent event(true, false); | |
| 192 MockKeyLoadObserver loader(&event); | |
| 193 scoped_refptr<OwnerManager> manager(new OwnerManager); | |
| 194 | |
| 195 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) | |
| 196 .WillRepeatedly(Return(tmpfile_)); | |
| 197 EXPECT_CALL(*mock_, ImportPublicKey(tmpfile_, _)) | |
| 198 .WillOnce(Return(false)) | |
| 199 .RetiresOnSaturation(); | |
| 200 | |
| 201 BrowserThread::PostTask( | |
| 202 BrowserThread::FILE, FROM_HERE, | |
| 203 base::Bind(&OwnerManager::LoadOwnerKey, manager.get())); | |
| 204 while (!event.IsSignaled()) | |
| 205 message_loop_.RunAllPending(); | |
| 206 } | |
| 207 | |
| 208 TEST_F(OwnerManagerTest, AlreadyLoadedOwnerKey) { | |
| 209 base::WaitableEvent event(true, false); | |
| 210 MockKeyLoadObserver loader(&event); | |
| 211 loader.ExpectKeyFetchSuccess(true); | |
| 212 scoped_refptr<OwnerManager> manager(new OwnerManager); | |
| 213 | |
| 214 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) | |
| 215 .WillRepeatedly(Return(tmpfile_)); | |
| 216 | |
| 217 InjectKeys(manager.get()); | |
| 218 | |
| 219 BrowserThread::PostTask( | |
| 220 BrowserThread::FILE, FROM_HERE, | |
| 221 base::Bind(&OwnerManager::LoadOwnerKey, manager.get())); | |
| 222 while (!event.IsSignaled()) | |
| 223 message_loop_.RunAllPending(); | |
| 224 } | |
| 225 | |
| 226 TEST_F(OwnerManagerTest, LoadOwnerKey) { | |
| 227 base::WaitableEvent event(true, false); | |
| 228 MockKeyLoadObserver loader(&event); | |
| 229 loader.ExpectKeyFetchSuccess(true); | |
| 230 scoped_refptr<OwnerManager> manager(new OwnerManager); | |
| 231 | |
| 232 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) | |
| 233 .WillRepeatedly(Return(tmpfile_)); | |
| 234 EXPECT_CALL(*mock_, ImportPublicKey(tmpfile_, _)) | |
| 235 .WillOnce(DoAll(SetArgumentPointee<1>(fake_public_key_), | |
| 236 Return(true))) | |
| 237 .RetiresOnSaturation(); | |
| 238 | |
| 239 BrowserThread::PostTask( | |
| 240 BrowserThread::FILE, FROM_HERE, | |
| 241 base::Bind(&OwnerManager::LoadOwnerKey, manager.get())); | |
| 242 while (!event.IsSignaled()) | |
| 243 message_loop_.RunAllPending(); | |
| 244 } | |
| 245 | |
| 246 TEST_F(OwnerManagerTest, GetKeyFailDuringVerify) { | |
| 247 StartUnowned(); | |
| 248 MockKeyLoadObserver loader(NULL); | |
| 249 loader.ExpectKeyFetchSuccess(false); | |
| 250 scoped_refptr<OwnerManager> manager(new OwnerManager); | |
| 251 | |
| 252 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) | |
| 253 .WillRepeatedly(Return(tmpfile_)); | |
| 254 EXPECT_CALL(*mock_, ImportPublicKey(tmpfile_, _)) | |
| 255 .WillOnce(Return(false)) | |
| 256 .RetiresOnSaturation(); | |
| 257 | |
| 258 base::WaitableEvent event(true, false); | |
| 259 MockKeyUser delegate(OwnerManager::KEY_UNAVAILABLE, &event); | |
| 260 | |
| 261 BrowserThread::PostTask( | |
| 262 BrowserThread::FILE, FROM_HERE, | |
| 263 base::Bind(&OwnerManager::Verify, manager.get(), BrowserThread::UI, | |
| 264 std::string(), std::vector<uint8>(), &delegate)); | |
| 265 while (!event.IsSignaled()) | |
| 266 message_loop_.RunAllPending(); | |
| 267 } | |
| 268 | |
| 269 TEST_F(OwnerManagerTest, AlreadyHaveKeysVerify) { | |
| 270 scoped_refptr<OwnerManager> manager(new OwnerManager); | |
| 271 | |
| 272 std::string data; | |
| 273 std::vector<uint8> sig(0, 2); | |
| 274 | |
| 275 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) | |
| 276 .WillRepeatedly(Return(tmpfile_)); | |
| 277 EXPECT_CALL(*mock_, Verify(Eq(data), Eq(sig), Eq(fake_public_key_))) | |
| 278 .WillOnce(Return(true)) | |
| 279 .RetiresOnSaturation(); | |
| 280 | |
| 281 InjectKeys(manager.get()); | |
| 282 base::WaitableEvent event(true, false); | |
| 283 MockKeyUser delegate(OwnerManager::SUCCESS, &event); | |
| 284 | |
| 285 BrowserThread::PostTask( | |
| 286 BrowserThread::FILE, FROM_HERE, | |
| 287 base::Bind(&OwnerManager::Verify, manager.get(), BrowserThread::UI, data, | |
| 288 sig, &delegate)); | |
| 289 while (!event.IsSignaled()) | |
| 290 message_loop_.RunAllPending(); | |
| 291 } | |
| 292 | |
| 293 TEST_F(OwnerManagerTest, GetKeyAndVerify) { | |
| 294 MockKeyLoadObserver loader(NULL); | |
| 295 loader.ExpectKeyFetchSuccess(true); | |
| 296 scoped_refptr<OwnerManager> manager(new OwnerManager); | |
| 297 | |
| 298 std::string data; | |
| 299 std::vector<uint8> sig(0, 2); | |
| 300 | |
| 301 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) | |
| 302 .WillRepeatedly(Return(tmpfile_)); | |
| 303 EXPECT_CALL(*mock_, ImportPublicKey(tmpfile_, _)) | |
| 304 .WillOnce(DoAll(SetArgumentPointee<1>(fake_public_key_), | |
| 305 Return(true))) | |
| 306 .RetiresOnSaturation(); | |
| 307 EXPECT_CALL(*mock_, Verify(Eq(data), Eq(sig), Eq(fake_public_key_))) | |
| 308 .WillOnce(Return(true)) | |
| 309 .RetiresOnSaturation(); | |
| 310 | |
| 311 base::WaitableEvent event(true, false); | |
| 312 MockKeyUser delegate(OwnerManager::SUCCESS, &event); | |
| 313 | |
| 314 BrowserThread::PostTask( | |
| 315 BrowserThread::FILE, FROM_HERE, | |
| 316 base::Bind(&OwnerManager::Verify, manager.get(), BrowserThread::UI, data, | |
| 317 sig, &delegate)); | |
| 318 while (!event.IsSignaled()) | |
| 319 message_loop_.RunAllPending(); | |
| 320 } | |
| 321 | |
| 322 TEST_F(OwnerManagerTest, AlreadyHaveKeysSign) { | |
| 323 scoped_refptr<OwnerManager> manager(new OwnerManager); | |
| 324 | |
| 325 std::string data; | |
| 326 std::vector<uint8> sig(0, 2); | |
| 327 | |
| 328 EXPECT_CALL(*mock_, GetOwnerKeyFilePath()) | |
| 329 .WillRepeatedly(Return(tmpfile_)); | |
| 330 EXPECT_CALL(*mock_, Sign(Eq(data), _, Eq(fake_private_key_.get()))) | |
| 331 .WillOnce(DoAll(SetArgumentPointee<1>(sig), | |
| 332 Return(true))) | |
| 333 .RetiresOnSaturation(); | |
| 334 | |
| 335 InjectKeys(manager.get()); | |
| 336 base::WaitableEvent event(true, false); | |
| 337 MockSigner delegate(OwnerManager::SUCCESS, sig, &event); | |
| 338 | |
| 339 BrowserThread::PostTask( | |
| 340 BrowserThread::FILE, FROM_HERE, | |
| 341 base::Bind(&OwnerManager::Sign, manager.get(), BrowserThread::UI, data, | |
| 342 &delegate)); | |
| 343 while (!event.IsSignaled()) | |
| 344 message_loop_.RunAllPending(); | |
| 345 } | |
| 346 | |
| 347 } // namespace chromeos | |
| OLD | NEW |