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_cache.h" | |
6 | |
7 #include <limits> | |
8 #include <string> | |
9 | |
10 #include "base/file_util.h" | |
11 #include "base/message_loop.h" | |
12 #include "base/scoped_temp_dir.h" | |
13 #include "base/values.h" | |
14 #include "chrome/browser/browser_thread.h" | |
15 #include "chrome/browser/policy/proto/cloud_policy.pb.h" | |
16 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
17 #include "chrome/browser/policy/proto/device_management_local.pb.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 namespace policy { | |
21 | |
22 Value* DecodeIntegerValue(google::protobuf::int64 value); | |
Mattias Nissler (ping if slow)
2011/02/15 10:46:39
Where are the implementations? Put a comment helpi
Jakob Kummerow
2011/02/15 14:22:00
Done.
| |
23 ListValue* DecodeStringList(const em::StringList& string_list); | |
24 | |
25 // Tests the device management policy cache. | |
26 class CloudPolicyCacheTest : public testing::Test { | |
27 protected: | |
28 typedef ConfigurationPolicyProvider::PolicyMapType PolicyMapType; | |
29 | |
30 CloudPolicyCacheTest() | |
31 : loop_(MessageLoop::TYPE_UI), | |
32 ui_thread_(BrowserThread::UI, &loop_), | |
33 file_thread_(BrowserThread::FILE, &loop_) {} | |
34 | |
35 void SetUp() { | |
36 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
37 } | |
38 | |
39 void TearDown() { | |
40 loop_.RunAllPending(); | |
41 } | |
42 | |
43 // Creates a (signed) CloudPolicyResponse setting the given |homepage| and | |
44 // featuring the given |timestamp| (as issued by the server). | |
45 // Mildly hacky special feature: pass an empty string as |homepage| to get | |
46 // a completely empty policy. | |
47 em::CloudPolicyResponse* CreateHomepagePolicy( | |
48 const std::string& homepage, | |
49 const base::Time& timestamp, | |
50 const em::PolicyOptions::PolicyMode policy_mode) { | |
51 em::SignedCloudPolicyResponse signed_response; | |
52 if (homepage != "") { | |
53 em::CloudPolicySettings* settings = signed_response.mutable_settings(); | |
54 em::HomepageLocationProto* homepagelocation_proto = | |
55 settings->mutable_homepagelocation(); | |
56 homepagelocation_proto->set_homepagelocation(homepage); | |
57 homepagelocation_proto->mutable_policy_options()->set_mode(policy_mode); | |
58 } | |
59 signed_response.set_timestamp(timestamp.ToTimeT()); | |
60 std::string serialized_signed_response; | |
61 EXPECT_TRUE(signed_response.SerializeToString(&serialized_signed_response)); | |
62 | |
63 em::CloudPolicyResponse* response = new em::CloudPolicyResponse; | |
64 response->set_signed_response(serialized_signed_response); | |
65 // TODO(jkummerow): Set proper certificate_chain and signature (when | |
66 // implementing support for signature verification). | |
67 response->set_signature("TODO"); | |
68 response->add_certificate_chain("TODO"); | |
69 return response; | |
70 } | |
71 | |
72 void WritePolicy(const em::CloudPolicyResponse& policy) { | |
73 std::string data; | |
74 em::CachedCloudPolicyResponse cached_policy; | |
75 cached_policy.mutable_cloud_policy()->CopyFrom(policy); | |
76 EXPECT_TRUE(cached_policy.SerializeToString(&data)); | |
77 int size = static_cast<int>(data.size()); | |
78 EXPECT_EQ(size, file_util::WriteFile(test_file(), data.c_str(), size)); | |
79 } | |
80 | |
81 FilePath test_file() { | |
82 return temp_dir_.path().AppendASCII("CloudPolicyCacheTest"); | |
83 } | |
84 | |
85 bool Equals(const PolicyMapType& a, const PolicyMapType& b) const { | |
86 return CloudPolicyCache::Equals(a, b); | |
87 } | |
88 | |
89 MessageLoop loop_; | |
90 | |
91 private: | |
92 ScopedTempDir temp_dir_; | |
93 BrowserThread ui_thread_; | |
94 BrowserThread file_thread_; | |
95 }; | |
96 | |
97 TEST_F(CloudPolicyCacheTest, Equals) { | |
98 PolicyMapType a; | |
99 a.insert(std::make_pair(kPolicyHomepageLocation, | |
100 Value::CreateStringValue("aaa"))); | |
101 PolicyMapType a2; | |
102 a2.insert(std::make_pair(kPolicyHomepageLocation, | |
103 Value::CreateStringValue("aaa"))); | |
104 PolicyMapType b; | |
105 b.insert(std::make_pair(kPolicyHomepageLocation, | |
106 Value::CreateStringValue("bbb"))); | |
107 PolicyMapType c; | |
108 c.insert(std::make_pair(kPolicyHomepageLocation, | |
109 Value::CreateStringValue("aaa"))); | |
110 c.insert(std::make_pair(kPolicyHomepageIsNewTabPage, | |
111 Value::CreateBooleanValue(true))); | |
112 EXPECT_FALSE(Equals(a, b)); | |
113 EXPECT_FALSE(Equals(b, a)); | |
114 EXPECT_FALSE(Equals(a, c)); | |
115 EXPECT_FALSE(Equals(c, a)); | |
116 EXPECT_TRUE(Equals(a, a2)); | |
117 EXPECT_TRUE(Equals(a2, a)); | |
118 PolicyMapType empty1; | |
119 PolicyMapType empty2; | |
120 EXPECT_TRUE(Equals(empty1, empty2)); | |
121 EXPECT_TRUE(Equals(empty2, empty1)); | |
122 EXPECT_FALSE(Equals(empty1, a)); | |
123 EXPECT_FALSE(Equals(a, empty1)); | |
124 } | |
125 | |
126 TEST_F(CloudPolicyCacheTest, DecodePolicy) { | |
127 em::CloudPolicySettings settings; | |
128 settings.mutable_homepagelocation()->set_homepagelocation("chromium.org"); | |
129 settings.mutable_javascriptenabled()->set_javascriptenabled(true); | |
130 settings.mutable_javascriptenabled()->mutable_policy_options()->set_mode( | |
131 em::PolicyOptions::MANDATORY); | |
132 settings.mutable_policyrefreshrate()->set_policyrefreshrate(5); | |
133 settings.mutable_policyrefreshrate()->mutable_policy_options()->set_mode( | |
134 em::PolicyOptions::RECOMMENDED); | |
135 PolicyMapType mandatory_policy; | |
136 PolicyMapType recommended_policy; | |
137 DecodePolicy(settings, &mandatory_policy, &recommended_policy); | |
138 PolicyMapType mandatory; | |
139 mandatory.insert(std::make_pair(kPolicyHomepageLocation, | |
140 Value::CreateStringValue("chromium.org"))); | |
141 mandatory.insert(std::make_pair(kPolicyJavascriptEnabled, | |
142 Value::CreateBooleanValue(true))); | |
143 PolicyMapType recommended; | |
144 recommended.insert(std::make_pair(kPolicyPolicyRefreshRate, | |
145 Value::CreateIntegerValue(5))); | |
146 EXPECT_TRUE(Equals(mandatory, mandatory_policy)); | |
147 EXPECT_TRUE(Equals(recommended, recommended_policy)); | |
148 } | |
149 | |
150 TEST_F(CloudPolicyCacheTest, DecodeIntegerValue) { | |
151 const int min = std::numeric_limits<int>::min(); | |
152 const int max = std::numeric_limits<int>::max(); | |
153 scoped_ptr<Value> value( | |
154 DecodeIntegerValue(static_cast<google::protobuf::int64>(42))); | |
155 ASSERT_TRUE(value.get()); | |
156 EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(42))); | |
157 value.reset( | |
158 DecodeIntegerValue(static_cast<google::protobuf::int64>(min - 1LL))); | |
159 EXPECT_EQ(NULL, value.get()); | |
160 value.reset(DecodeIntegerValue(static_cast<google::protobuf::int64>(min))); | |
161 ASSERT_TRUE(value.get()); | |
162 EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(min))); | |
163 value.reset( | |
164 DecodeIntegerValue(static_cast<google::protobuf::int64>(max + 1LL))); | |
165 EXPECT_EQ(NULL, value.get()); | |
166 value.reset(DecodeIntegerValue(static_cast<google::protobuf::int64>(max))); | |
167 ASSERT_TRUE(value.get()); | |
168 EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(max))); | |
169 } | |
170 | |
171 TEST_F(CloudPolicyCacheTest, DecodeStringList) { | |
172 em::StringList string_list; | |
173 string_list.add_entries("ponies"); | |
174 string_list.add_entries("more ponies"); | |
175 scoped_ptr<ListValue> decoded(DecodeStringList(string_list)); | |
176 ListValue expected; | |
177 expected.Append(Value::CreateStringValue("ponies")); | |
178 expected.Append(Value::CreateStringValue("more ponies")); | |
179 EXPECT_TRUE(decoded->Equals(&expected)); | |
180 } | |
181 | |
182 TEST_F(CloudPolicyCacheTest, Empty) { | |
183 CloudPolicyCache cache(test_file()); | |
184 PolicyMapType empty; | |
185 EXPECT_TRUE(Equals(empty, *cache.GetMandatoryPolicy())); | |
186 EXPECT_TRUE(Equals(empty, *cache.GetRecommendedPolicy())); | |
187 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time()); | |
188 } | |
189 | |
190 TEST_F(CloudPolicyCacheTest, LoadNoFile) { | |
191 CloudPolicyCache cache(test_file()); | |
192 cache.LoadPolicyFromFile(); | |
193 PolicyMapType empty; | |
194 EXPECT_TRUE(Equals(empty, *cache.GetMandatoryPolicy())); | |
195 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time()); | |
196 } | |
197 | |
198 TEST_F(CloudPolicyCacheTest, RejectFuture) { | |
199 scoped_ptr<em::CloudPolicyResponse> policy_response( | |
200 CreateHomepagePolicy("", base::Time::NowFromSystemTime() + | |
201 base::TimeDelta::FromMinutes(5), | |
202 em::PolicyOptions::MANDATORY)); | |
203 WritePolicy(*policy_response); | |
204 CloudPolicyCache cache(test_file()); | |
205 cache.LoadPolicyFromFile(); | |
206 PolicyMapType empty; | |
207 EXPECT_TRUE(Equals(empty, *cache.GetMandatoryPolicy())); | |
208 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time()); | |
209 } | |
210 | |
211 TEST_F(CloudPolicyCacheTest, LoadWithFile) { | |
212 scoped_ptr<em::CloudPolicyResponse> policy_response( | |
213 CreateHomepagePolicy("", base::Time::NowFromSystemTime(), | |
214 em::PolicyOptions::MANDATORY)); | |
215 WritePolicy(*policy_response); | |
216 CloudPolicyCache cache(test_file()); | |
217 cache.LoadPolicyFromFile(); | |
218 PolicyMapType empty; | |
219 EXPECT_TRUE(Equals(empty, *cache.GetMandatoryPolicy())); | |
220 EXPECT_NE(base::Time(), cache.last_policy_refresh_time()); | |
221 EXPECT_GE(base::Time::Now(), cache.last_policy_refresh_time()); | |
222 } | |
223 | |
224 TEST_F(CloudPolicyCacheTest, LoadWithData) { | |
225 scoped_ptr<em::CloudPolicyResponse> policy( | |
226 CreateHomepagePolicy("http://www.example.com", | |
227 base::Time::NowFromSystemTime(), | |
228 em::PolicyOptions::MANDATORY)); | |
229 WritePolicy(*policy); | |
230 CloudPolicyCache cache(test_file()); | |
231 cache.LoadPolicyFromFile(); | |
232 PolicyMapType expected; | |
233 expected.insert( | |
234 std::make_pair(kPolicyHomepageLocation, | |
235 Value::CreateStringValue("http://www.example.com"))); | |
236 EXPECT_TRUE(Equals(expected, *cache.GetMandatoryPolicy())); | |
237 } | |
238 | |
239 TEST_F(CloudPolicyCacheTest, SetPolicy) { | |
240 CloudPolicyCache cache(test_file()); | |
241 scoped_ptr<em::CloudPolicyResponse> policy( | |
242 CreateHomepagePolicy("http://www.example.com", | |
243 base::Time::NowFromSystemTime(), | |
244 em::PolicyOptions::MANDATORY)); | |
245 EXPECT_TRUE(cache.SetPolicy(*policy)); | |
246 scoped_ptr<em::CloudPolicyResponse> policy2( | |
247 CreateHomepagePolicy("http://www.example.com", | |
248 base::Time::NowFromSystemTime(), | |
249 em::PolicyOptions::MANDATORY)); | |
250 EXPECT_FALSE(cache.SetPolicy(*policy2)); | |
251 PolicyMapType expected; | |
252 expected.insert( | |
253 std::make_pair(kPolicyHomepageLocation, | |
254 Value::CreateStringValue("http://www.example.com"))); | |
255 PolicyMapType empty; | |
256 EXPECT_TRUE(Equals(expected, *cache.GetMandatoryPolicy())); | |
257 EXPECT_TRUE(Equals(empty, *cache.GetRecommendedPolicy())); | |
258 policy.reset(CreateHomepagePolicy("http://www.example.com", | |
259 base::Time::NowFromSystemTime(), | |
260 em::PolicyOptions::RECOMMENDED)); | |
261 EXPECT_TRUE(cache.SetPolicy(*policy)); | |
262 EXPECT_TRUE(Equals(expected, *cache.GetRecommendedPolicy())); | |
263 EXPECT_TRUE(Equals(empty, *cache.GetMandatoryPolicy())); | |
264 } | |
265 | |
266 TEST_F(CloudPolicyCacheTest, ResetPolicy) { | |
267 CloudPolicyCache cache(test_file()); | |
268 | |
269 scoped_ptr<em::CloudPolicyResponse> policy( | |
270 CreateHomepagePolicy("http://www.example.com", | |
271 base::Time::NowFromSystemTime(), | |
272 em::PolicyOptions::MANDATORY)); | |
273 EXPECT_TRUE(cache.SetPolicy(*policy)); | |
274 PolicyMapType expected; | |
275 expected.insert( | |
276 std::make_pair(kPolicyHomepageLocation, | |
277 Value::CreateStringValue("http://www.example.com"))); | |
278 EXPECT_TRUE(Equals(expected, *cache.GetMandatoryPolicy())); | |
279 | |
280 scoped_ptr<em::CloudPolicyResponse> empty_policy( | |
281 CreateHomepagePolicy("", base::Time::NowFromSystemTime(), | |
282 em::PolicyOptions::MANDATORY)); | |
283 EXPECT_TRUE(cache.SetPolicy(*empty_policy)); | |
284 PolicyMapType empty; | |
285 EXPECT_TRUE(Equals(empty, *cache.GetMandatoryPolicy())); | |
286 } | |
287 | |
288 TEST_F(CloudPolicyCacheTest, PersistPolicy) { | |
289 { | |
290 CloudPolicyCache cache(test_file()); | |
291 scoped_ptr<em::CloudPolicyResponse> policy( | |
292 CreateHomepagePolicy("http://www.example.com", | |
293 base::Time::NowFromSystemTime(), | |
294 em::PolicyOptions::MANDATORY)); | |
295 cache.SetPolicy(*policy); | |
296 } | |
297 | |
298 loop_.RunAllPending(); | |
299 | |
300 EXPECT_TRUE(file_util::PathExists(test_file())); | |
301 CloudPolicyCache cache(test_file()); | |
302 cache.LoadPolicyFromFile(); | |
303 PolicyMapType expected; | |
304 expected.insert( | |
305 std::make_pair(kPolicyHomepageLocation, | |
306 Value::CreateStringValue("http://www.example.com"))); | |
307 EXPECT_TRUE(Equals(expected, *cache.GetMandatoryPolicy())); | |
308 } | |
309 | |
310 TEST_F(CloudPolicyCacheTest, FreshPolicyOverride) { | |
311 scoped_ptr<em::CloudPolicyResponse> policy( | |
312 CreateHomepagePolicy("http://www.example.com", | |
313 base::Time::NowFromSystemTime(), | |
314 em::PolicyOptions::MANDATORY)); | |
315 WritePolicy(*policy); | |
316 | |
317 CloudPolicyCache cache(test_file()); | |
318 scoped_ptr<em::CloudPolicyResponse> updated_policy( | |
319 CreateHomepagePolicy("http://www.chromium.org", | |
320 base::Time::NowFromSystemTime(), | |
321 em::PolicyOptions::MANDATORY)); | |
322 EXPECT_TRUE(cache.SetPolicy(*updated_policy)); | |
323 | |
324 cache.LoadPolicyFromFile(); | |
325 PolicyMapType expected; | |
326 expected.insert( | |
327 std::make_pair(kPolicyHomepageLocation, | |
328 Value::CreateStringValue("http://www.chromium.org"))); | |
329 EXPECT_TRUE(Equals(expected, *cache.GetMandatoryPolicy())); | |
330 } | |
331 | |
Mattias Nissler (ping if slow)
2011/02/15 10:46:39
AFAICS, these tests only test the new format. Shou
Jakob Kummerow
2011/02/15 14:22:00
We still have the old tests in device_management_p
| |
332 } // namespace policy | |
OLD | NEW |