Initialize all data members in HTTPResponseInfo's new ctor and remove the related...
[chromium-blink-merge.git] / chrome_frame / test / util_unittests.cc
blobb4da4c46fba39b007357d4e3f3f298a1bd6825e0
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
3 // found in the LICENSE file.
5 #include "base/file_version_info.h"
6 #include "base/file_version_info_win.h"
7 #include "chrome_frame/utils.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/gmock/include/gmock/gmock.h"
12 const wchar_t kChannelName[] = L"-dev";
13 const wchar_t kSuffix[] = L"-fix";
15 TEST(UtilTests, GetModuleVersionTest) {
16 HMODULE mod = GetModuleHandle(L"kernel32.dll");
17 EXPECT_NE(mod, static_cast<HMODULE>(NULL));
18 wchar_t path[MAX_PATH] = {0};
19 GetModuleFileName(mod, path, arraysize(path));
21 // Use the method that goes to disk
22 scoped_ptr<FileVersionInfo> base_info(
23 FileVersionInfo::CreateFileVersionInfo(path));
24 EXPECT_TRUE(base_info.get() != NULL);
26 // Use the method that doesn't go to disk
27 uint32 low = 0, high = 0;
28 EXPECT_TRUE(GetModuleVersion(mod, &high, &low));
29 EXPECT_NE(high, 0u);
30 EXPECT_NE(low, 0u);
32 // Make sure they give the same results.
33 FileVersionInfoWin* base_info_win =
34 static_cast<FileVersionInfoWin*>(base_info.get());
35 VS_FIXEDFILEINFO* fixed_info = base_info_win->fixed_file_info();
36 EXPECT_TRUE(fixed_info != NULL);
38 EXPECT_EQ(fixed_info->dwFileVersionMS, static_cast<DWORD>(high));
39 EXPECT_EQ(fixed_info->dwFileVersionLS, static_cast<DWORD>(low));
42 TEST(UtilTests, HaveSameOrigin) {
43 struct OriginCompare {
44 const char* a;
45 const char* b;
46 bool same_origin;
47 } test_cases[] = {
48 { "", "", true },
49 { "*", "*", true },
50 { "*", "+", false },
51 { "http://www.google.com/", "http://www.google.com/", true },
52 { "http://www.google.com", "http://www.google.com/", true },
53 { "http://www.google.com:80/", "http://www.google.com/", true },
54 { "http://www.google.com:8080/", "http://www.google.com/", false },
55 { "https://www.google.com/", "http://www.google.com/", false },
56 { "http://docs.google.com/", "http://www.google.com/", false },
57 { "https://www.google.com/", "https://www.google.com:443/", true },
58 { "https://www.google.com/", "https://www.google.com:443", true },
61 for (int i = 0; i < arraysize(test_cases); ++i) {
62 const OriginCompare& test = test_cases[i];
63 EXPECT_EQ(test.same_origin, HaveSameOrigin(test.a, test.b));
67 TEST(UtilTests, IsValidUrlScheme) {
68 struct Cases {
69 const wchar_t* url;
70 bool is_privileged;
71 bool expected;
72 } test_cases[] = {
73 // non-privileged test cases
74 { L"http://www.google.ca", false, true },
75 { L"https://www.google.ca", false, true },
76 { L"about:config", false, true },
77 { L"view-source:http://www.google.ca", false, true },
78 { L"chrome-extension://aaaaaaaaaaaaaaaaaaa/toolstrip.html", false, false },
79 { L"ftp://www.google.ca", false, false },
80 { L"file://www.google.ca", false, false },
81 { L"file://C:\boot.ini", false, false },
83 // privileged test cases
84 { L"http://www.google.ca", true, true },
85 { L"https://www.google.ca", true, true },
86 { L"about:config", true, true },
87 { L"view-source:http://www.google.ca", true, true },
88 { L"chrome-extension://aaaaaaaaaaaaaaaaaaa/toolstrip.html", true, true },
89 { L"ftp://www.google.ca", true, false },
90 { L"file://www.google.ca", true, false },
91 { L"file://C:\boot.ini", true, false },
94 for (int i = 0; i < arraysize(test_cases); ++i) {
95 const Cases& test = test_cases[i];
96 EXPECT_EQ(test.expected, IsValidUrlScheme(test.url, test.is_privileged));
100 TEST(UtilTests, GuidToString) {
101 // {3C5E2125-35BA-48df-A841-5F669B9D69FC}
102 const GUID test_guid = { 0x3c5e2125, 0x35ba, 0x48df,
103 { 0xa8, 0x41, 0x5f, 0x66, 0x9b, 0x9d, 0x69, 0xfc } };
105 wchar_t compare[64] = {0};
106 ::StringFromGUID2(test_guid, compare, arraysize(compare));
108 std::wstring str_guid(GuidToString(test_guid));
109 EXPECT_EQ(0, str_guid.compare(compare));
110 EXPECT_EQ(static_cast<size_t>(lstrlenW(compare)), str_guid.length());
113 TEST(UtilTests, GetTempInternetFiles) {
114 FilePath path = GetIETemporaryFilesFolder();
115 EXPECT_FALSE(path.empty());
118 TEST(UtilTests, ParseAttachTabUrlTest) {
119 ChromeFrameUrl cf_url;
121 EXPECT_TRUE(cf_url.Parse(L"http://f/?attach_external_tab&10&1&0&0&100&100"));
123 EXPECT_TRUE(cf_url.attach_to_external_tab());
124 EXPECT_FALSE(cf_url.is_chrome_protocol());
126 EXPECT_EQ(10, cf_url.cookie());
127 EXPECT_EQ(1, cf_url.disposition());
128 EXPECT_EQ(0, cf_url.dimensions().x());
129 EXPECT_EQ(0, cf_url.dimensions().y());
130 EXPECT_EQ(100, cf_url.dimensions().width());
131 EXPECT_EQ(100, cf_url.dimensions().height());
133 EXPECT_TRUE(cf_url.Parse(L"http://www.foobar.com?&10&1&0&0&100&100"));
134 EXPECT_FALSE(cf_url.attach_to_external_tab());
136 EXPECT_FALSE(cf_url.Parse(L"attach_external_tab&10&1"));
137 EXPECT_TRUE(cf_url.attach_to_external_tab());
139 EXPECT_TRUE(cf_url.Parse(L"gcf:http://f/?attach_tab&10&1&0&0&100&100"));
140 EXPECT_FALSE(cf_url.attach_to_external_tab());
141 EXPECT_TRUE(cf_url.is_chrome_protocol());
144 // Mock for the IInternetSecurityManager interface
145 class MockIInternetSecurityManager : public IInternetSecurityManager {
146 public:
147 MOCK_METHOD2_WITH_CALLTYPE(__stdcall, QueryInterface,
148 HRESULT(REFIID iid, void** object));
150 MOCK_METHOD0_WITH_CALLTYPE(__stdcall, AddRef, ULONG());
151 MOCK_METHOD0_WITH_CALLTYPE(__stdcall, Release, ULONG());
153 MOCK_METHOD1_WITH_CALLTYPE(__stdcall, SetSecuritySite,
154 HRESULT(IInternetSecurityMgrSite* site));
155 MOCK_METHOD1_WITH_CALLTYPE(__stdcall, GetSecuritySite,
156 HRESULT(IInternetSecurityMgrSite** site));
157 MOCK_METHOD3_WITH_CALLTYPE(__stdcall, MapUrlToZone,
158 HRESULT(LPCWSTR url, DWORD* zone, DWORD flags));
159 MOCK_METHOD4_WITH_CALLTYPE(__stdcall, GetSecurityId,
160 HRESULT(LPCWSTR url, BYTE* security_id, DWORD* security_size,
161 DWORD_PTR reserved));
162 MOCK_METHOD8_WITH_CALLTYPE(__stdcall, ProcessUrlAction,
163 HRESULT(LPCWSTR url, DWORD action, BYTE* policy, DWORD cb_policy,
164 BYTE* context, DWORD context_size, DWORD flags,
165 DWORD reserved));
166 MOCK_METHOD7_WITH_CALLTYPE(__stdcall, QueryCustomPolicy,
167 HRESULT(LPCWSTR url, REFGUID guid, BYTE** policy, DWORD* cb_policy,
168 BYTE* context, DWORD cb_context, DWORD reserved));
169 MOCK_METHOD3_WITH_CALLTYPE(__stdcall, SetZoneMapping,
170 HRESULT(DWORD zone, LPCWSTR pattern, DWORD flags));
171 MOCK_METHOD3_WITH_CALLTYPE(__stdcall, GetZoneMappings,
172 HRESULT(DWORD zone, IEnumString** enum_string, DWORD flags));
175 TEST(UtilTests, CanNavigateFullTabModeTest) {
176 ChromeFrameUrl cf_url;
178 MockIInternetSecurityManager mock;
179 EXPECT_CALL(mock, MapUrlToZone(testing::StartsWith(L"http://blah"),
180 testing::_, testing::_))
181 .WillRepeatedly(testing::DoAll(
182 testing::SetArgumentPointee<1>(URLZONE_INTERNET),
183 testing::Return(S_OK)));
185 EXPECT_CALL(mock, MapUrlToZone(testing::StartsWith(L"http://untrusted"),
186 testing::_, testing::_))
187 .WillRepeatedly(testing::DoAll(
188 testing::SetArgumentPointee<1>(URLZONE_UNTRUSTED),
189 testing::Return(S_OK)));
191 EXPECT_CALL(mock, MapUrlToZone(testing::StartsWith(L"about:"),
192 testing::_, testing::_))
193 .WillRepeatedly(testing::DoAll(
194 testing::SetArgumentPointee<1>(URLZONE_TRUSTED),
195 testing::Return(S_OK)));
197 EXPECT_CALL(mock, MapUrlToZone(testing::StartsWith(L"view-source:"),
198 testing::_, testing::_))
199 .WillRepeatedly(testing::DoAll(
200 testing::SetArgumentPointee<1>(URLZONE_TRUSTED),
201 testing::Return(S_OK)));
203 EXPECT_TRUE(cf_url.Parse(
204 L"http://blah/?attach_external_tab&10&1&0&0&100&100"));
205 EXPECT_TRUE(CanNavigateInFullTabMode(cf_url, &mock));
207 EXPECT_TRUE(cf_url.Parse(
208 L"http://untrusted/bar.html"));
209 EXPECT_FALSE(CanNavigateInFullTabMode(cf_url, &mock));
211 EXPECT_TRUE(cf_url.Parse(
212 L"http://blah/?attach_external_tab&10&1&0&0&100&100"));
213 EXPECT_TRUE(CanNavigateInFullTabMode(cf_url, &mock));
215 EXPECT_TRUE(cf_url.Parse(L"gcf:about:blank"));
216 EXPECT_TRUE(CanNavigateInFullTabMode(cf_url, &mock));
218 EXPECT_TRUE(cf_url.Parse(L"gcf:view-source:http://www.foo.com"));
219 EXPECT_TRUE(CanNavigateInFullTabMode(cf_url, &mock));
221 EXPECT_TRUE(cf_url.Parse(L"gcf:about:version"));
222 EXPECT_TRUE(CanNavigateInFullTabMode(cf_url, &mock));
224 EXPECT_TRUE(cf_url.Parse(L"gcf:about:bar"));
225 EXPECT_TRUE(CanNavigateInFullTabMode(cf_url, &mock));
227 bool enable_gcf = GetConfigBool(false, kEnableGCFProtocol);
229 SetConfigBool(kEnableGCFProtocol, false);
231 EXPECT_TRUE(cf_url.Parse(L"gcf:http://blah"));
232 EXPECT_FALSE(CanNavigateInFullTabMode(cf_url, &mock));
234 SetConfigBool(kEnableGCFProtocol, true);
236 EXPECT_TRUE(cf_url.Parse(L"gcf:http://blah"));
237 EXPECT_TRUE(CanNavigateInFullTabMode(cf_url, &mock));
239 EXPECT_TRUE(cf_url.Parse(L"gcf:http://untrusted/bar"));
240 EXPECT_FALSE(CanNavigateInFullTabMode(cf_url, &mock));
242 SetConfigBool(kEnableGCFProtocol, enable_gcf);
245 TEST(UtilTests, ParseVersionTest) {
246 uint32 high = 0, low = 0;
247 EXPECT_FALSE(ParseVersion(L"", &high, &low));
248 EXPECT_TRUE(ParseVersion(L"1", &high, &low) && high == 1 && low == 0);
249 EXPECT_TRUE(ParseVersion(L"1.", &high, &low) && high == 1 && low == 0);
250 EXPECT_TRUE(ParseVersion(L"1.2", &high, &low) && high == 1 && low == 2);
251 EXPECT_TRUE(ParseVersion(L"1.2.3.4", &high, &low) && high == 1 && low == 2);
252 EXPECT_TRUE(ParseVersion(L"10.20", &high, &low) && high == 10 && low == 20);