OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <gtest/gtest.h> | 5 #include <gtest/gtest.h> |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
10 #include "base/stl_util-inl.h" | 10 #include "base/stl_util-inl.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 using base::win::RegKey; | 22 using base::win::RegKey; |
23 | 23 |
24 namespace policy { | 24 namespace policy { |
25 | 25 |
26 const wchar_t kUnitTestRegistrySubKey[] = L"SOFTWARE\\Chromium Unit Tests"; | 26 const wchar_t kUnitTestRegistrySubKey[] = L"SOFTWARE\\Chromium Unit Tests"; |
27 const wchar_t kUnitTestMachineOverrideSubKey[] = | 27 const wchar_t kUnitTestMachineOverrideSubKey[] = |
28 L"SOFTWARE\\Chromium Unit Tests\\HKLM Override"; | 28 L"SOFTWARE\\Chromium Unit Tests\\HKLM Override"; |
29 const wchar_t kUnitTestUserOverrideSubKey[] = | 29 const wchar_t kUnitTestUserOverrideSubKey[] = |
30 L"SOFTWARE\\Chromium Unit Tests\\HKCU Override"; | 30 L"SOFTWARE\\Chromium Unit Tests\\HKCU Override"; |
31 | 31 |
| 32 namespace { |
| 33 |
32 // Holds policy type, corresponding policy name string and a valid value for use | 34 // Holds policy type, corresponding policy name string and a valid value for use |
33 // in parametrized value tests. | 35 // in parametrized value tests. |
34 class PolicyTestParams { | 36 class PolicyTestParams { |
35 public: | 37 public: |
36 // Assumes ownership of |hklm_value| and |hkcu_value|. | 38 // Assumes ownership of |hklm_value| and |hkcu_value|. |
37 PolicyTestParams(ConfigurationPolicyStore::PolicyType type, | 39 PolicyTestParams(ConfigurationPolicyType type, |
38 const char* policy_name, | 40 const char* policy_name, |
39 Value* hklm_value, | 41 Value* hklm_value, |
40 Value* hkcu_value) | 42 Value* hkcu_value) |
41 : type_(type), | 43 : type_(type), |
42 policy_name_(policy_name), | 44 policy_name_(policy_name), |
43 hklm_value_(hklm_value), | 45 hklm_value_(hklm_value), |
44 hkcu_value_(hkcu_value) {} | 46 hkcu_value_(hkcu_value) {} |
45 | 47 |
46 // testing::TestWithParam does copy the parameters, so provide copy | 48 // testing::TestWithParam does copy the parameters, so provide copy |
47 // constructor and assignment operator. | 49 // constructor and assignment operator. |
48 PolicyTestParams(const PolicyTestParams& other) | 50 PolicyTestParams(const PolicyTestParams& other) |
49 : type_(other.type_), | 51 : type_(other.type_), |
50 policy_name_(other.policy_name_), | 52 policy_name_(other.policy_name_), |
51 hklm_value_(other.hklm_value_->DeepCopy()), | 53 hklm_value_(other.hklm_value_->DeepCopy()), |
52 hkcu_value_(other.hkcu_value_->DeepCopy()) {} | 54 hkcu_value_(other.hkcu_value_->DeepCopy()) {} |
53 | 55 |
54 const PolicyTestParams& operator=(PolicyTestParams other) { | 56 const PolicyTestParams& operator=(PolicyTestParams other) { |
55 swap(other); | 57 swap(other); |
56 return *this; | 58 return *this; |
57 } | 59 } |
58 | 60 |
59 void swap(PolicyTestParams& other) { | 61 void swap(PolicyTestParams& other) { |
60 std::swap(type_, other.type_); | 62 std::swap(type_, other.type_); |
61 std::swap(policy_name_, other.policy_name_); | 63 std::swap(policy_name_, other.policy_name_); |
62 hklm_value_.swap(other.hklm_value_); | 64 hklm_value_.swap(other.hklm_value_); |
63 hkcu_value_.swap(other.hkcu_value_); | 65 hkcu_value_.swap(other.hkcu_value_); |
64 } | 66 } |
65 | 67 |
66 ConfigurationPolicyStore::PolicyType type() const { return type_; } | 68 ConfigurationPolicyType type() const { return type_; } |
67 const char* policy_name() const { return policy_name_; } | 69 const char* policy_name() const { return policy_name_; } |
68 const Value* hklm_value() const { return hklm_value_.get(); } | 70 const Value* hklm_value() const { return hklm_value_.get(); } |
69 const Value* hkcu_value() const { return hkcu_value_.get(); } | 71 const Value* hkcu_value() const { return hkcu_value_.get(); } |
70 | 72 |
71 // Factory methods for different value types. | 73 // Factory methods for different value types. |
72 static PolicyTestParams ForStringPolicy( | 74 static PolicyTestParams ForStringPolicy( |
73 ConfigurationPolicyStore::PolicyType type, | 75 ConfigurationPolicyType type, |
74 const char* policy_name) { | 76 const char* policy_name) { |
75 return PolicyTestParams(type, | 77 return PolicyTestParams(type, |
76 policy_name, | 78 policy_name, |
77 Value::CreateStringValue("string_a"), | 79 Value::CreateStringValue("string_a"), |
78 Value::CreateStringValue("string_b")); | 80 Value::CreateStringValue("string_b")); |
79 } | 81 } |
80 static PolicyTestParams ForBooleanPolicy( | 82 static PolicyTestParams ForBooleanPolicy( |
81 ConfigurationPolicyStore::PolicyType type, | 83 ConfigurationPolicyType type, |
82 const char* policy_name) { | 84 const char* policy_name) { |
83 return PolicyTestParams(type, | 85 return PolicyTestParams(type, |
84 policy_name, | 86 policy_name, |
85 Value::CreateBooleanValue(true), | 87 Value::CreateBooleanValue(true), |
86 Value::CreateBooleanValue(false)); | 88 Value::CreateBooleanValue(false)); |
87 } | 89 } |
88 static PolicyTestParams ForIntegerPolicy( | 90 static PolicyTestParams ForIntegerPolicy( |
89 ConfigurationPolicyStore::PolicyType type, | 91 ConfigurationPolicyType type, |
90 const char* policy_name) { | 92 const char* policy_name) { |
91 return PolicyTestParams(type, | 93 return PolicyTestParams(type, |
92 policy_name, | 94 policy_name, |
93 Value::CreateIntegerValue(42), | 95 Value::CreateIntegerValue(42), |
94 Value::CreateIntegerValue(17)); | 96 Value::CreateIntegerValue(17)); |
95 } | 97 } |
96 static PolicyTestParams ForListPolicy( | 98 static PolicyTestParams ForListPolicy( |
97 ConfigurationPolicyStore::PolicyType type, | 99 ConfigurationPolicyType type, |
98 const char* policy_name) { | 100 const char* policy_name) { |
99 ListValue* hklm_value = new ListValue; | 101 ListValue* hklm_value = new ListValue; |
100 hklm_value->Set(0U, Value::CreateStringValue("It's a plane!")); | 102 hklm_value->Set(0U, Value::CreateStringValue("It's a plane!")); |
101 ListValue* hkcu_value = new ListValue; | 103 ListValue* hkcu_value = new ListValue; |
102 hkcu_value->Set(0U, Value::CreateStringValue("It's a bird!")); | 104 hkcu_value->Set(0U, Value::CreateStringValue("It's a bird!")); |
103 hkcu_value->Set(0U, Value::CreateStringValue("It's a flying carpet!")); | 105 hkcu_value->Set(0U, Value::CreateStringValue("It's a flying carpet!")); |
104 return PolicyTestParams(type, policy_name, hklm_value, hkcu_value); | 106 return PolicyTestParams(type, policy_name, hklm_value, hkcu_value); |
105 } | 107 } |
106 | 108 |
107 private: | 109 private: |
108 ConfigurationPolicyStore::PolicyType type_; | 110 ConfigurationPolicyType type_; |
109 const char* policy_name_; | 111 const char* policy_name_; |
110 scoped_ptr<Value> hklm_value_; | 112 scoped_ptr<Value> hklm_value_; |
111 scoped_ptr<Value> hkcu_value_; | 113 scoped_ptr<Value> hkcu_value_; |
112 }; | 114 }; |
113 | 115 |
| 116 } // namespace |
| 117 |
114 // This test class provides sandboxing and mocking for the parts of the | 118 // This test class provides sandboxing and mocking for the parts of the |
115 // Windows Registry implementing Group Policy. The |SetUp| method prepares | 119 // Windows Registry implementing Group Policy. The |SetUp| method prepares |
116 // two temporary sandbox keys in |kUnitTestRegistrySubKey|, one for HKLM and one | 120 // two temporary sandbox keys in |kUnitTestRegistrySubKey|, one for HKLM and one |
117 // for HKCU. A test's calls to the registry are redirected by Windows to these | 121 // for HKCU. A test's calls to the registry are redirected by Windows to these |
118 // sandboxes, allowing the tests to manipulate and access policy as if it | 122 // sandboxes, allowing the tests to manipulate and access policy as if it |
119 // were active, but without actually changing the parts of the Registry that | 123 // were active, but without actually changing the parts of the Registry that |
120 // are managed by Group Policy. | 124 // are managed by Group Policy. |
121 class ConfigurationPolicyProviderWinTest | 125 class ConfigurationPolicyProviderWinTest |
122 : public testing::TestWithParam<PolicyTestParams> { | 126 : public testing::TestWithParam<PolicyTestParams> { |
123 public: | 127 public: |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 case Value::TYPE_STRING: { | 252 case Value::TYPE_STRING: { |
249 std::string v; | 253 std::string v; |
250 ASSERT_TRUE(value->GetAsString(&v)); | 254 ASSERT_TRUE(value->GetAsString(&v)); |
251 WriteString(hive, name, UTF8ToUTF16(v).c_str()); | 255 WriteString(hive, name, UTF8ToUTF16(v).c_str()); |
252 break; | 256 break; |
253 } | 257 } |
254 case Value::TYPE_LIST: { | 258 case Value::TYPE_LIST: { |
255 const ListValue* list = static_cast<const ListValue*>(value); | 259 const ListValue* list = static_cast<const ListValue*>(value); |
256 RegKey key(hive, | 260 RegKey key(hive, |
257 (string16(policy::kRegistrySubKey) + ASCIIToUTF16("\\") + | 261 (string16(policy::kRegistrySubKey) + ASCIIToUTF16("\\") + |
258 UTF8ToUTF16(name)).c_str(), | 262 UTF8ToUTF16(name)).c_str(), |
259 KEY_ALL_ACCESS); | 263 KEY_ALL_ACCESS); |
260 int index = 1; | 264 int index = 1; |
261 for (ListValue::const_iterator element(list->begin()); | 265 for (ListValue::const_iterator element(list->begin()); |
262 element != list->end(); ++element) { | 266 element != list->end(); ++element) { |
263 ASSERT_TRUE((*element)->IsType(Value::TYPE_STRING)); | 267 ASSERT_TRUE((*element)->IsType(Value::TYPE_STRING)); |
264 std::string element_value; | 268 std::string element_value; |
265 ASSERT_TRUE((*element)->GetAsString(&element_value)); | 269 ASSERT_TRUE((*element)->GetAsString(&element_value)); |
266 key.WriteValue(base::IntToString16(index++).c_str(), | 270 key.WriteValue(base::IntToString16(index++).c_str(), |
267 UTF8ToUTF16(element_value).c_str()); | 271 UTF8ToUTF16(element_value).c_str()); |
268 } | 272 } |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 ASSERT_TRUE(value); | 335 ASSERT_TRUE(value); |
332 EXPECT_TRUE(value->Equals(GetParam().hklm_value())); | 336 EXPECT_TRUE(value->Equals(GetParam().hklm_value())); |
333 } | 337 } |
334 | 338 |
335 // Instantiate the test case for all supported policies. | 339 // Instantiate the test case for all supported policies. |
336 INSTANTIATE_TEST_CASE_P( | 340 INSTANTIATE_TEST_CASE_P( |
337 ConfigurationPolicyProviderWinTestInstance, | 341 ConfigurationPolicyProviderWinTestInstance, |
338 ConfigurationPolicyProviderWinTest, | 342 ConfigurationPolicyProviderWinTest, |
339 testing::Values( | 343 testing::Values( |
340 PolicyTestParams::ForStringPolicy( | 344 PolicyTestParams::ForStringPolicy( |
341 ConfigurationPolicyStore::kPolicyHomePage, | 345 kPolicyHomePage, |
342 key::kHomepageLocation), | 346 key::kHomepageLocation), |
343 PolicyTestParams::ForBooleanPolicy( | 347 PolicyTestParams::ForBooleanPolicy( |
344 ConfigurationPolicyStore::kPolicyHomepageIsNewTabPage, | 348 kPolicyHomepageIsNewTabPage, |
345 key::kHomepageIsNewTabPage), | 349 key::kHomepageIsNewTabPage), |
346 PolicyTestParams::ForIntegerPolicy( | 350 PolicyTestParams::ForIntegerPolicy( |
347 ConfigurationPolicyStore::kPolicyRestoreOnStartup, | 351 kPolicyRestoreOnStartup, |
348 key::kRestoreOnStartup), | 352 key::kRestoreOnStartup), |
349 PolicyTestParams::ForListPolicy( | 353 PolicyTestParams::ForListPolicy( |
350 ConfigurationPolicyStore::kPolicyURLsToRestoreOnStartup, | 354 kPolicyURLsToRestoreOnStartup, |
351 key::kURLsToRestoreOnStartup), | 355 key::kURLsToRestoreOnStartup), |
352 PolicyTestParams::ForBooleanPolicy( | 356 PolicyTestParams::ForBooleanPolicy( |
353 ConfigurationPolicyStore::kPolicyDefaultSearchProviderEnabled, | 357 kPolicyDefaultSearchProviderEnabled, |
354 key::kDefaultSearchProviderEnabled), | 358 key::kDefaultSearchProviderEnabled), |
355 PolicyTestParams::ForStringPolicy( | 359 PolicyTestParams::ForStringPolicy( |
356 ConfigurationPolicyStore::kPolicyDefaultSearchProviderName, | 360 kPolicyDefaultSearchProviderName, |
357 key::kDefaultSearchProviderName), | 361 key::kDefaultSearchProviderName), |
358 PolicyTestParams::ForStringPolicy( | 362 PolicyTestParams::ForStringPolicy( |
359 ConfigurationPolicyStore::kPolicyDefaultSearchProviderKeyword, | 363 kPolicyDefaultSearchProviderKeyword, |
360 key::kDefaultSearchProviderKeyword), | 364 key::kDefaultSearchProviderKeyword), |
361 PolicyTestParams::ForStringPolicy( | 365 PolicyTestParams::ForStringPolicy( |
362 ConfigurationPolicyStore::kPolicyDefaultSearchProviderSearchURL, | 366 kPolicyDefaultSearchProviderSearchURL, |
363 key::kDefaultSearchProviderSearchURL), | 367 key::kDefaultSearchProviderSearchURL), |
364 PolicyTestParams::ForStringPolicy( | 368 PolicyTestParams::ForStringPolicy( |
365 ConfigurationPolicyStore::kPolicyDefaultSearchProviderSuggestURL, | 369 kPolicyDefaultSearchProviderSuggestURL, |
366 key::kDefaultSearchProviderSuggestURL), | 370 key::kDefaultSearchProviderSuggestURL), |
367 PolicyTestParams::ForStringPolicy( | 371 PolicyTestParams::ForStringPolicy( |
368 ConfigurationPolicyStore::kPolicyDefaultSearchProviderIconURL, | 372 kPolicyDefaultSearchProviderIconURL, |
369 key::kDefaultSearchProviderIconURL), | 373 key::kDefaultSearchProviderIconURL), |
370 PolicyTestParams::ForStringPolicy( | 374 PolicyTestParams::ForStringPolicy( |
371 ConfigurationPolicyStore::kPolicyDefaultSearchProviderEncodings, | 375 kPolicyDefaultSearchProviderEncodings, |
372 key::kDefaultSearchProviderEncodings), | 376 key::kDefaultSearchProviderEncodings), |
373 PolicyTestParams::ForIntegerPolicy( | 377 PolicyTestParams::ForIntegerPolicy( |
374 ConfigurationPolicyStore::kPolicyProxyServerMode, | 378 kPolicyProxyServerMode, |
375 key::kProxyServerMode), | 379 key::kProxyServerMode), |
376 PolicyTestParams::ForStringPolicy( | 380 PolicyTestParams::ForStringPolicy( |
377 ConfigurationPolicyStore::kPolicyProxyServer, | 381 kPolicyProxyServer, |
378 key::kProxyServer), | 382 key::kProxyServer), |
379 PolicyTestParams::ForStringPolicy( | 383 PolicyTestParams::ForStringPolicy( |
380 ConfigurationPolicyStore::kPolicyProxyPacUrl, | 384 kPolicyProxyPacUrl, |
381 key::kProxyPacUrl), | 385 key::kProxyPacUrl), |
382 PolicyTestParams::ForStringPolicy( | 386 PolicyTestParams::ForStringPolicy( |
383 ConfigurationPolicyStore::kPolicyProxyBypassList, | 387 kPolicyProxyBypassList, |
384 key::kProxyBypassList), | 388 key::kProxyBypassList), |
385 PolicyTestParams::ForBooleanPolicy( | 389 PolicyTestParams::ForBooleanPolicy( |
386 ConfigurationPolicyStore::kPolicyAlternateErrorPagesEnabled, | 390 kPolicyAlternateErrorPagesEnabled, |
387 key::kAlternateErrorPagesEnabled), | 391 key::kAlternateErrorPagesEnabled), |
388 PolicyTestParams::ForBooleanPolicy( | 392 PolicyTestParams::ForBooleanPolicy( |
389 ConfigurationPolicyStore::kPolicySearchSuggestEnabled, | 393 kPolicySearchSuggestEnabled, |
390 key::kSearchSuggestEnabled), | 394 key::kSearchSuggestEnabled), |
391 PolicyTestParams::ForBooleanPolicy( | 395 PolicyTestParams::ForBooleanPolicy( |
392 ConfigurationPolicyStore::kPolicyDnsPrefetchingEnabled, | 396 kPolicyDnsPrefetchingEnabled, |
393 key::kDnsPrefetchingEnabled), | 397 key::kDnsPrefetchingEnabled), |
394 PolicyTestParams::ForBooleanPolicy( | 398 PolicyTestParams::ForBooleanPolicy( |
395 ConfigurationPolicyStore::kPolicySafeBrowsingEnabled, | 399 kPolicySafeBrowsingEnabled, |
396 key::kSafeBrowsingEnabled), | 400 key::kSafeBrowsingEnabled), |
397 PolicyTestParams::ForBooleanPolicy( | 401 PolicyTestParams::ForBooleanPolicy( |
398 ConfigurationPolicyStore::kPolicyMetricsReportingEnabled, | 402 kPolicyMetricsReportingEnabled, |
399 key::kMetricsReportingEnabled), | 403 key::kMetricsReportingEnabled), |
400 PolicyTestParams::ForBooleanPolicy( | 404 PolicyTestParams::ForBooleanPolicy( |
401 ConfigurationPolicyStore::kPolicyPasswordManagerEnabled, | 405 kPolicyPasswordManagerEnabled, |
402 key::kPasswordManagerEnabled), | 406 key::kPasswordManagerEnabled), |
403 PolicyTestParams::ForListPolicy( | 407 PolicyTestParams::ForListPolicy( |
404 ConfigurationPolicyStore::kPolicyDisabledPlugins, | 408 kPolicyDisabledPlugins, |
405 key::kDisabledPlugins), | 409 key::kDisabledPlugins), |
406 PolicyTestParams::ForBooleanPolicy( | 410 PolicyTestParams::ForBooleanPolicy( |
407 ConfigurationPolicyStore::kPolicyAutoFillEnabled, | 411 kPolicyAutoFillEnabled, |
408 key::kAutoFillEnabled), | 412 key::kAutoFillEnabled), |
409 PolicyTestParams::ForBooleanPolicy( | 413 PolicyTestParams::ForBooleanPolicy( |
410 ConfigurationPolicyStore::kPolicySyncDisabled, | 414 kPolicySyncDisabled, |
411 key::kSyncDisabled), | 415 key::kSyncDisabled), |
412 PolicyTestParams::ForStringPolicy( | 416 PolicyTestParams::ForStringPolicy( |
413 ConfigurationPolicyStore::kPolicyApplicationLocale, | 417 kPolicyApplicationLocale, |
414 key::kApplicationLocaleValue), | 418 key::kApplicationLocaleValue), |
415 PolicyTestParams::ForListPolicy( | 419 PolicyTestParams::ForListPolicy( |
416 ConfigurationPolicyStore::kPolicyExtensionInstallAllowList, | 420 kPolicyExtensionInstallAllowList, |
417 key::kExtensionInstallAllowList), | 421 key::kExtensionInstallAllowList), |
418 PolicyTestParams::ForListPolicy( | 422 PolicyTestParams::ForListPolicy( |
419 ConfigurationPolicyStore::kPolicyExtensionInstallDenyList, | 423 kPolicyExtensionInstallDenyList, |
420 key::kExtensionInstallDenyList), | 424 key::kExtensionInstallDenyList), |
421 PolicyTestParams::ForBooleanPolicy( | 425 PolicyTestParams::ForBooleanPolicy( |
422 ConfigurationPolicyStore::kPolicyShowHomeButton, | 426 kPolicyShowHomeButton, |
423 key::kShowHomeButton), | 427 key::kShowHomeButton), |
424 PolicyTestParams::ForBooleanPolicy( | 428 PolicyTestParams::ForBooleanPolicy( |
425 ConfigurationPolicyStore::kPolicyPrintingEnabled, | 429 kPolicyPrintingEnabled, |
426 key::kPrintingEnabled))); | 430 key::kPrintingEnabled))); |
427 | 431 |
428 } // namespace policy | 432 } // namespace policy |
OLD | NEW |