miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 1 | // 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 | |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 5 | #include "components/url_formatter/elide_url.h" |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 6 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 9 | #include "base/macros.h" |
kulshin | ce612eb | 2016-06-29 18:46:51 | [diff] [blame] | 10 | #include "base/run_loop.h" |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 11 | #include "base/strings/utf_string_conversions.h" |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 12 | #include "build/build_config.h" |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 14 | #include "url/gurl.h" |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 15 | #include "url/origin.h" |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 16 | |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 17 | #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 |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 22 | |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | struct Testcase { |
| 26 | const std::string input; |
| 27 | const std::string output; |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | #if !defined(OS_ANDROID) |
| 31 | void 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); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 35 | const float available_width = |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 36 | gfx::GetStringWidthF(base::UTF8ToUTF16(testcases[i].output), font_list); |
| 37 | EXPECT_EQ(base::UTF8ToUTF16(testcases[i].output), |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 38 | url_formatter::ElideUrl(url, font_list, available_width)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
| 42 | // Test eliding of commonplace URLs. |
| 43 | TEST(TextEliderTest, TestGeneralEliding) { |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 44 | const std::string kEllipsisStr(gfx::kEllipsis); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 45 | 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. |
| 71 | TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) { |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 72 | const std::string kEllipsisStr(gfx::kEllipsis); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 73 | |
| 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"); |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 77 | float available_width = gfx::GetStringWidthF( |
| 78 | base::UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr), |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 79 | 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). |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 85 | ASSERT_GT( |
| 86 | gfx::GetStringWidthF(base::UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr), |
| 87 | font_list), |
| 88 | gfx::GetStringWidthF(base::UTF8ToUTF16("d" + kEllipsisStr), font_list)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 89 | GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc"); |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 90 | base::string16 expected = url_formatter::ElideUrl( |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 91 | long_url, font_list, available_width); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 92 | // Ensure that the expected result still contains part of the directory name. |
| 93 | ASSERT_GT(expected.length(), std::string("battersbox.com/d").length()); |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 94 | EXPECT_EQ(expected, url_formatter::ElideUrl(url, font_list, available_width)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 95 | |
| 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. |
| 105 | TEST(TextEliderTest, TestMoreEliding) { |
kulshin | ce612eb | 2016-06-29 18:46:51 | [diff] [blame] | 106 | #if defined(OS_WIN) |
| 107 | // Needed to bypass DCHECK in GetFallbackFont. |
| 108 | base::MessageLoopForUI message_loop; |
| 109 | #endif |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 110 | const std::string kEllipsisStr(gfx::kEllipsis); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 111 | 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. |
| 149 | TEST(TextEliderTest, TestFileURLEliding) { |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 150 | const std::string kEllipsisStr(gfx::kEllipsis); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 151 | 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 | |
| 178 | TEST(TextEliderTest, TestHostEliding) { |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 179 | const std::string kEllipsisStr(gfx::kEllipsis); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 180 | Testcase testcases[] = { |
pkl | 2affa91 | 2017-02-22 15:23:20 | [diff] [blame^] | 181 | {"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) |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | for (size_t i = 0; i < arraysize(testcases); ++i) { |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 204 | 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)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 209 | } |
| 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"), |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 214 | url_formatter::ElideHost(GURL("http://google.com"), gfx::FontList(), 2)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 215 | EXPECT_EQ(base::UTF8ToUTF16(kEllipsisStr + ".google.com"), |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 216 | url_formatter::ElideHost(GURL("http://subdomain.google.com"), |
| 217 | gfx::FontList(), 2)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 218 | EXPECT_EQ( |
| 219 | base::ASCIIToUTF16("foo.bar"), |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 220 | url_formatter::ElideHost(GURL("http://foo.bar"), gfx::FontList(), 2)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | #endif // !defined(OS_ANDROID) |
| 224 | |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 225 | struct 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() |
| 235 | const 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", |
jshin | 78809c4d8 | 2016-10-06 20:15:45 | [diff] [blame] | 247 | L"http://\x4e2d\x56fd.icom.museum", L"\x4e2d\x56fd.icom.museum", |
| 248 | L"http://\x4e2d\x56fd.icom.museum"}, |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 249 | {"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 | |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 307 | TEST(TextEliderTest, FormatUrlForSecurityDisplay) { |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 308 | 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 | } |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 331 | |
| 332 | const OriginTestData tests[] = { |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 333 | {"File URI", "file:///usr/example/file.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 334 | L"file:///usr/example/file.html", L"file:///usr/example/file.html", |
| 335 | L"file:///usr/example/file.html"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 336 | {"File URI with hostname", "file://localhost/usr/example/file.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 337 | L"file:///usr/example/file.html", L"file:///usr/example/file.html", |
| 338 | L"file:///usr/example/file.html"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 339 | {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls", |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 340 | L"file:///CONTOSO/accounting/money.xls", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 341 | L"file:///CONTOSO/accounting/money.xls", |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 342 | L"file:///CONTOSO/accounting/money.xls"}, |
| 343 | {"UNC File URI 2", |
| 344 | "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO", |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 345 | L"file:///C:/Program%20Files/Music/Web%20Sys/main.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 346 | L"file:///C:/Program%20Files/Music/Web%20Sys/main.html", |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 347 | L"file:///C:/Program%20Files/Music/Web%20Sys/main.html"}, |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 348 | {"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]"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 351 | {"HTTP filesystem: URL with path", |
| 352 | "filesystem:http://www.google.com/temporary/test.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 353 | L"filesystem:http://www.google.com", L"filesystem:http://www.google.com", |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 354 | L"filesystem:http://www.google.com"}, |
| 355 | {"File filesystem: URL with path", |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 356 | "filesystem:file://localhost/temporary/stuff/" |
| 357 | "test.html?z=fun&goat=billy", |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 358 | L"filesystem:file:///temporary/stuff/test.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 359 | L"filesystem:file:///temporary/stuff/test.html", |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 360 | L"filesystem:file:///temporary/stuff/test.html"}, |
| 361 | {"Invalid scheme 1", "twelve://www.cyber.org/wow.php", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 362 | L"twelve://www.cyber.org/wow.php", L"twelve://www.cyber.org/wow.php", |
| 363 | L"twelve://www.cyber.org/wow.php"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 364 | {"Invalid scheme 2", "://www.cyber.org/wow.php", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 365 | L"://www.cyber.org/wow.php", L"://www.cyber.org/wow.php", |
| 366 | L"://www.cyber.org/wow.php"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 367 | {"Invalid port 1", "https://173.194.65.103:000", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 368 | L"https://173.194.65.103:0", L"173.194.65.103:0", L"173.194.65.103:0"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 369 | {"Invalid port 2", "https://173.194.65.103:gruffle", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 370 | L"https://173.194.65.103:gruffle", L"https://173.194.65.103:gruffle", |
| 371 | L"https://173.194.65.103:gruffle"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 372 | {"Blob URL", |
nick | 1b17dc3 | 2016-04-15 21:34:04 | [diff] [blame] | 373 | "blob:http://www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9", |
| 374 | L"blob:http://www.html5rocks.com/" |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 375 | L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9", |
nick | 1b17dc3 | 2016-04-15 21:34:04 | [diff] [blame] | 376 | L"blob:http://www.html5rocks.com/" |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 377 | L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9", |
| 378 | L"blob:http://www.html5rocks.com/" |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 379 | L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9"}}; |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 380 | |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 381 | for (size_t i = 0; i < arraysize(tests); ++i) { |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 382 | base::string16 formatted = |
| 383 | url_formatter::FormatUrlForSecurityDisplay(GURL(tests[i].input)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 384 | EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted) |
| 385 | << tests[i].description; |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 386 | |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 387 | 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) |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 401 | << tests[i].description; |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 402 | } |
| 403 | |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 404 | base::string16 formatted = url_formatter::FormatUrlForSecurityDisplay(GURL()); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 405 | EXPECT_EQ(base::string16(), formatted) |
| 406 | << "Explicitly test the 0-argument GURL constructor"; |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 407 | |
| 408 | base::string16 formatted_omit_scheme = |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 409 | 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); |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 416 | EXPECT_EQ(base::string16(), formatted_omit_scheme) |
| 417 | << "Explicitly test the 0-argument GURL constructor"; |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 418 | } |
| 419 | |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 420 | TEST(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 | |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 512 | } // namespace |