| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 "base/bind.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "chrome/browser/chromeos/cros/cros_library.h" | |
| 10 #include "chrome/browser/chromeos/cros_settings_names.h" | |
| 11 #include "chrome/browser/chromeos/login/mock_owner_key_utils.h" | |
| 12 #include "chrome/browser/chromeos/login/mock_ownership_service.h" | |
| 13 #include "chrome/browser/chromeos/login/owner_manager.h" | |
| 14 #include "chrome/browser/chromeos/login/signed_settings.h" | |
| 15 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" | |
| 16 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
| 17 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 18 #include "chromeos/dbus/mock_dbus_thread_manager.h" | |
| 19 #include "chromeos/dbus/mock_session_manager_client.h" | |
| 20 #include "content/public/test/test_browser_thread.h" | |
| 21 #include "testing/gmock/include/gmock/gmock.h" | |
| 22 #include "testing/gtest/include/gtest/gtest.h" | |
| 23 | |
| 24 using ::testing::_; | |
| 25 using ::testing::A; | |
| 26 using ::testing::InSequence; | |
| 27 using ::testing::Invoke; | |
| 28 using ::testing::WithArg; | |
| 29 | |
| 30 namespace em = enterprise_management; | |
| 31 namespace chromeos { | |
| 32 | |
| 33 ACTION_P(Retrieve, policy_blob) { arg0.Run(policy_blob); } | |
| 34 ACTION_P(Store, success) { arg1.Run(success); } | |
| 35 | |
| 36 class SignedSettingsHelperTest : public testing::Test, | |
| 37 public SignedSettingsHelper::TestDelegate { | |
| 38 public: | |
| 39 SignedSettingsHelperTest() | |
| 40 : message_loop_(MessageLoop::TYPE_UI), | |
| 41 ui_thread_(content::BrowserThread::UI, &message_loop_), | |
| 42 file_thread_(content::BrowserThread::FILE, &message_loop_), | |
| 43 pending_ops_(0), | |
| 44 mock_dbus_thread_manager_(new MockDBusThreadManager) { | |
| 45 } | |
| 46 | |
| 47 virtual void SetUp() { | |
| 48 SignedSettingsHelper::Get()->set_test_delegate(this); | |
| 49 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager_); | |
| 50 | |
| 51 fake_policy_data_ = BuildPolicyData(); | |
| 52 std::string data_serialized = fake_policy_data_.SerializeAsString(); | |
| 53 std::string serialized_policy_; | |
| 54 fake_policy_ = BuildProto(data_serialized, | |
| 55 std::string("false"), | |
| 56 &serialized_policy_); | |
| 57 | |
| 58 MockSessionManagerClient* client = | |
| 59 mock_dbus_thread_manager_->mock_session_manager_client(); | |
| 60 // Make sure the mocked out class calls back to notify success on store and | |
| 61 // retrieve ops. | |
| 62 EXPECT_CALL(*client, StoreDevicePolicy(_, _)) | |
| 63 .WillRepeatedly(Store(true)); | |
| 64 EXPECT_CALL(*client, RetrieveDevicePolicy(_)) | |
| 65 .WillRepeatedly(Retrieve(serialized_policy_)); | |
| 66 | |
| 67 EXPECT_CALL(m_, StartSigningAttempt(_, A<OwnerManager::Delegate*>())) | |
| 68 .WillRepeatedly(WithArg<1>( | |
| 69 Invoke(&SignedSettingsHelperTest::OnKeyOpComplete))); | |
| 70 EXPECT_CALL(m_, StartVerifyAttempt(_, _, A<OwnerManager::Delegate*>())) | |
| 71 .WillRepeatedly(WithArg<2>( | |
| 72 Invoke(&SignedSettingsHelperTest::OnKeyOpComplete))); | |
| 73 } | |
| 74 | |
| 75 virtual void TearDown() { | |
| 76 DBusThreadManager::Shutdown(); | |
| 77 SignedSettingsHelper::Get()->set_test_delegate(NULL); | |
| 78 } | |
| 79 | |
| 80 virtual void OnOpCreated(SignedSettings* op) { | |
| 81 // Use MockOwnershipService for all SignedSettings op. | |
| 82 op->set_service(&m_); | |
| 83 } | |
| 84 | |
| 85 virtual void OnOpStarted(SignedSettings* op) { | |
| 86 } | |
| 87 | |
| 88 virtual void OnOpCompleted(SignedSettings* op) { | |
| 89 --pending_ops_; | |
| 90 } | |
| 91 | |
| 92 static void OnKeyOpComplete(OwnerManager::Delegate* op) { | |
| 93 op->OnKeyOpComplete(OwnerManager::SUCCESS, std::vector<uint8>()); | |
| 94 } | |
| 95 | |
| 96 em::PolicyData BuildPolicyData() { | |
| 97 em::PolicyData to_return; | |
| 98 em::ChromeDeviceSettingsProto pol; | |
| 99 to_return.set_policy_type(chromeos::kDevicePolicyType); | |
| 100 to_return.set_policy_value(pol.SerializeAsString()); | |
| 101 return to_return; | |
| 102 } | |
| 103 | |
| 104 em::PolicyFetchResponse BuildProto(const std::string& data, | |
| 105 const std::string& sig, | |
| 106 std::string* out_serialized) { | |
| 107 em::PolicyFetchResponse fake_policy; | |
| 108 if (!data.empty()) | |
| 109 fake_policy.set_policy_data(data); | |
| 110 if (!sig.empty()) | |
| 111 fake_policy.set_policy_data_signature(sig); | |
| 112 EXPECT_TRUE(fake_policy.SerializeToString(out_serialized)); | |
| 113 return fake_policy; | |
| 114 } | |
| 115 | |
| 116 em::PolicyData fake_policy_data_; | |
| 117 em::PolicyFetchResponse fake_policy_; | |
| 118 std::string serialized_policy_; | |
| 119 MockOwnershipService m_; | |
| 120 | |
| 121 MessageLoop message_loop_; | |
| 122 content::TestBrowserThread ui_thread_; | |
| 123 content::TestBrowserThread file_thread_; | |
| 124 | |
| 125 int pending_ops_; | |
| 126 | |
| 127 MockDBusThreadManager* mock_dbus_thread_manager_; | |
| 128 | |
| 129 ScopedStubCrosEnabler stub_cros_enabler_; | |
| 130 }; | |
| 131 | |
| 132 class SignedSettingsCallbacks { | |
| 133 public: | |
| 134 virtual ~SignedSettingsCallbacks() {} | |
| 135 // Callback of StorePolicyOp. | |
| 136 virtual void OnStorePolicyCompleted(SignedSettings::ReturnCode code) = 0; | |
| 137 // Callback of RetrievePolicyOp. | |
| 138 virtual void OnRetrievePolicyCompleted( | |
| 139 SignedSettings::ReturnCode code, | |
| 140 const em::PolicyFetchResponse& policy) = 0; | |
| 141 }; | |
| 142 | |
| 143 class MockSignedSettingsCallbacks | |
| 144 : public SignedSettingsCallbacks, | |
| 145 public base::SupportsWeakPtr<MockSignedSettingsCallbacks> { | |
| 146 public: | |
| 147 virtual ~MockSignedSettingsCallbacks() {} | |
| 148 | |
| 149 MOCK_METHOD1(OnStorePolicyCompleted, void(SignedSettings::ReturnCode)); | |
| 150 MOCK_METHOD2(OnRetrievePolicyCompleted, void(SignedSettings::ReturnCode, | |
| 151 const em::PolicyFetchResponse&)); | |
| 152 }; | |
| 153 | |
| 154 TEST_F(SignedSettingsHelperTest, SerializedOps) { | |
| 155 MockSignedSettingsCallbacks cb; | |
| 156 | |
| 157 InSequence s; | |
| 158 EXPECT_CALL(cb, OnStorePolicyCompleted(SignedSettings::SUCCESS)) | |
| 159 .Times(1); | |
| 160 EXPECT_CALL(cb, OnRetrievePolicyCompleted(SignedSettings::SUCCESS, _)) | |
| 161 .Times(1); | |
| 162 | |
| 163 | |
| 164 pending_ops_ = 2; | |
| 165 SignedSettingsHelper::Get()->StartStorePolicyOp( | |
| 166 fake_policy_, | |
| 167 base::Bind(&MockSignedSettingsCallbacks::OnStorePolicyCompleted, | |
| 168 base::Unretained(&cb))); | |
| 169 SignedSettingsHelper::Get()->StartRetrievePolicyOp( | |
| 170 base::Bind(&MockSignedSettingsCallbacks::OnRetrievePolicyCompleted, | |
| 171 base::Unretained(&cb))); | |
| 172 | |
| 173 message_loop_.RunAllPending(); | |
| 174 ASSERT_EQ(0, pending_ops_); | |
| 175 } | |
| 176 | |
| 177 TEST_F(SignedSettingsHelperTest, CanceledOps) { | |
| 178 MockSignedSettingsCallbacks cb; | |
| 179 | |
| 180 InSequence s; | |
| 181 EXPECT_CALL(cb, OnStorePolicyCompleted(SignedSettings::SUCCESS)) | |
| 182 .Times(1); | |
| 183 EXPECT_CALL(cb, OnRetrievePolicyCompleted(SignedSettings::SUCCESS, _)) | |
| 184 .Times(1); | |
| 185 | |
| 186 pending_ops_ = 3; | |
| 187 | |
| 188 { | |
| 189 // This op will be deleted and never will be executed (expect only one call | |
| 190 // to OnRetrievePolicyCompleted above). However the OpComplete callback will | |
| 191 // be still called, therefore we expect three pending ops. | |
| 192 MockSignedSettingsCallbacks cb_to_be_deleted; | |
| 193 SignedSettingsHelper::Get()->StartRetrievePolicyOp( | |
| 194 base::Bind(&MockSignedSettingsCallbacks::OnRetrievePolicyCompleted, | |
| 195 cb_to_be_deleted.AsWeakPtr())); | |
| 196 } | |
| 197 SignedSettingsHelper::Get()->StartStorePolicyOp( | |
| 198 fake_policy_, | |
| 199 base::Bind(&MockSignedSettingsCallbacks::OnStorePolicyCompleted, | |
| 200 base::Unretained(&cb))); | |
| 201 SignedSettingsHelper::Get()->StartRetrievePolicyOp( | |
| 202 base::Bind(&MockSignedSettingsCallbacks::OnRetrievePolicyCompleted, | |
| 203 base::Unretained(&cb))); | |
| 204 | |
| 205 message_loop_.RunAllPending(); | |
| 206 ASSERT_EQ(0, pending_ops_); | |
| 207 } | |
| 208 | |
| 209 } // namespace chromeos | |
| OLD | NEW |