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