| 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/device_settings_provider.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/message_loop.h" | |
| 11 #include "base/values.h" | |
| 12 #include "chrome/browser/chromeos/cros/cros_library.h" | |
| 13 #include "chrome/browser/chromeos/cros_settings_names.h" | |
| 14 #include "chrome/browser/chromeos/login/mock_signed_settings_helper.h" | |
| 15 #include "chrome/browser/chromeos/login/mock_user_manager.h" | |
| 16 #include "chrome/browser/chromeos/login/ownership_service.h" | |
| 17 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" | |
| 18 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
| 19 #include "chrome/test/base/testing_browser_process.h" | |
| 20 #include "chrome/test/base/testing_pref_service.h" | |
| 21 #include "content/public/test/test_browser_thread.h" | |
| 22 #include "testing/gmock/include/gmock/gmock.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | |
| 24 | |
| 25 namespace em = enterprise_management; | |
| 26 namespace chromeos { | |
| 27 | |
| 28 using ::testing::_; | |
| 29 using ::testing::AnyNumber; | |
| 30 using ::testing::Mock; | |
| 31 using ::testing::Return; | |
| 32 using ::testing::SaveArg; | |
| 33 | |
| 34 class DeviceSettingsProviderTest: public testing::Test { | |
| 35 public: | |
| 36 MOCK_METHOD1(SettingChanged, void(const std::string&)); | |
| 37 MOCK_METHOD0(GetTrustedCallback, void(void)); | |
| 38 | |
| 39 protected: | |
| 40 DeviceSettingsProviderTest() | |
| 41 : message_loop_(MessageLoop::TYPE_UI), | |
| 42 ui_thread_(content::BrowserThread::UI, &message_loop_), | |
| 43 file_thread_(content::BrowserThread::FILE, &message_loop_), | |
| 44 local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)) { | |
| 45 } | |
| 46 | |
| 47 virtual ~DeviceSettingsProviderTest() { | |
| 48 } | |
| 49 | |
| 50 virtual void SetUp() OVERRIDE { | |
| 51 PrepareEmptyPolicy(); | |
| 52 | |
| 53 EXPECT_CALL(*this, SettingChanged(_)) | |
| 54 .Times(AnyNumber()); | |
| 55 | |
| 56 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)) | |
| 57 .WillRepeatedly( | |
| 58 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, | |
| 59 policy_blob_)); | |
| 60 EXPECT_CALL(signed_settings_helper_, StartStorePolicyOp(_,_)) | |
| 61 .WillRepeatedly(DoAll( | |
| 62 SaveArg<0>(&policy_blob_), | |
| 63 MockSignedSettingsHelperStorePolicy(SignedSettings::SUCCESS))); | |
| 64 | |
| 65 EXPECT_CALL(*mock_user_manager_.user_manager(), IsCurrentUserOwner()) | |
| 66 .WillRepeatedly(Return(true)); | |
| 67 | |
| 68 provider_.reset( | |
| 69 new DeviceSettingsProvider( | |
| 70 base::Bind(&DeviceSettingsProviderTest::SettingChanged, | |
| 71 base::Unretained(this)), | |
| 72 &signed_settings_helper_)); | |
| 73 provider_->set_ownership_status(OwnershipService::OWNERSHIP_TAKEN); | |
| 74 // To prevent flooding the logs. | |
| 75 provider_->set_retries_left(1); | |
| 76 provider_->Reload(); | |
| 77 } | |
| 78 | |
| 79 void PrepareEmptyPolicy() { | |
| 80 em::PolicyData policy; | |
| 81 em::ChromeDeviceSettingsProto pol; | |
| 82 // Set metrics to disabled to prevent us from running into code that is not | |
| 83 // mocked. | |
| 84 pol.mutable_metrics_enabled()->set_metrics_enabled(false); | |
| 85 policy.set_policy_type(chromeos::kDevicePolicyType); | |
| 86 policy.set_username("me@owner"); | |
| 87 policy.set_policy_value(pol.SerializeAsString()); | |
| 88 // Wipe the signed settings store. | |
| 89 policy_blob_.set_policy_data(policy.SerializeAsString()); | |
| 90 policy_blob_.set_policy_data_signature("false"); | |
| 91 } | |
| 92 | |
| 93 em::PolicyFetchResponse policy_blob_; | |
| 94 | |
| 95 scoped_ptr<DeviceSettingsProvider> provider_; | |
| 96 | |
| 97 MessageLoop message_loop_; | |
| 98 content::TestBrowserThread ui_thread_; | |
| 99 content::TestBrowserThread file_thread_; | |
| 100 | |
| 101 ScopedTestingLocalState local_state_; | |
| 102 | |
| 103 MockSignedSettingsHelper signed_settings_helper_; | |
| 104 | |
| 105 ScopedStubCrosEnabler stub_cros_enabler_; | |
| 106 ScopedMockUserManagerEnabler mock_user_manager_; | |
| 107 }; | |
| 108 | |
| 109 TEST_F(DeviceSettingsProviderTest, InitializationTest) { | |
| 110 // Verify that the policy blob has been correctly parsed and trusted. | |
| 111 // The trusted flag should be set before the call to PrepareTrustedValues. | |
| 112 EXPECT_EQ(CrosSettingsProvider::TRUSTED, | |
| 113 provider_->PrepareTrustedValues( | |
| 114 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback, | |
| 115 base::Unretained(this)))); | |
| 116 const base::Value* value = provider_->Get(kStatsReportingPref); | |
| 117 ASSERT_TRUE(value); | |
| 118 bool bool_value; | |
| 119 EXPECT_TRUE(value->GetAsBoolean(&bool_value)); | |
| 120 EXPECT_FALSE(bool_value); | |
| 121 } | |
| 122 | |
| 123 TEST_F(DeviceSettingsProviderTest, InitializationTestUnowned) { | |
| 124 // No calls to the SignedSettingsHelper should occur in this case! | |
| 125 Mock::VerifyAndClear(&signed_settings_helper_); | |
| 126 | |
| 127 provider_->set_ownership_status(OwnershipService::OWNERSHIP_NONE); | |
| 128 provider_->Reload(); | |
| 129 // The trusted flag should be set before the call to PrepareTrustedValues. | |
| 130 EXPECT_EQ(CrosSettingsProvider::TRUSTED, | |
| 131 provider_->PrepareTrustedValues( | |
| 132 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback, | |
| 133 base::Unretained(this)))); | |
| 134 const base::Value* value = provider_->Get(kReleaseChannel); | |
| 135 ASSERT_TRUE(value); | |
| 136 std::string string_value; | |
| 137 EXPECT_TRUE(value->GetAsString(&string_value)); | |
| 138 EXPECT_TRUE(string_value.empty()); | |
| 139 | |
| 140 // Sets should succeed though and be readable from the cache. | |
| 141 base::StringValue new_value("stable-channel"); | |
| 142 provider_->Set(kReleaseChannel, new_value); | |
| 143 // Do one more reload here to make sure we don't flip randomly between stores. | |
| 144 provider_->Reload(); | |
| 145 // Verify the change has not been applied. | |
| 146 const base::Value* saved_value = provider_->Get(kReleaseChannel); | |
| 147 ASSERT_TRUE(saved_value); | |
| 148 EXPECT_TRUE(saved_value->GetAsString(&string_value)); | |
| 149 ASSERT_EQ("stable-channel", string_value); | |
| 150 } | |
| 151 | |
| 152 TEST_F(DeviceSettingsProviderTest, SetPrefFailed) { | |
| 153 // If we are not the owner no sets should work. | |
| 154 EXPECT_CALL(*mock_user_manager_.user_manager(), IsCurrentUserOwner()) | |
| 155 .WillOnce(Return(false)); | |
| 156 base::FundamentalValue value(true); | |
| 157 provider_->Set(kStatsReportingPref, value); | |
| 158 // Verify the change has not been applied. | |
| 159 const base::Value* saved_value = provider_->Get(kStatsReportingPref); | |
| 160 ASSERT_TRUE(saved_value); | |
| 161 bool bool_value; | |
| 162 EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value)); | |
| 163 EXPECT_FALSE(bool_value); | |
| 164 } | |
| 165 | |
| 166 TEST_F(DeviceSettingsProviderTest, SetPrefSucceed) { | |
| 167 base::FundamentalValue value(true); | |
| 168 provider_->Set(kStatsReportingPref, value); | |
| 169 // Verify the change has not been applied. | |
| 170 const base::Value* saved_value = provider_->Get(kStatsReportingPref); | |
| 171 ASSERT_TRUE(saved_value); | |
| 172 bool bool_value; | |
| 173 EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value)); | |
| 174 EXPECT_TRUE(bool_value); | |
| 175 } | |
| 176 | |
| 177 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalFailedBadSingature) { | |
| 178 // No calls to the SignedSettingsHelper should occur in this case! | |
| 179 Mock::VerifyAndClear(&signed_settings_helper_); | |
| 180 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)) | |
| 181 .WillRepeatedly( | |
| 182 MockSignedSettingsHelperRetrievePolicy( | |
| 183 SignedSettings::BAD_SIGNATURE, | |
| 184 policy_blob_)); | |
| 185 provider_->Reload(); | |
| 186 // Verify that the cache policy blob is not "trusted". | |
| 187 EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED, | |
| 188 provider_->PrepareTrustedValues( | |
| 189 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback, | |
| 190 base::Unretained(this)))); | |
| 191 } | |
| 192 | |
| 193 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalOperationFailedPermanently) { | |
| 194 // No calls to the SignedSettingsHelper should occur in this case! | |
| 195 Mock::VerifyAndClear(&signed_settings_helper_); | |
| 196 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)) | |
| 197 .WillRepeatedly( | |
| 198 MockSignedSettingsHelperRetrievePolicy( | |
| 199 SignedSettings::OPERATION_FAILED, | |
| 200 policy_blob_)); | |
| 201 provider_->Reload(); | |
| 202 // Verify that the cache policy blob is not "trusted". | |
| 203 EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED, | |
| 204 provider_->PrepareTrustedValues( | |
| 205 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback, | |
| 206 base::Unretained(this)))); | |
| 207 } | |
| 208 | |
| 209 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalOperationFailedOnce) { | |
| 210 // No calls to the SignedSettingsHelper should occur in this case! | |
| 211 Mock::VerifyAndClear(&signed_settings_helper_); | |
| 212 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)) | |
| 213 .WillOnce( | |
| 214 MockSignedSettingsHelperRetrievePolicy( | |
| 215 SignedSettings::OPERATION_FAILED, | |
| 216 policy_blob_)) | |
| 217 .WillRepeatedly( | |
| 218 MockSignedSettingsHelperRetrievePolicy( | |
| 219 SignedSettings::SUCCESS, | |
| 220 policy_blob_)); | |
| 221 // Should be trusted after an automatic reload. | |
| 222 provider_->Reload(); | |
| 223 // Verify that the cache policy blob is not "trusted". | |
| 224 EXPECT_EQ(CrosSettingsProvider::TRUSTED, | |
| 225 provider_->PrepareTrustedValues( | |
| 226 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback, | |
| 227 base::Unretained(this)))); | |
| 228 } | |
| 229 | |
| 230 TEST_F(DeviceSettingsProviderTest, PolicyFailedPermanentlyNotification) { | |
| 231 Mock::VerifyAndClear(&signed_settings_helper_); | |
| 232 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)) | |
| 233 .WillRepeatedly( | |
| 234 MockSignedSettingsHelperRetrievePolicy( | |
| 235 SignedSettings::OPERATION_FAILED, | |
| 236 policy_blob_)); | |
| 237 | |
| 238 provider_->set_trusted_status(CrosSettingsProvider::TEMPORARILY_UNTRUSTED); | |
| 239 EXPECT_CALL(*this, GetTrustedCallback()); | |
| 240 EXPECT_EQ(CrosSettingsProvider::TEMPORARILY_UNTRUSTED, | |
| 241 provider_->PrepareTrustedValues( | |
| 242 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback, | |
| 243 base::Unretained(this)))); | |
| 244 provider_->Reload(); | |
| 245 } | |
| 246 | |
| 247 TEST_F(DeviceSettingsProviderTest, PolicyLoadNotification) { | |
| 248 provider_->set_trusted_status(CrosSettingsProvider::TEMPORARILY_UNTRUSTED); | |
| 249 EXPECT_CALL(*this, GetTrustedCallback()); | |
| 250 EXPECT_EQ(CrosSettingsProvider::TEMPORARILY_UNTRUSTED, | |
| 251 provider_->PrepareTrustedValues( | |
| 252 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback, | |
| 253 base::Unretained(this)))); | |
| 254 provider_->Reload(); | |
| 255 } | |
| 256 | |
| 257 } // namespace chromeos | |
| OLD | NEW |