Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: chrome/browser/policy/device_policy_cache_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698