blob: 4ded2f99db30df35939ddd940a79f6ac85ec5bd2 [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"
gabf64a25e2017-05-12 19:42:5610#include "base/message_loop/message_loop.h"
kulshince612eb2016-06-29 18:46:5111#include "base/run_loop.h"
miguelgdbcfb122015-07-23 10:45:1112#include "base/strings/utf_string_conversions.h"
avi5dd91f82015-12-25 22:30:4613#include "build/build_config.h"
miguelgdbcfb122015-07-23 10:45:1114#include "testing/gtest/include/gtest/gtest.h"
miguelgdbcfb122015-07-23 10:45:1115#include "url/gurl.h"
juncaicb63cac2016-05-13 00:41:5916#include "url/origin.h"
miguelgdbcfb122015-07-23 10:45:1117
rsleevi24f64dc22015-08-07 21:39:2118#if !defined(OS_ANDROID)
19#include "ui/gfx/font_list.h" // nogncheck
20#include "ui/gfx/text_elider.h" // nogncheck
21#include "ui/gfx/text_utils.h" // nogncheck
22#endif
miguelgdbcfb122015-07-23 10:45:1123
miguelgdbcfb122015-07-23 10:45:1124namespace {
25
26struct Testcase {
27 const std::string input;
28 const std::string output;
miguelgdbcfb122015-07-23 10:45:1129};
30
31#if !defined(OS_ANDROID)
32void RunUrlTest(Testcase* testcases, size_t num_testcases) {
33 static const gfx::FontList font_list;
34 for (size_t i = 0; i < num_testcases; ++i) {
35 const GURL url(testcases[i].input);
miguelgdbcfb122015-07-23 10:45:1136 const float available_width =
rsleevi24f64dc22015-08-07 21:39:2137 gfx::GetStringWidthF(base::UTF8ToUTF16(testcases[i].output), font_list);
38 EXPECT_EQ(base::UTF8ToUTF16(testcases[i].output),
jshin1fb76462016-04-05 22:13:0339 url_formatter::ElideUrl(url, font_list, available_width));
miguelgdbcfb122015-07-23 10:45:1140 }
41}
42
43// Test eliding of commonplace URLs.
44TEST(TextEliderTest, TestGeneralEliding) {
rsleevi24f64dc22015-08-07 21:39:2145 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:1146 Testcase testcases[] = {
47 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"},
48 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"},
49 {"http://www.google.com/intl/en/ads/",
50 "google.com/intl/" + kEllipsisStr + "/ads/"},
51 {"http://www.google.com/intl/en/ads/",
52 "google.com/" + kEllipsisStr + "/ads/"},
53 {"http://www.google.com/intl/en/ads/", "google.com/" + kEllipsisStr},
54 {"http://www.google.com/intl/en/ads/", "goog" + kEllipsisStr},
55 {"https://subdomain.foo.com/bar/filename.html",
56 "subdomain.foo.com/bar/filename.html"},
57 {"https://subdomain.foo.com/bar/filename.html",
58 "subdomain.foo.com/" + kEllipsisStr + "/filename.html"},
59 {"http://subdomain.foo.com/bar/filename.html",
60 kEllipsisStr + "foo.com/" + kEllipsisStr + "/filename.html"},
61 {"http://www.google.com/intl/en/ads/?aLongQueryWhichIsNotRequired",
62 "www.google.com/intl/en/ads/?aLongQ" + kEllipsisStr},
63 };
64
65 RunUrlTest(testcases, arraysize(testcases));
66}
67
68// When there is very little space available, the elision code will shorten
69// both path AND file name to an ellipsis - ".../...". To avoid this result,
70// there is a hack in place that simply treats them as one string in this
71// case.
72TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) {
rsleevi24f64dc22015-08-07 21:39:2173 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:1174
75 // Very little space, would cause double ellipsis.
76 gfx::FontList font_list;
77 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html");
rsleevi24f64dc22015-08-07 21:39:2178 float available_width = gfx::GetStringWidthF(
79 base::UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr),
miguelgdbcfb122015-07-23 10:45:1180 font_list);
81
82 // Create the expected string, after elision. Depending on font size, the
83 // directory might become /dir... or /di... or/d... - it never should be
84 // shorter than that. (If it is, the font considers d... to be longer
85 // than .../... - that should never happen).
rsleevi24f64dc22015-08-07 21:39:2186 ASSERT_GT(
87 gfx::GetStringWidthF(base::UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr),
88 font_list),
89 gfx::GetStringWidthF(base::UTF8ToUTF16("d" + kEllipsisStr), font_list));
miguelgdbcfb122015-07-23 10:45:1190 GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc");
rsleevi24f64dc22015-08-07 21:39:2191 base::string16 expected = url_formatter::ElideUrl(
jshin1fb76462016-04-05 22:13:0392 long_url, font_list, available_width);
miguelgdbcfb122015-07-23 10:45:1193 // Ensure that the expected result still contains part of the directory name.
94 ASSERT_GT(expected.length(), std::string("battersbox.com/d").length());
jshin1fb76462016-04-05 22:13:0395 EXPECT_EQ(expected, url_formatter::ElideUrl(url, font_list, available_width));
miguelgdbcfb122015-07-23 10:45:1196
97 // More space available - elide directories, partially elide filename.
98 Testcase testcases[] = {
99 {"http://battersbox.com/directory/foo/peter_paul_and_mary.html",
100 "battersbox.com/" + kEllipsisStr + "/peter" + kEllipsisStr},
101 };
102 RunUrlTest(testcases, arraysize(testcases));
103}
104
105// Test eliding of empty strings, URLs with ports, passwords, queries, etc.
106TEST(TextEliderTest, TestMoreEliding) {
kulshince612eb2016-06-29 18:46:51107#if defined(OS_WIN)
108 // Needed to bypass DCHECK in GetFallbackFont.
109 base::MessageLoopForUI message_loop;
110#endif
rsleevi24f64dc22015-08-07 21:39:21111 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:11112 Testcase testcases[] = {
113 {"http://www.google.com/foo?bar", "www.google.com/foo?bar"},
114 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo?" + kEllipsisStr},
115 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo" + kEllipsisStr},
116 {"http://xyz.google.com/foo?bar", "xyz.google.com/fo" + kEllipsisStr},
117 {"http://a.b.com/pathname/c?d", "a.b.com/" + kEllipsisStr + "/c?d"},
118 {"", ""},
119 {"http://foo.bar..example.com...hello/test/filename.html",
120 "foo.bar..example.com...hello/" + kEllipsisStr + "/filename.html"},
121 {"http://foo.bar../", "foo.bar.."},
122 {"http://xn--1lq90i.cn/foo", "\xe5\x8c\x97\xe4\xba\xac.cn/foo"},
123 {"http://me:[email protected]:99/foo?bar#baz",
124 "secrethost.com:99/foo?bar#baz"},
125 {"http://me:mypass@ss%xxfdsf.com/foo", "ss%25xxfdsf.com/foo"},
126 {"mailto:[email protected]", "mailto:[email protected]"},
127 {"javascript:click(0)", "javascript:click(0)"},
128 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename",
129 "chess.eecs.berkeley.edu:4430/login/arbitfilename"},
130 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename",
131 kEllipsisStr + "berkeley.edu:4430/" + kEllipsisStr + "/arbitfilename"},
132
133 // Unescaping.
134 {"http://www/%E4%BD%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0",
135 "www/\xe4\xbd\xa0\xe5\xa5\xbd?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"},
136
137 // Invalid unescaping for path. The ref will always be valid UTF-8. We
138 // don't
139 // bother to do too many edge cases, since these are handled by the
140 // escaper
141 // unittest.
142 {"http://www/%E4%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0",
143 "www/%E4%A0%E5%A5%BD?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"},
144 };
145
146 RunUrlTest(testcases, arraysize(testcases));
147}
148
149// Test eliding of file: URLs.
150TEST(TextEliderTest, TestFileURLEliding) {
rsleevi24f64dc22015-08-07 21:39:21151 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:11152 Testcase testcases[] = {
153 {"file:///C:/path1/path2/path3/filename",
154 "file:///C:/path1/path2/path3/filename"},
155 {"file:///C:/path1/path2/path3/filename", "C:/path1/path2/path3/filename"},
156// GURL parses "file:///C:path" differently on windows than it does on posix.
157#if defined(OS_WIN)
158 {"file:///C:path1/path2/path3/filename",
159 "C:/path1/path2/" + kEllipsisStr + "/filename"},
160 {"file:///C:path1/path2/path3/filename",
161 "C:/path1/" + kEllipsisStr + "/filename"},
162 {"file:///C:path1/path2/path3/filename",
163 "C:/" + kEllipsisStr + "/filename"},
164#endif // defined(OS_WIN)
165 {"file://filer/foo/bar/file", "filer/foo/bar/file"},
166 {"file://filer/foo/bar/file", "filer/foo/" + kEllipsisStr + "/file"},
167 {"file://filer/foo/bar/file", "filer/" + kEllipsisStr + "/file"},
168 {"file://filer/foo/", "file://filer/foo/"},
169 {"file://filer/foo/", "filer/foo/"},
170 {"file://filer/foo/", "filer" + kEllipsisStr},
171 // Eliding file URLs with nothing after the ':' shouldn't crash.
172 {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:", "aaa" + kEllipsisStr},
173 {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:/", "aaa" + kEllipsisStr},
174 };
175
176 RunUrlTest(testcases, arraysize(testcases));
177}
178
179TEST(TextEliderTest, TestHostEliding) {
rsleevi24f64dc22015-08-07 21:39:21180 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:11181 Testcase testcases[] = {
pkl2affa912017-02-22 15:23:20182 {"http://google.com", "google.com"},
183 {"http://reallyreallyreallylongdomainname.com",
184 "reallyreallyreallylongdomainname.com"},
185 {"http://foo", "foo"},
186 {"http://foo.bar", "foo.bar"},
pkl2affa912017-02-22 15:23:20187 {"http://subdomain.google.com", kEllipsisStr + ".google.com"},
188 {"http://a.b.c.d.e.f.com", kEllipsisStr + "f.com"},
189 {"http://subdomain.foo.bar", kEllipsisStr + "in.foo.bar"},
190 {"http://subdomain.reallylongdomainname.com",
191 kEllipsisStr + "ain.reallylongdomainname.com"},
192 {"http://a.b.c.d.e.f.com", kEllipsisStr + ".e.f.com"},
193 // IDN - Greek alpha.beta.gamma.delta.epsilon.zeta.com
194 {"http://xn--mxa.xn--nxa.xn--oxa.xn--pxa.xn--qxa.xn--rxa.com",
195 kEllipsisStr + ".\xCE\xB5.\xCE\xB6.com"},
miguelgdbcfb122015-07-23 10:45:11196 };
197
198 for (size_t i = 0; i < arraysize(testcases); ++i) {
rsleevi24f64dc22015-08-07 21:39:21199 const float available_width = gfx::GetStringWidthF(
200 base::UTF8ToUTF16(testcases[i].output), gfx::FontList());
201 EXPECT_EQ(base::UTF8ToUTF16(testcases[i].output),
202 url_formatter::ElideHost(GURL(testcases[i].input),
203 gfx::FontList(), available_width));
miguelgdbcfb122015-07-23 10:45:11204 }
205
206 // Trying to elide to a really short length will still keep the full TLD+1
207 EXPECT_EQ(
208 base::ASCIIToUTF16("google.com"),
rsleevi24f64dc22015-08-07 21:39:21209 url_formatter::ElideHost(GURL("http://google.com"), gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11210 EXPECT_EQ(base::UTF8ToUTF16(kEllipsisStr + ".google.com"),
rsleevi24f64dc22015-08-07 21:39:21211 url_formatter::ElideHost(GURL("http://subdomain.google.com"),
212 gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11213 EXPECT_EQ(
214 base::ASCIIToUTF16("foo.bar"),
rsleevi24f64dc22015-08-07 21:39:21215 url_formatter::ElideHost(GURL("http://foo.bar"), gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11216}
217
218#endif // !defined(OS_ANDROID)
219
juncaicb63cac2016-05-13 00:41:59220struct OriginTestData {
221 const char* const description;
222 const char* const input;
223 const wchar_t* const output;
224 const wchar_t* const output_omit_web_scheme;
225 const wchar_t* const output_omit_cryptographic_scheme;
226};
227
228// Common test data for both FormatUrlForSecurityDisplay() and
229// FormatOriginForSecurityDisplay()
230const OriginTestData common_tests[] = {
231 {"Empty URL", "", L"", L"", L""},
232 {"HTTP URL", "http://www.google.com/", L"http://www.google.com",
233 L"www.google.com", L"http://www.google.com"},
234 {"HTTPS URL", "https://www.google.com/", L"https://www.google.com",
235 L"www.google.com", L"www.google.com"},
236 {"Standard HTTP port", "http://www.google.com:80/",
237 L"http://www.google.com", L"www.google.com", L"http://www.google.com"},
238 {"Standard HTTPS port", "https://www.google.com:443/",
239 L"https://www.google.com", L"www.google.com", L"www.google.com"},
240 {"Standard HTTP port, IDN Chinese",
241 "http://\xe4\xb8\xad\xe5\x9b\xbd.icom.museum:80",
jshin78809c4d82016-10-06 20:15:45242 L"http://\x4e2d\x56fd.icom.museum", L"\x4e2d\x56fd.icom.museum",
243 L"http://\x4e2d\x56fd.icom.museum"},
juncaicb63cac2016-05-13 00:41:59244 {"HTTP URL, IDN Hebrew (RTL)",
245 "http://"
246 "\xd7\x90\xd7\x99\xd7\xa7\xd7\x95\xd7\xb4\xd7\x9d."
247 "\xd7\x99\xd7\xa9\xd7\xa8\xd7\x90\xd7\x9c.museum/",
248 L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum",
249 L"xn--4dbklr2c8d.xn--4dbrk0ce.museum",
250 L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum"},
251 {"HTTP URL with query string, IDN Arabic (RTL)",
252 "http://\xd9\x85\xd8\xb5\xd8\xb1.icom.museum/foo.html?yes=no",
253 L"http://xn--wgbh1c.icom.museum", L"xn--wgbh1c.icom.museum",
254 L"http://xn--wgbh1c.icom.museum"},
255 {"Non-standard HTTP port", "http://www.google.com:9000/",
256 L"http://www.google.com:9000", L"www.google.com:9000",
257 L"http://www.google.com:9000"},
258 {"Non-standard HTTPS port", "https://www.google.com:9000/",
259 L"https://www.google.com:9000", L"www.google.com:9000",
260 L"www.google.com:9000"},
261 {"HTTP URL with path", "http://www.google.com/test.html",
262 L"http://www.google.com", L"www.google.com", L"http://www.google.com"},
263 {"HTTPS URL with path", "https://www.google.com/test.html",
264 L"https://www.google.com", L"www.google.com", L"www.google.com"},
265 {"Unusual secure scheme (wss)", "wss://www.google.com/",
266 L"wss://www.google.com", L"wss://www.google.com", L"www.google.com"},
267 {"Unusual non-secure scheme (gopher)", "gopher://www.google.com/",
268 L"gopher://www.google.com", L"gopher://www.google.com",
269 L"gopher://www.google.com"},
270 {"Unlisted scheme (chrome)", "chrome://version", L"chrome://version",
271 L"chrome://version", L"chrome://version"},
272 {"HTTP IP address", "http://173.194.65.103", L"http://173.194.65.103",
273 L"173.194.65.103", L"http://173.194.65.103"},
274 {"HTTPS IP address", "https://173.194.65.103", L"https://173.194.65.103",
275 L"173.194.65.103", L"173.194.65.103"},
276 {"HTTP IPv6 address", "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/",
277 L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
278 L"http://[fe80::202:b3ff:fe1e:8329]"},
279 {"HTTPs IPv6 address", "https://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/",
280 L"https://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
281 L"[fe80::202:b3ff:fe1e:8329]"},
282 {"HTTP IPv6 address with port",
283 "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:80/",
284 L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
285 L"http://[fe80::202:b3ff:fe1e:8329]"},
286 {"HTTPs IPv6 address with port",
287 "https://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:443/",
288 L"https://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
289 L"[fe80::202:b3ff:fe1e:8329]"},
290 {"HTTPS IP address, non-default port", "https://173.194.65.103:8443",
291 L"https://173.194.65.103:8443", L"173.194.65.103:8443",
292 L"173.194.65.103:8443"},
293 {"Invalid host 1", "https://www.cyber../wow.php", L"https://www.cyber..",
294 L"www.cyber..", L"www.cyber.."},
295 {"Invalid host 2", "https://www...cyber/wow.php", L"https://www...cyber",
296 L"www...cyber", L"www...cyber"},
297 {"Invalid port 3", "https://173.194.65.103:/hello.aspx",
298 L"https://173.194.65.103", L"173.194.65.103", L"173.194.65.103"},
299 {"Trailing dot in DNS name", "https://www.example.com./get/goat",
300 L"https://www.example.com.", L"www.example.com.", L"www.example.com."}};
301
miguelgdbcfb122015-07-23 10:45:11302TEST(TextEliderTest, FormatUrlForSecurityDisplay) {
juncaicb63cac2016-05-13 00:41:59303 for (size_t i = 0; i < arraysize(common_tests); ++i) {
304 base::string16 formatted =
305 url_formatter::FormatUrlForSecurityDisplay(GURL(common_tests[i].input));
306 EXPECT_EQ(base::WideToUTF16(common_tests[i].output), formatted)
307 << common_tests[i].description;
308
309 base::string16 formatted_omit_web_scheme =
310 url_formatter::FormatUrlForSecurityDisplay(
311 GURL(common_tests[i].input),
312 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
313 EXPECT_EQ(base::WideToUTF16(common_tests[i].output_omit_web_scheme),
314 formatted_omit_web_scheme)
315 << common_tests[i].description;
316
317 base::string16 formatted_omit_cryptographic_scheme =
318 url_formatter::FormatUrlForSecurityDisplay(
319 GURL(common_tests[i].input),
320 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
321 EXPECT_EQ(
322 base::WideToUTF16(common_tests[i].output_omit_cryptographic_scheme),
323 formatted_omit_cryptographic_scheme)
324 << common_tests[i].description;
325 }
miguelgdbcfb122015-07-23 10:45:11326
327 const OriginTestData tests[] = {
miguelgdbcfb122015-07-23 10:45:11328 {"File URI", "file:///usr/example/file.html",
benwells2337b8102016-04-20 01:53:53329 L"file:///usr/example/file.html", L"file:///usr/example/file.html",
330 L"file:///usr/example/file.html"},
miguelgdbcfb122015-07-23 10:45:11331 {"File URI with hostname", "file://localhost/usr/example/file.html",
benwells2337b8102016-04-20 01:53:53332 L"file:///usr/example/file.html", L"file:///usr/example/file.html",
333 L"file:///usr/example/file.html"},
miguelgdbcfb122015-07-23 10:45:11334 {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls",
palmer153af982015-09-15 02:04:19335 L"file:///CONTOSO/accounting/money.xls",
benwells2337b8102016-04-20 01:53:53336 L"file:///CONTOSO/accounting/money.xls",
miguelgdbcfb122015-07-23 10:45:11337 L"file:///CONTOSO/accounting/money.xls"},
338 {"UNC File URI 2",
339 "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO",
palmer153af982015-09-15 02:04:19340 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html",
benwells2337b8102016-04-20 01:53:53341 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html",
miguelgdbcfb122015-07-23 10:45:11342 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html"},
benwells2337b8102016-04-20 01:53:53343 {"Invalid IPv6 address", "https://[2001:db8:0:1]/",
344 L"https://[2001:db8:0:1]", L"https://[2001:db8:0:1]",
345 L"https://[2001:db8:0:1]"},
miguelgdbcfb122015-07-23 10:45:11346 {"HTTP filesystem: URL with path",
347 "filesystem:http://www.google.com/temporary/test.html",
benwells2337b8102016-04-20 01:53:53348 L"filesystem:http://www.google.com", L"filesystem:http://www.google.com",
miguelgdbcfb122015-07-23 10:45:11349 L"filesystem:http://www.google.com"},
350 {"File filesystem: URL with path",
juncaicb63cac2016-05-13 00:41:59351 "filesystem:file://localhost/temporary/stuff/"
352 "test.html?z=fun&goat=billy",
palmer153af982015-09-15 02:04:19353 L"filesystem:file:///temporary/stuff/test.html",
benwells2337b8102016-04-20 01:53:53354 L"filesystem:file:///temporary/stuff/test.html",
miguelgdbcfb122015-07-23 10:45:11355 L"filesystem:file:///temporary/stuff/test.html"},
356 {"Invalid scheme 1", "twelve://www.cyber.org/wow.php",
benwells2337b8102016-04-20 01:53:53357 L"twelve://www.cyber.org/wow.php", L"twelve://www.cyber.org/wow.php",
358 L"twelve://www.cyber.org/wow.php"},
miguelgdbcfb122015-07-23 10:45:11359 {"Invalid scheme 2", "://www.cyber.org/wow.php",
benwells2337b8102016-04-20 01:53:53360 L"://www.cyber.org/wow.php", L"://www.cyber.org/wow.php",
361 L"://www.cyber.org/wow.php"},
miguelgdbcfb122015-07-23 10:45:11362 {"Invalid port 1", "https://173.194.65.103:000",
benwells2337b8102016-04-20 01:53:53363 L"https://173.194.65.103:0", L"173.194.65.103:0", L"173.194.65.103:0"},
miguelgdbcfb122015-07-23 10:45:11364 {"Invalid port 2", "https://173.194.65.103:gruffle",
benwells2337b8102016-04-20 01:53:53365 L"https://173.194.65.103:gruffle", L"https://173.194.65.103:gruffle",
366 L"https://173.194.65.103:gruffle"},
miguelgdbcfb122015-07-23 10:45:11367 {"Blob URL",
nick1b17dc32016-04-15 21:34:04368 "blob:http://www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
369 L"blob:http://www.html5rocks.com/"
palmer153af982015-09-15 02:04:19370 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
nick1b17dc32016-04-15 21:34:04371 L"blob:http://www.html5rocks.com/"
benwells2337b8102016-04-20 01:53:53372 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
373 L"blob:http://www.html5rocks.com/"
palmer153af982015-09-15 02:04:19374 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9"}};
miguelgdbcfb122015-07-23 10:45:11375
miguelgdbcfb122015-07-23 10:45:11376 for (size_t i = 0; i < arraysize(tests); ++i) {
jshin1fb76462016-04-05 22:13:03377 base::string16 formatted =
378 url_formatter::FormatUrlForSecurityDisplay(GURL(tests[i].input));
miguelgdbcfb122015-07-23 10:45:11379 EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted)
380 << tests[i].description;
palmer153af982015-09-15 02:04:19381
benwells2337b8102016-04-20 01:53:53382 base::string16 formatted_omit_web_scheme =
383 url_formatter::FormatUrlForSecurityDisplay(
384 GURL(tests[i].input),
385 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
386 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_web_scheme),
387 formatted_omit_web_scheme)
388 << tests[i].description;
389
390 base::string16 formatted_omit_cryptographic_scheme =
391 url_formatter::FormatUrlForSecurityDisplay(
392 GURL(tests[i].input),
393 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
394 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_cryptographic_scheme),
395 formatted_omit_cryptographic_scheme)
palmer153af982015-09-15 02:04:19396 << tests[i].description;
miguelgdbcfb122015-07-23 10:45:11397 }
398
juncaicb63cac2016-05-13 00:41:59399 base::string16 formatted = url_formatter::FormatUrlForSecurityDisplay(GURL());
miguelgdbcfb122015-07-23 10:45:11400 EXPECT_EQ(base::string16(), formatted)
401 << "Explicitly test the 0-argument GURL constructor";
palmer153af982015-09-15 02:04:19402
403 base::string16 formatted_omit_scheme =
benwells2337b8102016-04-20 01:53:53404 url_formatter::FormatUrlForSecurityDisplay(
405 GURL(), url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
406 EXPECT_EQ(base::string16(), formatted_omit_scheme)
407 << "Explicitly test the 0-argument GURL constructor";
408
409 formatted_omit_scheme = url_formatter::FormatUrlForSecurityDisplay(
410 GURL(), url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
palmer153af982015-09-15 02:04:19411 EXPECT_EQ(base::string16(), formatted_omit_scheme)
412 << "Explicitly test the 0-argument GURL constructor";
miguelgdbcfb122015-07-23 10:45:11413}
414
juncaicb63cac2016-05-13 00:41:59415TEST(TextEliderTest, FormatOriginForSecurityDisplay) {
416 for (size_t i = 0; i < arraysize(common_tests); ++i) {
417 base::string16 formatted = url_formatter::FormatOriginForSecurityDisplay(
418 url::Origin(GURL(common_tests[i].input)));
419 EXPECT_EQ(base::WideToUTF16(common_tests[i].output), formatted)
420 << common_tests[i].description;
421
422 base::string16 formatted_omit_web_scheme =
423 url_formatter::FormatOriginForSecurityDisplay(
424 url::Origin(GURL(common_tests[i].input)),
425 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
426 EXPECT_EQ(base::WideToUTF16(common_tests[i].output_omit_web_scheme),
427 formatted_omit_web_scheme)
428 << common_tests[i].description;
429
430 base::string16 formatted_omit_cryptographic_scheme =
431 url_formatter::FormatOriginForSecurityDisplay(
432 url::Origin(GURL(common_tests[i].input)),
433 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
434 EXPECT_EQ(
435 base::WideToUTF16(common_tests[i].output_omit_cryptographic_scheme),
436 formatted_omit_cryptographic_scheme)
437 << common_tests[i].description;
438 }
439
440 const OriginTestData tests[] = {
441 {"File URI", "file:///usr/example/file.html", L"file://", L"file://",
442 L"file://"},
443 {"File URI with hostname", "file://localhost/usr/example/file.html",
444 L"file://localhost", L"file://localhost", L"file://localhost"},
445 {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls", L"file://",
446 L"file://", L"file://"},
447 {"UNC File URI 2",
448 "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO",
449 L"file://", L"file://", L"file://"},
450 {"Invalid IPv6 address", "https://[2001:db8:0:1]/", L"", L"", L""},
451 {"HTTP filesystem: URL with path",
452 "filesystem:http://www.google.com/temporary/test.html",
453 L"http://www.google.com", L"www.google.com", L"http://www.google.com"},
454 {"File filesystem: URL with path",
455 "filesystem:file://localhost/temporary/stuff/test.html?z=fun&goat=billy",
456 L"file://", L"file://", L"file://"},
457 {"Invalid scheme 1", "twelve://www.cyber.org/wow.php", L"", L"", L""},
458 {"Invalid scheme 2", "://www.cyber.org/wow.php", L"", L"", L""},
459 {"Invalid port 1", "https://173.194.65.103:000", L"", L"", L""},
460 {"Invalid port 2", "https://173.194.65.103:gruffle", L"", L"", L""},
461 {"Blob URL",
462 "blob:http://www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
463 L"http://www.html5rocks.com", L"www.html5rocks.com",
464 L"http://www.html5rocks.com"}};
465
466 for (size_t i = 0; i < arraysize(tests); ++i) {
467 base::string16 formatted = url_formatter::FormatOriginForSecurityDisplay(
468 url::Origin(GURL(tests[i].input)));
469 EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted)
470 << tests[i].description;
471
472 base::string16 formatted_omit_web_scheme =
473 url_formatter::FormatOriginForSecurityDisplay(
474 url::Origin(GURL(tests[i].input)),
475 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
476 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_web_scheme),
477 formatted_omit_web_scheme)
478 << tests[i].description;
479
480 base::string16 formatted_omit_cryptographic_scheme =
481 url_formatter::FormatOriginForSecurityDisplay(
482 url::Origin(GURL(tests[i].input)),
483 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
484 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_cryptographic_scheme),
485 formatted_omit_cryptographic_scheme)
486 << tests[i].description;
487 }
488
489 base::string16 formatted =
490 url_formatter::FormatOriginForSecurityDisplay(url::Origin(GURL()));
491 EXPECT_EQ(base::string16(), formatted)
492 << "Explicitly test the url::Origin which takes an empty, invalid URL";
493
494 base::string16 formatted_omit_scheme =
495 url_formatter::FormatOriginForSecurityDisplay(
496 url::Origin(GURL()),
497 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
498 EXPECT_EQ(base::string16(), formatted_omit_scheme)
499 << "Explicitly test the url::Origin which takes an empty, invalid URL";
500
501 formatted_omit_scheme = url_formatter::FormatOriginForSecurityDisplay(
502 url::Origin(GURL()), url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
503 EXPECT_EQ(base::string16(), formatted_omit_scheme)
504 << "Explicitly test the url::Origin which takes an empty, invalid URL";
505}
506
miguelgdbcfb122015-07-23 10:45:11507} // namespace