[email protected] | 51bcc5d | 2013-04-24 01:41:37 | [diff] [blame] | 1 | // Copyright 2013 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. |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 4 | |
| 5 | #include <errno.h> |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame] | 6 | #include <stddef.h> |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 7 | |
[email protected] | 847aaab8 | 2014-05-07 14:05:46 | [diff] [blame] | 8 | #include "base/macros.h" |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 9 | #include "base/strings/utf_string_conversions.h" |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
tfarina | 018de6e | 2015-05-26 17:41:20 | [diff] [blame] | 11 | #include "url/third_party/mozilla/url_parse.h" |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 12 | #include "url/url_canon.h" |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 13 | #include "url/url_canon_internal.h" |
| 14 | #include "url/url_canon_stdstring.h" |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 15 | #include "url/url_test_utils.h" |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 16 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 17 | namespace url { |
| 18 | |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 19 | namespace { |
| 20 | |
| 21 | struct ComponentCase { |
| 22 | const char* input; |
| 23 | const char* expected; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 24 | Component expected_component; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 25 | bool expected_success; |
| 26 | }; |
| 27 | |
| 28 | // ComponentCase but with dual 8-bit/16-bit input. Generally, the unit tests |
| 29 | // treat each input as optional, and will only try processing if non-NULL. |
| 30 | // The output is always 8-bit. |
| 31 | struct DualComponentCase { |
| 32 | const char* input8; |
| 33 | const wchar_t* input16; |
| 34 | const char* expected; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 35 | Component expected_component; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 36 | bool expected_success; |
| 37 | }; |
| 38 | |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 39 | // Test cases for CanonicalizeIPAddress(). The inputs are identical to |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 40 | // DualComponentCase, but the output has extra CanonHostInfo fields. |
| 41 | struct IPAddressCase { |
| 42 | const char* input8; |
| 43 | const wchar_t* input16; |
| 44 | const char* expected; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 45 | Component expected_component; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 46 | |
| 47 | // CanonHostInfo fields, for verbose output. |
| 48 | CanonHostInfo::Family expected_family; |
| 49 | int expected_num_ipv4_components; |
| 50 | const char* expected_address_hex; // Two hex chars per IP address byte. |
| 51 | }; |
| 52 | |
| 53 | std::string BytesToHexString(unsigned char bytes[16], int length) { |
| 54 | EXPECT_TRUE(length == 0 || length == 4 || length == 16) |
| 55 | << "Bad IP address length: " << length; |
| 56 | std::string result; |
| 57 | for (int i = 0; i < length; ++i) { |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 58 | result.push_back(kHexCharLookup[(bytes[i] >> 4) & 0xf]); |
| 59 | result.push_back(kHexCharLookup[bytes[i] & 0xf]); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 60 | } |
| 61 | return result; |
| 62 | } |
| 63 | |
| 64 | struct ReplaceCase { |
| 65 | const char* base; |
| 66 | const char* scheme; |
| 67 | const char* username; |
| 68 | const char* password; |
| 69 | const char* host; |
| 70 | const char* port; |
| 71 | const char* path; |
| 72 | const char* query; |
| 73 | const char* ref; |
| 74 | const char* expected; |
| 75 | }; |
| 76 | |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 77 | // Magic string used in the replacements code that tells SetupReplComp to |
| 78 | // call the clear function. |
| 79 | const char kDeleteComp[] = "|"; |
| 80 | |
| 81 | // Sets up a replacement for a single component. This is given pointers to |
| 82 | // the set and clear function for the component being replaced, and will |
| 83 | // either set the component (if it exists) or clear it (if the replacement |
| 84 | // string matches kDeleteComp). |
| 85 | // |
| 86 | // This template is currently used only for the 8-bit case, and the strlen |
| 87 | // causes it to fail in other cases. It is left a template in case we have |
| 88 | // tests for wide replacements. |
| 89 | template<typename CHAR> |
| 90 | void SetupReplComp( |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 91 | void (Replacements<CHAR>::*set)(const CHAR*, const Component&), |
| 92 | void (Replacements<CHAR>::*clear)(), |
| 93 | Replacements<CHAR>* rep, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 94 | const CHAR* str) { |
| 95 | if (str && str[0] == kDeleteComp[0]) { |
| 96 | (rep->*clear)(); |
| 97 | } else if (str) { |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 98 | (rep->*set)(str, Component(0, static_cast<int>(strlen(str)))); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | } // namespace |
| 103 | |
| 104 | TEST(URLCanonTest, DoAppendUTF8) { |
| 105 | struct UTF8Case { |
| 106 | unsigned input; |
| 107 | const char* output; |
| 108 | } utf_cases[] = { |
| 109 | // Valid code points. |
| 110 | {0x24, "\x24"}, |
| 111 | {0xA2, "\xC2\xA2"}, |
| 112 | {0x20AC, "\xE2\x82\xAC"}, |
| 113 | {0x24B62, "\xF0\xA4\xAD\xA2"}, |
| 114 | {0x10FFFF, "\xF4\x8F\xBF\xBF"}, |
| 115 | }; |
| 116 | std::string out_str; |
viettrungluu | 4b691586 | 2014-10-16 03:42:49 | [diff] [blame] | 117 | for (size_t i = 0; i < arraysize(utf_cases); i++) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 118 | out_str.clear(); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 119 | StdStringCanonOutput output(&out_str); |
| 120 | AppendUTF8Value(utf_cases[i].input, &output); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 121 | output.Complete(); |
| 122 | EXPECT_EQ(utf_cases[i].output, out_str); |
| 123 | } |
| 124 | } |
| 125 | |
[email protected] | 7c0fccb3 | 2014-06-17 00:50:40 | [diff] [blame] | 126 | #if defined(GTEST_HAS_DEATH_TEST) |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 127 | // TODO(mattm): Can't run this in debug mode for now, since the DCHECK will |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 128 | // cause the Chromium stack trace dialog to appear and hang the test. |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 129 | // See http://crbug.com/49580. |
[email protected] | 7c0fccb3 | 2014-06-17 00:50:40 | [diff] [blame] | 130 | #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 131 | #define MAYBE_DoAppendUTF8Invalid DoAppendUTF8Invalid |
| 132 | #else |
| 133 | #define MAYBE_DoAppendUTF8Invalid DISABLED_DoAppendUTF8Invalid |
| 134 | #endif |
| 135 | TEST(URLCanonTest, MAYBE_DoAppendUTF8Invalid) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 136 | std::string out_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 137 | StdStringCanonOutput output(&out_str); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 138 | // Invalid code point (too large). |
| 139 | ASSERT_DEBUG_DEATH({ |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 140 | AppendUTF8Value(0x110000, &output); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 141 | output.Complete(); |
| 142 | EXPECT_EQ("", out_str); |
| 143 | }, ""); |
| 144 | } |
[email protected] | 7c0fccb3 | 2014-06-17 00:50:40 | [diff] [blame] | 145 | #endif // defined(GTEST_HAS_DEATH_TEST) |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 146 | |
| 147 | TEST(URLCanonTest, UTF) { |
| 148 | // Low-level test that we handle reading, canonicalization, and writing |
| 149 | // UTF-8/UTF-16 strings properly. |
| 150 | struct UTFCase { |
| 151 | const char* input8; |
| 152 | const wchar_t* input16; |
| 153 | bool expected_success; |
| 154 | const char* output; |
| 155 | } utf_cases[] = { |
| 156 | // Valid canonical input should get passed through & escaped. |
| 157 | {"\xe4\xbd\xa0\xe5\xa5\xbd", L"\x4f60\x597d", true, "%E4%BD%A0%E5%A5%BD"}, |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 158 | // Test a character that takes > 16 bits (U+10300 = old italic letter A) |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 159 | {"\xF0\x90\x8C\x80", L"\xd800\xdf00", true, "%F0%90%8C%80"}, |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 160 | // Non-shortest-form UTF-8 characters are invalid. The bad character |
| 161 | // should be replaced with the invalid character (EF BF DB in UTF-8). |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 162 | {"\xf0\x84\xbd\xa0\xe5\xa5\xbd", NULL, false, "%EF%BF%BD%E5%A5%BD"}, |
| 163 | // Invalid UTF-8 sequences should be marked as invalid (the first |
| 164 | // sequence is truncated). |
| 165 | {"\xe4\xa0\xe5\xa5\xbd", L"\xd800\x597d", false, "%EF%BF%BD%E5%A5%BD"}, |
| 166 | // Character going off the end. |
| 167 | {"\xe4\xbd\xa0\xe5\xa5", L"\x4f60\xd800", false, "%E4%BD%A0%EF%BF%BD"}, |
| 168 | // ...same with low surrogates with no high surrogate. |
| 169 | {"\xed\xb0\x80", L"\xdc00", false, "%EF%BF%BD"}, |
| 170 | // Test a UTF-8 encoded surrogate value is marked as invalid. |
| 171 | // ED A0 80 = U+D800 |
| 172 | {"\xed\xa0\x80", NULL, false, "%EF%BF%BD"}, |
| 173 | }; |
| 174 | |
| 175 | std::string out_str; |
viettrungluu | 4b691586 | 2014-10-16 03:42:49 | [diff] [blame] | 176 | for (size_t i = 0; i < arraysize(utf_cases); i++) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 177 | if (utf_cases[i].input8) { |
| 178 | out_str.clear(); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 179 | StdStringCanonOutput output(&out_str); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 180 | |
| 181 | int input_len = static_cast<int>(strlen(utf_cases[i].input8)); |
| 182 | bool success = true; |
| 183 | for (int ch = 0; ch < input_len; ch++) { |
| 184 | success &= AppendUTF8EscapedChar(utf_cases[i].input8, &ch, input_len, |
| 185 | &output); |
| 186 | } |
| 187 | output.Complete(); |
| 188 | EXPECT_EQ(utf_cases[i].expected_success, success); |
| 189 | EXPECT_EQ(std::string(utf_cases[i].output), out_str); |
| 190 | } |
| 191 | if (utf_cases[i].input16) { |
| 192 | out_str.clear(); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 193 | StdStringCanonOutput output(&out_str); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 194 | |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 195 | base::string16 input_str( |
| 196 | test_utils::TruncateWStringToUTF16(utf_cases[i].input16)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 197 | int input_len = static_cast<int>(input_str.length()); |
| 198 | bool success = true; |
| 199 | for (int ch = 0; ch < input_len; ch++) { |
| 200 | success &= AppendUTF8EscapedChar(input_str.c_str(), &ch, input_len, |
| 201 | &output); |
| 202 | } |
| 203 | output.Complete(); |
| 204 | EXPECT_EQ(utf_cases[i].expected_success, success); |
| 205 | EXPECT_EQ(std::string(utf_cases[i].output), out_str); |
| 206 | } |
| 207 | |
| 208 | if (utf_cases[i].input8 && utf_cases[i].input16 && |
| 209 | utf_cases[i].expected_success) { |
| 210 | // Check that the UTF-8 and UTF-16 inputs are equivalent. |
| 211 | |
| 212 | // UTF-16 -> UTF-8 |
| 213 | std::string input8_str(utf_cases[i].input8); |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 214 | base::string16 input16_str( |
| 215 | test_utils::TruncateWStringToUTF16(utf_cases[i].input16)); |
| 216 | EXPECT_EQ(input8_str, base::UTF16ToUTF8(input16_str)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 217 | |
| 218 | // UTF-8 -> UTF-16 |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 219 | EXPECT_EQ(input16_str, base::UTF8ToUTF16(input8_str)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 224 | TEST(URLCanonTest, Scheme) { |
| 225 | // Here, we're mostly testing that unusual characters are handled properly. |
| 226 | // The canonicalizer doesn't do any parsing or whitespace detection. It will |
| 227 | // also do its best on error, and will escape funny sequences (these won't be |
| 228 | // valid schemes and it will return error). |
| 229 | // |
| 230 | // Note that the canonicalizer will append a colon to the output to separate |
| 231 | // out the rest of the URL, which is not present in the input. We check, |
| 232 | // however, that the output range includes everything but the colon. |
| 233 | ComponentCase scheme_cases[] = { |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 234 | {"http", "http:", Component(0, 4), true}, |
| 235 | {"HTTP", "http:", Component(0, 4), true}, |
| 236 | {" HTTP ", "%20http%20:", Component(0, 10), false}, |
| 237 | {"htt: ", "htt%3A%20:", Component(0, 9), false}, |
| 238 | {"\xe4\xbd\xa0\xe5\xa5\xbdhttp", "%E4%BD%A0%E5%A5%BDhttp:", Component(0, 22), false}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 239 | // Don't re-escape something already escaped. Note that it will |
| 240 | // "canonicalize" the 'A' to 'a', but that's OK. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 241 | {"ht%3Atp", "ht%3atp:", Component(0, 7), false}, |
brettw | 46f9b83 | 2016-10-05 19:22:48 | [diff] [blame] | 242 | {"", ":", Component(0, 0), false}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | std::string out_str; |
| 246 | |
| 247 | for (size_t i = 0; i < arraysize(scheme_cases); i++) { |
| 248 | int url_len = static_cast<int>(strlen(scheme_cases[i].input)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 249 | Component in_comp(0, url_len); |
| 250 | Component out_comp; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 251 | |
| 252 | out_str.clear(); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 253 | StdStringCanonOutput output1(&out_str); |
| 254 | bool success = CanonicalizeScheme(scheme_cases[i].input, in_comp, &output1, |
| 255 | &out_comp); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 256 | output1.Complete(); |
| 257 | |
| 258 | EXPECT_EQ(scheme_cases[i].expected_success, success); |
| 259 | EXPECT_EQ(std::string(scheme_cases[i].expected), out_str); |
| 260 | EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin); |
| 261 | EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len); |
| 262 | |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 263 | // Now try the wide version. |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 264 | out_str.clear(); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 265 | StdStringCanonOutput output2(&out_str); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 266 | |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 267 | base::string16 wide_input(base::UTF8ToUTF16(scheme_cases[i].input)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 268 | in_comp.len = static_cast<int>(wide_input.length()); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 269 | success = CanonicalizeScheme(wide_input.c_str(), in_comp, &output2, |
| 270 | &out_comp); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 271 | output2.Complete(); |
| 272 | |
| 273 | EXPECT_EQ(scheme_cases[i].expected_success, success); |
| 274 | EXPECT_EQ(std::string(scheme_cases[i].expected), out_str); |
| 275 | EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin); |
| 276 | EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len); |
| 277 | } |
| 278 | |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 279 | // Test the case where the scheme is declared nonexistent, it should be |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 280 | // converted into an empty scheme. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 281 | Component out_comp; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 282 | out_str.clear(); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 283 | StdStringCanonOutput output(&out_str); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 284 | |
brettw | 46f9b83 | 2016-10-05 19:22:48 | [diff] [blame] | 285 | EXPECT_FALSE(CanonicalizeScheme("", Component(0, -1), &output, &out_comp)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 286 | output.Complete(); |
| 287 | |
| 288 | EXPECT_EQ(std::string(":"), out_str); |
| 289 | EXPECT_EQ(0, out_comp.begin); |
| 290 | EXPECT_EQ(0, out_comp.len); |
| 291 | } |
| 292 | |
| 293 | TEST(URLCanonTest, Host) { |
| 294 | IPAddressCase host_cases[] = { |
| 295 | // Basic canonicalization, uppercase should be converted to lowercase. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 296 | {"GoOgLe.CoM", L"GoOgLe.CoM", "google.com", Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 297 | // Spaces and some other characters should be escaped. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 298 | {"Goo%20 goo%7C|.com", L"Goo%20 goo%7C|.com", "goo%20%20goo%7C%7C.com", Component(0, 22), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 299 | // Exciting different types of spaces! |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 300 | {NULL, L"GOO\x00a0\x3000goo.com", "goo%20%20goo.com", Component(0, 16), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 301 | // Other types of space (no-break, zero-width, zero-width-no-break) are |
| 302 | // name-prepped away to nothing. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 303 | {NULL, L"GOO\x200b\x2060\xfeffgoo.com", "googoo.com", Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 304 | // Ideographic full stop (full-width period for Chinese, etc.) should be |
| 305 | // treated as a dot. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 306 | {NULL, L"www.foo\x3002" L"bar.com", "www.foo.bar.com", Component(0, 15), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 307 | // Invalid unicode characters should fail... |
| 308 | // ...In wide input, ICU will barf and we'll end up with the input as |
| 309 | // escaped UTF-8 (the invalid character should be replaced with the |
| 310 | // replacement character). |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 311 | {"\xef\xb7\x90zyx.com", L"\xfdd0zyx.com", "%EF%BF%BDzyx.com", Component(0, 16), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 312 | // ...This is the same as previous but with with escaped. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 313 | {"%ef%b7%90zyx.com", L"%ef%b7%90zyx.com", "%EF%BF%BDzyx.com", Component(0, 16), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 314 | // Test name prepping, fullwidth input should be converted to ASCII and NOT |
| 315 | // IDN-ized. This is "Go" in fullwidth UTF-8/UTF-16. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 316 | {"\xef\xbc\xa7\xef\xbd\x8f.com", L"\xff27\xff4f.com", "go.com", Component(0, 6), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 317 | // Test that fullwidth escaped values are properly name-prepped, |
| 318 | // then converted or rejected. |
| 319 | // ...%41 in fullwidth = 'A' (also as escaped UTF-8 input) |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 320 | {"\xef\xbc\x85\xef\xbc\x94\xef\xbc\x91.com", L"\xff05\xff14\xff11.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""}, |
| 321 | {"%ef%bc%85%ef%bc%94%ef%bc%91.com", L"%ef%bc%85%ef%bc%94%ef%bc%91.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 322 | // ...%00 in fullwidth should fail (also as escaped UTF-8 input) |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 323 | {"\xef\xbc\x85\xef\xbc\x90\xef\xbc\x90.com", L"\xff05\xff10\xff10.com", "%00.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""}, |
| 324 | {"%ef%bc%85%ef%bc%90%ef%bc%90.com", L"%ef%bc%85%ef%bc%90%ef%bc%90.com", "%00.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""}, |
brettw | 1141951b | 2015-11-26 00:29:35 | [diff] [blame] | 325 | // ICU will convert weird percents into ASCII percents, but not unescape |
| 326 | // further. A weird percent is U+FE6A (EF B9 AA in UTF-8) which is a |
| 327 | // "small percent". At this point we should be within our rights to mark |
| 328 | // anything as invalid since the URL is corrupt or malicious. The code |
| 329 | // happens to allow ASCII characters (%41 = "A" -> 'a') to be unescaped |
| 330 | // and kept as valid, so we validate that behavior here, but this level |
| 331 | // of fixing the input shouldn't be seen as required. "%81" is invalid. |
| 332 | {"\xef\xb9\xaa" "41.com", L"\xfe6a" L"41.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""}, |
| 333 | {"%ef%b9%aa" "41.com", L"\xfe6a" L"41.com", "a.com", Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""}, |
| 334 | {"\xef\xb9\xaa" "81.com", L"\xfe6a" L"81.com", "%81.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""}, |
| 335 | {"%ef%b9%aa" "81.com", L"\xfe6a" L"81.com", "%81.com", Component(0, 7), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 336 | // Basic IDN support, UTF-8 and UTF-16 input should be converted to IDN |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 337 | {"\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", L"\x4f60\x597d\x4f60\x597d", "xn--6qqa088eba", Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 338 | // See http://unicode.org/cldr/utility/idna.jsp for other |
| 339 | // examples/experiments and http://goo.gl/7yG11o |
| 340 | // for the full list of characters handled differently by |
| 341 | // IDNA 2003, UTS 46 (http://unicode.org/reports/tr46/ ) and IDNA 2008. |
| 342 | |
| 343 | // 4 Deviation characters are mapped/ignored in UTS 46 transitional |
| 344 | // mechansm. UTS 46, table 4 row (g). |
| 345 | // Sharp-s is mapped to 'ss' in UTS 46 and IDNA 2003. |
| 346 | // Otherwise, it'd be "xn--fuball-cta.de". |
| 347 | {"fu\xc3\x9f" "ball.de", L"fu\x00df" L"ball.de", "fussball.de", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 348 | Component(0, 11), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 349 | // Final-sigma (U+03C3) is mapped to regular sigma (U+03C2). |
| 350 | // Otherwise, it'd be "xn--wxaijb9b". |
| 351 | {"\xcf\x83\xcf\x8c\xce\xbb\xce\xbf\xcf\x82", L"\x3c3\x3cc\x3bb\x3bf\x3c2", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 352 | "xn--wxaikc6b", Component(0, 12), |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 353 | CanonHostInfo::NEUTRAL, -1, ""}, |
| 354 | // ZWNJ (U+200C) and ZWJ (U+200D) are mapped away in UTS 46 transitional |
| 355 | // handling as well as in IDNA 2003. |
| 356 | {"a\xe2\x80\x8c" "b\xe2\x80\x8d" "c", L"a\x200c" L"b\x200d" L"c", "abc", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 357 | Component(0, 3), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 358 | // ZWJ between Devanagari characters is still mapped away in UTS 46 |
| 359 | // transitional handling. IDNA 2008 would give xn--11bo0mv54g. |
| 360 | {"\xe0\xa4\x95\xe0\xa5\x8d\xe2\x80\x8d\xe0\xa4\x9c", |
| 361 | L"\x915\x94d\x200d\x91c", "xn--11bo0m", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 362 | Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 363 | // Fullwidth exclamation mark is disallowed. UTS 46, table 4, row (b) |
| 364 | // However, we do allow this at the moment because we don't use |
| 365 | // STD3 rules and canonicalize full-width ASCII to ASCII. |
| 366 | {"wow\xef\xbc\x81", L"wow\xff01", "wow%21", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 367 | Component(0, 6), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 368 | // U+2132 (turned capital F) is disallowed. UTS 46, table 4, row (c) |
| 369 | // Allowed in IDNA 2003, but the mapping changed after Unicode 3.2 |
| 370 | {"\xe2\x84\xb2oo", L"\x2132oo", "%E2%84%B2oo", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 371 | Component(0, 11), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 372 | // U+2F868 (CJK Comp) is disallowed. UTS 46, table 4, row (d) |
| 373 | // Allowed in IDNA 2003, but the mapping changed after Unicode 3.2 |
| 374 | {"\xf0\xaf\xa1\xa8\xe5\xa7\xbb.cn", L"\xd87e\xdc68\x59fb.cn", |
| 375 | "%F0%AF%A1%A8%E5%A7%BB.cn", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 376 | Component(0, 24), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 377 | // Maps uppercase letters to lower case letters. UTS 46 table 4 row (e) |
| 378 | {"M\xc3\x9cNCHEN", L"M\xdcNCHEN", "xn--mnchen-3ya", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 379 | Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 380 | // Symbol/punctuations are allowed in IDNA 2003/UTS46. |
| 381 | // Not allowed in IDNA 2008. UTS 46 table 4 row (f). |
| 382 | {"\xe2\x99\xa5ny.us", L"\x2665ny.us", "xn--ny-s0x.us", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 383 | Component(0, 13), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 384 | // U+11013 is new in Unicode 6.0 and is allowed. UTS 46 table 4, row (h) |
| 385 | // We used to allow it because we passed through unassigned code points. |
| 386 | {"\xf0\x91\x80\x93.com", L"\xd804\xdc13.com", "xn--n00d.com", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 387 | Component(0, 12), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 388 | // U+0602 is disallowed in UTS46/IDNA 2008. UTS 46 table 4, row(i) |
| 389 | // Used to be allowed in INDA 2003. |
| 390 | {"\xd8\x82.eg", L"\x602.eg", "%D8%82.eg", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 391 | Component(0, 9), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 392 | // U+20B7 is new in Unicode 5.2 (not a part of IDNA 2003 based |
| 393 | // on Unicode 3.2). We did allow it in the past because we let unassigned |
| 394 | // code point pass. We continue to allow it even though it's a |
| 395 | // "punctuation and symbol" blocked in IDNA 2008. |
| 396 | // UTS 46 table 4, row (j) |
| 397 | {"\xe2\x82\xb7.com", L"\x20b7.com", "xn--wzg.com", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 398 | Component(0, 11), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 399 | // Maps uppercase letters to lower case letters. |
| 400 | // In IDNA 2003, it's allowed without case-folding |
| 401 | // ( xn--bc-7cb.com ) because it's not defined in Unicode 3.2 |
| 402 | // (added in Unicode 4.1). UTS 46 table 4 row (k) |
| 403 | {"bc\xc8\xba.com", L"bc\x23a.com", "xn--bc-is1a.com", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 404 | Component(0, 15), CanonHostInfo::NEUTRAL, -1, ""}, |
jshin | 62a9283 | 2016-03-18 18:42:39 | [diff] [blame] | 405 | // Maps U+FF43 (Full Width Small Letter C) to 'c'. |
| 406 | {"ab\xef\xbd\x83.xyz", L"ab\xff43.xyz", "abc.xyz", |
| 407 | Component(0, 7), CanonHostInfo::NEUTRAL, -1, ""}, |
| 408 | // Maps U+1D68C (Math Monospace Small C) to 'c'. |
| 409 | // U+1D68C = \xD835\xDE8C in UTF-16 |
| 410 | {"ab\xf0\x9d\x9a\x8c.xyz", L"ab\xd835\xde8c.xyz", "abc.xyz", |
| 411 | Component(0, 7), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 412 | // BiDi check test |
| 413 | // "Divehi" in Divehi (Thaana script) ends with BidiClass=NSM. |
| 414 | // Disallowed in IDNA 2003 but now allowed in UTS 46/IDNA 2008. |
| 415 | {"\xde\x8b\xde\xa8\xde\x88\xde\xac\xde\x80\xde\xa8", |
| 416 | L"\x78b\x7a8\x788\x7ac\x780\x7a8", "xn--hqbpi0jcw", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 417 | Component(0, 13), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 418 | // Disallowed in both IDNA 2003 and 2008 with BiDi check. |
| 419 | // Labels starting with a RTL character cannot end with a LTR character. |
| 420 | {"\xd8\xac\xd8\xa7\xd8\xb1xyz", L"\x62c\x627\x631xyz", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 421 | "%D8%AC%D8%A7%D8%B1xyz", Component(0, 21), |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 422 | CanonHostInfo::BROKEN, -1, ""}, |
| 423 | // Labels starting with a RTL character can end with BC=EN (European |
| 424 | // number). Disallowed in IDNA 2003 but now allowed. |
| 425 | {"\xd8\xac\xd8\xa7\xd8\xb1" "2", L"\x62c\x627\x631" L"2", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 426 | "xn--2-ymcov", Component(0, 11), |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 427 | CanonHostInfo::NEUTRAL, -1, ""}, |
| 428 | // Labels starting with a RTL character cannot have "L" characters |
| 429 | // even if it ends with an BC=EN. Disallowed in both IDNA 2003/2008. |
| 430 | {"\xd8\xac\xd8\xa7\xd8\xb1xy2", L"\x62c\x627\x631xy2", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 431 | "%D8%AC%D8%A7%D8%B1xy2", Component(0, 21), |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 432 | CanonHostInfo::BROKEN, -1, ""}, |
| 433 | // Labels starting with a RTL character can end with BC=AN (Arabic number) |
| 434 | // Disallowed in IDNA 2003, but now allowed. |
| 435 | {"\xd8\xac\xd8\xa7\xd8\xb1\xd9\xa2", L"\x62c\x627\x631\x662", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 436 | "xn--mgbjq0r", Component(0, 11), |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 437 | CanonHostInfo::NEUTRAL, -1, ""}, |
| 438 | // Labels starting with a RTL character cannot have "L" characters |
| 439 | // even if it ends with an BC=AN (Arabic number). |
| 440 | // Disallowed in both IDNA 2003/2008. |
| 441 | {"\xd8\xac\xd8\xa7\xd8\xb1xy\xd9\xa2", L"\x62c\x627\x631xy\x662", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 442 | "%D8%AC%D8%A7%D8%B1xy%D9%A2", Component(0, 26), |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 443 | CanonHostInfo::BROKEN, -1, ""}, |
| 444 | // Labels starting with a RTL character cannot mix BC=EN and BC=AN |
| 445 | {"\xd8\xac\xd8\xa7\xd8\xb1xy2\xd9\xa2", L"\x62c\x627\x631xy2\x662", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 446 | "%D8%AC%D8%A7%D8%B1xy2%D9%A2", Component(0, 27), |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 447 | CanonHostInfo::BROKEN, -1, ""}, |
| 448 | // As of Unicode 6.2, U+20CF is not assigned. We do not allow it. |
| 449 | {"\xe2\x83\x8f.com", L"\x20cf.com", "%E2%83%8F.com", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 450 | Component(0, 13), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 451 | // U+0080 is not allowed. |
| 452 | {"\xc2\x80.com", L"\x80.com", "%C2%80.com", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 453 | Component(0, 10), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 454 | // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 455 | // Mixed UTF-8 and escaped UTF-8 (narrow case) and UTF-16 and escaped |
| 456 | // UTF-8 (wide case). The output should be equivalent to the true wide |
| 457 | // character input above). |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 458 | {"%E4%BD%A0%E5%A5%BD\xe4\xbd\xa0\xe5\xa5\xbd", |
| 459 | L"%E4%BD%A0%E5%A5%BD\x4f60\x597d", "xn--6qqa088eba", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 460 | Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 461 | // Invalid escaped characters should fail and the percents should be |
| 462 | // escaped. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 463 | {"%zz%66%a", L"%zz%66%a", "%25zzf%25a", Component(0, 10), |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 464 | CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 465 | // If we get an invalid character that has been escaped. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 466 | {"%25", L"%25", "%25", Component(0, 3), |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 467 | CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 468 | {"hello%00", L"hello%00", "hello%00", Component(0, 8), |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 469 | CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 470 | // Escaped numbers should be treated like IP addresses if they are. |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 471 | {"%30%78%63%30%2e%30%32%35%30.01", L"%30%78%63%30%2e%30%32%35%30.01", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 472 | "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3, |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 473 | "C0A80001"}, |
| 474 | {"%30%78%63%30%2e%30%32%35%30.01%2e", L"%30%78%63%30%2e%30%32%35%30.01%2e", |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 475 | "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3, |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 476 | "C0A80001"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 477 | // Invalid escaping should trigger the regular host error handling. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 478 | {"%3g%78%63%30%2e%30%32%35%30%2E.01", L"%3g%78%63%30%2e%30%32%35%30%2E.01", "%253gxc0.0250..01", Component(0, 17), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 479 | // Something that isn't exactly an IP should get treated as a host and |
| 480 | // spaces escaped. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 481 | {"192.168.0.1 hello", L"192.168.0.1 hello", "192.168.0.1%20hello", Component(0, 19), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 482 | // Fullwidth and escaped UTF-8 fullwidth should still be treated as IP. |
| 483 | // These are "0Xc0.0250.01" in fullwidth. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 484 | {"\xef\xbc\x90%Ef%bc\xb8%ef%Bd%83\xef\xbc\x90%EF%BC%8E\xef\xbc\x90\xef\xbc\x92\xef\xbc\x95\xef\xbc\x90\xef\xbc%8E\xef\xbc\x90\xef\xbc\x91", L"\xff10\xff38\xff43\xff10\xff0e\xff10\xff12\xff15\xff10\xff0e\xff10\xff11", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3, "C0A80001"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 485 | // Broken IP addresses get marked as such. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 486 | {"192.168.0.257", L"192.168.0.257", "192.168.0.257", Component(0, 13), CanonHostInfo::BROKEN, -1, ""}, |
| 487 | {"[google.com]", L"[google.com]", "[google.com]", Component(0, 12), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 488 | // Cyrillic letter followed by '(' should return punycode for '(' escaped |
| 489 | // before punycode string was created. I.e. |
| 490 | // if '(' is escaped after punycode is created we would get xn--%28-8tb |
| 491 | // (incorrect). |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 492 | {"\xd1\x82(", L"\x0442(", "xn--%28-7ed", Component(0, 11), |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 493 | CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 494 | // Address with all hexidecimal characters with leading number of 1<<32 |
| 495 | // or greater and should return NEUTRAL rather than BROKEN if not all |
| 496 | // components are numbers. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 497 | {"12345678912345.de", L"12345678912345.de", "12345678912345.de", Component(0, 17), CanonHostInfo::NEUTRAL, -1, ""}, |
| 498 | {"1.12345678912345.de", L"1.12345678912345.de", "1.12345678912345.de", Component(0, 19), CanonHostInfo::NEUTRAL, -1, ""}, |
| 499 | {"12345678912345.12345678912345.de", L"12345678912345.12345678912345.de", "12345678912345.12345678912345.de", Component(0, 32), CanonHostInfo::NEUTRAL, -1, ""}, |
| 500 | {"1.2.0xB3A73CE5B59.de", L"1.2.0xB3A73CE5B59.de", "1.2.0xb3a73ce5b59.de", Component(0, 20), CanonHostInfo::NEUTRAL, -1, ""}, |
| 501 | {"12345678912345.0xde", L"12345678912345.0xde", "12345678912345.0xde", Component(0, 19), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 502 | }; |
| 503 | |
| 504 | // CanonicalizeHost() non-verbose. |
| 505 | std::string out_str; |
| 506 | for (size_t i = 0; i < arraysize(host_cases); i++) { |
| 507 | // Narrow version. |
| 508 | if (host_cases[i].input8) { |
| 509 | int host_len = static_cast<int>(strlen(host_cases[i].input8)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 510 | Component in_comp(0, host_len); |
| 511 | Component out_comp; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 512 | |
| 513 | out_str.clear(); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 514 | StdStringCanonOutput output(&out_str); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 515 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 516 | bool success = CanonicalizeHost(host_cases[i].input8, in_comp, &output, |
| 517 | &out_comp); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 518 | output.Complete(); |
| 519 | |
| 520 | EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN, |
[email protected] | 18f00cd | 2013-09-29 10:52:50 | [diff] [blame] | 521 | success) << "for input: " << host_cases[i].input8; |
| 522 | EXPECT_EQ(std::string(host_cases[i].expected), out_str) << |
| 523 | "for input: " << host_cases[i].input8; |
| 524 | EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin) << |
| 525 | "for input: " << host_cases[i].input8; |
| 526 | EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len) << |
| 527 | "for input: " << host_cases[i].input8; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | // Wide version. |
| 531 | if (host_cases[i].input16) { |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 532 | base::string16 input16( |
| 533 | test_utils::TruncateWStringToUTF16(host_cases[i].input16)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 534 | int host_len = static_cast<int>(input16.length()); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 535 | Component in_comp(0, host_len); |
| 536 | Component out_comp; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 537 | |
| 538 | out_str.clear(); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 539 | StdStringCanonOutput output(&out_str); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 540 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 541 | bool success = CanonicalizeHost(input16.c_str(), in_comp, &output, |
| 542 | &out_comp); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 543 | output.Complete(); |
| 544 | |
| 545 | EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN, |
| 546 | success); |
| 547 | EXPECT_EQ(std::string(host_cases[i].expected), out_str); |
| 548 | EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin); |
| 549 | EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | // CanonicalizeHostVerbose() |
| 554 | for (size_t i = 0; i < arraysize(host_cases); i++) { |
| 555 | // Narrow version. |
| 556 | if (host_cases[i].input8) { |
| 557 | int host_len = static_cast<int>(strlen(host_cases[i].input8)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 558 | Component in_comp(0, host_len); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 559 | |
| 560 | out_str.clear(); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 561 | StdStringCanonOutput output(&out_str); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 562 | CanonHostInfo host_info; |
| 563 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 564 | CanonicalizeHostVerbose(host_cases[i].input8, in_comp, &output, |
| 565 | &host_info); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 566 | output.Complete(); |
| 567 | |
| 568 | EXPECT_EQ(host_cases[i].expected_family, host_info.family); |
| 569 | EXPECT_EQ(std::string(host_cases[i].expected), out_str); |
| 570 | EXPECT_EQ(host_cases[i].expected_component.begin, |
| 571 | host_info.out_host.begin); |
| 572 | EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len); |
| 573 | EXPECT_EQ(std::string(host_cases[i].expected_address_hex), |
| 574 | BytesToHexString(host_info.address, host_info.AddressLength())); |
| 575 | if (host_cases[i].expected_family == CanonHostInfo::IPV4) { |
| 576 | EXPECT_EQ(host_cases[i].expected_num_ipv4_components, |
| 577 | host_info.num_ipv4_components); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | // Wide version. |
| 582 | if (host_cases[i].input16) { |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 583 | base::string16 input16( |
| 584 | test_utils::TruncateWStringToUTF16(host_cases[i].input16)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 585 | int host_len = static_cast<int>(input16.length()); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 586 | Component in_comp(0, host_len); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 587 | |
| 588 | out_str.clear(); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 589 | StdStringCanonOutput output(&out_str); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 590 | CanonHostInfo host_info; |
| 591 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 592 | CanonicalizeHostVerbose(input16.c_str(), in_comp, &output, &host_info); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 593 | output.Complete(); |
| 594 | |
| 595 | EXPECT_EQ(host_cases[i].expected_family, host_info.family); |
| 596 | EXPECT_EQ(std::string(host_cases[i].expected), out_str); |
| 597 | EXPECT_EQ(host_cases[i].expected_component.begin, |
| 598 | host_info.out_host.begin); |
| 599 | EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len); |
| 600 | EXPECT_EQ(std::string(host_cases[i].expected_address_hex), |
| 601 | BytesToHexString(host_info.address, host_info.AddressLength())); |
| 602 | if (host_cases[i].expected_family == CanonHostInfo::IPV4) { |
| 603 | EXPECT_EQ(host_cases[i].expected_num_ipv4_components, |
| 604 | host_info.num_ipv4_components); |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | TEST(URLCanonTest, IPv4) { |
| 611 | IPAddressCase cases[] = { |
| 612 | // Empty is not an IP address. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 613 | {"", L"", "", Component(), CanonHostInfo::NEUTRAL, -1, ""}, |
| 614 | {".", L".", "", Component(), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 615 | // Regular IP addresses in different bases. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 616 | {"192.168.0.1", L"192.168.0.1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"}, |
| 617 | {"0300.0250.00.01", L"0300.0250.00.01", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"}, |
| 618 | {"0xC0.0Xa8.0x0.0x1", L"0xC0.0Xa8.0x0.0x1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 619 | // Non-IP addresses due to invalid characters. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 620 | {"192.168.9.com", L"192.168.9.com", "", Component(), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 621 | // Invalid characters for the base should be rejected. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 622 | {"19a.168.0.1", L"19a.168.0.1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""}, |
| 623 | {"0308.0250.00.01", L"0308.0250.00.01", "", Component(), CanonHostInfo::NEUTRAL, -1, ""}, |
| 624 | {"0xCG.0xA8.0x0.0x1", L"0xCG.0xA8.0x0.0x1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 625 | // If there are not enough components, the last one should fill them out. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 626 | {"192", L"192", "0.0.0.192", Component(0, 9), CanonHostInfo::IPV4, 1, "000000C0"}, |
| 627 | {"0xC0a80001", L"0xC0a80001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"}, |
| 628 | {"030052000001", L"030052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"}, |
| 629 | {"000030052000001", L"000030052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"}, |
| 630 | {"192.168", L"192.168", "192.0.0.168", Component(0, 11), CanonHostInfo::IPV4, 2, "C00000A8"}, |
| 631 | {"192.0x00A80001", L"192.0x000A80001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 2, "C0A80001"}, |
| 632 | {"0xc0.052000001", L"0xc0.052000001", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 2, "C0A80001"}, |
| 633 | {"192.168.1", L"192.168.1", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 3, "C0A80001"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 634 | // Too many components means not an IP address. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 635 | {"192.168.0.0.1", L"192.168.0.0.1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 636 | // We allow a single trailing dot. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 637 | {"192.168.0.1.", L"192.168.0.1.", "192.168.0.1", Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"}, |
| 638 | {"192.168.0.1. hello", L"192.168.0.1. hello", "", Component(), CanonHostInfo::NEUTRAL, -1, ""}, |
| 639 | {"192.168.0.1..", L"192.168.0.1..", "", Component(), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 640 | // Two dots in a row means not an IP address. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 641 | {"192.168..1", L"192.168..1", "", Component(), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 642 | // Any numerical overflow should be marked as BROKEN. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 643 | {"0x100.0", L"0x100.0", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 644 | {"0x100.0.0", L"0x100.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 645 | {"0x100.0.0.0", L"0x100.0.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 646 | {"0.0x100.0.0", L"0.0x100.0.0", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 647 | {"0.0.0x100.0", L"0.0.0x100.0", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 648 | {"0.0.0.0x100", L"0.0.0.0x100", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 649 | {"0.0.0x10000", L"0.0.0x10000", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 650 | {"0.0x1000000", L"0.0x1000000", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 651 | {"0x100000000", L"0x100000000", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 652 | // Repeat the previous tests, minus 1, to verify boundaries. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 653 | {"0xFF.0", L"0xFF.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 2, "FF000000"}, |
| 654 | {"0xFF.0.0", L"0xFF.0.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 3, "FF000000"}, |
| 655 | {"0xFF.0.0.0", L"0xFF.0.0.0", "255.0.0.0", Component(0, 9), CanonHostInfo::IPV4, 4, "FF000000"}, |
| 656 | {"0.0xFF.0.0", L"0.0xFF.0.0", "0.255.0.0", Component(0, 9), CanonHostInfo::IPV4, 4, "00FF0000"}, |
| 657 | {"0.0.0xFF.0", L"0.0.0xFF.0", "0.0.255.0", Component(0, 9), CanonHostInfo::IPV4, 4, "0000FF00"}, |
| 658 | {"0.0.0.0xFF", L"0.0.0.0xFF", "0.0.0.255", Component(0, 9), CanonHostInfo::IPV4, 4, "000000FF"}, |
| 659 | {"0.0.0xFFFF", L"0.0.0xFFFF", "0.0.255.255", Component(0, 11), CanonHostInfo::IPV4, 3, "0000FFFF"}, |
| 660 | {"0.0xFFFFFF", L"0.0xFFFFFF", "0.255.255.255", Component(0, 13), CanonHostInfo::IPV4, 2, "00FFFFFF"}, |
| 661 | {"0xFFFFFFFF", L"0xFFFFFFFF", "255.255.255.255", Component(0, 15), CanonHostInfo::IPV4, 1, "FFFFFFFF"}, |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 662 | // Old trunctations tests. They're all "BROKEN" now. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 663 | {"276.256.0xf1a2.077777", L"276.256.0xf1a2.077777", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 664 | {"192.168.0.257", L"192.168.0.257", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 665 | {"192.168.0xa20001", L"192.168.0xa20001", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 666 | {"192.015052000001", L"192.015052000001", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 667 | {"0X12C0a80001", L"0X12C0a80001", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 668 | {"276.1.2", L"276.1.2", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 669 | // Spaces should be rejected. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 670 | {"192.168.0.1 hello", L"192.168.0.1 hello", "", Component(), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 671 | // Very large numbers. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 672 | {"0000000000000300.0x00000000000000fF.00000000000000001", L"0000000000000300.0x00000000000000fF.00000000000000001", "192.255.0.1", Component(0, 11), CanonHostInfo::IPV4, 3, "C0FF0001"}, |
| 673 | {"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", L"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", "", Component(0, 11), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 674 | // A number has no length limit, but long numbers can still overflow. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 675 | {"00000000000000000001", L"00000000000000000001", "0.0.0.1", Component(0, 7), CanonHostInfo::IPV4, 1, "00000001"}, |
| 676 | {"0000000000000000100000000000000001", L"0000000000000000100000000000000001", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 677 | // If a long component is non-numeric, it's a hostname, *not* a broken IP. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 678 | {"0.0.0.000000000000000000z", L"0.0.0.000000000000000000z", "", Component(), CanonHostInfo::NEUTRAL, -1, ""}, |
| 679 | {"0.0.0.100000000000000000z", L"0.0.0.100000000000000000z", "", Component(), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 680 | // Truncation of all zeros should still result in 0. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 681 | {"0.00.0x.0x0", L"0.00.0x.0x0", "0.0.0.0", Component(0, 7), CanonHostInfo::IPV4, 4, "00000000"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 682 | }; |
| 683 | |
| 684 | for (size_t i = 0; i < arraysize(cases); i++) { |
| 685 | // 8-bit version. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 686 | Component component(0, static_cast<int>(strlen(cases[i].input8))); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 687 | |
| 688 | std::string out_str1; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 689 | StdStringCanonOutput output1(&out_str1); |
| 690 | CanonHostInfo host_info; |
| 691 | CanonicalizeIPAddress(cases[i].input8, component, &output1, &host_info); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 692 | output1.Complete(); |
| 693 | |
| 694 | EXPECT_EQ(cases[i].expected_family, host_info.family); |
| 695 | EXPECT_EQ(std::string(cases[i].expected_address_hex), |
| 696 | BytesToHexString(host_info.address, host_info.AddressLength())); |
| 697 | if (host_info.family == CanonHostInfo::IPV4) { |
| 698 | EXPECT_STREQ(cases[i].expected, out_str1.c_str()); |
| 699 | EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin); |
| 700 | EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len); |
| 701 | EXPECT_EQ(cases[i].expected_num_ipv4_components, |
| 702 | host_info.num_ipv4_components); |
| 703 | } |
| 704 | |
| 705 | // 16-bit version. |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 706 | base::string16 input16( |
| 707 | test_utils::TruncateWStringToUTF16(cases[i].input16)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 708 | component = Component(0, static_cast<int>(input16.length())); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 709 | |
| 710 | std::string out_str2; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 711 | StdStringCanonOutput output2(&out_str2); |
| 712 | CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 713 | output2.Complete(); |
| 714 | |
| 715 | EXPECT_EQ(cases[i].expected_family, host_info.family); |
| 716 | EXPECT_EQ(std::string(cases[i].expected_address_hex), |
| 717 | BytesToHexString(host_info.address, host_info.AddressLength())); |
| 718 | if (host_info.family == CanonHostInfo::IPV4) { |
| 719 | EXPECT_STREQ(cases[i].expected, out_str2.c_str()); |
| 720 | EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin); |
| 721 | EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len); |
| 722 | EXPECT_EQ(cases[i].expected_num_ipv4_components, |
| 723 | host_info.num_ipv4_components); |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | TEST(URLCanonTest, IPv6) { |
| 729 | IPAddressCase cases[] = { |
| 730 | // Empty is not an IP address. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 731 | {"", L"", "", Component(), CanonHostInfo::NEUTRAL, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 732 | // Non-IPs with [:] characters are marked BROKEN. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 733 | {":", L":", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 734 | {"[", L"[", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 735 | {"[:", L"[:", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 736 | {"]", L"]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 737 | {":]", L":]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 738 | {"[]", L"[]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 739 | {"[:]", L"[:]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 740 | // Regular IP address is invalid without bounding '[' and ']'. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 741 | {"2001:db8::1", L"2001:db8::1", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 742 | {"[2001:db8::1", L"[2001:db8::1", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 743 | {"2001:db8::1]", L"2001:db8::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 744 | // Regular IP addresses. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 745 | {"[::]", L"[::]", "[::]", Component(0,4), CanonHostInfo::IPV6, -1, "00000000000000000000000000000000"}, |
| 746 | {"[::1]", L"[::1]", "[::1]", Component(0,5), CanonHostInfo::IPV6, -1, "00000000000000000000000000000001"}, |
| 747 | {"[1::]", L"[1::]", "[1::]", Component(0,5), CanonHostInfo::IPV6, -1, "00010000000000000000000000000000"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 748 | |
| 749 | // Leading zeros should be stripped. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 750 | {"[000:01:02:003:004:5:6:007]", L"[000:01:02:003:004:5:6:007]", "[0:1:2:3:4:5:6:7]", Component(0,17), CanonHostInfo::IPV6, -1, "00000001000200030004000500060007"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 751 | |
| 752 | // Upper case letters should be lowercased. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 753 | {"[A:b:c:DE:fF:0:1:aC]", L"[A:b:c:DE:fF:0:1:aC]", "[a:b:c:de:ff:0:1:ac]", Component(0,20), CanonHostInfo::IPV6, -1, "000A000B000C00DE00FF0000000100AC"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 754 | |
| 755 | // The same address can be written with different contractions, but should |
| 756 | // get canonicalized to the same thing. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 757 | {"[1:0:0:2::3:0]", L"[1:0:0:2::3:0]", "[1::2:0:0:3:0]", Component(0,14), CanonHostInfo::IPV6, -1, "00010000000000020000000000030000"}, |
| 758 | {"[1::2:0:0:3:0]", L"[1::2:0:0:3:0]", "[1::2:0:0:3:0]", Component(0,14), CanonHostInfo::IPV6, -1, "00010000000000020000000000030000"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 759 | |
| 760 | // Addresses with embedded IPv4. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 761 | {"[::192.168.0.1]", L"[::192.168.0.1]", "[::c0a8:1]", Component(0,10), CanonHostInfo::IPV6, -1, "000000000000000000000000C0A80001"}, |
| 762 | {"[::ffff:192.168.0.1]", L"[::ffff:192.168.0.1]", "[::ffff:c0a8:1]", Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0A80001"}, |
| 763 | {"[::eeee:192.168.0.1]", L"[::eeee:192.168.0.1]", "[::eeee:c0a8:1]", Component(0, 15), CanonHostInfo::IPV6, -1, "00000000000000000000EEEEC0A80001"}, |
| 764 | {"[2001::192.168.0.1]", L"[2001::192.168.0.1]", "[2001::c0a8:1]", Component(0, 14), CanonHostInfo::IPV6, -1, "200100000000000000000000C0A80001"}, |
| 765 | {"[1:2:192.168.0.1:5:6]", L"[1:2:192.168.0.1:5:6]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 766 | |
| 767 | // IPv4 with last component missing. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 768 | {"[::ffff:192.1.2]", L"[::ffff:192.1.2]", "[::ffff:c001:2]", Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0010002"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 769 | |
| 770 | // IPv4 using hex. |
| 771 | // TODO(eroman): Should this format be disallowed? |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 772 | {"[::ffff:0xC0.0Xa8.0x0.0x1]", L"[::ffff:0xC0.0Xa8.0x0.0x1]", "[::ffff:c0a8:1]", Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0A80001"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 773 | |
| 774 | // There may be zeros surrounding the "::" contraction. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 775 | {"[0:0::0:0:8]", L"[0:0::0:0:8]", "[::8]", Component(0,5), CanonHostInfo::IPV6, -1, "00000000000000000000000000000008"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 776 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 777 | {"[2001:db8::1]", L"[2001:db8::1]", "[2001:db8::1]", Component(0,13), CanonHostInfo::IPV6, -1, "20010DB8000000000000000000000001"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 778 | |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 779 | // Can only have one "::" contraction in an IPv6 string literal. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 780 | {"[2001::db8::1]", L"[2001::db8::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 781 | // No more than 2 consecutive ':'s. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 782 | {"[2001:db8:::1]", L"[2001:db8:::1]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 783 | {"[:::]", L"[:::]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 784 | // Non-IP addresses due to invalid characters. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 785 | {"[2001::.com]", L"[2001::.com]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 786 | // If there are not enough components, the last one should fill them out. |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 787 | // ... omitted at this time ... |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 788 | // Too many components means not an IP address. Similarly, with too few |
| 789 | // if using IPv4 compat or mapped addresses. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 790 | {"[::192.168.0.0.1]", L"[::192.168.0.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 791 | {"[::ffff:192.168.0.0.1]", L"[::ffff:192.168.0.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 792 | {"[1:2:3:4:5:6:7:8:9]", L"[1:2:3:4:5:6:7:8:9]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 793 | // Too many bits (even though 8 comonents, the last one holds 32 bits). |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 794 | {"[0:0:0:0:0:0:0:192.168.0.1]", L"[0:0:0:0:0:0:0:192.168.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 795 | |
| 796 | // Too many bits specified -- the contraction would have to be zero-length |
| 797 | // to not exceed 128 bits. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 798 | {"[1:2:3:4:5:6::192.168.0.1]", L"[1:2:3:4:5:6::192.168.0.1]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 799 | |
| 800 | // The contraction is for 16 bits of zero. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 801 | {"[1:2:3:4:5:6::8]", L"[1:2:3:4:5:6::8]", "[1:2:3:4:5:6:0:8]", Component(0,17), CanonHostInfo::IPV6, -1, "00010002000300040005000600000008"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 802 | |
| 803 | // Cannot have a trailing colon. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 804 | {"[1:2:3:4:5:6:7:8:]", L"[1:2:3:4:5:6:7:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 805 | {"[1:2:3:4:5:6:192.168.0.1:]", L"[1:2:3:4:5:6:192.168.0.1:]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 806 | |
| 807 | // Cannot have negative numbers. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 808 | {"[-1:2:3:4:5:6:7:8]", L"[-1:2:3:4:5:6:7:8]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 809 | |
| 810 | // Scope ID -- the URL may contain an optional ["%" <scope_id>] section. |
| 811 | // The scope_id should be included in the canonicalized URL, and is an |
| 812 | // unsigned decimal number. |
| 813 | |
| 814 | // Invalid because no ID was given after the percent. |
| 815 | |
| 816 | // Don't allow scope-id |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 817 | {"[1::%1]", L"[1::%1]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 818 | {"[1::%eth0]", L"[1::%eth0]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 819 | {"[1::%]", L"[1::%]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 820 | {"[%]", L"[%]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 821 | {"[::%:]", L"[::%:]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 822 | |
| 823 | // Don't allow leading or trailing colons. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 824 | {"[:0:0::0:0:8]", L"[:0:0::0:0:8]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 825 | {"[0:0::0:0:8:]", L"[0:0::0:0:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
| 826 | {"[:0:0::0:0:8:]", L"[:0:0::0:0:8:]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 827 | |
| 828 | // We allow a single trailing dot. |
| 829 | // ... omitted at this time ... |
| 830 | // Two dots in a row means not an IP address. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 831 | {"[::192.168..1]", L"[::192.168..1]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 832 | // Any non-first components get truncated to one byte. |
| 833 | // ... omitted at this time ... |
| 834 | // Spaces should be rejected. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 835 | {"[::1 hello]", L"[::1 hello]", "", Component(), CanonHostInfo::BROKEN, -1, ""}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 836 | }; |
| 837 | |
| 838 | for (size_t i = 0; i < arraysize(cases); i++) { |
| 839 | // 8-bit version. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 840 | Component component(0, static_cast<int>(strlen(cases[i].input8))); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 841 | |
| 842 | std::string out_str1; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 843 | StdStringCanonOutput output1(&out_str1); |
| 844 | CanonHostInfo host_info; |
| 845 | CanonicalizeIPAddress(cases[i].input8, component, &output1, &host_info); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 846 | output1.Complete(); |
| 847 | |
| 848 | EXPECT_EQ(cases[i].expected_family, host_info.family); |
| 849 | EXPECT_EQ(std::string(cases[i].expected_address_hex), |
| 850 | BytesToHexString(host_info.address, host_info.AddressLength())) << "iter " << i << " host " << cases[i].input8; |
| 851 | if (host_info.family == CanonHostInfo::IPV6) { |
| 852 | EXPECT_STREQ(cases[i].expected, out_str1.c_str()); |
| 853 | EXPECT_EQ(cases[i].expected_component.begin, |
| 854 | host_info.out_host.begin); |
| 855 | EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len); |
| 856 | } |
| 857 | |
| 858 | // 16-bit version. |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 859 | base::string16 input16( |
| 860 | test_utils::TruncateWStringToUTF16(cases[i].input16)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 861 | component = Component(0, static_cast<int>(input16.length())); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 862 | |
| 863 | std::string out_str2; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 864 | StdStringCanonOutput output2(&out_str2); |
| 865 | CanonicalizeIPAddress(input16.c_str(), component, &output2, &host_info); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 866 | output2.Complete(); |
| 867 | |
| 868 | EXPECT_EQ(cases[i].expected_family, host_info.family); |
| 869 | EXPECT_EQ(std::string(cases[i].expected_address_hex), |
| 870 | BytesToHexString(host_info.address, host_info.AddressLength())); |
| 871 | if (host_info.family == CanonHostInfo::IPV6) { |
| 872 | EXPECT_STREQ(cases[i].expected, out_str2.c_str()); |
| 873 | EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin); |
| 874 | EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len); |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | TEST(URLCanonTest, IPEmpty) { |
| 880 | std::string out_str1; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 881 | StdStringCanonOutput output1(&out_str1); |
| 882 | CanonHostInfo host_info; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 883 | |
| 884 | // This tests tests. |
| 885 | const char spec[] = "192.168.0.1"; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 886 | CanonicalizeIPAddress(spec, Component(), &output1, &host_info); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 887 | EXPECT_FALSE(host_info.IsIPAddress()); |
| 888 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 889 | CanonicalizeIPAddress(spec, Component(0, 0), &output1, &host_info); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 890 | EXPECT_FALSE(host_info.IsIPAddress()); |
| 891 | } |
| 892 | |
brettw | 5a36380ef | 2016-10-27 19:51:56 | [diff] [blame] | 893 | // Verifies that CanonicalizeHostSubstring produces the expected output and |
| 894 | // does not "fix" IP addresses. Because this code is a subset of |
| 895 | // CanonicalizeHost, the shared functionality is not tested. |
| 896 | TEST(URLCanonTest, CanonicalizeHostSubstring) { |
| 897 | // Basic sanity check. |
| 898 | { |
| 899 | std::string out_str; |
| 900 | StdStringCanonOutput output(&out_str); |
| 901 | EXPECT_TRUE(CanonicalizeHostSubstring("M\xc3\x9cNCHEN.com", |
| 902 | Component(0, 12), &output)); |
| 903 | output.Complete(); |
| 904 | EXPECT_EQ("xn--mnchen-3ya.com", out_str); |
| 905 | } |
| 906 | |
| 907 | // Failure case. |
| 908 | { |
| 909 | std::string out_str; |
| 910 | StdStringCanonOutput output(&out_str); |
| 911 | EXPECT_FALSE(CanonicalizeHostSubstring( |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 912 | test_utils::TruncateWStringToUTF16(L"\xfdd0zyx.com").c_str(), |
| 913 | Component(0, 8), &output)); |
brettw | 5a36380ef | 2016-10-27 19:51:56 | [diff] [blame] | 914 | output.Complete(); |
| 915 | EXPECT_EQ("%EF%BF%BDzyx.com", out_str); |
| 916 | } |
| 917 | |
| 918 | // Should return true for empty input strings. |
| 919 | { |
| 920 | std::string out_str; |
| 921 | StdStringCanonOutput output(&out_str); |
| 922 | EXPECT_TRUE(CanonicalizeHostSubstring("", Component(0, 0), &output)); |
| 923 | output.Complete(); |
| 924 | EXPECT_EQ(std::string(), out_str); |
| 925 | } |
| 926 | |
| 927 | // Numbers that look like IP addresses should not be changed. |
| 928 | { |
| 929 | std::string out_str; |
| 930 | StdStringCanonOutput output(&out_str); |
| 931 | EXPECT_TRUE( |
| 932 | CanonicalizeHostSubstring("01.02.03.04", Component(0, 11), &output)); |
| 933 | output.Complete(); |
| 934 | EXPECT_EQ("01.02.03.04", out_str); |
| 935 | } |
| 936 | } |
| 937 | |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 938 | TEST(URLCanonTest, UserInfo) { |
| 939 | // Note that the canonicalizer should escape and treat empty components as |
| 940 | // not being there. |
| 941 | |
| 942 | // We actually parse a full input URL so we can get the initial components. |
| 943 | struct UserComponentCase { |
| 944 | const char* input; |
| 945 | const char* expected; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 946 | Component expected_username; |
| 947 | Component expected_password; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 948 | bool expected_success; |
| 949 | } user_info_cases[] = { |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 950 | {"http://user:[email protected]/", "user:pass@", Component(0, 4), Component(5, 4), true}, |
| 951 | {"http://@host.com/", "", Component(0, -1), Component(0, -1), true}, |
| 952 | {"http://:@host.com/", "", Component(0, -1), Component(0, -1), true}, |
| 953 | {"http://foo:@host.com/", "foo@", Component(0, 3), Component(0, -1), true}, |
| 954 | {"http://:[email protected]/", ":foo@", Component(0, 0), Component(1, 3), true}, |
| 955 | {"http://^ :$\[email protected]/", "%5E%20:$%09@", Component(0, 6), Component(7, 4), true}, |
| 956 | {"http://user:pass@/", "user:pass@", Component(0, 4), Component(5, 4), true}, |
| 957 | {"http://%2540:[email protected]/", "%2540:bar@", Component(0, 5), Component(6, 3), true }, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 958 | |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 959 | // IE7 compatibility: old versions allowed backslashes in usernames, but |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 960 | // IE7 does not. We disallow it as well. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 961 | {"ftp://me\\mydomain:[email protected]/", "", Component(0, -1), Component(0, -1), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 962 | }; |
| 963 | |
viettrungluu | 4b691586 | 2014-10-16 03:42:49 | [diff] [blame] | 964 | for (size_t i = 0; i < arraysize(user_info_cases); i++) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 965 | int url_len = static_cast<int>(strlen(user_info_cases[i].input)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 966 | Parsed parsed; |
| 967 | ParseStandardURL(user_info_cases[i].input, url_len, &parsed); |
| 968 | Component out_user, out_pass; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 969 | std::string out_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 970 | StdStringCanonOutput output1(&out_str); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 971 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 972 | bool success = CanonicalizeUserInfo(user_info_cases[i].input, |
| 973 | parsed.username, |
| 974 | user_info_cases[i].input, |
| 975 | parsed.password, |
| 976 | &output1, |
| 977 | &out_user, |
| 978 | &out_pass); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 979 | output1.Complete(); |
| 980 | |
| 981 | EXPECT_EQ(user_info_cases[i].expected_success, success); |
| 982 | EXPECT_EQ(std::string(user_info_cases[i].expected), out_str); |
| 983 | EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin); |
| 984 | EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len); |
| 985 | EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin); |
| 986 | EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len); |
| 987 | |
| 988 | // Now try the wide version |
| 989 | out_str.clear(); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 990 | StdStringCanonOutput output2(&out_str); |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 991 | base::string16 wide_input(base::UTF8ToUTF16(user_info_cases[i].input)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 992 | success = CanonicalizeUserInfo(wide_input.c_str(), |
| 993 | parsed.username, |
| 994 | wide_input.c_str(), |
| 995 | parsed.password, |
| 996 | &output2, |
| 997 | &out_user, |
| 998 | &out_pass); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 999 | output2.Complete(); |
| 1000 | |
| 1001 | EXPECT_EQ(user_info_cases[i].expected_success, success); |
| 1002 | EXPECT_EQ(std::string(user_info_cases[i].expected), out_str); |
| 1003 | EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin); |
| 1004 | EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len); |
| 1005 | EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin); |
| 1006 | EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len); |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | TEST(URLCanonTest, Port) { |
| 1011 | // We only need to test that the number gets properly put into the output |
| 1012 | // buffer. The parser unit tests will test scanning the number correctly. |
| 1013 | // |
| 1014 | // Note that the CanonicalizePort will always prepend a colon to the output |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 1015 | // to separate it from the colon that it assumes precedes it. |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1016 | struct PortCase { |
| 1017 | const char* input; |
| 1018 | int default_port; |
| 1019 | const char* expected; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1020 | Component expected_component; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1021 | bool expected_success; |
| 1022 | } port_cases[] = { |
| 1023 | // Invalid input should be copied w/ failure. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1024 | {"as df", 80, ":as%20df", Component(1, 7), false}, |
| 1025 | {"-2", 80, ":-2", Component(1, 2), false}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1026 | // Default port should be omitted. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1027 | {"80", 80, "", Component(0, -1), true}, |
| 1028 | {"8080", 80, ":8080", Component(1, 4), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1029 | // PORT_UNSPECIFIED should mean always keep the port. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1030 | {"80", PORT_UNSPECIFIED, ":80", Component(1, 2), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1031 | }; |
| 1032 | |
viettrungluu | 4b691586 | 2014-10-16 03:42:49 | [diff] [blame] | 1033 | for (size_t i = 0; i < arraysize(port_cases); i++) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1034 | int url_len = static_cast<int>(strlen(port_cases[i].input)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1035 | Component in_comp(0, url_len); |
| 1036 | Component out_comp; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1037 | std::string out_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1038 | StdStringCanonOutput output1(&out_str); |
| 1039 | bool success = CanonicalizePort(port_cases[i].input, |
| 1040 | in_comp, |
| 1041 | port_cases[i].default_port, |
| 1042 | &output1, |
| 1043 | &out_comp); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1044 | output1.Complete(); |
| 1045 | |
| 1046 | EXPECT_EQ(port_cases[i].expected_success, success); |
| 1047 | EXPECT_EQ(std::string(port_cases[i].expected), out_str); |
| 1048 | EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin); |
| 1049 | EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len); |
| 1050 | |
| 1051 | // Now try the wide version |
| 1052 | out_str.clear(); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1053 | StdStringCanonOutput output2(&out_str); |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 1054 | base::string16 wide_input(base::UTF8ToUTF16(port_cases[i].input)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1055 | success = CanonicalizePort(wide_input.c_str(), |
| 1056 | in_comp, |
| 1057 | port_cases[i].default_port, |
| 1058 | &output2, |
| 1059 | &out_comp); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1060 | output2.Complete(); |
| 1061 | |
| 1062 | EXPECT_EQ(port_cases[i].expected_success, success); |
| 1063 | EXPECT_EQ(std::string(port_cases[i].expected), out_str); |
| 1064 | EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin); |
| 1065 | EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len); |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | TEST(URLCanonTest, Path) { |
| 1070 | DualComponentCase path_cases[] = { |
| 1071 | // ----- path collapsing tests ----- |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1072 | {"/././foo", L"/././foo", "/foo", Component(0, 4), true}, |
| 1073 | {"/./.foo", L"/./.foo", "/.foo", Component(0, 5), true}, |
| 1074 | {"/foo/.", L"/foo/.", "/foo/", Component(0, 5), true}, |
| 1075 | {"/foo/./", L"/foo/./", "/foo/", Component(0, 5), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1076 | // double dots followed by a slash or the end of the string count |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1077 | {"/foo/bar/..", L"/foo/bar/..", "/foo/", Component(0, 5), true}, |
| 1078 | {"/foo/bar/../", L"/foo/bar/../", "/foo/", Component(0, 5), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1079 | // don't count double dots when they aren't followed by a slash |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1080 | {"/foo/..bar", L"/foo/..bar", "/foo/..bar", Component(0, 10), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1081 | // some in the middle |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1082 | {"/foo/bar/../ton", L"/foo/bar/../ton", "/foo/ton", Component(0, 8), true}, |
| 1083 | {"/foo/bar/../ton/../../a", L"/foo/bar/../ton/../../a", "/a", Component(0, 2), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1084 | // we should not be able to go above the root |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1085 | {"/foo/../../..", L"/foo/../../..", "/", Component(0, 1), true}, |
| 1086 | {"/foo/../../../ton", L"/foo/../../../ton", "/ton", Component(0, 4), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1087 | // escaped dots should be unescaped and treated the same as dots |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1088 | {"/foo/%2e", L"/foo/%2e", "/foo/", Component(0, 5), true}, |
| 1089 | {"/foo/%2e%2", L"/foo/%2e%2", "/foo/.%2", Component(0, 8), true}, |
| 1090 | {"/foo/%2e./%2e%2e/.%2e/%2e.bar", L"/foo/%2e./%2e%2e/.%2e/%2e.bar", "/..bar", Component(0, 6), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1091 | // Multiple slashes in a row should be preserved and treated like empty |
| 1092 | // directory names. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1093 | {"////../..", L"////../..", "//", Component(0, 2), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1094 | |
| 1095 | // ----- escaping tests ----- |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1096 | {"/foo", L"/foo", "/foo", Component(0, 4), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1097 | // Valid escape sequence |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1098 | {"/%20foo", L"/%20foo", "/%20foo", Component(0, 7), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1099 | // Invalid escape sequence we should pass through unchanged. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1100 | {"/foo%", L"/foo%", "/foo%", Component(0, 5), true}, |
| 1101 | {"/foo%2", L"/foo%2", "/foo%2", Component(0, 6), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1102 | // Invalid escape sequence: bad characters should be treated the same as |
| 1103 | // the sourrounding text, not as escaped (in this case, UTF-8). |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1104 | {"/foo%2zbar", L"/foo%2zbar", "/foo%2zbar", Component(0, 10), true}, |
| 1105 | {"/foo%2\xc2\xa9zbar", NULL, "/foo%2%C2%A9zbar", Component(0, 16), true}, |
| 1106 | {NULL, L"/foo%2\xc2\xa9zbar", "/foo%2%C3%82%C2%A9zbar", Component(0, 22), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1107 | // Regular characters that are escaped should be unescaped |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1108 | {"/foo%41%7a", L"/foo%41%7a", "/fooAz", Component(0, 6), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1109 | // Funny characters that are unescaped should be escaped |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1110 | {"/foo\x09\x91%91", NULL, "/foo%09%91%91", Component(0, 13), true}, |
| 1111 | {NULL, L"/foo\x09\x91%91", "/foo%09%C2%91%91", Component(0, 16), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1112 | // Invalid characters that are escaped should cause a failure. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1113 | {"/foo%00%51", L"/foo%00%51", "/foo%00Q", Component(0, 8), false}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1114 | // Some characters should be passed through unchanged regardless of esc. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1115 | {"/(%28:%3A%29)", L"/(%28:%3A%29)", "/(%28:%3A%29)", Component(0, 13), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1116 | // Characters that are properly escaped should not have the case changed |
| 1117 | // of hex letters. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1118 | {"/%3A%3a%3C%3c", L"/%3A%3a%3C%3c", "/%3A%3a%3C%3c", Component(0, 13), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1119 | // Funny characters that are unescaped should be escaped |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1120 | {"/foo\tbar", L"/foo\tbar", "/foo%09bar", Component(0, 10), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1121 | // Backslashes should get converted to forward slashes |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1122 | {"\\foo\\bar", L"\\foo\\bar", "/foo/bar", Component(0, 8), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1123 | // Hashes found in paths (possibly only when the caller explicitly sets |
| 1124 | // the path on an already-parsed URL) should be escaped. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1125 | {"/foo#bar", L"/foo#bar", "/foo%23bar", Component(0, 10), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1126 | // %7f should be allowed and %3D should not be unescaped (these were wrong |
| 1127 | // in a previous version). |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1128 | {"/%7Ffp3%3Eju%3Dduvgw%3Dd", L"/%7Ffp3%3Eju%3Dduvgw%3Dd", "/%7Ffp3%3Eju%3Dduvgw%3Dd", Component(0, 24), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1129 | // @ should be passed through unchanged (escaped or unescaped). |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1130 | {"/@asdf%40", L"/@asdf%40", "/@asdf%40", Component(0, 9), true}, |
pkasting | 6982036 | 2015-09-22 01:54:05 | [diff] [blame] | 1131 | // Nested escape sequences should result in escaping the leading '%' if |
| 1132 | // unescaping would result in a new escape sequence. |
| 1133 | {"/%A%42", L"/%A%42", "/%25AB", Component(0, 6), true}, |
| 1134 | {"/%%41B", L"/%%41B", "/%25AB", Component(0, 6), true}, |
| 1135 | {"/%%41%42", L"/%%41%42", "/%25AB", Component(0, 6), true}, |
| 1136 | // Make sure truncated "nested" escapes don't result in reading off the |
| 1137 | // string end. |
| 1138 | {"/%%41", L"/%%41", "/%A", Component(0, 3), true}, |
| 1139 | // Don't unescape the leading '%' if unescaping doesn't result in a valid |
| 1140 | // new escape sequence. |
| 1141 | {"/%%470", L"/%%470", "/%G0", Component(0, 4), true}, |
| 1142 | {"/%%2D%41", L"/%%2D%41", "/%-A", Component(0, 4), true}, |
| 1143 | // Don't erroneously downcast a UTF-16 charater in a way that makes it |
| 1144 | // look like part of an escape sequence. |
| 1145 | {NULL, L"/%%41\x0130", "/%A%C4%B0", Component(0, 9), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1146 | |
| 1147 | // ----- encoding tests ----- |
| 1148 | // Basic conversions |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1149 | {"/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", L"/\x4f60\x597d\x4f60\x597d", "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD", Component(0, 37), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1150 | // Invalid unicode characters should fail. We only do validation on |
| 1151 | // UTF-16 input, so this doesn't happen on 8-bit. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1152 | {"/\xef\xb7\x90zyx", NULL, "/%EF%B7%90zyx", Component(0, 13), true}, |
| 1153 | {NULL, L"/\xfdd0zyx", "/%EF%BF%BDzyx", Component(0, 13), false}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1154 | }; |
| 1155 | |
| 1156 | for (size_t i = 0; i < arraysize(path_cases); i++) { |
| 1157 | if (path_cases[i].input8) { |
| 1158 | int len = static_cast<int>(strlen(path_cases[i].input8)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1159 | Component in_comp(0, len); |
| 1160 | Component out_comp; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1161 | std::string out_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1162 | StdStringCanonOutput output(&out_str); |
| 1163 | bool success = |
| 1164 | CanonicalizePath(path_cases[i].input8, in_comp, &output, &out_comp); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1165 | output.Complete(); |
| 1166 | |
| 1167 | EXPECT_EQ(path_cases[i].expected_success, success); |
| 1168 | EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin); |
| 1169 | EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len); |
| 1170 | EXPECT_EQ(path_cases[i].expected, out_str); |
| 1171 | } |
| 1172 | |
| 1173 | if (path_cases[i].input16) { |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 1174 | base::string16 input16( |
| 1175 | test_utils::TruncateWStringToUTF16(path_cases[i].input16)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1176 | int len = static_cast<int>(input16.length()); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1177 | Component in_comp(0, len); |
| 1178 | Component out_comp; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1179 | std::string out_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1180 | StdStringCanonOutput output(&out_str); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1181 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1182 | bool success = |
| 1183 | CanonicalizePath(input16.c_str(), in_comp, &output, &out_comp); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1184 | output.Complete(); |
| 1185 | |
| 1186 | EXPECT_EQ(path_cases[i].expected_success, success); |
| 1187 | EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin); |
| 1188 | EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len); |
| 1189 | EXPECT_EQ(path_cases[i].expected, out_str); |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | // Manual test: embedded NULLs should be escaped and the URL should be marked |
| 1194 | // as invalid. |
| 1195 | const char path_with_null[] = "/ab\0c"; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1196 | Component in_comp(0, 5); |
| 1197 | Component out_comp; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1198 | |
| 1199 | std::string out_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1200 | StdStringCanonOutput output(&out_str); |
| 1201 | bool success = CanonicalizePath(path_with_null, in_comp, &output, &out_comp); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1202 | output.Complete(); |
| 1203 | EXPECT_FALSE(success); |
| 1204 | EXPECT_EQ("/ab%00c", out_str); |
| 1205 | } |
| 1206 | |
| 1207 | TEST(URLCanonTest, Query) { |
| 1208 | struct QueryCase { |
| 1209 | const char* input8; |
| 1210 | const wchar_t* input16; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1211 | const char* expected; |
| 1212 | } query_cases[] = { |
[email protected] | 847aaab8 | 2014-05-07 14:05:46 | [diff] [blame] | 1213 | // Regular ASCII case. |
| 1214 | {"foo=bar", L"foo=bar", "?foo=bar"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1215 | // Allow question marks in the query without escaping |
[email protected] | 847aaab8 | 2014-05-07 14:05:46 | [diff] [blame] | 1216 | {"as?df", L"as?df", "?as?df"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1217 | // Always escape '#' since it would mark the ref. |
[email protected] | 847aaab8 | 2014-05-07 14:05:46 | [diff] [blame] | 1218 | {"as#df", L"as#df", "?as%23df"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1219 | // Escape some questionable 8-bit characters, but never unescape. |
[email protected] | 847aaab8 | 2014-05-07 14:05:46 | [diff] [blame] | 1220 | {"\x02hello\x7f bye", L"\x02hello\x7f bye", "?%02hello%7F%20bye"}, |
| 1221 | {"%40%41123", L"%40%41123", "?%40%41123"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1222 | // Chinese input/output |
[email protected] | 847aaab8 | 2014-05-07 14:05:46 | [diff] [blame] | 1223 | {"q=\xe4\xbd\xa0\xe5\xa5\xbd", L"q=\x4f60\x597d", "?q=%E4%BD%A0%E5%A5%BD"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1224 | // Invalid UTF-8/16 input should be replaced with invalid characters. |
[email protected] | 847aaab8 | 2014-05-07 14:05:46 | [diff] [blame] | 1225 | {"q=\xed\xed", L"q=\xd800\xd800", "?q=%EF%BF%BD%EF%BF%BD"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1226 | // Don't allow < or > because sometimes they are used for XSS if the |
| 1227 | // URL is echoed in content. Firefox does this, IE doesn't. |
[email protected] | 847aaab8 | 2014-05-07 14:05:46 | [diff] [blame] | 1228 | {"q=<asdf>", L"q=<asdf>", "?q=%3Casdf%3E"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1229 | // Escape double quotemarks in the query. |
[email protected] | 847aaab8 | 2014-05-07 14:05:46 | [diff] [blame] | 1230 | {"q=\"asdf\"", L"q=\"asdf\"", "?q=%22asdf%22"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1231 | }; |
| 1232 | |
viettrungluu | 4b691586 | 2014-10-16 03:42:49 | [diff] [blame] | 1233 | for (size_t i = 0; i < arraysize(query_cases); i++) { |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1234 | Component out_comp; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1235 | |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1236 | if (query_cases[i].input8) { |
| 1237 | int len = static_cast<int>(strlen(query_cases[i].input8)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1238 | Component in_comp(0, len); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1239 | std::string out_str; |
| 1240 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1241 | StdStringCanonOutput output(&out_str); |
[email protected] | 847aaab8 | 2014-05-07 14:05:46 | [diff] [blame] | 1242 | CanonicalizeQuery(query_cases[i].input8, in_comp, NULL, &output, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1243 | &out_comp); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1244 | output.Complete(); |
| 1245 | |
| 1246 | EXPECT_EQ(query_cases[i].expected, out_str); |
| 1247 | } |
| 1248 | |
| 1249 | if (query_cases[i].input16) { |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 1250 | base::string16 input16( |
| 1251 | test_utils::TruncateWStringToUTF16(query_cases[i].input16)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1252 | int len = static_cast<int>(input16.length()); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1253 | Component in_comp(0, len); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1254 | std::string out_str; |
| 1255 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1256 | StdStringCanonOutput output(&out_str); |
[email protected] | 847aaab8 | 2014-05-07 14:05:46 | [diff] [blame] | 1257 | CanonicalizeQuery(input16.c_str(), in_comp, NULL, &output, &out_comp); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1258 | output.Complete(); |
| 1259 | |
| 1260 | EXPECT_EQ(query_cases[i].expected, out_str); |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | // Extra test for input with embedded NULL; |
| 1265 | std::string out_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1266 | StdStringCanonOutput output(&out_str); |
| 1267 | Component out_comp; |
| 1268 | CanonicalizeQuery("a \x00z\x01", Component(0, 5), NULL, &output, &out_comp); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1269 | output.Complete(); |
| 1270 | EXPECT_EQ("?a%20%00z%01", out_str); |
| 1271 | } |
| 1272 | |
| 1273 | TEST(URLCanonTest, Ref) { |
| 1274 | // Refs are trivial, it just checks the encoding. |
| 1275 | DualComponentCase ref_cases[] = { |
| 1276 | // Regular one, we shouldn't escape spaces, et al. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1277 | {"hello, world", L"hello, world", "#hello, world", Component(1, 12), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1278 | // UTF-8/wide input should be preserved |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1279 | {"\xc2\xa9", L"\xa9", "#\xc2\xa9", Component(1, 2), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1280 | // Test a characer that takes > 16 bits (U+10300 = old italic letter A) |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1281 | {"\xF0\x90\x8C\x80ss", L"\xd800\xdf00ss", "#\xF0\x90\x8C\x80ss", Component(1, 6), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1282 | // Escaping should be preserved unchanged, even invalid ones |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1283 | {"%41%a", L"%41%a", "#%41%a", Component(1, 5), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1284 | // Invalid UTF-8/16 input should be flagged and the input made valid |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1285 | {"\xc2", NULL, "#\xef\xbf\xbd", Component(1, 3), true}, |
| 1286 | {NULL, L"\xd800\x597d", "#\xef\xbf\xbd\xe5\xa5\xbd", Component(1, 6), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1287 | // Test a Unicode invalid character. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1288 | {"a\xef\xb7\x90", L"a\xfdd0", "#a\xef\xbf\xbd", Component(1, 4), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1289 | // Refs can have # signs and we should preserve them. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1290 | {"asdf#qwer", L"asdf#qwer", "#asdf#qwer", Component(1, 9), true}, |
| 1291 | {"#asdf", L"#asdf", "##asdf", Component(1, 5), true}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1292 | }; |
| 1293 | |
| 1294 | for (size_t i = 0; i < arraysize(ref_cases); i++) { |
| 1295 | // 8-bit input |
| 1296 | if (ref_cases[i].input8) { |
| 1297 | int len = static_cast<int>(strlen(ref_cases[i].input8)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1298 | Component in_comp(0, len); |
| 1299 | Component out_comp; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1300 | |
| 1301 | std::string out_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1302 | StdStringCanonOutput output(&out_str); |
| 1303 | CanonicalizeRef(ref_cases[i].input8, in_comp, &output, &out_comp); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1304 | output.Complete(); |
| 1305 | |
| 1306 | EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin); |
| 1307 | EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len); |
| 1308 | EXPECT_EQ(ref_cases[i].expected, out_str); |
| 1309 | } |
| 1310 | |
| 1311 | // 16-bit input |
| 1312 | if (ref_cases[i].input16) { |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 1313 | base::string16 input16( |
| 1314 | test_utils::TruncateWStringToUTF16(ref_cases[i].input16)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1315 | int len = static_cast<int>(input16.length()); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1316 | Component in_comp(0, len); |
| 1317 | Component out_comp; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1318 | |
| 1319 | std::string out_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1320 | StdStringCanonOutput output(&out_str); |
| 1321 | CanonicalizeRef(input16.c_str(), in_comp, &output, &out_comp); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1322 | output.Complete(); |
| 1323 | |
| 1324 | EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin); |
| 1325 | EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len); |
| 1326 | EXPECT_EQ(ref_cases[i].expected, out_str); |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | // Try one with an embedded NULL. It should be stripped. |
| 1331 | const char null_input[5] = "ab\x00z"; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1332 | Component null_input_component(0, 4); |
| 1333 | Component out_comp; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1334 | |
| 1335 | std::string out_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1336 | StdStringCanonOutput output(&out_str); |
| 1337 | CanonicalizeRef(null_input, null_input_component, &output, &out_comp); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1338 | output.Complete(); |
| 1339 | |
| 1340 | EXPECT_EQ(1, out_comp.begin); |
| 1341 | EXPECT_EQ(3, out_comp.len); |
| 1342 | EXPECT_EQ("#abz", out_str); |
| 1343 | } |
| 1344 | |
| 1345 | TEST(URLCanonTest, CanonicalizeStandardURL) { |
| 1346 | // The individual component canonicalize tests should have caught the cases |
| 1347 | // for each of those components. Here, we just need to test that the various |
| 1348 | // parts are included or excluded properly, and have the correct separators. |
| 1349 | struct URLCase { |
| 1350 | const char* input; |
| 1351 | const char* expected; |
| 1352 | bool expected_success; |
| 1353 | } cases[] = { |
| 1354 | {"http://www.google.com/foo?bar=baz#", "http://www.google.com/foo?bar=baz#", true}, |
| 1355 | {"http://[www.google.com]/", "http://[www.google.com]/", false}, |
| 1356 | {"ht\ttp:@www.google.com:80/;p?#", "ht%09tp://www.google.com:80/;p?#", false}, |
| 1357 | {"http:////////user:@google.com:99?foo", "http://[email protected]:99/?foo", true}, |
brettw | 46f9b83 | 2016-10-05 19:22:48 | [diff] [blame] | 1358 | {"www.google.com", ":www.google.com/", false}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1359 | {"http://192.0x00A80001", "http://192.168.0.1/", true}, |
| 1360 | {"http://www/foo%2Ehtml", "http://www/foo.html", true}, |
| 1361 | {"http://user:pass@/", "http://user:pass@/", false}, |
| 1362 | {"http://%25DOMAIN:[email protected]/", "http://%25DOMAIN:[email protected]/", true}, |
| 1363 | |
| 1364 | // Backslashes should get converted to forward slashes. |
| 1365 | {"http:\\\\www.google.com\\foo", "http://www.google.com/foo", true}, |
| 1366 | |
| 1367 | // Busted refs shouldn't make the whole thing fail. |
| 1368 | {"http://www.google.com/asdf#\xc2", "http://www.google.com/asdf#\xef\xbf\xbd", true}, |
| 1369 | |
| 1370 | // Basic port tests. |
| 1371 | {"http://foo:80/", "http://foo/", true}, |
| 1372 | {"http://foo:81/", "http://foo:81/", true}, |
| 1373 | {"httpa://foo:80/", "httpa://foo:80/", true}, |
| 1374 | {"http://foo:-80/", "http://foo:-80/", false}, |
| 1375 | |
| 1376 | {"https://foo:443/", "https://foo/", true}, |
| 1377 | {"https://foo:80/", "https://foo:80/", true}, |
| 1378 | {"ftp://foo:21/", "ftp://foo/", true}, |
| 1379 | {"ftp://foo:80/", "ftp://foo:80/", true}, |
| 1380 | {"gopher://foo:70/", "gopher://foo/", true}, |
| 1381 | {"gopher://foo:443/", "gopher://foo:443/", true}, |
| 1382 | {"ws://foo:80/", "ws://foo/", true}, |
| 1383 | {"ws://foo:81/", "ws://foo:81/", true}, |
| 1384 | {"ws://foo:443/", "ws://foo:443/", true}, |
| 1385 | {"ws://foo:815/", "ws://foo:815/", true}, |
| 1386 | {"wss://foo:80/", "wss://foo:80/", true}, |
| 1387 | {"wss://foo:81/", "wss://foo:81/", true}, |
| 1388 | {"wss://foo:443/", "wss://foo/", true}, |
| 1389 | {"wss://foo:815/", "wss://foo:815/", true}, |
brettw | 1141951b | 2015-11-26 00:29:35 | [diff] [blame] | 1390 | |
| 1391 | // This particular code path ends up "backing up" to replace an invalid |
| 1392 | // host ICU generated with an escaped version. Test that in the context |
| 1393 | // of a full URL to make sure the backing up doesn't mess up the non-host |
| 1394 | // parts of the URL. "EF B9 AA" is U+FE6A which is a type of percent that |
| 1395 | // ICU will convert to an ASCII one, generating "%81". |
| 1396 | {"ws:)W\x1eW\xef\xb9\xaa""81:80/", "ws://%29w%1ew%81/", false}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1397 | }; |
| 1398 | |
viettrungluu | 4b691586 | 2014-10-16 03:42:49 | [diff] [blame] | 1399 | for (size_t i = 0; i < arraysize(cases); i++) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1400 | int url_len = static_cast<int>(strlen(cases[i].input)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1401 | Parsed parsed; |
| 1402 | ParseStandardURL(cases[i].input, url_len, &parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1403 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1404 | Parsed out_parsed; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1405 | std::string out_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1406 | StdStringCanonOutput output(&out_str); |
| 1407 | bool success = CanonicalizeStandardURL( |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1408 | cases[i].input, url_len, parsed, NULL, &output, &out_parsed); |
| 1409 | output.Complete(); |
| 1410 | |
| 1411 | EXPECT_EQ(cases[i].expected_success, success); |
| 1412 | EXPECT_EQ(cases[i].expected, out_str); |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | // The codepath here is the same as for regular canonicalization, so we just |
| 1417 | // need to test that things are replaced or not correctly. |
| 1418 | TEST(URLCanonTest, ReplaceStandardURL) { |
| 1419 | ReplaceCase replace_cases[] = { |
| 1420 | // Common case of truncating the path. |
| 1421 | {"http://www.google.com/foo?bar=baz#ref", NULL, NULL, NULL, NULL, NULL, "/", kDeleteComp, kDeleteComp, "http://www.google.com/"}, |
| 1422 | // Replace everything |
| 1423 | {"http://a:[email protected]:22/foo;bar?baz@cat", "https", "me", "pw", "host.com", "99", "/path", "query", "ref", "https://me:[email protected]:99/path?query#ref"}, |
| 1424 | // Replace nothing |
| 1425 | {"http://a:[email protected]:22/foo?baz@cat", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "http://a:[email protected]:22/foo?baz@cat"}, |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 1426 | // Replace scheme with filesystem. The result is garbage, but you asked |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1427 | // for it. |
| 1428 | {"http://a:[email protected]:22/foo?baz@cat", "filesystem", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem://a:[email protected]:22/foo?baz@cat"}, |
| 1429 | }; |
| 1430 | |
| 1431 | for (size_t i = 0; i < arraysize(replace_cases); i++) { |
| 1432 | const ReplaceCase& cur = replace_cases[i]; |
| 1433 | int base_len = static_cast<int>(strlen(cur.base)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1434 | Parsed parsed; |
| 1435 | ParseStandardURL(cur.base, base_len, &parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1436 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1437 | Replacements<char> r; |
| 1438 | typedef Replacements<char> R; // Clean up syntax. |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1439 | |
| 1440 | // Note that for the scheme we pass in a different clear function since |
| 1441 | // there is no function to clear the scheme. |
| 1442 | SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme); |
| 1443 | SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username); |
| 1444 | SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password); |
| 1445 | SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host); |
| 1446 | SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port); |
| 1447 | SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path); |
| 1448 | SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query); |
| 1449 | SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref); |
| 1450 | |
| 1451 | std::string out_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1452 | StdStringCanonOutput output(&out_str); |
| 1453 | Parsed out_parsed; |
| 1454 | ReplaceStandardURL(replace_cases[i].base, parsed, r, NULL, &output, |
| 1455 | &out_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1456 | output.Complete(); |
| 1457 | |
| 1458 | EXPECT_EQ(replace_cases[i].expected, out_str); |
| 1459 | } |
| 1460 | |
| 1461 | // The path pointer should be ignored if the address is invalid. |
| 1462 | { |
| 1463 | const char src[] = "http://www.google.com/here_is_the_path"; |
| 1464 | int src_len = static_cast<int>(strlen(src)); |
| 1465 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1466 | Parsed parsed; |
| 1467 | ParseStandardURL(src, src_len, &parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1468 | |
| 1469 | // Replace the path to 0 length string. By using 1 as the string address, |
| 1470 | // the test should get an access violation if it tries to dereference it. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1471 | Replacements<char> r; |
| 1472 | r.SetPath(reinterpret_cast<char*>(0x00000001), Component(0, 0)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1473 | std::string out_str1; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1474 | StdStringCanonOutput output1(&out_str1); |
| 1475 | Parsed new_parsed; |
| 1476 | ReplaceStandardURL(src, parsed, r, NULL, &output1, &new_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1477 | output1.Complete(); |
| 1478 | EXPECT_STREQ("http://www.google.com/", out_str1.c_str()); |
| 1479 | |
| 1480 | // Same with an "invalid" path. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1481 | r.SetPath(reinterpret_cast<char*>(0x00000001), Component()); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1482 | std::string out_str2; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1483 | StdStringCanonOutput output2(&out_str2); |
| 1484 | ReplaceStandardURL(src, parsed, r, NULL, &output2, &new_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1485 | output2.Complete(); |
| 1486 | EXPECT_STREQ("http://www.google.com/", out_str2.c_str()); |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | TEST(URLCanonTest, ReplaceFileURL) { |
| 1491 | ReplaceCase replace_cases[] = { |
| 1492 | // Replace everything |
| 1493 | {"file:///C:/gaba?query#ref", NULL, NULL, NULL, "filer", NULL, "/foo", "b", "c", "file://filer/foo?b#c"}, |
| 1494 | // Replace nothing |
| 1495 | {"file:///C:/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///C:/gaba?query#ref"}, |
| 1496 | // Clear non-path components (common) |
| 1497 | {"file:///C:/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "file:///C:/gaba"}, |
| 1498 | // Replace path with something that doesn't begin with a slash and make |
| 1499 | // sure it gets added properly. |
| 1500 | {"file:///C:/gaba", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "file:///interesting/"}, |
| 1501 | {"file:///home/gaba?query#ref", NULL, NULL, NULL, "filer", NULL, "/foo", "b", "c", "file://filer/foo?b#c"}, |
| 1502 | {"file:///home/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///home/gaba?query#ref"}, |
| 1503 | {"file:///home/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "file:///home/gaba"}, |
| 1504 | {"file:///home/gaba", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "file:///interesting/"}, |
| 1505 | // Replace scheme -- shouldn't do anything. |
| 1506 | {"file:///C:/gaba?query#ref", "http", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///C:/gaba?query#ref"}, |
| 1507 | }; |
| 1508 | |
| 1509 | for (size_t i = 0; i < arraysize(replace_cases); i++) { |
| 1510 | const ReplaceCase& cur = replace_cases[i]; |
| 1511 | int base_len = static_cast<int>(strlen(cur.base)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1512 | Parsed parsed; |
| 1513 | ParseFileURL(cur.base, base_len, &parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1514 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1515 | Replacements<char> r; |
| 1516 | typedef Replacements<char> R; // Clean up syntax. |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1517 | SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme); |
| 1518 | SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username); |
| 1519 | SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password); |
| 1520 | SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host); |
| 1521 | SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port); |
| 1522 | SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path); |
| 1523 | SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query); |
| 1524 | SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref); |
| 1525 | |
| 1526 | std::string out_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1527 | StdStringCanonOutput output(&out_str); |
| 1528 | Parsed out_parsed; |
| 1529 | ReplaceFileURL(cur.base, parsed, r, NULL, &output, &out_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1530 | output.Complete(); |
| 1531 | |
| 1532 | EXPECT_EQ(replace_cases[i].expected, out_str); |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | TEST(URLCanonTest, ReplaceFileSystemURL) { |
| 1537 | ReplaceCase replace_cases[] = { |
| 1538 | // Replace everything in the outer URL. |
| 1539 | {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, "/foo", "b", "c", "filesystem:file:///temporary/foo?b#c"}, |
| 1540 | // Replace nothing |
| 1541 | {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem:file:///temporary/gaba?query#ref"}, |
| 1542 | // Clear non-path components (common) |
| 1543 | {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "filesystem:file:///temporary/gaba"}, |
| 1544 | // Replace path with something that doesn't begin with a slash and make |
| 1545 | // sure it gets added properly. |
| 1546 | {"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "filesystem:file:///temporary/interesting/?query#ref"}, |
| 1547 | // Replace scheme -- shouldn't do anything. |
| 1548 | {"filesystem:http://u:[email protected]/t/gaba?query#ref", "http", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem:http://u:[email protected]/t/gaba?query#ref"}, |
| 1549 | // Replace username -- shouldn't do anything. |
| 1550 | {"filesystem:http://u:[email protected]/t/gaba?query#ref", NULL, "u2", NULL, NULL, NULL, NULL, NULL, NULL, "filesystem:http://u:[email protected]/t/gaba?query#ref"}, |
| 1551 | // Replace password -- shouldn't do anything. |
| 1552 | {"filesystem:http://u:[email protected]/t/gaba?query#ref", NULL, NULL, "pw2", NULL, NULL, NULL, NULL, NULL, "filesystem:http://u:[email protected]/t/gaba?query#ref"}, |
| 1553 | // Replace host -- shouldn't do anything. |
| 1554 | {"filesystem:http://u:[email protected]/t/gaba?query#ref", NULL, NULL, NULL, "foo.com", NULL, NULL, NULL, NULL, "filesystem:http://u:[email protected]/t/gaba?query#ref"}, |
| 1555 | // Replace port -- shouldn't do anything. |
| 1556 | {"filesystem:http://u:[email protected]:40/t/gaba?query#ref", NULL, NULL, NULL, NULL, "41", NULL, NULL, NULL, "filesystem:http://u:[email protected]:40/t/gaba?query#ref"}, |
| 1557 | }; |
| 1558 | |
| 1559 | for (size_t i = 0; i < arraysize(replace_cases); i++) { |
| 1560 | const ReplaceCase& cur = replace_cases[i]; |
| 1561 | int base_len = static_cast<int>(strlen(cur.base)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1562 | Parsed parsed; |
| 1563 | ParseFileSystemURL(cur.base, base_len, &parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1564 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1565 | Replacements<char> r; |
| 1566 | typedef Replacements<char> R; // Clean up syntax. |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1567 | SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme); |
| 1568 | SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username); |
| 1569 | SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password); |
| 1570 | SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host); |
| 1571 | SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port); |
| 1572 | SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path); |
| 1573 | SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query); |
| 1574 | SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref); |
| 1575 | |
| 1576 | std::string out_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1577 | StdStringCanonOutput output(&out_str); |
| 1578 | Parsed out_parsed; |
| 1579 | ReplaceFileSystemURL(cur.base, parsed, r, NULL, &output, &out_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1580 | output.Complete(); |
| 1581 | |
| 1582 | EXPECT_EQ(replace_cases[i].expected, out_str); |
| 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | TEST(URLCanonTest, ReplacePathURL) { |
| 1587 | ReplaceCase replace_cases[] = { |
| 1588 | // Replace everything |
| 1589 | {"data:foo", "javascript", NULL, NULL, NULL, NULL, "alert('foo?');", NULL, NULL, "javascript:alert('foo?');"}, |
| 1590 | // Replace nothing |
| 1591 | {"data:foo", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "data:foo"}, |
| 1592 | // Replace one or the other |
| 1593 | {"data:foo", "javascript", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "javascript:foo"}, |
| 1594 | {"data:foo", NULL, NULL, NULL, NULL, NULL, "bar", NULL, NULL, "data:bar"}, |
| 1595 | {"data:foo", NULL, NULL, NULL, NULL, NULL, kDeleteComp, NULL, NULL, "data:"}, |
| 1596 | }; |
| 1597 | |
| 1598 | for (size_t i = 0; i < arraysize(replace_cases); i++) { |
| 1599 | const ReplaceCase& cur = replace_cases[i]; |
| 1600 | int base_len = static_cast<int>(strlen(cur.base)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1601 | Parsed parsed; |
| 1602 | ParsePathURL(cur.base, base_len, false, &parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1603 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1604 | Replacements<char> r; |
| 1605 | typedef Replacements<char> R; // Clean up syntax. |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1606 | SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme); |
| 1607 | SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username); |
| 1608 | SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password); |
| 1609 | SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host); |
| 1610 | SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port); |
| 1611 | SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path); |
| 1612 | SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query); |
| 1613 | SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref); |
| 1614 | |
| 1615 | std::string out_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1616 | StdStringCanonOutput output(&out_str); |
| 1617 | Parsed out_parsed; |
| 1618 | ReplacePathURL(cur.base, parsed, r, &output, &out_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1619 | output.Complete(); |
| 1620 | |
| 1621 | EXPECT_EQ(replace_cases[i].expected, out_str); |
| 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | TEST(URLCanonTest, ReplaceMailtoURL) { |
| 1626 | ReplaceCase replace_cases[] = { |
| 1627 | // Replace everything |
| 1628 | {"mailto:[email protected]?body=sup", "mailto", NULL, NULL, NULL, NULL, "addr1", "to=tony", NULL, "mailto:addr1?to=tony"}, |
| 1629 | // Replace nothing |
| 1630 | {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "mailto:[email protected]?body=sup"}, |
| 1631 | // Replace the path |
| 1632 | {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", NULL, NULL, "mailto:jason?body=sup"}, |
| 1633 | // Replace the query |
| 1634 | {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "custom=1", NULL, "mailto:[email protected]?custom=1"}, |
| 1635 | // Replace the path and query |
| 1636 | {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", "custom=1", NULL, "mailto:jason?custom=1"}, |
| 1637 | // Set the query to empty (should leave trailing question mark) |
| 1638 | {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "", NULL, "mailto:[email protected]?"}, |
| 1639 | // Clear the query |
| 1640 | {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "|", NULL, "mailto:[email protected]"}, |
| 1641 | // Clear the path |
| 1642 | {"mailto:[email protected]?body=sup", NULL, NULL, NULL, NULL, NULL, "|", NULL, NULL, "mailto:?body=sup"}, |
| 1643 | // Clear the path + query |
| 1644 | {"mailto:", NULL, NULL, NULL, NULL, NULL, "|", "|", NULL, "mailto:"}, |
| 1645 | // Setting the ref should have no effect |
| 1646 | {"mailto:addr1", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "BLAH", "mailto:addr1"}, |
| 1647 | }; |
| 1648 | |
| 1649 | for (size_t i = 0; i < arraysize(replace_cases); i++) { |
| 1650 | const ReplaceCase& cur = replace_cases[i]; |
| 1651 | int base_len = static_cast<int>(strlen(cur.base)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1652 | Parsed parsed; |
| 1653 | ParseMailtoURL(cur.base, base_len, &parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1654 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1655 | Replacements<char> r; |
| 1656 | typedef Replacements<char> R; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1657 | SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme); |
| 1658 | SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username); |
| 1659 | SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password); |
| 1660 | SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host); |
| 1661 | SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port); |
| 1662 | SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path); |
| 1663 | SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query); |
| 1664 | SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref); |
| 1665 | |
| 1666 | std::string out_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1667 | StdStringCanonOutput output(&out_str); |
| 1668 | Parsed out_parsed; |
| 1669 | ReplaceMailtoURL(cur.base, parsed, r, &output, &out_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1670 | output.Complete(); |
| 1671 | |
| 1672 | EXPECT_EQ(replace_cases[i].expected, out_str); |
| 1673 | } |
| 1674 | } |
| 1675 | |
| 1676 | TEST(URLCanonTest, CanonicalizeFileURL) { |
| 1677 | struct URLCase { |
| 1678 | const char* input; |
| 1679 | const char* expected; |
| 1680 | bool expected_success; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1681 | Component expected_host; |
| 1682 | Component expected_path; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1683 | } cases[] = { |
| 1684 | #ifdef _WIN32 |
| 1685 | // Windows-style paths |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1686 | {"file:c:\\foo\\bar.html", "file:///C:/foo/bar.html", true, Component(), Component(7, 16)}, |
| 1687 | {" File:c|////foo\\bar.html", "file:///C:////foo/bar.html", true, Component(), Component(7, 19)}, |
| 1688 | {"file:", "file:///", true, Component(), Component(7, 1)}, |
| 1689 | {"file:UNChost/path", "file://unchost/path", true, Component(7, 7), Component(14, 5)}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1690 | // CanonicalizeFileURL supports absolute Windows style paths for IE |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 1691 | // compatibility. Note that the caller must decide that this is a file |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1692 | // URL itself so it can call the file canonicalizer. This is usually |
| 1693 | // done automatically as part of relative URL resolving. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1694 | {"c:\\foo\\bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)}, |
| 1695 | {"C|/foo/bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)}, |
| 1696 | {"/C|\\foo\\bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)}, |
| 1697 | {"//C|/foo/bar", "file:///C:/foo/bar", true, Component(), Component(7, 11)}, |
| 1698 | {"//server/file", "file://server/file", true, Component(7, 6), Component(13, 5)}, |
| 1699 | {"\\\\server\\file", "file://server/file", true, Component(7, 6), Component(13, 5)}, |
| 1700 | {"/\\server/file", "file://server/file", true, Component(7, 6), Component(13, 5)}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1701 | // We should preserve the number of slashes after the colon for IE |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 1702 | // compatibility, except when there is none, in which case we should |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1703 | // add one. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1704 | {"file:c:foo/bar.html", "file:///C:/foo/bar.html", true, Component(), Component(7, 16)}, |
| 1705 | {"file:/\\/\\C:\\\\//foo\\bar.html", "file:///C:////foo/bar.html", true, Component(), Component(7, 19)}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1706 | // Three slashes should be non-UNC, even if there is no drive spec (IE |
| 1707 | // does this, which makes the resulting request invalid). |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1708 | {"file:///foo/bar.txt", "file:///foo/bar.txt", true, Component(), Component(7, 12)}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1709 | // TODO(brettw) we should probably fail for invalid host names, which |
| 1710 | // would change the expected result on this test. We also currently allow |
| 1711 | // colon even though it's probably invalid, because its currently the |
| 1712 | // "natural" result of the way the canonicalizer is written. There doesn't |
| 1713 | // seem to be a strong argument for why allowing it here would be bad, so |
| 1714 | // we just tolerate it and the load will fail later. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1715 | {"FILE:/\\/\\7:\\\\//foo\\bar.html", "file://7:////foo/bar.html", false, Component(7, 2), Component(9, 16)}, |
| 1716 | {"file:filer/home\\me", "file://filer/home/me", true, Component(7, 5), Component(12, 8)}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1717 | // Make sure relative paths can't go above the "C:" |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1718 | {"file:///C:/foo/../../../bar.html", "file:///C:/bar.html", true, Component(), Component(7, 12)}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1719 | // Busted refs shouldn't make the whole thing fail. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1720 | {"file:///C:/asdf#\xc2", "file:///C:/asdf#\xef\xbf\xbd", true, Component(), Component(7, 8)}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1721 | #else |
| 1722 | // Unix-style paths |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1723 | {"file:///home/me", "file:///home/me", true, Component(), Component(7, 8)}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1724 | // Windowsy ones should get still treated as Unix-style. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1725 | {"file:c:\\foo\\bar.html", "file:///c:/foo/bar.html", true, Component(), Component(7, 16)}, |
| 1726 | {"file:c|//foo\\bar.html", "file:///c%7C//foo/bar.html", true, Component(), Component(7, 19)}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1727 | // file: tests from WebKit (LayoutTests/fast/loader/url-parse-1.html) |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1728 | {"//", "file:///", true, Component(), Component(7, 1)}, |
| 1729 | {"///", "file:///", true, Component(), Component(7, 1)}, |
| 1730 | {"///test", "file:///test", true, Component(), Component(7, 5)}, |
| 1731 | {"file://test", "file://test/", true, Component(7, 4), Component(11, 1)}, |
| 1732 | {"file://localhost", "file://localhost/", true, Component(7, 9), Component(16, 1)}, |
| 1733 | {"file://localhost/", "file://localhost/", true, Component(7, 9), Component(16, 1)}, |
| 1734 | {"file://localhost/test", "file://localhost/test", true, Component(7, 9), Component(16, 5)}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1735 | #endif // _WIN32 |
| 1736 | }; |
| 1737 | |
viettrungluu | 4b691586 | 2014-10-16 03:42:49 | [diff] [blame] | 1738 | for (size_t i = 0; i < arraysize(cases); i++) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1739 | int url_len = static_cast<int>(strlen(cases[i].input)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1740 | Parsed parsed; |
| 1741 | ParseFileURL(cases[i].input, url_len, &parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1742 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1743 | Parsed out_parsed; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1744 | std::string out_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1745 | StdStringCanonOutput output(&out_str); |
| 1746 | bool success = CanonicalizeFileURL(cases[i].input, url_len, parsed, NULL, |
| 1747 | &output, &out_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1748 | output.Complete(); |
| 1749 | |
| 1750 | EXPECT_EQ(cases[i].expected_success, success); |
| 1751 | EXPECT_EQ(cases[i].expected, out_str); |
| 1752 | |
| 1753 | // Make sure the spec was properly identified, the file canonicalizer has |
| 1754 | // different code for writing the spec. |
| 1755 | EXPECT_EQ(0, out_parsed.scheme.begin); |
| 1756 | EXPECT_EQ(4, out_parsed.scheme.len); |
| 1757 | |
| 1758 | EXPECT_EQ(cases[i].expected_host.begin, out_parsed.host.begin); |
| 1759 | EXPECT_EQ(cases[i].expected_host.len, out_parsed.host.len); |
| 1760 | |
| 1761 | EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin); |
| 1762 | EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len); |
| 1763 | } |
| 1764 | } |
| 1765 | |
| 1766 | TEST(URLCanonTest, CanonicalizeFileSystemURL) { |
| 1767 | struct URLCase { |
| 1768 | const char* input; |
| 1769 | const char* expected; |
| 1770 | bool expected_success; |
| 1771 | } cases[] = { |
| 1772 | {"Filesystem:htTp://www.Foo.com:80/tempoRary", "filesystem:http://www.foo.com/tempoRary/", true}, |
| 1773 | {"filesystem:httpS://www.foo.com/temporary/", "filesystem:https://www.foo.com/temporary/", true}, |
| 1774 | {"filesystem:http://www.foo.com//", "filesystem:http://www.foo.com//", false}, |
| 1775 | {"filesystem:http://www.foo.com/persistent/bob?query#ref", "filesystem:http://www.foo.com/persistent/bob?query#ref", true}, |
| 1776 | {"filesystem:fIle://\\temporary/", "filesystem:file:///temporary/", true}, |
| 1777 | {"filesystem:fiLe:///temporary", "filesystem:file:///temporary/", true}, |
| 1778 | {"filesystem:File:///temporary/Bob?qUery#reF", "filesystem:file:///temporary/Bob?qUery#reF", true}, |
| 1779 | }; |
| 1780 | |
viettrungluu | 4b691586 | 2014-10-16 03:42:49 | [diff] [blame] | 1781 | for (size_t i = 0; i < arraysize(cases); i++) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1782 | int url_len = static_cast<int>(strlen(cases[i].input)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1783 | Parsed parsed; |
| 1784 | ParseFileSystemURL(cases[i].input, url_len, &parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1785 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1786 | Parsed out_parsed; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1787 | std::string out_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1788 | StdStringCanonOutput output(&out_str); |
| 1789 | bool success = CanonicalizeFileSystemURL(cases[i].input, url_len, parsed, |
| 1790 | NULL, &output, &out_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1791 | output.Complete(); |
| 1792 | |
| 1793 | EXPECT_EQ(cases[i].expected_success, success); |
| 1794 | EXPECT_EQ(cases[i].expected, out_str); |
| 1795 | |
| 1796 | // Make sure the spec was properly identified, the filesystem canonicalizer |
| 1797 | // has different code for writing the spec. |
| 1798 | EXPECT_EQ(0, out_parsed.scheme.begin); |
| 1799 | EXPECT_EQ(10, out_parsed.scheme.len); |
| 1800 | if (success) |
| 1801 | EXPECT_GT(out_parsed.path.len, 0); |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | TEST(URLCanonTest, CanonicalizePathURL) { |
| 1806 | // Path URLs should get canonicalized schemes but nothing else. |
| 1807 | struct PathCase { |
| 1808 | const char* input; |
| 1809 | const char* expected; |
| 1810 | } path_cases[] = { |
| 1811 | {"javascript:", "javascript:"}, |
| 1812 | {"JavaScript:Foo", "javascript:Foo"}, |
brettw | 46f9b83 | 2016-10-05 19:22:48 | [diff] [blame] | 1813 | {"Foo:\":This /is interesting;?#", "foo:\":This /is interesting;?#"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1814 | }; |
| 1815 | |
viettrungluu | 4b691586 | 2014-10-16 03:42:49 | [diff] [blame] | 1816 | for (size_t i = 0; i < arraysize(path_cases); i++) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1817 | int url_len = static_cast<int>(strlen(path_cases[i].input)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1818 | Parsed parsed; |
| 1819 | ParsePathURL(path_cases[i].input, url_len, true, &parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1820 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1821 | Parsed out_parsed; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1822 | std::string out_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1823 | StdStringCanonOutput output(&out_str); |
| 1824 | bool success = CanonicalizePathURL(path_cases[i].input, url_len, parsed, |
| 1825 | &output, &out_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1826 | output.Complete(); |
| 1827 | |
| 1828 | EXPECT_TRUE(success); |
| 1829 | EXPECT_EQ(path_cases[i].expected, out_str); |
| 1830 | |
| 1831 | EXPECT_EQ(0, out_parsed.host.begin); |
| 1832 | EXPECT_EQ(-1, out_parsed.host.len); |
| 1833 | |
| 1834 | // When we end with a colon at the end, there should be no path. |
| 1835 | if (path_cases[i].input[url_len - 1] == ':') { |
[email protected] | 369e84f7 | 2013-11-23 01:53:52 | [diff] [blame] | 1836 | EXPECT_EQ(0, out_parsed.GetContent().begin); |
| 1837 | EXPECT_EQ(-1, out_parsed.GetContent().len); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1838 | } |
| 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | TEST(URLCanonTest, CanonicalizeMailtoURL) { |
| 1843 | struct URLCase { |
| 1844 | const char* input; |
| 1845 | const char* expected; |
| 1846 | bool expected_success; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1847 | Component expected_path; |
| 1848 | Component expected_query; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1849 | } cases[] = { |
elawrence | d7548509 | 2017-04-18 20:44:20 | [diff] [blame] | 1850 | // Null character should be escaped to %00. |
| 1851 | // Keep this test first in the list as it is handled specially below. |
| 1852 | {"mailto:addr1\0addr2?foo", |
| 1853 | "mailto:addr1%00addr2?foo", |
| 1854 | true, Component(7, 13), Component(21, 3)}, |
| 1855 | {"mailto:addr1", |
| 1856 | "mailto:addr1", |
| 1857 | true, Component(7, 5), Component()}, |
| 1858 | {"mailto:[email protected]", |
| 1859 | "mailto:[email protected]", |
| 1860 | true, Component(7, 13), Component()}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1861 | // Trailing whitespace is stripped. |
elawrence | d7548509 | 2017-04-18 20:44:20 | [diff] [blame] | 1862 | {"MaIlTo:addr1 \t ", |
| 1863 | "mailto:addr1", |
| 1864 | true, Component(7, 5), Component()}, |
| 1865 | {"MaIlTo:addr1?to=jon", |
| 1866 | "mailto:addr1?to=jon", |
| 1867 | true, Component(7, 5), Component(13,6)}, |
| 1868 | {"mailto:addr1,addr2", |
| 1869 | "mailto:addr1,addr2", |
| 1870 | true, Component(7, 11), Component()}, |
| 1871 | // Embedded spaces must be encoded. |
| 1872 | {"mailto:addr1, addr2", |
| 1873 | "mailto:addr1,%20addr2", |
| 1874 | true, Component(7, 14), Component()}, |
| 1875 | {"mailto:addr1, addr2?subject=one two ", |
| 1876 | "mailto:addr1,%20addr2?subject=one%20two", |
| 1877 | true, Component(7, 14), Component(22, 17)}, |
| 1878 | {"mailto:addr1%2caddr2", |
| 1879 | "mailto:addr1%2caddr2", |
| 1880 | true, Component(7, 13), Component()}, |
| 1881 | {"mailto:\xF0\x90\x8C\x80", |
| 1882 | "mailto:%F0%90%8C%80", |
| 1883 | true, Component(7, 12), Component()}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1884 | // Invalid -- UTF-8 encoded surrogate value. |
elawrence | d7548509 | 2017-04-18 20:44:20 | [diff] [blame] | 1885 | {"mailto:\xed\xa0\x80", |
| 1886 | "mailto:%EF%BF%BD", |
| 1887 | false, Component(7, 9), Component()}, |
| 1888 | {"mailto:addr1?", |
| 1889 | "mailto:addr1?", |
| 1890 | true, Component(7, 5), Component(13, 0)}, |
| 1891 | // Certain characters have special meanings and must be encoded. |
| 1892 | {"mailto:! \x22$&()+,-./09:;<=>@AZ[\\]&_`az{|}~\x7f?Query! \x22$&()+,-./09:;<=>@AZ[\\]&_`az{|}~", |
| 1893 | "mailto:!%20%22$&()+,-./09:;%3C=%3E@AZ[\\]&_%60az%7B%7C%7D~%7F?Query!%20%22$&()+,-./09:;%3C=%3E@AZ[\\]&_`az{|}~", |
| 1894 | true, Component(7, 53), Component(61, 47)}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1895 | }; |
| 1896 | |
| 1897 | // Define outside of loop to catch bugs where components aren't reset |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1898 | Parsed parsed; |
| 1899 | Parsed out_parsed; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1900 | |
viettrungluu | 4b691586 | 2014-10-16 03:42:49 | [diff] [blame] | 1901 | for (size_t i = 0; i < arraysize(cases); i++) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1902 | int url_len = static_cast<int>(strlen(cases[i].input)); |
elawrence | d7548509 | 2017-04-18 20:44:20 | [diff] [blame] | 1903 | if (i == 0) { |
| 1904 | // The first test case purposely has a '\0' in it -- don't count it |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1905 | // as the string terminator. |
| 1906 | url_len = 22; |
| 1907 | } |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1908 | ParseMailtoURL(cases[i].input, url_len, &parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1909 | |
| 1910 | std::string out_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1911 | StdStringCanonOutput output(&out_str); |
| 1912 | bool success = CanonicalizeMailtoURL(cases[i].input, url_len, parsed, |
| 1913 | &output, &out_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1914 | output.Complete(); |
| 1915 | |
| 1916 | EXPECT_EQ(cases[i].expected_success, success); |
| 1917 | EXPECT_EQ(cases[i].expected, out_str); |
| 1918 | |
| 1919 | // Make sure the spec was properly identified |
| 1920 | EXPECT_EQ(0, out_parsed.scheme.begin); |
| 1921 | EXPECT_EQ(6, out_parsed.scheme.len); |
| 1922 | |
| 1923 | EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin); |
| 1924 | EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len); |
| 1925 | |
| 1926 | EXPECT_EQ(cases[i].expected_query.begin, out_parsed.query.begin); |
| 1927 | EXPECT_EQ(cases[i].expected_query.len, out_parsed.query.len); |
| 1928 | } |
| 1929 | } |
| 1930 | |
| 1931 | #ifndef WIN32 |
| 1932 | |
| 1933 | TEST(URLCanonTest, _itoa_s) { |
| 1934 | // We fill the buffer with 0xff to ensure that it's getting properly |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 1935 | // null-terminated. We also allocate one byte more than what we tell |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1936 | // _itoa_s about, and ensure that the extra byte is untouched. |
| 1937 | char buf[6]; |
| 1938 | memset(buf, 0xff, sizeof(buf)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1939 | EXPECT_EQ(0, _itoa_s(12, buf, sizeof(buf) - 1, 10)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1940 | EXPECT_STREQ("12", buf); |
| 1941 | EXPECT_EQ('\xFF', buf[3]); |
| 1942 | |
| 1943 | // Test the edge cases - exactly the buffer size and one over |
| 1944 | memset(buf, 0xff, sizeof(buf)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1945 | EXPECT_EQ(0, _itoa_s(1234, buf, sizeof(buf) - 1, 10)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1946 | EXPECT_STREQ("1234", buf); |
| 1947 | EXPECT_EQ('\xFF', buf[5]); |
| 1948 | |
| 1949 | memset(buf, 0xff, sizeof(buf)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1950 | EXPECT_EQ(EINVAL, _itoa_s(12345, buf, sizeof(buf) - 1, 10)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1951 | EXPECT_EQ('\xFF', buf[5]); // should never write to this location |
| 1952 | |
| 1953 | // Test the template overload (note that this will see the full buffer) |
| 1954 | memset(buf, 0xff, sizeof(buf)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1955 | EXPECT_EQ(0, _itoa_s(12, buf, 10)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1956 | EXPECT_STREQ("12", buf); |
| 1957 | EXPECT_EQ('\xFF', buf[3]); |
| 1958 | |
| 1959 | memset(buf, 0xff, sizeof(buf)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1960 | EXPECT_EQ(0, _itoa_s(12345, buf, 10)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1961 | EXPECT_STREQ("12345", buf); |
| 1962 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1963 | EXPECT_EQ(EINVAL, _itoa_s(123456, buf, 10)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1964 | |
| 1965 | // Test that radix 16 is supported. |
| 1966 | memset(buf, 0xff, sizeof(buf)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1967 | EXPECT_EQ(0, _itoa_s(1234, buf, sizeof(buf) - 1, 16)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1968 | EXPECT_STREQ("4d2", buf); |
| 1969 | EXPECT_EQ('\xFF', buf[5]); |
| 1970 | } |
| 1971 | |
| 1972 | TEST(URLCanonTest, _itow_s) { |
| 1973 | // We fill the buffer with 0xff to ensure that it's getting properly |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 1974 | // null-terminated. We also allocate one byte more than what we tell |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1975 | // _itoa_s about, and ensure that the extra byte is untouched. |
[email protected] | 3774f83 | 2013-06-11 21:21:57 | [diff] [blame] | 1976 | base::char16 buf[6]; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1977 | const char fill_mem = 0xff; |
[email protected] | 3774f83 | 2013-06-11 21:21:57 | [diff] [blame] | 1978 | const base::char16 fill_char = 0xffff; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1979 | memset(buf, fill_mem, sizeof(buf)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1980 | EXPECT_EQ(0, _itow_s(12, buf, sizeof(buf) / 2 - 1, 10)); |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 1981 | EXPECT_EQ(base::UTF8ToUTF16("12"), base::string16(buf)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1982 | EXPECT_EQ(fill_char, buf[3]); |
| 1983 | |
| 1984 | // Test the edge cases - exactly the buffer size and one over |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1985 | EXPECT_EQ(0, _itow_s(1234, buf, sizeof(buf) / 2 - 1, 10)); |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 1986 | EXPECT_EQ(base::UTF8ToUTF16("1234"), base::string16(buf)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1987 | EXPECT_EQ(fill_char, buf[5]); |
| 1988 | |
| 1989 | memset(buf, fill_mem, sizeof(buf)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1990 | EXPECT_EQ(EINVAL, _itow_s(12345, buf, sizeof(buf) / 2 - 1, 10)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1991 | EXPECT_EQ(fill_char, buf[5]); // should never write to this location |
| 1992 | |
| 1993 | // Test the template overload (note that this will see the full buffer) |
| 1994 | memset(buf, fill_mem, sizeof(buf)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 1995 | EXPECT_EQ(0, _itow_s(12, buf, 10)); |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 1996 | EXPECT_EQ(base::UTF8ToUTF16("12"), |
| 1997 | base::string16(buf)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 1998 | EXPECT_EQ(fill_char, buf[3]); |
| 1999 | |
| 2000 | memset(buf, fill_mem, sizeof(buf)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2001 | EXPECT_EQ(0, _itow_s(12345, buf, 10)); |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 2002 | EXPECT_EQ(base::UTF8ToUTF16("12345"), base::string16(buf)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2003 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2004 | EXPECT_EQ(EINVAL, _itow_s(123456, buf, 10)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2005 | } |
| 2006 | |
| 2007 | #endif // !WIN32 |
| 2008 | |
| 2009 | // Returns true if the given two structures are the same. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2010 | static bool ParsedIsEqual(const Parsed& a, const Parsed& b) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2011 | return a.scheme.begin == b.scheme.begin && a.scheme.len == b.scheme.len && |
| 2012 | a.username.begin == b.username.begin && a.username.len == b.username.len && |
| 2013 | a.password.begin == b.password.begin && a.password.len == b.password.len && |
| 2014 | a.host.begin == b.host.begin && a.host.len == b.host.len && |
| 2015 | a.port.begin == b.port.begin && a.port.len == b.port.len && |
| 2016 | a.path.begin == b.path.begin && a.path.len == b.path.len && |
| 2017 | a.query.begin == b.query.begin && a.query.len == b.query.len && |
| 2018 | a.ref.begin == b.ref.begin && a.ref.len == b.ref.len; |
| 2019 | } |
| 2020 | |
| 2021 | TEST(URLCanonTest, ResolveRelativeURL) { |
| 2022 | struct RelativeCase { |
| 2023 | const char* base; // Input base URL: MUST BE CANONICAL |
| 2024 | bool is_base_hier; // Is the base URL hierarchical |
| 2025 | bool is_base_file; // Tells us if the base is a file URL. |
| 2026 | const char* test; // Input URL to test against. |
| 2027 | bool succeed_relative; // Whether we expect IsRelativeURL to succeed |
| 2028 | bool is_rel; // Whether we expect |test| to be relative or not. |
| 2029 | bool succeed_resolve; // Whether we expect ResolveRelativeURL to succeed. |
| 2030 | const char* resolved; // What we expect in the result when resolving. |
| 2031 | } rel_cases[] = { |
| 2032 | // Basic absolute input. |
| 2033 | {"http://host/a", true, false, "http://another/", true, false, false, NULL}, |
| 2034 | {"http://host/a", true, false, "http:////another/", true, false, false, NULL}, |
| 2035 | // Empty relative URLs should only remove the ref part of the URL, |
| 2036 | // leaving the rest unchanged. |
| 2037 | {"http://foo/bar", true, false, "", true, true, true, "http://foo/bar"}, |
| 2038 | {"http://foo/bar#ref", true, false, "", true, true, true, "http://foo/bar"}, |
| 2039 | {"http://foo/bar#", true, false, "", true, true, true, "http://foo/bar"}, |
| 2040 | // Spaces at the ends of the relative path should be ignored. |
| 2041 | {"http://foo/bar", true, false, " another ", true, true, true, "http://foo/another"}, |
| 2042 | {"http://foo/bar", true, false, " . ", true, true, true, "http://foo/"}, |
| 2043 | {"http://foo/bar", true, false, " \t ", true, true, true, "http://foo/bar"}, |
| 2044 | // Matching schemes without two slashes are treated as relative. |
| 2045 | {"http://host/a", true, false, "http:path", true, true, true, "http://host/path"}, |
| 2046 | {"http://host/a/", true, false, "http:path", true, true, true, "http://host/a/path"}, |
| 2047 | {"http://host/a", true, false, "http:/path", true, true, true, "http://host/path"}, |
| 2048 | {"http://host/a", true, false, "HTTP:/path", true, true, true, "http://host/path"}, |
| 2049 | // Nonmatching schemes are absolute. |
| 2050 | {"http://host/a", true, false, "https:host2", true, false, false, NULL}, |
| 2051 | {"http://host/a", true, false, "htto:/host2", true, false, false, NULL}, |
| 2052 | // Absolute path input |
| 2053 | {"http://host/a", true, false, "/b/c/d", true, true, true, "http://host/b/c/d"}, |
| 2054 | {"http://host/a", true, false, "\\b\\c\\d", true, true, true, "http://host/b/c/d"}, |
| 2055 | {"http://host/a", true, false, "/b/../c", true, true, true, "http://host/c"}, |
| 2056 | {"http://host/a?b#c", true, false, "/b/../c", true, true, true, "http://host/c"}, |
| 2057 | {"http://host/a", true, false, "\\b/../c?x#y", true, true, true, "http://host/c?x#y"}, |
| 2058 | {"http://host/a?b#c", true, false, "/b/../c?x#y", true, true, true, "http://host/c?x#y"}, |
| 2059 | // Relative path input |
| 2060 | {"http://host/a", true, false, "b", true, true, true, "http://host/b"}, |
| 2061 | {"http://host/a", true, false, "bc/de", true, true, true, "http://host/bc/de"}, |
| 2062 | {"http://host/a/", true, false, "bc/de?query#ref", true, true, true, "http://host/a/bc/de?query#ref"}, |
| 2063 | {"http://host/a/", true, false, ".", true, true, true, "http://host/a/"}, |
| 2064 | {"http://host/a/", true, false, "..", true, true, true, "http://host/"}, |
| 2065 | {"http://host/a/", true, false, "./..", true, true, true, "http://host/"}, |
| 2066 | {"http://host/a/", true, false, "../.", true, true, true, "http://host/"}, |
| 2067 | {"http://host/a/", true, false, "././.", true, true, true, "http://host/a/"}, |
| 2068 | {"http://host/a?query#ref", true, false, "../../../foo", true, true, true, "http://host/foo"}, |
| 2069 | // Query input |
| 2070 | {"http://host/a", true, false, "?foo=bar", true, true, true, "http://host/a?foo=bar"}, |
| 2071 | {"http://host/a?x=y#z", true, false, "?", true, true, true, "http://host/a?"}, |
| 2072 | {"http://host/a?x=y#z", true, false, "?foo=bar#com", true, true, true, "http://host/a?foo=bar#com"}, |
| 2073 | // Ref input |
| 2074 | {"http://host/a", true, false, "#ref", true, true, true, "http://host/a#ref"}, |
| 2075 | {"http://host/a#b", true, false, "#", true, true, true, "http://host/a#"}, |
| 2076 | {"http://host/a?foo=bar#hello", true, false, "#bye", true, true, true, "http://host/a?foo=bar#bye"}, |
| 2077 | // Non-hierarchical base: no relative handling. Relative input should |
| 2078 | // error, and if a scheme is present, it should be treated as absolute. |
| 2079 | {"data:foobar", false, false, "baz.html", false, false, false, NULL}, |
| 2080 | {"data:foobar", false, false, "data:baz", true, false, false, NULL}, |
| 2081 | {"data:foobar", false, false, "data:/base", true, false, false, NULL}, |
| 2082 | // Non-hierarchical base: absolute input should succeed. |
| 2083 | {"data:foobar", false, false, "http://host/", true, false, false, NULL}, |
| 2084 | {"data:foobar", false, false, "http:host", true, false, false, NULL}, |
ramya.v | 56d42205 | 2015-12-02 02:24:46 | [diff] [blame] | 2085 | // Non-hierarchical base: empty URL should give error. |
| 2086 | {"data:foobar", false, false, "", false, false, false, NULL}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2087 | // Invalid schemes should be treated as relative. |
| 2088 | {"http://foo/bar", true, false, "./asd:fgh", true, true, true, "http://foo/asd:fgh"}, |
| 2089 | {"http://foo/bar", true, false, ":foo", true, true, true, "http://foo/:foo"}, |
| 2090 | {"http://foo/bar", true, false, " hello world", true, true, true, "http://foo/hello%20world"}, |
| 2091 | {"data:asdf", false, false, ":foo", false, false, false, NULL}, |
[email protected] | 45172e6 | 2014-03-03 21:21:35 | [diff] [blame] | 2092 | {"data:asdf", false, false, "bad(':foo')", false, false, false, NULL}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2093 | // We should treat semicolons like any other character in URL resolving |
| 2094 | {"http://host/a", true, false, ";foo", true, true, true, "http://host/;foo"}, |
| 2095 | {"http://host/a;", true, false, ";foo", true, true, true, "http://host/;foo"}, |
| 2096 | {"http://host/a", true, false, ";/../bar", true, true, true, "http://host/bar"}, |
| 2097 | // Relative URLs can also be written as "//foo/bar" which is relative to |
| 2098 | // the scheme. In this case, it would take the old scheme, so for http |
| 2099 | // the example would resolve to "http://foo/bar". |
| 2100 | {"http://host/a", true, false, "//another", true, true, true, "http://another/"}, |
| 2101 | {"http://host/a", true, false, "//another/path?query#ref", true, true, true, "http://another/path?query#ref"}, |
| 2102 | {"http://host/a", true, false, "///another/path", true, true, true, "http://another/path"}, |
| 2103 | {"http://host/a", true, false, "//Another\\path", true, true, true, "http://another/path"}, |
| 2104 | {"http://host/a", true, false, "//", true, true, false, "http:"}, |
| 2105 | // IE will also allow one or the other to be a backslash to get the same |
| 2106 | // behavior. |
| 2107 | {"http://host/a", true, false, "\\/another/path", true, true, true, "http://another/path"}, |
| 2108 | {"http://host/a", true, false, "/\\Another\\path", true, true, true, "http://another/path"}, |
| 2109 | #ifdef WIN32 |
| 2110 | // Resolving against Windows file base URLs. |
| 2111 | {"file:///C:/foo", true, true, "http://host/", true, false, false, NULL}, |
| 2112 | {"file:///C:/foo", true, true, "bar", true, true, true, "file:///C:/bar"}, |
| 2113 | {"file:///C:/foo", true, true, "../../../bar.html", true, true, true, "file:///C:/bar.html"}, |
| 2114 | {"file:///C:/foo", true, true, "/../bar.html", true, true, true, "file:///C:/bar.html"}, |
| 2115 | // But two backslashes on Windows should be UNC so should be treated |
| 2116 | // as absolute. |
| 2117 | {"http://host/a", true, false, "\\\\another\\path", true, false, false, NULL}, |
| 2118 | // IE doesn't support drive specs starting with two slashes. It fails |
| 2119 | // immediately and doesn't even try to load. We fix it up to either |
| 2120 | // an absolute path or UNC depending on what it looks like. |
| 2121 | {"file:///C:/something", true, true, "//c:/foo", true, true, true, "file:///C:/foo"}, |
| 2122 | {"file:///C:/something", true, true, "//localhost/c:/foo", true, true, true, "file:///C:/foo"}, |
| 2123 | // Windows drive specs should be allowed and treated as absolute. |
| 2124 | {"file:///C:/foo", true, true, "c:", true, false, false, NULL}, |
| 2125 | {"file:///C:/foo", true, true, "c:/foo", true, false, false, NULL}, |
| 2126 | {"http://host/a", true, false, "c:\\foo", true, false, false, NULL}, |
| 2127 | // Relative paths with drive letters should be allowed when the base is |
| 2128 | // also a file. |
| 2129 | {"file:///C:/foo", true, true, "/z:/bar", true, true, true, "file:///Z:/bar"}, |
| 2130 | // Treat absolute paths as being off of the drive. |
| 2131 | {"file:///C:/foo", true, true, "/bar", true, true, true, "file:///C:/bar"}, |
| 2132 | {"file://localhost/C:/foo", true, true, "/bar", true, true, true, "file://localhost/C:/bar"}, |
| 2133 | {"file:///C:/foo/com/", true, true, "/bar", true, true, true, "file:///C:/bar"}, |
| 2134 | // On Windows, two slashes without a drive letter when the base is a file |
| 2135 | // means that the path is UNC. |
| 2136 | {"file:///C:/something", true, true, "//somehost/path", true, true, true, "file://somehost/path"}, |
| 2137 | {"file:///C:/something", true, true, "/\\//somehost/path", true, true, true, "file://somehost/path"}, |
| 2138 | #else |
| 2139 | // On Unix we fall back to relative behavior since there's nothing else |
| 2140 | // reasonable to do. |
| 2141 | {"http://host/a", true, false, "\\\\Another\\path", true, true, true, "http://another/path"}, |
| 2142 | #endif |
| 2143 | // Even on Windows, we don't allow relative drive specs when the base |
| 2144 | // is not file. |
| 2145 | {"http://host/a", true, false, "/c:\\foo", true, true, true, "http://host/c:/foo"}, |
| 2146 | {"http://host/a", true, false, "//c:\\foo", true, true, true, "http://c/foo"}, |
[email protected] | 598c838 | 2013-09-18 22:55:31 | [diff] [blame] | 2147 | // Ensure that ports aren't allowed for hosts relative to a file url. |
| 2148 | // Although the result string shows a host:port portion, the call to |
| 2149 | // resolve the relative URL returns false, indicating parse failure, |
| 2150 | // which is what is required. |
| 2151 | {"file:///foo.txt", true, true, "//host:80/bar.txt", true, true, false, "file://host:80/bar.txt"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2152 | // Filesystem URL tests; filesystem URLs are only valid and relative if |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 2153 | // they have no scheme, e.g. "./index.html". There's no valid equivalent |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2154 | // to http:index.html. |
| 2155 | {"filesystem:http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL}, |
| 2156 | {"filesystem:http://host/t/path", true, false, "filesystem:https://host/t/path2", true, false, false, NULL}, |
| 2157 | {"filesystem:http://host/t/path", true, false, "http://host/t/path2", true, false, false, NULL}, |
| 2158 | {"http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL}, |
| 2159 | {"filesystem:http://host/t/path", true, false, "./path2", true, true, true, "filesystem:http://host/t/path2"}, |
| 2160 | {"filesystem:http://host/t/path/", true, false, "path2", true, true, true, "filesystem:http://host/t/path/path2"}, |
| 2161 | {"filesystem:http://host/t/path", true, false, "filesystem:http:path2", true, false, false, NULL}, |
| 2162 | // Absolute URLs are still not relative to a non-standard base URL. |
| 2163 | {"about:blank", false, false, "http://X/A", true, false, true, ""}, |
| 2164 | {"about:blank", false, false, "content://content.Provider/", true, false, true, ""}, |
| 2165 | }; |
| 2166 | |
viettrungluu | 4b691586 | 2014-10-16 03:42:49 | [diff] [blame] | 2167 | for (size_t i = 0; i < arraysize(rel_cases); i++) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2168 | const RelativeCase& cur_case = rel_cases[i]; |
| 2169 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2170 | Parsed parsed; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2171 | int base_len = static_cast<int>(strlen(cur_case.base)); |
| 2172 | if (cur_case.is_base_file) |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2173 | ParseFileURL(cur_case.base, base_len, &parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2174 | else if (cur_case.is_base_hier) |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2175 | ParseStandardURL(cur_case.base, base_len, &parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2176 | else |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2177 | ParsePathURL(cur_case.base, base_len, false, &parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2178 | |
| 2179 | // First see if it is relative. |
| 2180 | int test_len = static_cast<int>(strlen(cur_case.test)); |
| 2181 | bool is_relative; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2182 | Component relative_component; |
| 2183 | bool succeed_is_rel = IsRelativeURL( |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2184 | cur_case.base, parsed, cur_case.test, test_len, cur_case.is_base_hier, |
| 2185 | &is_relative, &relative_component); |
| 2186 | |
| 2187 | EXPECT_EQ(cur_case.succeed_relative, succeed_is_rel) << |
| 2188 | "succeed is rel failure on " << cur_case.test; |
| 2189 | EXPECT_EQ(cur_case.is_rel, is_relative) << |
| 2190 | "is rel failure on " << cur_case.test; |
| 2191 | // Now resolve it. |
| 2192 | if (succeed_is_rel && is_relative && cur_case.is_rel) { |
| 2193 | std::string resolved; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2194 | StdStringCanonOutput output(&resolved); |
| 2195 | Parsed resolved_parsed; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2196 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2197 | bool succeed_resolve = ResolveRelativeURL( |
| 2198 | cur_case.base, parsed, cur_case.is_base_file, cur_case.test, |
| 2199 | relative_component, NULL, &output, &resolved_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2200 | output.Complete(); |
| 2201 | |
| 2202 | EXPECT_EQ(cur_case.succeed_resolve, succeed_resolve); |
| 2203 | EXPECT_EQ(cur_case.resolved, resolved) << " on " << cur_case.test; |
| 2204 | |
| 2205 | // Verify that the output parsed structure is the same as parsing a |
| 2206 | // the URL freshly. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2207 | Parsed ref_parsed; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2208 | int resolved_len = static_cast<int>(resolved.size()); |
[email protected] | 369e84f7 | 2013-11-23 01:53:52 | [diff] [blame] | 2209 | if (cur_case.is_base_file) { |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2210 | ParseFileURL(resolved.c_str(), resolved_len, &ref_parsed); |
[email protected] | 369e84f7 | 2013-11-23 01:53:52 | [diff] [blame] | 2211 | } else if (cur_case.is_base_hier) { |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2212 | ParseStandardURL(resolved.c_str(), resolved_len, &ref_parsed); |
[email protected] | 369e84f7 | 2013-11-23 01:53:52 | [diff] [blame] | 2213 | } else { |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2214 | ParsePathURL(resolved.c_str(), resolved_len, false, &ref_parsed); |
[email protected] | 369e84f7 | 2013-11-23 01:53:52 | [diff] [blame] | 2215 | } |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2216 | EXPECT_TRUE(ParsedIsEqual(ref_parsed, resolved_parsed)); |
| 2217 | } |
| 2218 | } |
| 2219 | } |
| 2220 | |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 2221 | // It used to be the case that when we did a replacement with a long buffer of |
| 2222 | // UTF-16 characters, we would get invalid data in the URL. This is because the |
| 2223 | // buffer that it used to hold the UTF-8 data was resized, while some pointers |
| 2224 | // were still kept to the old buffer that was removed. |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2225 | TEST(URLCanonTest, ReplacementOverflow) { |
| 2226 | const char src[] = "file:///C:/foo/bar"; |
| 2227 | int src_len = static_cast<int>(strlen(src)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2228 | Parsed parsed; |
| 2229 | ParseFileURL(src, src_len, &parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2230 | |
| 2231 | // Override two components, the path with something short, and the query with |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 2232 | // something long enough to trigger the bug. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2233 | Replacements<base::char16> repl; |
[email protected] | 3774f83 | 2013-06-11 21:21:57 | [diff] [blame] | 2234 | base::string16 new_query; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2235 | for (int i = 0; i < 4800; i++) |
| 2236 | new_query.push_back('a'); |
| 2237 | |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 2238 | base::string16 new_path(test_utils::TruncateWStringToUTF16(L"/foo")); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2239 | repl.SetPath(new_path.c_str(), Component(0, 4)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2240 | repl.SetQuery(new_query.c_str(), |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2241 | Component(0, static_cast<int>(new_query.length()))); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2242 | |
| 2243 | // Call ReplaceComponents on the string. It doesn't matter if we call it for |
| 2244 | // standard URLs, file URLs, etc, since they will go to the same replacement |
| 2245 | // function that was buggy. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2246 | Parsed repl_parsed; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2247 | std::string repl_str; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2248 | StdStringCanonOutput repl_output(&repl_str); |
| 2249 | ReplaceFileURL(src, parsed, repl, NULL, &repl_output, &repl_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 2250 | repl_output.Complete(); |
| 2251 | |
| 2252 | // Generate the expected string and check. |
| 2253 | std::string expected("file:///foo?"); |
| 2254 | for (size_t i = 0; i < new_query.length(); i++) |
| 2255 | expected.push_back('a'); |
| 2256 | EXPECT_TRUE(expected == repl_str); |
| 2257 | } |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2258 | |
jww | 03628409 | 2016-10-14 14:49:26 | [diff] [blame] | 2259 | TEST(URLCanonTest, DefaultPortForScheme) { |
| 2260 | struct TestCases { |
| 2261 | const char* scheme; |
| 2262 | const int expected_port; |
| 2263 | } cases[]{ |
| 2264 | {"http", 80}, |
| 2265 | {"https", 443}, |
| 2266 | {"ftp", 21}, |
| 2267 | {"ws", 80}, |
| 2268 | {"wss", 443}, |
| 2269 | {"gopher", 70}, |
jww | 0448040 | 2016-10-25 02:50:33 | [diff] [blame] | 2270 | {"http-so", 80}, |
| 2271 | {"https-so", 443}, |
jww | 03628409 | 2016-10-14 14:49:26 | [diff] [blame] | 2272 | {"fake-scheme", PORT_UNSPECIFIED}, |
| 2273 | {"HTTP", PORT_UNSPECIFIED}, |
| 2274 | {"HTTPS", PORT_UNSPECIFIED}, |
| 2275 | {"FTP", PORT_UNSPECIFIED}, |
| 2276 | {"WS", PORT_UNSPECIFIED}, |
| 2277 | {"WSS", PORT_UNSPECIFIED}, |
| 2278 | {"GOPHER", PORT_UNSPECIFIED}, |
jww | 0448040 | 2016-10-25 02:50:33 | [diff] [blame] | 2279 | {"HTTP-SO", PORT_UNSPECIFIED}, |
| 2280 | {"HTTPS-SO", PORT_UNSPECIFIED}, |
jww | 03628409 | 2016-10-14 14:49:26 | [diff] [blame] | 2281 | }; |
| 2282 | |
| 2283 | for (auto& test_case : cases) { |
| 2284 | SCOPED_TRACE(test_case.scheme); |
| 2285 | EXPECT_EQ(test_case.expected_port, |
| 2286 | DefaultPortForScheme(test_case.scheme, strlen(test_case.scheme))); |
| 2287 | } |
| 2288 | } |
| 2289 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 2290 | } // namespace url |