OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/file_path.h" | 5 #include "base/file_path.h" |
6 #include "base/file_version_info.h" | 6 #include "base/file_version_info.h" |
7 #include "base/file_version_info_win.h" | 7 #include "base/file_version_info_win.h" |
8 #include "base/win/registry.h" | 8 #include "base/win/registry.h" |
| 9 #include "chrome_frame/test/chrome_frame_test_utils.h" |
9 #include "chrome_frame/utils.h" | 10 #include "chrome_frame/utils.h" |
10 | 11 |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
13 | 14 |
14 using base::win::RegKey; | 15 using base::win::RegKey; |
| 16 using chrome_frame_test::TempRegKeyOverride; |
15 | 17 |
16 const wchar_t kChannelName[] = L"-dev"; | 18 const wchar_t kChannelName[] = L"-dev"; |
17 const wchar_t kSuffix[] = L"-fix"; | 19 const wchar_t kSuffix[] = L"-fix"; |
18 | 20 |
19 TEST(UtilTests, GetModuleVersionTest) { | 21 class UtilTests : public testing::Test { |
| 22 protected: |
| 23 void SetUp() { |
| 24 TempRegKeyOverride::DeleteAllTempKeys(); |
| 25 DeleteAllSingletons(); |
| 26 |
| 27 hklm_pol_.reset(new TempRegKeyOverride(HKEY_LOCAL_MACHINE, L"hklm_fake")); |
| 28 hkcu_pol_.reset(new TempRegKeyOverride(HKEY_CURRENT_USER, L"hkcu_fake")); |
| 29 } |
| 30 |
| 31 void TearDown() { |
| 32 hkcu_pol_.reset(NULL); |
| 33 hklm_pol_.reset(NULL); |
| 34 TempRegKeyOverride::DeleteAllTempKeys(); |
| 35 } |
| 36 |
| 37 // This is used to manage life cycle of PolicySettings singleton. |
| 38 // base::ShadowingAtExitManager at_exit_manager_; |
| 39 scoped_ptr<TempRegKeyOverride> hklm_pol_; |
| 40 scoped_ptr<TempRegKeyOverride> hkcu_pol_; |
| 41 }; |
| 42 |
| 43 TEST_F(UtilTests, GetModuleVersionTest) { |
20 HMODULE mod = GetModuleHandle(L"kernel32.dll"); | 44 HMODULE mod = GetModuleHandle(L"kernel32.dll"); |
21 EXPECT_NE(mod, static_cast<HMODULE>(NULL)); | 45 EXPECT_NE(mod, static_cast<HMODULE>(NULL)); |
22 wchar_t path[MAX_PATH] = {0}; | 46 wchar_t path[MAX_PATH] = {0}; |
23 GetModuleFileName(mod, path, arraysize(path)); | 47 GetModuleFileName(mod, path, arraysize(path)); |
24 | 48 |
25 // Use the method that goes to disk | 49 // Use the method that goes to disk |
26 scoped_ptr<FileVersionInfo> base_info( | 50 scoped_ptr<FileVersionInfo> base_info( |
27 FileVersionInfo::CreateFileVersionInfo(path)); | 51 FileVersionInfo::CreateFileVersionInfo(path)); |
28 EXPECT_TRUE(base_info.get() != NULL); | 52 EXPECT_TRUE(base_info.get() != NULL); |
29 | 53 |
30 // Use the method that doesn't go to disk | 54 // Use the method that doesn't go to disk |
31 uint32 low = 0, high = 0; | 55 uint32 low = 0, high = 0; |
32 EXPECT_TRUE(GetModuleVersion(mod, &high, &low)); | 56 EXPECT_TRUE(GetModuleVersion(mod, &high, &low)); |
33 EXPECT_NE(high, 0u); | 57 EXPECT_NE(high, 0u); |
34 EXPECT_NE(low, 0u); | 58 EXPECT_NE(low, 0u); |
35 | 59 |
36 // Make sure they give the same results. | 60 // Make sure they give the same results. |
37 FileVersionInfoWin* base_info_win = | 61 FileVersionInfoWin* base_info_win = |
38 static_cast<FileVersionInfoWin*>(base_info.get()); | 62 static_cast<FileVersionInfoWin*>(base_info.get()); |
39 VS_FIXEDFILEINFO* fixed_info = base_info_win->fixed_file_info(); | 63 VS_FIXEDFILEINFO* fixed_info = base_info_win->fixed_file_info(); |
40 EXPECT_TRUE(fixed_info != NULL); | 64 EXPECT_TRUE(fixed_info != NULL); |
41 | 65 |
42 EXPECT_EQ(fixed_info->dwFileVersionMS, static_cast<DWORD>(high)); | 66 EXPECT_EQ(fixed_info->dwFileVersionMS, static_cast<DWORD>(high)); |
43 EXPECT_EQ(fixed_info->dwFileVersionLS, static_cast<DWORD>(low)); | 67 EXPECT_EQ(fixed_info->dwFileVersionLS, static_cast<DWORD>(low)); |
44 } | 68 } |
45 | 69 |
46 TEST(UtilTests, HaveSameOrigin) { | 70 TEST_F(UtilTests, HaveSameOrigin) { |
47 struct OriginCompare { | 71 struct OriginCompare { |
48 const char* a; | 72 const char* a; |
49 const char* b; | 73 const char* b; |
50 bool same_origin; | 74 bool same_origin; |
51 } test_cases[] = { | 75 } test_cases[] = { |
52 { "", "", true }, | 76 { "", "", true }, |
53 { "*", "*", true }, | 77 { "*", "*", true }, |
54 { "*", "+", false }, | 78 { "*", "+", false }, |
55 { "http://www.google.com/", "http://www.google.com/", true }, | 79 { "http://www.google.com/", "http://www.google.com/", true }, |
56 { "http://www.google.com", "http://www.google.com/", true }, | 80 { "http://www.google.com", "http://www.google.com/", true }, |
57 { "http://www.google.com:80/", "http://www.google.com/", true }, | 81 { "http://www.google.com:80/", "http://www.google.com/", true }, |
58 { "http://www.google.com:8080/", "http://www.google.com/", false }, | 82 { "http://www.google.com:8080/", "http://www.google.com/", false }, |
59 { "https://www.google.com/", "http://www.google.com/", false }, | 83 { "https://www.google.com/", "http://www.google.com/", false }, |
60 { "http://docs.google.com/", "http://www.google.com/", false }, | 84 { "http://docs.google.com/", "http://www.google.com/", false }, |
61 { "https://www.google.com/", "https://www.google.com:443/", true }, | 85 { "https://www.google.com/", "https://www.google.com:443/", true }, |
62 { "https://www.google.com/", "https://www.google.com:443", true }, | 86 { "https://www.google.com/", "https://www.google.com:443", true }, |
63 }; | 87 }; |
64 | 88 |
65 for (int i = 0; i < arraysize(test_cases); ++i) { | 89 for (int i = 0; i < arraysize(test_cases); ++i) { |
66 const OriginCompare& test = test_cases[i]; | 90 const OriginCompare& test = test_cases[i]; |
67 EXPECT_EQ(test.same_origin, HaveSameOrigin(test.a, test.b)); | 91 EXPECT_EQ(test.same_origin, HaveSameOrigin(test.a, test.b)); |
68 } | 92 } |
69 } | 93 } |
70 | 94 |
71 TEST(UtilTests, IsValidUrlScheme) { | 95 TEST_F(UtilTests, IsValidUrlScheme) { |
72 struct Cases { | 96 struct Cases { |
73 const wchar_t* url; | 97 const wchar_t* url; |
74 bool is_privileged; | 98 bool is_privileged; |
75 bool expected; | 99 bool expected; |
76 } test_cases[] = { | 100 } test_cases[] = { |
77 // non-privileged test cases | 101 // non-privileged test cases |
78 { L"http://www.google.ca", false, true }, | 102 { L"http://www.google.ca", false, true }, |
79 { L"https://www.google.ca", false, true }, | 103 { L"https://www.google.ca", false, true }, |
80 { L"about:config", false, true }, | 104 { L"about:config", false, true }, |
81 { L"view-source:http://www.google.ca", false, true }, | 105 { L"view-source:http://www.google.ca", false, true }, |
(...skipping 13 matching lines...) Expand all Loading... |
95 { L"file://C:\boot.ini", true, false }, | 119 { L"file://C:\boot.ini", true, false }, |
96 }; | 120 }; |
97 | 121 |
98 for (int i = 0; i < arraysize(test_cases); ++i) { | 122 for (int i = 0; i < arraysize(test_cases); ++i) { |
99 const Cases& test = test_cases[i]; | 123 const Cases& test = test_cases[i]; |
100 EXPECT_EQ(test.expected, IsValidUrlScheme(GURL(test.url), | 124 EXPECT_EQ(test.expected, IsValidUrlScheme(GURL(test.url), |
101 test.is_privileged)); | 125 test.is_privileged)); |
102 } | 126 } |
103 } | 127 } |
104 | 128 |
105 TEST(UtilTests, GuidToString) { | 129 TEST_F(UtilTests, GuidToString) { |
106 // {3C5E2125-35BA-48df-A841-5F669B9D69FC} | 130 // {3C5E2125-35BA-48df-A841-5F669B9D69FC} |
107 const GUID test_guid = { 0x3c5e2125, 0x35ba, 0x48df, | 131 const GUID test_guid = { 0x3c5e2125, 0x35ba, 0x48df, |
108 { 0xa8, 0x41, 0x5f, 0x66, 0x9b, 0x9d, 0x69, 0xfc } }; | 132 { 0xa8, 0x41, 0x5f, 0x66, 0x9b, 0x9d, 0x69, 0xfc } }; |
109 | 133 |
110 wchar_t compare[64] = {0}; | 134 wchar_t compare[64] = {0}; |
111 ::StringFromGUID2(test_guid, compare, arraysize(compare)); | 135 ::StringFromGUID2(test_guid, compare, arraysize(compare)); |
112 | 136 |
113 std::wstring str_guid(GuidToString(test_guid)); | 137 std::wstring str_guid(GuidToString(test_guid)); |
114 EXPECT_EQ(0, str_guid.compare(compare)); | 138 EXPECT_EQ(0, str_guid.compare(compare)); |
115 EXPECT_EQ(static_cast<size_t>(lstrlenW(compare)), str_guid.length()); | 139 EXPECT_EQ(static_cast<size_t>(lstrlenW(compare)), str_guid.length()); |
116 } | 140 } |
117 | 141 |
118 TEST(UtilTests, GetTempInternetFiles) { | 142 TEST_F(UtilTests, GetTempInternetFiles) { |
119 FilePath path = GetIETemporaryFilesFolder(); | 143 FilePath path = GetIETemporaryFilesFolder(); |
120 EXPECT_FALSE(path.empty()); | 144 EXPECT_FALSE(path.empty()); |
121 } | 145 } |
122 | 146 |
123 TEST(UtilTests, ParseAttachTabUrlTest) { | 147 TEST_F(UtilTests, ParseAttachTabUrlTest) { |
124 ChromeFrameUrl cf_url; | 148 ChromeFrameUrl cf_url; |
125 | 149 |
126 static const std::string kProfileName("iexplore"); | 150 static const std::string kProfileName("iexplore"); |
127 | 151 |
128 EXPECT_TRUE(cf_url.Parse( | 152 EXPECT_TRUE(cf_url.Parse( |
129 L"http://f/?attach_external_tab&10&1&2&3&123&321&iexplore")); | 153 L"http://f/?attach_external_tab&10&1&2&3&123&321&iexplore")); |
130 | 154 |
131 EXPECT_TRUE(cf_url.attach_to_external_tab()); | 155 EXPECT_TRUE(cf_url.attach_to_external_tab()); |
132 EXPECT_FALSE(cf_url.is_chrome_protocol()); | 156 EXPECT_FALSE(cf_url.is_chrome_protocol()); |
133 EXPECT_EQ(10, cf_url.cookie()); | 157 EXPECT_EQ(10, cf_url.cookie()); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 DWORD reserved)); | 219 DWORD reserved)); |
196 MOCK_METHOD7_WITH_CALLTYPE(__stdcall, QueryCustomPolicy, | 220 MOCK_METHOD7_WITH_CALLTYPE(__stdcall, QueryCustomPolicy, |
197 HRESULT(LPCWSTR url, REFGUID guid, BYTE** policy, DWORD* cb_policy, | 221 HRESULT(LPCWSTR url, REFGUID guid, BYTE** policy, DWORD* cb_policy, |
198 BYTE* context, DWORD cb_context, DWORD reserved)); | 222 BYTE* context, DWORD cb_context, DWORD reserved)); |
199 MOCK_METHOD3_WITH_CALLTYPE(__stdcall, SetZoneMapping, | 223 MOCK_METHOD3_WITH_CALLTYPE(__stdcall, SetZoneMapping, |
200 HRESULT(DWORD zone, LPCWSTR pattern, DWORD flags)); | 224 HRESULT(DWORD zone, LPCWSTR pattern, DWORD flags)); |
201 MOCK_METHOD3_WITH_CALLTYPE(__stdcall, GetZoneMappings, | 225 MOCK_METHOD3_WITH_CALLTYPE(__stdcall, GetZoneMappings, |
202 HRESULT(DWORD zone, IEnumString** enum_string, DWORD flags)); | 226 HRESULT(DWORD zone, IEnumString** enum_string, DWORD flags)); |
203 }; | 227 }; |
204 | 228 |
205 TEST(UtilTests, CanNavigateTest) { | 229 TEST_F(UtilTests, CanNavigateTest) { |
206 MockIInternetSecurityManager mock; | 230 MockIInternetSecurityManager mock; |
207 | 231 |
208 struct Zones { | 232 struct Zones { |
209 const wchar_t* url_prefix; | 233 const wchar_t* url_prefix; |
210 URLZONE zone; | 234 URLZONE zone; |
211 } test_zones[] = { | 235 } test_zones[] = { |
212 { L"http://blah", URLZONE_INTERNET }, | 236 { L"http://blah", URLZONE_INTERNET }, |
213 { L"http://untrusted", URLZONE_UNTRUSTED }, | 237 { L"http://untrusted", URLZONE_UNTRUSTED }, |
214 { L"about:", URLZONE_TRUSTED }, | 238 { L"about:", URLZONE_TRUSTED }, |
215 { L"view-source:", URLZONE_TRUSTED }, | 239 { L"view-source:", URLZONE_TRUSTED }, |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 | 310 |
287 for (int i = 0; i < arraysize(test_cases); ++i) { | 311 for (int i = 0; i < arraysize(test_cases); ++i) { |
288 const Cases& test = test_cases[i]; | 312 const Cases& test = test_cases[i]; |
289 bool actual = CanNavigate(GURL(test.url), &mock, test.is_privileged); | 313 bool actual = CanNavigate(GURL(test.url), &mock, test.is_privileged); |
290 EXPECT_EQ(test.unsafe_expected, actual) << "Failure url: " << test.url; | 314 EXPECT_EQ(test.unsafe_expected, actual) << "Failure url: " << test.url; |
291 } | 315 } |
292 | 316 |
293 SetConfigBool(kAllowUnsafeURLs, enable_gcf); | 317 SetConfigBool(kAllowUnsafeURLs, enable_gcf); |
294 } | 318 } |
295 | 319 |
296 TEST(UtilTests, IsDefaultRendererTest) { | 320 TEST_F(UtilTests, IsDefaultRendererTest) { |
297 RegKey config_key(HKEY_CURRENT_USER, kChromeFrameConfigKey, KEY_ALL_ACCESS); | 321 RegKey config_key(HKEY_CURRENT_USER, kChromeFrameConfigKey, KEY_ALL_ACCESS); |
298 EXPECT_TRUE(config_key.Valid()); | 322 EXPECT_TRUE(config_key.Valid()); |
299 | 323 |
300 DWORD saved_default_renderer = 0; // NOLINT | 324 DWORD saved_default_renderer = 0; // NOLINT |
301 config_key.ReadValueDW(kEnableGCFRendererByDefault, &saved_default_renderer); | 325 config_key.ReadValueDW(kEnableGCFRendererByDefault, &saved_default_renderer); |
302 | 326 |
303 config_key.DeleteValue(kEnableGCFRendererByDefault); | 327 config_key.DeleteValue(kEnableGCFRendererByDefault); |
304 EXPECT_FALSE(IsGcfDefaultRenderer()); | 328 EXPECT_FALSE(IsGcfDefaultRenderer()); |
305 | 329 |
306 config_key.WriteValue(kEnableGCFRendererByDefault, static_cast<DWORD>(0)); | 330 config_key.WriteValue(kEnableGCFRendererByDefault, static_cast<DWORD>(0)); |
307 EXPECT_FALSE(IsGcfDefaultRenderer()); | 331 EXPECT_FALSE(IsGcfDefaultRenderer()); |
308 | 332 |
309 config_key.WriteValue(kEnableGCFRendererByDefault, static_cast<DWORD>(1)); | 333 config_key.WriteValue(kEnableGCFRendererByDefault, static_cast<DWORD>(1)); |
310 EXPECT_TRUE(IsGcfDefaultRenderer()); | 334 EXPECT_TRUE(IsGcfDefaultRenderer()); |
311 | 335 |
312 config_key.WriteValue(kEnableGCFRendererByDefault, saved_default_renderer); | 336 config_key.WriteValue(kEnableGCFRendererByDefault, saved_default_renderer); |
313 } | 337 } |
314 | 338 |
315 TEST(UtilTests, RendererTypeForUrlTest) { | 339 TEST_F(UtilTests, RendererTypeForUrlTest) { |
316 // Open all the keys we need. | 340 // Open all the keys we need. |
317 RegKey config_key(HKEY_CURRENT_USER, kChromeFrameConfigKey, KEY_ALL_ACCESS); | 341 RegKey config_key(HKEY_CURRENT_USER, kChromeFrameConfigKey, KEY_ALL_ACCESS); |
318 EXPECT_TRUE(config_key.Valid()); | 342 EXPECT_TRUE(config_key.Valid()); |
319 RegKey opt_for_gcf(config_key.Handle(), kRenderInGCFUrlList, KEY_ALL_ACCESS); | 343 RegKey opt_for_gcf(config_key.Handle(), kRenderInGCFUrlList, KEY_ALL_ACCESS); |
320 EXPECT_TRUE(opt_for_gcf.Valid()); | 344 EXPECT_TRUE(opt_for_gcf.Valid()); |
321 RegKey opt_for_host(config_key.Handle(), kRenderInHostUrlList, | 345 RegKey opt_for_host(config_key.Handle(), kRenderInHostUrlList, |
322 KEY_ALL_ACCESS); | 346 KEY_ALL_ACCESS); |
323 EXPECT_TRUE(opt_for_host.Valid()); | 347 EXPECT_TRUE(opt_for_host.Valid()); |
324 if (!config_key.Valid() || !opt_for_gcf.Valid() || !opt_for_host.Valid()) | 348 if (!config_key.Valid() || !opt_for_gcf.Valid() || !opt_for_host.Valid()) |
325 return; | 349 return; |
(...skipping 23 matching lines...) Expand all Loading... |
349 RendererTypeForUrl(kTestUrl)); | 373 RendererTypeForUrl(kTestUrl)); |
350 opt_for_host.WriteValue(kTestFilter, L""); | 374 opt_for_host.WriteValue(kTestFilter, L""); |
351 EXPECT_EQ(RENDERER_TYPE_UNDETERMINED, RendererTypeForUrl(kTestUrl)); | 375 EXPECT_EQ(RENDERER_TYPE_UNDETERMINED, RendererTypeForUrl(kTestUrl)); |
352 | 376 |
353 // Cleanup. | 377 // Cleanup. |
354 opt_for_gcf.DeleteValue(kTestFilter); | 378 opt_for_gcf.DeleteValue(kTestFilter); |
355 opt_for_host.DeleteValue(kTestFilter); | 379 opt_for_host.DeleteValue(kTestFilter); |
356 config_key.WriteValue(kEnableGCFRendererByDefault, saved_default_renderer); | 380 config_key.WriteValue(kEnableGCFRendererByDefault, saved_default_renderer); |
357 } | 381 } |
358 | 382 |
359 TEST(UtilTests, XUaCompatibleDirectiveTest) { | 383 TEST_F(UtilTests, XUaCompatibleDirectiveTest) { |
360 int all_versions[] = {0, 1, 2, 5, 6, 7, 8, 9, 10, 11, 99, 100, 101, 1000}; | 384 int all_versions[] = {0, 1, 2, 5, 6, 7, 8, 9, 10, 11, 99, 100, 101, 1000}; |
361 | 385 |
362 struct Cases { | 386 struct Cases { |
363 const char* header_value; | 387 const char* header_value; |
364 int max_version; | 388 int max_version; |
365 } test_cases[] = { | 389 } test_cases[] = { |
366 // Negative cases | 390 // Negative cases |
367 { "", -1 }, | 391 { "", -1 }, |
368 { "chrome=", -1 }, | 392 { "chrome=", -1 }, |
369 { "chrome", -1 }, | 393 { "chrome", -1 }, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 | 438 |
415 ASSERT_EQ(expect_match, | 439 ASSERT_EQ(expect_match, |
416 CheckXUaCompatibleDirective(test.header_value, | 440 CheckXUaCompatibleDirective(test.header_value, |
417 all_versions[version_index])) | 441 all_versions[version_index])) |
418 << "Expect '" << test.header_value << "' to " | 442 << "Expect '" << test.header_value << "' to " |
419 << (expect_match ? "match" : "not match") << " IE major version " | 443 << (expect_match ? "match" : "not match") << " IE major version " |
420 << all_versions[version_index]; | 444 << all_versions[version_index]; |
421 } | 445 } |
422 } | 446 } |
423 } | 447 } |
OLD | NEW |