Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/login/signed_settings_helper.h" | 5 #include "chrome/browser/chromeos/login/signed_settings_helper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | |
| 7 #include "chrome/browser/chromeos/cros/cros_library.h" | 8 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 8 #include "chrome/browser/chromeos/cros_settings_names.h" | 9 #include "chrome/browser/chromeos/cros_settings_names.h" |
| 9 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" | 10 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" |
| 11 #include "chrome/browser/chromeos/dbus/mock_dbus_thread_manager.h" | |
| 12 #include "chrome/browser/chromeos/dbus/mock_session_manager_client.h" | |
| 13 #include "chrome/browser/chromeos/login/mock_owner_key_utils.h" | |
| 10 #include "chrome/browser/chromeos/login/mock_ownership_service.h" | 14 #include "chrome/browser/chromeos/login/mock_ownership_service.h" |
| 11 #include "chrome/browser/chromeos/login/owner_manager.h" | 15 #include "chrome/browser/chromeos/login/owner_manager.h" |
| 12 #include "chrome/browser/chromeos/login/signed_settings.h" | 16 #include "chrome/browser/chromeos/login/signed_settings.h" |
| 13 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" | 17 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" |
| 14 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 18 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 15 #include "content/test/test_browser_thread.h" | 19 #include "content/test/test_browser_thread.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 22 |
| 19 using content::BrowserThread; | |
| 20 using ::testing::_; | 23 using ::testing::_; |
| 21 using ::testing::A; | 24 using ::testing::A; |
| 22 using ::testing::AtLeast; | |
| 23 using ::testing::InSequence; | 25 using ::testing::InSequence; |
| 24 using ::testing::Invoke; | 26 using ::testing::Invoke; |
| 25 using ::testing::Return; | |
| 26 using ::testing::ReturnRef; | |
| 27 using ::testing::SaveArg; | |
| 28 using ::testing::WithArg; | 27 using ::testing::WithArg; |
| 29 | 28 |
| 30 namespace em = enterprise_management; | 29 namespace em = enterprise_management; |
| 31 namespace chromeos { | 30 namespace chromeos { |
| 32 | 31 |
| 33 class MockSignedSettingsHelperCallback : public SignedSettingsHelper::Callback { | 32 ACTION_P(Retrieve, policy_blob) { arg0.Run(policy_blob); } |
| 34 public: | 33 ACTION_P(Store, success) { arg1.Run(success); } |
| 35 virtual ~MockSignedSettingsHelperCallback() {} | |
| 36 | |
| 37 MOCK_METHOD3(OnStorePropertyCompleted, void( | |
| 38 SignedSettings::ReturnCode code, | |
| 39 const std::string& name, | |
| 40 const base::Value& value)); | |
| 41 MOCK_METHOD3(OnRetrievePropertyCompleted, void( | |
| 42 SignedSettings::ReturnCode code, | |
| 43 const std::string& name, | |
| 44 const base::Value* value)); | |
| 45 }; | |
| 46 | 34 |
| 47 class SignedSettingsHelperTest : public testing::Test, | 35 class SignedSettingsHelperTest : public testing::Test, |
| 48 public SignedSettingsHelper::TestDelegate { | 36 public SignedSettingsHelper::TestDelegate { |
| 49 public: | 37 public: |
| 50 SignedSettingsHelperTest() | 38 SignedSettingsHelperTest() |
| 51 : fake_email_("fakey@example.com"), | 39 : message_loop_(MessageLoop::TYPE_UI), |
| 52 fake_prop_(kReleaseChannel), | 40 ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 53 fake_value_("false"), | 41 file_thread_(content::BrowserThread::FILE, &message_loop_), |
| 54 message_loop_(MessageLoop::TYPE_UI), | 42 pending_ops_(0), |
| 55 ui_thread_(BrowserThread::UI, &message_loop_), | 43 mock_dbus_thread_manager_(new MockDBusThreadManager) { |
| 56 file_thread_(BrowserThread::FILE), | |
| 57 pending_ops_(0) { | |
| 58 } | 44 } |
| 59 | 45 |
| 60 virtual void SetUp() { | 46 virtual void SetUp() { |
| 61 file_thread_.Start(); | |
| 62 SignedSettingsHelper::Get()->set_test_delegate(this); | 47 SignedSettingsHelper::Get()->set_test_delegate(this); |
| 63 DBusThreadManager::Initialize(); | 48 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager_); |
| 49 | |
| 50 fake_policy_data_ = BuildPolicyData(); | |
| 51 std::string data_serialized = fake_policy_data_.SerializeAsString(); | |
| 52 std::string serialized_policy_; | |
| 53 fake_policy_ = BuildProto(data_serialized, | |
| 54 std::string("false"), | |
| 55 &serialized_policy_); | |
| 56 | |
| 57 MockSessionManagerClient* client = | |
| 58 mock_dbus_thread_manager_->mock_session_manager_client(); | |
| 59 EXPECT_CALL(*client, StorePolicy(_, _)) | |
| 60 .WillRepeatedly(Store(true)); | |
|
Mattias Nissler (ping if slow)
2011/12/02 12:13:37
Can this be inlined with something along the lines
pastarmovj
2011/12/02 14:43:38
Couldn't get this running with some gmock voodoo b
| |
| 61 EXPECT_CALL(*client, RetrievePolicy(_)) | |
| 62 .WillRepeatedly(Retrieve(serialized_policy_)); | |
| 63 | |
| 64 EXPECT_CALL(m_, StartSigningAttempt(_, A<OwnerManager::Delegate*>())) | |
| 65 .WillRepeatedly(WithArg<1>( | |
| 66 Invoke(&SignedSettingsHelperTest::OnKeyOpComplete))); | |
| 67 EXPECT_CALL(m_, StartVerifyAttempt(_, _, A<OwnerManager::Delegate*>())) | |
| 68 .WillRepeatedly(WithArg<2>( | |
| 69 Invoke(&SignedSettingsHelperTest::OnKeyOpComplete))); | |
| 64 } | 70 } |
| 65 | 71 |
| 66 virtual void TearDown() { | 72 virtual void TearDown() { |
| 67 DBusThreadManager::Shutdown(); | 73 DBusThreadManager::Shutdown(); |
| 68 SignedSettingsHelper::Get()->set_test_delegate(NULL); | 74 SignedSettingsHelper::Get()->set_test_delegate(NULL); |
| 69 } | 75 } |
| 70 | 76 |
| 71 virtual void OnOpCreated(SignedSettings* op) { | 77 virtual void OnOpCreated(SignedSettings* op) { |
| 72 // Use MockOwnershipService for all SignedSettings op. | 78 // Use MockOwnershipService for all SignedSettings op. |
| 73 op->set_service(&m_); | 79 op->set_service(&m_); |
| 74 } | 80 } |
| 75 | 81 |
| 76 virtual void OnOpStarted(SignedSettings* op) { | 82 virtual void OnOpStarted(SignedSettings* op) { |
| 77 } | 83 } |
| 78 | 84 |
| 79 virtual void OnOpCompleted(SignedSettings* op) { | 85 virtual void OnOpCompleted(SignedSettings* op) { |
| 80 --pending_ops_; | 86 --pending_ops_; |
| 81 if (!pending_ops_) | |
| 82 MessageLoop::current()->Quit(); | |
| 83 } | 87 } |
| 84 | 88 |
| 85 static void OnKeyOpComplete(OwnerManager::Delegate* op) { | 89 static void OnKeyOpComplete(OwnerManager::Delegate* op) { |
| 86 op->OnKeyOpComplete(OwnerManager::SUCCESS, std::vector<uint8>()); | 90 op->OnKeyOpComplete(OwnerManager::SUCCESS, std::vector<uint8>()); |
| 87 } | 91 } |
| 88 | 92 |
| 89 em::PolicyData BuildPolicyData() { | 93 em::PolicyData BuildPolicyData() { |
| 90 em::PolicyData to_return; | 94 em::PolicyData to_return; |
| 91 em::ChromeDeviceSettingsProto pol; | 95 em::ChromeDeviceSettingsProto pol; |
| 92 to_return.set_policy_type(SignedSettings::kDevicePolicyType); | 96 to_return.set_policy_type(chromeos::kDevicePolicyType); |
| 93 to_return.set_policy_value(pol.SerializeAsString()); | 97 to_return.set_policy_value(pol.SerializeAsString()); |
| 94 return to_return; | 98 return to_return; |
| 95 } | 99 } |
| 96 | 100 |
| 97 const std::string fake_email_; | 101 em::PolicyFetchResponse BuildProto(const std::string& data, |
| 98 const std::string fake_prop_; | 102 const std::string& sig, |
| 99 const base::StringValue fake_value_; | 103 std::string* out_serialized) { |
| 104 em::PolicyFetchResponse fake_policy; | |
| 105 if (!data.empty()) | |
| 106 fake_policy.set_policy_data(data); | |
| 107 if (!sig.empty()) | |
| 108 fake_policy.set_policy_data_signature(sig); | |
| 109 EXPECT_TRUE(fake_policy.SerializeToString(out_serialized)); | |
| 110 return fake_policy; | |
| 111 } | |
| 112 | |
| 113 em::PolicyData fake_policy_data_; | |
| 114 em::PolicyFetchResponse fake_policy_; | |
| 115 std::string serialized_policy_; | |
| 100 MockOwnershipService m_; | 116 MockOwnershipService m_; |
| 101 | 117 |
| 102 MessageLoop message_loop_; | 118 MessageLoop message_loop_; |
| 103 content::TestBrowserThread ui_thread_; | 119 content::TestBrowserThread ui_thread_; |
| 104 content::TestBrowserThread file_thread_; | 120 content::TestBrowserThread file_thread_; |
| 105 | 121 |
| 106 int pending_ops_; | 122 int pending_ops_; |
| 107 | 123 |
| 124 MockDBusThreadManager* mock_dbus_thread_manager_; | |
| 125 | |
| 108 ScopedStubCrosEnabler stub_cros_enabler_; | 126 ScopedStubCrosEnabler stub_cros_enabler_; |
| 109 }; | 127 }; |
| 110 | 128 |
| 129 class SignedSettingsCallbacks { | |
| 130 public: | |
| 131 virtual ~SignedSettingsCallbacks() {} | |
| 132 // Callback of StorePolicyOp. | |
| 133 virtual void OnStorePolicyCompleted(SignedSettings::ReturnCode code) = 0; | |
| 134 // Callback of RetrievePolicyOp. | |
| 135 virtual void OnRetrievePolicyCompleted( | |
| 136 SignedSettings::ReturnCode code, | |
| 137 const em::PolicyFetchResponse& policy) = 0; | |
| 138 }; | |
| 139 | |
| 140 class MockSignedSettingsCallbacks | |
| 141 : public SignedSettingsCallbacks, | |
| 142 public base::SupportsWeakPtr<MockSignedSettingsCallbacks> { | |
| 143 public: | |
| 144 virtual ~MockSignedSettingsCallbacks() {} | |
| 145 | |
| 146 MOCK_METHOD1(OnStorePolicyCompleted, void(SignedSettings::ReturnCode)); | |
| 147 MOCK_METHOD2(OnRetrievePolicyCompleted, void(SignedSettings::ReturnCode, | |
| 148 const em::PolicyFetchResponse&)); | |
| 149 }; | |
| 150 | |
| 111 TEST_F(SignedSettingsHelperTest, SerializedOps) { | 151 TEST_F(SignedSettingsHelperTest, SerializedOps) { |
| 112 MockSignedSettingsHelperCallback cb; | 152 MockSignedSettingsCallbacks cb; |
| 113 | |
| 114 EXPECT_CALL(m_, GetStatus(_)) | |
| 115 .Times(2) | |
| 116 .WillRepeatedly(Return(OwnershipService::OWNERSHIP_TAKEN)); | |
| 117 EXPECT_CALL(m_, has_cached_policy()) | |
| 118 .Times(2) | |
| 119 .WillRepeatedly(Return(true)); | |
| 120 em::PolicyData fake_pol = BuildPolicyData(); | |
| 121 EXPECT_CALL(m_, cached_policy()) | |
| 122 .Times(2) | |
| 123 .WillRepeatedly(ReturnRef(fake_pol)); | |
| 124 EXPECT_CALL(m_, set_cached_policy(A<const em::PolicyData&>())) | |
| 125 .Times(1) | |
| 126 .WillRepeatedly(SaveArg<0>(&fake_pol)); | |
| 127 | 153 |
| 128 InSequence s; | 154 InSequence s; |
| 129 EXPECT_CALL(m_, StartSigningAttempt(_, A<OwnerManager::Delegate*>())) | 155 EXPECT_CALL(cb, OnStorePolicyCompleted(SignedSettings::SUCCESS)) |
| 130 .WillOnce(WithArg<1>(Invoke(&SignedSettingsHelperTest::OnKeyOpComplete))); | |
| 131 EXPECT_CALL(cb, OnStorePropertyCompleted(SignedSettings::SUCCESS, _, _)) | |
| 132 .Times(1); | 156 .Times(1); |
| 133 | 157 EXPECT_CALL(cb, OnRetrievePolicyCompleted(SignedSettings::SUCCESS, _)) |
| 134 EXPECT_CALL(cb, OnRetrievePropertyCompleted(SignedSettings::SUCCESS, _, _)) | |
| 135 .Times(1); | 158 .Times(1); |
| 136 | 159 |
| 137 | 160 |
| 138 pending_ops_ = 2; | 161 pending_ops_ = 2; |
| 139 SignedSettingsHelper::Get()->StartStorePropertyOp(fake_prop_, fake_value_, | 162 SignedSettingsHelper::Get()->StartStorePolicyOp( |
| 140 &cb); | 163 fake_policy_, |
| 141 SignedSettingsHelper::Get()->StartRetrieveProperty(fake_prop_, &cb); | 164 base::Bind(&MockSignedSettingsCallbacks::OnStorePolicyCompleted, |
| 165 base::Unretained(&cb))); | |
| 166 SignedSettingsHelper::Get()->StartRetrievePolicyOp( | |
| 167 base::Bind(&MockSignedSettingsCallbacks::OnRetrievePolicyCompleted, | |
| 168 base::Unretained(&cb))); | |
| 142 | 169 |
| 143 message_loop_.Run(); | 170 message_loop_.RunAllPending(); |
| 171 ASSERT_EQ(0, pending_ops_); | |
| 144 } | 172 } |
| 145 | 173 |
| 146 TEST_F(SignedSettingsHelperTest, CanceledOps) { | 174 TEST_F(SignedSettingsHelperTest, CanceledOps) { |
| 147 MockSignedSettingsHelperCallback cb; | 175 MockSignedSettingsCallbacks cb; |
| 148 | |
| 149 EXPECT_CALL(m_, GetStatus(_)) | |
| 150 .Times(3) | |
| 151 .WillRepeatedly(Return(OwnershipService::OWNERSHIP_TAKEN)); | |
| 152 EXPECT_CALL(m_, has_cached_policy()) | |
| 153 .Times(3) | |
| 154 .WillRepeatedly(Return(true)); | |
| 155 em::PolicyData fake_pol = BuildPolicyData(); | |
| 156 EXPECT_CALL(m_, cached_policy()) | |
| 157 .Times(3) | |
| 158 .WillRepeatedly(ReturnRef(fake_pol)); | |
| 159 EXPECT_CALL(m_, set_cached_policy(A<const em::PolicyData&>())) | |
| 160 .Times(1) | |
| 161 .WillRepeatedly(SaveArg<0>(&fake_pol)); | |
| 162 | 176 |
| 163 InSequence s; | 177 InSequence s; |
| 164 | 178 EXPECT_CALL(cb, OnStorePolicyCompleted(SignedSettings::SUCCESS)) |
| 165 // RetrievePropertyOp for cb_to_be_canceled still gets executed but callback | |
| 166 // does not happen. | |
| 167 EXPECT_CALL(m_, StartSigningAttempt(_, A<OwnerManager::Delegate*>())) | |
| 168 .WillOnce(WithArg<1>(Invoke(&SignedSettingsHelperTest::OnKeyOpComplete))); | |
| 169 EXPECT_CALL(cb, OnStorePropertyCompleted(SignedSettings::SUCCESS, _, _)) | |
| 170 .Times(1); | 179 .Times(1); |
| 171 EXPECT_CALL(cb, OnRetrievePropertyCompleted(SignedSettings::SUCCESS, _, _)) | 180 EXPECT_CALL(cb, OnRetrievePolicyCompleted(SignedSettings::SUCCESS, _)) |
| 172 .Times(1); | 181 .Times(1); |
| 173 | 182 |
| 174 pending_ops_ = 3; | 183 pending_ops_ = 3; |
| 175 MockSignedSettingsHelperCallback cb_to_be_canceled; | |
| 176 SignedSettingsHelper::Get()->StartRetrieveProperty(fake_prop_, | |
| 177 &cb_to_be_canceled); | |
| 178 SignedSettingsHelper::Get()->CancelCallback(&cb_to_be_canceled); | |
| 179 | 184 |
| 180 SignedSettingsHelper::Get()->StartStorePropertyOp(fake_prop_, fake_value_, | 185 { |
| 181 &cb); | 186 // This op will be deleted and never will be executed (expect only one call |
| 182 SignedSettingsHelper::Get()->StartRetrieveProperty(fake_prop_, &cb); | 187 // to OnRetrievePolicyCompleted above). However the OpComplete callback will |
| 188 // be still called, therefore we expect three pending ops. | |
| 189 MockSignedSettingsCallbacks cb_to_be_deleted; | |
| 190 SignedSettingsHelper::Get()->StartRetrievePolicyOp( | |
| 191 base::Bind(&MockSignedSettingsCallbacks::OnRetrievePolicyCompleted, | |
| 192 cb_to_be_deleted.AsWeakPtr())); | |
| 193 } | |
| 194 SignedSettingsHelper::Get()->StartStorePolicyOp( | |
| 195 fake_policy_, | |
| 196 base::Bind(&MockSignedSettingsCallbacks::OnStorePolicyCompleted, | |
| 197 base::Unretained(&cb))); | |
| 198 SignedSettingsHelper::Get()->StartRetrievePolicyOp( | |
| 199 base::Bind(&MockSignedSettingsCallbacks::OnRetrievePolicyCompleted, | |
| 200 base::Unretained(&cb))); | |
| 183 | 201 |
| 184 message_loop_.Run(); | 202 message_loop_.RunAllPending(); |
| 203 ASSERT_EQ(0, pending_ops_); | |
| 185 } | 204 } |
| 186 | 205 |
| 187 } // namespace chromeos | 206 } // namespace chromeos |
| OLD | NEW |