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" |
gab | f64a25e | 2017-05-12 19:42:56 | [diff] [blame] | 10 | #include "base/message_loop/message_loop.h" |
kulshin | ce612eb | 2016-06-29 18:46:51 | [diff] [blame] | 11 | #include "base/run_loop.h" |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 13 | #include "build/build_config.h" |
Christopher Grant | 6871e121 | 2017-10-09 15:47:50 | [diff] [blame] | 14 | #include "components/url_formatter/url_formatter.h" |
| 15 | #include "net/base/escape.h" |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
Christopher Grant | 6871e121 | 2017-10-09 15:47:50 | [diff] [blame] | 17 | #include "ui/gfx/font_list.h" |
| 18 | #include "ui/gfx/text_elider.h" |
| 19 | #include "ui/gfx/text_utils.h" |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 20 | #include "url/gurl.h" |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 21 | #include "url/origin.h" |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 22 | |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 23 | namespace { |
| 24 | |
Christopher Grant | 6871e121 | 2017-10-09 15:47:50 | [diff] [blame] | 25 | enum ElisionMethod { |
| 26 | kMethodOriginal, |
| 27 | kMethodSimple, |
| 28 | }; |
| 29 | |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 30 | struct Testcase { |
| 31 | const std::string input; |
| 32 | const std::string output; |
| 33 | }; |
| 34 | |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 35 | struct ProgressiveTestcase { |
| 36 | const std::string input; |
| 37 | const std::vector<std::string> output; |
| 38 | }; |
| 39 | |
Christopher Grant | 6871e121 | 2017-10-09 15:47:50 | [diff] [blame] | 40 | struct UrlComponent { |
| 41 | url::Parsed::ComponentType type; |
| 42 | int begin; |
| 43 | int len; |
| 44 | }; |
| 45 | |
| 46 | struct ParsingTestcase { |
| 47 | const std::string input; |
| 48 | const std::string output; |
| 49 | const std::vector<UrlComponent> components; |
| 50 | }; |
| 51 | |
Christopher Grant | bd541e5 | 2017-10-13 20:59:41 | [diff] [blame] | 52 | base::string16 FormatAndElideUrlSimple(const GURL& url, |
| 53 | const gfx::FontList& font_list, |
| 54 | float available_pixel_width, |
| 55 | url::Parsed* parsed) { |
| 56 | const base::string16 url_string = url_formatter::FormatUrl( |
| 57 | url, url_formatter::kFormatUrlOmitDefaults, net::UnescapeRule::SPACES, |
| 58 | parsed, nullptr, nullptr); |
| 59 | return url_formatter::ElideUrlSimple(url, url_string, font_list, |
| 60 | available_pixel_width, parsed); |
| 61 | } |
| 62 | |
Christopher Grant | 6871e121 | 2017-10-09 15:47:50 | [diff] [blame] | 63 | base::string16 Elide(const GURL& url, |
| 64 | const gfx::FontList& font_list, |
| 65 | float available_width, |
| 66 | ElisionMethod method) { |
| 67 | switch (method) { |
| 68 | case kMethodSimple: { |
| 69 | url::Parsed parsed; |
Christopher Grant | bd541e5 | 2017-10-13 20:59:41 | [diff] [blame] | 70 | return FormatAndElideUrlSimple(url, font_list, available_width, &parsed); |
Christopher Grant | 6871e121 | 2017-10-09 15:47:50 | [diff] [blame] | 71 | } |
| 72 | #if !defined(OS_ANDROID) |
| 73 | case kMethodOriginal: |
| 74 | return url_formatter::ElideUrl(url, font_list, available_width); |
| 75 | #endif |
| 76 | default: |
| 77 | NOTREACHED(); |
| 78 | return base::string16(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | url::Component* GetComponent(url::Parsed* parsed, |
| 83 | url::Parsed::ComponentType type) { |
| 84 | switch (type) { |
| 85 | case url::Parsed::SCHEME: |
| 86 | return &parsed->scheme; |
| 87 | case url::Parsed::USERNAME: |
| 88 | return &parsed->username; |
| 89 | case url::Parsed::PASSWORD: |
| 90 | return &parsed->password; |
| 91 | case url::Parsed::HOST: |
| 92 | return &parsed->host; |
| 93 | case url::Parsed::PORT: |
| 94 | return &parsed->port; |
| 95 | case url::Parsed::PATH: |
| 96 | return &parsed->path; |
| 97 | case url::Parsed::QUERY: |
| 98 | return &parsed->query; |
| 99 | case url::Parsed::REF: |
| 100 | return &parsed->ref; |
| 101 | default: |
| 102 | NOTREACHED(); |
| 103 | return nullptr; |
| 104 | } |
| 105 | } |
| 106 | |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 107 | // Verify that one or more URLs passes through an explicit sequence of elided |
| 108 | // strings as available space progressively decreases. This helps ensure that |
| 109 | // transitional corner cases are handled properly. To be tolerant of |
| 110 | // character-width variation across platforms, the test allows a limited number |
| 111 | // of expected strings to be skipped mid-run. The first and last expected |
| 112 | // strings must be matched. If the algorithm produces a string that isn't in the |
| 113 | // expected string list, the test fill fail. Example test expectations: |
| 114 | // |
| 115 | // google.com/intl/en/.../ads/ <-- Must match. |
| 116 | // google.com/intl/.../ads/ |
| 117 | // google.com/.../ads/ |
| 118 | // google.com/intl... <- Elider can skip this, in case the 'l' does not fit. |
| 119 | // google.com/int... |
| 120 | // google.com/in... <- Must match. |
| 121 | // |
| 122 | void RunProgressiveElisionTest( |
Christopher Grant | 6871e121 | 2017-10-09 15:47:50 | [diff] [blame] | 123 | const std::vector<ProgressiveTestcase>& testcases, |
| 124 | ElisionMethod method) { |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 125 | const gfx::FontList font_list; |
| 126 | for (const auto& testcase : testcases) { |
| 127 | SCOPED_TRACE("Eliding " + testcase.input); |
| 128 | const GURL url(testcase.input); |
| 129 | |
| 130 | // Occasionally, a parsed URL can grow in length before elision, such as |
| 131 | // when parsing a Windows file path with missing slashes. |
| 132 | ASSERT_FALSE(testcase.output.empty()); |
| 133 | float width = std::max( |
| 134 | gfx::GetStringWidthF(base::UTF8ToUTF16(testcase.input), font_list), |
| 135 | gfx::GetStringWidthF(base::UTF8ToUTF16(testcase.output.front()), |
| 136 | font_list)); |
| 137 | |
| 138 | // Ideally, this test would iterate through all available field widths on a |
| 139 | // per-pixel basis, but this is slow. Instead, compute the next input field |
| 140 | // width as slightly less than the previous elided string. This approach |
| 141 | // misses coverage in cases where a smaller available width generates a |
| 142 | // longer string than some other larger available width, but the tradeoff |
| 143 | // feels acceptable. |
| 144 | int mismatches = 0; |
| 145 | const int kMaxConsecutiveMismatches = 3; |
| 146 | for (size_t i = 0; i < testcase.output.size(); i++) { |
| 147 | const auto& expected = testcase.output[i]; |
| 148 | base::string16 expected_utf16 = base::UTF8ToUTF16(expected); |
Christopher Grant | 6871e121 | 2017-10-09 15:47:50 | [diff] [blame] | 149 | base::string16 elided = Elide(url, font_list, width, method); |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 150 | if (expected_utf16 != elided) { |
| 151 | if (i > 0 && i < testcase.output.size() - 1 && |
| 152 | mismatches < kMaxConsecutiveMismatches) { |
| 153 | mismatches++; |
| 154 | continue; |
| 155 | } |
| 156 | EXPECT_EQ(expected_utf16, elided); |
| 157 | break; |
| 158 | } |
| 159 | mismatches = 0; |
| 160 | float new_width = gfx::GetStringWidthF(elided, font_list); |
| 161 | // Elision rounds fractional available widths up. |
| 162 | EXPECT_LE(new_width, std::ceil(width)) << " at " << elided; |
| 163 | width = new_width - 1.0f; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
Christopher Grant | 6871e121 | 2017-10-09 15:47:50 | [diff] [blame] | 168 | #if !defined(OS_ANDROID) |
| 169 | |
| 170 | void RunElisionTest(const std::vector<Testcase>& testcases) { |
| 171 | const gfx::FontList font_list; |
| 172 | for (const auto& testcase : testcases) { |
| 173 | SCOPED_TRACE("Eliding " + testcase.input); |
| 174 | const GURL url(testcase.input); |
| 175 | const float available_width = |
| 176 | gfx::GetStringWidthF(base::UTF8ToUTF16(testcase.output), font_list); |
| 177 | EXPECT_EQ(base::UTF8ToUTF16(testcase.output), |
| 178 | url_formatter::ElideUrl(url, font_list, available_width)); |
| 179 | } |
| 180 | } |
| 181 | |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 182 | // Test eliding of commonplace URLs. |
| 183 | TEST(TextEliderTest, TestGeneralEliding) { |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 184 | const std::string kEllipsisStr(gfx::kEllipsis); |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 185 | const std::vector<ProgressiveTestcase> progressive_testcases = { |
| 186 | // Elide a non-www URL (www URLs are handled differently). In this first |
| 187 | // case, elide down to nothing to test the terminal cases. |
| 188 | {"http://xyz.google.com/foo?bar", |
| 189 | { |
| 190 | /* clang-format off */ |
| 191 | "xyz.google.com/foo?bar", |
| 192 | "xyz.google.com/foo?b" + kEllipsisStr, |
| 193 | "xyz.google.com/foo?" + kEllipsisStr, |
| 194 | "xyz.google.com/foo" + kEllipsisStr, |
| 195 | "xyz.google.com/fo" + kEllipsisStr, |
| 196 | "xyz.google.com/f" + kEllipsisStr, |
| 197 | kEllipsisStr + "google.com/foo" + kEllipsisStr, |
| 198 | kEllipsisStr + "google.com/fo" + kEllipsisStr, |
| 199 | kEllipsisStr + "google.com/f" + kEllipsisStr, |
| 200 | kEllipsisStr + "google.com/" + kEllipsisStr, |
| 201 | kEllipsisStr + "google.com" + kEllipsisStr, |
| 202 | kEllipsisStr + "google.co" + kEllipsisStr, |
| 203 | kEllipsisStr + "google.c" + kEllipsisStr, |
| 204 | kEllipsisStr + "google." + kEllipsisStr, |
| 205 | kEllipsisStr + "google" + kEllipsisStr, |
| 206 | kEllipsisStr + "googl" + kEllipsisStr, |
| 207 | kEllipsisStr + "goog" + kEllipsisStr, |
| 208 | kEllipsisStr + "goo" + kEllipsisStr, |
| 209 | kEllipsisStr + "go" + kEllipsisStr, |
| 210 | kEllipsisStr + "g" + kEllipsisStr, |
| 211 | kEllipsisStr + kEllipsisStr, |
| 212 | kEllipsisStr, |
| 213 | "" |
| 214 | /* clang-format on */ |
| 215 | }}, |
| 216 | // The trailing directory name is preserved |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 217 | {"http://www.google.com/intl/en/ads/", |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 218 | { |
| 219 | /* clang-format off */ |
| 220 | "www.google.com/intl/en/ads/", |
| 221 | "google.com/intl/en/ads/", |
| 222 | "google.com/intl/" + kEllipsisStr + "/ads/", |
| 223 | "google.com/" + kEllipsisStr + "/ads/", |
| 224 | "google.com/" + kEllipsisStr + "/ad" + kEllipsisStr, |
| 225 | "google.com/" + kEllipsisStr + "/a" + kEllipsisStr, |
| 226 | "google.com/intl/e" + kEllipsisStr, |
| 227 | "google.com/intl/" + kEllipsisStr, |
| 228 | "google.com/intl" + kEllipsisStr, |
| 229 | "google.com/int" + kEllipsisStr, |
| 230 | "google.com/in" + kEllipsisStr, |
| 231 | "google.com/i" + kEllipsisStr, |
| 232 | "google.com/" + kEllipsisStr, |
| 233 | "google.com" + kEllipsisStr, |
| 234 | "google.co" + kEllipsisStr, |
| 235 | "google.c" + kEllipsisStr, |
| 236 | "google." + kEllipsisStr, |
| 237 | "google" + kEllipsisStr, |
| 238 | "googl" + kEllipsisStr, |
| 239 | /* clang-format on */ |
| 240 | }}, |
| 241 | // Subdomain is completely elided if the last path element doesn't fit. |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 242 | {"https://subdomain.foo.com/bar/filename.html", |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 243 | { |
| 244 | /* clang-format off */ |
| 245 | "https://subdomain.foo.com/bar/filename.html", |
| 246 | "subdomain.foo.com/bar/filename.html", |
| 247 | "subdomain.foo.com/" + kEllipsisStr + "/filename.html", |
| 248 | kEllipsisStr + "foo.com/bar/filename.html", |
| 249 | kEllipsisStr + "foo.com/" + kEllipsisStr + "/filename.html", |
| 250 | /* clang-format on */ |
| 251 | }}, |
| 252 | // Path eliding works when a query is present. |
| 253 | {"http://www.g.com/subdir/ads/?query", |
| 254 | { |
| 255 | /* clang-format off */ |
| 256 | "www.g.com/subdir/ads/?query", |
| 257 | "www.g.com/subdir/ads/?que" + kEllipsisStr, |
| 258 | "www.g.com/subdir/ads/?qu" + kEllipsisStr, |
| 259 | "www.g.com/subdir/ads/?q" + kEllipsisStr, |
| 260 | "www.g.com/subdir/ads/?" + kEllipsisStr, |
| 261 | "www.g.com/subdir/ads/" + kEllipsisStr, |
| 262 | "www.g.com/subdir/ads" + kEllipsisStr, |
| 263 | "www.g.com/subdir/ad" + kEllipsisStr, |
| 264 | /* clang-format on */ |
| 265 | }}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 266 | }; |
Christopher Grant | 6871e121 | 2017-10-09 15:47:50 | [diff] [blame] | 267 | RunProgressiveElisionTest(progressive_testcases, kMethodOriginal); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | // When there is very little space available, the elision code will shorten |
| 271 | // both path AND file name to an ellipsis - ".../...". To avoid this result, |
| 272 | // there is a hack in place that simply treats them as one string in this |
| 273 | // case. |
| 274 | TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) { |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 275 | const std::string kEllipsisStr(gfx::kEllipsis); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 276 | |
| 277 | // Very little space, would cause double ellipsis. |
| 278 | gfx::FontList font_list; |
| 279 | GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html"); |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 280 | float available_width = gfx::GetStringWidthF( |
| 281 | base::UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr), |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 282 | font_list); |
| 283 | |
| 284 | // Create the expected string, after elision. Depending on font size, the |
| 285 | // directory might become /dir... or /di... or/d... - it never should be |
| 286 | // shorter than that. (If it is, the font considers d... to be longer |
| 287 | // than .../... - that should never happen). |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 288 | ASSERT_GT( |
| 289 | gfx::GetStringWidthF(base::UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr), |
| 290 | font_list), |
| 291 | gfx::GetStringWidthF(base::UTF8ToUTF16("d" + kEllipsisStr), font_list)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 292 | GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc"); |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 293 | base::string16 expected = url_formatter::ElideUrl( |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 294 | long_url, font_list, available_width); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 295 | // Ensure that the expected result still contains part of the directory name. |
| 296 | ASSERT_GT(expected.length(), std::string("battersbox.com/d").length()); |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 297 | EXPECT_EQ(expected, url_formatter::ElideUrl(url, font_list, available_width)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 298 | |
Matt Giuca | 49fcda17 | 2017-08-29 07:16:12 | [diff] [blame] | 299 | // Regression test for https://crbug.com/756717. An empty path, eliding to a |
| 300 | // width in between the full domain ("www.angelfire.lycos.com") and a bit |
| 301 | // longer than the ETLD+1 ("…lycos.com…/…UV"). This previously crashed due to |
| 302 | // the path being empty. |
| 303 | url = GURL("http://www.angelfire.lycos.com/"); |
| 304 | available_width = gfx::GetStringWidthF( |
| 305 | base::UTF8ToUTF16(kEllipsisStr + "angelfire.lycos.com"), font_list); |
| 306 | EXPECT_EQ(base::UTF8ToUTF16(kEllipsisStr + "lycos.com"), |
| 307 | url_formatter::ElideUrl(url, font_list, available_width)); |
| 308 | |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 309 | // More space available - elide directories, partially elide filename. |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 310 | const std::vector<Testcase> testcases = { |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 311 | {"http://battersbox.com/directory/foo/peter_paul_and_mary.html", |
| 312 | "battersbox.com/" + kEllipsisStr + "/peter" + kEllipsisStr}, |
| 313 | }; |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 314 | RunElisionTest(testcases); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | // Test eliding of empty strings, URLs with ports, passwords, queries, etc. |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 318 | TEST(TextEliderTest, TestElisionSpecialCases) { |
kulshin | ce612eb | 2016-06-29 18:46:51 | [diff] [blame] | 319 | #if defined(OS_WIN) |
| 320 | // Needed to bypass DCHECK in GetFallbackFont. |
| 321 | base::MessageLoopForUI message_loop; |
| 322 | #endif |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 323 | const std::string kEllipsisStr(gfx::kEllipsis); |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 324 | const std::vector<Testcase> testcases = { |
Matt Giuca | 49fcda17 | 2017-08-29 07:16:12 | [diff] [blame] | 325 | // URL with "www" subdomain (gets removed specially). |
| 326 | {"http://www.google.com/foo?bar", "www.google.com/foo?bar"}, |
| 327 | {"http://www.google.com/foo?bar", "google.com/foo?bar"}, |
| 328 | |
Matt Giuca | 51f68cc | 2017-07-07 01:34:57 | [diff] [blame] | 329 | // URL with no path. |
Matt Giuca | 49fcda17 | 2017-08-29 07:16:12 | [diff] [blame] | 330 | {"http://xyz.google.com", kEllipsisStr + "google.com"}, |
| 331 | {"https://xyz.google.com", kEllipsisStr + "google.com"}, |
Matt Giuca | 51f68cc | 2017-07-07 01:34:57 | [diff] [blame] | 332 | |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 333 | {"http://a.b.com/pathname/c?d", "a.b.com/" + kEllipsisStr + "/c?d"}, |
| 334 | {"", ""}, |
| 335 | {"http://foo.bar..example.com...hello/test/filename.html", |
| 336 | "foo.bar..example.com...hello/" + kEllipsisStr + "/filename.html"}, |
| 337 | {"http://foo.bar../", "foo.bar.."}, |
| 338 | {"http://xn--1lq90i.cn/foo", "\xe5\x8c\x97\xe4\xba\xac.cn/foo"}, |
| 339 | {"http://me:[email protected]:99/foo?bar#baz", |
| 340 | "secrethost.com:99/foo?bar#baz"}, |
| 341 | {"http://me:mypass@ss%xxfdsf.com/foo", "ss%25xxfdsf.com/foo"}, |
| 342 | {"mailto:[email protected]", "mailto:[email protected]"}, |
| 343 | {"javascript:click(0)", "javascript:click(0)"}, |
| 344 | {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename", |
| 345 | "chess.eecs.berkeley.edu:4430/login/arbitfilename"}, |
| 346 | {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename", |
| 347 | kEllipsisStr + "berkeley.edu:4430/" + kEllipsisStr + "/arbitfilename"}, |
| 348 | |
| 349 | // Unescaping. |
Eric Lawrence | 03f9a90 | 2017-12-16 04:48:11 | [diff] [blame^] | 350 | {"http://www/%E4%BD%A0%E5%A5%BD?" |
| 351 | "q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0\xe4\xbd\xa0\xe4\xbd\xa0", |
| 352 | "www/\xe4\xbd\xa0\xe5\xa5\xbd?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0" + |
Mike West | f8f6ed5 | 2017-10-09 20:58:50 | [diff] [blame] | 353 | kEllipsisStr}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 354 | |
| 355 | // Invalid unescaping for path. The ref will always be valid UTF-8. We |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 356 | // don't bother to do too many edge cases, since these are handled by the |
| 357 | // escaper unittest. |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 358 | {"http://www/%E4%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0", |
Eric Lawrence | 03f9a90 | 2017-12-16 04:48:11 | [diff] [blame^] | 359 | "www/%E4%A0%E5%A5%BD?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 360 | }; |
| 361 | |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 362 | RunElisionTest(testcases); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | // Test eliding of file: URLs. |
| 366 | TEST(TextEliderTest, TestFileURLEliding) { |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 367 | const std::string kEllipsisStr(gfx::kEllipsis); |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 368 | const std::vector<ProgressiveTestcase> progressive_testcases = { |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 369 | {"file:///C:/path1/path2/path3/filename", |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 370 | { |
| 371 | /* clang-format off */ |
| 372 | "file:///C:/path1/path2/path3/filename", |
| 373 | "C:/path1/path2/path3/filename", |
| 374 | "C:/path1/path2/" + kEllipsisStr + "/filename", |
| 375 | /* clang-format on */ |
| 376 | }}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 377 | // GURL parses "file:///C:path" differently on windows than it does on posix. |
| 378 | #if defined(OS_WIN) |
| 379 | {"file:///C:path1/path2/path3/filename", |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 380 | { |
| 381 | /* clang-format off */ |
| 382 | "file:///C:/path1/path2/path3/filename", |
| 383 | "C:/path1/path2/path3/filename", |
| 384 | "C:/path1/path2/" + kEllipsisStr + "/filename", |
| 385 | "C:/path1/" + kEllipsisStr + "/filename", |
| 386 | "C:/" + kEllipsisStr + "/filename", |
| 387 | /* clang-format on */ |
| 388 | }}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 389 | #endif // defined(OS_WIN) |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 390 | {"file://filer/foo/bar/file", |
| 391 | { |
| 392 | /* clang-format off */ |
| 393 | "file://filer/foo/bar/file", |
| 394 | "filer/foo/bar/file", |
| 395 | "filer/foo/" + kEllipsisStr + "/file", |
| 396 | "filer/" + kEllipsisStr + "/file", |
| 397 | "filer/foo" + kEllipsisStr, |
| 398 | "filer/fo" + kEllipsisStr, |
| 399 | "filer/f" + kEllipsisStr, |
| 400 | "filer/" + kEllipsisStr, |
| 401 | "filer" + kEllipsisStr, |
| 402 | "file" + kEllipsisStr, |
| 403 | /* clang-format on */ |
| 404 | }}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 405 | }; |
| 406 | |
Christopher Grant | 6871e121 | 2017-10-09 15:47:50 | [diff] [blame] | 407 | RunProgressiveElisionTest(progressive_testcases, kMethodOriginal); |
Christopher Grant | 5ed8d11 | 2017-09-12 20:00:06 | [diff] [blame] | 408 | |
| 409 | const std::vector<Testcase> testcases = { |
| 410 | // Eliding file URLs with nothing after the ':' shouldn't crash. |
| 411 | {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:", "aaa" + kEllipsisStr}, |
| 412 | {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:/", "aaa" + kEllipsisStr}, |
| 413 | }; |
| 414 | RunElisionTest(testcases); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | TEST(TextEliderTest, TestHostEliding) { |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 418 | const std::string kEllipsisStr(gfx::kEllipsis); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 419 | Testcase testcases[] = { |
pkl | 2affa91 | 2017-02-22 15:23:20 | [diff] [blame] | 420 | {"http://google.com", "google.com"}, |
| 421 | {"http://reallyreallyreallylongdomainname.com", |
| 422 | "reallyreallyreallylongdomainname.com"}, |
| 423 | {"http://foo", "foo"}, |
| 424 | {"http://foo.bar", "foo.bar"}, |
pkl | 2affa91 | 2017-02-22 15:23:20 | [diff] [blame] | 425 | {"http://subdomain.google.com", kEllipsisStr + ".google.com"}, |
| 426 | {"http://a.b.c.d.e.f.com", kEllipsisStr + "f.com"}, |
| 427 | {"http://subdomain.foo.bar", kEllipsisStr + "in.foo.bar"}, |
| 428 | {"http://subdomain.reallylongdomainname.com", |
| 429 | kEllipsisStr + "ain.reallylongdomainname.com"}, |
| 430 | {"http://a.b.c.d.e.f.com", kEllipsisStr + ".e.f.com"}, |
| 431 | // IDN - Greek alpha.beta.gamma.delta.epsilon.zeta.com |
| 432 | {"http://xn--mxa.xn--nxa.xn--oxa.xn--pxa.xn--qxa.xn--rxa.com", |
| 433 | kEllipsisStr + ".\xCE\xB5.\xCE\xB6.com"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 434 | }; |
| 435 | |
| 436 | for (size_t i = 0; i < arraysize(testcases); ++i) { |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 437 | const float available_width = gfx::GetStringWidthF( |
| 438 | base::UTF8ToUTF16(testcases[i].output), gfx::FontList()); |
| 439 | EXPECT_EQ(base::UTF8ToUTF16(testcases[i].output), |
| 440 | url_formatter::ElideHost(GURL(testcases[i].input), |
| 441 | gfx::FontList(), available_width)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // Trying to elide to a really short length will still keep the full TLD+1 |
| 445 | EXPECT_EQ( |
| 446 | base::ASCIIToUTF16("google.com"), |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 447 | url_formatter::ElideHost(GURL("http://google.com"), gfx::FontList(), 2)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 448 | EXPECT_EQ(base::UTF8ToUTF16(kEllipsisStr + ".google.com"), |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 449 | url_formatter::ElideHost(GURL("http://subdomain.google.com"), |
| 450 | gfx::FontList(), 2)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 451 | EXPECT_EQ( |
| 452 | base::ASCIIToUTF16("foo.bar"), |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 453 | url_formatter::ElideHost(GURL("http://foo.bar"), gfx::FontList(), 2)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | #endif // !defined(OS_ANDROID) |
| 457 | |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 458 | struct OriginTestData { |
| 459 | const char* const description; |
| 460 | const char* const input; |
| 461 | const wchar_t* const output; |
| 462 | const wchar_t* const output_omit_web_scheme; |
| 463 | const wchar_t* const output_omit_cryptographic_scheme; |
| 464 | }; |
| 465 | |
| 466 | // Common test data for both FormatUrlForSecurityDisplay() and |
| 467 | // FormatOriginForSecurityDisplay() |
| 468 | const OriginTestData common_tests[] = { |
| 469 | {"Empty URL", "", L"", L"", L""}, |
| 470 | {"HTTP URL", "http://www.google.com/", L"http://www.google.com", |
| 471 | L"www.google.com", L"http://www.google.com"}, |
| 472 | {"HTTPS URL", "https://www.google.com/", L"https://www.google.com", |
| 473 | L"www.google.com", L"www.google.com"}, |
| 474 | {"Standard HTTP port", "http://www.google.com:80/", |
| 475 | L"http://www.google.com", L"www.google.com", L"http://www.google.com"}, |
| 476 | {"Standard HTTPS port", "https://www.google.com:443/", |
| 477 | L"https://www.google.com", L"www.google.com", L"www.google.com"}, |
| 478 | {"Standard HTTP port, IDN Chinese", |
| 479 | "http://\xe4\xb8\xad\xe5\x9b\xbd.icom.museum:80", |
jshin | 78809c4d8 | 2016-10-06 20:15:45 | [diff] [blame] | 480 | L"http://\x4e2d\x56fd.icom.museum", L"\x4e2d\x56fd.icom.museum", |
| 481 | L"http://\x4e2d\x56fd.icom.museum"}, |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 482 | {"HTTP URL, IDN Hebrew (RTL)", |
| 483 | "http://" |
| 484 | "\xd7\x90\xd7\x99\xd7\xa7\xd7\x95\xd7\xb4\xd7\x9d." |
| 485 | "\xd7\x99\xd7\xa9\xd7\xa8\xd7\x90\xd7\x9c.museum/", |
| 486 | L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum", |
| 487 | L"xn--4dbklr2c8d.xn--4dbrk0ce.museum", |
| 488 | L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum"}, |
| 489 | {"HTTP URL with query string, IDN Arabic (RTL)", |
| 490 | "http://\xd9\x85\xd8\xb5\xd8\xb1.icom.museum/foo.html?yes=no", |
| 491 | L"http://xn--wgbh1c.icom.museum", L"xn--wgbh1c.icom.museum", |
| 492 | L"http://xn--wgbh1c.icom.museum"}, |
| 493 | {"Non-standard HTTP port", "http://www.google.com:9000/", |
| 494 | L"http://www.google.com:9000", L"www.google.com:9000", |
| 495 | L"http://www.google.com:9000"}, |
| 496 | {"Non-standard HTTPS port", "https://www.google.com:9000/", |
| 497 | L"https://www.google.com:9000", L"www.google.com:9000", |
| 498 | L"www.google.com:9000"}, |
| 499 | {"HTTP URL with path", "http://www.google.com/test.html", |
| 500 | L"http://www.google.com", L"www.google.com", L"http://www.google.com"}, |
| 501 | {"HTTPS URL with path", "https://www.google.com/test.html", |
| 502 | L"https://www.google.com", L"www.google.com", L"www.google.com"}, |
| 503 | {"Unusual secure scheme (wss)", "wss://www.google.com/", |
| 504 | L"wss://www.google.com", L"wss://www.google.com", L"www.google.com"}, |
| 505 | {"Unusual non-secure scheme (gopher)", "gopher://www.google.com/", |
| 506 | L"gopher://www.google.com", L"gopher://www.google.com", |
| 507 | L"gopher://www.google.com"}, |
| 508 | {"Unlisted scheme (chrome)", "chrome://version", L"chrome://version", |
| 509 | L"chrome://version", L"chrome://version"}, |
| 510 | {"HTTP IP address", "http://173.194.65.103", L"http://173.194.65.103", |
| 511 | L"173.194.65.103", L"http://173.194.65.103"}, |
| 512 | {"HTTPS IP address", "https://173.194.65.103", L"https://173.194.65.103", |
| 513 | L"173.194.65.103", L"173.194.65.103"}, |
| 514 | {"HTTP IPv6 address", "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/", |
| 515 | L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]", |
| 516 | L"http://[fe80::202:b3ff:fe1e:8329]"}, |
| 517 | {"HTTPs IPv6 address", "https://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/", |
| 518 | L"https://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]", |
| 519 | L"[fe80::202:b3ff:fe1e:8329]"}, |
| 520 | {"HTTP IPv6 address with port", |
| 521 | "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:80/", |
| 522 | L"http://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]", |
| 523 | L"http://[fe80::202:b3ff:fe1e:8329]"}, |
| 524 | {"HTTPs IPv6 address with port", |
| 525 | "https://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:443/", |
| 526 | L"https://[fe80::202:b3ff:fe1e:8329]", L"[fe80::202:b3ff:fe1e:8329]", |
| 527 | L"[fe80::202:b3ff:fe1e:8329]"}, |
| 528 | {"HTTPS IP address, non-default port", "https://173.194.65.103:8443", |
| 529 | L"https://173.194.65.103:8443", L"173.194.65.103:8443", |
| 530 | L"173.194.65.103:8443"}, |
| 531 | {"Invalid host 1", "https://www.cyber../wow.php", L"https://www.cyber..", |
| 532 | L"www.cyber..", L"www.cyber.."}, |
| 533 | {"Invalid host 2", "https://www...cyber/wow.php", L"https://www...cyber", |
| 534 | L"www...cyber", L"www...cyber"}, |
| 535 | {"Invalid port 3", "https://173.194.65.103:/hello.aspx", |
| 536 | L"https://173.194.65.103", L"173.194.65.103", L"173.194.65.103"}, |
| 537 | {"Trailing dot in DNS name", "https://www.example.com./get/goat", |
| 538 | L"https://www.example.com.", L"www.example.com.", L"www.example.com."}}; |
| 539 | |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 540 | TEST(TextEliderTest, FormatUrlForSecurityDisplay) { |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 541 | for (size_t i = 0; i < arraysize(common_tests); ++i) { |
| 542 | base::string16 formatted = |
| 543 | url_formatter::FormatUrlForSecurityDisplay(GURL(common_tests[i].input)); |
| 544 | EXPECT_EQ(base::WideToUTF16(common_tests[i].output), formatted) |
| 545 | << common_tests[i].description; |
| 546 | |
| 547 | base::string16 formatted_omit_web_scheme = |
| 548 | url_formatter::FormatUrlForSecurityDisplay( |
| 549 | GURL(common_tests[i].input), |
| 550 | url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS); |
| 551 | EXPECT_EQ(base::WideToUTF16(common_tests[i].output_omit_web_scheme), |
| 552 | formatted_omit_web_scheme) |
| 553 | << common_tests[i].description; |
| 554 | |
| 555 | base::string16 formatted_omit_cryptographic_scheme = |
| 556 | url_formatter::FormatUrlForSecurityDisplay( |
| 557 | GURL(common_tests[i].input), |
| 558 | url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC); |
| 559 | EXPECT_EQ( |
| 560 | base::WideToUTF16(common_tests[i].output_omit_cryptographic_scheme), |
| 561 | formatted_omit_cryptographic_scheme) |
| 562 | << common_tests[i].description; |
| 563 | } |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 564 | |
| 565 | const OriginTestData tests[] = { |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 566 | {"File URI", "file:///usr/example/file.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 567 | L"file:///usr/example/file.html", L"file:///usr/example/file.html", |
| 568 | L"file:///usr/example/file.html"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 569 | {"File URI with hostname", "file://localhost/usr/example/file.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 570 | L"file:///usr/example/file.html", L"file:///usr/example/file.html", |
| 571 | L"file:///usr/example/file.html"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 572 | {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls", |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 573 | L"file:///CONTOSO/accounting/money.xls", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 574 | L"file:///CONTOSO/accounting/money.xls", |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 575 | L"file:///CONTOSO/accounting/money.xls"}, |
| 576 | {"UNC File URI 2", |
| 577 | "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO", |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 578 | L"file:///C:/Program%20Files/Music/Web%20Sys/main.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 579 | L"file:///C:/Program%20Files/Music/Web%20Sys/main.html", |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 580 | L"file:///C:/Program%20Files/Music/Web%20Sys/main.html"}, |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 581 | {"Invalid IPv6 address", "https://[2001:db8:0:1]/", |
| 582 | L"https://[2001:db8:0:1]", L"https://[2001:db8:0:1]", |
| 583 | L"https://[2001:db8:0:1]"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 584 | {"HTTP filesystem: URL with path", |
| 585 | "filesystem:http://www.google.com/temporary/test.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 586 | L"filesystem:http://www.google.com", L"filesystem:http://www.google.com", |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 587 | L"filesystem:http://www.google.com"}, |
| 588 | {"File filesystem: URL with path", |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 589 | "filesystem:file://localhost/temporary/stuff/" |
| 590 | "test.html?z=fun&goat=billy", |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 591 | L"filesystem:file:///temporary/stuff/test.html", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 592 | L"filesystem:file:///temporary/stuff/test.html", |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 593 | L"filesystem:file:///temporary/stuff/test.html"}, |
| 594 | {"Invalid scheme 1", "twelve://www.cyber.org/wow.php", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 595 | L"twelve://www.cyber.org/wow.php", L"twelve://www.cyber.org/wow.php", |
| 596 | L"twelve://www.cyber.org/wow.php"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 597 | {"Invalid scheme 2", "://www.cyber.org/wow.php", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 598 | L"://www.cyber.org/wow.php", L"://www.cyber.org/wow.php", |
| 599 | L"://www.cyber.org/wow.php"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 600 | {"Invalid port 1", "https://173.194.65.103:000", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 601 | 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] | 602 | {"Invalid port 2", "https://173.194.65.103:gruffle", |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 603 | L"https://173.194.65.103:gruffle", L"https://173.194.65.103:gruffle", |
| 604 | L"https://173.194.65.103:gruffle"}, |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 605 | {"Blob URL", |
nick | 1b17dc3 | 2016-04-15 21:34:04 | [diff] [blame] | 606 | "blob:http://www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9", |
| 607 | L"blob:http://www.html5rocks.com/" |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 608 | L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9", |
nick | 1b17dc3 | 2016-04-15 21:34:04 | [diff] [blame] | 609 | L"blob:http://www.html5rocks.com/" |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 610 | L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9", |
| 611 | L"blob:http://www.html5rocks.com/" |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 612 | L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9"}}; |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 613 | |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 614 | for (size_t i = 0; i < arraysize(tests); ++i) { |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 615 | base::string16 formatted = |
| 616 | url_formatter::FormatUrlForSecurityDisplay(GURL(tests[i].input)); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 617 | EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted) |
| 618 | << tests[i].description; |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 619 | |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 620 | base::string16 formatted_omit_web_scheme = |
| 621 | url_formatter::FormatUrlForSecurityDisplay( |
| 622 | GURL(tests[i].input), |
| 623 | url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS); |
| 624 | EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_web_scheme), |
| 625 | formatted_omit_web_scheme) |
| 626 | << tests[i].description; |
| 627 | |
| 628 | base::string16 formatted_omit_cryptographic_scheme = |
| 629 | url_formatter::FormatUrlForSecurityDisplay( |
| 630 | GURL(tests[i].input), |
| 631 | url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC); |
| 632 | EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_cryptographic_scheme), |
| 633 | formatted_omit_cryptographic_scheme) |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 634 | << tests[i].description; |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 635 | } |
| 636 | |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 637 | base::string16 formatted = url_formatter::FormatUrlForSecurityDisplay(GURL()); |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 638 | EXPECT_EQ(base::string16(), formatted) |
| 639 | << "Explicitly test the 0-argument GURL constructor"; |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 640 | |
| 641 | base::string16 formatted_omit_scheme = |
benwells | 2337b810 | 2016-04-20 01:53:53 | [diff] [blame] | 642 | url_formatter::FormatUrlForSecurityDisplay( |
| 643 | GURL(), url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS); |
| 644 | EXPECT_EQ(base::string16(), formatted_omit_scheme) |
| 645 | << "Explicitly test the 0-argument GURL constructor"; |
| 646 | |
| 647 | formatted_omit_scheme = url_formatter::FormatUrlForSecurityDisplay( |
| 648 | GURL(), url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC); |
palmer | 153af98 | 2015-09-15 02:04:19 | [diff] [blame] | 649 | EXPECT_EQ(base::string16(), formatted_omit_scheme) |
| 650 | << "Explicitly test the 0-argument GURL constructor"; |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 651 | } |
| 652 | |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 653 | TEST(TextEliderTest, FormatOriginForSecurityDisplay) { |
| 654 | for (size_t i = 0; i < arraysize(common_tests); ++i) { |
| 655 | base::string16 formatted = url_formatter::FormatOriginForSecurityDisplay( |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 656 | url::Origin::Create(GURL(common_tests[i].input))); |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 657 | EXPECT_EQ(base::WideToUTF16(common_tests[i].output), formatted) |
| 658 | << common_tests[i].description; |
| 659 | |
| 660 | base::string16 formatted_omit_web_scheme = |
| 661 | url_formatter::FormatOriginForSecurityDisplay( |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 662 | url::Origin::Create(GURL(common_tests[i].input)), |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 663 | url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS); |
| 664 | EXPECT_EQ(base::WideToUTF16(common_tests[i].output_omit_web_scheme), |
| 665 | formatted_omit_web_scheme) |
| 666 | << common_tests[i].description; |
| 667 | |
| 668 | base::string16 formatted_omit_cryptographic_scheme = |
| 669 | url_formatter::FormatOriginForSecurityDisplay( |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 670 | url::Origin::Create(GURL(common_tests[i].input)), |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 671 | url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC); |
| 672 | EXPECT_EQ( |
| 673 | base::WideToUTF16(common_tests[i].output_omit_cryptographic_scheme), |
| 674 | formatted_omit_cryptographic_scheme) |
| 675 | << common_tests[i].description; |
| 676 | } |
| 677 | |
| 678 | const OriginTestData tests[] = { |
| 679 | {"File URI", "file:///usr/example/file.html", L"file://", L"file://", |
| 680 | L"file://"}, |
| 681 | {"File URI with hostname", "file://localhost/usr/example/file.html", |
| 682 | L"file://localhost", L"file://localhost", L"file://localhost"}, |
| 683 | {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls", L"file://", |
| 684 | L"file://", L"file://"}, |
| 685 | {"UNC File URI 2", |
| 686 | "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO", |
| 687 | L"file://", L"file://", L"file://"}, |
| 688 | {"Invalid IPv6 address", "https://[2001:db8:0:1]/", L"", L"", L""}, |
| 689 | {"HTTP filesystem: URL with path", |
| 690 | "filesystem:http://www.google.com/temporary/test.html", |
| 691 | L"http://www.google.com", L"www.google.com", L"http://www.google.com"}, |
| 692 | {"File filesystem: URL with path", |
| 693 | "filesystem:file://localhost/temporary/stuff/test.html?z=fun&goat=billy", |
| 694 | L"file://", L"file://", L"file://"}, |
| 695 | {"Invalid scheme 1", "twelve://www.cyber.org/wow.php", L"", L"", L""}, |
| 696 | {"Invalid scheme 2", "://www.cyber.org/wow.php", L"", L"", L""}, |
| 697 | {"Invalid port 1", "https://173.194.65.103:000", L"", L"", L""}, |
| 698 | {"Invalid port 2", "https://173.194.65.103:gruffle", L"", L"", L""}, |
| 699 | {"Blob URL", |
| 700 | "blob:http://www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9", |
| 701 | L"http://www.html5rocks.com", L"www.html5rocks.com", |
| 702 | L"http://www.html5rocks.com"}}; |
| 703 | |
| 704 | for (size_t i = 0; i < arraysize(tests); ++i) { |
| 705 | base::string16 formatted = url_formatter::FormatOriginForSecurityDisplay( |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 706 | url::Origin::Create(GURL(tests[i].input))); |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 707 | EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted) |
| 708 | << tests[i].description; |
| 709 | |
| 710 | base::string16 formatted_omit_web_scheme = |
| 711 | url_formatter::FormatOriginForSecurityDisplay( |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 712 | url::Origin::Create(GURL(tests[i].input)), |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 713 | url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS); |
| 714 | EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_web_scheme), |
| 715 | formatted_omit_web_scheme) |
| 716 | << tests[i].description; |
| 717 | |
| 718 | base::string16 formatted_omit_cryptographic_scheme = |
| 719 | url_formatter::FormatOriginForSecurityDisplay( |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 720 | url::Origin::Create(GURL(tests[i].input)), |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 721 | url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC); |
| 722 | EXPECT_EQ(base::WideToUTF16(tests[i].output_omit_cryptographic_scheme), |
| 723 | formatted_omit_cryptographic_scheme) |
| 724 | << tests[i].description; |
| 725 | } |
| 726 | |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 727 | base::string16 formatted = url_formatter::FormatOriginForSecurityDisplay( |
| 728 | url::Origin::Create(GURL())); |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 729 | EXPECT_EQ(base::string16(), formatted) |
| 730 | << "Explicitly test the url::Origin which takes an empty, invalid URL"; |
| 731 | |
| 732 | base::string16 formatted_omit_scheme = |
| 733 | url_formatter::FormatOriginForSecurityDisplay( |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 734 | url::Origin::Create(GURL()), |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 735 | url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS); |
| 736 | EXPECT_EQ(base::string16(), formatted_omit_scheme) |
| 737 | << "Explicitly test the url::Origin which takes an empty, invalid URL"; |
| 738 | |
| 739 | formatted_omit_scheme = url_formatter::FormatOriginForSecurityDisplay( |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 740 | url::Origin::Create(GURL()), |
| 741 | url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC); |
juncai | cb63cac | 2016-05-13 00:41:59 | [diff] [blame] | 742 | EXPECT_EQ(base::string16(), formatted_omit_scheme) |
| 743 | << "Explicitly test the url::Origin which takes an empty, invalid URL"; |
| 744 | } |
| 745 | |
Christopher Grant | 6871e121 | 2017-10-09 15:47:50 | [diff] [blame] | 746 | TEST(TextEliderTest, TestSimpleElisionMethod) { |
| 747 | const std::string kEllipsisStr(gfx::kEllipsis); |
| 748 | const std::vector<ProgressiveTestcase> testcases = { |
| 749 | {"https://www.abc.com/def/", |
| 750 | { |
| 751 | /* clang-format off */ |
| 752 | {"https://www.abc.com/def/"}, |
| 753 | {"https://www.abc.com/d" + kEllipsisStr}, |
| 754 | {"https://www.abc.com/" + kEllipsisStr}, |
| 755 | {"www.abc.com/def/"}, |
| 756 | {"www.abc.com/d" + kEllipsisStr}, |
| 757 | {"www.abc.com/" + kEllipsisStr}, |
| 758 | {kEllipsisStr + "ww.abc.com/" + kEllipsisStr}, |
| 759 | {kEllipsisStr + "w.abc.com/" + kEllipsisStr}, |
| 760 | {kEllipsisStr + ".abc.com/" + kEllipsisStr}, |
| 761 | {kEllipsisStr + "abc.com/" + kEllipsisStr}, |
| 762 | {kEllipsisStr + "bc.com/" + kEllipsisStr}, |
| 763 | {kEllipsisStr + "c.com/" + kEllipsisStr}, |
| 764 | {kEllipsisStr + ".com/" + kEllipsisStr}, |
| 765 | {kEllipsisStr + "com/" + kEllipsisStr}, |
| 766 | {kEllipsisStr + "om/" + kEllipsisStr}, |
| 767 | {kEllipsisStr + "m/" + kEllipsisStr}, |
| 768 | {kEllipsisStr + "/" + kEllipsisStr}, |
| 769 | {kEllipsisStr}, |
| 770 | {""}, |
| 771 | /* clang-format on */ |
| 772 | }}, |
| 773 | {"file://fs/file", |
| 774 | { |
| 775 | /* clang-format off */ |
| 776 | "file://fs/file", |
| 777 | "file://fs/fi" + kEllipsisStr, |
| 778 | "file://fs/f" + kEllipsisStr, |
| 779 | "file://fs/" + kEllipsisStr, |
| 780 | "file://fs" + kEllipsisStr, |
| 781 | "file://f" + kEllipsisStr, |
| 782 | "file://" + kEllipsisStr, |
| 783 | "file:/" + kEllipsisStr, |
| 784 | "file:" + kEllipsisStr, |
| 785 | "file" + kEllipsisStr, |
| 786 | "fil" + kEllipsisStr, |
| 787 | "fi" + kEllipsisStr, |
| 788 | "f" + kEllipsisStr, |
| 789 | kEllipsisStr, |
| 790 | "", |
| 791 | /* clang-format on */ |
| 792 | }}, |
| 793 | }; |
| 794 | RunProgressiveElisionTest(testcases, kMethodSimple); |
| 795 | } |
| 796 | |
| 797 | // Verify that the secure elision method returns URL component data that |
| 798 | // correctly represents the elided URL. |
| 799 | void RunElisionParsingTest(const std::vector<ParsingTestcase>& testcases) { |
| 800 | const gfx::FontList font_list; |
| 801 | for (const auto& testcase : testcases) { |
| 802 | SCOPED_TRACE(testcase.input + " to " + testcase.output); |
| 803 | |
| 804 | const GURL url(testcase.input); |
| 805 | const float available_width = |
| 806 | gfx::GetStringWidthF(base::UTF8ToUTF16(testcase.output), font_list); |
| 807 | |
| 808 | url::Parsed parsed; |
| 809 | auto elided = |
Christopher Grant | bd541e5 | 2017-10-13 20:59:41 | [diff] [blame] | 810 | FormatAndElideUrlSimple(url, font_list, available_width, &parsed); |
Christopher Grant | 6871e121 | 2017-10-09 15:47:50 | [diff] [blame] | 811 | EXPECT_EQ(base::UTF8ToUTF16(testcase.output), elided); |
| 812 | |
| 813 | // Build an expected Parsed struct from the sparse test expectations. |
| 814 | url::Parsed expected; |
| 815 | for (const auto& expectation : testcase.components) { |
| 816 | url::Component* component = GetComponent(&expected, expectation.type); |
| 817 | component->begin = expectation.begin; |
| 818 | component->len = expectation.len; |
| 819 | } |
| 820 | |
| 821 | const std::vector<url::Parsed::ComponentType> kComponents = { |
| 822 | url::Parsed::SCHEME, url::Parsed::USERNAME, url::Parsed::PASSWORD, |
| 823 | url::Parsed::HOST, url::Parsed::PORT, url::Parsed::PATH, |
| 824 | url::Parsed::QUERY, url::Parsed::REF, |
| 825 | }; |
| 826 | for (const auto& type : kComponents) { |
| 827 | EXPECT_EQ(GetComponent(&expected, type)->begin, |
| 828 | GetComponent(&parsed, type)->begin) |
| 829 | << " in component " << type; |
| 830 | EXPECT_EQ(GetComponent(&expected, type)->len, |
| 831 | GetComponent(&parsed, type)->len) |
| 832 | << " in component " << type; |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | // Verify that during elision, the parsed URL components are properly modified. |
| 838 | TEST(TextEliderTest, TestElisionParsingAdjustments) { |
| 839 | const std::string kEllipsisStr(gfx::kEllipsis); |
| 840 | const std::vector<ParsingTestcase> testcases = { |
| 841 | // HTTPS with path. |
| 842 | {"https://www.google.com/intl/en/ads/", |
| 843 | "https://www.google.com/intl/en/ads/", |
| 844 | {{url::Parsed::ComponentType::SCHEME, 0, 5}, |
| 845 | {url::Parsed::ComponentType::HOST, 8, 14}, |
| 846 | {url::Parsed::ComponentType::PATH, 22, 13}}}, |
| 847 | {"https://www.google.com/intl/en/ads/", |
| 848 | "https://www.google.com/intl/en/a" + kEllipsisStr, |
| 849 | {{url::Parsed::ComponentType::SCHEME, 0, 5}, |
| 850 | {url::Parsed::ComponentType::HOST, 8, 14}, |
| 851 | {url::Parsed::ComponentType::PATH, 22, 11}}}, |
| 852 | {"https://www.google.com/intl/en/ads/", |
| 853 | "https://www.google.com/" + kEllipsisStr, |
| 854 | {{url::Parsed::ComponentType::SCHEME, 0, 5}, |
| 855 | {url::Parsed::ComponentType::HOST, 8, 14}, |
| 856 | {url::Parsed::ComponentType::PATH, 22, 2}}}, |
| 857 | {"https://www.google.com/intl/en/ads/", |
| 858 | kEllipsisStr + "google.com/" + kEllipsisStr, |
| 859 | {{url::Parsed::ComponentType::HOST, 0, 11}, |
| 860 | {url::Parsed::ComponentType::PATH, 11, 2}}}, |
| 861 | {"https://www.google.com/intl/en/ads/", |
| 862 | kEllipsisStr, |
| 863 | {{url::Parsed::ComponentType::PATH, 0, 1}}}, |
| 864 | // HTTPS with no path. |
| 865 | {"https://www.google.com/", |
| 866 | "www.google.com", |
| 867 | {{url::Parsed::ComponentType::HOST, 0, 14}}}, |
| 868 | {"https://www.google.com/", |
| 869 | kEllipsisStr, |
| 870 | {{url::Parsed::ComponentType::HOST, 0, 1}}}, |
| 871 | // HTTP with no path. |
| 872 | {"http://www.google.com/", |
| 873 | "www.google.com", |
| 874 | {{url::Parsed::ComponentType::HOST, 0, 14}}}, |
| 875 | // File URLs. |
| 876 | {"file:///C:/path1/path2", |
| 877 | "file:///C:/path1/" + kEllipsisStr, |
| 878 | {{url::Parsed::ComponentType::SCHEME, 0, 4}, |
| 879 | {url::Parsed::ComponentType::PATH, 7, 11}}}, |
| 880 | {"file:///C:/path1/path2", |
| 881 | "fi" + kEllipsisStr, |
| 882 | {{url::Parsed::ComponentType::SCHEME, 0, 3}}}, |
| 883 | {"file:///C:/path1/path2", |
| 884 | kEllipsisStr, |
| 885 | {{url::Parsed::ComponentType::SCHEME, 0, 1}}}, |
| 886 | // RTL URL. |
| 887 | {"http://127.0.0.1/ا/http://attack.com", |
| 888 | kEllipsisStr + "7.0.0.1/" + kEllipsisStr, |
| 889 | {{url::Parsed::ComponentType::HOST, 0, 8}, |
| 890 | {url::Parsed::ComponentType::PATH, 8, 2}}}, |
| 891 | }; |
| 892 | |
| 893 | RunElisionParsingTest(testcases); |
| 894 | } |
| 895 | |
miguelg | dbcfb12 | 2015-07-23 10:45:11 | [diff] [blame] | 896 | } // namespace |