diff options
Diffstat (limited to 'components/secure_display')
-rw-r--r-- | components/secure_display/BUILD.gn | 29 | ||||
-rw-r--r-- | components/secure_display/DEPS | 7 | ||||
-rw-r--r-- | components/secure_display/OWNERS | 2 | ||||
-rw-r--r-- | components/secure_display/elide_url.cc | 353 | ||||
-rw-r--r-- | components/secure_display/elide_url.h | 72 | ||||
-rw-r--r-- | components/secure_display/elide_url_unittest.cc | 324 |
6 files changed, 0 insertions, 787 deletions
diff --git a/components/secure_display/BUILD.gn b/components/secure_display/BUILD.gn deleted file mode 100644 index 5e7439d..0000000 --- a/components/secure_display/BUILD.gn +++ /dev/null @@ -1,29 +0,0 @@ -import("//testing/test.gni") - -source_set("secure_display") { - sources = [ - "elide_url.cc", - "elide_url.h", - ] - - deps = [ - "//base:base", - "//net:net", - "//ui/gfx", - "//url", - ] -} - -source_set("unit_tests") { - testonly = true - sources = [ - "elide_url_unittest.cc", - ] - - deps = [ - ":secure_display", - "//base:base", - "//base/test:test_support", - "//testing/gtest", - ] -} diff --git a/components/secure_display/DEPS b/components/secure_display/DEPS deleted file mode 100644 index e378ef8..0000000 --- a/components/secure_display/DEPS +++ /dev/null @@ -1,7 +0,0 @@ -include_rules = [ - "+base/", - "+base/strings", - "+net/base", - "+ui/gfx", - "+url/", -] diff --git a/components/secure_display/OWNERS b/components/secure_display/OWNERS deleted file mode 100644 index b0e35156c7..0000000 --- a/components/secure_display/OWNERS +++ /dev/null @@ -1,2 +0,0 @@ -felt@chromium.org -palmer@chromium.org diff --git a/components/secure_display/elide_url.cc b/components/secure_display/elide_url.cc deleted file mode 100644 index ec4d65a..0000000 --- a/components/secure_display/elide_url.cc +++ /dev/null @@ -1,353 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/secure_display/elide_url.h" - -#include "base/logging.h" -#include "base/strings/string_split.h" -#include "base/strings/utf_string_conversions.h" -#include "net/base/escape.h" -#include "net/base/net_util.h" -#include "net/base/registry_controlled_domains/registry_controlled_domain.h" -#include "ui/gfx/text_elider.h" -#include "ui/gfx/text_utils.h" -#include "url/gurl.h" -#include "url/url_constants.h" - -using base::UTF8ToUTF16; -using gfx::ElideText; -using gfx::GetStringWidthF; -using gfx::kEllipsisUTF16; -using gfx::kForwardSlash; - -namespace { - -#if !defined(OS_ANDROID) -const base::char16 kDot = '.'; - -// Build a path from the first |num_components| elements in |path_elements|. -// Prepends |path_prefix|, appends |filename|, inserts ellipsis if appropriate. -base::string16 BuildPathFromComponents( - const base::string16& path_prefix, - const std::vector<base::string16>& path_elements, - const base::string16& filename, - size_t num_components) { - // Add the initial elements of the path. - base::string16 path = path_prefix; - - // Build path from first |num_components| elements. - for (size_t j = 0; j < num_components; ++j) - path += path_elements[j] + kForwardSlash; - - // Add |filename|, ellipsis if necessary. - if (num_components != (path_elements.size() - 1)) - path += base::string16(kEllipsisUTF16) + kForwardSlash; - path += filename; - - return path; -} - -// Takes a prefix (Domain, or Domain+subdomain) and a collection of path -// components and elides if possible. Returns a string containing the longest -// possible elided path, or an empty string if elision is not possible. -base::string16 ElideComponentizedPath( - const base::string16& url_path_prefix, - const std::vector<base::string16>& url_path_elements, - const base::string16& url_filename, - const base::string16& url_query, - const gfx::FontList& font_list, - float available_pixel_width) { - const size_t url_path_number_of_elements = url_path_elements.size(); - - CHECK(url_path_number_of_elements); - for (size_t i = url_path_number_of_elements - 1; i > 0; --i) { - base::string16 elided_path = BuildPathFromComponents( - url_path_prefix, url_path_elements, url_filename, i); - if (available_pixel_width >= GetStringWidthF(elided_path, font_list)) - return ElideText(elided_path + url_query, font_list, - available_pixel_width, gfx::ELIDE_TAIL); - } - - return base::string16(); -} - -// Splits the hostname in the |url| into sub-strings for the full hostname, -// the domain (TLD+1), and the subdomain (everything leading the domain). -void SplitHost(const GURL& url, - base::string16* url_host, - base::string16* url_domain, - base::string16* url_subdomain) { - // Get Host. - *url_host = UTF8ToUTF16(url.host()); - - // Get domain and registry information from the URL. - *url_domain = - UTF8ToUTF16(net::registry_controlled_domains::GetDomainAndRegistry( - url, net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)); - if (url_domain->empty()) - *url_domain = *url_host; - - // Add port if required. - if (!url.port().empty()) { - *url_host += UTF8ToUTF16(":" + url.port()); - *url_domain += UTF8ToUTF16(":" + url.port()); - } - - // Get sub domain. - const size_t domain_start_index = url_host->find(*url_domain); - base::string16 kWwwPrefix = UTF8ToUTF16("www."); - if (domain_start_index != base::string16::npos) - *url_subdomain = url_host->substr(0, domain_start_index); - if ((*url_subdomain == kWwwPrefix || url_subdomain->empty() || - url.SchemeIsFile())) { - url_subdomain->clear(); - } -} - -#endif // !defined(OS_ANDROID) -} // namespace - -namespace secure_display { - -#if !defined(OS_ANDROID) - -// TODO(pkasting): http://crbug.com/77883 This whole function gets -// kerning/ligatures/etc. issues potentially wrong by assuming that the width of -// a rendered string is always the sum of the widths of its substrings. Also I -// suspect it could be made simpler. -base::string16 ElideUrl(const GURL& url, - const gfx::FontList& font_list, - float available_pixel_width, - const std::string& languages) { - // Get a formatted string and corresponding parsing of the url. - url::Parsed parsed; - const base::string16 url_string = - net::FormatUrl(url, languages, net::kFormatUrlOmitAll, - net::UnescapeRule::SPACES, &parsed, NULL, NULL); - if (available_pixel_width <= 0) - return url_string; - - // If non-standard, return plain eliding. - if (!url.IsStandard()) - return ElideText(url_string, font_list, available_pixel_width, - gfx::ELIDE_TAIL); - - // Now start eliding url_string to fit within available pixel width. - // Fist pass - check to see whether entire url_string fits. - const float pixel_width_url_string = GetStringWidthF(url_string, font_list); - if (available_pixel_width >= pixel_width_url_string) - return url_string; - - // Get the path substring, including query and reference. - const size_t path_start_index = parsed.path.begin; - const size_t path_len = parsed.path.len; - base::string16 url_path_query_etc = url_string.substr(path_start_index); - base::string16 url_path = url_string.substr(path_start_index, path_len); - - // Return general elided text if url minus the query fits. - const base::string16 url_minus_query = - url_string.substr(0, path_start_index + path_len); - if (available_pixel_width >= GetStringWidthF(url_minus_query, font_list)) - return ElideText(url_string, font_list, available_pixel_width, - gfx::ELIDE_TAIL); - - base::string16 url_host; - base::string16 url_domain; - base::string16 url_subdomain; - SplitHost(url, &url_host, &url_domain, &url_subdomain); - - // If this is a file type, the path is now defined as everything after ":". - // For example, "C:/aa/aa/bb", the path is "/aa/bb/cc". Interesting, the - // domain is now C: - this is a nice hack for eliding to work pleasantly. - if (url.SchemeIsFile()) { - // Split the path string using ":" - const base::string16 kColon(1, ':'); - std::vector<base::string16> file_path_split = base::SplitString( - url_path, kColon, base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); - if (file_path_split.size() > 1) { // File is of type "file:///C:/.." - url_host.clear(); - url_domain.clear(); - url_subdomain.clear(); - - url_host = url_domain = file_path_split.at(0).substr(1) + kColon; - url_path_query_etc = url_path = file_path_split.at(1); - } - } - - // Second Pass - remove scheme - the rest fits. - const float pixel_width_url_host = GetStringWidthF(url_host, font_list); - const float pixel_width_url_path = - GetStringWidthF(url_path_query_etc, font_list); - if (available_pixel_width >= pixel_width_url_host + pixel_width_url_path) - return url_host + url_path_query_etc; - - // Third Pass: Subdomain, domain and entire path fits. - const float pixel_width_url_domain = GetStringWidthF(url_domain, font_list); - const float pixel_width_url_subdomain = - GetStringWidthF(url_subdomain, font_list); - if (available_pixel_width >= - pixel_width_url_subdomain + pixel_width_url_domain + pixel_width_url_path) - return url_subdomain + url_domain + url_path_query_etc; - - // Query element. - base::string16 url_query; - const float kPixelWidthDotsTrailer = - GetStringWidthF(base::string16(kEllipsisUTF16), font_list); - if (parsed.query.is_nonempty()) { - url_query = UTF8ToUTF16("?") + url_string.substr(parsed.query.begin); - if (available_pixel_width >= - (pixel_width_url_subdomain + pixel_width_url_domain + - pixel_width_url_path - GetStringWidthF(url_query, font_list))) { - return ElideText(url_subdomain + url_domain + url_path_query_etc, - font_list, available_pixel_width, gfx::ELIDE_TAIL); - } - } - - // Parse url_path using '/'. - std::vector<base::string16> url_path_elements = - base::SplitString(url_path, base::string16(1, kForwardSlash), - base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); - - // Get filename - note that for a path ending with / - // such as www.google.com/intl/ads/, the file name is ads/. - base::string16 url_filename( - url_path_elements.empty() ? base::string16() : url_path_elements.back()); - size_t url_path_number_of_elements = url_path_elements.size(); - if (url_filename.empty() && (url_path_number_of_elements > 1)) { - // Path ends with a '/'. - --url_path_number_of_elements; - url_filename = - url_path_elements[url_path_number_of_elements - 1] + kForwardSlash; - } - - const size_t kMaxNumberOfUrlPathElementsAllowed = 1024; - if (url_path_number_of_elements <= 1 || - url_path_number_of_elements > kMaxNumberOfUrlPathElementsAllowed) { - // No path to elide, or too long of a path (could overflow in loop below) - // Just elide this as a text string. - return ElideText(url_subdomain + url_domain + url_path_query_etc, font_list, - available_pixel_width, gfx::ELIDE_TAIL); - } - - // Start eliding the path and replacing elements by ".../". - const base::string16 kEllipsisAndSlash = - base::string16(kEllipsisUTF16) + kForwardSlash; - const float pixel_width_ellipsis_slash = - GetStringWidthF(kEllipsisAndSlash, font_list); - - // Check with both subdomain and domain. - base::string16 elided_path = ElideComponentizedPath( - url_subdomain + url_domain, url_path_elements, url_filename, url_query, - font_list, available_pixel_width); - if (!elided_path.empty()) - return elided_path; - - // Check with only domain. - // If a subdomain is present, add an ellipsis before domain. - // This is added only if the subdomain pixel width is larger than - // the pixel width of kEllipsis. Otherwise, subdomain remains, - // which means that this case has been resolved earlier. - base::string16 url_elided_domain = url_subdomain + url_domain; - if (pixel_width_url_subdomain > kPixelWidthDotsTrailer) { - if (!url_subdomain.empty()) - url_elided_domain = kEllipsisAndSlash[0] + url_domain; - else - url_elided_domain = url_domain; - - elided_path = ElideComponentizedPath(url_elided_domain, url_path_elements, - url_filename, url_query, font_list, - available_pixel_width); - - if (!elided_path.empty()) - return elided_path; - } - - // Return elided domain/.../filename anyway. - base::string16 final_elided_url_string(url_elided_domain); - const float url_elided_domain_width = - GetStringWidthF(url_elided_domain, font_list); - - // A hack to prevent trailing ".../...". - if ((available_pixel_width - url_elided_domain_width) > - pixel_width_ellipsis_slash + kPixelWidthDotsTrailer + - GetStringWidthF(base::ASCIIToUTF16("UV"), font_list)) { - final_elided_url_string += BuildPathFromComponents( - base::string16(), url_path_elements, url_filename, 1); - } else { - final_elided_url_string += url_path; - } - - return ElideText(final_elided_url_string, font_list, available_pixel_width, - gfx::ELIDE_TAIL); -} - -base::string16 ElideHost(const GURL& url, - const gfx::FontList& font_list, - float available_pixel_width) { - base::string16 url_host; - base::string16 url_domain; - base::string16 url_subdomain; - SplitHost(url, &url_host, &url_domain, &url_subdomain); - - const float pixel_width_url_host = GetStringWidthF(url_host, font_list); - if (available_pixel_width >= pixel_width_url_host) - return url_host; - - if (url_subdomain.empty()) - return url_domain; - - const float pixel_width_url_domain = GetStringWidthF(url_domain, font_list); - float subdomain_width = available_pixel_width - pixel_width_url_domain; - if (subdomain_width <= 0) - return base::string16(kEllipsisUTF16) + kDot + url_domain; - - const base::string16 elided_subdomain = - ElideText(url_subdomain, font_list, subdomain_width, gfx::ELIDE_HEAD); - return elided_subdomain + url_domain; -} - -#endif // !defined(OS_ANDROID) - -base::string16 FormatUrlForSecurityDisplay(const GURL& url, - const std::string& languages) { - if (!url.is_valid() || url.is_empty() || !url.IsStandard()) - return net::FormatUrl(url, languages); - - const base::string16 colon(base::ASCIIToUTF16(":")); - const base::string16 scheme_separator( - base::ASCIIToUTF16(url::kStandardSchemeSeparator)); - - if (url.SchemeIsFile()) { - return base::ASCIIToUTF16(url::kFileScheme) + scheme_separator + - base::UTF8ToUTF16(url.path()); - } - - if (url.SchemeIsFileSystem()) { - const GURL* inner_url = url.inner_url(); - if (inner_url->SchemeIsFile()) { - return base::ASCIIToUTF16(url::kFileSystemScheme) + colon + - FormatUrlForSecurityDisplay(*inner_url, languages) + - base::UTF8ToUTF16(url.path()); - } - return base::ASCIIToUTF16(url::kFileSystemScheme) + colon + - FormatUrlForSecurityDisplay(*inner_url, languages); - } - - const GURL origin = url.GetOrigin(); - const std::string& scheme = origin.scheme(); - const std::string& host = origin.host(); - - base::string16 result = base::UTF8ToUTF16(scheme); - result += scheme_separator; - result += base::UTF8ToUTF16(host); - - const int port = origin.IntPort(); - const int default_port = url::DefaultPortForScheme( - scheme.c_str(), static_cast<int>(scheme.length())); - if (port != url::PORT_UNSPECIFIED && port != default_port) - result += colon + base::UTF8ToUTF16(origin.port()); - - return result; -} -} // namespace secure_display diff --git a/components/secure_display/elide_url.h b/components/secure_display/elide_url.h deleted file mode 100644 index 7d77b04..0000000 --- a/components/secure_display/elide_url.h +++ /dev/null @@ -1,72 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. -// -// This file defines utility functions for eliding URLs. - -#ifndef COMPONENTS_SECURE_DISPLAY_ELIDE_URL_H_ -#define COMPONENTS_SECURE_DISPLAY_ELIDE_URL_H_ - -#include <string> - -#include "base/strings/string16.h" - -class GURL; - -namespace gfx { -class FontList; -} - -namespace secure_display { - -// ElideUrl and Elide host require -// gfx::GetStringWidthF which is not implemented in Android -#if !defined(OS_ANDROID) -// This function takes a GURL object and elides it. It returns a string -// which composed of parts from subdomain, domain, path, filename and query. -// A "..." is added automatically at the end if the elided string is bigger -// than the |available_pixel_width|. For |available_pixel_width| == 0, a -// formatted, but un-elided, string is returned. |languages| is a comma -// separated list of ISO 639 language codes and is used to determine what -// characters are understood by a user. It should come from -// |prefs::kAcceptLanguages|. -// -// Note: in RTL locales, if the URL returned by this function is going to be -// displayed in the UI, then it is likely that the string needs to be marked -// as an LTR string (using base::i18n::WrapStringWithLTRFormatting()) so that it -// is displayed properly in an RTL context. Please refer to -// http://crbug.com/6487 for more information. -base::string16 ElideUrl(const GURL& url, - const gfx::FontList& font_list, - float available_pixel_width, - const std::string& languages); - -// This function takes a GURL object and elides the host to fit within -// the given width. The function will never elide past the TLD+1 point, -// but after that, will leading-elide the domain name to fit the width. -// Example: http://sub.domain.com ---> "...domain.com", or "...b.domain.com" -// depending on the width. -base::string16 ElideHost(const GURL& host_url, - const gfx::FontList& font_list, - float available_pixel_width); -#endif // !defined(OS_ANDROID) - -// This is a convenience function for formatting a URL in a concise and -// human-friendly way, to help users make security-related decisions (or in -// other circumstances when people need to distinguish sites, origins, or -// otherwise-simplified URLs from each other). -// -// Internationalized domain names (IDN) may be presented in Unicode if -// |languages| accepts the Unicode representation (see |net::FormatUrl| for more -// details on the algorithm). -// -// - Omits the path for standard schemes, excepting file and filesystem. -// - Omits the port if it is the default for the scheme. -// -// Do not use this for URLs which will be parsed or sent to other applications. -base::string16 FormatUrlForSecurityDisplay(const GURL& origin, - const std::string& languages); - -} // namespace secure_display - -#endif // COMPONENTS_SECURE_DISPLAY_ELIDE_URL_H_ diff --git a/components/secure_display/elide_url_unittest.cc b/components/secure_display/elide_url_unittest.cc deleted file mode 100644 index 71e1a5e..0000000 --- a/components/secure_display/elide_url_unittest.cc +++ /dev/null @@ -1,324 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/secure_display/elide_url.h" - -#include "base/ios/ios_util.h" -#include "base/strings/utf_string_conversions.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "ui/gfx/font_list.h" -#include "ui/gfx/text_elider.h" -#include "ui/gfx/text_utils.h" -#include "url/gurl.h" - -using base::UTF8ToUTF16; -using gfx::GetStringWidthF; -using gfx::kEllipsis; - -namespace { - -struct Testcase { - const std::string input; - const std::string output; -}; - -#if !defined(OS_ANDROID) -void RunUrlTest(Testcase* testcases, size_t num_testcases) { - static const gfx::FontList font_list; - for (size_t i = 0; i < num_testcases; ++i) { - const GURL url(testcases[i].input); - // Should we test with non-empty language list? - // That's kinda redundant with net_util_unittests. - const float available_width = - GetStringWidthF(UTF8ToUTF16(testcases[i].output), font_list); - EXPECT_EQ(UTF8ToUTF16(testcases[i].output), - secure_display::ElideUrl(url, font_list, available_width, - std::string())); - } -} - -// Test eliding of commonplace URLs. -TEST(TextEliderTest, TestGeneralEliding) { - const std::string kEllipsisStr(kEllipsis); - Testcase testcases[] = { - {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"}, - {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"}, - {"http://www.google.com/intl/en/ads/", - "google.com/intl/" + kEllipsisStr + "/ads/"}, - {"http://www.google.com/intl/en/ads/", - "google.com/" + kEllipsisStr + "/ads/"}, - {"http://www.google.com/intl/en/ads/", "google.com/" + kEllipsisStr}, - {"http://www.google.com/intl/en/ads/", "goog" + kEllipsisStr}, - {"https://subdomain.foo.com/bar/filename.html", - "subdomain.foo.com/bar/filename.html"}, - {"https://subdomain.foo.com/bar/filename.html", - "subdomain.foo.com/" + kEllipsisStr + "/filename.html"}, - {"http://subdomain.foo.com/bar/filename.html", - kEllipsisStr + "foo.com/" + kEllipsisStr + "/filename.html"}, - {"http://www.google.com/intl/en/ads/?aLongQueryWhichIsNotRequired", - "www.google.com/intl/en/ads/?aLongQ" + kEllipsisStr}, - }; - - RunUrlTest(testcases, arraysize(testcases)); -} - -// When there is very little space available, the elision code will shorten -// both path AND file name to an ellipsis - ".../...". To avoid this result, -// there is a hack in place that simply treats them as one string in this -// case. -TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) { - const std::string kEllipsisStr(kEllipsis); - - // Very little space, would cause double ellipsis. - gfx::FontList font_list; - GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html"); - float available_width = GetStringWidthF( - UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr), - font_list); - - // Create the expected string, after elision. Depending on font size, the - // directory might become /dir... or /di... or/d... - it never should be - // shorter than that. (If it is, the font considers d... to be longer - // than .../... - that should never happen). - ASSERT_GT(GetStringWidthF(UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr), - font_list), - GetStringWidthF(UTF8ToUTF16("d" + kEllipsisStr), font_list)); - GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc"); - base::string16 expected = secure_display::ElideUrl( - long_url, font_list, available_width, std::string()); - // Ensure that the expected result still contains part of the directory name. - ASSERT_GT(expected.length(), std::string("battersbox.com/d").length()); - EXPECT_EQ(expected, secure_display::ElideUrl(url, font_list, available_width, - std::string())); - - // More space available - elide directories, partially elide filename. - Testcase testcases[] = { - {"http://battersbox.com/directory/foo/peter_paul_and_mary.html", - "battersbox.com/" + kEllipsisStr + "/peter" + kEllipsisStr}, - }; - RunUrlTest(testcases, arraysize(testcases)); -} - -// Test eliding of empty strings, URLs with ports, passwords, queries, etc. -TEST(TextEliderTest, TestMoreEliding) { - const std::string kEllipsisStr(kEllipsis); - Testcase testcases[] = { - {"http://www.google.com/foo?bar", "www.google.com/foo?bar"}, - {"http://xyz.google.com/foo?bar", "xyz.google.com/foo?" + kEllipsisStr}, - {"http://xyz.google.com/foo?bar", "xyz.google.com/foo" + kEllipsisStr}, - {"http://xyz.google.com/foo?bar", "xyz.google.com/fo" + kEllipsisStr}, - {"http://a.b.com/pathname/c?d", "a.b.com/" + kEllipsisStr + "/c?d"}, - {"", ""}, - {"http://foo.bar..example.com...hello/test/filename.html", - "foo.bar..example.com...hello/" + kEllipsisStr + "/filename.html"}, - {"http://foo.bar../", "foo.bar.."}, - {"http://xn--1lq90i.cn/foo", "\xe5\x8c\x97\xe4\xba\xac.cn/foo"}, - {"http://me:mypass@secrethost.com:99/foo?bar#baz", - "secrethost.com:99/foo?bar#baz"}, - {"http://me:mypass@ss%xxfdsf.com/foo", "ss%25xxfdsf.com/foo"}, - {"mailto:elgoato@elgoato.com", "mailto:elgoato@elgoato.com"}, - {"javascript:click(0)", "javascript:click(0)"}, - {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename", - "chess.eecs.berkeley.edu:4430/login/arbitfilename"}, - {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename", - kEllipsisStr + "berkeley.edu:4430/" + kEllipsisStr + "/arbitfilename"}, - - // Unescaping. - {"http://www/%E4%BD%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0", - "www/\xe4\xbd\xa0\xe5\xa5\xbd?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"}, - - // Invalid unescaping for path. The ref will always be valid UTF-8. We - // don't - // bother to do too many edge cases, since these are handled by the - // escaper - // unittest. - {"http://www/%E4%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0", - "www/%E4%A0%E5%A5%BD?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"}, - }; - - RunUrlTest(testcases, arraysize(testcases)); -} - -// Test eliding of file: URLs. -TEST(TextEliderTest, TestFileURLEliding) { - const std::string kEllipsisStr(kEllipsis); - Testcase testcases[] = { - {"file:///C:/path1/path2/path3/filename", - "file:///C:/path1/path2/path3/filename"}, - {"file:///C:/path1/path2/path3/filename", "C:/path1/path2/path3/filename"}, -// GURL parses "file:///C:path" differently on windows than it does on posix. -#if defined(OS_WIN) - {"file:///C:path1/path2/path3/filename", - "C:/path1/path2/" + kEllipsisStr + "/filename"}, - {"file:///C:path1/path2/path3/filename", - "C:/path1/" + kEllipsisStr + "/filename"}, - {"file:///C:path1/path2/path3/filename", - "C:/" + kEllipsisStr + "/filename"}, -#endif // defined(OS_WIN) - {"file://filer/foo/bar/file", "filer/foo/bar/file"}, - {"file://filer/foo/bar/file", "filer/foo/" + kEllipsisStr + "/file"}, - {"file://filer/foo/bar/file", "filer/" + kEllipsisStr + "/file"}, - {"file://filer/foo/", "file://filer/foo/"}, - {"file://filer/foo/", "filer/foo/"}, - {"file://filer/foo/", "filer" + kEllipsisStr}, - // Eliding file URLs with nothing after the ':' shouldn't crash. - {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:", "aaa" + kEllipsisStr}, - {"file:///aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:/", "aaa" + kEllipsisStr}, - }; - - RunUrlTest(testcases, arraysize(testcases)); -} - -TEST(TextEliderTest, TestHostEliding) { -#if defined(OS_IOS) - // TODO(eugenebut): Disable test on iOS9 crbug.com/513703 - if (base::ios::IsRunningOnIOS9OrLater()) { - LOG(WARNING) << "Test disabled on iOS9."; - return; - } -#endif - const std::string kEllipsisStr(kEllipsis); - Testcase testcases[] = { - {"http://google.com", "google.com"}, - {"http://subdomain.google.com", kEllipsisStr + ".google.com"}, - {"http://reallyreallyreallylongdomainname.com", - "reallyreallyreallylongdomainname.com"}, - {"http://a.b.c.d.e.f.com", kEllipsisStr + "f.com"}, - {"http://foo", "foo"}, - {"http://foo.bar", "foo.bar"}, - {"http://subdomain.foo.bar", kEllipsisStr + "in.foo.bar"}, -// IOS width calculations are off by a letter from other platforms for -// some strings from other platforms, probably for strings with too -// many kerned letters on the default font set. -#if !defined(OS_IOS) - {"http://subdomain.reallylongdomainname.com", - kEllipsisStr + "ain.reallylongdomainname.com"}, - {"http://a.b.c.d.e.f.com", kEllipsisStr + ".e.f.com"}, -#endif // !defined(OS_IOS) - }; - - for (size_t i = 0; i < arraysize(testcases); ++i) { - const float available_width = - GetStringWidthF(UTF8ToUTF16(testcases[i].output), gfx::FontList()); - EXPECT_EQ(UTF8ToUTF16(testcases[i].output), - secure_display::ElideHost(GURL(testcases[i].input), - gfx::FontList(), available_width)); - } - - // Trying to elide to a really short length will still keep the full TLD+1 - EXPECT_EQ( - base::ASCIIToUTF16("google.com"), - secure_display::ElideHost(GURL("http://google.com"), gfx::FontList(), 2)); - EXPECT_EQ(base::UTF8ToUTF16(kEllipsisStr + ".google.com"), - secure_display::ElideHost(GURL("http://subdomain.google.com"), - gfx::FontList(), 2)); - EXPECT_EQ( - base::ASCIIToUTF16("foo.bar"), - secure_display::ElideHost(GURL("http://foo.bar"), gfx::FontList(), 2)); -} - -#endif // !defined(OS_ANDROID) - -TEST(TextEliderTest, FormatUrlForSecurityDisplay) { - struct OriginTestData { - const char* const description; - const char* const input; - const wchar_t* const output; - }; - - const OriginTestData tests[] = { - {"Empty URL", "", L""}, - {"HTTP URL", "http://www.google.com/", L"http://www.google.com"}, - {"HTTPS URL", "https://www.google.com/", L"https://www.google.com"}, - {"Standard HTTP port", "http://www.google.com:80/", - L"http://www.google.com"}, - {"Standard HTTPS port", "https://www.google.com:443/", - L"https://www.google.com"}, - {"Standard HTTP port, IDN Chinese", - "http://\xe4\xb8\xad\xe5\x9b\xbd.icom.museum:80", - L"http://xn--fiqs8s.icom.museum"}, - {"HTTP URL, IDN Hebrew (RTL)", - "http://" - "\xd7\x90\xd7\x99\xd7\xa7\xd7\x95\xd7\xb4\xd7\x9d." - "\xd7\x99\xd7\xa9\xd7\xa8\xd7\x90\xd7\x9c.museum/", - L"http://xn--4dbklr2c8d.xn--4dbrk0ce.museum"}, - {"HTTP URL with query string, IDN Arabic (RTL)", - "http://\xd9\x85\xd8\xb5\xd8\xb1.icom.museum/foo.html?yes=no", - L"http://xn--wgbh1c.icom.museum"}, - {"Non-standard HTTP port", "http://www.google.com:9000/", - L"http://www.google.com:9000"}, - {"Non-standard HTTPS port", "https://www.google.com:9000/", - L"https://www.google.com:9000"}, - {"File URI", "file:///usr/example/file.html", - L"file:///usr/example/file.html"}, - {"File URI with hostname", "file://localhost/usr/example/file.html", - L"file:///usr/example/file.html"}, - {"UNC File URI 1", "file:///CONTOSO/accounting/money.xls", - L"file:///CONTOSO/accounting/money.xls"}, - {"UNC File URI 2", - "file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO", - L"file:///C:/Program%20Files/Music/Web%20Sys/main.html"}, - {"HTTP URL with path", "http://www.google.com/test.html", - L"http://www.google.com"}, - {"HTTPS URL with path", "https://www.google.com/test.html", - L"https://www.google.com"}, - {"Unusual secure scheme (wss)", "wss://www.google.com/", - L"wss://www.google.com"}, - {"Unusual non-secure scheme (gopher)", "gopher://www.google.com/", - L"gopher://www.google.com"}, - {"Unlisted scheme (chrome)", "chrome://version", L"chrome://version"}, - {"HTTP IP address", "http://173.194.65.103", L"http://173.194.65.103"}, - {"HTTPS IP address", "https://173.194.65.103", L"https://173.194.65.103"}, - {"HTTP IPv6 address", "http://[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]/", - L"http://[fe80::202:b3ff:fe1e:8329]"}, - {"HTTPS IPv6 address with port", "https://[2001:db8:0:1]:443/", - L"https://[2001:db8:0:1]"}, - {"HTTPS IP address, non-default port", "https://173.194.65.103:8443", - L"https://173.194.65.103:8443"}, - {"HTTP filesystem: URL with path", - "filesystem:http://www.google.com/temporary/test.html", - L"filesystem:http://www.google.com"}, - {"File filesystem: URL with path", - "filesystem:file://localhost/temporary/stuff/test.html?z=fun&goat=billy", - L"filesystem:file:///temporary/stuff/test.html"}, - {"Invalid scheme 1", "twelve://www.cyber.org/wow.php", - L"twelve://www.cyber.org/wow.php"}, - {"Invalid scheme 2", "://www.cyber.org/wow.php", - L"://www.cyber.org/wow.php"}, - {"Invalid host 1", "https://www.cyber../wow.php", L"https://www.cyber.."}, - {"Invalid host 2", "https://www...cyber/wow.php", L"https://www...cyber"}, - {"Invalid port 1", "https://173.194.65.103:000", - L"https://173.194.65.103:0"}, - {"Invalid port 2", "https://173.194.65.103:gruffle", - L"https://173.194.65.103:gruffle"}, - {"Invalid port 3", "https://173.194.65.103:/hello.aspx", - L"https://173.194.65.103"}, - {"Trailing dot in DNS name", "https://www.example.com./get/goat", - L"https://www.example.com."}, - {"Blob URL", - "blob:http%3A//www.html5rocks.com/4d4ff040-6d61-4446-86d3-13ca07ec9ab9", - L"blob:http%3A//www.html5rocks.com/" - L"4d4ff040-6d61-4446-86d3-13ca07ec9ab9"}, - }; - - const char languages[] = "zh-TW,en-US,en,am,ar-EG,ar"; - for (size_t i = 0; i < arraysize(tests); ++i) { - base::string16 formatted = secure_display::FormatUrlForSecurityDisplay( - GURL(tests[i].input), std::string()); - EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted) - << tests[i].description; - base::string16 formatted_with_languages = - secure_display::FormatUrlForSecurityDisplay(GURL(tests[i].input), - languages); - EXPECT_EQ(base::WideToUTF16(tests[i].output), formatted_with_languages) - << tests[i].description; - } - - base::string16 formatted = - secure_display::FormatUrlForSecurityDisplay(GURL(), std::string()); - EXPECT_EQ(base::string16(), formatted) - << "Explicitly test the 0-argument GURL constructor"; -} - -} // namespace |