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

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

Issue 13963003: Add standard policy provider tests for PReg policy reading. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 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/policy_loader_win.h" 5 #include "chrome/browser/policy/policy_loader_win.h"
6 6
7 #include <userenv.h> 7 #include <userenv.h>
8 #include <windows.h> 8 #include <windows.h>
9 9
10 #include <algorithm>
11 #include <iterator>
12 #include <vector>
10 #include <cstring> 13 #include <cstring>
Joao da Silva 2013/04/16 10:46:25 nit: order
Mattias Nissler (ping if slow) 2013/04/16 13:00:05 Done.
11 14
12 #include "base/file_util.h" 15 #include "base/file_util.h"
13 #include "base/files/file_path.h" 16 #include "base/files/file_path.h"
14 #include "base/json/json_writer.h" 17 #include "base/json/json_writer.h"
18 #include "base/strings/string_number_conversions.h"
Joao da Silva 2013/04/16 10:46:25 nit: already included
Mattias Nissler (ping if slow) 2013/04/16 13:00:05 Done.
15 #include "base/path_service.h" 19 #include "base/path_service.h"
20 #include "base/sys_byteorder.h"
Joao da Silva 2013/04/16 10:46:25 nit: order
Mattias Nissler (ping if slow) 2013/04/16 13:00:05 Done.
16 #include "base/process.h" 21 #include "base/process.h"
17 #include "base/string16.h" 22 #include "base/string16.h"
18 #include "base/string_util.h" 23 #include "base/string_util.h"
19 #include "base/stringprintf.h" 24 #include "base/stringprintf.h"
25 #include "base/files/scoped_temp_dir.h"
Joao da Silva 2013/04/16 10:46:25 nit: order
Mattias Nissler (ping if slow) 2013/04/16 13:00:05 Done.
20 #include "base/strings/string_number_conversions.h" 26 #include "base/strings/string_number_conversions.h"
21 #include "base/utf_string_conversions.h" 27 #include "base/utf_string_conversions.h"
22 #include "base/win/registry.h" 28 #include "base/win/registry.h"
23 #include "chrome/browser/policy/async_policy_provider.h" 29 #include "chrome/browser/policy/async_policy_provider.h"
30 #include "chrome/browser/policy/preg_parser_win.h"
Joao da Silva 2013/04/16 10:46:25 nit: order
Mattias Nissler (ping if slow) 2013/04/16 13:00:05 Done.
24 #include "chrome/browser/policy/configuration_policy_provider_test.h" 31 #include "chrome/browser/policy/configuration_policy_provider_test.h"
25 #include "chrome/browser/policy/policy_bundle.h" 32 #include "chrome/browser/policy/policy_bundle.h"
26 #include "chrome/browser/policy/policy_map.h" 33 #include "chrome/browser/policy/policy_map.h"
27 #include "chrome/common/chrome_paths.h" 34 #include "chrome/common/chrome_paths.h"
28 #include "chrome/common/json_schema/json_schema_constants.h" 35 #include "chrome/common/json_schema/json_schema_constants.h"
29 #include "policy/policy_constants.h" 36 #include "policy/policy_constants.h"
30 #include "testing/gtest/include/gtest/gtest.h" 37 #include "testing/gtest/include/gtest/gtest.h"
31 38
32 namespace schema = json_schema_constants; 39 namespace schema = json_schema_constants;
33 40
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 std::wstring key_name_; 232 std::wstring key_name_;
226 233
227 // Keys are created for the lifetime of a test to contain 234 // Keys are created for the lifetime of a test to contain
228 // the sandboxed HKCU and HKLM hives, respectively. 235 // the sandboxed HKCU and HKLM hives, respectively.
229 RegKey temp_hkcu_hive_key_; 236 RegKey temp_hkcu_hive_key_;
230 RegKey temp_hklm_hive_key_; 237 RegKey temp_hklm_hive_key_;
231 238
232 DISALLOW_COPY_AND_ASSIGN(ScopedGroupPolicyRegistrySandbox); 239 DISALLOW_COPY_AND_ASSIGN(ScopedGroupPolicyRegistrySandbox);
233 }; 240 };
234 241
235 class TestHarness : public PolicyProviderTestHarness, 242 // A test harness that feeds policy via the Chrome GPO registry subtree.
236 public AppliedGPOListProvider { 243 class RegistryTestHarness : public PolicyProviderTestHarness,
244 public AppliedGPOListProvider {
237 public: 245 public:
238 explicit TestHarness(HKEY hive, PolicyScope scope); 246 explicit RegistryTestHarness(HKEY hive, PolicyScope scope);
Joao da Silva 2013/04/16 10:46:25 Doesn't have to be explicit.
Mattias Nissler (ping if slow) 2013/04/16 13:00:05 Done.
239 virtual ~TestHarness(); 247 virtual ~RegistryTestHarness();
240 248
249 // PolicyProviderTestHarness:
241 virtual void SetUp() OVERRIDE; 250 virtual void SetUp() OVERRIDE;
242 251
243 // PolicyProviderTestHarness:
244 virtual ConfigurationPolicyProvider* CreateProvider( 252 virtual ConfigurationPolicyProvider* CreateProvider(
245 const PolicyDefinitionList* policy_definition_list) OVERRIDE; 253 const PolicyDefinitionList* policy_list) OVERRIDE;
246 254
247 virtual void InstallEmptyPolicy() OVERRIDE; 255 virtual void InstallEmptyPolicy() OVERRIDE;
248 virtual void InstallStringPolicy(const std::string& policy_name, 256 virtual void InstallStringPolicy(const std::string& policy_name,
249 const std::string& policy_value) OVERRIDE; 257 const std::string& policy_value) OVERRIDE;
250 virtual void InstallIntegerPolicy(const std::string& policy_name, 258 virtual void InstallIntegerPolicy(const std::string& policy_name,
251 int policy_value) OVERRIDE; 259 int policy_value) OVERRIDE;
252 virtual void InstallBooleanPolicy(const std::string& policy_name, 260 virtual void InstallBooleanPolicy(const std::string& policy_name,
253 bool policy_value) OVERRIDE; 261 bool policy_value) OVERRIDE;
254 virtual void InstallStringListPolicy( 262 virtual void InstallStringListPolicy(
255 const std::string& policy_name, 263 const std::string& policy_name,
(...skipping 15 matching lines...) Expand all
271 // Creates a harness instance that will install policy in HKCU or HKLM, 279 // Creates a harness instance that will install policy in HKCU or HKLM,
272 // respectively. 280 // respectively.
273 static PolicyProviderTestHarness* CreateHKCU(); 281 static PolicyProviderTestHarness* CreateHKCU();
274 static PolicyProviderTestHarness* CreateHKLM(); 282 static PolicyProviderTestHarness* CreateHKLM();
275 283
276 private: 284 private:
277 HKEY hive_; 285 HKEY hive_;
278 286
279 ScopedGroupPolicyRegistrySandbox registry_sandbox_; 287 ScopedGroupPolicyRegistrySandbox registry_sandbox_;
280 288
281 DISALLOW_COPY_AND_ASSIGN(TestHarness); 289 DISALLOW_COPY_AND_ASSIGN(RegistryTestHarness);
290 };
291
292 // A test harness that generates PReg files for the provider to read.
293 class PRegTestHarness : public PolicyProviderTestHarness,
294 public AppliedGPOListProvider {
295 public:
296 explicit PRegTestHarness();
Joao da Silva 2013/04/16 10:46:25 Doesn't have to be explicit.
Mattias Nissler (ping if slow) 2013/04/16 13:00:05 Done.
297 virtual ~PRegTestHarness();
298
299 // PolicyProviderTestHarness:
300 virtual void SetUp() OVERRIDE;
301
302 virtual ConfigurationPolicyProvider* CreateProvider(
303 const PolicyDefinitionList* policy_list) OVERRIDE;
304
305 virtual void InstallEmptyPolicy() OVERRIDE;
306 virtual void InstallStringPolicy(const std::string& policy_name,
307 const std::string& policy_value) OVERRIDE;
308 virtual void InstallIntegerPolicy(const std::string& policy_name,
309 int policy_value) OVERRIDE;
310 virtual void InstallBooleanPolicy(const std::string& policy_name,
311 bool policy_value) OVERRIDE;
312 virtual void InstallStringListPolicy(
313 const std::string& policy_name,
314 const base::ListValue* policy_value) OVERRIDE;
315 virtual void InstallDictionaryPolicy(
316 const std::string& policy_name,
317 const base::DictionaryValue* policy_value) OVERRIDE;
318 virtual void Install3rdPartyPolicy(
319 const base::DictionaryValue* policies) OVERRIDE;
320
321 // AppliedGPOListProvider:
322 virtual DWORD GetAppliedGPOList(DWORD flags,
323 LPCTSTR machine_name,
324 PSID sid_user,
325 GUID* extension_guid,
326 PGROUP_POLICY_OBJECT* gpo_list) OVERRIDE;
327 virtual BOOL FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) OVERRIDE;
328
329 // Creates a harness instance.
330 static PolicyProviderTestHarness* Create();
331
332 private:
333 // Helper to append a string16 to an uint8 buffer.
334 static void AppendChars(std::vector<uint8>* buffer, const string16& chars);
335
336 // Appends a record with the given fields to the PReg file.
337 void AppendRecordToPRegFile(const string16& path,
338 const std::string& key,
339 DWORD type,
340 DWORD size,
341 uint8* data);
342
343 // Appends the given DWORD |value| for |path| + |key| to the PReg file.
344 void AppendDWORDToPRegFile(const string16& path,
345 const std::string& key,
346 DWORD value);
347
348 // Appends the given string |value| for |path| + |key| to the PReg file.
349 void AppendStringToPRegFile(const string16& path,
350 const std::string& key,
351 const std::string& value);
352
353 // Appends the given policy |value| for |path| + |key| to the PReg file,
354 // converting and recursing as necessary.
355 void AppendPolicyToPRegFile(const string16& path,
356 const std::string& key,
357 const base::Value* value);
358
359 PolicyScope scope_;
Joao da Silva 2013/04/16 10:46:25 This isn't used.
Mattias Nissler (ping if slow) 2013/04/16 13:00:05 Done.
360 base::ScopedTempDir temp_dir_;
361 base::FilePath preg_file_path_;
362 GROUP_POLICY_OBJECT gpo_;
363
364 DISALLOW_COPY_AND_ASSIGN(PRegTestHarness);
282 }; 365 };
283 366
284 ScopedGroupPolicyRegistrySandbox::ScopedGroupPolicyRegistrySandbox() { 367 ScopedGroupPolicyRegistrySandbox::ScopedGroupPolicyRegistrySandbox() {
285 // Generate a unique registry key for the override for each test. This 368 // Generate a unique registry key for the override for each test. This
286 // makes sure that tests executing in parallel won't delete each other's 369 // makes sure that tests executing in parallel won't delete each other's
287 // key, at DeleteKeys(). 370 // key, at DeleteKeys().
288 key_name_ = ASCIIToWide(base::StringPrintf( 371 key_name_ = ASCIIToWide(base::StringPrintf(
289 "SOFTWARE\\chromium unittest %d", 372 "SOFTWARE\\chromium unittest %d",
290 base::Process::Current().pid())); 373 base::Process::Current().pid()));
291 std::wstring hklm_key_name = key_name_ + L"\\HKLM"; 374 std::wstring hklm_key_name = key_name_ + L"\\HKLM";
(...skipping 27 matching lines...) Expand all
319 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_LOCAL_MACHINE, 0)); 402 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_LOCAL_MACHINE, 0));
320 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_CURRENT_USER, 0)); 403 ASSERT_HRESULT_SUCCEEDED(RegOverridePredefKey(HKEY_CURRENT_USER, 0));
321 } 404 }
322 405
323 void ScopedGroupPolicyRegistrySandbox::DeleteKeys() { 406 void ScopedGroupPolicyRegistrySandbox::DeleteKeys() {
324 RegKey key(HKEY_CURRENT_USER, key_name_.c_str(), KEY_ALL_ACCESS); 407 RegKey key(HKEY_CURRENT_USER, key_name_.c_str(), KEY_ALL_ACCESS);
325 ASSERT_TRUE(key.Valid()); 408 ASSERT_TRUE(key.Valid());
326 key.DeleteKey(L""); 409 key.DeleteKey(L"");
327 } 410 }
328 411
329 TestHarness::TestHarness(HKEY hive, PolicyScope scope) 412 RegistryTestHarness::RegistryTestHarness(HKEY hive, PolicyScope scope)
330 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, scope), hive_(hive) {} 413 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, scope), hive_(hive) {}
331 414
332 TestHarness::~TestHarness() {} 415 RegistryTestHarness::~RegistryTestHarness() {}
333 416
334 void TestHarness::SetUp() {} 417 void RegistryTestHarness::SetUp() {}
335 418
336 DWORD TestHarness::GetAppliedGPOList(DWORD flags, 419 ConfigurationPolicyProvider* RegistryTestHarness::CreateProvider(
337 LPCTSTR machine_name,
338 PSID sid_user,
339 GUID* extension_guid,
340 PGROUP_POLICY_OBJECT* gpo_list) {
341 *gpo_list = NULL;
342 return ERROR_ACCESS_DENIED;
343 }
344
345 BOOL TestHarness::FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) {
346 return TRUE;
347 }
348
349 ConfigurationPolicyProvider* TestHarness::CreateProvider(
350 const PolicyDefinitionList* policy_list) { 420 const PolicyDefinitionList* policy_list) {
351 scoped_ptr<AsyncPolicyLoader> loader( 421 scoped_ptr<AsyncPolicyLoader> loader(
352 new PolicyLoaderWin(policy_list, kRegistryChromePolicyKey, this)); 422 new PolicyLoaderWin(policy_list, kRegistryChromePolicyKey, this));
353 return new AsyncPolicyProvider(loader.Pass()); 423 return new AsyncPolicyProvider(loader.Pass());
354 } 424 }
355 425
356 void TestHarness::InstallEmptyPolicy() {} 426 void RegistryTestHarness::InstallEmptyPolicy() {}
357 427
358 void TestHarness::InstallStringPolicy(const std::string& policy_name, 428 void RegistryTestHarness::InstallStringPolicy(
359 const std::string& policy_value) { 429 const std::string& policy_name,
430 const std::string& policy_value) {
360 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS); 431 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS);
361 ASSERT_TRUE(key.Valid()); 432 ASSERT_TRUE(key.Valid());
362 ASSERT_HRESULT_SUCCEEDED(key.WriteValue(UTF8ToUTF16(policy_name).c_str(), 433 ASSERT_HRESULT_SUCCEEDED(key.WriteValue(UTF8ToUTF16(policy_name).c_str(),
363 UTF8ToUTF16(policy_value).c_str())); 434 UTF8ToUTF16(policy_value).c_str()));
364 } 435 }
365 436
366 void TestHarness::InstallIntegerPolicy(const std::string& policy_name, 437 void RegistryTestHarness::InstallIntegerPolicy(
367 int policy_value) { 438 const std::string& policy_name,
439 int policy_value) {
368 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS); 440 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS);
369 ASSERT_TRUE(key.Valid()); 441 ASSERT_TRUE(key.Valid());
370 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), 442 key.WriteValue(UTF8ToUTF16(policy_name).c_str(),
371 static_cast<DWORD>(policy_value)); 443 static_cast<DWORD>(policy_value));
372 } 444 }
373 445
374 void TestHarness::InstallBooleanPolicy(const std::string& policy_name, 446 void RegistryTestHarness::InstallBooleanPolicy(
375 bool policy_value) { 447 const std::string& policy_name,
448 bool policy_value) {
376 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS); 449 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS);
377 ASSERT_TRUE(key.Valid()); 450 ASSERT_TRUE(key.Valid());
378 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), 451 key.WriteValue(UTF8ToUTF16(policy_name).c_str(),
379 static_cast<DWORD>(policy_value)); 452 static_cast<DWORD>(policy_value));
380 } 453 }
381 454
382 void TestHarness::InstallStringListPolicy(const std::string& policy_name, 455 void RegistryTestHarness::InstallStringListPolicy(
383 const base::ListValue* policy_value) { 456 const std::string& policy_name,
457 const base::ListValue* policy_value) {
384 RegKey key(hive_, 458 RegKey key(hive_,
385 (string16(kRegistryChromePolicyKey) + ASCIIToUTF16("\\") + 459 (string16(kRegistryChromePolicyKey) + ASCIIToUTF16("\\") +
386 UTF8ToUTF16(policy_name)).c_str(), 460 UTF8ToUTF16(policy_name)).c_str(),
387 KEY_ALL_ACCESS); 461 KEY_ALL_ACCESS);
388 ASSERT_TRUE(key.Valid()); 462 ASSERT_TRUE(key.Valid());
389 int index = 1; 463 int index = 1;
390 for (base::ListValue::const_iterator element(policy_value->begin()); 464 for (base::ListValue::const_iterator element(policy_value->begin());
391 element != policy_value->end(); 465 element != policy_value->end();
392 ++element) { 466 ++element) {
393 std::string element_value; 467 std::string element_value;
394 if (!(*element)->GetAsString(&element_value)) 468 if (!(*element)->GetAsString(&element_value))
395 continue; 469 continue;
396 std::string name(base::IntToString(index++)); 470 std::string name(base::IntToString(index++));
397 key.WriteValue(UTF8ToUTF16(name).c_str(), 471 key.WriteValue(UTF8ToUTF16(name).c_str(),
398 UTF8ToUTF16(element_value).c_str()); 472 UTF8ToUTF16(element_value).c_str());
399 } 473 }
400 } 474 }
401 475
402 void TestHarness::InstallDictionaryPolicy( 476 void RegistryTestHarness::InstallDictionaryPolicy(
403 const std::string& policy_name, 477 const std::string& policy_name,
404 const base::DictionaryValue* policy_value) { 478 const base::DictionaryValue* policy_value) {
405 std::string json; 479 std::string json;
406 base::JSONWriter::Write(policy_value, &json); 480 base::JSONWriter::Write(policy_value, &json);
407 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS); 481 RegKey key(hive_, kRegistryChromePolicyKey, KEY_ALL_ACCESS);
408 ASSERT_TRUE(key.Valid()); 482 ASSERT_TRUE(key.Valid());
409 key.WriteValue(UTF8ToUTF16(policy_name).c_str(), 483 key.WriteValue(UTF8ToUTF16(policy_name).c_str(),
410 UTF8ToUTF16(json).c_str()); 484 UTF8ToUTF16(json).c_str());
411 } 485 }
412 486
413 void TestHarness::Install3rdPartyPolicy(const base::DictionaryValue* policies) { 487 void RegistryTestHarness::Install3rdPartyPolicy(
488 const base::DictionaryValue* policies) {
414 // The first level entries are domains, and the second level entries map 489 // The first level entries are domains, and the second level entries map
415 // components to their policy. 490 // components to their policy.
416 const string16 kPathPrefix = string16(kRegistryChromePolicyKey) + kPathSep + 491 const string16 kPathPrefix = string16(kRegistryChromePolicyKey) + kPathSep +
417 kThirdParty + kPathSep; 492 kThirdParty + kPathSep;
418 for (base::DictionaryValue::Iterator domain(*policies); 493 for (base::DictionaryValue::Iterator domain(*policies);
419 !domain.IsAtEnd(); domain.Advance()) { 494 !domain.IsAtEnd(); domain.Advance()) {
420 const base::DictionaryValue* components = NULL; 495 const base::DictionaryValue* components = NULL;
421 if (!domain.value().GetAsDictionary(&components)) { 496 if (!domain.value().GetAsDictionary(&components)) {
422 ADD_FAILURE(); 497 ADD_FAILURE();
423 continue; 498 continue;
424 } 499 }
425 for (base::DictionaryValue::Iterator component(*components); 500 for (base::DictionaryValue::Iterator component(*components);
426 !component.IsAtEnd(); component.Advance()) { 501 !component.IsAtEnd(); component.Advance()) {
427 const string16 path = string16(kRegistryChromePolicyKey) + kPathSep + 502 const string16 path = kPathPrefix +
428 kThirdParty + kPathSep +
429 UTF8ToUTF16(domain.key()) + kPathSep + 503 UTF8ToUTF16(domain.key()) + kPathSep +
430 UTF8ToUTF16(component.key()); 504 UTF8ToUTF16(component.key());
431 InstallValue(component.value(), hive_, path, kMandatory); 505 InstallValue(component.value(), hive_, path, kMandatory);
432 EXPECT_TRUE(InstallSchema(component.value(), hive_, path, kSchema)); 506 EXPECT_TRUE(InstallSchema(component.value(), hive_, path, kSchema));
433 } 507 }
434 } 508 }
435 } 509 }
436 510
437 // static 511 DWORD RegistryTestHarness::GetAppliedGPOList(DWORD flags,
438 PolicyProviderTestHarness* TestHarness::CreateHKCU() { 512 LPCTSTR machine_name,
439 return new TestHarness(HKEY_CURRENT_USER, POLICY_SCOPE_USER); 513 PSID sid_user,
440 } 514 GUID* extension_guid,
441 515 PGROUP_POLICY_OBJECT* gpo_list) {
Joao da Silva 2013/04/16 10:46:25 nit: indent
Mattias Nissler (ping if slow) 2013/04/16 13:00:05 Done.
442 // static 516 *gpo_list = NULL;
443 PolicyProviderTestHarness* TestHarness::CreateHKLM() { 517 return ERROR_ACCESS_DENIED;
444 return new TestHarness(HKEY_LOCAL_MACHINE, POLICY_SCOPE_MACHINE); 518 }
519
520 BOOL RegistryTestHarness::FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) {
521 return TRUE;
522 }
523
524 // static
525 PolicyProviderTestHarness* RegistryTestHarness::CreateHKCU() {
526 return new RegistryTestHarness(HKEY_CURRENT_USER, POLICY_SCOPE_USER);
527 }
528
529 // static
530 PolicyProviderTestHarness* RegistryTestHarness::CreateHKLM() {
531 return new RegistryTestHarness(HKEY_LOCAL_MACHINE, POLICY_SCOPE_MACHINE);
532 }
533
534 PRegTestHarness::PRegTestHarness()
535 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE) {}
536
537 PRegTestHarness::~PRegTestHarness() {
538 temp_dir_.Take();
Joao da Silva 2013/04/16 10:46:25 Why? Did you intend to remove this before uploadin
Mattias Nissler (ping if slow) 2013/04/16 13:00:05 This happens if you only get to work on a given th
539 }
540
541 void PRegTestHarness::SetUp() {
542 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
543 preg_file_path_ = temp_dir_.path().Append(PolicyLoaderWin::kPRegFileName);
544 ASSERT_TRUE(file_util::WriteFile(preg_file_path_,
545 preg_parser::kPRegFileHeader,
546 arraysize(preg_parser::kPRegFileHeader)));
547
548 memset(&gpo_, 0, sizeof(GROUP_POLICY_OBJECT));
549 gpo_.lpFileSysPath = const_cast<wchar_t*>(temp_dir_.path().value().c_str());
550 }
551
552 ConfigurationPolicyProvider* PRegTestHarness::CreateProvider(
553 const PolicyDefinitionList* policy_list) {
554 scoped_ptr<AsyncPolicyLoader> loader(
555 new PolicyLoaderWin(policy_list, kRegistryChromePolicyKey, this));
556 return new AsyncPolicyProvider(loader.Pass());
557 }
558
559 void PRegTestHarness::InstallEmptyPolicy() {}
560
561 void PRegTestHarness::InstallStringPolicy(const std::string& policy_name,
562 const std::string& policy_value) {
563 AppendStringToPRegFile(kRegistryChromePolicyKey, policy_name, policy_value);
564 }
565
566 void PRegTestHarness::InstallIntegerPolicy(const std::string& policy_name,
567 int policy_value) {
568 AppendDWORDToPRegFile(kRegistryChromePolicyKey, policy_name, policy_value);
569 }
570
571 void PRegTestHarness::InstallBooleanPolicy(const std::string& policy_name,
572 bool policy_value) {
573 AppendDWORDToPRegFile(kRegistryChromePolicyKey, policy_name, policy_value);
574 }
575
576 void PRegTestHarness::InstallStringListPolicy(
577 const std::string& policy_name,
578 const base::ListValue* policy_value) {
579 AppendPolicyToPRegFile(kRegistryChromePolicyKey, policy_name, policy_value);
580 }
581
582 void PRegTestHarness::InstallDictionaryPolicy(
583 const std::string& policy_name,
584 const base::DictionaryValue* policy_value) {
585 std::string json;
586 base::JSONWriter::Write(policy_value, &json);
587 AppendStringToPRegFile(kRegistryChromePolicyKey, policy_name, json);
588 }
589
590 void PRegTestHarness::Install3rdPartyPolicy(
591 const base::DictionaryValue* policies) {
592 // The first level entries are domains, and the second level entries map
593 // components to their policy.
594 const string16 kPathPrefix = string16(kRegistryChromePolicyKey) + kPathSep +
595 kThirdParty + kPathSep;
596 for (base::DictionaryValue::Iterator domain(*policies);
597 !domain.IsAtEnd(); domain.Advance()) {
598 const base::DictionaryValue* components = NULL;
599 if (!domain.value().GetAsDictionary(&components)) {
600 ADD_FAILURE();
601 continue;
602 }
603 const string16 domain_path = kPathPrefix + UTF8ToUTF16(domain.key());
604 for (base::DictionaryValue::Iterator component(*components);
605 !component.IsAtEnd(); component.Advance()) {
606 const string16 component_path =
607 domain_path + kPathSep + UTF8ToUTF16(component.key());
608 AppendPolicyToPRegFile(component_path, UTF16ToUTF8(kMandatory),
609 &component.value());
610
611 scoped_ptr<base::DictionaryValue> schema_dict(
612 BuildSchema(component.value()));
613 std::string schema_json;
614 base::JSONWriter::Write(schema_dict.get(), &schema_json);
615 if (!schema_json.empty()) {
616 AppendStringToPRegFile(component_path, UTF16ToUTF8(kSchema),
617 schema_json);
618 }
619 }
620 }
621 }
622
623 DWORD PRegTestHarness::GetAppliedGPOList(DWORD flags,
624 LPCTSTR machine_name,
625 PSID sid_user,
626 GUID* extension_guid,
627 PGROUP_POLICY_OBJECT* gpo_list) {
628 *gpo_list = flags & GPO_LIST_FLAG_MACHINE ? &gpo_ : NULL;
629 return ERROR_SUCCESS;
630 }
631
632 BOOL PRegTestHarness::FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) {
633 return TRUE;
634 }
635
636 // static
637 PolicyProviderTestHarness* PRegTestHarness::Create() {
638 return new PRegTestHarness();
639 }
640
641 // static
642 void PRegTestHarness::AppendChars(std::vector<uint8>* buffer,
643 const string16& chars) {
644 for (string16::const_iterator c(chars.begin()); c != chars.end(); ++c) {
645 buffer->push_back(*c & 0xff);
646 buffer->push_back((*c >> 8) & 0xff);
647 }
648 }
649
650 void PRegTestHarness::AppendRecordToPRegFile(const string16& path,
651 const std::string& key,
652 DWORD type,
653 DWORD size,
654 uint8* data) {
655 std::vector<uint8> buffer;
656 AppendChars(&buffer, L"[");
657 AppendChars(&buffer, path);
658 AppendChars(&buffer, string16(L"\0;", 2));
659 AppendChars(&buffer, UTF8ToUTF16(key));
660 AppendChars(&buffer, string16(L"\0;", 2));
661 uint8* type_data = reinterpret_cast<uint8*>(&type);
662 buffer.insert(buffer.end(), type_data, type_data + sizeof(DWORD));
663 AppendChars(&buffer, L";");
664 uint8* size_data = reinterpret_cast<uint8*>(&size);
665 buffer.insert(buffer.end(), size_data, size_data + sizeof(DWORD));
666 AppendChars(&buffer, L";");
667 buffer.insert(buffer.end(), data, data + size);
668 AppendChars(&buffer, L"]");
669
670 ASSERT_EQ(buffer.size(),
671 file_util::AppendToFile(
672 preg_file_path_,
673 reinterpret_cast<const char*>(vector_as_array(&buffer)),
674 buffer.size()));
675 }
676
677 void PRegTestHarness::AppendDWORDToPRegFile(const string16& path,
678 const std::string& key,
679 DWORD value) {
680 AppendRecordToPRegFile(path, key, REG_DWORD, sizeof(DWORD),
681 reinterpret_cast<uint8*>(&value));
682 }
683
684 void PRegTestHarness::AppendStringToPRegFile(const string16& path,
685 const std::string& key,
686 const std::string& value) {
687 string16 string16_value(UTF8ToUTF16(value));
688 std::vector<char16> data;
689 std::transform(string16_value.begin(), string16_value.end(),
690 std::back_inserter(data), std::ptr_fun(base::ByteSwapToLE16));
Joao da Silva 2013/04/16 10:46:25 #include <functional>
Mattias Nissler (ping if slow) 2013/04/16 13:00:05 Done.
691 data.push_back(base::ByteSwapToLE16(L'\0'));
692
693 AppendRecordToPRegFile(path, key, REG_SZ, data.size() * sizeof(char16),
694 reinterpret_cast<uint8*>(vector_as_array(&data)));
695 }
696
697 void PRegTestHarness::AppendPolicyToPRegFile(const string16& path,
698 const std::string& key,
699 const base::Value* value) {
700 switch (value->GetType()) {
701 case base::Value::TYPE_BOOLEAN: {
702 bool boolean_value = false;
703 ASSERT_TRUE(value->GetAsBoolean(&boolean_value));
704 AppendDWORDToPRegFile(path, key, boolean_value);
705 break;
706 }
707 case base::Value::TYPE_INTEGER: {
708 int int_value = 0;
709 ASSERT_TRUE(value->GetAsInteger(&int_value));
710 AppendDWORDToPRegFile(path, key, int_value);
711 break;
712 }
713 case base::Value::TYPE_DOUBLE: {
714 double double_value = 0;
715 std::string string_value;
716 ASSERT_TRUE(value->GetAsDouble(&double_value));
717 AppendStringToPRegFile(path, key, base::DoubleToString(double_value));
718 break;
719 }
720 case base::Value::TYPE_STRING: {
721 std::string string_value;
722 ASSERT_TRUE(value->GetAsString(&string_value));
723 AppendStringToPRegFile(path, key, string_value);
724 break;
725 }
726 case base::Value::TYPE_DICTIONARY: {
727 string16 subpath = path + kPathSep + UTF8ToUTF16(key);
728 const base::DictionaryValue* dict = NULL;
729 ASSERT_TRUE(value->GetAsDictionary(&dict));
730 for (base::DictionaryValue::Iterator entry(*dict); !entry.IsAtEnd();
731 entry.Advance()) {
732 AppendPolicyToPRegFile(subpath, entry.key(), &entry.value());
733 }
734 break;
735 }
736 case base::Value::TYPE_LIST: {
737 string16 subpath = path + kPathSep + UTF8ToUTF16(key);
738 const base::ListValue* list = NULL;
739 ASSERT_TRUE(value->GetAsList(&list));
740 for (size_t i = 0; i < list->GetSize(); ++i) {
741 const base::Value* entry = NULL;
742 ASSERT_TRUE(list->Get(i, &entry));
743 AppendPolicyToPRegFile(subpath, base::IntToString(i + 1), entry);
744 }
745 break;
746 }
747 case base::Value::TYPE_BINARY:
748 case base::Value::TYPE_NULL: {
749 ADD_FAILURE();
750 break;
751 }
752 }
445 } 753 }
446 754
447 } // namespace 755 } // namespace
448 756
449 // Instantiate abstract test case for basic policy reading tests. 757 // Instantiate abstract test case for basic policy reading tests.
450 INSTANTIATE_TEST_CASE_P( 758 INSTANTIATE_TEST_CASE_P(
451 PolicyProviderWinTest, 759 PolicyProviderWinTest,
452 ConfigurationPolicyProviderTest, 760 ConfigurationPolicyProviderTest,
453 testing::Values(TestHarness::CreateHKCU, TestHarness::CreateHKLM)); 761 testing::Values(RegistryTestHarness::CreateHKCU,
762 RegistryTestHarness::CreateHKLM,
763 PRegTestHarness::Create));
454 764
455 // Instantiate abstract test case for 3rd party policy reading tests. 765 // Instantiate abstract test case for 3rd party policy reading tests.
456 INSTANTIATE_TEST_CASE_P( 766 INSTANTIATE_TEST_CASE_P(
457 ThirdPartyPolicyProviderWinTest, 767 ThirdPartyPolicyProviderWinTest,
458 Configuration3rdPartyPolicyProviderTest, 768 Configuration3rdPartyPolicyProviderTest,
459 testing::Values(TestHarness::CreateHKCU, TestHarness::CreateHKLM)); 769 testing::Values(RegistryTestHarness::CreateHKCU,
770 RegistryTestHarness::CreateHKLM,
771 PRegTestHarness::Create));
460 772
461 // Test cases for windows policy provider specific functionality. 773 // Test cases for windows policy provider specific functionality.
462 class PolicyLoaderWinTest : public PolicyTestBase, 774 class PolicyLoaderWinTest : public PolicyTestBase,
463 public AppliedGPOListProvider { 775 public AppliedGPOListProvider {
464 protected: 776 protected:
465 // The policy key this tests places data under. This must match the data 777 // The policy key this tests places data under. This must match the data
466 // files in chrome/test/data/policy/gpo. 778 // files in chrome/test/data/policy/gpo.
467 static const char16 kTestPolicyKey[]; 779 static const char16 kTestPolicyKey[];
468 780
469 PolicyLoaderWinTest() 781 PolicyLoaderWinTest()
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after
851 GROUP_POLICY_OBJECT gpo2; 1163 GROUP_POLICY_OBJECT gpo2;
852 InitGPO(&gpo1, 0, gpo_dir, &gpo2, NULL); 1164 InitGPO(&gpo1, 0, gpo_dir, &gpo2, NULL);
853 InitGPO(&gpo2, 0, unc_path, NULL, &gpo1); 1165 InitGPO(&gpo2, 0, unc_path, NULL, &gpo1);
854 gpo_list_ = &gpo1; 1166 gpo_list_ = &gpo1;
855 gpo_list_status_ = ERROR_SUCCESS; 1167 gpo_list_status_ = ERROR_SUCCESS;
856 1168
857 EXPECT_TRUE(MatchesRegistrySentinel()); 1169 EXPECT_TRUE(MatchesRegistrySentinel());
858 } 1170 }
859 1171
860 } // namespace policy 1172 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698