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