OLD | NEW |
(Empty) | |
| 1 // Copyright 2008, Google Inc. |
| 2 // All rights reserved. |
| 3 // |
| 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are |
| 6 // met: |
| 7 // |
| 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above |
| 11 // copyright notice, this list of conditions and the following disclaimer |
| 12 // in the documentation and/or other materials provided with the |
| 13 // distribution. |
| 14 // * Neither the name of Google Inc. nor the names of its |
| 15 // contributors may be used to endorse or promote products derived from |
| 16 // this software without specific prior written permission. |
| 17 // |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
| 30 #include "googleurl/src/url_canon.h" |
| 31 #include "googleurl/src/url_canon_stdstring.h" |
| 32 #include "googleurl/src/url_parse.h" |
| 33 #include "googleurl/src/url_test_utils.h" |
| 34 #include "googleurl/src/url_util.h" |
| 35 #include "testing/gtest/include/gtest/gtest.h" |
| 36 |
| 37 TEST(URLUtilTest, FindAndCompareScheme) { |
| 38 url_parse::Component found_scheme; |
| 39 |
| 40 // Simple case where the scheme is found and matches. |
| 41 const char kStr1[] = "http://www.com/"; |
| 42 EXPECT_TRUE(url_util::FindAndCompareScheme( |
| 43 kStr1, static_cast<int>(strlen(kStr1)), "http", NULL)); |
| 44 EXPECT_TRUE(url_util::FindAndCompareScheme( |
| 45 kStr1, static_cast<int>(strlen(kStr1)), "http", &found_scheme)); |
| 46 EXPECT_TRUE(found_scheme == url_parse::Component(0, 4)); |
| 47 |
| 48 // A case where the scheme is found and doesn't match. |
| 49 EXPECT_FALSE(url_util::FindAndCompareScheme( |
| 50 kStr1, static_cast<int>(strlen(kStr1)), "https", &found_scheme)); |
| 51 EXPECT_TRUE(found_scheme == url_parse::Component(0, 4)); |
| 52 |
| 53 // A case where there is no scheme. |
| 54 const char kStr2[] = "httpfoobar"; |
| 55 EXPECT_FALSE(url_util::FindAndCompareScheme( |
| 56 kStr2, static_cast<int>(strlen(kStr2)), "http", &found_scheme)); |
| 57 EXPECT_TRUE(found_scheme == url_parse::Component()); |
| 58 |
| 59 // When there is an empty scheme, it should match the empty scheme. |
| 60 const char kStr3[] = ":foo.com/"; |
| 61 EXPECT_TRUE(url_util::FindAndCompareScheme( |
| 62 kStr3, static_cast<int>(strlen(kStr3)), "", &found_scheme)); |
| 63 EXPECT_TRUE(found_scheme == url_parse::Component(0, 0)); |
| 64 |
| 65 // But when there is no scheme, it should fail. |
| 66 EXPECT_FALSE(url_util::FindAndCompareScheme("", 0, "", &found_scheme)); |
| 67 EXPECT_TRUE(found_scheme == url_parse::Component()); |
| 68 |
| 69 // When there is a whitespace char in scheme, it should canonicalize the url |
| 70 // before comparison. |
| 71 const char whtspc_str[] = " \r\n\tjav\ra\nscri\tpt:alert(1)"; |
| 72 EXPECT_TRUE(url_util::FindAndCompareScheme( |
| 73 whtspc_str, static_cast<int>(strlen(whtspc_str)), "javascript", |
| 74 &found_scheme)); |
| 75 EXPECT_TRUE(found_scheme == url_parse::Component(1, 10)); |
| 76 |
| 77 // Control characters should be stripped out on the ends, and kept in the |
| 78 // middle. |
| 79 const char ctrl_str[] = "\02jav\02scr\03ipt:alert(1)"; |
| 80 EXPECT_FALSE(url_util::FindAndCompareScheme( |
| 81 ctrl_str, static_cast<int>(strlen(ctrl_str)), "javascript", |
| 82 &found_scheme)); |
| 83 EXPECT_TRUE(found_scheme == url_parse::Component(1, 11)); |
| 84 } |
| 85 |
| 86 TEST(URLUtilTest, ReplaceComponents) { |
| 87 url_parse::Parsed parsed; |
| 88 url_canon::RawCanonOutputT<char> output; |
| 89 url_parse::Parsed new_parsed; |
| 90 |
| 91 // Check that the following calls do not cause crash |
| 92 url_canon::Replacements<char> replacements; |
| 93 replacements.SetRef("test", url_parse::Component(0, 4)); |
| 94 url_util::ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, |
| 95 &new_parsed); |
| 96 url_util::ReplaceComponents("", 0, parsed, replacements, NULL, &output, |
| 97 &new_parsed); |
| 98 replacements.ClearRef(); |
| 99 replacements.SetHost("test", url_parse::Component(0, 4)); |
| 100 url_util::ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, |
| 101 &new_parsed); |
| 102 url_util::ReplaceComponents("", 0, parsed, replacements, NULL, &output, |
| 103 &new_parsed); |
| 104 |
| 105 replacements.ClearHost(); |
| 106 url_util::ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, |
| 107 &new_parsed); |
| 108 url_util::ReplaceComponents("", 0, parsed, replacements, NULL, &output, |
| 109 &new_parsed); |
| 110 url_util::ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, |
| 111 &new_parsed); |
| 112 url_util::ReplaceComponents("", 0, parsed, replacements, NULL, &output, |
| 113 &new_parsed); |
| 114 } |
| 115 |
| 116 static std::string CheckReplaceScheme(const char* base_url, |
| 117 const char* scheme) { |
| 118 // Make sure the input is canonicalized. |
| 119 url_canon::RawCanonOutput<32> original; |
| 120 url_parse::Parsed original_parsed; |
| 121 url_util::Canonicalize(base_url, strlen(base_url), NULL, |
| 122 &original, &original_parsed); |
| 123 |
| 124 url_canon::Replacements<char> replacements; |
| 125 replacements.SetScheme(scheme, url_parse::Component(0, strlen(scheme))); |
| 126 |
| 127 std::string output_string; |
| 128 url_canon::StdStringCanonOutput output(&output_string); |
| 129 url_parse::Parsed output_parsed; |
| 130 url_util::ReplaceComponents(original.data(), original.length(), |
| 131 original_parsed, replacements, NULL, |
| 132 &output, &output_parsed); |
| 133 |
| 134 output.Complete(); |
| 135 return output_string; |
| 136 } |
| 137 |
| 138 TEST(URLUtilTest, ReplaceScheme) { |
| 139 EXPECT_EQ("https://google.com/", |
| 140 CheckReplaceScheme("http://google.com/", "https")); |
| 141 EXPECT_EQ("file://google.com/", |
| 142 CheckReplaceScheme("http://google.com/", "file")); |
| 143 EXPECT_EQ("http://home/Build", |
| 144 CheckReplaceScheme("file:///Home/Build", "http")); |
| 145 EXPECT_EQ("javascript:foo", |
| 146 CheckReplaceScheme("about:foo", "javascript")); |
| 147 EXPECT_EQ("://google.com/", |
| 148 CheckReplaceScheme("http://google.com/", "")); |
| 149 EXPECT_EQ("http://google.com/", |
| 150 CheckReplaceScheme("about:google.com", "http")); |
| 151 EXPECT_EQ("http:", CheckReplaceScheme("", "http")); |
| 152 |
| 153 #ifdef WIN32 |
| 154 // Magic Windows drive letter behavior when converting to a file URL. |
| 155 EXPECT_EQ("file:///E:/foo/", |
| 156 CheckReplaceScheme("http://localhost/e:foo/", "file")); |
| 157 #endif |
| 158 |
| 159 // This will probably change to "about://google.com/" when we fix |
| 160 // http://crbug.com/160 which should also be an acceptable result. |
| 161 EXPECT_EQ("about://google.com/", |
| 162 CheckReplaceScheme("http://google.com/", "about")); |
| 163 } |
| 164 |
| 165 TEST(URLUtilTest, DecodeURLEscapeSequences) { |
| 166 struct DecodeCase { |
| 167 const char* input; |
| 168 const char* output; |
| 169 } decode_cases[] = { |
| 170 {"hello, world", "hello, world"}, |
| 171 {"%01%02%03%04%05%06%07%08%09%0a%0B%0C%0D%0e%0f/", |
| 172 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0B\x0C\x0D\x0e\x0f/"}, |
| 173 {"%10%11%12%13%14%15%16%17%18%19%1a%1B%1C%1D%1e%1f/", |
| 174 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1B\x1C\x1D\x1e\x1f/"}, |
| 175 {"%20%21%22%23%24%25%26%27%28%29%2a%2B%2C%2D%2e%2f/", |
| 176 " !\"#$%&'()*+,-.//"}, |
| 177 {"%30%31%32%33%34%35%36%37%38%39%3a%3B%3C%3D%3e%3f/", |
| 178 "0123456789:;<=>?/"}, |
| 179 {"%40%41%42%43%44%45%46%47%48%49%4a%4B%4C%4D%4e%4f/", |
| 180 "@ABCDEFGHIJKLMNO/"}, |
| 181 {"%50%51%52%53%54%55%56%57%58%59%5a%5B%5C%5D%5e%5f/", |
| 182 "PQRSTUVWXYZ[\\]^_/"}, |
| 183 {"%60%61%62%63%64%65%66%67%68%69%6a%6B%6C%6D%6e%6f/", |
| 184 "`abcdefghijklmno/"}, |
| 185 {"%70%71%72%73%74%75%76%77%78%79%7a%7B%7C%7D%7e%7f/", |
| 186 "pqrstuvwxyz{|}~\x7f/"}, |
| 187 // Test un-UTF-8-ization. |
| 188 {"%e4%bd%a0%e5%a5%bd", "\xe4\xbd\xa0\xe5\xa5\xbd"}, |
| 189 }; |
| 190 |
| 191 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(decode_cases); i++) { |
| 192 const char* input = decode_cases[i].input; |
| 193 url_canon::RawCanonOutputT<char16> output; |
| 194 url_util::DecodeURLEscapeSequences(input, strlen(input), &output); |
| 195 EXPECT_EQ(decode_cases[i].output, |
| 196 url_test_utils::ConvertUTF16ToUTF8( |
| 197 string16(output.data(), output.length()))); |
| 198 } |
| 199 |
| 200 // Our decode should decode %00 |
| 201 const char zero_input[] = "%00"; |
| 202 url_canon::RawCanonOutputT<char16> zero_output; |
| 203 url_util::DecodeURLEscapeSequences(zero_input, strlen(zero_input), |
| 204 &zero_output); |
| 205 EXPECT_NE("%00", |
| 206 url_test_utils::ConvertUTF16ToUTF8( |
| 207 string16(zero_output.data(), zero_output.length()))); |
| 208 |
| 209 // Test the error behavior for invalid UTF-8. |
| 210 const char invalid_input[] = "%e4%a0%e5%a5%bd"; |
| 211 const char16 invalid_expected[4] = {0x00e4, 0x00a0, 0x597d, 0}; |
| 212 url_canon::RawCanonOutputT<char16> invalid_output; |
| 213 url_util::DecodeURLEscapeSequences(invalid_input, strlen(invalid_input), |
| 214 &invalid_output); |
| 215 EXPECT_EQ(string16(invalid_expected), |
| 216 string16(invalid_output.data(), invalid_output.length())); |
| 217 } |
| 218 |
| 219 TEST(URLUtilTest, TestEncodeURIComponent) { |
| 220 struct EncodeCase { |
| 221 const char* input; |
| 222 const char* output; |
| 223 } encode_cases[] = { |
| 224 {"hello, world", "hello%2C%20world"}, |
| 225 {"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F", |
| 226 "%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F"}, |
| 227 {"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F", |
| 228 "%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F"}, |
| 229 {" !\"#$%&'()*+,-./", |
| 230 "%20!%22%23%24%25%26'()*%2B%2C-.%2F"}, |
| 231 {"0123456789:;<=>?", |
| 232 "0123456789%3A%3B%3C%3D%3E%3F"}, |
| 233 {"@ABCDEFGHIJKLMNO", |
| 234 "%40ABCDEFGHIJKLMNO"}, |
| 235 {"PQRSTUVWXYZ[\\]^_", |
| 236 "PQRSTUVWXYZ%5B%5C%5D%5E_"}, |
| 237 {"`abcdefghijklmno", |
| 238 "%60abcdefghijklmno"}, |
| 239 {"pqrstuvwxyz{|}~\x7f", |
| 240 "pqrstuvwxyz%7B%7C%7D~%7F"}, |
| 241 }; |
| 242 |
| 243 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(encode_cases); i++) { |
| 244 const char* input = encode_cases[i].input; |
| 245 url_canon::RawCanonOutputT<char> buffer; |
| 246 url_util::EncodeURIComponent(input, strlen(input), &buffer); |
| 247 std::string output(buffer.data(), buffer.length()); |
| 248 EXPECT_EQ(encode_cases[i].output, output); |
| 249 } |
| 250 } |
| 251 |
| 252 TEST(URLUtilTest, TestResolveRelativeWithNonStandardBase) { |
| 253 // This tests non-standard (in the sense that GURL::IsStandard() == false) |
| 254 // hierarchical schemes. |
| 255 struct ResolveRelativeCase { |
| 256 const char* base; |
| 257 const char* rel; |
| 258 bool is_valid; |
| 259 const char* out; |
| 260 } resolve_non_standard_cases[] = { |
| 261 // Resolving a relative path against a non-hierarchical URL should fail. |
| 262 {"scheme:opaque_data", "/path", false, ""}, |
| 263 // Resolving a relative path against a non-standard authority-based base |
| 264 // URL doesn't alter the authority section. |
| 265 {"scheme://Authority/", "../path", true, "scheme://Authority/path"}, |
| 266 // A non-standard hierarchical base is resolved with path URL |
| 267 // canoncialization rules. |
| 268 {"data:/Blah:Blah/", "file.html", true, "data:/Blah:Blah/file.html"}, |
| 269 {"data:/Path/../part/part2", "file.html", true, "data:/Path/../part/file.htm
l"}, |
| 270 // Path URL canonicalization rules also apply to non-standard authority- |
| 271 // based URLs. |
| 272 {"custom://Authority/", "file.html", true, "custom://Authority/file.html"}, |
| 273 {"custom://Authority/", "other://Auth/", true, "other://Auth/"}, |
| 274 {"custom://Authority/", "../../file.html", true, "custom://Authority/file.ht
ml"}, |
| 275 {"custom://Authority/path/", "file.html", true, "custom://Authority/path/fil
e.html"}, |
| 276 {"custom://Authority:NoCanon/path/", "file.html", true, "custom://Authority:
NoCanon/path/file.html"}, |
| 277 // It's still possible to get an invalid path URL. |
| 278 {"custom://Invalid:!#Auth/", "file.html", false, ""}, |
| 279 // A path with an authority section gets canonicalized under standard URL |
| 280 // rules, even though the base was non-standard. |
| 281 {"content://content.Provider/", "//other.Provider", true, "content://other.p
rovider/"}, |
| 282 // Resolving an absolute URL doesn't cause canonicalization of the |
| 283 // result. |
| 284 {"about:blank", "custom://Authority", true, "custom://Authority"}, |
| 285 // Resolving should fail if the base URL is authority-based but is |
| 286 // missing a path component (the '/' at the end). |
| 287 {"scheme://Authority", "path", false, ""}, |
| 288 }; |
| 289 |
| 290 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(resolve_non_standard_cases); i++) { |
| 291 const ResolveRelativeCase& test_data = resolve_non_standard_cases[i]; |
| 292 url_parse::Parsed base_parsed; |
| 293 url_parse::ParsePathURL(test_data.base, strlen(test_data.base), |
| 294 &base_parsed); |
| 295 |
| 296 std::string resolved; |
| 297 url_canon::StdStringCanonOutput output(&resolved); |
| 298 url_parse::Parsed resolved_parsed; |
| 299 bool valid = |
| 300 url_util::ResolveRelative(test_data.base, strlen(test_data.base), |
| 301 base_parsed, |
| 302 test_data.rel, strlen(test_data.rel), |
| 303 NULL, &output, &resolved_parsed); |
| 304 output.Complete(); |
| 305 |
| 306 EXPECT_EQ(test_data.is_valid, valid) << i; |
| 307 if (test_data.is_valid && valid) |
| 308 EXPECT_EQ(test_data.out, resolved) << i; |
| 309 } |
| 310 } |
OLD | NEW |