| 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 "base/message_loop.h" |
| 8 #include "chrome/browser/chromeos/login/mock_signed_settings_helper.h" | 8 #include "chrome/browser/policy/device_policy_cache_test_base.h" |
| 9 #include "chrome/browser/policy/cloud_policy_data_store.h" | |
| 10 #include "chrome/browser/policy/enterprise_install_attributes.h" | |
| 11 #include "content/test/test_browser_thread.h" | 9 #include "content/test/test_browser_thread.h" |
| 12 #include "policy/policy_constants.h" | 10 #include "policy/policy_constants.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 13 |
| 16 namespace em = enterprise_management; | 14 namespace em = enterprise_management; |
| 17 | 15 |
| 16 using ::testing::InSequence; |
| 17 |
| 18 namespace policy { | 18 namespace policy { |
| 19 | 19 |
| 20 namespace { | |
| 21 | |
| 22 // Test registration user name. | |
| 23 const char kTestUser[] = "test@example.com"; | |
| 24 | |
| 25 using ::chromeos::SignedSettings; | |
| 26 using ::testing::InSequence; | |
| 27 using ::testing::_; | |
| 28 | |
| 29 void CreatePolicy(em::PolicyFetchResponse* policy, | |
| 30 const std::string& user, | |
| 31 em::ChromeDeviceSettingsProto& settings) { | |
| 32 // This method omits a few fields which currently aren't needed by tests: | |
| 33 // timestamp, machine_name, public key info. | |
| 34 em::PolicyData signed_response; | |
| 35 signed_response.set_username(user); | |
| 36 signed_response.set_request_token("dmtoken"); | |
| 37 signed_response.set_device_id("deviceid"); | |
| 38 EXPECT_TRUE( | |
| 39 settings.SerializeToString(signed_response.mutable_policy_value())); | |
| 40 std::string serialized_signed_response; | |
| 41 EXPECT_TRUE(signed_response.SerializeToString(&serialized_signed_response)); | |
| 42 policy->set_policy_data(serialized_signed_response); | |
| 43 } | |
| 44 | |
| 45 void CreateRefreshRatePolicy(em::PolicyFetchResponse* policy, | |
| 46 const std::string& user, | |
| 47 int refresh_rate) { | |
| 48 em::ChromeDeviceSettingsProto settings; | |
| 49 settings.mutable_device_policy_refresh_rate()-> | |
| 50 set_device_policy_refresh_rate(refresh_rate); | |
| 51 CreatePolicy(policy, user, settings); | |
| 52 } | |
| 53 | |
| 54 void CreateProxyPolicy(em::PolicyFetchResponse* policy, | |
| 55 const std::string& user, | |
| 56 const std::string& proxy_mode, | |
| 57 const std::string& proxy_server, | |
| 58 const std::string& proxy_pac_url, | |
| 59 const std::string& proxy_bypass_list) { | |
| 60 em::ChromeDeviceSettingsProto settings; | |
| 61 em::DeviceProxySettingsProto* proxy_settings = | |
| 62 settings.mutable_device_proxy_settings(); | |
| 63 proxy_settings->set_proxy_mode(proxy_mode); | |
| 64 proxy_settings->set_proxy_server(proxy_server); | |
| 65 proxy_settings->set_proxy_pac_url(proxy_pac_url); | |
| 66 proxy_settings->set_proxy_bypass_list(proxy_bypass_list); | |
| 67 CreatePolicy(policy, user, settings); | |
| 68 } | |
| 69 | |
| 70 } // namespace | |
| 71 | |
| 72 class DevicePolicyCacheTest : public testing::Test { | 20 class DevicePolicyCacheTest : public testing::Test { |
| 73 protected: | 21 protected: |
| 74 DevicePolicyCacheTest() | 22 DevicePolicyCacheTest() |
| 75 : cryptohome_(chromeos::CryptohomeLibrary::GetImpl(true)), | 23 : message_loop_(MessageLoop::TYPE_UI), |
| 76 install_attributes_(cryptohome_.get()), | |
| 77 message_loop_(MessageLoop::TYPE_UI), | |
| 78 ui_thread_(content::BrowserThread::UI, &message_loop_), | 24 ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 79 file_thread_(content::BrowserThread::FILE, &message_loop_) {} | 25 file_thread_(content::BrowserThread::FILE, &message_loop_) {} |
| 80 | 26 |
| 81 virtual void SetUp() { | 27 virtual void SetUp() OVERRIDE { |
| 82 data_store_.reset(CloudPolicyDataStore::CreateForUserPolicies()); | 28 cache_helper_.reset(new DevicePolicyCacheTestHelper()); |
| 83 cache_.reset(new DevicePolicyCache(data_store_.get(), | 29 cache_ = cache_helper_->cache(); |
| 84 &install_attributes_, | 30 ASSERT_TRUE(cache_); |
| 85 &signed_settings_helper_)); | |
| 86 } | 31 } |
| 87 | 32 |
| 88 virtual void TearDown() { | 33 virtual void TearDown() OVERRIDE { |
| 89 cache_.reset(); | 34 cache_helper_.reset(); |
| 90 } | |
| 91 | |
| 92 void MakeEnterpriseDevice(const char* registration_user) { | |
| 93 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, | |
| 94 install_attributes_.LockDevice( | |
| 95 registration_user, | |
| 96 DEVICE_MODE_ENTERPRISE, | |
| 97 std::string())); | |
| 98 } | 35 } |
| 99 | 36 |
| 100 const Value* GetPolicy(const char* policy_name) { | 37 const Value* GetPolicy(const char* policy_name) { |
| 101 return cache_->policy()->GetValue(policy_name); | 38 return cache_->policy()->GetValue(policy_name); |
| 102 } | 39 } |
| 103 | 40 |
| 104 scoped_ptr<chromeos::CryptohomeLibrary> cryptohome_; | |
| 105 EnterpriseInstallAttributes install_attributes_; | |
| 106 scoped_ptr<CloudPolicyDataStore> data_store_; | |
| 107 chromeos::MockSignedSettingsHelper signed_settings_helper_; | |
| 108 scoped_ptr<DevicePolicyCache> cache_; | |
| 109 | |
| 110 MessageLoop message_loop_; | 41 MessageLoop message_loop_; |
| 111 content::TestBrowserThread ui_thread_; | 42 content::TestBrowserThread ui_thread_; |
| 112 content::TestBrowserThread file_thread_; | 43 content::TestBrowserThread file_thread_; |
| 113 | 44 |
| 45 scoped_ptr<DevicePolicyCacheTestHelper> cache_helper_; |
| 46 DevicePolicyCache* cache_; |
| 47 |
| 114 private: | 48 private: |
| 115 DISALLOW_COPY_AND_ASSIGN(DevicePolicyCacheTest); | 49 DISALLOW_COPY_AND_ASSIGN(DevicePolicyCacheTest); |
| 116 }; | 50 }; |
| 117 | 51 |
| 118 TEST_F(DevicePolicyCacheTest, Startup) { | 52 TEST_F(DevicePolicyCacheTest, Startup) { |
| 119 em::PolicyFetchResponse policy; | 53 cache_helper_->SetRefreshRatePolicy(120); |
| 120 CreateRefreshRatePolicy(&policy, kTestUser, 120); | 54 EXPECT_TRUE(cache_helper_->LoadPolicy()); |
| 121 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( | |
| 122 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, | |
| 123 policy)); | |
| 124 cache_->Load(); | |
| 125 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); | |
| 126 base::FundamentalValue expected(120); | 55 base::FundamentalValue expected(120); |
| 127 EXPECT_TRUE(Value::Equals(&expected, | 56 EXPECT_TRUE(Value::Equals(&expected, |
| 128 GetPolicy(key::kDevicePolicyRefreshRate))); | 57 GetPolicy(key::kDevicePolicyRefreshRate))); |
| 129 } | 58 } |
| 130 | 59 |
| 131 TEST_F(DevicePolicyCacheTest, SetPolicy) { | 60 TEST_F(DevicePolicyCacheTest, SetPolicy) { |
| 132 InSequence s; | 61 cache_helper_->MakeEnterpriseDevice(); |
| 133 | |
| 134 MakeEnterpriseDevice(kTestUser); | |
| 135 | 62 |
| 136 // Startup. | 63 // Startup. |
| 137 em::PolicyFetchResponse policy; | 64 cache_helper_->SetRefreshRatePolicy(120); |
| 138 CreateRefreshRatePolicy(&policy, kTestUser, 120); | 65 EXPECT_TRUE(cache_helper_->LoadPolicy()); |
| 139 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( | |
| 140 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, | |
| 141 policy)); | |
| 142 cache_->Load(); | |
| 143 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); | |
| 144 base::FundamentalValue expected(120); | 66 base::FundamentalValue expected(120); |
| 145 EXPECT_TRUE(Value::Equals(&expected, | 67 EXPECT_TRUE(Value::Equals(&expected, |
| 146 GetPolicy(key::kDevicePolicyRefreshRate))); | 68 GetPolicy(key::kDevicePolicyRefreshRate))); |
| 147 | 69 |
| 148 // Set new policy information. | 70 // Set new policy information. |
| 149 em::PolicyFetchResponse new_policy; | 71 cache_helper_->SetRefreshRatePolicy(300); |
| 150 CreateRefreshRatePolicy(&new_policy, kTestUser, 300); | 72 EXPECT_TRUE(cache_helper_->SetPolicy(true)); |
| 151 EXPECT_CALL(signed_settings_helper_, StartStorePolicyOp(_, _)).WillOnce( | |
| 152 MockSignedSettingsHelperStorePolicy(chromeos::SignedSettings::SUCCESS)); | |
| 153 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( | |
| 154 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, | |
| 155 new_policy)); | |
| 156 EXPECT_TRUE(cache_->SetPolicy(new_policy)); | |
| 157 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); | |
| 158 base::FundamentalValue updated_expected(300); | 73 base::FundamentalValue updated_expected(300); |
| 159 EXPECT_TRUE(Value::Equals(&updated_expected, | 74 EXPECT_TRUE(Value::Equals(&updated_expected, |
| 160 GetPolicy(key::kDevicePolicyRefreshRate))); | 75 GetPolicy(key::kDevicePolicyRefreshRate))); |
| 161 } | 76 } |
| 162 | 77 |
| 163 TEST_F(DevicePolicyCacheTest, SetPolicyWrongUser) { | 78 TEST_F(DevicePolicyCacheTest, SetPolicyWrongUser) { |
| 164 InSequence s; | 79 cache_helper_->MakeEnterpriseDevice(); |
| 165 | |
| 166 MakeEnterpriseDevice(kTestUser); | |
| 167 | 80 |
| 168 // Startup. | 81 // Startup. |
| 169 em::PolicyFetchResponse policy; | 82 cache_helper_->SetRefreshRatePolicy(120); |
| 170 CreateRefreshRatePolicy(&policy, kTestUser, 120); | 83 EXPECT_TRUE(cache_helper_->LoadPolicy()); |
| 171 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( | |
| 172 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, | |
| 173 policy)); | |
| 174 cache_->Load(); | |
| 175 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); | |
| 176 | 84 |
| 177 // Set new policy information. This should fail due to invalid user. | 85 // Set new policy information. This should fail due to invalid user. |
| 178 em::PolicyFetchResponse new_policy; | 86 cache_helper_->SetUsername("foreign_user@example.com"); |
| 179 CreateRefreshRatePolicy(&new_policy, "foreign_user@example.com", 300); | 87 cache_helper_->SetRefreshRatePolicy(300); |
| 180 EXPECT_CALL(signed_settings_helper_, StartStorePolicyOp(_, _)).Times(0); | 88 EXPECT_FALSE(cache_helper_->SetPolicy(false)); |
| 181 EXPECT_FALSE(cache_->SetPolicy(new_policy)); | |
| 182 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); | |
| 183 | 89 |
| 184 base::FundamentalValue expected(120); | 90 base::FundamentalValue expected(120); |
| 185 EXPECT_TRUE(Value::Equals(&expected, | 91 EXPECT_TRUE(Value::Equals(&expected, |
| 186 GetPolicy(key::kDevicePolicyRefreshRate))); | 92 GetPolicy(key::kDevicePolicyRefreshRate))); |
| 187 } | 93 } |
| 188 | 94 |
| 189 TEST_F(DevicePolicyCacheTest, SetPolicyNonEnterpriseDevice) { | 95 TEST_F(DevicePolicyCacheTest, SetPolicyNonEnterpriseDevice) { |
| 190 InSequence s; | 96 // Startup. |
| 97 cache_helper_->SetRefreshRatePolicy(120); |
| 98 EXPECT_TRUE(cache_helper_->LoadPolicy()); |
| 191 | 99 |
| 192 // Startup. | 100 // Set new policy information. This should fail because the device is not |
| 193 em::PolicyFetchResponse policy; | 101 // enrolled. |
| 194 CreateRefreshRatePolicy(&policy, kTestUser, 120); | 102 cache_helper_->SetRefreshRatePolicy(300); |
| 195 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( | 103 EXPECT_FALSE(cache_helper_->SetPolicy(false)); |
| 196 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, | |
| 197 policy)); | |
| 198 cache_->Load(); | |
| 199 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); | |
| 200 | |
| 201 // Set new policy information. This should fail due to invalid user. | |
| 202 em::PolicyFetchResponse new_policy; | |
| 203 CreateRefreshRatePolicy(&new_policy, kTestUser, 120); | |
| 204 EXPECT_CALL(signed_settings_helper_, StartStorePolicyOp(_, _)).Times(0); | |
| 205 EXPECT_FALSE(cache_->SetPolicy(new_policy)); | |
| 206 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); | |
| 207 | 104 |
| 208 base::FundamentalValue expected(120); | 105 base::FundamentalValue expected(120); |
| 209 EXPECT_TRUE(Value::Equals(&expected, | 106 EXPECT_TRUE(Value::Equals(&expected, |
| 210 GetPolicy(key::kDevicePolicyRefreshRate))); | 107 GetPolicy(key::kDevicePolicyRefreshRate))); |
| 211 } | 108 } |
| 212 | 109 |
| 213 TEST_F(DevicePolicyCacheTest, SetProxyPolicy) { | 110 TEST_F(DevicePolicyCacheTest, SetProxyPolicy) { |
| 214 MakeEnterpriseDevice(kTestUser); | 111 cache_helper_->MakeEnterpriseDevice(); |
| 215 | 112 |
| 216 // Startup. | 113 // Startup. |
| 217 em::PolicyFetchResponse policy; | 114 cache_helper_->SetProxyPolicy("direct", "http://proxy:8080", |
| 218 CreateProxyPolicy(&policy, kTestUser, "direct", "http://proxy:8080", | 115 "http://proxy:8080/pac.js", |
| 219 "http://proxy:8080/pac.js", "127.0.0.1,example.com"); | 116 "127.0.0.1,example.com"); |
| 220 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( | 117 EXPECT_TRUE(cache_helper_->LoadPolicy()); |
| 221 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, | 118 |
| 222 policy)); | |
| 223 cache_->Load(); | |
| 224 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); | |
| 225 DictionaryValue expected; | 119 DictionaryValue expected; |
| 226 expected.SetString(key::kProxyMode, "direct"); | 120 expected.SetString(key::kProxyMode, "direct"); |
| 227 expected.SetString(key::kProxyServer, "http://proxy:8080"); | 121 expected.SetString(key::kProxyServer, "http://proxy:8080"); |
| 228 expected.SetString(key::kProxyPacUrl, "http://proxy:8080/pac.js"); | 122 expected.SetString(key::kProxyPacUrl, "http://proxy:8080/pac.js"); |
| 229 expected.SetString(key::kProxyBypassList, "127.0.0.1,example.com"); | 123 expected.SetString(key::kProxyBypassList, "127.0.0.1,example.com"); |
| 230 EXPECT_TRUE(Value::Equals(&expected, | 124 EXPECT_TRUE(Value::Equals(&expected, |
| 231 GetPolicy(key::kProxySettings))); | 125 GetPolicy(key::kProxySettings))); |
| 232 } | 126 } |
| 233 | 127 |
| 234 TEST_F(DevicePolicyCacheTest, SetDeviceNetworkConfigurationPolicy) { | 128 TEST_F(DevicePolicyCacheTest, SetDeviceNetworkConfigurationPolicy) { |
| 235 MakeEnterpriseDevice(kTestUser); | 129 cache_helper_->MakeEnterpriseDevice(); |
| 236 | 130 |
| 237 // Startup. | 131 // Startup. |
| 238 std::string fake_config("{ 'NetworkConfigurations': [] }"); | 132 std::string fake_config("{ 'NetworkConfigurations': [] }"); |
| 239 em::PolicyFetchResponse policy; | |
| 240 em::ChromeDeviceSettingsProto settings; | 133 em::ChromeDeviceSettingsProto settings; |
| 241 settings.mutable_open_network_configuration()->set_open_network_configuration( | 134 settings.mutable_open_network_configuration()->set_open_network_configuration( |
| 242 fake_config); | 135 fake_config); |
| 243 CreatePolicy(&policy, kTestUser, settings); | 136 cache_helper_->SetSettings(settings); |
| 244 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)).WillOnce( | 137 EXPECT_TRUE(cache_helper_->LoadPolicy()); |
| 245 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, | |
| 246 policy)); | |
| 247 cache_->Load(); | |
| 248 testing::Mock::VerifyAndClearExpectations(&signed_settings_helper_); | |
| 249 StringValue expected_config(fake_config); | 138 StringValue expected_config(fake_config); |
| 250 EXPECT_TRUE( | 139 EXPECT_TRUE(Value::Equals(&expected_config, |
| 251 Value::Equals(&expected_config, | 140 GetPolicy(key::kDeviceOpenNetworkConfiguration))); |
| 252 GetPolicy(key::kDeviceOpenNetworkConfiguration))); | |
| 253 } | 141 } |
| 254 | 142 |
| 255 } // namespace policy | 143 } // namespace policy |
| OLD | NEW |