blob: e92df4eabc475325f914f5797263086c57a9ebb2 [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
miguelgdbcfb122015-07-23 10:45:1123namespace {
24
25struct Testcase {
26 const std::string input;
27 const std::string output;
miguelgdbcfb122015-07-23 10:45:1128};
29
30#if !defined(OS_ANDROID)
31void RunUrlTest(Testcase* testcases, size_t num_testcases) {
32 static const gfx::FontList font_list;
33 for (size_t i = 0; i < num_testcases; ++i) {
34 const GURL url(testcases[i].input);
miguelgdbcfb122015-07-23 10:45:1135 const float available_width =
rsleevi24f64dc22015-08-07 21:39:2136 gfx::GetStringWidthF(base::UTF8ToUTF16(testcases[i].output), font_list);
37 EXPECT_EQ(base::UTF8ToUTF16(testcases[i].output),
jshin1fb76462016-04-05 22:13:0338 url_formatter::ElideUrl(url, font_list, available_width));
miguelgdbcfb122015-07-23 10:45:1139 }
40}
41
42// Test eliding of commonplace URLs.
43TEST(TextEliderTest, TestGeneralEliding) {
rsleevi24f64dc22015-08-07 21:39:2144 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:1145 Testcase testcases[] = {
46 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"},
47 {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"},
48 {"http://www.google.com/intl/en/ads/",
49 "google.com/intl/" + kEllipsisStr + "/ads/"},
50 {"http://www.google.com/intl/en/ads/",
51 "google.com/" + kEllipsisStr + "/ads/"},
52 {"http://www.google.com/intl/en/ads/", "google.com/" + kEllipsisStr},
53 {"http://www.google.com/intl/en/ads/", "goog" + kEllipsisStr},
54 {"https://subdomain.foo.com/bar/filename.html",
55 "subdomain.foo.com/bar/filename.html"},
56 {"https://subdomain.foo.com/bar/filename.html",
57 "subdomain.foo.com/" + kEllipsisStr + "/filename.html"},
58 {"http://subdomain.foo.com/bar/filename.html",
59 kEllipsisStr + "foo.com/" + kEllipsisStr + "/filename.html"},
60 {"http://www.google.com/intl/en/ads/?aLongQueryWhichIsNotRequired",
61 "www.google.com/intl/en/ads/?aLongQ" + kEllipsisStr},
62 };
63
64 RunUrlTest(testcases, arraysize(testcases));
65}
66
67// When there is very little space available, the elision code will shorten
68// both path AND file name to an ellipsis - ".../...". To avoid this result,
69// there is a hack in place that simply treats them as one string in this
70// case.
71TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) {
rsleevi24f64dc22015-08-07 21:39:2172 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:1173
74 // Very little space, would cause double ellipsis.
75 gfx::FontList font_list;
76 GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html");
rsleevi24f64dc22015-08-07 21:39:2177 float available_width = gfx::GetStringWidthF(
78 base::UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr),
miguelgdbcfb122015-07-23 10:45:1179 font_list);
80
81 // Create the expected string, after elision. Depending on font size, the
82 // directory might become /dir... or /di... or/d... - it never should be
83 // shorter than that. (If it is, the font considers d... to be longer
84 // than .../... - that should never happen).
rsleevi24f64dc22015-08-07 21:39:2185 ASSERT_GT(
86 gfx::GetStringWidthF(base::UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr),
87 font_list),
88 gfx::GetStringWidthF(base::UTF8ToUTF16("d" + kEllipsisStr), font_list));
miguelgdbcfb122015-07-23 10:45:1189 GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc");
rsleevi24f64dc22015-08-07 21:39:2190 base::string16 expected = url_formatter::ElideUrl(
jshin1fb76462016-04-05 22:13:0391 long_url, font_list, available_width);
miguelgdbcfb122015-07-23 10:45:1192 // Ensure that the expected result still contains part of the directory name.
93 ASSERT_GT(expected.length(), std::string("battersbox.com/d").length());
jshin1fb76462016-04-05 22:13:0394 EXPECT_EQ(expected, url_formatter::ElideUrl(url, font_list, available_width));
miguelgdbcfb122015-07-23 10:45:1195
96 // More space available - elide directories, partially elide filename.
97 Testcase testcases[] = {
98 {"http://battersbox.com/directory/foo/peter_paul_and_mary.html",
99 "battersbox.com/" + kEllipsisStr + "/peter" + kEllipsisStr},
100 };
101 RunUrlTest(testcases, arraysize(testcases));
102}
103
104// Test eliding of empty strings, URLs with ports, passwords, queries, etc.
105TEST(TextEliderTest, TestMoreEliding) {
kulshince612eb2016-06-29 18:46:51106#if defined(OS_WIN)
107 // Needed to bypass DCHECK in GetFallbackFont.
108 base::MessageLoopForUI message_loop;
109#endif
rsleevi24f64dc22015-08-07 21:39:21110 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:11111 Testcase testcases[] = {
112 {"http://www.google.com/foo?bar", "www.google.com/foo?bar"},
113 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo?" + kEllipsisStr},
114 {"http://xyz.google.com/foo?bar", "xyz.google.com/foo" + kEllipsisStr},
115 {"http://xyz.google.com/foo?bar", "xyz.google.com/fo" + kEllipsisStr},
116 {"http://a.b.com/pathname/c?d", "a.b.com/" + kEllipsisStr + "/c?d"},
117 {"", ""},
118 {"http://foo.bar..example.com...hello/test/filename.html",
119 "foo.bar..example.com...hello/" + kEllipsisStr + "/filename.html"},
120 {"http://foo.bar../", "foo.bar.."},
121 {"http://xn--1lq90i.cn/foo", "\xe5\x8c\x97\xe4\xba\xac.cn/foo"},
122 {"http://me:[email protected]:99/foo?bar#baz",
123 "secrethost.com:99/foo?bar#baz"},
124 {"http://me:mypass@ss%xxfdsf.com/foo", "ss%25xxfdsf.com/foo"},
125 {"mailto:[email protected]", "mailto:[email protected]"},
126 {"javascript:click(0)", "javascript:click(0)"},
127 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename",
128 "chess.eecs.berkeley.edu:4430/login/arbitfilename"},
129 {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename",
130 kEllipsisStr + "berkeley.edu:4430/" + kEllipsisStr + "/arbitfilename"},
131
132 // Unescaping.
133 {"http://www/%E4%BD%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0",
134 "www/\xe4\xbd\xa0\xe5\xa5\xbd?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"},
135
136 // Invalid unescaping for path. The ref will always be valid UTF-8. We
137 // don't
138 // bother to do too many edge cases, since these are handled by the
139 // escaper
140 // unittest.
141 {"http://www/%E4%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0",
142 "www/%E4%A0%E5%A5%BD?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"},
143 };
144
145 RunUrlTest(testcases, arraysize(testcases));
146}
147
148// Test eliding of file: URLs.
149TEST(TextEliderTest, TestFileURLEliding) {
rsleevi24f64dc22015-08-07 21:39:21150 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:11151 Testcase testcases[] = {
152 {"file:///C:/path1/path2/path3/filename",
153 "file:///C:/path1/path2/path3/filename"},
154 {"file:///C:/path1/path2/path3/filename", "C:/path1/path2/path3/filename"},
155// GURL parses "file:///C:path" differently on windows than it does on posix.
156#if defined(OS_WIN)
157 {"file:///C:path1/path2/path3/filename",
158 "C:/path1/path2/" + kEllipsisStr + "/filename"},
159 {"file:///C:path1/path2/path3/filename",
160 "C:/path1/" + kEllipsisStr + "/filename"},
161 {"file:///C:path1/path2/path3/filename",
162 "C:/" + kEllipsisStr + "/filename"},
163#endif // defined(OS_WIN)
164 {"file://filer/foo/bar/file", "filer/foo/bar/file"},
165 {"file://filer/foo/bar/file", "filer/foo/" + kEllipsisStr + "/file"},
166 {"file://filer/foo/bar/file", "filer/" + kEllipsisStr + "/file"},
167 {"file://filer/foo/", "file://filer/foo/"},
168 {"file://filer/foo/", "filer/foo/"},
169 {"file://filer/foo/", "filer" + kEllipsisStr},
170 // Eliding file URLs with nothing after the ':' shouldn't crash.
171 {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:", "aaa" + kEllipsisStr},
172 {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:/", "aaa" + kEllipsisStr},
173 };
174
175 RunUrlTest(testcases, arraysize(testcases));
176}
177
178TEST(TextEliderTest, TestHostEliding) {
rsleevi24f64dc22015-08-07 21:39:21179 const std::string kEllipsisStr(gfx::kEllipsis);
miguelgdbcfb122015-07-23 10:45:11180 Testcase testcases[] = {
pkl2affa912017-02-22 15:23:20181 {"http://google.com", "google.com"},
182 {"http://reallyreallyreallylongdomainname.com",
183 "reallyreallyreallylongdomainname.com"},
184 {"http://foo", "foo"},
185 {"http://foo.bar", "foo.bar"},
186#if !defined(OS_IOS)
187 // iOS width calculations are off by a letter from other platforms for
188 // strings with too many kerned letters on the default font set.
189 // TODO(rohitrao): Fix secure_display::ElideHost for iOS
190 // (crbug.com/517604).
191 {"http://subdomain.google.com", kEllipsisStr + ".google.com"},
192 {"http://a.b.c.d.e.f.com", kEllipsisStr + "f.com"},
193 {"http://subdomain.foo.bar", kEllipsisStr + "in.foo.bar"},
194 {"http://subdomain.reallylongdomainname.com",
195 kEllipsisStr + "ain.reallylongdomainname.com"},
196 {"http://a.b.c.d.e.f.com", kEllipsisStr + ".e.f.com"},
197 // IDN - Greek alpha.beta.gamma.delta.epsilon.zeta.com
198 {"http://xn--mxa.xn--nxa.xn--oxa.xn--pxa.xn--qxa.xn--rxa.com",
199 kEllipsisStr + ".\xCE\xB5.\xCE\xB6.com"},
200#endif // !defined(OS_IOS)
miguelgdbcfb122015-07-23 10:45:11201 };
202
203 for (size_t i = 0; i < arraysize(testcases); ++i) {
rsleevi24f64dc22015-08-07 21:39:21204 const float available_width = gfx::GetStringWidthF(
205 base::UTF8ToUTF16(testcases[i].output), gfx::FontList());
206 EXPECT_EQ(base::UTF8ToUTF16(testcases[i].output),
207 url_formatter::ElideHost(GURL(testcases[i].input),
208 gfx::FontList(), available_width));
miguelgdbcfb122015-07-23 10:45:11209 }
210
211 // Trying to elide to a really short length will still keep the full TLD+1
212 EXPECT_EQ(
213 base::ASCIIToUTF16("google.com"),
rsleevi24f64dc22015-08-07 21:39:21214 url_formatter::ElideHost(GURL("http://google.com"), gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11215 EXPECT_EQ(base::UTF8ToUTF16(kEllipsisStr + ".google.com"),
rsleevi24f64dc22015-08-07 21:39:21216 url_formatter::ElideHost(GURL("http://subdomain.google.com"),
217 gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11218 EXPECT_EQ(
219 base::ASCIIToUTF16("foo.bar"),
rsleevi24f64dc22015-08-07 21:39:21220 url_formatter::ElideHost(GURL("http://foo.bar"), gfx::FontList(), 2));
miguelgdbcfb122015-07-23 10:45:11221}
222
223#endif // !defined(OS_ANDROID)
224
juncaicb63cac2016-05-13 00:41:59225struct OriginTestData {
226 const char* const description;
227 const char* const input;
228 const wchar_t* const output;
229 const wchar_t* const output_omit_web_scheme;
230 const wchar_t* const output_omit_cryptographic_scheme;
231};
232
233// Common test data for both FormatUrlForSecurityDisplay() and
234// FormatOriginForSecurityDisplay()
235const OriginTestData common_tests[] = {
236 {"Empty URL", "", L"", L"", L""},
237 {"HTTP URL", "http://www.google.com/", L"http://www.google.com",
238 L"www.google.com", L"http://www.google.com"},
239 {"HTTPS URL", "https://www.google.com/", L"https://www.google.com",
240 L"www.google.com", L"www.google.com"},
241 {"Standard HTTP port", "http://www.google.com:80/",
242 L"http://www.google.com", L"www.google.com", L"http://www.google.com"},
243 {"Standard HTTPS port", "https://www.google.com:443/",
244 L"https://www.google.com", L"www.google.com", L"www.google.com"},
245 {"Standard HTTP port, IDN Chinese",
246 "http://\xe4\xb8\xad\xe5\x9b\xbd.icom.museum:80",
jshin78809c4d82016-10-06 20:15:45247 L"http://\x4e2d\x56fd.icom.museum", L"\x4e2d\x56fd.icom.museum",
248 L"http://\x4e2d\x56fd.icom.museum"},
juncaicb63cac2016-05-13 00:41:59249 {"HTTP URL, IDN Hebrew (RTL)",
250 "http://"
251 "\xd7\x90\xd7\x99\xd7\xa7\xd7\x95\xd7\xb4\xd7\x9d."
252 "\xd7\x99\xd7\xa9\xd7\xa8\xd7\x90\xd7\x9c.museum/",
253 L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum",
254 L"xn--4dbklr2c8d.xn--4dbrk0ce.museum",
255 L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum"},
256 {"HTTP URL with query string, IDN Arabic (RTL)",
257 "http://\xd9\x85\xd8\xb5\xd8\xb1.icom.museum/foo.html?yes=no",
258 L"http://xn--wgbh1c.icom.museum", L"xn--wgbh1c.icom.museum",
259 L"http://xn--wgbh1c.icom.museum"},
260 {"Non-standard HTTP port", "http://www.google.com:9000/",
261 L"http://www.google.com:9000", L"www.google.com:9000",
262 L"http://www.google.com:9000"},
263 {"Non-standard HTTPS port", "https://www.google.com:9000/",
264 L"https://www.google.com:9000", L"www.google.com:9000",
265 L"www.google.com:9000"},
266 {"HTTP URL with path", "http://www.google.com/test.html",
267 L"http://www.google.com", L"www.google.com", L"http://www.google.com"},
268 {"HTTPS URL with path", "https://www.google.com/test.html",
269 L"https://www.google.com", L"www.google.com", L"www.google.com"},
270 {"Unusual secure scheme (wss)", "wss://www.google.com/",
271 L"wss://www.google.com", L"wss://www.google.com", L"www.google.com"},
272 {"Unusual non-secure scheme (gopher)", "gopher://www.google.com/",
273 L"gopher://www.google.com", L"gopher://www.google.com",
274 L"gopher://www.google.com"},
275 {"Unlisted scheme (chrome)", "chrome://version", L"chrome://version",
276 L"chrome://version", L"chrome://version"},
277 {"HTTP IP address", "http://173.194.65.103", L"http://173.194.65.103",
278 L"173.194.65.103", L"http://173.194.65.103"},
279 {"HTTPS IP address", "https://173.194.65.103", L"https://173.194.65.103",
280 L"173.194.65.103", L"173.194.65.103"},
281 {"HTTP IPv6 address", "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/",
282 L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
283 L"http://[fe80::202:b3ff:fe1e:8329]"},
284 {"HTTPs IPv6 address", "https://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/",
285 L"https://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
286 L"[fe80::202:b3ff:fe1e:8329]"},
287 {"HTTP IPv6 address with port",
288 "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:80/",
289 L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
290 L"http://[fe80::202:b3ff:fe1e:8329]"},
291 {"HTTPs IPv6 address with port",
292 "https://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:443/",
293 L"https://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]",
294 L"[fe80::202:b3ff:fe1e:8329]"},
295 {"HTTPS IP address, non-default port", "https://173.194.65.103:8443",
296 L"https://173.194.65.103:8443", L"173.194.65.103:8443",
297 L"173.194.65.103:8443"},
298 {"Invalid host 1", "https://www.cyber../wow.php", L"https://www.cyber..",
299 L"www.cyber..", L"www.cyber.."},
300 {"Invalid host 2", "https://www...cyber/wow.php", L"https://www...cyber",
301 L"www...cyber", L"www...cyber"},
302 {"Invalid port 3", "https://173.194.65.103:/hello.aspx",
303 L"https://173.194.65.103", L"173.194.65.103", L"173.194.65.103"},
304 {"Trailing dot in DNS name", "https://www.example.com./get/goat",
305 L"https://www.example.com.", L"www.example.com.", L"www.example.com."}};
306
miguelgdbcfb122015-07-23 10:45:11307TEST(TextEliderTest, FormatUrlForSecurityDisplay) {
juncaicb63cac2016-05-13 00:41:59308 for (size_t i = 0; i < arraysize(common_tests); ++i) {
309 base::string16 formatted =
310 url_formatter::FormatUrlForSecurityDisplay(GURL(common_tests[i].input));
311 EXPECT_EQ(base::WideToUTF16(common_tests[i].output), formatted)
312 << common_tests[i].description;
313
314 base::string16 formatted_omit_web_scheme =
315 url_formatter::FormatUrlForSecurityDisplay(
316 GURL(common_tests[i].input),
317 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
318 EXPECT_EQ(base::WideToUTF16(common_tests[i].output_omit_web_scheme),
319 formatted_omit_web_scheme)
320 << common_tests[i].description;
321
322 base::string16 formatted_omit_cryptographic_scheme =
323 url_formatter::FormatUrlForSecurityDisplay(
324 GURL(common_tests[i].input),
325 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
326 EXPECT_EQ(
327 base::WideToUTF16(common_tests[i].output_omit_cryptographic_scheme),
328 formatted_omit_cryptographic_scheme)
329 << common_tests[i].description;
330 }
miguelgdbcfb122015-07-23 10:45:11331
332 const OriginTestData tests[] = {
miguelgdbcfb122015-07-23 10:45:11333 {"File URI", "file:///usr/example/file.html",
benwells2337b8102016-04-20 01:53:53334 L"file:///usr/example/file.html", L"file:///usr/example/file.html",
335 L"file:///usr/example/file.html"},
miguelgdbcfb122015-07-23 10:45:11336 {"File URI with hostname", "file://localhost/usr/example/file.html",
benwells2337b8102016-04-20 01:53:53337 L"file:///usr/example/file.html", L"file:///usr/example/file.html",
338 L"file:///usr/example/file.html"},
miguelgdbcfb122015-07-23 10:45:11339 {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls",
palmer153af982015-09-15 02:04:19340 L"file:///CONTOSO/accounting/money.xls",
benwells2337b8102016-04-20 01:53:53341 L"file:///CONTOSO/accounting/money.xls",
miguelgdbcfb122015-07-23 10:45:11342 L"file:///CONTOSO/accounting/money.xls"},
343 {"UNC File URI 2",
344 "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO",
palmer153af982015-09-15 02:04:19345 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html",
benwells2337b8102016-04-20 01:53:53346 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html",
miguelgdbcfb122015-07-23 10:45:11347 L"file:///C:/Program%20Files/Music/Web%20Sys/main.html"},
benwells2337b8102016-04-20 01:53:53348 {"Invalid IPv6 address", "https://[2001:db8:0:1]/",
349 L"https://[2001:db8:0:1]", L"https://[2001:db8:0:1]",
350 L"https://[2001:db8:0:1]"},
miguelgdbcfb122015-07-23 10:45:11351 {"HTTP filesystem: URL with path",
352 "filesystem:http://www.google.com/temporary/test.html",
benwells2337b8102016-04-20 01:53:53353 L"filesystem:http://www.google.com", L"filesystem:http://www.google.com",
miguelgdbcfb122015-07-23 10:45:11354 L"filesystem:http://www.google.com"},
355 {"File filesystem: URL with path",
juncaicb63cac2016-05-13 00:41:59356 "filesystem:file://localhost/temporary/stuff/"
357 "test.html?z=fun&goat=billy",
palmer153af982015-09-15 02:04:19358 L"filesystem:file:///temporary/stuff/test.html",
benwells2337b8102016-04-20 01:53:53359 L"filesystem:file:///temporary/stuff/test.html",
miguelgdbcfb122015-07-23 10:45:11360 L"filesystem:file:///temporary/stuff/test.html"},
361 {"Invalid scheme 1", "twelve://www.cyber.org/wow.php",
benwells2337b8102016-04-20 01:53:53362 L"twelve://www.cyber.org/wow.php", L"twelve://www.cyber.org/wow.php",
363 L"twelve://www.cyber.org/wow.php"},
miguelgdbcfb122015-07-23 10:45:11364 {"Invalid scheme 2", "://www.cyber.org/wow.php",
benwells2337b8102016-04-20 01:53:53365 L"://www.cyber.org/wow.php", L"://www.cyber.org/wow.php",
366 L"://www.cyber.org/wow.php"},
miguelgdbcfb122015-07-23 10:45:11367 {"Invalid port 1", "https://173.194.65.103:000",
benwells2337b8102016-04-20 01:53:53368 L"https://173.194.65.103:0", L"173.194.65.103:0", L"173.194.65.103:0"},
miguelgdbcfb122015-07-23 10:45:11369 {"Invalid port 2", "https://173.194.65.103:gruffle",
benwells2337b8102016-04-20 01:53:53370 L"https://173.194.65.103:gruffle", L"https://173.194.65.103:gruffle",
371 L"https://173.194.65.103:gruffle"},
miguelgdbcfb122015-07-23 10:45:11372 {"Blob URL",
nick1b17dc32016-04-15 21:34:04373 "blob:http://www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
374 L"blob:http://www.html5rocks.com/"
palmer153af982015-09-15 02:04:19375 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
nick1b17dc32016-04-15 21:34:04376 L"blob:http://www.html5rocks.com/"
benwells2337b8102016-04-20 01:53:53377 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
378 L"blob:http://www.html5rocks.com/"
palmer153af982015-09-15 02:04:19379 L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9"}};
miguelgdbcfb122015-07-23 10:45:11380
miguelgdbcfb122015-07-23 10:45:11381 for (size_t i = 0; i < arraysize(tests); ++i) {
jshin1fb76462016-04-05 22:13:03382 base::string16 formatted =
383 url_formatter::FormatUrlForSecurityDisplay(GURL(tests[i].input));
miguelgdbcfb122015-07-23 10:45:11384 EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted)
385 << tests[i].description;
palmer153af982015-09-15 02:04:19386
benwells2337b8102016-04-20 01:53:53387 base::string16 formatted_omit_web_scheme =
388 url_formatter::FormatUrlForSecurityDisplay(
389 GURL(tests[i].input),
390 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
391 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_web_scheme),
392 formatted_omit_web_scheme)
393 << tests[i].description;
394
395 base::string16 formatted_omit_cryptographic_scheme =
396 url_formatter::FormatUrlForSecurityDisplay(
397 GURL(tests[i].input),
398 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
399 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_cryptographic_scheme),
400 formatted_omit_cryptographic_scheme)
palmer153af982015-09-15 02:04:19401 << tests[i].description;
miguelgdbcfb122015-07-23 10:45:11402 }
403
juncaicb63cac2016-05-13 00:41:59404 base::string16 formatted = url_formatter::FormatUrlForSecurityDisplay(GURL());
miguelgdbcfb122015-07-23 10:45:11405 EXPECT_EQ(base::string16(), formatted)
406 << "Explicitly test the 0-argument GURL constructor";
palmer153af982015-09-15 02:04:19407
408 base::string16 formatted_omit_scheme =
benwells2337b8102016-04-20 01:53:53409 url_formatter::FormatUrlForSecurityDisplay(
410 GURL(), url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
411 EXPECT_EQ(base::string16(), formatted_omit_scheme)
412 << "Explicitly test the 0-argument GURL constructor";
413
414 formatted_omit_scheme = url_formatter::FormatUrlForSecurityDisplay(
415 GURL(), url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
palmer153af982015-09-15 02:04:19416 EXPECT_EQ(base::string16(), formatted_omit_scheme)
417 << "Explicitly test the 0-argument GURL constructor";
miguelgdbcfb122015-07-23 10:45:11418}
419
juncaicb63cac2016-05-13 00:41:59420TEST(TextEliderTest, FormatOriginForSecurityDisplay) {
421 for (size_t i = 0; i < arraysize(common_tests); ++i) {
422 base::string16 formatted = url_formatter::FormatOriginForSecurityDisplay(
423 url::Origin(GURL(common_tests[i].input)));
424 EXPECT_EQ(base::WideToUTF16(common_tests[i].output), formatted)
425 << common_tests[i].description;
426
427 base::string16 formatted_omit_web_scheme =
428 url_formatter::FormatOriginForSecurityDisplay(
429 url::Origin(GURL(common_tests[i].input)),
430 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
431 EXPECT_EQ(base::WideToUTF16(common_tests[i].output_omit_web_scheme),
432 formatted_omit_web_scheme)
433 << common_tests[i].description;
434
435 base::string16 formatted_omit_cryptographic_scheme =
436 url_formatter::FormatOriginForSecurityDisplay(
437 url::Origin(GURL(common_tests[i].input)),
438 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
439 EXPECT_EQ(
440 base::WideToUTF16(common_tests[i].output_omit_cryptographic_scheme),
441 formatted_omit_cryptographic_scheme)
442 << common_tests[i].description;
443 }
444
445 const OriginTestData tests[] = {
446 {"File URI", "file:///usr/example/file.html", L"file://", L"file://",
447 L"file://"},
448 {"File URI with hostname", "file://localhost/usr/example/file.html",
449 L"file://localhost", L"file://localhost", L"file://localhost"},
450 {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls", L"file://",
451 L"file://", L"file://"},
452 {"UNC File URI 2",
453 "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO",
454 L"file://", L"file://", L"file://"},
455 {"Invalid IPv6 address", "https://[2001:db8:0:1]/", L"", L"", L""},
456 {"HTTP filesystem: URL with path",
457 "filesystem:http://www.google.com/temporary/test.html",
458 L"http://www.google.com", L"www.google.com", L"http://www.google.com"},
459 {"File filesystem: URL with path",
460 "filesystem:file://localhost/temporary/stuff/test.html?z=fun&goat=billy",
461 L"file://", L"file://", L"file://"},
462 {"Invalid scheme 1", "twelve://www.cyber.org/wow.php", L"", L"", L""},
463 {"Invalid scheme 2", "://www.cyber.org/wow.php", L"", L"", L""},
464 {"Invalid port 1", "https://173.194.65.103:000", L"", L"", L""},
465 {"Invalid port 2", "https://173.194.65.103:gruffle", L"", L"", L""},
466 {"Blob URL",
467 "blob:http://www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9",
468 L"http://www.html5rocks.com", L"www.html5rocks.com",
469 L"http://www.html5rocks.com"}};
470
471 for (size_t i = 0; i < arraysize(tests); ++i) {
472 base::string16 formatted = url_formatter::FormatOriginForSecurityDisplay(
473 url::Origin(GURL(tests[i].input)));
474 EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted)
475 << tests[i].description;
476
477 base::string16 formatted_omit_web_scheme =
478 url_formatter::FormatOriginForSecurityDisplay(
479 url::Origin(GURL(tests[i].input)),
480 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
481 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_web_scheme),
482 formatted_omit_web_scheme)
483 << tests[i].description;
484
485 base::string16 formatted_omit_cryptographic_scheme =
486 url_formatter::FormatOriginForSecurityDisplay(
487 url::Origin(GURL(tests[i].input)),
488 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
489 EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_cryptographic_scheme),
490 formatted_omit_cryptographic_scheme)
491 << tests[i].description;
492 }
493
494 base::string16 formatted =
495 url_formatter::FormatOriginForSecurityDisplay(url::Origin(GURL()));
496 EXPECT_EQ(base::string16(), formatted)
497 << "Explicitly test the url::Origin which takes an empty, invalid URL";
498
499 base::string16 formatted_omit_scheme =
500 url_formatter::FormatOriginForSecurityDisplay(
501 url::Origin(GURL()),
502 url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
503 EXPECT_EQ(base::string16(), formatted_omit_scheme)
504 << "Explicitly test the url::Origin which takes an empty, invalid URL";
505
506 formatted_omit_scheme = url_formatter::FormatOriginForSecurityDisplay(
507 url::Origin(GURL()), url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC);
508 EXPECT_EQ(base::string16(), formatted_omit_scheme)
509 << "Explicitly test the url::Origin which takes an empty, invalid URL";
510}
511
miguelgdbcfb122015-07-23 10:45:11512} // namespace