1// Copyright (c) 2013 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 "components/policy/core/common/preg_parser_win.h" 6 7#include "base/base_paths.h" 8#include "base/files/file_path.h" 9#include "base/json/json_writer.h" 10#include "base/logging.h" 11#include "base/path_service.h" 12#include "base/values.h" 13#include "components/policy/core/common/policy_load_status.h" 14#include "components/policy/core/common/registry_dict_win.h" 15#include "testing/gtest/include/gtest/gtest.h" 16 17namespace policy { 18namespace preg_parser { 19namespace { 20 21// Check whether two RegistryDicts equal each other. 22testing::AssertionResult RegistryDictEquals(const RegistryDict& a, 23 const RegistryDict& b) { 24 RegistryDict::KeyMap::const_iterator iter_key_a(a.keys().begin()); 25 RegistryDict::KeyMap::const_iterator iter_key_b(b.keys().begin()); 26 for (; iter_key_a != a.keys().end() && iter_key_b != b.keys().end(); 27 ++iter_key_a, ++iter_key_b) { 28 if (iter_key_a->first != iter_key_b->first) { 29 return testing::AssertionFailure() 30 << "Key mismatch " << iter_key_a->first 31 << " vs. " << iter_key_b->first; 32 } 33 testing::AssertionResult result = RegistryDictEquals(*iter_key_a->second, 34 *iter_key_b->second); 35 if (!result) 36 return result; 37 } 38 39 RegistryDict::ValueMap::const_iterator iter_value_a(a.values().begin()); 40 RegistryDict::ValueMap::const_iterator iter_value_b(b.values().begin()); 41 for (; iter_value_a != a.values().end() && iter_value_b != b.values().end(); 42 ++iter_value_a, ++iter_value_b) { 43 if (iter_value_a->first != iter_value_b->first || 44 !base::Value::Equals(iter_value_a->second, iter_value_b->second)) { 45 return testing::AssertionFailure() 46 << "Value mismatch " 47 << iter_value_a->first << "=" << *iter_value_a->second 48 << " vs. " << iter_value_b->first << "=" << *iter_value_b->second; 49 } 50 } 51 52 return testing::AssertionSuccess(); 53} 54 55void SetInteger(RegistryDict* dict, 56 const std::string& name, 57 int value) { 58 dict->SetValue( 59 name, 60 make_scoped_ptr<base::Value>(new base::FundamentalValue(value))); 61} 62 63void SetString(RegistryDict* dict, 64 const std::string& name, 65 const std::string& value) { 66 dict->SetValue( 67 name, 68 make_scoped_ptr<base::Value>(new base::StringValue(value))); 69} 70 71TEST(PRegParserWinTest, TestParseFile) { 72 base::FilePath test_data_dir; 73 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir)); 74 75 // Prepare the test dictionary with some data so the test can check that the 76 // PReg action triggers work, i.e. remove these items. 77 RegistryDict dict; 78 SetInteger(&dict, "DeleteValuesTest1", 1); 79 SetString(&dict, "DeleteValuesTest2", "2"); 80 dict.SetKey("DeleteKeysTest1", make_scoped_ptr(new RegistryDict())); 81 scoped_ptr<RegistryDict> delete_keys_test(new RegistryDict()); 82 SetInteger(delete_keys_test.get(), "DeleteKeysTest2Entry", 1); 83 dict.SetKey("DeleteKeysTest2", delete_keys_test.Pass()); 84 SetInteger(&dict, "DelTest", 1); 85 scoped_ptr<RegistryDict> subdict(new RegistryDict()); 86 SetInteger(subdict.get(), "DelValsTest1", 1); 87 SetString(subdict.get(), "DelValsTest2", "2"); 88 subdict->SetKey("DelValsTest3", make_scoped_ptr(new RegistryDict())); 89 dict.SetKey("DelValsTest", subdict.Pass()); 90 91 // Run the parser. 92 base::FilePath test_file( 93 test_data_dir.AppendASCII("chrome/test/data/policy/registry.pol")); 94 PolicyLoadStatusSample status; 95 ASSERT_TRUE(preg_parser::ReadFile( 96 test_file, L"SOFTWARE\\Policies\\Chromium", &dict, &status)); 97 98 // Build the expected output dictionary. 99 RegistryDict expected; 100 scoped_ptr<RegistryDict> del_vals_dict(new RegistryDict()); 101 del_vals_dict->SetKey("DelValsTest3", make_scoped_ptr(new RegistryDict())); 102 expected.SetKey("DelValsTest", del_vals_dict.Pass()); 103 SetInteger(&expected, "HomepageIsNewTabPage", 1); 104 SetString(&expected, "HomepageLocation", "http://www.example.com"); 105 SetInteger(&expected, "RestoreOnStartup", 4); 106 scoped_ptr<RegistryDict> startup_urls(new RegistryDict()); 107 SetString(startup_urls.get(), "1", "http://www.chromium.org"); 108 SetString(startup_urls.get(), "2", "http://www.example.com"); 109 expected.SetKey("RestoreOnStartupURLs", startup_urls.Pass()); 110 SetInteger(&expected, "ShowHomeButton", 1); 111 SetString(&expected, "Snowman", "\xE2\x98\x83"); 112 SetString(&expected, "Empty", ""); 113 114 EXPECT_TRUE(RegistryDictEquals(dict, expected)); 115} 116 117} // namespace 118} // namespace preg_parser 119} // namespace policy 120