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

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

Issue 6705031: Send policy blobs to session_manager (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix nits Created 9 years, 9 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
(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/memory/scoped_temp_dir.h"
12 #include "base/message_loop.h"
13 #include "base/values.h"
14 #include "chrome/browser/policy/configuration_policy_provider.h"
15 #include "chrome/browser/policy/proto/cloud_policy.pb.h"
16 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
17 // TODO(jkummerow): remove this import when removing old DMPC test cases.
18 #include "chrome/browser/policy/proto/device_management_constants.h"
19 #include "chrome/browser/policy/proto/device_management_local.pb.h"
20 #include "content/browser/browser_thread.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 namespace policy {
25
26 // Decodes a CloudPolicySettings object into two maps with mandatory and
27 // recommended settings, respectively. The implementation is generated code
28 // in policy/cloud_policy_generated.cc.
29 void DecodePolicy(const em::CloudPolicySettings& policy,
30 PolicyMap* mandatory, PolicyMap* recommended);
31
32 // The implementations of these methods are in cloud_policy_generated.cc.
33 Value* DecodeIntegerValue(google::protobuf::int64 value);
34 ListValue* DecodeStringList(const em::StringList& string_list);
35
36 class MockConfigurationPolicyProviderObserver
37 : public ConfigurationPolicyProvider::Observer {
38 public:
39 MockConfigurationPolicyProviderObserver() {}
40 virtual ~MockConfigurationPolicyProviderObserver() {}
41 MOCK_METHOD0(OnUpdatePolicy, void());
42 void OnProviderGoingAway() {}
43 };
44
45 // Tests the device management policy cache.
46 class CloudPolicyCacheTest : public testing::Test {
47 protected:
48 CloudPolicyCacheTest()
49 : loop_(MessageLoop::TYPE_UI),
50 ui_thread_(BrowserThread::UI, &loop_),
51 file_thread_(BrowserThread::FILE, &loop_) {}
52
53 void SetUp() {
54 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
55 }
56
57 void TearDown() {
58 loop_.RunAllPending();
59 }
60
61 // Creates a (signed) PolicyFetchResponse setting the given |homepage| and
62 // featuring the given |timestamp| (as issued by the server).
63 // Mildly hacky special feature: pass an empty string as |homepage| to get
64 // a completely empty policy.
65 em::PolicyFetchResponse* CreateHomepagePolicy(
66 const std::string& homepage,
67 const base::Time& timestamp,
68 const em::PolicyOptions::PolicyMode policy_mode) {
69 em::PolicyData signed_response;
70 if (homepage != "") {
71 em::CloudPolicySettings settings;
72 em::HomepageLocationProto* homepagelocation_proto =
73 settings.mutable_homepagelocation();
74 homepagelocation_proto->set_homepagelocation(homepage);
75 homepagelocation_proto->mutable_policy_options()->set_mode(policy_mode);
76 EXPECT_TRUE(
77 settings.SerializeToString(signed_response.mutable_policy_value()));
78 }
79 signed_response.set_timestamp(
80 (timestamp - base::Time::UnixEpoch()).InMilliseconds());
81 std::string serialized_signed_response;
82 EXPECT_TRUE(signed_response.SerializeToString(&serialized_signed_response));
83
84 em::PolicyFetchResponse* response = new em::PolicyFetchResponse;
85 response->set_policy_data(serialized_signed_response);
86 // TODO(jkummerow): Set proper new_public_key and signature (when
87 // implementing support for signature verification).
88 response->set_policy_data_signature("TODO");
89 response->set_new_public_key("TODO");
90 return response;
91 }
92
93 void WritePolicy(const em::PolicyFetchResponse& policy) {
94 std::string data;
95 em::CachedCloudPolicyResponse cached_policy;
96 cached_policy.mutable_cloud_policy()->CopyFrom(policy);
97 EXPECT_TRUE(cached_policy.SerializeToString(&data));
98 int size = static_cast<int>(data.size());
99 EXPECT_EQ(size, file_util::WriteFile(test_file(), data.c_str(), size));
100 }
101
102 // Takes ownership of |policy_response|.
103 void SetPolicy(CloudPolicyCache* cache,
104 em::PolicyFetchResponse* policy_response,
105 bool expect_changed_policy) {
106 scoped_ptr<em::PolicyFetchResponse> policy(policy_response);
107 ConfigurationPolicyObserverRegistrar registrar;
108 registrar.Init(cache->GetManagedPolicyProvider(), &observer);
109 if (expect_changed_policy)
110 EXPECT_CALL(observer, OnUpdatePolicy()).Times(1);
111 else
112 EXPECT_CALL(observer, OnUpdatePolicy()).Times(0);
113 cache->SetPolicy(*policy);
114 testing::Mock::VerifyAndClearExpectations(&observer);
115 }
116
117 FilePath test_file() {
118 return temp_dir_.path().AppendASCII("CloudPolicyCacheTest");
119 }
120
121 const PolicyMap& mandatory_policy(const CloudPolicyCache& cache) {
122 return cache.mandatory_policy_;
123 }
124
125 const PolicyMap& recommended_policy(const CloudPolicyCache& cache) {
126 return cache.recommended_policy_;
127 }
128
129 MessageLoop loop_;
130 MockConfigurationPolicyProviderObserver observer;
131
132 private:
133 ScopedTempDir temp_dir_;
134 BrowserThread ui_thread_;
135 BrowserThread file_thread_;
136 };
137
138 TEST_F(CloudPolicyCacheTest, DecodePolicy) {
139 em::CloudPolicySettings settings;
140 settings.mutable_homepagelocation()->set_homepagelocation("chromium.org");
141 settings.mutable_javascriptenabled()->set_javascriptenabled(true);
142 settings.mutable_javascriptenabled()->mutable_policy_options()->set_mode(
143 em::PolicyOptions::MANDATORY);
144 settings.mutable_policyrefreshrate()->set_policyrefreshrate(5);
145 settings.mutable_policyrefreshrate()->mutable_policy_options()->set_mode(
146 em::PolicyOptions::RECOMMENDED);
147 PolicyMap mandatory_policy;
148 PolicyMap recommended_policy;
149 DecodePolicy(settings, &mandatory_policy, &recommended_policy);
150 PolicyMap mandatory;
151 mandatory.Set(kPolicyHomepageLocation,
152 Value::CreateStringValue("chromium.org"));
153 mandatory.Set(kPolicyJavascriptEnabled, Value::CreateBooleanValue(true));
154 PolicyMap recommended;
155 recommended.Set(kPolicyPolicyRefreshRate, Value::CreateIntegerValue(5));
156 EXPECT_TRUE(mandatory.Equals(mandatory_policy));
157 EXPECT_TRUE(recommended.Equals(recommended_policy));
158 }
159
160 TEST_F(CloudPolicyCacheTest, DecodeIntegerValue) {
161 const int min = std::numeric_limits<int>::min();
162 const int max = std::numeric_limits<int>::max();
163 scoped_ptr<Value> value(
164 DecodeIntegerValue(static_cast<google::protobuf::int64>(42)));
165 ASSERT_TRUE(value.get());
166 FundamentalValue expected_42(42);
167 EXPECT_TRUE(value->Equals(&expected_42));
168 value.reset(
169 DecodeIntegerValue(static_cast<google::protobuf::int64>(min - 1LL)));
170 EXPECT_EQ(NULL, value.get());
171 value.reset(DecodeIntegerValue(static_cast<google::protobuf::int64>(min)));
172 ASSERT_TRUE(value.get());
173 FundamentalValue expected_min(min);
174 EXPECT_TRUE(value->Equals(&expected_min));
175 value.reset(
176 DecodeIntegerValue(static_cast<google::protobuf::int64>(max + 1LL)));
177 EXPECT_EQ(NULL, value.get());
178 value.reset(DecodeIntegerValue(static_cast<google::protobuf::int64>(max)));
179 ASSERT_TRUE(value.get());
180 FundamentalValue expected_max(max);
181 EXPECT_TRUE(value->Equals(&expected_max));
182 }
183
184 TEST_F(CloudPolicyCacheTest, DecodeStringList) {
185 em::StringList string_list;
186 string_list.add_entries("ponies");
187 string_list.add_entries("more ponies");
188 scoped_ptr<ListValue> decoded(DecodeStringList(string_list));
189 ListValue expected;
190 expected.Append(Value::CreateStringValue("ponies"));
191 expected.Append(Value::CreateStringValue("more ponies"));
192 EXPECT_TRUE(decoded->Equals(&expected));
193 }
194
195 TEST_F(CloudPolicyCacheTest, Empty) {
196 CloudPolicyCache cache(test_file());
197 PolicyMap empty;
198 EXPECT_TRUE(empty.Equals(mandatory_policy(cache)));
199 EXPECT_TRUE(empty.Equals(recommended_policy(cache)));
200 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time());
201 }
202
203 TEST_F(CloudPolicyCacheTest, LoadNoFile) {
204 CloudPolicyCache cache(test_file());
205 cache.LoadFromFile();
206 PolicyMap empty;
207 EXPECT_TRUE(empty.Equals(mandatory_policy(cache)));
208 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time());
209 }
210
211 TEST_F(CloudPolicyCacheTest, RejectFuture) {
212 scoped_ptr<em::PolicyFetchResponse> policy_response(
213 CreateHomepagePolicy("", base::Time::NowFromSystemTime() +
214 base::TimeDelta::FromMinutes(5),
215 em::PolicyOptions::MANDATORY));
216 WritePolicy(*policy_response);
217 CloudPolicyCache cache(test_file());
218 cache.LoadFromFile();
219 PolicyMap empty;
220 EXPECT_TRUE(empty.Equals(mandatory_policy(cache)));
221 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time());
222 }
223
224 TEST_F(CloudPolicyCacheTest, LoadWithFile) {
225 scoped_ptr<em::PolicyFetchResponse> policy_response(
226 CreateHomepagePolicy("", base::Time::NowFromSystemTime(),
227 em::PolicyOptions::MANDATORY));
228 WritePolicy(*policy_response);
229 CloudPolicyCache cache(test_file());
230 cache.LoadFromFile();
231 PolicyMap empty;
232 EXPECT_TRUE(empty.Equals(mandatory_policy(cache)));
233 EXPECT_NE(base::Time(), cache.last_policy_refresh_time());
234 EXPECT_GE(base::Time::Now(), cache.last_policy_refresh_time());
235 }
236
237 TEST_F(CloudPolicyCacheTest, LoadWithData) {
238 scoped_ptr<em::PolicyFetchResponse> policy(
239 CreateHomepagePolicy("http://www.example.com",
240 base::Time::NowFromSystemTime(),
241 em::PolicyOptions::MANDATORY));
242 WritePolicy(*policy);
243 CloudPolicyCache cache(test_file());
244 cache.LoadFromFile();
245 PolicyMap expected;
246 expected.Set(kPolicyHomepageLocation,
247 Value::CreateStringValue("http://www.example.com"));
248 EXPECT_TRUE(expected.Equals(mandatory_policy(cache)));
249 }
250
251 TEST_F(CloudPolicyCacheTest, SetPolicy) {
252 CloudPolicyCache cache(test_file());
253 em::PolicyFetchResponse* policy =
254 CreateHomepagePolicy("http://www.example.com",
255 base::Time::NowFromSystemTime(),
256 em::PolicyOptions::MANDATORY);
257 SetPolicy(&cache, policy, true);
258 em::PolicyFetchResponse* policy2 =
259 CreateHomepagePolicy("http://www.example.com",
260 base::Time::NowFromSystemTime(),
261 em::PolicyOptions::MANDATORY);
262 SetPolicy(&cache, policy2, false);
263 PolicyMap expected;
264 expected.Set(kPolicyHomepageLocation,
265 Value::CreateStringValue("http://www.example.com"));
266 PolicyMap empty;
267 EXPECT_TRUE(expected.Equals(mandatory_policy(cache)));
268 EXPECT_TRUE(empty.Equals(recommended_policy(cache)));
269 policy = CreateHomepagePolicy("http://www.example.com",
270 base::Time::NowFromSystemTime(),
271 em::PolicyOptions::RECOMMENDED);
272 SetPolicy(&cache, policy, true);
273 EXPECT_TRUE(expected.Equals(recommended_policy(cache)));
274 EXPECT_TRUE(empty.Equals(mandatory_policy(cache)));
275 }
276
277 TEST_F(CloudPolicyCacheTest, ResetPolicy) {
278 CloudPolicyCache cache(test_file());
279
280 em::PolicyFetchResponse* policy =
281 CreateHomepagePolicy("http://www.example.com",
282 base::Time::NowFromSystemTime(),
283 em::PolicyOptions::MANDATORY);
284 SetPolicy(&cache, policy, true);
285 PolicyMap expected;
286 expected.Set(kPolicyHomepageLocation,
287 Value::CreateStringValue("http://www.example.com"));
288 EXPECT_TRUE(expected.Equals(mandatory_policy(cache)));
289
290 em::PolicyFetchResponse* empty_policy =
291 CreateHomepagePolicy("", base::Time::NowFromSystemTime(),
292 em::PolicyOptions::MANDATORY);
293 SetPolicy(&cache, empty_policy, true);
294 PolicyMap empty;
295 EXPECT_TRUE(empty.Equals(mandatory_policy(cache)));
296 }
297
298 TEST_F(CloudPolicyCacheTest, PersistPolicy) {
299 {
300 CloudPolicyCache cache(test_file());
301 scoped_ptr<em::PolicyFetchResponse> policy(
302 CreateHomepagePolicy("http://www.example.com",
303 base::Time::NowFromSystemTime(),
304 em::PolicyOptions::MANDATORY));
305 cache.SetPolicy(*policy);
306 }
307
308 loop_.RunAllPending();
309
310 EXPECT_TRUE(file_util::PathExists(test_file()));
311 CloudPolicyCache cache(test_file());
312 cache.LoadFromFile();
313 PolicyMap expected;
314 expected.Set(kPolicyHomepageLocation,
315 Value::CreateStringValue("http://www.example.com"));
316 EXPECT_TRUE(expected.Equals(mandatory_policy(cache)));
317 }
318
319 TEST_F(CloudPolicyCacheTest, FreshPolicyOverride) {
320 scoped_ptr<em::PolicyFetchResponse> policy(
321 CreateHomepagePolicy("http://www.example.com",
322 base::Time::NowFromSystemTime(),
323 em::PolicyOptions::MANDATORY));
324 WritePolicy(*policy);
325
326 CloudPolicyCache cache(test_file());
327 em::PolicyFetchResponse* updated_policy =
328 CreateHomepagePolicy("http://www.chromium.org",
329 base::Time::NowFromSystemTime(),
330 em::PolicyOptions::MANDATORY);
331 SetPolicy(&cache, updated_policy, true);
332
333 cache.LoadFromFile();
334 PolicyMap expected;
335 expected.Set(kPolicyHomepageLocation,
336 Value::CreateStringValue("http://www.chromium.org"));
337 EXPECT_TRUE(expected.Equals(mandatory_policy(cache)));
338 }
339
340 } // namespace policy
341
342 // ==================================================================
343 // Everything below this line can go when we phase out support for
344 // the old (trusted testing/pilot program) policy format.
345
346 // This is a (slightly updated) copy of the old
347 // device_management_policy_cache_unittest.cc. The new CloudPolicyCache
348 // supports the old DMPC's interface for now (until it is phased out), so for
349 // this transitional period, we keep these old test cases but apply them to the
350 // new implementation (CPC).
351
352 namespace policy {
353
354 // Wraps base functionaly for the test cases.
355 class DeviceManagementPolicyCacheTestBase : public testing::Test {
356 protected:
357 // Add a string policy setting to a policy response message.
358 void AddStringPolicy(em::DevicePolicyResponse* policy,
359 const std::string& name,
360 const std::string& value) {
361 em::DevicePolicySetting* setting = policy->add_setting();
362 setting->set_policy_key(kChromeDevicePolicySettingKey);
363 em::GenericSetting* policy_value = setting->mutable_policy_value();
364 em::GenericNamedValue* named_value = policy_value->add_named_value();
365 named_value->set_name(name);
366 em::GenericValue* value_container = named_value->mutable_value();
367 value_container->set_value_type(em::GenericValue::VALUE_TYPE_STRING);
368 value_container->set_string_value(value);
369 }
370 };
371
372 // Tests the device management policy cache.
373 class DeviceManagementPolicyCacheTest
374 : public DeviceManagementPolicyCacheTestBase {
375 protected:
376 DeviceManagementPolicyCacheTest()
377 : loop_(MessageLoop::TYPE_UI),
378 ui_thread_(BrowserThread::UI, &loop_),
379 file_thread_(BrowserThread::FILE, &loop_) {}
380
381 void SetUp() {
382 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
383 }
384
385 void TearDown() {
386 loop_.RunAllPending();
387 }
388
389 void WritePolicy(const em::DevicePolicyResponse& policy,
390 const base::Time& timestamp) {
391 std::string data;
392 em::CachedCloudPolicyResponse cached_policy;
393 cached_policy.mutable_device_policy()->CopyFrom(policy);
394 cached_policy.set_timestamp(timestamp.ToTimeT());
395 EXPECT_TRUE(cached_policy.SerializeToString(&data));
396 int size = static_cast<int>(data.size());
397 EXPECT_EQ(size, file_util::WriteFile(test_file(), data.c_str(), size));
398 }
399
400 FilePath test_file() {
401 return temp_dir_.path().AppendASCII("DeviceManagementPolicyCacheTest");
402 }
403
404 const DictionaryValue* device_policy(const CloudPolicyCache& cache) {
405 return cache.device_policy_.get();
406 }
407
408 MessageLoop loop_;
409
410 private:
411 ScopedTempDir temp_dir_;
412 BrowserThread ui_thread_;
413 BrowserThread file_thread_;
414 };
415
416 TEST_F(DeviceManagementPolicyCacheTest, Empty) {
417 CloudPolicyCache cache(test_file());
418 DictionaryValue empty;
419 EXPECT_TRUE(empty.Equals(device_policy(cache)));
420 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time());
421 }
422
423 TEST_F(DeviceManagementPolicyCacheTest, LoadNoFile) {
424 CloudPolicyCache cache(test_file());
425 cache.LoadFromFile();
426 DictionaryValue empty;
427 EXPECT_TRUE(empty.Equals(device_policy(cache)));
428 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time());
429 }
430
431 TEST_F(DeviceManagementPolicyCacheTest, RejectFuture) {
432 em::DevicePolicyResponse policy_response;
433 WritePolicy(policy_response, base::Time::NowFromSystemTime() +
434 base::TimeDelta::FromMinutes(5));
435 CloudPolicyCache cache(test_file());
436 cache.LoadFromFile();
437 DictionaryValue empty;
438 EXPECT_TRUE(empty.Equals(device_policy(cache)));
439 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time());
440 }
441
442 TEST_F(DeviceManagementPolicyCacheTest, LoadWithFile) {
443 em::DevicePolicyResponse policy_response;
444 WritePolicy(policy_response, base::Time::NowFromSystemTime());
445 CloudPolicyCache cache(test_file());
446 cache.LoadFromFile();
447 DictionaryValue empty;
448 EXPECT_TRUE(empty.Equals(device_policy(cache)));
449 EXPECT_NE(base::Time(), cache.last_policy_refresh_time());
450 EXPECT_GE(base::Time::Now(), cache.last_policy_refresh_time());
451 }
452
453 TEST_F(DeviceManagementPolicyCacheTest, LoadWithData) {
454 em::DevicePolicyResponse policy;
455 AddStringPolicy(&policy, "HomepageLocation", "http://www.example.com");
456 WritePolicy(policy, base::Time::NowFromSystemTime());
457 CloudPolicyCache cache(test_file());
458 cache.LoadFromFile();
459 DictionaryValue expected;
460 expected.Set("HomepageLocation",
461 Value::CreateStringValue("http://www.example.com"));
462 EXPECT_TRUE(expected.Equals(device_policy(cache)));
463 }
464
465 TEST_F(DeviceManagementPolicyCacheTest, SetDevicePolicy) {
466 CloudPolicyCache cache(test_file());
467 em::DevicePolicyResponse policy;
468 AddStringPolicy(&policy, "HomepageLocation", "http://www.example.com");
469 cache.SetDevicePolicy(policy);
470 em::DevicePolicyResponse policy2;
471 AddStringPolicy(&policy2, "HomepageLocation", "http://www.example.com");
472 cache.SetDevicePolicy(policy2); // Does not notify observers.
473 DictionaryValue expected;
474 expected.Set("HomepageLocation",
475 Value::CreateStringValue("http://www.example.com"));
476 EXPECT_TRUE(expected.Equals(device_policy(cache)));
477 }
478
479 TEST_F(DeviceManagementPolicyCacheTest, ResetPolicy) {
480 CloudPolicyCache cache(test_file());
481
482 em::DevicePolicyResponse policy;
483 AddStringPolicy(&policy, "HomepageLocation", "http://www.example.com");
484 cache.SetDevicePolicy(policy);
485 DictionaryValue expected;
486 expected.Set("HomepageLocation",
487 Value::CreateStringValue("http://www.example.com"));
488 EXPECT_TRUE(expected.Equals(device_policy(cache)));
489
490 cache.SetDevicePolicy(em::DevicePolicyResponse());
491 DictionaryValue empty;
492 EXPECT_TRUE(empty.Equals(device_policy(cache)));
493 }
494
495 TEST_F(DeviceManagementPolicyCacheTest, PersistPolicy) {
496 {
497 CloudPolicyCache cache(test_file());
498 em::DevicePolicyResponse policy;
499 AddStringPolicy(&policy, "HomepageLocation", "http://www.example.com");
500 cache.SetDevicePolicy(policy);
501 }
502
503 loop_.RunAllPending();
504
505 EXPECT_TRUE(file_util::PathExists(test_file()));
506 CloudPolicyCache cache(test_file());
507 cache.LoadFromFile();
508 DictionaryValue expected;
509 expected.Set("HomepageLocation",
510 Value::CreateStringValue("http://www.example.com"));
511 EXPECT_TRUE(expected.Equals(device_policy(cache)));
512 }
513
514 TEST_F(DeviceManagementPolicyCacheTest, FreshPolicyOverride) {
515 em::DevicePolicyResponse policy;
516 AddStringPolicy(&policy, "HomepageLocation", "http://www.example.com");
517 WritePolicy(policy, base::Time::NowFromSystemTime());
518
519 CloudPolicyCache cache(test_file());
520 em::DevicePolicyResponse updated_policy;
521 AddStringPolicy(&updated_policy, "HomepageLocation",
522 "http://www.chromium.org");
523 cache.SetDevicePolicy(updated_policy);
524
525 cache.LoadFromFile();
526 DictionaryValue expected;
527 expected.Set("HomepageLocation",
528 Value::CreateStringValue("http://www.chromium.org"));
529 EXPECT_TRUE(expected.Equals(device_policy(cache)));
530 }
531
532 // Tests proper decoding of policy values.
533 class DeviceManagementPolicyCacheDecodeTest
534 : public DeviceManagementPolicyCacheTestBase {
535 protected:
536 void DecodeAndCheck(Value* expected_value_ptr) {
537 scoped_ptr<Value> expected_value(expected_value_ptr);
538 scoped_ptr<Value> decoded_value(
539 CloudPolicyCache::DecodeValue(value_));
540 if (expected_value_ptr) {
541 ASSERT_TRUE(decoded_value.get());
542 EXPECT_TRUE(decoded_value->Equals(expected_value.get()));
543 } else {
544 ASSERT_FALSE(decoded_value.get());
545 }
546 }
547
548 DictionaryValue* DecodeDevicePolicy(const em::DevicePolicyResponse policy) {
549 return CloudPolicyCache::DecodeDevicePolicy(policy);
550 }
551
552 em::GenericValue value_;
553 };
554
555 TEST_F(DeviceManagementPolicyCacheDecodeTest, Bool) {
556 value_.set_value_type(em::GenericValue::VALUE_TYPE_BOOL);
557 value_.set_bool_value(true);
558 DecodeAndCheck(Value::CreateBooleanValue(true));
559 }
560
561 TEST_F(DeviceManagementPolicyCacheDecodeTest, Int64) {
562 value_.set_value_type(em::GenericValue::VALUE_TYPE_INT64);
563 value_.set_int64_value(42);
564 DecodeAndCheck(Value::CreateIntegerValue(42));
565 }
566
567 TEST_F(DeviceManagementPolicyCacheDecodeTest, Int64Overflow) {
568 const int min = std::numeric_limits<int>::min();
569 const int max = std::numeric_limits<int>::max();
570 value_.set_value_type(em::GenericValue::VALUE_TYPE_INT64);
571 value_.set_int64_value(min - 1LL);
572 DecodeAndCheck(NULL);
573 value_.set_int64_value(max + 1LL);
574 DecodeAndCheck(NULL);
575 value_.set_int64_value(min);
576 DecodeAndCheck(Value::CreateIntegerValue(min));
577 value_.set_int64_value(max);
578 DecodeAndCheck(Value::CreateIntegerValue(max));
579 }
580
581 TEST_F(DeviceManagementPolicyCacheDecodeTest, String) {
582 value_.set_value_type(em::GenericValue::VALUE_TYPE_STRING);
583 value_.set_string_value("ponies!");
584 DecodeAndCheck(Value::CreateStringValue("ponies!"));
585 }
586
587 TEST_F(DeviceManagementPolicyCacheDecodeTest, Double) {
588 value_.set_value_type(em::GenericValue::VALUE_TYPE_DOUBLE);
589 value_.set_double_value(0.42L);
590 DecodeAndCheck(Value::CreateDoubleValue(0.42L));
591 }
592
593 TEST_F(DeviceManagementPolicyCacheDecodeTest, Bytes) {
594 std::string data("binary ponies.");
595 value_.set_value_type(em::GenericValue::VALUE_TYPE_BYTES);
596 value_.set_bytes_value(data);
597 DecodeAndCheck(
598 BinaryValue::CreateWithCopiedBuffer(data.c_str(), data.size()));
599 }
600
601 TEST_F(DeviceManagementPolicyCacheDecodeTest, BoolArray) {
602 value_.set_value_type(em::GenericValue::VALUE_TYPE_BOOL_ARRAY);
603 value_.add_bool_array(false);
604 value_.add_bool_array(true);
605 ListValue* list = new ListValue;
606 list->Append(Value::CreateBooleanValue(false));
607 list->Append(Value::CreateBooleanValue(true));
608 DecodeAndCheck(list);
609 }
610
611 TEST_F(DeviceManagementPolicyCacheDecodeTest, Int64Array) {
612 value_.set_value_type(em::GenericValue::VALUE_TYPE_INT64_ARRAY);
613 value_.add_int64_array(42);
614 value_.add_int64_array(17);
615 ListValue* list = new ListValue;
616 list->Append(Value::CreateIntegerValue(42));
617 list->Append(Value::CreateIntegerValue(17));
618 DecodeAndCheck(list);
619 }
620
621 TEST_F(DeviceManagementPolicyCacheDecodeTest, StringArray) {
622 value_.set_value_type(em::GenericValue::VALUE_TYPE_STRING_ARRAY);
623 value_.add_string_array("ponies");
624 value_.add_string_array("more ponies");
625 ListValue* list = new ListValue;
626 list->Append(Value::CreateStringValue("ponies"));
627 list->Append(Value::CreateStringValue("more ponies"));
628 DecodeAndCheck(list);
629 }
630
631 TEST_F(DeviceManagementPolicyCacheDecodeTest, DoubleArray) {
632 value_.set_value_type(em::GenericValue::VALUE_TYPE_DOUBLE_ARRAY);
633 value_.add_double_array(0.42L);
634 value_.add_double_array(0.17L);
635 ListValue* list = new ListValue;
636 list->Append(Value::CreateDoubleValue(0.42L));
637 list->Append(Value::CreateDoubleValue(0.17L));
638 DecodeAndCheck(list);
639 }
640
641 TEST_F(DeviceManagementPolicyCacheDecodeTest, DecodePolicy) {
642 em::DevicePolicyResponse policy;
643 AddStringPolicy(&policy, "HomepageLocation", "http://www.example.com");
644 scoped_ptr<Value> decoded(DecodeDevicePolicy(policy));
645 DictionaryValue expected;
646 expected.Set("HomepageLocation",
647 Value::CreateStringValue("http://www.example.com"));
648 EXPECT_TRUE(expected.Equals(decoded.get()));
649 }
650
651 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/cloud_policy_cache_base.cc ('k') | chrome/browser/policy/cloud_policy_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698