| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/policy/device_policy_cache.h" | 5 #include "chrome/browser/policy/device_policy_cache.h" |
| 6 | 6 |
| 7 #include "chrome/browser/chromeos/cros/cryptohome_library.h" | 7 #include "chrome/browser/chromeos/cros/cryptohome_library.h" |
| 8 #include "chrome/browser/chromeos/login/mock_signed_settings_helper.h" | 8 #include "chrome/browser/chromeos/login/mock_signed_settings_helper.h" |
| 9 #include "chrome/browser/policy/cloud_policy_data_store.h" | 9 #include "chrome/browser/policy/cloud_policy_data_store.h" |
| 10 #include "chrome/browser/policy/enterprise_install_attributes.h" | 10 #include "chrome/browser/policy/enterprise_install_attributes.h" |
| 11 #include "content/test/test_browser_thread.h" | 11 #include "content/test/test_browser_thread.h" |
| 12 #include "policy/configuration_policy_type.h" | 12 #include "policy/policy_constants.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 namespace em = enterprise_management; | 16 namespace em = enterprise_management; |
| 17 | 17 |
| 18 namespace policy { | 18 namespace policy { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 // Test registration user name. | 22 // Test registration user name. |
| 23 const char kTestUser[] = "test@example.com"; | 23 const char kTestUser[] = "test@example.com"; |
| 24 | 24 |
| 25 using ::chromeos::SignedSettings; | 25 using ::chromeos::SignedSettings; |
| 26 using ::testing::InSequence; | 26 using ::testing::InSequence; |
| 27 using ::testing::_; | 27 using ::testing::_; |
| 28 | 28 |
| 29 void CreatePolicy(em::PolicyFetchResponse* policy, | 29 void CreatePolicy(em::PolicyFetchResponse* policy, |
| 30 const std::string& user, | 30 const std::string& user, |
| 31 em::ChromeDeviceSettingsProto& settings) { | 31 em::ChromeDeviceSettingsProto& settings) { |
| 32 // This method omits a few fields which currently aren't needed by tests: | 32 // This method omits a few fields which currently aren't needed by tests: |
| 33 // timestamp, machine_name, policy_type, public key info. | 33 // timestamp, machine_name, public key info. |
| 34 em::PolicyData signed_response; | 34 em::PolicyData signed_response; |
| 35 signed_response.set_username(user); | 35 signed_response.set_username(user); |
| 36 signed_response.set_request_token("dmtoken"); | 36 signed_response.set_request_token("dmtoken"); |
| 37 signed_response.set_device_id("deviceid"); | 37 signed_response.set_device_id("deviceid"); |
| 38 EXPECT_TRUE( | 38 EXPECT_TRUE( |
| 39 settings.SerializeToString(signed_response.mutable_policy_value())); | 39 settings.SerializeToString(signed_response.mutable_policy_value())); |
| 40 std::string serialized_signed_response; | 40 std::string serialized_signed_response; |
| 41 EXPECT_TRUE(signed_response.SerializeToString(&serialized_signed_response)); | 41 EXPECT_TRUE(signed_response.SerializeToString(&serialized_signed_response)); |
| 42 policy->set_policy_data(serialized_signed_response); | 42 policy->set_policy_data(serialized_signed_response); |
| 43 } | 43 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 | 87 |
| 88 virtual void TearDown() { | 88 virtual void TearDown() { |
| 89 cache_.reset(); | 89 cache_.reset(); |
| 90 } | 90 } |
| 91 | 91 |
| 92 void MakeEnterpriseDevice(const char* registration_user) { | 92 void MakeEnterpriseDevice(const char* registration_user) { |
| 93 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, | 93 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, |
| 94 install_attributes_.LockDevice(registration_user)); | 94 install_attributes_.LockDevice(registration_user)); |
| 95 } | 95 } |
| 96 | 96 |
| 97 const Value* GetMandatoryPolicy(ConfigurationPolicyType policy) { | 97 const Value* GetPolicy(const char* policy_name) { |
| 98 return cache_->mandatory_policy_.Get(policy); | 98 return cache_->policy()->GetValue(policy_name); |
| 99 } | |
| 100 | |
| 101 const Value* GetRecommendedPolicy(ConfigurationPolicyType policy) { | |
| 102 return cache_->recommended_policy_.Get(policy); | |
| 103 } | 99 } |
| 104 | 100 |
| 105 scoped_ptr<chromeos::CryptohomeLibrary> cryptohome_; | 101 scoped_ptr<chromeos::CryptohomeLibrary> cryptohome_; |
| 106 EnterpriseInstallAttributes install_attributes_; | 102 EnterpriseInstallAttributes install_attributes_; |
| 107 scoped_ptr<CloudPolicyDataStore> data_store_; | 103 scoped_ptr<CloudPolicyDataStore> data_store_; |
| 108 chromeos::MockSignedSettingsHelper signed_settings_helper_; | 104 chromeos::MockSignedSettingsHelper signed_settings_helper_; |
| 109 scoped_ptr<DevicePolicyCache> cache_; | 105 scoped_ptr<DevicePolicyCache> cache_; |
| 110 | 106 |
| 111 MessageLoop message_loop_; | 107 MessageLoop message_loop_; |
| 112 content::TestBrowserThread ui_thread_; | 108 content::TestBrowserThread ui_thread_; |
| 113 content::TestBrowserThread file_thread_; | 109 content::TestBrowserThread file_thread_; |
| 114 | 110 |
| 115 private: | 111 private: |
| 116 DISALLOW_COPY_AND_ASSIGN(DevicePolicyCacheTest); | 112 DISALLOW_COPY_AND_ASSIGN(DevicePolicyCacheTest); |
| 117 }; | 113 }; |
| 118 | 114 |
| 119 TEST_F(DevicePolicyCacheTest, Startup) { | 115 TEST_F(DevicePolicyCacheTest, Startup) { |
| 120 em::PolicyFetchResponse policy; | 116 em::PolicyFetchResponse policy; |
| 121 CreateRefreshRatePolicy(&policy, kTestUser, 120); | 117 CreateRefreshRatePolicy(&policy, kTestUser, 120); |
| 122 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( | 118 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( |
| 123 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, | 119 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, |
| 124 policy)); | 120 policy)); |
| 125 cache_->Load(); | 121 cache_->Load(); |
| 126 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); | 122 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); |
| 127 base::FundamentalValue expected(120); | 123 base::FundamentalValue expected(120); |
| 128 EXPECT_TRUE(Value::Equals(&expected, | 124 EXPECT_TRUE(Value::Equals(&expected, |
| 129 GetMandatoryPolicy( | 125 GetPolicy(key::kDevicePolicyRefreshRate))); |
| 130 kPolicyDevicePolicyRefreshRate))); | |
| 131 } | 126 } |
| 132 | 127 |
| 133 TEST_F(DevicePolicyCacheTest, SetPolicy) { | 128 TEST_F(DevicePolicyCacheTest, SetPolicy) { |
| 134 InSequence s; | 129 InSequence s; |
| 135 | 130 |
| 136 MakeEnterpriseDevice(kTestUser); | 131 MakeEnterpriseDevice(kTestUser); |
| 137 | 132 |
| 138 // Startup. | 133 // Startup. |
| 139 em::PolicyFetchResponse policy; | 134 em::PolicyFetchResponse policy; |
| 140 CreateRefreshRatePolicy(&policy, kTestUser, 120); | 135 CreateRefreshRatePolicy(&policy, kTestUser, 120); |
| 141 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( | 136 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( |
| 142 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, | 137 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, |
| 143 policy)); | 138 policy)); |
| 144 cache_->Load(); | 139 cache_->Load(); |
| 145 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); | 140 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); |
| 146 base::FundamentalValue expected(120); | 141 base::FundamentalValue expected(120); |
| 147 EXPECT_TRUE(Value::Equals(&expected, | 142 EXPECT_TRUE(Value::Equals(&expected, |
| 148 GetMandatoryPolicy( | 143 GetPolicy(key::kDevicePolicyRefreshRate))); |
| 149 kPolicyDevicePolicyRefreshRate))); | |
| 150 | 144 |
| 151 // Set new policy information. | 145 // Set new policy information. |
| 152 em::PolicyFetchResponse new_policy; | 146 em::PolicyFetchResponse new_policy; |
| 153 CreateRefreshRatePolicy(&new_policy, kTestUser, 300); | 147 CreateRefreshRatePolicy(&new_policy, kTestUser, 300); |
| 154 EXPECT_CALL(signed_settings_helper_, StartStorePolicyOp(_, _)).WillOnce( | 148 EXPECT_CALL(signed_settings_helper_, StartStorePolicyOp(_, _)).WillOnce( |
| 155 MockSignedSettingsHelperStorePolicy(chromeos::SignedSettings::SUCCESS)); | 149 MockSignedSettingsHelperStorePolicy(chromeos::SignedSettings::SUCCESS)); |
| 156 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( | 150 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( |
| 157 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, | 151 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, |
| 158 new_policy)); | 152 new_policy)); |
| 159 cache_->SetPolicy(new_policy); | 153 cache_->SetPolicy(new_policy); |
| 160 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); | 154 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); |
| 161 base::FundamentalValue updated_expected(300); | 155 base::FundamentalValue updated_expected(300); |
| 162 EXPECT_TRUE(Value::Equals(&updated_expected, | 156 EXPECT_TRUE(Value::Equals(&updated_expected, |
| 163 GetMandatoryPolicy( | 157 GetPolicy(key::kDevicePolicyRefreshRate))); |
| 164 kPolicyDevicePolicyRefreshRate))); | |
| 165 } | 158 } |
| 166 | 159 |
| 167 TEST_F(DevicePolicyCacheTest, SetPolicyWrongUser) { | 160 TEST_F(DevicePolicyCacheTest, SetPolicyWrongUser) { |
| 168 InSequence s; | 161 InSequence s; |
| 169 | 162 |
| 170 MakeEnterpriseDevice(kTestUser); | 163 MakeEnterpriseDevice(kTestUser); |
| 171 | 164 |
| 172 // Startup. | 165 // Startup. |
| 173 em::PolicyFetchResponse policy; | 166 em::PolicyFetchResponse policy; |
| 174 CreateRefreshRatePolicy(&policy, kTestUser, 120); | 167 CreateRefreshRatePolicy(&policy, kTestUser, 120); |
| 175 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( | 168 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( |
| 176 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, | 169 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, |
| 177 policy)); | 170 policy)); |
| 178 cache_->Load(); | 171 cache_->Load(); |
| 179 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); | 172 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); |
| 180 | 173 |
| 181 // Set new policy information. This should fail due to invalid user. | 174 // Set new policy information. This should fail due to invalid user. |
| 182 em::PolicyFetchResponse new_policy; | 175 em::PolicyFetchResponse new_policy; |
| 183 CreateRefreshRatePolicy(&new_policy, "foreign_user@example.com", 300); | 176 CreateRefreshRatePolicy(&new_policy, "foreign_user@example.com", 300); |
| 184 EXPECT_CALL(signed_settings_helper_, StartStorePolicyOp(_, _)).Times(0); | 177 EXPECT_CALL(signed_settings_helper_, StartStorePolicyOp(_, _)).Times(0); |
| 185 cache_->SetPolicy(new_policy); | 178 cache_->SetPolicy(new_policy); |
| 186 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); | 179 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); |
| 187 | 180 |
| 188 base::FundamentalValue expected(120); | 181 base::FundamentalValue expected(120); |
| 189 EXPECT_TRUE(Value::Equals(&expected, | 182 EXPECT_TRUE(Value::Equals(&expected, |
| 190 GetMandatoryPolicy( | 183 GetPolicy(key::kDevicePolicyRefreshRate))); |
| 191 kPolicyDevicePolicyRefreshRate))); | |
| 192 } | 184 } |
| 193 | 185 |
| 194 TEST_F(DevicePolicyCacheTest, SetPolicyNonEnterpriseDevice) { | 186 TEST_F(DevicePolicyCacheTest, SetPolicyNonEnterpriseDevice) { |
| 195 InSequence s; | 187 InSequence s; |
| 196 | 188 |
| 197 // Startup. | 189 // Startup. |
| 198 em::PolicyFetchResponse policy; | 190 em::PolicyFetchResponse policy; |
| 199 CreateRefreshRatePolicy(&policy, kTestUser, 120); | 191 CreateRefreshRatePolicy(&policy, kTestUser, 120); |
| 200 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( | 192 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( |
| 201 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, | 193 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, |
| 202 policy)); | 194 policy)); |
| 203 cache_->Load(); | 195 cache_->Load(); |
| 204 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); | 196 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); |
| 205 | 197 |
| 206 // Set new policy information. This should fail due to invalid user. | 198 // Set new policy information. This should fail due to invalid user. |
| 207 em::PolicyFetchResponse new_policy; | 199 em::PolicyFetchResponse new_policy; |
| 208 CreateRefreshRatePolicy(&new_policy, kTestUser, 120); | 200 CreateRefreshRatePolicy(&new_policy, kTestUser, 120); |
| 209 EXPECT_CALL(signed_settings_helper_, StartStorePolicyOp(_, _)).Times(0); | 201 EXPECT_CALL(signed_settings_helper_, StartStorePolicyOp(_, _)).Times(0); |
| 210 cache_->SetPolicy(new_policy); | 202 cache_->SetPolicy(new_policy); |
| 211 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); | 203 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); |
| 212 | 204 |
| 213 base::FundamentalValue expected(120); | 205 base::FundamentalValue expected(120); |
| 214 EXPECT_TRUE(Value::Equals(&expected, | 206 EXPECT_TRUE(Value::Equals(&expected, |
| 215 GetMandatoryPolicy( | 207 GetPolicy(key::kDevicePolicyRefreshRate))); |
| 216 kPolicyDevicePolicyRefreshRate))); | |
| 217 } | 208 } |
| 218 | 209 |
| 219 TEST_F(DevicePolicyCacheTest, SetProxyPolicy) { | 210 TEST_F(DevicePolicyCacheTest, SetProxyPolicy) { |
| 220 MakeEnterpriseDevice(kTestUser); | 211 MakeEnterpriseDevice(kTestUser); |
| 221 | 212 |
| 222 // Startup. | 213 // Startup. |
| 223 em::PolicyFetchResponse policy; | 214 em::PolicyFetchResponse policy; |
| 224 CreateProxyPolicy(&policy, kTestUser, "direct", "http://proxy:8080", | 215 CreateProxyPolicy(&policy, kTestUser, "direct", "http://proxy:8080", |
| 225 "http://proxy:8080/pac.js", "127.0.0.1,example.com"); | 216 "http://proxy:8080/pac.js", "127.0.0.1,example.com"); |
| 226 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( | 217 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( |
| 227 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, | 218 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, |
| 228 policy)); | 219 policy)); |
| 229 cache_->Load(); | 220 cache_->Load(); |
| 230 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); | 221 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); |
| 231 StringValue expected_proxy_mode("direct"); | 222 DictionaryValue expected; |
| 232 StringValue expected_proxy_server("http://proxy:8080"); | 223 expected.SetString(key::kProxyMode, "direct"); |
| 233 StringValue expected_proxy_pac_url("http://proxy:8080/pac.js"); | 224 expected.SetString(key::kProxyServer, "http://proxy:8080"); |
| 234 StringValue expected_proxy_bypass_list("127.0.0.1,example.com"); | 225 expected.SetString(key::kProxyPacUrl, "http://proxy:8080/pac.js"); |
| 235 EXPECT_TRUE(Value::Equals(&expected_proxy_mode, | 226 expected.SetString(key::kProxyBypassList, "127.0.0.1,example.com"); |
| 236 GetRecommendedPolicy(kPolicyProxyMode))); | 227 EXPECT_TRUE(Value::Equals(&expected, |
| 237 EXPECT_TRUE(Value::Equals(&expected_proxy_server, | 228 GetPolicy(key::kProxySettings))); |
| 238 GetRecommendedPolicy(kPolicyProxyServer))); | |
| 239 EXPECT_TRUE(Value::Equals(&expected_proxy_pac_url, | |
| 240 GetRecommendedPolicy(kPolicyProxyPacUrl))); | |
| 241 EXPECT_TRUE(Value::Equals(&expected_proxy_bypass_list, | |
| 242 GetRecommendedPolicy(kPolicyProxyBypassList))); | |
| 243 } | 229 } |
| 244 | 230 |
| 245 TEST_F(DevicePolicyCacheTest, SetDeviceNetworkConfigurationPolicy) { | 231 TEST_F(DevicePolicyCacheTest, SetDeviceNetworkConfigurationPolicy) { |
| 246 MakeEnterpriseDevice(kTestUser); | 232 MakeEnterpriseDevice(kTestUser); |
| 247 | 233 |
| 248 // Startup. | 234 // Startup. |
| 249 std::string fake_config("{ 'NetworkConfigurations': [] }"); | 235 std::string fake_config("{ 'NetworkConfigurations': [] }"); |
| 250 em::PolicyFetchResponse policy; | 236 em::PolicyFetchResponse policy; |
| 251 em::ChromeDeviceSettingsProto settings; | 237 em::ChromeDeviceSettingsProto settings; |
| 252 settings.mutable_open_network_configuration()->set_open_network_configuration( | 238 settings.mutable_open_network_configuration()->set_open_network_configuration( |
| 253 fake_config); | 239 fake_config); |
| 254 CreatePolicy(&policy, kTestUser, settings); | 240 CreatePolicy(&policy, kTestUser, settings); |
| 255 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( | 241 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( |
| 256 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, | 242 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, |
| 257 policy)); | 243 policy)); |
| 258 cache_->Load(); | 244 cache_->Load(); |
| 259 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); | 245 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); |
| 260 StringValue expected_config(fake_config); | 246 StringValue expected_config(fake_config); |
| 261 EXPECT_TRUE( | 247 EXPECT_TRUE( |
| 262 Value::Equals(&expected_config, | 248 Value::Equals(&expected_config, |
| 263 GetMandatoryPolicy(kPolicyDeviceOpenNetworkConfiguration))); | 249 GetPolicy(key::kDeviceOpenNetworkConfiguration))); |
| 264 } | 250 } |
| 265 | 251 |
| 266 } // namespace policy | 252 } // namespace policy |
| OLD | NEW |