diff options
Diffstat (limited to 'net/base/net_util_unittest.cc')
-rw-r--r-- | net/base/net_util_unittest.cc | 205 |
1 files changed, 161 insertions, 44 deletions
diff --git a/net/base/net_util_unittest.cc b/net/base/net_util_unittest.cc index 0d10bb1..47fe251 100644 --- a/net/base/net_util_unittest.cc +++ b/net/base/net_util_unittest.cc @@ -364,7 +364,7 @@ struct UrlTestData { const char* description; const char* input; const std::wstring languages; - bool omit; + net::FormatUrlTypes format_types; UnescapeRule::Type escape_rules; const std::wstring output; size_t prefix_len; @@ -1267,74 +1267,78 @@ TEST(NetUtilTest, GetHostName) { } TEST(NetUtilTest, FormatUrl) { + net::FormatUrlTypes default_format_type = net::kFormatUrlOmitUsernamePassword; const UrlTestData tests[] = { - {"Empty URL", "", L"", true, UnescapeRule::NORMAL, L"", 0}, + {"Empty URL", "", L"", default_format_type, UnescapeRule::NORMAL, L"", 0}, {"Simple URL", - "http://www.google.com/", L"", true, UnescapeRule::NORMAL, + "http://www.google.com/", L"", default_format_type, UnescapeRule::NORMAL, L"http://www.google.com/", 7}, {"With a port number and a reference", - "http://www.google.com:8080/#\xE3\x82\xB0", L"", true, + "http://www.google.com:8080/#\xE3\x82\xB0", L"", default_format_type, UnescapeRule::NORMAL, L"http://www.google.com:8080/#\x30B0", 7}, // -------- IDN tests -------- {"Japanese IDN with ja", - "http://xn--l8jvb1ey91xtjb.jp", L"ja", true, UnescapeRule::NORMAL, - L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7}, + "http://xn--l8jvb1ey91xtjb.jp", L"ja", default_format_type, + UnescapeRule::NORMAL, L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7}, {"Japanese IDN with en", - "http://xn--l8jvb1ey91xtjb.jp", L"en", true, UnescapeRule::NORMAL, - L"http://xn--l8jvb1ey91xtjb.jp/", 7}, + "http://xn--l8jvb1ey91xtjb.jp", L"en", default_format_type, + UnescapeRule::NORMAL, L"http://xn--l8jvb1ey91xtjb.jp/", 7}, {"Japanese IDN without any languages", - "http://xn--l8jvb1ey91xtjb.jp", L"", true, UnescapeRule::NORMAL, + "http://xn--l8jvb1ey91xtjb.jp", L"", default_format_type, + UnescapeRule::NORMAL, // Single script is safe for empty languages. L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7}, {"mailto: with Japanese IDN", - "mailto:foo@xn--l8jvb1ey91xtjb.jp", L"ja", true, UnescapeRule::NORMAL, + "mailto:foo@xn--l8jvb1ey91xtjb.jp", L"ja", default_format_type, + UnescapeRule::NORMAL, // GURL doesn't assume an email address's domain part as a host name. L"mailto:foo@xn--l8jvb1ey91xtjb.jp", 7}, {"file: with Japanese IDN", - "file://xn--l8jvb1ey91xtjb.jp/config.sys", L"ja", true, + "file://xn--l8jvb1ey91xtjb.jp/config.sys", L"ja", default_format_type, UnescapeRule::NORMAL, L"file://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 7}, {"ftp: with Japanese IDN", - "ftp://xn--l8jvb1ey91xtjb.jp/config.sys", L"ja", true, + "ftp://xn--l8jvb1ey91xtjb.jp/config.sys", L"ja", default_format_type, UnescapeRule::NORMAL, L"ftp://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 6}, // -------- omit_username_password flag tests -------- {"With username and password, omit_username_password=false", - "http://user:passwd@example.com/foo", L"", false, UnescapeRule::NORMAL, + "http://user:passwd@example.com/foo", L"", + net::kFormatUrlOmitNothing, UnescapeRule::NORMAL, L"http://user:passwd@example.com/foo", 19}, {"With username and password, omit_username_password=true", - "http://user:passwd@example.com/foo", L"", true, UnescapeRule::NORMAL, - L"http://example.com/foo", 7}, + "http://user:passwd@example.com/foo", L"", default_format_type, + UnescapeRule::NORMAL, L"http://example.com/foo", 7}, {"With username and no password", - "http://user@example.com/foo", L"", true, UnescapeRule::NORMAL, - L"http://example.com/foo", 7}, + "http://user@example.com/foo", L"", default_format_type, + UnescapeRule::NORMAL, L"http://example.com/foo", 7}, {"Just '@' without username and password", - "http://@example.com/foo", L"", true, UnescapeRule::NORMAL, + "http://@example.com/foo", L"", default_format_type, UnescapeRule::NORMAL, L"http://example.com/foo", 7}, // GURL doesn't think local-part of an email address is username for URL. {"mailto:, omit_username_password=true", - "mailto:foo@example.com", L"", true, UnescapeRule::NORMAL, + "mailto:foo@example.com", L"", default_format_type, UnescapeRule::NORMAL, L"mailto:foo@example.com", 7}, // -------- unescape flag tests -------- {"Do not unescape", "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/" "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" - "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", L"en", true, + "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", L"en", default_format_type, UnescapeRule::NONE, // GURL parses %-encoded hostnames into Punycode. L"http://xn--qcka1pmc.jp/%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" @@ -1343,38 +1347,59 @@ TEST(NetUtilTest, FormatUrl) { {"Unescape normally", "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/" "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" - "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", L"en", true, + "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", L"en", default_format_type, UnescapeRule::NORMAL, L"http://xn--qcka1pmc.jp/\x30B0\x30FC\x30B0\x30EB" L"?q=\x30B0\x30FC\x30B0\x30EB", 7}, {"Unescape normally including unescape spaces", - "http://www.google.com/search?q=Hello%20World", L"en", true, - UnescapeRule::SPACES, - L"http://www.google.com/search?q=Hello World", 7}, + "http://www.google.com/search?q=Hello%20World", L"en", default_format_type, + UnescapeRule::SPACES, L"http://www.google.com/search?q=Hello World", 7}, /* {"unescape=true with some special characters", - "http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", L"", false, true, + "http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", L"", + net::kFormatUrlOmitNothing, UnescapeRule::NORMAL, L"http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", 25}, */ // Disabled: the resultant URL becomes "...user%253A:%2540passwd...". // -------- view-source: -------- {"view-source", - "view-source:http://xn--qcka1pmc.jp/", L"ja", true, UnescapeRule::NORMAL, - L"view-source:http://\x30B0\x30FC\x30B0\x30EB.jp/", 12 + 7}, + "view-source:http://xn--qcka1pmc.jp/", L"ja", default_format_type, + UnescapeRule::NORMAL, L"view-source:http://\x30B0\x30FC\x30B0\x30EB.jp/", + 12 + 7}, {"view-source of view-source", - "view-source:view-source:http://xn--qcka1pmc.jp/", L"ja", true, - UnescapeRule::NORMAL, + "view-source:view-source:http://xn--qcka1pmc.jp/", L"ja", + default_format_type, UnescapeRule::NORMAL, L"view-source:view-source:http://xn--qcka1pmc.jp/", 12}, + + // -------- omit http: -------- + {"omit http with user name", + "http://user@example.com/foo", L"", net::kFormatUrlOmitAll, + UnescapeRule::NORMAL, L"example.com/foo", 0}, + + {"omit http", + "http://www.google.com/", L"en", net::kFormatUrlOmitHTTP, + UnescapeRule::NORMAL, L"www.google.com/", + 0}, + + {"omit http with https", + "https://www.google.com/", L"en", net::kFormatUrlOmitHTTP, + UnescapeRule::NORMAL, L"https://www.google.com/", + 8}, + + {"omit http starts with ftp.", + "http://ftp.google.com/", L"en", net::kFormatUrlOmitHTTP, + UnescapeRule::NORMAL, L"http://ftp.google.com/", + 7}, }; for (size_t i = 0; i < arraysize(tests); ++i) { size_t prefix_len; std::wstring formatted = net::FormatUrl( - GURL(tests[i].input), tests[i].languages, tests[i].omit, + GURL(tests[i].input), tests[i].languages, tests[i].format_types, tests[i].escape_rules, NULL, &prefix_len, NULL); EXPECT_EQ(tests[i].output, formatted) << tests[i].description; EXPECT_EQ(tests[i].prefix_len, prefix_len) << tests[i].description; @@ -1387,7 +1412,8 @@ TEST(NetUtilTest, FormatUrlParsed) { std::wstring formatted = net::FormatUrl( GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), - L"ja", false, UnescapeRule::NONE, &parsed, NULL, NULL); + L"ja", net::kFormatUrlOmitNothing, UnescapeRule::NONE, &parsed, NULL, + NULL); EXPECT_EQ(L"http://%E3%82%B0:%E3%83%BC@\x30B0\x30FC\x30B0\x30EB.jp:8080" L"/%E3%82%B0/?q=%E3%82%B0#\x30B0", formatted); EXPECT_EQ(L"%E3%82%B0", @@ -1407,7 +1433,8 @@ TEST(NetUtilTest, FormatUrlParsed) { formatted = net::FormatUrl( GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), - L"ja", false, UnescapeRule::NORMAL, &parsed, NULL, NULL); + L"ja", net::kFormatUrlOmitNothing, UnescapeRule::NORMAL, &parsed, NULL, + NULL); EXPECT_EQ(L"http://\x30B0:\x30FC@\x30B0\x30FC\x30B0\x30EB.jp:8080" L"/\x30B0/?q=\x30B0#\x30B0", formatted); EXPECT_EQ(L"\x30B0", @@ -1426,7 +1453,8 @@ TEST(NetUtilTest, FormatUrlParsed) { formatted = net::FormatUrl( GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), - L"ja", true, UnescapeRule::NORMAL, &parsed, NULL, NULL); + L"ja", net::kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, + &parsed, NULL, NULL); EXPECT_EQ(L"http://\x30B0\x30FC\x30B0\x30EB.jp:8080" L"/\x30B0/?q=\x30B0#\x30B0", formatted); EXPECT_FALSE(parsed.username.is_valid()); @@ -1442,7 +1470,8 @@ TEST(NetUtilTest, FormatUrlParsed) { // View-source case. formatted = net::FormatUrl( GURL("view-source:http://user:passwd@host:81/path?query#ref"), - L"", true, UnescapeRule::NORMAL, &parsed, NULL, NULL); + L"", net::kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, &parsed, + NULL, NULL); EXPECT_EQ(L"view-source:http://host:81/path?query#ref", formatted); EXPECT_EQ(L"view-source:http", formatted.substr(parsed.scheme.begin, parsed.scheme.len)); @@ -1453,6 +1482,50 @@ TEST(NetUtilTest, FormatUrlParsed) { EXPECT_EQ(L"/path", formatted.substr(parsed.path.begin, parsed.path.len)); EXPECT_EQ(L"query", formatted.substr(parsed.query.begin, parsed.query.len)); EXPECT_EQ(L"ref", formatted.substr(parsed.ref.begin, parsed.ref.len)); + + // omit http case. + formatted = net::FormatUrl( + GURL("http://host:8000/a?b=c#d"), + L"", net::kFormatUrlOmitHTTP, UnescapeRule::NORMAL, &parsed, NULL, NULL); + EXPECT_EQ(L"host:8000/a?b=c#d", formatted); + EXPECT_FALSE(parsed.scheme.is_valid()); + EXPECT_FALSE(parsed.username.is_valid()); + EXPECT_FALSE(parsed.password.is_valid()); + EXPECT_EQ(L"host", formatted.substr(parsed.host.begin, parsed.host.len)); + EXPECT_EQ(L"8000", formatted.substr(parsed.port.begin, parsed.port.len)); + EXPECT_EQ(L"/a", formatted.substr(parsed.path.begin, parsed.path.len)); + EXPECT_EQ(L"b=c", formatted.substr(parsed.query.begin, parsed.query.len)); + EXPECT_EQ(L"d", formatted.substr(parsed.ref.begin, parsed.ref.len)); + + // omit http starts with ftp case. + formatted = net::FormatUrl( + GURL("http://ftp.host:8000/a?b=c#d"), + L"", net::kFormatUrlOmitHTTP, UnescapeRule::NORMAL, &parsed, NULL, NULL); + EXPECT_EQ(L"http://ftp.host:8000/a?b=c#d", formatted); + EXPECT_TRUE(parsed.scheme.is_valid()); + EXPECT_FALSE(parsed.username.is_valid()); + EXPECT_FALSE(parsed.password.is_valid()); + EXPECT_EQ(L"http", formatted.substr(parsed.scheme.begin, parsed.scheme.len)); + EXPECT_EQ(L"ftp.host", formatted.substr(parsed.host.begin, parsed.host.len)); + EXPECT_EQ(L"8000", formatted.substr(parsed.port.begin, parsed.port.len)); + EXPECT_EQ(L"/a", formatted.substr(parsed.path.begin, parsed.path.len)); + EXPECT_EQ(L"b=c", formatted.substr(parsed.query.begin, parsed.query.len)); + EXPECT_EQ(L"d", formatted.substr(parsed.ref.begin, parsed.ref.len)); + + // omit http starts with 'f' case. + formatted = net::FormatUrl( + GURL("http://f/"), + L"", net::kFormatUrlOmitHTTP, UnescapeRule::NORMAL, &parsed, NULL, NULL); + EXPECT_EQ(L"f/", formatted); + EXPECT_FALSE(parsed.scheme.is_valid()); + EXPECT_FALSE(parsed.username.is_valid()); + EXPECT_FALSE(parsed.password.is_valid()); + EXPECT_FALSE(parsed.port.is_valid()); + EXPECT_TRUE(parsed.path.is_valid()); + EXPECT_FALSE(parsed.query.is_valid()); + EXPECT_FALSE(parsed.ref.is_valid()); + EXPECT_EQ(L"f", formatted.substr(parsed.host.begin, parsed.host.len)); + EXPECT_EQ(L"/", formatted.substr(parsed.path.begin, parsed.path.len)); } TEST(NetUtilTest, FormatUrlAdjustOffset) { @@ -1472,8 +1545,9 @@ TEST(NetUtilTest, FormatUrlAdjustOffset) { }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(basic_cases); ++i) { size_t offset = basic_cases[i].input_offset; - net::FormatUrl(GURL("http://www.google.com/foo/"), L"en", true, - UnescapeRule::NORMAL, NULL, NULL, &offset); + net::FormatUrl(GURL("http://www.google.com/foo/"), L"en", + net::kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, + NULL, NULL, &offset); EXPECT_EQ(basic_cases[i].output_offset, offset); } @@ -1495,8 +1569,9 @@ TEST(NetUtilTest, FormatUrlAdjustOffset) { }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(omit_auth_cases); ++i) { size_t offset = omit_auth_cases[i].input_offset; - net::FormatUrl(GURL(omit_auth_cases[i].input_url), L"en", true, - UnescapeRule::NORMAL, NULL, NULL, &offset); + net::FormatUrl(GURL(omit_auth_cases[i].input_url), L"en", + net::kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, + NULL, NULL, &offset); EXPECT_EQ(omit_auth_cases[i].output_offset, offset); } @@ -1514,8 +1589,9 @@ TEST(NetUtilTest, FormatUrlAdjustOffset) { }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(view_source_cases); ++i) { size_t offset = view_source_cases[i].input_offset; - net::FormatUrl(GURL("view-source:http://foo@www.google.com/"), L"en", true, - UnescapeRule::NORMAL, NULL, NULL, &offset); + net::FormatUrl(GURL("view-source:http://foo@www.google.com/"), L"en", + net::kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, + NULL, NULL, &offset); EXPECT_EQ(view_source_cases[i].output_offset, offset); } @@ -1529,8 +1605,9 @@ TEST(NetUtilTest, FormatUrlAdjustOffset) { for (size_t i = 0; i < ARRAYSIZE_UNSAFE(idn_hostname_cases); ++i) { size_t offset = idn_hostname_cases[i].input_offset; // "http://\x671d\x65e5\x3042\x3055\x3072.jp/foo/" - net::FormatUrl(GURL("http://xn--l8jvb1ey91xtjb.jp/foo/"), L"ja", true, - UnescapeRule::NORMAL, NULL, NULL, &offset); + net::FormatUrl(GURL("http://xn--l8jvb1ey91xtjb.jp/foo/"), L"ja", + net::kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, + NULL, NULL, &offset); EXPECT_EQ(idn_hostname_cases[i].output_offset, offset); } @@ -1551,7 +1628,8 @@ TEST(NetUtilTest, FormatUrlAdjustOffset) { // "http://www.google.com/foo bar/\x30B0\x30FC\x30B0\x30EB" net::FormatUrl(GURL( "http://www.google.com/foo%20bar/%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB"), - L"en", true, UnescapeRule::SPACES, NULL, NULL, &offset); + L"en", net::kFormatUrlOmitUsernamePassword, UnescapeRule::SPACES, NULL, + NULL, &offset); EXPECT_EQ(unescape_cases[i].output_offset, offset); } @@ -1568,9 +1646,48 @@ TEST(NetUtilTest, FormatUrlAdjustOffset) { // "http://www.google.com/foo.html#\x30B0\x30B0z" net::FormatUrl(GURL( "http://www.google.com/foo.html#\xE3\x82\xB0\xE3\x82\xB0z"), L"en", - true, UnescapeRule::NORMAL, NULL, NULL, &offset); + net::kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, NULL, NULL, + &offset); EXPECT_EQ(ref_cases[i].output_offset, offset); } + + const AdjustOffsetCase omit_http_cases[] = { + {0, std::wstring::npos}, + {3, std::wstring::npos}, + {7, 0}, + {8, 1}, + }; + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(omit_http_cases); ++i) { + size_t offset = omit_http_cases[i].input_offset; + net::FormatUrl(GURL("http://www.google.com"), L"en", + net::kFormatUrlOmitHTTP, UnescapeRule::NORMAL, NULL, NULL, &offset); + EXPECT_EQ(omit_http_cases[i].output_offset, offset); + } + + const AdjustOffsetCase omit_http_start_with_ftp[] = { + {0, 0}, + {3, 3}, + {8, 8}, + }; + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(omit_http_start_with_ftp); ++i) { + size_t offset = omit_http_start_with_ftp[i].input_offset; + net::FormatUrl(GURL("http://ftp.google.com"), L"en", + net::kFormatUrlOmitHTTP, UnescapeRule::NORMAL, NULL, NULL, &offset); + EXPECT_EQ(omit_http_start_with_ftp[i].output_offset, offset); + } + + const AdjustOffsetCase omit_all_cases[] = { + {12, 0}, + {13, 1}, + {0, std::wstring::npos}, + {3, std::wstring::npos}, + }; + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(omit_all_cases); ++i) { + size_t offset = omit_all_cases[i].input_offset; + net::FormatUrl(GURL("http://user@foo.com/"), L"en", net::kFormatUrlOmitAll, + UnescapeRule::NORMAL, NULL, NULL, &offset); + EXPECT_EQ(omit_all_cases[i].output_offset, offset); + } } TEST(NetUtilTest, SimplifyUrlForRequest) { |