OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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/policy/cloud_policy_provider.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/values.h" | |
9 #include "chrome/browser/policy/cloud_policy_cache_base.h" | |
10 #include "chrome/browser/policy/cloud_policy_provider_impl.h" | |
11 #include "chrome/browser/policy/configuration_policy_pref_store.h" | |
12 #include "chrome/browser/policy/mock_configuration_policy_store.h" | |
13 #include "testing/gmock/include/gmock/gmock.h" | |
14 | |
15 using testing::AnyNumber; | |
16 using testing::_; | |
17 | |
18 namespace policy { | |
19 | |
20 class MockCloudPolicyCache : public CloudPolicyCacheBase { | |
21 public: | |
22 MockCloudPolicyCache() {} | |
23 virtual ~MockCloudPolicyCache() {} | |
24 | |
25 // CloudPolicyCacheBase implementation. | |
26 void Load() {} | |
27 void SetPolicy(const em::PolicyFetchResponse& policy) {} | |
28 bool DecodePolicyData(const em::PolicyData& policy_data, | |
29 PolicyMap* mandatory, | |
30 PolicyMap* recommended) { | |
31 return true; | |
32 } | |
33 bool IsReady() { | |
34 return true; | |
35 } | |
36 | |
37 // Non-const accessors for underlying PolicyMaps. | |
38 PolicyMap* raw_mandatory_policy() { | |
39 return &mandatory_policy_; | |
40 } | |
41 | |
42 PolicyMap* raw_recommended_policy() { | |
43 return &recommended_policy_; | |
44 } | |
45 | |
46 void SetUnmanaged() { | |
47 is_unmanaged_ = true; | |
48 } | |
49 | |
50 void set_initialized(bool initialized) { | |
51 initialization_complete_ = initialized; | |
52 } | |
53 | |
54 private: | |
55 DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyCache); | |
56 }; | |
57 | |
58 class CloudPolicyProviderTest : public testing::Test { | |
59 protected: | |
60 void CreateCloudPolicyProvider(CloudPolicyCacheBase::PolicyLevel level) { | |
61 cloud_policy_provider_.reset(new CloudPolicyProviderImpl( | |
62 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), level)); | |
63 } | |
64 | |
65 // Appends the caches to a provider and then provides the policies to | |
66 // |store_|. | |
67 void RunCachesThroughProvider(MockCloudPolicyCache caches[], int n, | |
Joao da Silva
2011/06/29 11:36:17
Nit: maybe use a vector for |caches|, but this is
gfeher
2011/06/29 12:53:07
I'll leave it like this, mostly because I am lazy,
| |
68 CloudPolicyCacheBase::PolicyLevel level) { | |
69 store_.reset(new MockConfigurationPolicyStore); | |
70 CloudPolicyProviderImpl provider( | |
71 policy::ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), | |
72 level); | |
73 for (int i = 0; i < n; i++) { | |
74 provider.AppendCache(&caches[i]); | |
75 } | |
76 EXPECT_CALL(*store_.get(), Apply(_, _)).Times(AnyNumber()); | |
77 provider.Provide(store_.get()); | |
78 } | |
79 | |
80 // Checks a string policy in |store_|. | |
81 void ExpectStringPolicy(const std::string& expected, | |
82 ConfigurationPolicyType type) { | |
83 const Value* value = store_->Get(type); | |
84 std::string string_value; | |
85 EXPECT_TRUE(value != NULL); | |
Joao da Silva
2011/06/29 11:36:17
Nit: the next line will segfault when this test fa
gfeher
2011/06/29 12:53:07
Done.
| |
86 EXPECT_TRUE(value->GetAsString(&string_value)); | |
87 EXPECT_EQ(expected, string_value); | |
88 } | |
89 | |
90 // Checks a boolean policy in |store_|. | |
91 void ExpectBoolPolicy(bool expected, ConfigurationPolicyType type) { | |
92 const Value* value = store_->Get(type); | |
93 bool bool_value; | |
94 EXPECT_TRUE(value != NULL); | |
Joao da Silva
2011/06/29 11:36:17
Nit: same as above.
gfeher
2011/06/29 12:53:07
Done.
| |
95 EXPECT_TRUE(value->GetAsBoolean(&bool_value)); | |
96 EXPECT_EQ(expected, bool_value); | |
97 } | |
98 | |
99 void ExpectNoPolicy(ConfigurationPolicyType type) { | |
100 EXPECT_TRUE(NULL == store_->Get(type)); | |
101 } | |
102 | |
103 void CombineTwoPolicyMaps(const PolicyMap& base, | |
104 const PolicyMap& overlay, | |
105 PolicyMap* out_map) { | |
106 DCHECK(cloud_policy_provider_.get()); | |
107 cloud_policy_provider_->CombineTwoPolicyMaps(base, overlay, out_map); | |
108 } | |
109 | |
110 private: | |
111 // Some tests need a list of policies that doesn't contain any proxy | |
112 // policies. Note: these policies will be handled as if they had the | |
113 // type of Value::TYPE_INTEGER. | |
114 static const ConfigurationPolicyType simple_policies[]; | |
115 | |
116 scoped_ptr<CloudPolicyProviderImpl> cloud_policy_provider_; | |
117 scoped_ptr<MockConfigurationPolicyStore> store_; | |
118 }; | |
119 | |
120 // Proxy setting distributed over multiple caches. | |
121 TEST_F(CloudPolicyProviderTest, | |
122 ProxySettingDistributedOverMultipleCaches) { | |
123 // There are proxy_policy_count()+1 = 6 caches and they are mixed together by | |
124 // one instance of CloudPolicyProvider. The first cache has some policies but | |
125 // no proxy-related ones. The following caches have each one proxy-policy set. | |
126 const int n = 6; | |
127 MockCloudPolicyCache caches[n]; | |
128 | |
129 // Prepare |cache[0]| to serve some non-proxy policies. | |
130 caches[0].raw_mandatory_policy()->Set(kPolicyShowHomeButton, | |
131 Value::CreateBooleanValue(true)); | |
132 caches[0].raw_mandatory_policy()->Set(kPolicyIncognitoEnabled, | |
133 Value::CreateBooleanValue(true)); | |
134 caches[0].raw_mandatory_policy()->Set(kPolicyTranslateEnabled, | |
135 Value::CreateBooleanValue(true)); | |
136 caches[0].set_initialized(true); | |
137 | |
138 // Prepare the other caches to serve one proxy-policy each. | |
139 caches[1].raw_mandatory_policy()->Set(kPolicyProxyMode, | |
140 Value::CreateStringValue("cache 1")); | |
141 caches[1].set_initialized(true); | |
142 caches[2].raw_mandatory_policy()->Set(kPolicyProxyServerMode, | |
143 Value::CreateIntegerValue(2)); | |
144 caches[2].set_initialized(true); | |
145 caches[3].raw_mandatory_policy()->Set(kPolicyProxyServer, | |
146 Value::CreateStringValue("cache 3")); | |
147 caches[3].set_initialized(true); | |
148 caches[4].raw_mandatory_policy()->Set(kPolicyProxyPacUrl, | |
149 Value::CreateStringValue("cache 4")); | |
150 caches[4].set_initialized(true); | |
151 caches[5].raw_mandatory_policy()->Set(kPolicyProxyMode, | |
152 Value::CreateStringValue("cache 5")); | |
153 caches[5].set_initialized(true); | |
154 | |
155 RunCachesThroughProvider( | |
156 caches, n, CloudPolicyCacheBase::POLICY_LEVEL_MANDATORY); | |
157 | |
158 // Verify expectations. | |
159 ExpectStringPolicy("cache 1", kPolicyProxyMode); | |
160 ExpectNoPolicy(kPolicyProxyServerMode); | |
161 ExpectNoPolicy(kPolicyProxyServer); | |
162 ExpectNoPolicy(kPolicyProxyPacUrl); | |
163 ExpectBoolPolicy(true, kPolicyShowHomeButton); | |
164 ExpectBoolPolicy(true, kPolicyIncognitoEnabled); | |
165 ExpectBoolPolicy(true, kPolicyTranslateEnabled); | |
166 } | |
167 | |
168 // Combining two PolicyMaps. | |
169 TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsSame) { | |
170 PolicyMap A, B, C; | |
171 CreateCloudPolicyProvider(CloudPolicyCacheBase::POLICY_LEVEL_RECOMMENDED); | |
172 A.Set(kPolicyHomepageLocation, | |
173 Value::CreateStringValue("http://www.chromium.org")); | |
174 B.Set(kPolicyHomepageLocation, | |
175 Value::CreateStringValue("http://www.google.com")); | |
176 A.Set(kPolicyApplicationLocaleValue, Value::CreateStringValue("hu")); | |
177 B.Set(kPolicyApplicationLocaleValue, Value::CreateStringValue("us")); | |
178 A.Set(kPolicyDevicePolicyRefreshRate, new FundamentalValue(100)); | |
179 B.Set(kPolicyDevicePolicyRefreshRate, new FundamentalValue(200)); | |
180 CombineTwoPolicyMaps(A, B, &C); | |
181 EXPECT_TRUE(A.Equals(C)); | |
182 } | |
183 | |
184 TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsEmpty) { | |
185 PolicyMap A, B, C; | |
186 CreateCloudPolicyProvider(CloudPolicyCacheBase::POLICY_LEVEL_RECOMMENDED); | |
187 CombineTwoPolicyMaps(A, B, &C); | |
188 EXPECT_TRUE(C.empty()); | |
189 } | |
190 | |
191 TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsPartial) { | |
192 PolicyMap A, B, C; | |
193 CreateCloudPolicyProvider(CloudPolicyCacheBase::POLICY_LEVEL_RECOMMENDED); | |
194 | |
195 A.Set(kPolicyHomepageLocation, | |
196 Value::CreateStringValue("http://www.chromium.org")); | |
197 B.Set(kPolicyHomepageLocation, | |
198 Value::CreateStringValue("http://www.google.com")); | |
199 B.Set(kPolicyApplicationLocaleValue, Value::CreateStringValue("us")); | |
200 A.Set(kPolicyDevicePolicyRefreshRate, new FundamentalValue(100)); | |
201 B.Set(kPolicyDevicePolicyRefreshRate, new FundamentalValue(200)); | |
202 CombineTwoPolicyMaps(A, B, &C); | |
203 | |
204 const Value* value; | |
205 std::string string_value; | |
206 int int_value; | |
207 value = C.Get(kPolicyHomepageLocation); | |
208 EXPECT_TRUE(NULL != value); | |
Joao da Silva
2011/06/29 11:36:17
Nit: same as above, also 2 more times below.
gfeher
2011/06/29 12:53:07
Done.
| |
209 EXPECT_TRUE(value->GetAsString(&string_value)); | |
210 EXPECT_EQ("http://www.chromium.org", string_value); | |
211 value = C.Get(kPolicyApplicationLocaleValue); | |
212 EXPECT_TRUE(NULL != value); | |
213 EXPECT_TRUE(value->GetAsString(&string_value)); | |
214 EXPECT_EQ("us", string_value); | |
215 value = C.Get(kPolicyDevicePolicyRefreshRate); | |
216 EXPECT_TRUE(NULL != value); | |
217 EXPECT_TRUE(value->GetAsInteger(&int_value)); | |
218 EXPECT_EQ(100, int_value); | |
219 } | |
220 | |
221 TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsProxies) { | |
222 const int a_value = 1; | |
223 const int b_value = -1; | |
224 PolicyMap A, B, C; | |
225 CreateCloudPolicyProvider(CloudPolicyCacheBase::POLICY_LEVEL_RECOMMENDED); | |
226 | |
227 A.Set(policy::kPolicyProxyMode, Value::CreateIntegerValue(a_value)); | |
228 | |
229 B.Set(policy::kPolicyProxyServerMode, Value::CreateIntegerValue(b_value)); | |
230 B.Set(policy::kPolicyProxyServer, Value::CreateIntegerValue(b_value)); | |
231 B.Set(policy::kPolicyProxyPacUrl, Value::CreateIntegerValue(b_value)); | |
232 B.Set(policy::kPolicyProxyBypassList, Value::CreateIntegerValue(b_value)); | |
233 | |
234 CombineTwoPolicyMaps(A, B, &C); | |
235 | |
236 EXPECT_TRUE(A.Equals(C)); | |
237 EXPECT_FALSE(B.Equals(C)); | |
238 } | |
239 | |
240 } // namespace policy | |
OLD | NEW |