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 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_OWNER_MANAGER_UNITTEST_H_ 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_OWNER_MANAGER_UNITTEST_H_ 7 8 #include "chrome/browser/chromeos/login/owner_manager.h" 9 10 #include <string> 11 #include <vector> 12 13 #include "base/file_path.h" 14 #include "base/file_util.h" 15 #include "base/logging.h" 16 #include "content/common/notification_observer.h" 17 #include "content/common/notification_registrar.h" 18 #include "content/common/notification_service.h" 19 #include "content/common/notification_type.h" 20 #include "testing/gmock/include/gmock/gmock.h" 21 #include "testing/gtest/include/gtest/gtest.h" 22 23 24 namespace chromeos { 25 class MockKeyLoadObserver : public NotificationObserver { 26 public: 27 MockKeyLoadObserver() 28 : success_expected_(false), 29 quit_on_observe_(true), 30 observed_(false) { 31 registrar_.Add( 32 this, 33 NotificationType::OWNER_KEY_FETCH_ATTEMPT_FAILED, 34 NotificationService::AllSources()); 35 registrar_.Add( 36 this, 37 NotificationType::OWNER_KEY_FETCH_ATTEMPT_SUCCEEDED, 38 NotificationService::AllSources()); 39 } 40 41 virtual ~MockKeyLoadObserver() { 42 EXPECT_TRUE(observed_); 43 } 44 45 // NotificationObserver implementation. 46 virtual void Observe(NotificationType type, 47 const NotificationSource& source, 48 const NotificationDetails& details) { 49 LOG(INFO) << "Observed key fetch event"; 50 if (type == NotificationType::OWNER_KEY_FETCH_ATTEMPT_SUCCEEDED) { 51 EXPECT_TRUE(success_expected_); 52 observed_ = true; 53 if (quit_on_observe_) 54 MessageLoop::current()->Quit(); 55 } else if (type == NotificationType::OWNER_KEY_FETCH_ATTEMPT_FAILED) { 56 EXPECT_FALSE(success_expected_); 57 observed_ = true; 58 if (quit_on_observe_) 59 MessageLoop::current()->Quit(); 60 } 61 } 62 63 void ExpectKeyFetchSuccess(bool should_succeed) { 64 success_expected_ = should_succeed; 65 } 66 67 void SetQuitOnKeyFetch(bool should_quit) { quit_on_observe_ = should_quit; } 68 69 private: 70 NotificationRegistrar registrar_; 71 bool success_expected_; 72 bool quit_on_observe_; 73 bool observed_; 74 DISALLOW_COPY_AND_ASSIGN(MockKeyLoadObserver); 75 }; 76 77 class MockKeyUser : public OwnerManager::Delegate { 78 public: 79 explicit MockKeyUser(const OwnerManager::KeyOpCode expected) 80 : expected_(expected), 81 quit_on_callback_(true) { 82 } 83 MockKeyUser(const OwnerManager::KeyOpCode expected, bool quit_on_callback) 84 : expected_(expected), 85 quit_on_callback_(quit_on_callback) { 86 } 87 88 virtual ~MockKeyUser() {} 89 90 void OnKeyOpComplete(const OwnerManager::KeyOpCode return_code, 91 const std::vector<uint8>& payload) { 92 EXPECT_EQ(expected_, return_code); 93 if (quit_on_callback_) 94 MessageLoop::current()->Quit(); 95 } 96 97 const OwnerManager::KeyOpCode expected_; 98 const bool quit_on_callback_; 99 private: 100 DISALLOW_COPY_AND_ASSIGN(MockKeyUser); 101 }; 102 103 class MockKeyUpdateUser : public OwnerManager::KeyUpdateDelegate { 104 public: 105 MockKeyUpdateUser() {} 106 virtual ~MockKeyUpdateUser() {} 107 108 virtual void OnKeyUpdated() { 109 MessageLoop::current()->Quit(); 110 } 111 112 private: 113 DISALLOW_COPY_AND_ASSIGN(MockKeyUpdateUser); 114 }; 115 116 117 class MockSigner : public OwnerManager::Delegate { 118 public: 119 MockSigner(const OwnerManager::KeyOpCode expected, 120 const std::vector<uint8>& sig) 121 : expected_code_(expected), 122 expected_sig_(sig) { 123 } 124 125 virtual ~MockSigner() {} 126 127 void OnKeyOpComplete(const OwnerManager::KeyOpCode return_code, 128 const std::vector<uint8>& payload) { 129 EXPECT_EQ(expected_code_, return_code); 130 for (uint32 i = 0; i < payload.size(); ++i) 131 EXPECT_EQ(expected_sig_[i], payload[i]); 132 MessageLoop::current()->Quit(); 133 } 134 135 const OwnerManager::KeyOpCode expected_code_; 136 const std::vector<uint8> expected_sig_; 137 138 private: 139 DISALLOW_COPY_AND_ASSIGN(MockSigner); 140 }; 141 142 } // namespace chromeos 143 144 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_OWNER_MANAGER_UNITTEST_H_ 145