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), |
| 54 message_loop_(MessageLoop::TYPE_UI), | 42 pending_ops_(0), |
| 55 ui_thread_(BrowserThread::UI, &message_loop_), | 43 mock_(new MockKeyUtils), |
|
Mattias Nissler (ping if slow)
2011/11/30 12:25:50
Is this needed?
pastarmovj
2011/11/30 17:21:16
Done.
| |
| 56 file_thread_(BrowserThread::FILE), | 44 injector_(mock_) /* injector_ takes ownership of mock_ */, |
|
Mattias Nissler (ping if slow)
2011/11/30 12:25:50
Is this really needed?
pastarmovj
2011/11/30 17:21:16
Done.
| |
| 57 pending_ops_(0) { | 45 mock_dbus_thread_manager_(new MockDBusThreadManager) { |
| 58 } | 46 } |
| 59 | 47 |
| 60 virtual void SetUp() { | 48 virtual void SetUp() { |
| 61 file_thread_.Start(); | 49 file_thread_.Start(); |
| 62 SignedSettingsHelper::Get()->set_test_delegate(this); | 50 SignedSettingsHelper::Get()->set_test_delegate(this); |
| 63 DBusThreadManager::Initialize(); | 51 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager_); |
| 52 | |
| 53 fake_policy_data_ = BuildPolicyData(); | |
| 54 std::string data_serialized = fake_policy_data_.SerializeAsString(); | |
| 55 std::string serialized_policy_; | |
| 56 fake_policy_ = BuildProto(data_serialized, | |
| 57 std::string("false"), | |
| 58 &serialized_policy_); | |
| 59 | |
| 60 MockSessionManagerClient* client = | |
| 61 mock_dbus_thread_manager_->mock_session_manager_client(); | |
| 62 EXPECT_CALL(*client, StorePolicy(_, _)) | |
| 63 .WillRepeatedly(Store(true)); | |
| 64 EXPECT_CALL(*client, RetrievePolicy(_)) | |
| 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))); | |
| 64 } | 73 } |
| 65 | 74 |
| 66 virtual void TearDown() { | 75 virtual void TearDown() { |
| 67 DBusThreadManager::Shutdown(); | 76 DBusThreadManager::Shutdown(); |
| 68 SignedSettingsHelper::Get()->set_test_delegate(NULL); | 77 SignedSettingsHelper::Get()->set_test_delegate(NULL); |
| 69 } | 78 } |
| 70 | 79 |
| 71 virtual void OnOpCreated(SignedSettings* op) { | 80 virtual void OnOpCreated(SignedSettings* op) { |
| 72 // Use MockOwnershipService for all SignedSettings op. | 81 // Use MockOwnershipService for all SignedSettings op. |
| 73 op->set_service(&m_); | 82 op->set_service(&m_); |
| 74 } | 83 } |
| 75 | 84 |
| 76 virtual void OnOpStarted(SignedSettings* op) { | 85 virtual void OnOpStarted(SignedSettings* op) { |
| 77 } | 86 } |
| 78 | 87 |
| 79 virtual void OnOpCompleted(SignedSettings* op) { | 88 virtual void OnOpCompleted(SignedSettings* op) { |
| 80 --pending_ops_; | 89 --pending_ops_; |
| 81 if (!pending_ops_) | |
| 82 MessageLoop::current()->Quit(); | |
| 83 } | 90 } |
| 84 | 91 |
| 85 static void OnKeyOpComplete(OwnerManager::Delegate* op) { | 92 static void OnKeyOpComplete(OwnerManager::Delegate* op) { |
| 86 op->OnKeyOpComplete(OwnerManager::SUCCESS, std::vector<uint8>()); | 93 op->OnKeyOpComplete(OwnerManager::SUCCESS, std::vector<uint8>()); |
| 87 } | 94 } |
| 88 | 95 |
| 89 em::PolicyData BuildPolicyData() { | 96 em::PolicyData BuildPolicyData() { |
| 90 em::PolicyData to_return; | 97 em::PolicyData to_return; |
| 91 em::ChromeDeviceSettingsProto pol; | 98 em::ChromeDeviceSettingsProto pol; |
| 92 to_return.set_policy_type(SignedSettings::kDevicePolicyType); | 99 to_return.set_policy_type(chromeos::kDevicePolicyType); |
| 93 to_return.set_policy_value(pol.SerializeAsString()); | 100 to_return.set_policy_value(pol.SerializeAsString()); |
| 94 return to_return; | 101 return to_return; |
| 95 } | 102 } |
| 96 | 103 |
| 97 const std::string fake_email_; | 104 em::PolicyFetchResponse BuildProto(const std::string& data, |
| 98 const std::string fake_prop_; | 105 const std::string& sig, |
| 99 const base::StringValue fake_value_; | 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_; | |
| 100 MockOwnershipService m_; | 119 MockOwnershipService m_; |
| 101 | 120 |
| 102 MessageLoop message_loop_; | 121 MessageLoop message_loop_; |
| 103 content::TestBrowserThread ui_thread_; | 122 content::TestBrowserThread ui_thread_; |
| 104 content::TestBrowserThread file_thread_; | 123 content::TestBrowserThread file_thread_; |
| 105 | 124 |
| 106 int pending_ops_; | 125 int pending_ops_; |
| 107 | 126 |
| 127 MockKeyUtils* mock_; | |
| 128 MockInjector injector_; | |
| 129 MockDBusThreadManager* mock_dbus_thread_manager_; | |
| 130 | |
| 108 ScopedStubCrosEnabler stub_cros_enabler_; | 131 ScopedStubCrosEnabler stub_cros_enabler_; |
| 109 }; | 132 }; |
| 110 | 133 |
| 134 class SignedSettingsCallbacks { | |
| 135 public: | |
| 136 virtual ~SignedSettingsCallbacks() {} | |
| 137 // Callback of StorePolicyOp. | |
| 138 virtual void OnStorePolicyCompleted(SignedSettings::ReturnCode code) = 0; | |
| 139 // Callback of RetrievePolicyOp. | |
| 140 virtual void OnRetrievePolicyCompleted( | |
| 141 SignedSettings::ReturnCode code, | |
| 142 const em::PolicyFetchResponse& policy) = 0; | |
| 143 }; | |
| 144 | |
| 145 class MockSignedSettingsCallbacks | |
| 146 : public SignedSettingsCallbacks, | |
| 147 public base::SupportsWeakPtr<MockSignedSettingsCallbacks> { | |
| 148 public: | |
| 149 virtual ~MockSignedSettingsCallbacks() {} | |
| 150 | |
| 151 MOCK_METHOD1(OnStorePolicyCompleted, void(SignedSettings::ReturnCode)); | |
| 152 MOCK_METHOD2(OnRetrievePolicyCompleted, void(SignedSettings::ReturnCode, | |
| 153 const em::PolicyFetchResponse&)); | |
| 154 }; | |
| 155 | |
| 111 TEST_F(SignedSettingsHelperTest, SerializedOps) { | 156 TEST_F(SignedSettingsHelperTest, SerializedOps) { |
| 112 MockSignedSettingsHelperCallback cb; | 157 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 | 158 |
| 128 InSequence s; | 159 InSequence s; |
| 129 EXPECT_CALL(m_, StartSigningAttempt(_, A<OwnerManager::Delegate*>())) | 160 EXPECT_CALL(cb, OnStorePolicyCompleted(SignedSettings::SUCCESS)) |
| 130 .WillOnce(WithArg<1>(Invoke(&SignedSettingsHelperTest::OnKeyOpComplete))); | |
| 131 EXPECT_CALL(cb, OnStorePropertyCompleted(SignedSettings::SUCCESS, _, _)) | |
| 132 .Times(1); | 161 .Times(1); |
| 133 | 162 EXPECT_CALL(cb, OnRetrievePolicyCompleted(SignedSettings::SUCCESS, _)) |
| 134 EXPECT_CALL(cb, OnRetrievePropertyCompleted(SignedSettings::SUCCESS, _, _)) | |
| 135 .Times(1); | 163 .Times(1); |
| 136 | 164 |
| 137 | 165 |
| 138 pending_ops_ = 2; | 166 pending_ops_ = 2; |
| 139 SignedSettingsHelper::Get()->StartStorePropertyOp(fake_prop_, fake_value_, | 167 SignedSettingsHelper::Get()->StartStorePolicyOp( |
| 140 &cb); | 168 fake_policy_, |
| 141 SignedSettingsHelper::Get()->StartRetrieveProperty(fake_prop_, &cb); | 169 base::Bind(&MockSignedSettingsCallbacks::OnStorePolicyCompleted, |
| 170 base::Unretained(&cb))); | |
| 171 SignedSettingsHelper::Get()->StartRetrievePolicyOp( | |
| 172 base::Bind(&MockSignedSettingsCallbacks::OnRetrievePolicyCompleted, | |
| 173 base::Unretained(&cb))); | |
| 142 | 174 |
| 143 message_loop_.Run(); | 175 message_loop_.RunAllPending(); |
| 176 ASSERT_EQ(0, pending_ops_); | |
| 144 } | 177 } |
| 145 | 178 |
| 146 TEST_F(SignedSettingsHelperTest, CanceledOps) { | 179 TEST_F(SignedSettingsHelperTest, CanceledOps) { |
| 147 MockSignedSettingsHelperCallback cb; | 180 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 | 181 |
| 163 InSequence s; | 182 InSequence s; |
| 164 | 183 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); | 184 .Times(1); |
| 171 EXPECT_CALL(cb, OnRetrievePropertyCompleted(SignedSettings::SUCCESS, _, _)) | 185 EXPECT_CALL(cb, OnRetrievePolicyCompleted(SignedSettings::SUCCESS, _)) |
| 172 .Times(1); | 186 .Times(1); |
| 173 | 187 |
| 174 pending_ops_ = 3; | 188 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 | 189 |
| 180 SignedSettingsHelper::Get()->StartStorePropertyOp(fake_prop_, fake_value_, | 190 { |
| 181 &cb); | 191 // This op will be deleted and never will be executed (expect only one call |
|
Mattias Nissler (ping if slow)
2011/11/30 12:25:50
you don't close the parentheses opened on this lin
pastarmovj
2011/11/30 17:21:16
Done.
| |
| 182 SignedSettingsHelper::Get()->StartRetrieveProperty(fake_prop_, &cb); | 192 // to OnRetrievePolicyCompleted above. However the OpComplete callback will |
| 193 // be still called therefore we expect three pending ops. | |
|
Mattias Nissler (ping if slow)
2011/11/30 12:25:50
comma after called
pastarmovj
2011/11/30 17:21:16
Done.
| |
| 194 MockSignedSettingsCallbacks cb_to_be_deleted; | |
| 195 SignedSettingsHelper::Get()->StartRetrievePolicyOp( | |
| 196 base::Bind(&MockSignedSettingsCallbacks::OnRetrievePolicyCompleted, | |
| 197 cb_to_be_deleted.AsWeakPtr())); | |
| 198 } | |
| 199 SignedSettingsHelper::Get()->StartStorePolicyOp( | |
| 200 fake_policy_, | |
| 201 base::Bind(&MockSignedSettingsCallbacks::OnStorePolicyCompleted, | |
| 202 base::Unretained(&cb))); | |
| 203 SignedSettingsHelper::Get()->StartRetrievePolicyOp( | |
| 204 base::Bind(&MockSignedSettingsCallbacks::OnRetrievePolicyCompleted, | |
| 205 base::Unretained(&cb))); | |
| 183 | 206 |
| 184 message_loop_.Run(); | 207 message_loop_.RunAllPending(); |
| 208 ASSERT_EQ(0, pending_ops_); | |
| 185 } | 209 } |
| 186 | 210 |
| 187 } // namespace chromeos | 211 } // namespace chromeos |
| OLD | NEW |