OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/at_exit.h" | |
6 #include "base/basictypes.h" | |
7 #include "base/logging.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/strings/stringprintf.h" | |
10 #include "base/strings/utf_string_conversions.h" | |
11 #include "base/win/registry.h" | |
12 #include "chrome_frame/policy_settings.h" | |
13 #include "chrome_frame/test/chrome_frame_test_utils.h" | |
14 #include "content/public/common/content_switches.h" | |
15 #include "policy/policy_constants.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 using base::ASCIIToWide; | |
19 using base::win::RegKey; | |
20 using chrome_frame_test::ScopedVirtualizeHklmAndHkcu; | |
21 | |
22 namespace { | |
23 | |
24 // A best effort way to zap CF policy entries that may be in the registry. | |
25 void DeleteChromeFramePolicyEntries(HKEY root) { | |
26 RegKey key; | |
27 if (key.Open(root, policy::kRegistryChromePolicyKey, | |
28 KEY_ALL_ACCESS) == ERROR_SUCCESS) { | |
29 key.DeleteValue( | |
30 ASCIIToWide(policy::key::kChromeFrameRendererSettings).c_str()); | |
31 key.DeleteKey(ASCIIToWide(policy::key::kRenderInChromeFrameList).c_str()); | |
32 key.DeleteKey(ASCIIToWide(policy::key::kRenderInHostList).c_str()); | |
33 key.DeleteKey(ASCIIToWide(policy::key::kChromeFrameContentTypes).c_str()); | |
34 key.DeleteValue(ASCIIToWide(policy::key::kApplicationLocaleValue).c_str()); | |
35 key.DeleteValue( | |
36 ASCIIToWide(policy::key::kSuppressChromeFrameTurndownPrompt).c_str()); | |
37 } | |
38 } | |
39 | |
40 bool InitializePolicyKey(HKEY policy_root, RegKey* policy_key) { | |
41 EXPECT_EQ(ERROR_SUCCESS, policy_key->Create(policy_root, | |
42 policy::kRegistryChromePolicyKey, KEY_ALL_ACCESS)); | |
43 return policy_key->Valid(); | |
44 } | |
45 | |
46 void WritePolicyList(RegKey* policy_key, | |
47 const wchar_t* list_name, | |
48 const wchar_t* values[], int count) { | |
49 DCHECK(policy_key); | |
50 // Remove any previous settings | |
51 policy_key->DeleteKey(list_name); | |
52 | |
53 RegKey list_key; | |
54 EXPECT_EQ(ERROR_SUCCESS, list_key.Create(policy_key->Handle(), list_name, | |
55 KEY_ALL_ACCESS)); | |
56 for (int i = 0; i < count; ++i) { | |
57 EXPECT_EQ(ERROR_SUCCESS, | |
58 list_key.WriteValue(base::StringPrintf(L"%i", i).c_str(), values[i])); | |
59 } | |
60 } | |
61 | |
62 bool SetRendererSettings(HKEY policy_root, | |
63 PolicySettings::RendererForUrl renderer, | |
64 const wchar_t* exclusions[], | |
65 int exclusion_count) { | |
66 RegKey policy_key; | |
67 if (!InitializePolicyKey(policy_root, &policy_key)) | |
68 return false; | |
69 | |
70 policy_key.WriteValue( | |
71 ASCIIToWide(policy::key::kChromeFrameRendererSettings).c_str(), | |
72 static_cast<DWORD>(renderer)); | |
73 | |
74 std::wstring in_cf(ASCIIToWide(policy::key::kRenderInChromeFrameList)); | |
75 std::wstring in_host(ASCIIToWide(policy::key::kRenderInHostList)); | |
76 std::wstring exclusion_list( | |
77 renderer == PolicySettings::RENDER_IN_CHROME_FRAME ? in_host : in_cf); | |
78 WritePolicyList(&policy_key, exclusion_list.c_str(), exclusions, | |
79 exclusion_count); | |
80 | |
81 return true; | |
82 } | |
83 | |
84 bool SetCFContentTypes(HKEY policy_root, | |
85 const wchar_t* content_types[], | |
86 int count) { | |
87 RegKey policy_key; | |
88 if (!InitializePolicyKey(policy_root, &policy_key)) | |
89 return false; | |
90 | |
91 std::wstring type_list(ASCIIToWide(policy::key::kChromeFrameContentTypes)); | |
92 WritePolicyList(&policy_key, type_list.c_str(), content_types, count); | |
93 | |
94 return true; | |
95 } | |
96 | |
97 bool SetCFPolicyString(HKEY policy_root, | |
98 const char* policy_name, | |
99 const wchar_t* value) { | |
100 RegKey policy_key; | |
101 if (!InitializePolicyKey(policy_root, &policy_key)) | |
102 return false; | |
103 | |
104 std::wstring policy_name_str(ASCIIToWide(policy_name)); | |
105 EXPECT_EQ(ERROR_SUCCESS, | |
106 policy_key.WriteValue(policy_name_str.c_str(), value)); | |
107 return true; | |
108 } | |
109 | |
110 void SetCFPolicyBool(HKEY policy_root, | |
111 const char* policy_name, | |
112 bool value) { | |
113 RegKey policy_key; | |
114 if (InitializePolicyKey(policy_root, &policy_key)) { | |
115 std::wstring policy_name_str(ASCIIToWide(policy_name)); | |
116 EXPECT_EQ(ERROR_SUCCESS, | |
117 policy_key.WriteValue(policy_name_str.c_str(), value ? 1U : 0U)); | |
118 } | |
119 } | |
120 | |
121 } // end namespace | |
122 | |
123 class PolicySettingsTest : public testing::Test { | |
124 protected: | |
125 void SetUp() { | |
126 ResetPolicySettings(); | |
127 } | |
128 | |
129 void TearDown() { | |
130 } | |
131 | |
132 void ResetPolicySettings() { | |
133 //at_exit_manager_.ProcessCallbacksNow(); | |
134 DeleteAllSingletons(); | |
135 } | |
136 | |
137 // This is used to manage life cycle of PolicySettings singleton. | |
138 // base::ShadowingAtExitManager at_exit_manager_; | |
139 | |
140 ScopedVirtualizeHklmAndHkcu registry_virtualization_; | |
141 }; | |
142 | |
143 TEST_F(PolicySettingsTest, RendererForUrl) { | |
144 const wchar_t* kTestUrls[] = { | |
145 L"http://www.example.com", | |
146 L"http://www.pattern.com", | |
147 L"http://www.test.com" | |
148 }; | |
149 const wchar_t* kTestFilters[] = { | |
150 L"*.example.com", | |
151 L"*.pattern.com", | |
152 L"*.test.com" | |
153 }; | |
154 const wchar_t kNoMatchUrl[] = L"http://www.chromium.org"; | |
155 | |
156 EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED, | |
157 PolicySettings::GetInstance()->default_renderer()); | |
158 EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED, | |
159 PolicySettings::GetInstance()->GetRendererForUrl(kNoMatchUrl)); | |
160 | |
161 HKEY root[] = { HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER }; | |
162 for (int i = 0; i < arraysize(root); ++i) { | |
163 EXPECT_TRUE(SetRendererSettings(root[i], | |
164 PolicySettings::RENDER_IN_CHROME_FRAME, kTestFilters, | |
165 arraysize(kTestFilters))); | |
166 | |
167 ResetPolicySettings(); | |
168 EXPECT_EQ(PolicySettings::RENDER_IN_CHROME_FRAME, | |
169 PolicySettings::GetInstance()->GetRendererForUrl(kNoMatchUrl)); | |
170 for (int j = 0; j < arraysize(kTestUrls); ++j) { | |
171 EXPECT_EQ(PolicySettings::RENDER_IN_HOST, | |
172 PolicySettings::GetInstance()->GetRendererForUrl(kTestUrls[j])); | |
173 } | |
174 | |
175 EXPECT_TRUE(SetRendererSettings(root[i], | |
176 PolicySettings::RENDER_IN_HOST, NULL, 0)); | |
177 | |
178 ResetPolicySettings(); | |
179 EXPECT_EQ(PolicySettings::RENDER_IN_HOST, | |
180 PolicySettings::GetInstance()->GetRendererForUrl(kNoMatchUrl)); | |
181 for (int j = 0; j < arraysize(kTestUrls); ++j) { | |
182 EXPECT_EQ(PolicySettings::RENDER_IN_HOST, | |
183 PolicySettings::GetInstance()->GetRendererForUrl(kTestUrls[j])); | |
184 } | |
185 | |
186 DeleteChromeFramePolicyEntries(root[i]); | |
187 } | |
188 } | |
189 | |
190 TEST_F(PolicySettingsTest, RendererForContentType) { | |
191 EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED, | |
192 PolicySettings::GetInstance()->GetRendererForContentType( | |
193 L"text/xml")); | |
194 | |
195 const wchar_t* kTestPolicyContentTypes[] = { | |
196 L"application/xml", | |
197 L"text/xml", | |
198 L"application/pdf", | |
199 }; | |
200 | |
201 HKEY root[] = { HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER }; | |
202 for (int i = 0; i < arraysize(root); ++i) { | |
203 SetCFContentTypes(root[i], kTestPolicyContentTypes, | |
204 arraysize(kTestPolicyContentTypes)); | |
205 ResetPolicySettings(); | |
206 for (int type = 0; type < arraysize(kTestPolicyContentTypes); ++type) { | |
207 EXPECT_EQ(PolicySettings::RENDER_IN_CHROME_FRAME, | |
208 PolicySettings::GetInstance()->GetRendererForContentType( | |
209 kTestPolicyContentTypes[type])); | |
210 } | |
211 | |
212 EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED, | |
213 PolicySettings::GetInstance()->GetRendererForContentType( | |
214 L"text/html")); | |
215 | |
216 DeleteChromeFramePolicyEntries(root[i]); | |
217 } | |
218 } | |
219 | |
220 TEST_F(PolicySettingsTest, ApplicationLocale) { | |
221 EXPECT_TRUE(PolicySettings::GetInstance()->ApplicationLocale().empty()); | |
222 | |
223 static const wchar_t kTestApplicationLocale[] = L"fr-CA"; | |
224 | |
225 HKEY root[] = { HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER }; | |
226 for (int i = 0; i < arraysize(root); ++i) { | |
227 SetCFPolicyString(root[i], policy::key::kApplicationLocaleValue, | |
228 kTestApplicationLocale); | |
229 ResetPolicySettings(); | |
230 EXPECT_EQ(std::wstring(kTestApplicationLocale), | |
231 PolicySettings::GetInstance()->ApplicationLocale()); | |
232 | |
233 DeleteChromeFramePolicyEntries(root[i]); | |
234 } | |
235 } | |
236 | |
237 TEST_F(PolicySettingsTest, AdditionalLaunchParameters) { | |
238 EXPECT_TRUE(PolicySettings::GetInstance()-> | |
239 AdditionalLaunchParameters().GetProgram().empty()); | |
240 | |
241 std::string test_switches("--"); | |
242 test_switches += switches::kDisableWebKitMediaSource; | |
243 | |
244 HKEY root[] = { HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER }; | |
245 for (int i = 0; i < arraysize(root); ++i) { | |
246 SetCFPolicyString(root[i], policy::key::kAdditionalLaunchParameters, | |
247 ASCIIToWide(test_switches).c_str()); | |
248 ResetPolicySettings(); | |
249 const CommandLine& additional_params = | |
250 PolicySettings::GetInstance()->AdditionalLaunchParameters(); | |
251 EXPECT_TRUE(additional_params.HasSwitch( | |
252 switches::kDisableWebKitMediaSource)); | |
253 | |
254 base::FilePath program_path(FILE_PATH_LITERAL("my_chrome.exe")); | |
255 CommandLine new_cmd_line(program_path); | |
256 new_cmd_line.AppendArguments(additional_params, false); | |
257 EXPECT_NE(new_cmd_line.GetProgram(), additional_params.GetProgram()); | |
258 EXPECT_TRUE(new_cmd_line.HasSwitch(switches::kDisableWebKitMediaSource)); | |
259 | |
260 DeleteChromeFramePolicyEntries(root[i]); | |
261 } | |
262 } | |
263 | |
264 TEST_F(PolicySettingsTest, SuppressTurndownPrompt) { | |
265 EXPECT_FALSE(PolicySettings::GetInstance()->suppress_turndown_prompt()); | |
266 | |
267 HKEY root[] = { HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER }; | |
268 for (int i = 0; i < arraysize(root); ++i) { | |
269 SetCFPolicyBool(root[i], policy::key::kSuppressChromeFrameTurndownPrompt, | |
270 true); | |
271 ResetPolicySettings(); | |
272 EXPECT_TRUE(PolicySettings::GetInstance()->suppress_turndown_prompt()); | |
273 | |
274 DeleteChromeFramePolicyEntries(root[i]); | |
275 } | |
276 } | |
OLD | NEW |