signed_settings_helper_unittest.cc revision bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293
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/signed_settings_helper.h" 6 7#include "chrome/browser/chrome_thread.h" 8#include "chrome/browser/chromeos/cros/cros_library.h" 9#include "chrome/browser/chromeos/login/mock_ownership_service.h" 10#include "chrome/browser/chromeos/login/owner_manager.h" 11#include "chrome/browser/chromeos/login/signed_settings.h" 12#include "testing/gmock/include/gmock/gmock.h" 13#include "testing/gtest/include/gtest/gtest.h" 14 15using ::testing::_; 16using ::testing::InSequence; 17 18namespace chromeos { 19 20class MockSignedSettingsHelperCallback : public SignedSettingsHelper::Callback { 21 public: 22 MOCK_METHOD2(OnCheckWhiteListCompleted, void( 23 bool success, const std::string& email)); 24 MOCK_METHOD2(OnWhitelistCompleted, void( 25 bool success, const std::string& email)); 26 MOCK_METHOD2(OnUnwhitelistCompleted, void( 27 bool success, const std::string& email)); 28 MOCK_METHOD3(OnStorePropertyCompleted, void( 29 bool success, const std::string& name, const std::string& value)); 30 MOCK_METHOD3(OnRetrievePropertyCompleted, void( 31 bool success, const std::string& name, const std::string& value)); 32}; 33 34class SignedSettingsHelperTest : public ::testing::Test, 35 public SignedSettingsHelper::TestDelegate { 36 public: 37 SignedSettingsHelperTest() 38 : fake_email_("fakey"), 39 fake_prop_("prop_name"), 40 fake_value_("stub"), 41 message_loop_(MessageLoop::TYPE_UI), 42 ui_thread_(ChromeThread::UI, &message_loop_), 43 file_thread_(ChromeThread::FILE), 44 pending_ops_(0) { 45 } 46 47 virtual void SetUp() { 48 chromeos::CrosLibrary::Get()->GetTestApi()->SetUseStubImpl(); 49 file_thread_.Start(); 50 SignedSettingsHelper::Get()->set_test_delegate(this); 51 } 52 53 virtual void TearDown() { 54 SignedSettingsHelper::Get()->set_test_delegate(NULL); 55 } 56 57 virtual void OnOpCreated(SignedSettings* op) { 58 // Use MockOwnershipService for all SignedSettings op. 59 op->set_service(&m_); 60 } 61 62 virtual void OnOpStarted(SignedSettings* op) { 63 op->OnKeyOpComplete(OwnerManager::SUCCESS, std::vector<uint8>()); 64 } 65 66 virtual void OnOpCompleted(SignedSettings* op) { 67 --pending_ops_; 68 if (!pending_ops_) 69 MessageLoop::current()->Quit(); 70 } 71 72 const std::string fake_email_; 73 const std::string fake_prop_; 74 const std::string fake_value_; 75 MockOwnershipService m_; 76 77 MessageLoop message_loop_; 78 ChromeThread ui_thread_; 79 ChromeThread file_thread_; 80 81 int pending_ops_; 82}; 83 84TEST_F(SignedSettingsHelperTest, SerializedOps) { 85 MockSignedSettingsHelperCallback cb; 86 87 InSequence s; 88 EXPECT_CALL(m_, StartVerifyAttempt(_, _, _)).Times(1); 89 EXPECT_CALL(cb, OnCheckWhiteListCompleted(true, _)) 90 .Times(1); 91 EXPECT_CALL(m_, StartSigningAttempt(_, _)).Times(1); 92 EXPECT_CALL(cb, OnWhitelistCompleted(true, _)) 93 .Times(1); 94 EXPECT_CALL(m_, StartSigningAttempt(_, _)).Times(1); 95 EXPECT_CALL(cb, OnUnwhitelistCompleted(true, _)) 96 .Times(1); 97 EXPECT_CALL(m_, StartSigningAttempt(_, _)).Times(1); 98 EXPECT_CALL(cb, OnStorePropertyCompleted(true, _, _)) 99 .Times(1); 100 EXPECT_CALL(m_, StartVerifyAttempt(_, _, _)).Times(1); 101 EXPECT_CALL(cb, OnRetrievePropertyCompleted(true, _, _)) 102 .Times(1); 103 104 pending_ops_ = 5; 105 SignedSettingsHelper::Get()->StartCheckWhitelistOp(fake_email_, &cb); 106 SignedSettingsHelper::Get()->StartWhitelistOp(fake_email_, true, &cb); 107 SignedSettingsHelper::Get()->StartWhitelistOp(fake_email_, false, &cb); 108 SignedSettingsHelper::Get()->StartStorePropertyOp(fake_prop_, fake_value_, 109 &cb); 110 SignedSettingsHelper::Get()->StartRetrieveProperty(fake_prop_, &cb); 111 112 message_loop_.Run(); 113} 114 115TEST_F(SignedSettingsHelperTest, CanceledOps) { 116 MockSignedSettingsHelperCallback cb; 117 118 InSequence s; 119 EXPECT_CALL(m_, StartVerifyAttempt(_, _, _)).Times(1); 120 EXPECT_CALL(cb, OnCheckWhiteListCompleted(true, _)) 121 .Times(1); 122 EXPECT_CALL(m_, StartSigningAttempt(_, _)).Times(1); 123 EXPECT_CALL(cb, OnWhitelistCompleted(true, _)) 124 .Times(1); 125 EXPECT_CALL(m_, StartSigningAttempt(_, _)).Times(1); 126 EXPECT_CALL(cb, OnUnwhitelistCompleted(true, _)) 127 .Times(1); 128 129 // CheckWhitelistOp for cb_to_be_canceled still gets executed but callback 130 // does not happen. 131 EXPECT_CALL(m_, StartVerifyAttempt(_, _, _)).Times(1); 132 133 EXPECT_CALL(m_, StartSigningAttempt(_, _)).Times(1); 134 EXPECT_CALL(cb, OnStorePropertyCompleted(true, _, _)) 135 .Times(1); 136 EXPECT_CALL(m_, StartVerifyAttempt(_, _, _)).Times(1); 137 EXPECT_CALL(cb, OnRetrievePropertyCompleted(true, _, _)) 138 .Times(1); 139 140 pending_ops_ = 6; 141 SignedSettingsHelper::Get()->StartCheckWhitelistOp(fake_email_, &cb); 142 SignedSettingsHelper::Get()->StartWhitelistOp(fake_email_, true, &cb); 143 SignedSettingsHelper::Get()->StartWhitelistOp(fake_email_, false, &cb); 144 145 MockSignedSettingsHelperCallback cb_to_be_canceled; 146 SignedSettingsHelper::Get()->StartCheckWhitelistOp(fake_email_, 147 &cb_to_be_canceled); 148 SignedSettingsHelper::Get()->CancelCallback(&cb_to_be_canceled); 149 150 SignedSettingsHelper::Get()->StartStorePropertyOp(fake_prop_, fake_value_, 151 &cb); 152 SignedSettingsHelper::Get()->StartRetrieveProperty(fake_prop_, &cb); 153 154 message_loop_.Run(); 155} 156 157} // namespace chromeos 158