blob: e8a92019f063fe528faf26ee4f30182ff2f0372f [file] [log] [blame]
miguelgdbcfb122015-07-23 10:45:111// Copyright 2014 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
rsleevi24f64dc22015-08-07 21:39:215#include "components/url_formatter/elide_url.h"
miguelgdbcfb122015-07-23 10:45:116
avi5dd91f82015-12-25 22:30:467#include <stddef.h>
8
avi5dd91f82015-12-25 22:30:469#include "base/macros.h"
kulshince612eb2016-06-29 18:46:5110#include "base/run_loop.h"
miguelgdbcfb122015-07-23 10:45:1111#include "base/strings/utf_string_conversions.h"
avi5dd91f82015-12-25 22:30:4612#include "build/build_config.h"
miguelgdbcfb122015-07-23 10:45:1113#include "testing/gtest/include/gtest/gtest.h"
miguelgdbcfb122015-07-23 10:45:1114#include "url/gurl.h"
juncaicb63cac2016-05-13 00:41:5915#include "url/origin.h"
miguelgdbcfb122015-07-23 10:45:1116
rsleevi24f64dc22015-08-07 21:39:2117#if !defined(OS_ANDROID)
18#include "ui/gfx/font_list.h" // nogncheck
19#include "ui/gfx/text_elider.h" // nogncheck
20#include "ui/gfx/text_utils.h" // nogncheck
21#endif
miguelgdbcfb122015-07-23 10:45:1122
wychen7b07e7b2017-01-10 17:48:2923#if defined(OS_IOS)
24#include "base/ios/ios_util.h"
25#endif
26
miguelgdbcfb122015-07-23 10:45:1127namespace {
28
29struct Testcase {
30 const std::string input;
31 const std::string output;
eugenebutbec2c5f2015-08-07 23:22:3232 enum SupportedPlatforms {
33 ALL = 0,
34 NO_IOS9_OR_LATER,
35 NO_IOS,
36 } platforms;
miguelgdbcfb122015-07-23 10:45:1137};
38
39#if !defined(OS_ANDROID)
40void RunUrlTest(Testcase* testcases, size_t num_testcases) {
41 static const gfx::FontList font_list;
42 for (size_t i = 0; i < num_testcases; ++i) {
43 const GURL url(testcases[i].input);
miguelgdbcfb122015-07-23 10:45:1144 const float available_width =
rsleevi24f64dc22015-08-07 21:39:2145 gfx::GetStringWidthF(base::UTF8ToUTF16(testcases[i].output), font_list);
46 EXPECT_EQ(base::UTF8ToUTF16(testcases[i].output),
jshin1fb76462016-04-05 22:13:0347 url_formatter::ElideUrl(url, font_list, available_width));
miguelgdbcfb122015-07-23 10:45:1148 }
49}
50
51// Test eliding of commonplace URLs.
52TEST(TextEliderTest, TestGeneralEliding) {
rsleevi24f64dc22015-08-07 21:39:2153 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:1154 Testcase testcases[] = {
55 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"},
56 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"},
57 {"http://www.google.com/intl/en/ads/",
58 "google.com/intl/" + kEllipsisStr + "/ads/"},
59 {"http://www.google.com/intl/en/ads/",
60 "google.com/" + kEllipsisStr + "/ads/"},
61 {"http://www.google.com/intl/en/ads/", "google.com/" + kEllipsisStr},
62 {"http://www.google.com/intl/en/ads/", "goog" + kEllipsisStr},
63 {"https://subdomain.foo.com/bar/filename.html",
64 "subdomain.foo.com/bar/filename.html"},
65 {"https://subdomain.foo.com/bar/filename.html",
66 "subdomain.foo.com/" + kEllipsisStr + "/filename.html"},
67 {"http://subdomain.foo.com/bar/filename.html",
68 kEllipsisStr + "foo.com/" + kEllipsisStr + "/filename.html"},
69 {"http://www.google.com/intl/en/ads/?aLongQueryWhichIsNotRequired",
70 "www.google.com/intl/en/ads/?aLongQ" + kEllipsisStr},
71 };
72
73 RunUrlTest(testcases, arraysize(testcases));
74}
75
76// When there is very little space available, the elision code will shorten
77// both path AND file name to an ellipsis - ".../...". To avoid this result,
78// there is a hack in place that simply treats them as one string in this
79// case.
80TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) {
rsleevi24f64dc22015-08-07 21:39:2181 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:1182
83 // Very little space, would cause double ellipsis.
84 gfx::FontList font_list;
85 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html");
rsleevi24f64dc22015-08-07 21:39:2186 float available_width = gfx::GetStringWidthF(
87 base::UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr),
miguelgdbcfb122015-07-23 10:45:1188 font_list);
89
90 // Create the expected string, after elision. Depending on font size, the
91 // directory might become /dir... or /di... or/d... - it never should be
92 // shorter than that. (If it is, the font considers d... to be longer
93 // than .../... - that should never happen).
rsleevi24f64dc22015-08-07 21:39:2194 ASSERT_GT(
95 gfx::GetStringWidthF(base::UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr),
96 font_list),
97 gfx::GetStringWidthF(base::UTF8ToUTF16("d" + kEllipsisStr), font_list));
miguelgdbcfb122015-07-23 10:45:1198 GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc");
rsleevi24f64dc22015-08-07 21:39:2199 base::string16 expected = url_formatter::ElideUrl(
jshin1fb76462016-04-05 22:13:03100 long_url, font_list, available_width);
miguelgdbcfb122015-07-23 10:45:11101 // Ensure that the expected result still contains part of the directory name.
102 ASSERT_GT(expected.length(), std::string("battersbox.com/d").length());
jshin1fb76462016-04-05 22:13:03103 EXPECT_EQ(expected, url_formatter::ElideUrl(url, font_list, available_width));
miguelgdbcfb122015-07-23 10:45:11104
105 // More space available - elide directories, partially elide filename.
106 Testcase testcases[] = {
107 {"http://battersbox.com/directory/foo/peter_paul_and_mary.html",
108 "battersbox.com/" + kEllipsisStr + "/peter" + kEllipsisStr},
109 };
110 RunUrlTest(testcases, arraysize(testcases));
111}
112
113// Test eliding of empty strings, URLs with ports, passwords, queries, etc.
114TEST(TextEliderTest, TestMoreEliding) {
kulshince612eb2016-06-29 18:46:51115#if defined(OS_WIN)
116 // Needed to bypass DCHECK in GetFallbackFont.
117 base::MessageLoopForUI message_loop;
118#endif
rsleevi24f64dc22015-08-07 21:39:21119 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:11120 Testcase testcases[] = {
121 {"http://www.google.com/foo?bar", "www.google.com/foo?bar"},
122 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo?" + kEllipsisStr},
123 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo" + kEllipsisStr},
124 {"http://xyz.google.com/foo?bar", "xyz.google.com/fo" + kEllipsisStr},
125 {"http://a.b.com/pathname/c?d", "a.b.com/" + kEllipsisStr + "/c?d"},
126 {"", ""},
127 {"http://foo.bar..example.com...hello/test/filename.html",
128 "foo.bar..example.com...hello/" + kEllipsisStr + "/filename.html"},
129 {"http://foo.bar../", "foo.bar.."},
130 {"http://xn--1lq90i.cn/foo", "\xe5\x8c\x97\xe4\xba\xac.cn/foo"},
131 {"http://me:[email protected]:99/foo?bar#baz",
132 "secrethost.com:99/foo?bar#baz"},
133 {"http://me:mypass@ss%xxfdsf.com/foo", "ss%25xxfdsf.com/foo"},
134 {"mailto:[email protected]", "mailto:[email protected]"},
135 {"javascript:click(0)", "javascript:click(0)"},
136 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename",
137 "chess.eecs.berkeley.edu:4430/login/arbitfilename"},
138 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename",
139 kEllipsisStr + "berkeley.edu:4430/" + kEllipsisStr + "/arbitfilename"},
140
141 // Unescaping.
142 {"http://www/%E4%BD%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0",
143 "www/\xe4\xbd\xa0\xe5\xa5\xbd?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"},
144
145 // Invalid unescaping for path. The ref will always be valid UTF-8. We
146 // don't
147 // bother to do too many edge cases, since these are handled by the
148 // escaper
149 // unittest.
150 {"http://www/%E4%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0",
151 "www/%E4%A0%E5%A5%BD?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"},
152 };
153
154 RunUrlTest(testcases, arraysize(testcases));
155}
156
157// Test eliding of file: URLs.
158TEST(TextEliderTest, TestFileURLEliding) {
rsleevi24f64dc22015-08-07 21:39:21159 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:11160 Testcase testcases[] = {
161 {"file:///C:/path1/path2/path3/filename",
162 "file:///C:/path1/path2/path3/filename"},
163 {"file:///C:/path1/path2/path3/filename", "C:/path1/path2/path3/filename"},
164// GURL parses "file:///C:path" differently on windows than it does on posix.
165#if defined(OS_WIN)
166 {"file:///C:path1/path2/path3/filename",
167 "C:/path1/path2/" + kEllipsisStr + "/filename"},
168 {"file:///C:path1/path2/path3/filename",
169 "C:/path1/" + kEllipsisStr + "/filename"},
170 {"file:///C:path1/path2/path3/filename",
171 "C:/" + kEllipsisStr + "/filename"},
172#endif // defined(OS_WIN)
173 {"file://filer/foo/bar/file", "filer/foo/bar/file"},
174 {"file://filer/foo/bar/file", "filer/foo/" + kEllipsisStr + "/file"},
175 {"file://filer/foo/bar/file", "filer/" + kEllipsisStr + "/file"},
176 {"file://filer/foo/", "file://filer/foo/"},
177 {"file://filer/foo/", "filer/foo/"},
178 {"file://filer/foo/", "filer" + kEllipsisStr},
179 // Eliding file URLs with nothing after the ':' shouldn't crash.
180 {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:", "aaa" + kEllipsisStr},
181 {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:/", "aaa" + kEllipsisStr},
182 };
183
184 RunUrlTest(testcases, arraysize(testcases));
185}
186
187TEST(TextEliderTest, TestHostEliding) {
rsleevi24f64dc22015-08-07 21:39:21188 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:11189 Testcase testcases[] = {
eugenebutbec2c5f2015-08-07 23:22:32190 {"http://google.com", "google.com"},
191 // iOS width calculations are off by a letter from other platforms for
192 // strings with too many kerned letters on the default font set.
193 // TODO(rohitrao): Fix secure_display::ElideHost for iOS
194 // (crbug.com/517604).
195 {"http://subdomain.google.com", kEllipsisStr + ".google.com",
196 Testcase::NO_IOS9_OR_LATER},
197 {"http://reallyreallyreallylongdomainname.com",
198 "reallyreallyreallylongdomainname.com"},
199 {"http://a.b.c.d.e.f.com", kEllipsisStr + "f.com",
200 Testcase::NO_IOS9_OR_LATER},
201 {"http://foo", "foo"},
202 {"http://foo.bar", "foo.bar"},
203 {"http://subdomain.foo.bar", kEllipsisStr + "in.foo.bar",
204 Testcase::NO_IOS9_OR_LATER},
205 {"http://subdomain.reallylongdomainname.com",
206 kEllipsisStr + "ain.reallylongdomainname.com", Testcase::NO_IOS},
207 {"http://a.b.c.d.e.f.com", kEllipsisStr + ".e.f.com", Testcase::NO_IOS},
jshinee46c77d2016-04-07 00:16:42208 // IDN - Greek alpha.beta.gamma.delta.epsilon.zeta.com
209 {"http://xn--mxa.xn--nxa.xn--oxa.xn--pxa.xn--qxa.xn--rxa.com",
210 kEllipsisStr + ".\xCE\xB5.\xCE\xB6.com", Testcase::NO_IOS},
miguelgdbcfb122015-07-23 10:45:11211 };
212
213 for (size_t i = 0; i < arraysize(testcases); ++i) {
eugenebutbec2c5f2015-08-07 23:22:32214#if defined(OS_IOS)
215 if (testcases[i].platforms == Testcase::NO_IOS ||
216 (testcases[i].platforms == Testcase::NO_IOS9_OR_LATER &&
217 base::ios::IsRunningOnIOS9OrLater())) {
218 continue;
219 }
220#endif
rsleevi24f64dc22015-08-07 21:39:21221 const float available_width = gfx::GetStringWidthF(
222 base::UTF8ToUTF16(testcases[i].output), gfx::FontList());
223 EXPECT_EQ(base::UTF8ToUTF16(testcases[i].output),
224 url_formatter::ElideHost(GURL(testcases[i].input),
225 gfx::FontList(), available_width));
miguelgdbcfb122015-07-23 10:45:11226 }
227
228 // Trying to elide to a really short length will still keep the full TLD+1
229 EXPECT_EQ(
230 base::ASCIIToUTF16("google.com"),
rsleevi24f64dc22015-08-07 21:39:21231 url_formatter::ElideHost(GURL("http://google.com"), gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11232 EXPECT_EQ(base::UTF8ToUTF16(kEllipsisStr + ".google.com"),
rsleevi24f64dc22015-08-07 21:39:21233 url_formatter::ElideHost(GURL("http://subdomain.google.com"),
234 gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11235 EXPECT_EQ(
236 base::ASCIIToUTF16("foo.bar"),
rsleevi24f64dc22015-08-07 21:39:21237 url_formatter::ElideHost(GURL("http://foo.bar"), gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11238}
239
240#endif // !defined(OS_ANDROID)
241
juncaicb63cac2016-05-13 00:41:59242struct OriginTestData {
243 const char* const description;
244 const char* const input;
245 const wchar_t* const output;
246 const wchar_t* const output_omit_web_scheme;
247 const wchar_t* const output_omit_cryptographic_scheme;
248};
249
250// Common test data for both FormatUrlForSecurityDisplay() and
251// FormatOriginForSecurityDisplay()
252const OriginTestData common_tests[] = {
253 {"Empty URL", "", L"", L"", L""},
254 {"HTTP URL", "http://www.google.com/", L"http://www.google.com",
255 L"www.google.com", L"http://www.google.com"},
256 {"HTTPS URL", "https://www.google.com/", L"https://www.google.com",
257 L"www.google.com", L"www.google.com"},
258 {"Standard HTTP port", "http://www.google.com:80/",
259 L"http://www.google.com", L"www.google.com", L"http://www.google.com"},
260 {"Standard HTTPS port", "https://www.google.com:443/",
261 L"https://www.google.com", L"www.google.com", L"www.google.com"},
262 {"Standard HTTP port, IDN Chinese",
263 "http://\xe4\xb8\xad\xe5\x9b\xbd.icom.museum:80",
jshin78809c4d82016-10-06 20:15:45264 L"http://\x4e2d\x56fd.icom.museum", L"\x4e2d\x56fd.icom.museum",
265 L"http://\x4e2d\x56fd.icom.museum"},
juncaicb63cac2016-05-13 00:41:59266 {"HTTP URL, IDN Hebrew (RTL)",
267 "http://"
268 "\xd7\x90\xd7\x99\xd7\xa7\xd7\x95\xd7\xb4\xd7\x9d."
269 "\xd7\x99\xd7\xa9\xd7\xa8\xd7\x90\xd7\x9c.museum/",
270 L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum",
271 L"xn--4dbklr2c8d.xn--4dbrk0ce.museum",
272 L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum"},
273 {"HTTP URL with query string, IDN Arabic (RTL)",
274 "http://\xd9\x85\xd8\xb5\xd8\xb1.icom.museum/foo.html?yes=no",
275 L"http://xn--wgbh1c.icom.museum", L"xn--wgbh1c.icom.museum",
276 L"http://xn--wgbh1c.icom.museum"},
277 {"Non-standard HTTP port", "http://www.google.com:9000/",
278 L"http://www.google.com:9000", L"www.google.com:9000",
279 L"http://www.google.com:9000"},
280 {"Non-standard HTTPS port", "https://www.google.com:9000/",
281 L"https://www.google.com:9000", L"www.google.com:9000",
282 L"www.google.com:9000"},
283 {"HTTP URL with path", "http://www.google.com/test.html",
284 L"http://www.google.com", L"www.google.com", L"http://www.google.com"},
285 {"HTTPS URL with path", "https://www.google.com/test.html",
286 L"https://www.google.com", L"www.google.com", L"www.google.com"},
287 {"Unusual secure scheme (wss)", "wss://www.google.com/",
288 L"wss://www.google.com", L"wss://www.google.com", L"www.google.com"},
289 {"Unusual non-secure scheme (gopher)", "gopher://www.google.com/",
290 L"gopher://www.google.com", L"gopher://www.google.com",
291 L"gopher://www.google.com"},
292 {"Unlisted scheme (chrome)", "chrome://version", L"chrome://version",
293 L"chrome://version", L"chrome://version"},
294 {"HTTP IP address", "http://173.194.65.103", L"http://173.194.65.103",
295 L"173.194.65.103", L"http://173.194.65.103"},
296 {"HTTPS IP address", "https://173.194.65.103", L"https://173.194.65.103",
297 L"173.194.65.103", L"173.194.65.103"},
298 {"HTTP IPv6 address", "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/",
299 L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
300 L"http://[fe80::202:b3ff:fe1e:8329]"},
301 {"HTTPs IPv6 address", "https://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/",
302 L"https://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
303 L"[fe80::202:b3ff:fe1e:8329]"},
304 {"HTTP IPv6 address with port",
305 "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:80/",
306 L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
307 L"http://[fe80::202:b3ff:fe1e:8329]"},
308 {"HTTPs IPv6 address with port",
309 "https://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:443/",
310 L"https://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
311 L"[fe80::202:b3ff:fe1e:8329]"},
312 {"HTTPS IP address, non-default port", "https://173.194.65.103:8443",
313 L"https://173.194.65.103:8443", L"173.194.65.103:8443",
314 L"173.194.65.103:8443"},
315 {"Invalid host 1", "https://www.cyber../wow.php", L"https://www.cyber..",
316 L"www.cyber..", L"www.cyber.."},
317 {"Invalid host 2", "https://www...cyber/wow.php", L"https://www...cyber",
318 L"www...cyber", L"www...cyber"},
319 {"Invalid port 3", "https://173.194.65.103:/hello.aspx",
320 L"https://173.194.65.103", L"173.194.65.103", L"173.194.65.103"},
321 {"Trailing dot in DNS name", "https://www.example.com./get/goat",
322 L"https://www.example.com.", L"www.example.com.", L"www.example.com."}};
323
miguelgdbcfb122015-07-23 10:45:11324TEST(TextEliderTest, FormatUrlForSecurityDisplay) {
juncaicb63cac2016-05-13 00:41:59325 for (size_t i = 0; i < arraysize(common_tests); ++i) {
326 base::string16 formatted =
327 url_formatter::FormatUrlForSecurityDisplay(GURL(common_tests[i].input));
328 EXPECT_EQ(base::WideToUTF16(common_tests[i].output), formatted)
329 << common_tests[i].description;
330
331 base::string16 formatted_omit_web_scheme =
332 url_formatter::FormatUrlForSecurityDisplay(
333 GURL(common_tests[i].input),
334 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
335 EXPECT_EQ(base::WideToUTF16(common_tests[i].output_omit_web_scheme),
336 formatted_omit_web_scheme)
337 << common_tests[i].description;
338
339 base::string16 formatted_omit_cryptographic_scheme =
340 url_formatter::FormatUrlForSecurityDisplay(
341 GURL(common_tests[i].input),
342 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
343 EXPECT_EQ(
344 base::WideToUTF16(common_tests[i].output_omit_cryptographic_scheme),
345 formatted_omit_cryptographic_scheme)
346 << common_tests[i].description;
347 }
miguelgdbcfb122015-07-23 10:45:11348
349 const OriginTestData tests[] = {
miguelgdbcfb122015-07-23 10:45:11350 {"File URI", "file:///usr/example/file.html",
benwells2337b8102016-04-20 01:53:53351 L"file:///usr/example/file.html", L"file:///usr/example/file.html",
352 L"file:///usr/example/file.html"},
miguelgdbcfb122015-07-23 10:45:11353 {"File URI with hostname", "file://localhost/usr/example/file.html",
benwells2337b8102016-04-20 01:53:53354 L"file:///usr/example/file.html", L"file:///usr/example/file.html",
355 L"file:///usr/example/file.html"},
miguelgdbcfb122015-07-23 10:45:11356 {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls",
palmer153af982015-09-15 02:04:19357 L"file:///CONTOSO/accounting/money.xls",
benwells2337b8102016-04-20 01:53:53358 L"file:///CONTOSO/accounting/money.xls",
miguelgdbcfb122015-07-23 10:45:11359 L"file:///CONTOSO/accounting/money.xls"},
360 {"UNC File URI 2",
361 "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO",
palmer153af982015-09-15 02:04:19362 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html",
benwells2337b8102016-04-20 01:53:53363 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html",
miguelgdbcfb122015-07-23 10:45:11364 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html"},
benwells2337b8102016-04-20 01:53:53365 {"Invalid IPv6 address", "https://[2001:db8:0:1]/",
366 L"https://[2001:db8:0:1]", L"https://[2001:db8:0:1]",
367 L"https://[2001:db8:0:1]"},
miguelgdbcfb122015-07-23 10:45:11368 {"HTTP filesystem: URL with path",
369 "filesystem:http://www.google.com/temporary/test.html",
benwells2337b8102016-04-20 01:53:53370 L"filesystem:http://www.google.com", L"filesystem:http://www.google.com",
miguelgdbcfb122015-07-23 10:45:11371 L"filesystem:http://www.google.com"},
372 {"File filesystem: URL with path",
juncaicb63cac2016-05-13 00:41:59373 "filesystem:file://localhost/temporary/stuff/"
374 "test.html?z=fun&goat=billy",
palmer153af982015-09-15 02:04:19375 L"filesystem:file:///temporary/stuff/test.html",
benwells2337b8102016-04-20 01:53:53376 L"filesystem:file:///temporary/stuff/test.html",
miguelgdbcfb122015-07-23 10:45:11377 L"filesystem:file:///temporary/stuff/test.html"},
378 {"Invalid scheme 1", "twelve://www.cyber.org/wow.php",
benwells2337b8102016-04-20 01:53:53379 L"twelve://www.cyber.org/wow.php", L"twelve://www.cyber.org/wow.php",
380 L"twelve://www.cyber.org/wow.php"},
miguelgdbcfb122015-07-23 10:45:11381 {"Invalid scheme 2", "://www.cyber.org/wow.php",
benwells2337b8102016-04-20 01:53:53382 L"://www.cyber.org/wow.php", L"://www.cyber.org/wow.php",
383 L"://www.cyber.org/wow.php"},
miguelgdbcfb122015-07-23 10:45:11384 {"Invalid port 1", "https://173.194.65.103:000",
benwells2337b8102016-04-20 01:53:53385 L"https://173.194.65.103:0", L"173.194.65.103:0", L"173.194.65.103:0"},
miguelgdbcfb122015-07-23 10:45:11386 {"Invalid port 2", "https://173.194.65.103:gruffle",
benwells2337b8102016-04-20 01:53:53387 L"https://173.194.65.103:gruffle", L"https://173.194.65.103:gruffle",
388 L"https://173.194.65.103:gruffle"},
miguelgdbcfb122015-07-23 10:45:11389 {"Blob URL",
nick1b17dc32016-04-15 21:34:04390 "blob:http://www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
391 L"blob:http://www.html5rocks.com/"
palmer153af982015-09-15 02:04:19392 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
nick1b17dc32016-04-15 21:34:04393 L"blob:http://www.html5rocks.com/"
benwells2337b8102016-04-20 01:53:53394 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
395 L"blob:http://www.html5rocks.com/"
palmer153af982015-09-15 02:04:19396 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9"}};
miguelgdbcfb122015-07-23 10:45:11397
miguelgdbcfb122015-07-23 10:45:11398 for (size_t i = 0; i < arraysize(tests); ++i) {
jshin1fb76462016-04-05 22:13:03399 base::string16 formatted =
400 url_formatter::FormatUrlForSecurityDisplay(GURL(tests[i].input));
miguelgdbcfb122015-07-23 10:45:11401 EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted)
402 << tests[i].description;
palmer153af982015-09-15 02:04:19403
benwells2337b8102016-04-20 01:53:53404 base::string16 formatted_omit_web_scheme =
405 url_formatter::FormatUrlForSecurityDisplay(
406 GURL(tests[i].input),
407 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
408 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_web_scheme),
409 formatted_omit_web_scheme)
410 << tests[i].description;
411
412 base::string16 formatted_omit_cryptographic_scheme =
413 url_formatter::FormatUrlForSecurityDisplay(
414 GURL(tests[i].input),
415 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
416 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_cryptographic_scheme),
417 formatted_omit_cryptographic_scheme)
palmer153af982015-09-15 02:04:19418 << tests[i].description;
miguelgdbcfb122015-07-23 10:45:11419 }
420
juncaicb63cac2016-05-13 00:41:59421 base::string16 formatted = url_formatter::FormatUrlForSecurityDisplay(GURL());
miguelgdbcfb122015-07-23 10:45:11422 EXPECT_EQ(base::string16(), formatted)
423 << "Explicitly test the 0-argument GURL constructor";
palmer153af982015-09-15 02:04:19424
425 base::string16 formatted_omit_scheme =
benwells2337b8102016-04-20 01:53:53426 url_formatter::FormatUrlForSecurityDisplay(
427 GURL(), url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
428 EXPECT_EQ(base::string16(), formatted_omit_scheme)
429 << "Explicitly test the 0-argument GURL constructor";
430
431 formatted_omit_scheme = url_formatter::FormatUrlForSecurityDisplay(
432 GURL(), url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
palmer153af982015-09-15 02:04:19433 EXPECT_EQ(base::string16(), formatted_omit_scheme)
434 << "Explicitly test the 0-argument GURL constructor";
miguelgdbcfb122015-07-23 10:45:11435}
436
juncaicb63cac2016-05-13 00:41:59437TEST(TextEliderTest, FormatOriginForSecurityDisplay) {
438 for (size_t i = 0; i < arraysize(common_tests); ++i) {
439 base::string16 formatted = url_formatter::FormatOriginForSecurityDisplay(
440 url::Origin(GURL(common_tests[i].input)));
441 EXPECT_EQ(base::WideToUTF16(common_tests[i].output), formatted)
442 << common_tests[i].description;
443
444 base::string16 formatted_omit_web_scheme =
445 url_formatter::FormatOriginForSecurityDisplay(
446 url::Origin(GURL(common_tests[i].input)),
447 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
448 EXPECT_EQ(base::WideToUTF16(common_tests[i].output_omit_web_scheme),
449 formatted_omit_web_scheme)
450 << common_tests[i].description;
451
452 base::string16 formatted_omit_cryptographic_scheme =
453 url_formatter::FormatOriginForSecurityDisplay(
454 url::Origin(GURL(common_tests[i].input)),
455 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
456 EXPECT_EQ(
457 base::WideToUTF16(common_tests[i].output_omit_cryptographic_scheme),
458 formatted_omit_cryptographic_scheme)
459 << common_tests[i].description;
460 }
461
462 const OriginTestData tests[] = {
463 {"File URI", "file:///usr/example/file.html", L"file://", L"file://",
464 L"file://"},
465 {"File URI with hostname", "file://localhost/usr/example/file.html",
466 L"file://localhost", L"file://localhost", L"file://localhost"},
467 {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls", L"file://",
468 L"file://", L"file://"},
469 {"UNC File URI 2",
470 "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO",
471 L"file://", L"file://", L"file://"},
472 {"Invalid IPv6 address", "https://[2001:db8:0:1]/", L"", L"", L""},
473 {"HTTP filesystem: URL with path",
474 "filesystem:http://www.google.com/temporary/test.html",
475 L"http://www.google.com", L"www.google.com", L"http://www.google.com"},
476 {"File filesystem: URL with path",
477 "filesystem:file://localhost/temporary/stuff/test.html?z=fun&goat=billy",
478 L"file://", L"file://", L"file://"},
479 {"Invalid scheme 1", "twelve://www.cyber.org/wow.php", L"", L"", L""},
480 {"Invalid scheme 2", "://www.cyber.org/wow.php", L"", L"", L""},
481 {"Invalid port 1", "https://173.194.65.103:000", L"", L"", L""},
482 {"Invalid port 2", "https://173.194.65.103:gruffle", L"", L"", L""},
483 {"Blob URL",
484 "blob:http://www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
485 L"http://www.html5rocks.com", L"www.html5rocks.com",
486 L"http://www.html5rocks.com"}};
487
488 for (size_t i = 0; i < arraysize(tests); ++i) {
489 base::string16 formatted = url_formatter::FormatOriginForSecurityDisplay(
490 url::Origin(GURL(tests[i].input)));
491 EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted)
492 << tests[i].description;
493
494 base::string16 formatted_omit_web_scheme =
495 url_formatter::FormatOriginForSecurityDisplay(
496 url::Origin(GURL(tests[i].input)),
497 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
498 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_web_scheme),
499 formatted_omit_web_scheme)
500 << tests[i].description;
501
502 base::string16 formatted_omit_cryptographic_scheme =
503 url_formatter::FormatOriginForSecurityDisplay(
504 url::Origin(GURL(tests[i].input)),
505 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
506 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_cryptographic_scheme),
507 formatted_omit_cryptographic_scheme)
508 << tests[i].description;
509 }
510
511 base::string16 formatted =
512 url_formatter::FormatOriginForSecurityDisplay(url::Origin(GURL()));
513 EXPECT_EQ(base::string16(), formatted)
514 << "Explicitly test the url::Origin which takes an empty, invalid URL";
515
516 base::string16 formatted_omit_scheme =
517 url_formatter::FormatOriginForSecurityDisplay(
518 url::Origin(GURL()),
519 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
520 EXPECT_EQ(base::string16(), formatted_omit_scheme)
521 << "Explicitly test the url::Origin which takes an empty, invalid URL";
522
523 formatted_omit_scheme = url_formatter::FormatOriginForSecurityDisplay(
524 url::Origin(GURL()), url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
525 EXPECT_EQ(base::string16(), formatted_omit_scheme)
526 << "Explicitly test the url::Origin which takes an empty, invalid URL";
527}
528
miguelgdbcfb122015-07-23 10:45:11529} // namespace