| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 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 "app/test/data/resource.h" |
| 6 #include "base/registry.h" |
| 7 #include "base/string_util.h" |
| 8 #include "base/scoped_ptr.h" |
| 9 #include "base/values.h" |
| 10 #include "chrome/browser/group_policy.h" |
| 11 #include "chrome/browser/group_policy_settings.h" |
| 12 #include "chrome/installer/util/browser_distribution.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 class GroupPolicyTest : public testing::Test { |
| 16 protected: |
| 17 bool can_set_hklm_; |
| 18 bool can_set_hkcu_; |
| 19 |
| 20 virtual void SetUp() { |
| 21 std::wstring policy_key(L""); |
| 22 group_policy::GetPolicySettingsRootKey(&policy_key); |
| 23 policy_key.append(L"\\Preferences\\"); |
| 24 RegKey hkcu = RegKey(HKEY_CURRENT_USER, policy_key.c_str(), KEY_WRITE); |
| 25 RegKey hklm = RegKey(HKEY_LOCAL_MACHINE, policy_key.c_str(), KEY_WRITE); |
| 26 can_set_hkcu_ = hkcu.Valid(); |
| 27 can_set_hklm_ = hklm.Valid(); |
| 28 |
| 29 if (can_set_hkcu_) { |
| 30 EXPECT_TRUE(hkcu.WriteValue(L"Homepage", L"www.google.com/hkcu")); |
| 31 EXPECT_TRUE(hkcu.WriteValue(L"HomepageIsNewTabPage", (DWORD)0)); |
| 32 } |
| 33 if (can_set_hklm_) { |
| 34 EXPECT_TRUE(hklm.WriteValue(L"Homepage", L"www.google.com/hklm")); |
| 35 EXPECT_TRUE(hklm.WriteValue(L"HomepageIsNewTabPage", (DWORD)1)); |
| 36 } |
| 37 policy_key.clear(); |
| 38 group_policy::GetPolicySettingsRootKey(&policy_key); |
| 39 policy_key.append(L"\\ChromeFrameDomainWhiteList\\"); |
| 40 if (can_set_hkcu_) { |
| 41 RegKey hkcu_list = RegKey(HKEY_CURRENT_USER, |
| 42 policy_key.c_str(), |
| 43 KEY_WRITE); |
| 44 EXPECT_TRUE(hkcu_list.Valid()); |
| 45 EXPECT_TRUE(hkcu_list.WriteValue(L"www.google.com/test1", |
| 46 L"www.google.com/test1")); |
| 47 EXPECT_TRUE(hkcu_list.WriteValue(L"www.google.com/test2", |
| 48 L"www.google.com/test2")); |
| 49 EXPECT_TRUE(hkcu_list.WriteValue(L"www.google.com/test3", |
| 50 L"www.google.com/test3")); |
| 51 } |
| 52 if (can_set_hklm_) { |
| 53 RegKey hklm_list = RegKey(HKEY_LOCAL_MACHINE, |
| 54 policy_key.c_str(), |
| 55 KEY_WRITE); |
| 56 |
| 57 EXPECT_TRUE(hklm_list.Valid()); |
| 58 EXPECT_TRUE(hklm_list.WriteValue(L"www.google.com/testa", |
| 59 L"www.google.com/testa")); |
| 60 EXPECT_TRUE(hklm_list.WriteValue(L"www.google.com/testb", |
| 61 L"www.google.com/testb")); |
| 62 EXPECT_TRUE(hklm_list.WriteValue(L"www.google.com/testc", |
| 63 L"www.google.com/testc")); |
| 64 } |
| 65 } |
| 66 |
| 67 virtual void TearDown() { |
| 68 // Delete the appropriate registry keys. |
| 69 std::wstring policy_key(L""); |
| 70 group_policy::GetPolicySettingsRootKey(&policy_key); |
| 71 policy_key.append(L"\\Preferences\\"); |
| 72 RegKey hkcu = RegKey(HKEY_CURRENT_USER, policy_key.c_str(), KEY_WRITE); |
| 73 RegKey hklm = RegKey(HKEY_LOCAL_MACHINE, policy_key.c_str(), KEY_WRITE); |
| 74 if (can_set_hkcu_) { |
| 75 EXPECT_TRUE(hkcu.Valid()); |
| 76 hkcu.DeleteValue(L"Homepage"); |
| 77 hkcu.DeleteValue(L"HomepageIsNewTabPage"); |
| 78 } |
| 79 if (can_set_hklm_) { |
| 80 EXPECT_TRUE(hklm.Valid()); |
| 81 hklm.DeleteValue(L"Homepage"); |
| 82 hklm.DeleteValue(L"HomepageIsNewTabPage"); |
| 83 } |
| 84 policy_key.clear(); |
| 85 group_policy::GetPolicySettingsRootKey(&policy_key); |
| 86 if (can_set_hkcu_) { |
| 87 RegKey hkcu_list = RegKey(HKEY_CURRENT_USER, |
| 88 policy_key.c_str(), |
| 89 KEY_WRITE); |
| 90 EXPECT_TRUE(hkcu_list.DeleteKey(L"ChromeFrameDomainWhiteList")); |
| 91 } |
| 92 if (can_set_hklm_) { |
| 93 RegKey hklm_list = RegKey(HKEY_LOCAL_MACHINE, |
| 94 policy_key.c_str(), |
| 95 KEY_WRITE); |
| 96 EXPECT_TRUE(hklm_list.DeleteKey(L"ChromeFrameDomainWhiteList")); |
| 97 } |
| 98 } |
| 99 }; |
| 100 |
| 101 TEST_F(GroupPolicyTest, TestGetPolicyKey) { |
| 102 std::wstring mystring(L""); |
| 103 group_policy::GetPolicySettingsRootKey(&mystring); |
| 104 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 105 std::wstring app_name = dist->GetApplicationName(); |
| 106 std::wstring publisher_name = dist->GetPublisherName(); |
| 107 EXPECT_NE(-1, mystring.find(app_name)); |
| 108 EXPECT_NE(-1, mystring.find(publisher_name)); |
| 109 } |
| 110 |
| 111 TEST_F(GroupPolicyTest, TestGetGroupPolicyString) { |
| 112 std::wstring setting = std::wstring(L""); |
| 113 bool found = false; |
| 114 if (can_set_hklm_ || can_set_hkcu_) { |
| 115 EXPECT_TRUE(group_policy::gpHomepage.IsPolicyControlled()); |
| 116 HRESULT hr = group_policy::gpHomepage.GetSetting(&setting, &found); |
| 117 EXPECT_TRUE(found); |
| 118 EXPECT_TRUE(SUCCEEDED(hr)); |
| 119 if (can_set_hklm_) |
| 120 EXPECT_EQ(0, setting.compare(L"www.google.com/hklm")); |
| 121 else |
| 122 EXPECT_EQ(0, setting.compare(L"www.google.com/hkcu")); |
| 123 } |
| 124 } |
| 125 |
| 126 TEST_F(GroupPolicyTest, TestGetGroupPolicyBool) { |
| 127 bool setting = false; |
| 128 bool found = false; |
| 129 if (can_set_hklm_ || can_set_hkcu_) { |
| 130 EXPECT_TRUE(group_policy::gpHomepageIsNewTabPage.IsPolicyControlled()); |
| 131 HRESULT hr = group_policy::gpHomepageIsNewTabPage.GetSetting(&setting, |
| 132 &found); |
| 133 EXPECT_TRUE(found); |
| 134 EXPECT_TRUE(SUCCEEDED(hr)); |
| 135 EXPECT_TRUE(setting); |
| 136 } |
| 137 } |
| 138 |
| 139 TEST_F(GroupPolicyTest, TestGetStringList) { |
| 140 scoped_ptr<ListValue> mixed_list(new ListValue()); |
| 141 bool found = false; |
| 142 if (can_set_hklm_ || can_set_hkcu_) { |
| 143 // Because of the way the lists of strings are appended, |
| 144 // HKCU appears before HKLM, and they appear in reverse order. |
| 145 std::string test1 = std::string("www.google.com/testc"); |
| 146 std::string test2 = std::string("www.google.com/testb"); |
| 147 std::string test3 = std::string("www.google.com/testa"); |
| 148 std::string test4 = std::string("www.google.com/test3"); |
| 149 std::string test5 = std::string("www.google.com/test2"); |
| 150 std::string test6 = std::string("www.google.com/test1"); |
| 151 HRESULT hr = \ |
| 152 group_policy::gpChromeFrameDomainWhiteList.GetSetting(mixed_list.get(), |
| 153 &found); |
| 154 EXPECT_TRUE(found); |
| 155 EXPECT_TRUE(SUCCEEDED(hr)); |
| 156 EXPECT_EQ((can_set_hklm_ ? 6 : 3), mixed_list->GetSize()); |
| 157 std::string string_value; |
| 158 |
| 159 if (can_set_hklm_) { |
| 160 EXPECT_TRUE(mixed_list->GetString(0, &string_value)); |
| 161 EXPECT_EQ(0, string_value.compare(test1)); |
| 162 EXPECT_EQ(string_value, test1); |
| 163 |
| 164 EXPECT_TRUE(mixed_list->GetString(1, &string_value)); |
| 165 EXPECT_EQ(0, string_value.compare(test2)); |
| 166 EXPECT_EQ(string_value, test2); |
| 167 |
| 168 EXPECT_TRUE(mixed_list->GetString(2, &string_value)); |
| 169 EXPECT_EQ(0, string_value.compare(test3)); |
| 170 EXPECT_EQ(string_value, test3); |
| 171 |
| 172 EXPECT_TRUE(mixed_list->GetString(3, &string_value)); |
| 173 EXPECT_EQ(0, string_value.compare(test4)); |
| 174 EXPECT_EQ(string_value, test4); |
| 175 |
| 176 EXPECT_TRUE(mixed_list->GetString(4, &string_value)); |
| 177 EXPECT_EQ(0, string_value.compare(test5)); |
| 178 EXPECT_EQ(string_value, test5); |
| 179 |
| 180 EXPECT_TRUE(mixed_list->GetString(5, &string_value)); |
| 181 EXPECT_EQ(0, string_value.compare(test6)); |
| 182 EXPECT_EQ(string_value, test6); |
| 183 } else { |
| 184 EXPECT_TRUE(mixed_list->GetString(0, &string_value)); |
| 185 EXPECT_EQ(0, string_value.compare(test4)); |
| 186 EXPECT_EQ(string_value, test4); |
| 187 |
| 188 EXPECT_TRUE(mixed_list->GetString(1, &string_value)); |
| 189 EXPECT_EQ(0, string_value.compare(test5)); |
| 190 EXPECT_EQ(string_value, test5); |
| 191 |
| 192 EXPECT_TRUE(mixed_list->GetString(2, &string_value)); |
| 193 EXPECT_EQ(0, string_value.compare(test6)); |
| 194 EXPECT_EQ(string_value, test6); |
| 195 } |
| 196 } |
| 197 } |
| 198 |
| OLD | NEW |