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 | |
justincohen | b96612bf | 2015-07-24 22:26:09 | [diff] [blame] | 9 | #include "base/ios/ios_util.h" |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 10 | #include "base/macros.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" |
| 15 | |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 16 | #if !defined(OS_ANDROID) |
| 17 | #include "ui/gfx/font_list.h" // nogncheck |
| 18 | #include "ui/gfx/text_elider.h" // nogncheck |
| 19 | #include "ui/gfx/text_utils.h" // nogncheck |
| 20 | #endif |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 21 | |
| 22 | namespace { |
| 23 | |
| 24 | struct Testcase { |
| 25 | const std::string input; |
| 26 | const std::string output; |
eugenebut | bec2c5f | 2015-08-07 23:22:32 | [diff] [blame] | 27 | enum SupportedPlatforms { |
| 28 | ALL = 0, |
| 29 | NO_IOS9_OR_LATER, |
| 30 | NO_IOS, |
| 31 | } platforms; |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | #if !defined(OS_ANDROID) |
| 35 | void RunUrlTest(Testcase* testcases, size_t num_testcases) { |
| 36 | static const gfx::FontList font_list; |
| 37 | for (size_t i = 0; i < num_testcases; ++i) { |
| 38 | const GURL url(testcases[i].input); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 39 | const float available_width = |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 40 | gfx::GetStringWidthF(base::UTF8ToUTF16(testcases[i].output), font_list); |
| 41 | EXPECT_EQ(base::UTF8ToUTF16(testcases[i].output), |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 42 | url_formatter::ElideUrl(url, font_list, available_width)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
| 46 | // Test eliding of commonplace URLs. |
| 47 | TEST(TextEliderTest, TestGeneralEliding) { |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 48 | const std::string kEllipsisStr(gfx::kEllipsis); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 49 | Testcase testcases[] = { |
| 50 | {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"}, |
| 51 | {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"}, |
| 52 | {"http://www.google.com/intl/en/ads/", |
| 53 | "google.com/intl/" + kEllipsisStr + "/ads/"}, |
| 54 | {"http://www.google.com/intl/en/ads/", |
| 55 | "google.com/" + kEllipsisStr + "/ads/"}, |
| 56 | {"http://www.google.com/intl/en/ads/", "google.com/" + kEllipsisStr}, |
| 57 | {"http://www.google.com/intl/en/ads/", "goog" + kEllipsisStr}, |
| 58 | {"https://subdomain.foo.com/bar/filename.html", |
| 59 | "subdomain.foo.com/bar/filename.html"}, |
| 60 | {"https://subdomain.foo.com/bar/filename.html", |
| 61 | "subdomain.foo.com/" + kEllipsisStr + "/filename.html"}, |
| 62 | {"http://subdomain.foo.com/bar/filename.html", |
| 63 | kEllipsisStr + "foo.com/" + kEllipsisStr + "/filename.html"}, |
| 64 | {"http://www.google.com/intl/en/ads/?aLongQueryWhichIsNotRequired", |
| 65 | "www.google.com/intl/en/ads/?aLongQ" + kEllipsisStr}, |
| 66 | }; |
| 67 | |
| 68 | RunUrlTest(testcases, arraysize(testcases)); |
| 69 | } |
| 70 | |
| 71 | // When there is very little space available, the elision code will shorten |
| 72 | // both path AND file name to an ellipsis - ".../...". To avoid this result, |
| 73 | // there is a hack in place that simply treats them as one string in this |
| 74 | // case. |
| 75 | TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) { |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 76 | const std::string kEllipsisStr(gfx::kEllipsis); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 77 | |
| 78 | // Very little space, would cause double ellipsis. |
| 79 | gfx::FontList font_list; |
| 80 | GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html"); |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 81 | float available_width = gfx::GetStringWidthF( |
| 82 | base::UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr), |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 83 | font_list); |
| 84 | |
| 85 | // Create the expected string, after elision. Depending on font size, the |
| 86 | // directory might become /dir... or /di... or/d... - it never should be |
| 87 | // shorter than that. (If it is, the font considers d... to be longer |
| 88 | // than .../... - that should never happen). |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 89 | ASSERT_GT( |
| 90 | gfx::GetStringWidthF(base::UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr), |
| 91 | font_list), |
| 92 | gfx::GetStringWidthF(base::UTF8ToUTF16("d" + kEllipsisStr), font_list)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 93 | GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc"); |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 94 | base::string16 expected = url_formatter::ElideUrl( |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 95 | long_url, font_list, available_width); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 96 | // Ensure that the expected result still contains part of the directory name. |
| 97 | ASSERT_GT(expected.length(), std::string("battersbox.com/d").length()); |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 98 | EXPECT_EQ(expected, url_formatter::ElideUrl(url, font_list, available_width)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 99 | |
| 100 | // More space available - elide directories, partially elide filename. |
| 101 | Testcase testcases[] = { |
| 102 | {"http://battersbox.com/directory/foo/peter_paul_and_mary.html", |
| 103 | "battersbox.com/" + kEllipsisStr + "/peter" + kEllipsisStr}, |
| 104 | }; |
| 105 | RunUrlTest(testcases, arraysize(testcases)); |
| 106 | } |
| 107 | |
| 108 | // Test eliding of empty strings, URLs with ports, passwords, queries, etc. |
| 109 | TEST(TextEliderTest, TestMoreEliding) { |
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[] = { |
eugenebut | bec2c5f | 2015-08-07 23:22:32 | [diff] [blame] | 181 | {"http://google.com", "google.com"}, |
| 182 | // iOS width calculations are off by a letter from other platforms for |
| 183 | // strings with too many kerned letters on the default font set. |
| 184 | // TODO(rohitrao): Fix secure_display::ElideHost for iOS |
| 185 | // (crbug.com/517604). |
| 186 | {"http://subdomain.google.com", kEllipsisStr + ".google.com", |
| 187 | Testcase::NO_IOS9_OR_LATER}, |
| 188 | {"http://reallyreallyreallylongdomainname.com", |
| 189 | "reallyreallyreallylongdomainname.com"}, |
| 190 | {"http://a.b.c.d.e.f.com", kEllipsisStr + "f.com", |
| 191 | Testcase::NO_IOS9_OR_LATER}, |
| 192 | {"http://foo", "foo"}, |
| 193 | {"http://foo.bar", "foo.bar"}, |
| 194 | {"http://subdomain.foo.bar", kEllipsisStr + "in.foo.bar", |
| 195 | Testcase::NO_IOS9_OR_LATER}, |
| 196 | {"http://subdomain.reallylongdomainname.com", |
| 197 | kEllipsisStr + "ain.reallylongdomainname.com", Testcase::NO_IOS}, |
| 198 | {"http://a.b.c.d.e.f.com", kEllipsisStr + ".e.f.com", Testcase::NO_IOS}, |
jshin | ee46c77d | 2016-04-07 00:16:42 | [diff] [blame] | 199 | // IDN - Greek alpha.beta.gamma.delta.epsilon.zeta.com |
| 200 | {"http://xn--mxa.xn--nxa.xn--oxa.xn--pxa.xn--qxa.xn--rxa.com", |
| 201 | kEllipsisStr + ".\xCE\xB5.\xCE\xB6.com", Testcase::NO_IOS}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | for (size_t i = 0; i < arraysize(testcases); ++i) { |
eugenebut | bec2c5f | 2015-08-07 23:22:32 | [diff] [blame] | 205 | #if defined(OS_IOS) |
| 206 | if (testcases[i].platforms == Testcase::NO_IOS || |
| 207 | (testcases[i].platforms == Testcase::NO_IOS9_OR_LATER && |
| 208 | base::ios::IsRunningOnIOS9OrLater())) { |
| 209 | continue; |
| 210 | } |
| 211 | #endif |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 212 | const float available_width = gfx::GetStringWidthF( |
| 213 | base::UTF8ToUTF16(testcases[i].output), gfx::FontList()); |
| 214 | EXPECT_EQ(base::UTF8ToUTF16(testcases[i].output), |
| 215 | url_formatter::ElideHost(GURL(testcases[i].input), |
| 216 | gfx::FontList(), available_width)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // Trying to elide to a really short length will still keep the full TLD+1 |
| 220 | EXPECT_EQ( |
| 221 | base::ASCIIToUTF16("google.com"), |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 222 | url_formatter::ElideHost(GURL("http://google.com"), gfx::FontList(), 2)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 223 | EXPECT_EQ(base::UTF8ToUTF16(kEllipsisStr + ".google.com"), |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 224 | url_formatter::ElideHost(GURL("http://subdomain.google.com"), |
| 225 | gfx::FontList(), 2)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 226 | EXPECT_EQ( |
| 227 | base::ASCIIToUTF16("foo.bar"), |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 228 | url_formatter::ElideHost(GURL("http://foo.bar"), gfx::FontList(), 2)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | #endif // !defined(OS_ANDROID) |
| 232 | |
| 233 | TEST(TextEliderTest, FormatUrlForSecurityDisplay) { |
| 234 | struct OriginTestData { |
| 235 | const char* const description; |
| 236 | const char* const input; |
| 237 | const wchar_t* const output; |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 238 | const wchar_t* const output_omit_web_scheme; |
| 239 | const wchar_t* const output_omit_cryptographic_scheme; |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | const OriginTestData tests[] = { |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 243 | {"Empty URL", "", L"", L"", L""}, |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 244 | {"HTTP URL", "http://www.google.com/", L"http://www.google.com", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 245 | L"www.google.com", L"http://www.google.com"}, |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 246 | {"HTTPS URL", "https://www.google.com/", L"https://www.google.com", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 247 | L"www.google.com", L"www.google.com"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 248 | {"Standard HTTP port", "http://www.google.com:80/", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 249 | L"http://www.google.com", L"www.google.com", L"http://www.google.com"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 250 | {"Standard HTTPS port", "https://www.google.com:443/", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 251 | L"https://www.google.com", L"www.google.com", L"www.google.com"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 252 | {"Standard HTTP port, IDN Chinese", |
| 253 | "http://\xe4\xb8\xad\xe5\x9b\xbd.icom.museum:80", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 254 | L"http://xn--fiqs8s.icom.museum", L"xn--fiqs8s.icom.museum", |
| 255 | L"http://xn--fiqs8s.icom.museum"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 256 | {"HTTP URL, IDN Hebrew (RTL)", |
| 257 | "http://" |
| 258 | "\xd7\x90\xd7\x99\xd7\xa7\xd7\x95\xd7\xb4\xd7\x9d." |
| 259 | "\xd7\x99\xd7\xa9\xd7\xa8\xd7\x90\xd7\x9c.museum/", |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 260 | L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 261 | L"xn--4dbklr2c8d.xn--4dbrk0ce.museum", |
| 262 | L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 263 | {"HTTP URL with query string, IDN Arabic (RTL)", |
| 264 | "http://\xd9\x85\xd8\xb5\xd8\xb1.icom.museum/foo.html?yes=no", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 265 | L"http://xn--wgbh1c.icom.museum", L"xn--wgbh1c.icom.museum", |
| 266 | L"http://xn--wgbh1c.icom.museum"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 267 | {"Non-standard HTTP port", "http://www.google.com:9000/", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 268 | L"http://www.google.com:9000", L"www.google.com:9000", |
| 269 | L"http://www.google.com:9000"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 270 | {"Non-standard HTTPS port", "https://www.google.com:9000/", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 271 | L"https://www.google.com:9000", L"www.google.com:9000", |
| 272 | L"www.google.com:9000"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 273 | {"File URI", "file:///usr/example/file.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 274 | L"file:///usr/example/file.html", L"file:///usr/example/file.html", |
| 275 | L"file:///usr/example/file.html"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 276 | {"File URI with hostname", "file://localhost/usr/example/file.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 277 | L"file:///usr/example/file.html", L"file:///usr/example/file.html", |
| 278 | L"file:///usr/example/file.html"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 279 | {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls", |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 280 | L"file:///CONTOSO/accounting/money.xls", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 281 | L"file:///CONTOSO/accounting/money.xls", |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 282 | L"file:///CONTOSO/accounting/money.xls"}, |
| 283 | {"UNC File URI 2", |
| 284 | "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO", |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 285 | L"file:///C:/Program%20Files/Music/Web%20Sys/main.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 286 | L"file:///C:/Program%20Files/Music/Web%20Sys/main.html", |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 287 | L"file:///C:/Program%20Files/Music/Web%20Sys/main.html"}, |
| 288 | {"HTTP URL with path", "http://www.google.com/test.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 289 | L"http://www.google.com", L"www.google.com", L"http://www.google.com"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 290 | {"HTTPS URL with path", "https://www.google.com/test.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 291 | L"https://www.google.com", L"www.google.com", L"www.google.com"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 292 | {"Unusual secure scheme (wss)", "wss://www.google.com/", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 293 | L"wss://www.google.com", L"wss://www.google.com", L"www.google.com"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 294 | {"Unusual non-secure scheme (gopher)", "gopher://www.google.com/", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 295 | L"gopher://www.google.com", L"gopher://www.google.com", |
| 296 | L"gopher://www.google.com"}, |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 297 | {"Unlisted scheme (chrome)", "chrome://version", L"chrome://version", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 298 | L"chrome://version", L"chrome://version"}, |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 299 | {"HTTP IP address", "http://173.194.65.103", L"http://173.194.65.103", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 300 | L"173.194.65.103", L"http://173.194.65.103"}, |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 301 | {"HTTPS IP address", "https://173.194.65.103", L"https://173.194.65.103", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 302 | L"173.194.65.103", L"173.194.65.103"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 303 | {"HTTP IPv6 address", "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 304 | L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]", |
| 305 | L"http://[fe80::202:b3ff:fe1e:8329]"}, |
| 306 | {"HTTPs IPv6 address", |
| 307 | "https://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/", |
| 308 | L"https://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]", |
| 309 | L"[fe80::202:b3ff:fe1e:8329]"}, |
| 310 | {"HTTP IPv6 address with port", |
| 311 | "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:80/", |
| 312 | L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]", |
| 313 | L"http://[fe80::202:b3ff:fe1e:8329]"}, |
| 314 | {"HTTPs IPv6 address with port", |
| 315 | "https://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:443/", |
| 316 | L"https://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]", |
| 317 | L"[fe80::202:b3ff:fe1e:8329]"}, |
| 318 | {"Invalid IPv6 address", "https://[2001:db8:0:1]/", |
| 319 | L"https://[2001:db8:0:1]", L"https://[2001:db8:0:1]", |
| 320 | L"https://[2001:db8:0:1]"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 321 | {"HTTPS IP address, non-default port", "https://173.194.65.103:8443", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 322 | L"https://173.194.65.103:8443", L"173.194.65.103:8443", |
| 323 | L"173.194.65.103:8443"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 324 | {"HTTP filesystem: URL with path", |
| 325 | "filesystem:http://www.google.com/temporary/test.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 326 | L"filesystem:http://www.google.com", L"filesystem:http://www.google.com", |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 327 | L"filesystem:http://www.google.com"}, |
| 328 | {"File filesystem: URL with path", |
| 329 | "filesystem:file://localhost/temporary/stuff/test.html?z=fun&goat=billy", |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 330 | L"filesystem:file:///temporary/stuff/test.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 331 | L"filesystem:file:///temporary/stuff/test.html", |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 332 | L"filesystem:file:///temporary/stuff/test.html"}, |
| 333 | {"Invalid scheme 1", "twelve://www.cyber.org/wow.php", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 334 | L"twelve://www.cyber.org/wow.php", L"twelve://www.cyber.org/wow.php", |
| 335 | L"twelve://www.cyber.org/wow.php"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 336 | {"Invalid scheme 2", "://www.cyber.org/wow.php", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 337 | L"://www.cyber.org/wow.php", L"://www.cyber.org/wow.php", |
| 338 | L"://www.cyber.org/wow.php"}, |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 339 | {"Invalid host 1", "https://www.cyber../wow.php", L"https://www.cyber..", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 340 | L"www.cyber..", L"www.cyber.."}, |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 341 | {"Invalid host 2", "https://www...cyber/wow.php", L"https://www...cyber", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 342 | L"www...cyber", L"www...cyber"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 343 | {"Invalid port 1", "https://173.194.65.103:000", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 344 | 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] | 345 | {"Invalid port 2", "https://173.194.65.103:gruffle", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 346 | L"https://173.194.65.103:gruffle", L"https://173.194.65.103:gruffle", |
| 347 | L"https://173.194.65.103:gruffle"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 348 | {"Invalid port 3", "https://173.194.65.103:/hello.aspx", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 349 | L"https://173.194.65.103", L"173.194.65.103", L"173.194.65.103"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 350 | {"Trailing dot in DNS name", "https://www.example.com./get/goat", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 351 | L"https://www.example.com.", L"www.example.com.", L"www.example.com."}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 352 | {"Blob URL", |
nick | 1b17dc3 | 2016-04-15 21:34:04 | [diff] [blame] | 353 | "blob:http://www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9", |
| 354 | L"blob:http://www.html5rocks.com/" |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 355 | L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9", |
nick | 1b17dc3 | 2016-04-15 21:34:04 | [diff] [blame] | 356 | L"blob:http://www.html5rocks.com/" |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 357 | L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9", |
| 358 | L"blob:http://www.html5rocks.com/" |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 359 | L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9"}}; |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 360 | |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 361 | for (size_t i = 0; i < arraysize(tests); ++i) { |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 362 | base::string16 formatted = |
| 363 | url_formatter::FormatUrlForSecurityDisplay(GURL(tests[i].input)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 364 | EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted) |
| 365 | << tests[i].description; |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 366 | |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 367 | base::string16 formatted_omit_web_scheme = |
| 368 | url_formatter::FormatUrlForSecurityDisplay( |
| 369 | GURL(tests[i].input), |
| 370 | url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS); |
| 371 | EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_web_scheme), |
| 372 | formatted_omit_web_scheme) |
| 373 | << tests[i].description; |
| 374 | |
| 375 | base::string16 formatted_omit_cryptographic_scheme = |
| 376 | url_formatter::FormatUrlForSecurityDisplay( |
| 377 | GURL(tests[i].input), |
| 378 | url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC); |
| 379 | EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_cryptographic_scheme), |
| 380 | formatted_omit_cryptographic_scheme) |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 381 | << tests[i].description; |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | base::string16 formatted = |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 385 | url_formatter::FormatUrlForSecurityDisplay(GURL()); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 386 | EXPECT_EQ(base::string16(), formatted) |
| 387 | << "Explicitly test the 0-argument GURL constructor"; |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 388 | |
| 389 | base::string16 formatted_omit_scheme = |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame^] | 390 | url_formatter::FormatUrlForSecurityDisplay( |
| 391 | GURL(), url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS); |
| 392 | EXPECT_EQ(base::string16(), formatted_omit_scheme) |
| 393 | << "Explicitly test the 0-argument GURL constructor"; |
| 394 | |
| 395 | formatted_omit_scheme = url_formatter::FormatUrlForSecurityDisplay( |
| 396 | GURL(), url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC); |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 397 | EXPECT_EQ(base::string16(), formatted_omit_scheme) |
| 398 | << "Explicitly test the 0-argument GURL constructor"; |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | } // namespace |