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.
5 #include "base/basictypes.h"
6 #include "base/at_exit.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/stringprintf.h"
10 #include "base/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"
18 using base::win::RegKey
;
19 using chrome_frame_test::ScopedVirtualizeHklmAndHkcu
;
23 // A best effort way to zap CF policy entries that may be in the registry.
24 void DeleteChromeFramePolicyEntries(HKEY root
) {
26 if (key
.Open(root
, policy::kRegistryMandatorySubKey
,
27 KEY_ALL_ACCESS
) == ERROR_SUCCESS
) {
29 ASCIIToWide(policy::key::kChromeFrameRendererSettings
).c_str());
30 key
.DeleteKey(ASCIIToWide(policy::key::kRenderInChromeFrameList
).c_str());
31 key
.DeleteKey(ASCIIToWide(policy::key::kRenderInHostList
).c_str());
32 key
.DeleteKey(ASCIIToWide(policy::key::kChromeFrameContentTypes
).c_str());
33 key
.DeleteKey(ASCIIToWide(policy::key::kApplicationLocaleValue
).c_str());
37 bool InitializePolicyKey(HKEY policy_root
, RegKey
* policy_key
) {
38 EXPECT_EQ(ERROR_SUCCESS
, policy_key
->Create(policy_root
,
39 policy::kRegistryMandatorySubKey
, KEY_ALL_ACCESS
));
40 return policy_key
->Valid();
43 void WritePolicyList(RegKey
* policy_key
,
44 const wchar_t* list_name
,
45 const wchar_t* values
[], int count
) {
47 // Remove any previous settings
48 policy_key
->DeleteKey(list_name
);
51 EXPECT_EQ(ERROR_SUCCESS
, list_key
.Create(policy_key
->Handle(), list_name
,
53 for (int i
= 0; i
< count
; ++i
) {
54 EXPECT_EQ(ERROR_SUCCESS
,
55 list_key
.WriteValue(base::StringPrintf(L
"%i", i
).c_str(), values
[i
]));
59 bool SetRendererSettings(HKEY policy_root
,
60 PolicySettings::RendererForUrl renderer
,
61 const wchar_t* exclusions
[],
62 int exclusion_count
) {
64 if (!InitializePolicyKey(policy_root
, &policy_key
))
67 policy_key
.WriteValue(
68 ASCIIToWide(policy::key::kChromeFrameRendererSettings
).c_str(),
69 static_cast<DWORD
>(renderer
));
71 std::wstring
in_cf(ASCIIToWide(policy::key::kRenderInChromeFrameList
));
72 std::wstring
in_host(ASCIIToWide(policy::key::kRenderInHostList
));
73 std::wstring
exclusion_list(
74 renderer
== PolicySettings::RENDER_IN_CHROME_FRAME
? in_host
: in_cf
);
75 WritePolicyList(&policy_key
, exclusion_list
.c_str(), exclusions
,
81 bool SetCFContentTypes(HKEY policy_root
,
82 const wchar_t* content_types
[],
85 if (!InitializePolicyKey(policy_root
, &policy_key
))
88 std::wstring
type_list(ASCIIToWide(policy::key::kChromeFrameContentTypes
));
89 WritePolicyList(&policy_key
, type_list
.c_str(), content_types
, count
);
94 bool SetCFPolicyString(HKEY policy_root
,
95 const char* policy_name
,
96 const wchar_t* value
) {
98 if (!InitializePolicyKey(policy_root
, &policy_key
))
101 std::wstring
policy_name_str(ASCIIToWide(policy_name
));
102 EXPECT_EQ(ERROR_SUCCESS
,
103 policy_key
.WriteValue(policy_name_str
.c_str(), value
));
109 class PolicySettingsTest
: public testing::Test
{
112 ResetPolicySettings();
118 void ResetPolicySettings() {
119 //at_exit_manager_.ProcessCallbacksNow();
120 DeleteAllSingletons();
123 // This is used to manage life cycle of PolicySettings singleton.
124 // base::ShadowingAtExitManager at_exit_manager_;
126 ScopedVirtualizeHklmAndHkcu registry_virtualization_
;
129 TEST_F(PolicySettingsTest
, RendererForUrl
) {
130 const wchar_t* kTestUrls
[] = {
131 L
"http://www.example.com",
132 L
"http://www.pattern.com",
133 L
"http://www.test.com"
135 const wchar_t* kTestFilters
[] = {
140 const wchar_t kNoMatchUrl
[] = L
"http://www.chromium.org";
142 EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED
,
143 PolicySettings::GetInstance()->default_renderer());
144 EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED
,
145 PolicySettings::GetInstance()->GetRendererForUrl(kNoMatchUrl
));
147 HKEY root
[] = { HKEY_LOCAL_MACHINE
, HKEY_CURRENT_USER
};
148 for (int i
= 0; i
< arraysize(root
); ++i
) {
149 EXPECT_TRUE(SetRendererSettings(root
[i
],
150 PolicySettings::RENDER_IN_CHROME_FRAME
, kTestFilters
,
151 arraysize(kTestFilters
)));
153 ResetPolicySettings();
154 EXPECT_EQ(PolicySettings::RENDER_IN_CHROME_FRAME
,
155 PolicySettings::GetInstance()->GetRendererForUrl(kNoMatchUrl
));
156 for (int j
= 0; j
< arraysize(kTestUrls
); ++j
) {
157 EXPECT_EQ(PolicySettings::RENDER_IN_HOST
,
158 PolicySettings::GetInstance()->GetRendererForUrl(kTestUrls
[j
]));
161 EXPECT_TRUE(SetRendererSettings(root
[i
],
162 PolicySettings::RENDER_IN_HOST
, NULL
, 0));
164 ResetPolicySettings();
165 EXPECT_EQ(PolicySettings::RENDER_IN_HOST
,
166 PolicySettings::GetInstance()->GetRendererForUrl(kNoMatchUrl
));
167 for (int j
= 0; j
< arraysize(kTestUrls
); ++j
) {
168 EXPECT_EQ(PolicySettings::RENDER_IN_HOST
,
169 PolicySettings::GetInstance()->GetRendererForUrl(kTestUrls
[j
]));
172 DeleteChromeFramePolicyEntries(root
[i
]);
176 TEST_F(PolicySettingsTest
, RendererForContentType
) {
177 EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED
,
178 PolicySettings::GetInstance()->GetRendererForContentType(
181 const wchar_t* kTestPolicyContentTypes
[] = {
187 HKEY root
[] = { HKEY_LOCAL_MACHINE
, HKEY_CURRENT_USER
};
188 for (int i
= 0; i
< arraysize(root
); ++i
) {
189 SetCFContentTypes(root
[i
], kTestPolicyContentTypes
,
190 arraysize(kTestPolicyContentTypes
));
191 ResetPolicySettings();
192 for (int type
= 0; type
< arraysize(kTestPolicyContentTypes
); ++type
) {
193 EXPECT_EQ(PolicySettings::RENDER_IN_CHROME_FRAME
,
194 PolicySettings::GetInstance()->GetRendererForContentType(
195 kTestPolicyContentTypes
[type
]));
198 EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED
,
199 PolicySettings::GetInstance()->GetRendererForContentType(
202 DeleteChromeFramePolicyEntries(root
[i
]);
206 TEST_F(PolicySettingsTest
, ApplicationLocale
) {
207 EXPECT_TRUE(PolicySettings::GetInstance()->ApplicationLocale().empty());
209 static const wchar_t kTestApplicationLocale
[] = L
"fr-CA";
211 HKEY root
[] = { HKEY_LOCAL_MACHINE
, HKEY_CURRENT_USER
};
212 for (int i
= 0; i
< arraysize(root
); ++i
) {
213 SetCFPolicyString(root
[i
], policy::key::kApplicationLocaleValue
,
214 kTestApplicationLocale
);
215 ResetPolicySettings();
216 EXPECT_EQ(std::wstring(kTestApplicationLocale
),
217 PolicySettings::GetInstance()->ApplicationLocale());
219 DeleteChromeFramePolicyEntries(root
[i
]);
223 TEST_F(PolicySettingsTest
, AdditionalLaunchParameters
) {
224 EXPECT_TRUE(PolicySettings::GetInstance()->
225 AdditionalLaunchParameters().GetProgram().empty());
227 std::string
test_switches("--");
228 test_switches
+= switches::kDisableMediaSource
;
230 HKEY root
[] = { HKEY_LOCAL_MACHINE
, HKEY_CURRENT_USER
};
231 for (int i
= 0; i
< arraysize(root
); ++i
) {
232 SetCFPolicyString(root
[i
], policy::key::kAdditionalLaunchParameters
,
233 ASCIIToWide(test_switches
).c_str());
234 ResetPolicySettings();
235 const CommandLine
& additional_params
=
236 PolicySettings::GetInstance()->AdditionalLaunchParameters();
237 EXPECT_TRUE(additional_params
.HasSwitch(switches::kDisableMediaSource
));
239 base::FilePath
program_path(FILE_PATH_LITERAL("my_chrome.exe"));
240 CommandLine
new_cmd_line(program_path
);
241 new_cmd_line
.AppendArguments(additional_params
, false);
242 EXPECT_NE(new_cmd_line
.GetProgram(), additional_params
.GetProgram());
243 EXPECT_TRUE(new_cmd_line
.HasSwitch(switches::kDisableMediaSource
));
245 DeleteChromeFramePolicyEntries(root
[i
]);