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

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

Issue 9111022: Removed ConfigurationPolicyType and extended PolicyMap. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased 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/cloud_policy_provider.h" 5 #include "chrome/browser/policy/cloud_policy_provider.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "chrome/browser/policy/browser_policy_connector.h" 9 #include "chrome/browser/policy/browser_policy_connector.h"
10 #include "chrome/browser/policy/cloud_policy_cache_base.h" 10 #include "chrome/browser/policy/cloud_policy_cache_base.h"
11 #include "chrome/browser/policy/cloud_policy_provider_impl.h" 11 #include "chrome/browser/policy/cloud_policy_provider_impl.h"
12 #include "chrome/browser/policy/configuration_policy_provider.h" 12 #include "chrome/browser/policy/configuration_policy_provider.h"
13 #include "chrome/browser/policy/mock_configuration_policy_provider.h" 13 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
14 #include "policy/policy_constants.h" 14 #include "policy/policy_constants.h"
15 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
16 16
17 using testing::Mock; 17 using testing::Mock;
18 using testing::_; 18 using testing::_;
19 19
20 namespace em = enterprise_management; 20 namespace em = enterprise_management;
21 21
22 namespace policy { 22 namespace policy {
23 23
24 namespace {
25
26 // Utility function for tests.
27 void SetPolicy(PolicyMap* map, const char* policy, Value* value) {
28 map->Set(policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, value);
29 }
30
31 } // namespace
32
24 class MockCloudPolicyCache : public CloudPolicyCacheBase { 33 class MockCloudPolicyCache : public CloudPolicyCacheBase {
25 public: 34 public:
26 MockCloudPolicyCache() {} 35 MockCloudPolicyCache() {}
27 virtual ~MockCloudPolicyCache() {} 36 virtual ~MockCloudPolicyCache() {}
28 37
29 // CloudPolicyCacheBase implementation. 38 // CloudPolicyCacheBase implementation.
30 void Load() OVERRIDE {} 39 void Load() OVERRIDE {}
31 void SetPolicy(const em::PolicyFetchResponse& policy) OVERRIDE {} 40 void SetPolicy(const em::PolicyFetchResponse& policy) OVERRIDE {}
32 bool DecodePolicyData(const em::PolicyData& policy_data, 41 bool DecodePolicyData(const em::PolicyData& policy_data,
33 PolicyMap* mandatory, 42 PolicyMap* policies) OVERRIDE {
34 PolicyMap* recommended) OVERRIDE {
35 return true; 43 return true;
36 } 44 }
37 45
38 void SetUnmanaged() OVERRIDE { 46 void SetUnmanaged() OVERRIDE {
39 is_unmanaged_ = true; 47 is_unmanaged_ = true;
40 } 48 }
41 49
42 // Non-const accessors for underlying PolicyMaps. 50 PolicyMap* mutable_policy() {
43 PolicyMap* raw_mandatory_policy() { 51 return &policies_;
44 return &mandatory_policy_;
45 }
46
47 PolicyMap* raw_recommended_policy() {
48 return &recommended_policy_;
49 } 52 }
50 53
51 void set_initialized(bool initialized) { 54 void set_initialized(bool initialized) {
52 initialization_complete_ = initialized; 55 initialization_complete_ = initialized;
53 } 56 }
54 57
58 void Set(const char *name, Value* value) {
59 policies_.Set(name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, value);
60 }
61
55 private: 62 private:
56 DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyCache); 63 DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyCache);
57 }; 64 };
58 65
59 class CloudPolicyProviderTest : public testing::Test { 66 class CloudPolicyProviderTest : public testing::Test {
60 protected: 67 protected:
61 void CreateCloudPolicyProvider() { 68 void CreateCloudPolicyProvider() {
62 cloud_policy_provider_.reset( 69 cloud_policy_provider_.reset(
63 new CloudPolicyProviderImpl( 70 new CloudPolicyProviderImpl(&browser_policy_connector_,
64 &browser_policy_connector_, 71 GetChromePolicyDefinitionList(),
65 GetChromePolicyDefinitionList(), 72 POLICY_LEVEL_MANDATORY));
66 CloudPolicyCacheBase::POLICY_LEVEL_MANDATORY));
67 } 73 }
68 74
69 // Appends the caches to a provider and then provides the policies to 75 // Appends the caches to a provider and then provides the policies to
70 // |result|. 76 // |result|.
71 void RunCachesThroughProvider(MockCloudPolicyCache caches[], int n, 77 void RunCachesThroughProvider(MockCloudPolicyCache caches[], int n,
72 PolicyMap* result) { 78 PolicyMap* result) {
73 CloudPolicyProviderImpl provider( 79 CloudPolicyProviderImpl provider(
74 &browser_policy_connector_, 80 &browser_policy_connector_,
75 GetChromePolicyDefinitionList(), 81 GetChromePolicyDefinitionList(),
76 CloudPolicyCacheBase::POLICY_LEVEL_MANDATORY); 82 POLICY_LEVEL_MANDATORY);
77 for (int i = 0; i < n; i++) { 83 for (int i = 0; i < n; i++) {
78 provider.AppendCache(&caches[i]); 84 provider.AppendCache(&caches[i]);
79 } 85 }
80 provider.Provide(result); 86 provider.Provide(result);
81 } 87 }
82 88
83 void CombineTwoPolicyMaps(const PolicyMap& base, 89 void CombineTwoPolicyMaps(const PolicyMap& base,
84 const PolicyMap& overlay, 90 const PolicyMap& overlay,
85 PolicyMap* out_map) { 91 PolicyMap* out_map) {
86 MockCloudPolicyCache caches[2]; 92 MockCloudPolicyCache caches[2];
87 caches[0].raw_mandatory_policy()->CopyFrom(base); 93 caches[0].mutable_policy()->CopyFrom(base);
88 caches[0].set_initialized(true); 94 caches[0].set_initialized(true);
89 caches[1].raw_mandatory_policy()->CopyFrom(overlay); 95 caches[1].mutable_policy()->CopyFrom(overlay);
90 caches[1].set_initialized(true); 96 caches[1].set_initialized(true);
91 RunCachesThroughProvider(caches, 2, out_map); 97 RunCachesThroughProvider(caches, 2, out_map);
92 } 98 }
93 99
94 void FixDeprecatedPolicies(PolicyMap* policies) { 100 void FixDeprecatedPolicies(PolicyMap* policies) {
95 CloudPolicyProviderImpl::FixDeprecatedPolicies(policies); 101 CloudPolicyProviderImpl::FixDeprecatedPolicies(policies);
96 } 102 }
97 103
98 scoped_ptr<CloudPolicyProviderImpl> cloud_policy_provider_; 104 scoped_ptr<CloudPolicyProviderImpl> cloud_policy_provider_;
99 105
100 private: 106 private:
101 BrowserPolicyConnector browser_policy_connector_; 107 BrowserPolicyConnector browser_policy_connector_;
102 }; 108 };
103 109
104 // Proxy setting distributed over multiple caches. 110 // Proxy setting distributed over multiple caches.
105 TEST_F(CloudPolicyProviderTest, 111 TEST_F(CloudPolicyProviderTest,
106 ProxySettingDistributedOverMultipleCaches) { 112 ProxySettingDistributedOverMultipleCaches) {
107 // There are proxy_policy_count()+1 = 6 caches and they are mixed together by 113 // There are proxy_policy_count()+1 = 6 caches and they are mixed together by
108 // one instance of CloudPolicyProvider. The first cache has some policies but 114 // one instance of CloudPolicyProvider. The first cache has some policies but
109 // no proxy-related ones. The following caches have each one proxy-policy set. 115 // no proxy-related ones. The following caches have each one proxy-policy set.
110 const int n = 6; 116 const int n = 6;
111 MockCloudPolicyCache caches[n]; 117 MockCloudPolicyCache caches[n];
112 118
113 // Prepare |cache[0]| to serve some non-proxy policies. 119 // Prepare |cache[0]| to serve some non-proxy policies.
114 caches[0].raw_mandatory_policy()->Set(kPolicyShowHomeButton, 120 caches[0].Set(key::kShowHomeButton, Value::CreateBooleanValue(true));
115 Value::CreateBooleanValue(true)); 121 caches[0].Set(key::kIncognitoEnabled, Value::CreateBooleanValue(true));
116 caches[0].raw_mandatory_policy()->Set(kPolicyIncognitoEnabled, 122 caches[0].Set(key::kTranslateEnabled, Value::CreateBooleanValue(true));
117 Value::CreateBooleanValue(true));
118 caches[0].raw_mandatory_policy()->Set(kPolicyTranslateEnabled,
119 Value::CreateBooleanValue(true));
120 caches[0].set_initialized(true); 123 caches[0].set_initialized(true);
121 124
122 // Prepare the other caches to serve one proxy-policy each. 125 // Prepare the other caches to serve one proxy-policy each.
123 caches[1].raw_mandatory_policy()->Set(kPolicyProxyMode, 126 caches[1].Set(key::kProxyMode, Value::CreateStringValue("cache 1"));
124 Value::CreateStringValue("cache 1"));
125 caches[1].set_initialized(true); 127 caches[1].set_initialized(true);
126 caches[2].raw_mandatory_policy()->Set(kPolicyProxyServerMode, 128 caches[2].Set(key::kProxyServerMode, Value::CreateIntegerValue(2));
127 Value::CreateIntegerValue(2));
128 caches[2].set_initialized(true); 129 caches[2].set_initialized(true);
129 caches[3].raw_mandatory_policy()->Set(kPolicyProxyServer, 130 caches[3].Set(key::kProxyServer, Value::CreateStringValue("cache 3"));
130 Value::CreateStringValue("cache 3"));
131 caches[3].set_initialized(true); 131 caches[3].set_initialized(true);
132 caches[4].raw_mandatory_policy()->Set(kPolicyProxyPacUrl, 132 caches[4].Set(key::kProxyPacUrl, Value::CreateStringValue("cache 4"));
133 Value::CreateStringValue("cache 4"));
134 caches[4].set_initialized(true); 133 caches[4].set_initialized(true);
135 caches[5].raw_mandatory_policy()->Set(kPolicyProxyMode, 134 caches[5].Set(key::kProxyMode, Value::CreateStringValue("cache 5"));
136 Value::CreateStringValue("cache 5"));
137 caches[5].set_initialized(true); 135 caches[5].set_initialized(true);
138 136
139 PolicyMap policies; 137 PolicyMap policies;
140 RunCachesThroughProvider(caches, n, &policies); 138 RunCachesThroughProvider(caches, n, &policies);
141 139
142 // Verify expectations. 140 // Verify expectations.
143 EXPECT_TRUE(policies.Get(kPolicyProxyMode) == NULL); 141 EXPECT_TRUE(policies.Get(key::kProxyMode) == NULL);
144 EXPECT_TRUE(policies.Get(kPolicyProxyServerMode) == NULL); 142 EXPECT_TRUE(policies.Get(key::kProxyServerMode) == NULL);
145 EXPECT_TRUE(policies.Get(kPolicyProxyServer) == NULL); 143 EXPECT_TRUE(policies.Get(key::kProxyServer) == NULL);
146 EXPECT_TRUE(policies.Get(kPolicyProxyPacUrl) == NULL); 144 EXPECT_TRUE(policies.Get(key::kProxyPacUrl) == NULL);
147 145
148 const Value* value = policies.Get(kPolicyProxySettings); 146 const Value* value = policies.GetValue(key::kProxySettings);
149 ASSERT_TRUE(value != NULL); 147 ASSERT_TRUE(value != NULL);
150 ASSERT_TRUE(value->IsType(Value::TYPE_DICTIONARY)); 148 ASSERT_TRUE(value->IsType(Value::TYPE_DICTIONARY));
151 const DictionaryValue* settings = static_cast<const DictionaryValue*>(value); 149 const DictionaryValue* settings = static_cast<const DictionaryValue*>(value);
152 std::string mode; 150 std::string mode;
153 EXPECT_TRUE(settings->GetString(GetPolicyName(kPolicyProxyMode), &mode)); 151 EXPECT_TRUE(settings->GetString(key::kProxyMode, &mode));
154 EXPECT_EQ("cache 1", mode); 152 EXPECT_EQ("cache 1", mode);
155 153
156 base::FundamentalValue expected(true); 154 base::FundamentalValue expected(true);
157 EXPECT_TRUE(base::Value::Equals(&expected, 155 EXPECT_TRUE(base::Value::Equals(&expected,
158 policies.Get(kPolicyShowHomeButton))); 156 policies.GetValue(key::kShowHomeButton)));
159 EXPECT_TRUE(base::Value::Equals(&expected, 157 EXPECT_TRUE(base::Value::Equals(&expected,
160 policies.Get(kPolicyIncognitoEnabled))); 158 policies.GetValue(key::kIncognitoEnabled)));
161 EXPECT_TRUE(base::Value::Equals(&expected, 159 EXPECT_TRUE(base::Value::Equals(&expected,
162 policies.Get(kPolicyTranslateEnabled))); 160 policies.GetValue(key::kTranslateEnabled)));
163 } 161 }
164 162
165 // Combining two PolicyMaps. 163 // Combining two PolicyMaps.
166 TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsSame) { 164 TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsSame) {
167 PolicyMap A, B, C; 165 PolicyMap A, B, C;
168 A.Set(kPolicyHomepageLocation, 166 SetPolicy(&A, key::kHomepageLocation,
169 Value::CreateStringValue("http://www.chromium.org")); 167 Value::CreateStringValue("http://www.chromium.org"));
170 B.Set(kPolicyHomepageLocation, 168 SetPolicy(&B, key::kHomepageLocation,
171 Value::CreateStringValue("http://www.google.com")); 169 Value::CreateStringValue("http://www.google.com"));
172 A.Set(kPolicyApplicationLocaleValue, Value::CreateStringValue("hu")); 170 SetPolicy(&A, key::kApplicationLocaleValue, Value::CreateStringValue("hu"));
173 B.Set(kPolicyApplicationLocaleValue, Value::CreateStringValue("us")); 171 SetPolicy(&B, key::kApplicationLocaleValue, Value::CreateStringValue("us"));
174 A.Set(kPolicyDevicePolicyRefreshRate, new base::FundamentalValue(100)); 172 SetPolicy(&A, key::kDevicePolicyRefreshRate, new base::FundamentalValue(100));
175 B.Set(kPolicyDevicePolicyRefreshRate, new base::FundamentalValue(200)); 173 SetPolicy(&B, key::kDevicePolicyRefreshRate, new base::FundamentalValue(200));
176 CombineTwoPolicyMaps(A, B, &C); 174 CombineTwoPolicyMaps(A, B, &C);
177 EXPECT_TRUE(A.Equals(C)); 175 EXPECT_TRUE(A.Equals(C));
178 } 176 }
179 177
180 TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsEmpty) { 178 TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsEmpty) {
181 PolicyMap A, B, C; 179 PolicyMap A, B, C;
182 CombineTwoPolicyMaps(A, B, &C); 180 CombineTwoPolicyMaps(A, B, &C);
183 EXPECT_TRUE(C.empty()); 181 EXPECT_TRUE(C.empty());
184 } 182 }
185 183
186 TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsPartial) { 184 TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsPartial) {
187 PolicyMap A, B, C; 185 PolicyMap A, B, C;
188 186
189 A.Set(kPolicyHomepageLocation, 187 SetPolicy(&A, key::kHomepageLocation,
190 Value::CreateStringValue("http://www.chromium.org")); 188 Value::CreateStringValue("http://www.chromium.org"));
191 B.Set(kPolicyHomepageLocation, 189 SetPolicy(&B, key::kHomepageLocation,
192 Value::CreateStringValue("http://www.google.com")); 190 Value::CreateStringValue("http://www.google.com"));
193 B.Set(kPolicyApplicationLocaleValue, Value::CreateStringValue("us")); 191 SetPolicy(&B, key::kApplicationLocaleValue, Value::CreateStringValue("us"));
194 A.Set(kPolicyDevicePolicyRefreshRate, new base::FundamentalValue(100)); 192 SetPolicy(&A, key::kDevicePolicyRefreshRate, new base::FundamentalValue(100));
195 B.Set(kPolicyDevicePolicyRefreshRate, new base::FundamentalValue(200)); 193 SetPolicy(&B, key::kDevicePolicyRefreshRate, new base::FundamentalValue(200));
196 CombineTwoPolicyMaps(A, B, &C); 194 CombineTwoPolicyMaps(A, B, &C);
197 195
198 const Value* value; 196 const Value* value;
199 std::string string_value; 197 std::string string_value;
200 int int_value; 198 int int_value;
201 value = C.Get(kPolicyHomepageLocation); 199 value = C.GetValue(key::kHomepageLocation);
202 ASSERT_TRUE(NULL != value); 200 ASSERT_TRUE(NULL != value);
203 EXPECT_TRUE(value->GetAsString(&string_value)); 201 EXPECT_TRUE(value->GetAsString(&string_value));
204 EXPECT_EQ("http://www.chromium.org", string_value); 202 EXPECT_EQ("http://www.chromium.org", string_value);
205 value = C.Get(kPolicyApplicationLocaleValue); 203 value = C.GetValue(key::kApplicationLocaleValue);
206 ASSERT_TRUE(NULL != value); 204 ASSERT_TRUE(NULL != value);
207 EXPECT_TRUE(value->GetAsString(&string_value)); 205 EXPECT_TRUE(value->GetAsString(&string_value));
208 EXPECT_EQ("us", string_value); 206 EXPECT_EQ("us", string_value);
209 value = C.Get(kPolicyDevicePolicyRefreshRate); 207 value = C.GetValue(key::kDevicePolicyRefreshRate);
210 ASSERT_TRUE(NULL != value); 208 ASSERT_TRUE(NULL != value);
211 EXPECT_TRUE(value->GetAsInteger(&int_value)); 209 EXPECT_TRUE(value->GetAsInteger(&int_value));
212 EXPECT_EQ(100, int_value); 210 EXPECT_EQ(100, int_value);
213 } 211 }
214 212
215 TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsProxies) { 213 TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsProxies) {
216 const int a_value = 1; 214 const int a_value = 1;
217 const int b_value = -1; 215 const int b_value = -1;
218 PolicyMap A, B, C; 216 PolicyMap A, B, C;
219 217
220 A.Set(kPolicyProxyMode, Value::CreateIntegerValue(a_value)); 218 SetPolicy(&A, key::kProxyMode, Value::CreateIntegerValue(a_value));
221 219 SetPolicy(&B, key::kProxyServerMode, Value::CreateIntegerValue(b_value));
222 B.Set(kPolicyProxyServerMode, Value::CreateIntegerValue(b_value)); 220 SetPolicy(&B, key::kProxyServer, Value::CreateIntegerValue(b_value));
223 B.Set(kPolicyProxyServer, Value::CreateIntegerValue(b_value)); 221 SetPolicy(&B, key::kProxyPacUrl, Value::CreateIntegerValue(b_value));
224 B.Set(kPolicyProxyPacUrl, Value::CreateIntegerValue(b_value)); 222 SetPolicy(&B, key::kProxyBypassList, Value::CreateIntegerValue(b_value));
225 B.Set(kPolicyProxyBypassList, Value::CreateIntegerValue(b_value));
226 223
227 CombineTwoPolicyMaps(A, B, &C); 224 CombineTwoPolicyMaps(A, B, &C);
228 225
229 FixDeprecatedPolicies(&A); 226 FixDeprecatedPolicies(&A);
230 FixDeprecatedPolicies(&B); 227 FixDeprecatedPolicies(&B);
231 EXPECT_TRUE(A.Equals(C)); 228 EXPECT_TRUE(A.Equals(C));
232 EXPECT_FALSE(B.Equals(C)); 229 EXPECT_FALSE(B.Equals(C));
233 } 230 }
234 231
235 TEST_F(CloudPolicyProviderTest, RefreshPolicies) { 232 TEST_F(CloudPolicyProviderTest, RefreshPolicies) {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 cloud_policy_provider_->OnCacheUpdate(&cache0); 283 cloud_policy_provider_->OnCacheUpdate(&cache0);
287 Mock::VerifyAndClearExpectations(&observer); 284 Mock::VerifyAndClearExpectations(&observer);
288 285
289 // Fire updates if one of the required caches goes away while waiting. 286 // Fire updates if one of the required caches goes away while waiting.
290 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(1); 287 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(1);
291 cloud_policy_provider_->OnCacheGoingAway(&cache2); 288 cloud_policy_provider_->OnCacheGoingAway(&cache2);
292 Mock::VerifyAndClearExpectations(&observer); 289 Mock::VerifyAndClearExpectations(&observer);
293 } 290 }
294 291
295 } // namespace policy 292 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/cloud_policy_provider_impl.cc ('k') | chrome/browser/policy/cloud_policy_subsystem_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698