| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <algorithm> | 5 #include <algorithm> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "net/base/escape.h" | 8 #include "net/base/escape.h" |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 size_t output_offset; | 52 size_t output_offset; |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 struct EscapeForHTMLCase { | 55 struct EscapeForHTMLCase { |
| 56 const char* input; | 56 const char* input; |
| 57 const char* expected_output; | 57 const char* expected_output; |
| 58 }; | 58 }; |
| 59 | 59 |
| 60 TEST(EscapeTest, EscapeTextForFormSubmission) { | 60 TEST(EscapeTest, EscapeTextForFormSubmission) { |
| 61 const EscapeCase escape_cases[] = { | 61 const EscapeCase escape_cases[] = { |
| 62 {"foo", "foo"}, | 62 {"foo", "foo"}, {"foo bar", "foo+bar"}, {"foo++", "foo%2B%2B"}}; |
| 63 {"foo bar", "foo+bar"}, | |
| 64 {"foo++", "foo%2B%2B"} | |
| 65 }; | |
| 66 for (size_t i = 0; i < arraysize(escape_cases); ++i) { | 63 for (size_t i = 0; i < arraysize(escape_cases); ++i) { |
| 67 EscapeCase value = escape_cases[i]; | 64 EscapeCase value = escape_cases[i]; |
| 68 EXPECT_EQ(value.output, EscapeQueryParamValue(value.input, true)); | 65 EXPECT_EQ(value.output, EscapeQueryParamValue(value.input, true)); |
| 69 } | 66 } |
| 70 | 67 |
| 71 const EscapeCase escape_cases_no_plus[] = { | 68 const EscapeCase escape_cases_no_plus[] = { |
| 72 {"foo", "foo"}, | 69 {"foo", "foo"}, {"foo bar", "foo%20bar"}, {"foo++", "foo%2B%2B"}}; |
| 73 {"foo bar", "foo%20bar"}, | |
| 74 {"foo++", "foo%2B%2B"} | |
| 75 }; | |
| 76 for (size_t i = 0; i < arraysize(escape_cases_no_plus); ++i) { | 70 for (size_t i = 0; i < arraysize(escape_cases_no_plus); ++i) { |
| 77 EscapeCase value = escape_cases_no_plus[i]; | 71 EscapeCase value = escape_cases_no_plus[i]; |
| 78 EXPECT_EQ(value.output, EscapeQueryParamValue(value.input, false)); | 72 EXPECT_EQ(value.output, EscapeQueryParamValue(value.input, false)); |
| 79 } | 73 } |
| 80 | 74 |
| 81 // Test all the values in we're supposed to be escaping. | 75 // Test all the values in we're supposed to be escaping. |
| 82 const std::string no_escape( | 76 const std::string no_escape( |
| 83 "abcdefghijklmnopqrstuvwxyz" | 77 "abcdefghijklmnopqrstuvwxyz" |
| 84 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 78 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 85 "0123456789" | 79 "0123456789" |
| 86 "!'()*-._~"); | 80 "!'()*-._~"); |
| 87 for (int i = 0; i < 256; ++i) { | 81 for (int i = 0; i < 256; ++i) { |
| 88 std::string in; | 82 std::string in; |
| 89 in.push_back(i); | 83 in.push_back(i); |
| 90 std::string out = EscapeQueryParamValue(in, true); | 84 std::string out = EscapeQueryParamValue(in, true); |
| 91 if (0 == i) { | 85 if (0 == i) { |
| 92 EXPECT_EQ(out, std::string("%00")); | 86 EXPECT_EQ(out, std::string("%00")); |
| 93 } else if (32 == i) { | 87 } else if (32 == i) { |
| 94 // Spaces are plus escaped like web forms. | 88 // Spaces are plus escaped like web forms. |
| 95 EXPECT_EQ(out, std::string("+")); | 89 EXPECT_EQ(out, std::string("+")); |
| 96 } else if (no_escape.find(in) == std::string::npos) { | 90 } else if (no_escape.find(in) == std::string::npos) { |
| 97 // Check %hex escaping | 91 // Check %hex escaping |
| 98 std::string expected = base::StringPrintf("%%%02X", i); | 92 std::string expected = base::StringPrintf("%%%02X", i); |
| 99 EXPECT_EQ(expected, out); | 93 EXPECT_EQ(expected, out); |
| 100 } else { | 94 } else { |
| 101 // No change for things in the no_escape list. | 95 // No change for things in the no_escape list. |
| 102 EXPECT_EQ(out, in); | 96 EXPECT_EQ(out, in); |
| 103 } | 97 } |
| 104 } | 98 } |
| 105 } | 99 } |
| 106 | 100 |
| 107 TEST(EscapeTest, EscapePath) { | 101 TEST(EscapeTest, EscapePath) { |
| 108 ASSERT_EQ( | 102 ASSERT_EQ( |
| 109 // Most of the character space we care about, un-escaped | 103 // Most of the character space we care about, un-escaped |
| 110 EscapePath( | 104 EscapePath( |
| 111 "\x02\n\x1d !\"#$%&'()*+,-./0123456789:;" | 105 "\x02\n\x1d !\"#$%&'()*+,-./0123456789:;" |
| 112 "<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 106 "<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 113 "[\\]^_`abcdefghijklmnopqrstuvwxyz" | 107 "[\\]^_`abcdefghijklmnopqrstuvwxyz" |
| 114 "{|}~\x7f\x80\xff"), | 108 "{|}~\x7f\x80\xff"), |
| 115 // Escaped | 109 // Escaped |
| 116 "%02%0A%1D%20!%22%23$%25&'()*+,-./0123456789%3A;" | 110 "%02%0A%1D%20!%22%23$%25&'()*+,-./0123456789%3A;" |
| 117 "%3C=%3E%3F@ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 111 "%3C=%3E%3F@ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 118 "%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz" | 112 "%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz" |
| 119 "%7B%7C%7D~%7F%80%FF"); | 113 "%7B%7C%7D~%7F%80%FF"); |
| 120 } | 114 } |
| 121 | 115 |
| 122 TEST(EscapeTest, DataURLWithAccentedCharacters) { | 116 TEST(EscapeTest, DataURLWithAccentedCharacters) { |
| 123 const std::string url = | 117 const std::string url = |
| 124 "text/html;charset=utf-8,%3Chtml%3E%3Cbody%3ETonton,%20ton%20th%C3" | 118 "text/html;charset=utf-8,%3Chtml%3E%3Cbody%3ETonton,%20ton%20th%C3" |
| 125 "%A9%20t'a-t-il%20%C3%B4t%C3%A9%20ta%20toux%20"; | 119 "%A9%20t'a-t-il%20%C3%B4t%C3%A9%20ta%20toux%20"; |
| 126 | 120 |
| 127 base::OffsetAdjuster::Adjustments adjustments; | 121 base::OffsetAdjuster::Adjustments adjustments; |
| 128 net::UnescapeAndDecodeUTF8URLComponentWithAdjustments( | 122 net::UnescapeAndDecodeUTF8URLComponentWithAdjustments( |
| 129 url, UnescapeRule::SPACES, &adjustments); | 123 url, UnescapeRule::SPACES, &adjustments); |
| 130 } | 124 } |
| 131 | 125 |
| 132 TEST(EscapeTest, EscapeUrlEncodedData) { | 126 TEST(EscapeTest, EscapeUrlEncodedData) { |
| 133 ASSERT_EQ( | 127 ASSERT_EQ( |
| 134 // Most of the character space we care about, un-escaped | 128 // Most of the character space we care about, un-escaped |
| 135 EscapeUrlEncodedData( | 129 EscapeUrlEncodedData( |
| 136 "\x02\n\x1d !\"#$%&'()*+,-./0123456789:;" | 130 "\x02\n\x1d !\"#$%&'()*+,-./0123456789:;" |
| 137 "<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 131 "<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 138 "[\\]^_`abcdefghijklmnopqrstuvwxyz" | 132 "[\\]^_`abcdefghijklmnopqrstuvwxyz" |
| 139 "{|}~\x7f\x80\xff", true), | 133 "{|}~\x7f\x80\xff", |
| 140 // Escaped | 134 true), |
| 141 "%02%0A%1D+!%22%23%24%25%26%27()*%2B,-./0123456789:%3B" | 135 // Escaped |
| 142 "%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 136 "%02%0A%1D+!%22%23%24%25%26%27()*%2B,-./0123456789:%3B" |
| 143 "%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz" | 137 "%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 144 "%7B%7C%7D~%7F%80%FF"); | 138 "%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz" |
| 139 "%7B%7C%7D~%7F%80%FF"); |
| 145 } | 140 } |
| 146 | 141 |
| 147 TEST(EscapeTest, EscapeUrlEncodedDataSpace) { | 142 TEST(EscapeTest, EscapeUrlEncodedDataSpace) { |
| 148 ASSERT_EQ(EscapeUrlEncodedData("a b", true), "a+b"); | 143 ASSERT_EQ(EscapeUrlEncodedData("a b", true), "a+b"); |
| 149 ASSERT_EQ(EscapeUrlEncodedData("a b", false), "a%20b"); | 144 ASSERT_EQ(EscapeUrlEncodedData("a b", false), "a%20b"); |
| 150 } | 145 } |
| 151 | 146 |
| 152 TEST(EscapeTest, UnescapeURLComponentASCII) { | 147 TEST(EscapeTest, UnescapeURLComponentASCII) { |
| 153 const UnescapeURLCaseASCII unescape_cases[] = { | 148 const UnescapeURLCaseASCII unescape_cases[] = { |
| 154 {"", UnescapeRule::NORMAL, ""}, | 149 {"", UnescapeRule::NORMAL, ""}, |
| 155 {"%2", UnescapeRule::NORMAL, "%2"}, | 150 {"%2", UnescapeRule::NORMAL, "%2"}, |
| 156 {"%%%%%%", UnescapeRule::NORMAL, "%%%%%%"}, | 151 {"%%%%%%", UnescapeRule::NORMAL, "%%%%%%"}, |
| 157 {"Don't escape anything", UnescapeRule::NORMAL, "Don't escape anything"}, | 152 {"Don't escape anything", UnescapeRule::NORMAL, "Don't escape anything"}, |
| 158 {"Invalid %escape %2", UnescapeRule::NORMAL, "Invalid %escape %2"}, | 153 {"Invalid %escape %2", UnescapeRule::NORMAL, "Invalid %escape %2"}, |
| 159 {"Some%20random text %25%2dOK", UnescapeRule::NONE, | 154 {"Some%20random text %25%2dOK", |
| 160 "Some%20random text %25%2dOK"}, | 155 UnescapeRule::NONE, |
| 161 {"Some%20random text %25%2dOK", UnescapeRule::NORMAL, | 156 "Some%20random text %25%2dOK"}, |
| 162 "Some%20random text %25-OK"}, | 157 {"Some%20random text %25%2dOK", |
| 163 {"Some%20random text %25%2dOK", UnescapeRule::SPACES, | 158 UnescapeRule::NORMAL, |
| 164 "Some random text %25-OK"}, | 159 "Some%20random text %25-OK"}, |
| 165 {"Some%20random text %25%2dOK", UnescapeRule::URL_SPECIAL_CHARS, | 160 {"Some%20random text %25%2dOK", |
| 166 "Some%20random text %-OK"}, | 161 UnescapeRule::SPACES, |
| 167 {"Some%20random text %25%2dOK", | 162 "Some random text %25-OK"}, |
| 168 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS, | 163 {"Some%20random text %25%2dOK", |
| 169 "Some random text %-OK"}, | 164 UnescapeRule::URL_SPECIAL_CHARS, |
| 170 {"%A0%B1%C2%D3%E4%F5", UnescapeRule::NORMAL, "\xA0\xB1\xC2\xD3\xE4\xF5"}, | 165 "Some%20random text %-OK"}, |
| 171 {"%Aa%Bb%Cc%Dd%Ee%Ff", UnescapeRule::NORMAL, "\xAa\xBb\xCc\xDd\xEe\xFf"}, | 166 {"Some%20random text %25%2dOK", |
| 172 // Certain URL-sensitive characters should not be unescaped unless asked. | 167 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS, |
| 173 {"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", UnescapeRule::SPACES, | 168 "Some random text %-OK"}, |
| 174 "Hello %13%10world %23# %3F? %3D= %26& %25% %2B+"}, | 169 {"%A0%B1%C2%D3%E4%F5", UnescapeRule::NORMAL, "\xA0\xB1\xC2\xD3\xE4\xF5"}, |
| 175 {"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", | 170 {"%Aa%Bb%Cc%Dd%Ee%Ff", UnescapeRule::NORMAL, "\xAa\xBb\xCc\xDd\xEe\xFf"}, |
| 176 UnescapeRule::URL_SPECIAL_CHARS, | 171 // Certain URL-sensitive characters should not be unescaped unless asked. |
| 177 "Hello%20%13%10world ## ?? == && %% ++"}, | 172 {"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", |
| 178 // We can neither escape nor unescape '@' since some websites expect it to | 173 UnescapeRule::SPACES, |
| 179 // be preserved as either '@' or "%40". | 174 "Hello %13%10world %23# %3F? %3D= %26& %25% %2B+"}, |
| 180 // See http://b/996720 and http://crbug.com/23933 . | 175 {"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", |
| 181 {"me@my%40example", UnescapeRule::NORMAL, "me@my%40example"}, | 176 UnescapeRule::URL_SPECIAL_CHARS, |
| 182 // Control characters. | 177 "Hello%20%13%10world ## ?? == && %% ++"}, |
| 183 {"%01%02%03%04%05%06%07%08%09 %25", UnescapeRule::URL_SPECIAL_CHARS, | 178 // We can neither escape nor unescape '@' since some websites expect it to |
| 184 "%01%02%03%04%05%06%07%08%09 %"}, | 179 // be preserved as either '@' or "%40". |
| 185 {"%01%02%03%04%05%06%07%08%09 %25", UnescapeRule::CONTROL_CHARS, | 180 // See http://b/996720 and http://crbug.com/23933 . |
| 186 "\x01\x02\x03\x04\x05\x06\x07\x08\x09 %25"}, | 181 {"me@my%40example", UnescapeRule::NORMAL, "me@my%40example"}, |
| 187 {"Hello%20%13%10%02", UnescapeRule::SPACES, "Hello %13%10%02"}, | 182 // Control characters. |
| 188 {"Hello%20%13%10%02", UnescapeRule::CONTROL_CHARS, "Hello%20\x13\x10\x02"}, | 183 {"%01%02%03%04%05%06%07%08%09 %25", |
| 184 UnescapeRule::URL_SPECIAL_CHARS, |
| 185 "%01%02%03%04%05%06%07%08%09 %"}, |
| 186 {"%01%02%03%04%05%06%07%08%09 %25", |
| 187 UnescapeRule::CONTROL_CHARS, |
| 188 "\x01\x02\x03\x04\x05\x06\x07\x08\x09 %25"}, |
| 189 {"Hello%20%13%10%02", UnescapeRule::SPACES, "Hello %13%10%02"}, |
| 190 {"Hello%20%13%10%02", |
| 191 UnescapeRule::CONTROL_CHARS, |
| 192 "Hello%20\x13\x10\x02"}, |
| 189 }; | 193 }; |
| 190 | 194 |
| 191 for (size_t i = 0; i < arraysize(unescape_cases); i++) { | 195 for (size_t i = 0; i < arraysize(unescape_cases); i++) { |
| 192 std::string str(unescape_cases[i].input); | 196 std::string str(unescape_cases[i].input); |
| 193 EXPECT_EQ(std::string(unescape_cases[i].output), | 197 EXPECT_EQ(std::string(unescape_cases[i].output), |
| 194 UnescapeURLComponent(str, unescape_cases[i].rules)); | 198 UnescapeURLComponent(str, unescape_cases[i].rules)); |
| 195 } | 199 } |
| 196 | 200 |
| 197 // Test the NULL character unescaping (which wouldn't work above since those | 201 // Test the NULL character unescaping (which wouldn't work above since those |
| 198 // are just char pointers). | 202 // are just char pointers). |
| (...skipping 10 matching lines...) Expand all Loading... |
| 209 | 213 |
| 210 // When we're not unescaping NULLs. | 214 // When we're not unescaping NULLs. |
| 211 expected = "Null"; | 215 expected = "Null"; |
| 212 expected.push_back(0); | 216 expected.push_back(0); |
| 213 expected.append("%009Test"); | 217 expected.append("%009Test"); |
| 214 EXPECT_EQ(expected, UnescapeURLComponent(input, UnescapeRule::NORMAL)); | 218 EXPECT_EQ(expected, UnescapeURLComponent(input, UnescapeRule::NORMAL)); |
| 215 } | 219 } |
| 216 | 220 |
| 217 TEST(EscapeTest, UnescapeURLComponent) { | 221 TEST(EscapeTest, UnescapeURLComponent) { |
| 218 const UnescapeURLCase unescape_cases[] = { | 222 const UnescapeURLCase unescape_cases[] = { |
| 219 {L"", UnescapeRule::NORMAL, L""}, | 223 {L"", UnescapeRule::NORMAL, L""}, |
| 220 {L"%2", UnescapeRule::NORMAL, L"%2"}, | 224 {L"%2", UnescapeRule::NORMAL, L"%2"}, |
| 221 {L"%%%%%%", UnescapeRule::NORMAL, L"%%%%%%"}, | 225 {L"%%%%%%", UnescapeRule::NORMAL, L"%%%%%%"}, |
| 222 {L"Don't escape anything", UnescapeRule::NORMAL, L"Don't escape anything"}, | 226 {L"Don't escape anything", |
| 223 {L"Invalid %escape %2", UnescapeRule::NORMAL, L"Invalid %escape %2"}, | 227 UnescapeRule::NORMAL, |
| 224 {L"Some%20random text %25%2dOK", UnescapeRule::NONE, | 228 L"Don't escape anything"}, |
| 225 L"Some%20random text %25%2dOK"}, | 229 {L"Invalid %escape %2", UnescapeRule::NORMAL, L"Invalid %escape %2"}, |
| 226 {L"Some%20random text %25%2dOK", UnescapeRule::NORMAL, | 230 {L"Some%20random text %25%2dOK", |
| 227 L"Some%20random text %25-OK"}, | 231 UnescapeRule::NONE, |
| 228 {L"Some%20random text %25%E2%80", UnescapeRule::NORMAL, | 232 L"Some%20random text %25%2dOK"}, |
| 229 L"Some%20random text %25\xE2\x80"}, | 233 {L"Some%20random text %25%2dOK", |
| 230 {L"Some%20random text %25%E2%80OK", UnescapeRule::NORMAL, | 234 UnescapeRule::NORMAL, |
| 231 L"Some%20random text %25\xE2\x80OK"}, | 235 L"Some%20random text %25-OK"}, |
| 232 {L"Some%20random text %25%E2%80%84OK", UnescapeRule::NORMAL, | 236 {L"Some%20random text %25%E2%80", |
| 233 L"Some%20random text %25\xE2\x80\x84OK"}, | 237 UnescapeRule::NORMAL, |
| 238 L"Some%20random text %25\xE2\x80"}, |
| 239 {L"Some%20random text %25%E2%80OK", |
| 240 UnescapeRule::NORMAL, |
| 241 L"Some%20random text %25\xE2\x80OK"}, |
| 242 {L"Some%20random text %25%E2%80%84OK", |
| 243 UnescapeRule::NORMAL, |
| 244 L"Some%20random text %25\xE2\x80\x84OK"}, |
| 234 | 245 |
| 235 // BiDi Control characters should not be unescaped. | 246 // BiDi Control characters should not be unescaped. |
| 236 {L"Some%20random text %25%D8%9COK", UnescapeRule::NORMAL, | 247 {L"Some%20random text %25%D8%9COK", |
| 237 L"Some%20random text %25%D8%9COK"}, | 248 UnescapeRule::NORMAL, |
| 238 {L"Some%20random text %25%E2%80%8EOK", UnescapeRule::NORMAL, | 249 L"Some%20random text %25%D8%9COK"}, |
| 239 L"Some%20random text %25%E2%80%8EOK"}, | 250 {L"Some%20random text %25%E2%80%8EOK", |
| 240 {L"Some%20random text %25%E2%80%8FOK", UnescapeRule::NORMAL, | 251 UnescapeRule::NORMAL, |
| 241 L"Some%20random text %25%E2%80%8FOK"}, | 252 L"Some%20random text %25%E2%80%8EOK"}, |
| 242 {L"Some%20random text %25%E2%80%AAOK", UnescapeRule::NORMAL, | 253 {L"Some%20random text %25%E2%80%8FOK", |
| 243 L"Some%20random text %25%E2%80%AAOK"}, | 254 UnescapeRule::NORMAL, |
| 244 {L"Some%20random text %25%E2%80%ABOK", UnescapeRule::NORMAL, | 255 L"Some%20random text %25%E2%80%8FOK"}, |
| 245 L"Some%20random text %25%E2%80%ABOK"}, | 256 {L"Some%20random text %25%E2%80%AAOK", |
| 246 {L"Some%20random text %25%E2%80%AEOK", UnescapeRule::NORMAL, | 257 UnescapeRule::NORMAL, |
| 247 L"Some%20random text %25%E2%80%AEOK"}, | 258 L"Some%20random text %25%E2%80%AAOK"}, |
| 248 {L"Some%20random text %25%E2%81%A6OK", UnescapeRule::NORMAL, | 259 {L"Some%20random text %25%E2%80%ABOK", |
| 249 L"Some%20random text %25%E2%81%A6OK"}, | 260 UnescapeRule::NORMAL, |
| 250 {L"Some%20random text %25%E2%81%A9OK", UnescapeRule::NORMAL, | 261 L"Some%20random text %25%E2%80%ABOK"}, |
| 251 L"Some%20random text %25%E2%81%A9OK"}, | 262 {L"Some%20random text %25%E2%80%AEOK", |
| 263 UnescapeRule::NORMAL, |
| 264 L"Some%20random text %25%E2%80%AEOK"}, |
| 265 {L"Some%20random text %25%E2%81%A6OK", |
| 266 UnescapeRule::NORMAL, |
| 267 L"Some%20random text %25%E2%81%A6OK"}, |
| 268 {L"Some%20random text %25%E2%81%A9OK", |
| 269 UnescapeRule::NORMAL, |
| 270 L"Some%20random text %25%E2%81%A9OK"}, |
| 252 | 271 |
| 253 {L"Some%20random text %25%2dOK", UnescapeRule::SPACES, | 272 {L"Some%20random text %25%2dOK", |
| 254 L"Some random text %25-OK"}, | 273 UnescapeRule::SPACES, |
| 255 {L"Some%20random text %25%2dOK", UnescapeRule::URL_SPECIAL_CHARS, | 274 L"Some random text %25-OK"}, |
| 256 L"Some%20random text %-OK"}, | 275 {L"Some%20random text %25%2dOK", |
| 257 {L"Some%20random text %25%2dOK", | 276 UnescapeRule::URL_SPECIAL_CHARS, |
| 258 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS, | 277 L"Some%20random text %-OK"}, |
| 259 L"Some random text %-OK"}, | 278 {L"Some%20random text %25%2dOK", |
| 260 {L"%A0%B1%C2%D3%E4%F5", UnescapeRule::NORMAL, L"\xA0\xB1\xC2\xD3\xE4\xF5"}, | 279 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS, |
| 261 {L"%Aa%Bb%Cc%Dd%Ee%Ff", UnescapeRule::NORMAL, L"\xAa\xBb\xCc\xDd\xEe\xFf"}, | 280 L"Some random text %-OK"}, |
| 262 // Certain URL-sensitive characters should not be unescaped unless asked. | 281 {L"%A0%B1%C2%D3%E4%F5", |
| 263 {L"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", UnescapeRule::SPACES, | 282 UnescapeRule::NORMAL, |
| 264 L"Hello %13%10world %23# %3F? %3D= %26& %25% %2B+"}, | 283 L"\xA0\xB1\xC2\xD3\xE4\xF5"}, |
| 265 {L"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", | 284 {L"%Aa%Bb%Cc%Dd%Ee%Ff", |
| 266 UnescapeRule::URL_SPECIAL_CHARS, | 285 UnescapeRule::NORMAL, |
| 267 L"Hello%20%13%10world ## ?? == && %% ++"}, | 286 L"\xAa\xBb\xCc\xDd\xEe\xFf"}, |
| 268 // We can neither escape nor unescape '@' since some websites expect it to | 287 // Certain URL-sensitive characters should not be unescaped unless asked. |
| 269 // be preserved as either '@' or "%40". | 288 {L"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", |
| 270 // See http://b/996720 and http://crbug.com/23933 . | 289 UnescapeRule::SPACES, |
| 271 {L"me@my%40example", UnescapeRule::NORMAL, L"me@my%40example"}, | 290 L"Hello %13%10world %23# %3F? %3D= %26& %25% %2B+"}, |
| 272 // Control characters. | 291 {L"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", |
| 273 {L"%01%02%03%04%05%06%07%08%09 %25", UnescapeRule::URL_SPECIAL_CHARS, | 292 UnescapeRule::URL_SPECIAL_CHARS, |
| 274 L"%01%02%03%04%05%06%07%08%09 %"}, | 293 L"Hello%20%13%10world ## ?? == && %% ++"}, |
| 275 {L"%01%02%03%04%05%06%07%08%09 %25", UnescapeRule::CONTROL_CHARS, | 294 // We can neither escape nor unescape '@' since some websites expect it to |
| 276 L"\x01\x02\x03\x04\x05\x06\x07\x08\x09 %25"}, | 295 // be preserved as either '@' or "%40". |
| 277 {L"Hello%20%13%10%02", UnescapeRule::SPACES, L"Hello %13%10%02"}, | 296 // See http://b/996720 and http://crbug.com/23933 . |
| 278 {L"Hello%20%13%10%02", UnescapeRule::CONTROL_CHARS, | 297 {L"me@my%40example", UnescapeRule::NORMAL, L"me@my%40example"}, |
| 279 L"Hello%20\x13\x10\x02"}, | 298 // Control characters. |
| 280 {L"Hello\x9824\x9827", UnescapeRule::CONTROL_CHARS, | 299 {L"%01%02%03%04%05%06%07%08%09 %25", |
| 281 L"Hello\x9824\x9827"}, | 300 UnescapeRule::URL_SPECIAL_CHARS, |
| 301 L"%01%02%03%04%05%06%07%08%09 %"}, |
| 302 {L"%01%02%03%04%05%06%07%08%09 %25", |
| 303 UnescapeRule::CONTROL_CHARS, |
| 304 L"\x01\x02\x03\x04\x05\x06\x07\x08\x09 %25"}, |
| 305 {L"Hello%20%13%10%02", UnescapeRule::SPACES, L"Hello %13%10%02"}, |
| 306 {L"Hello%20%13%10%02", |
| 307 UnescapeRule::CONTROL_CHARS, |
| 308 L"Hello%20\x13\x10\x02"}, |
| 309 {L"Hello\x9824\x9827", UnescapeRule::CONTROL_CHARS, L"Hello\x9824\x9827"}, |
| 282 }; | 310 }; |
| 283 | 311 |
| 284 for (size_t i = 0; i < arraysize(unescape_cases); i++) { | 312 for (size_t i = 0; i < arraysize(unescape_cases); i++) { |
| 285 base::string16 str(base::WideToUTF16(unescape_cases[i].input)); | 313 base::string16 str(base::WideToUTF16(unescape_cases[i].input)); |
| 286 EXPECT_EQ(base::WideToUTF16(unescape_cases[i].output), | 314 EXPECT_EQ(base::WideToUTF16(unescape_cases[i].output), |
| 287 UnescapeURLComponent(str, unescape_cases[i].rules)); | 315 UnescapeURLComponent(str, unescape_cases[i].rules)); |
| 288 } | 316 } |
| 289 | 317 |
| 290 // Test the NULL character unescaping (which wouldn't work above since those | 318 // Test the NULL character unescaping (which wouldn't work above since those |
| 291 // are just char pointers). | 319 // are just char pointers). |
| (...skipping 10 matching lines...) Expand all Loading... |
| 302 | 330 |
| 303 // When we're not unescaping NULLs. | 331 // When we're not unescaping NULLs. |
| 304 expected = base::WideToUTF16(L"Null"); | 332 expected = base::WideToUTF16(L"Null"); |
| 305 expected.push_back(0); | 333 expected.push_back(0); |
| 306 expected.append(base::WideToUTF16(L"%009Test")); | 334 expected.append(base::WideToUTF16(L"%009Test")); |
| 307 EXPECT_EQ(expected, UnescapeURLComponent(input, UnescapeRule::NORMAL)); | 335 EXPECT_EQ(expected, UnescapeURLComponent(input, UnescapeRule::NORMAL)); |
| 308 } | 336 } |
| 309 | 337 |
| 310 TEST(EscapeTest, UnescapeAndDecodeUTF8URLComponent) { | 338 TEST(EscapeTest, UnescapeAndDecodeUTF8URLComponent) { |
| 311 const UnescapeAndDecodeCase unescape_cases[] = { | 339 const UnescapeAndDecodeCase unescape_cases[] = { |
| 312 { "%", | 340 {"%", "%", "%", L"%"}, |
| 313 "%", | 341 {"+", "+", " ", L"+"}, |
| 314 "%", | 342 {"%2+", "%2+", "%2 ", L"%2+"}, |
| 315 L"%"}, | 343 {"+%%%+%%%", "+%%%+%%%", " %%% %%%", L"+%%%+%%%"}, |
| 316 { "+", | 344 {"Don't escape anything", |
| 317 "+", | 345 "Don't escape anything", |
| 318 " ", | 346 "Don't escape anything", |
| 319 L"+"}, | 347 L"Don't escape anything"}, |
| 320 { "%2+", | 348 {"+Invalid %escape %2+", |
| 321 "%2+", | 349 "+Invalid %escape %2+", |
| 322 "%2 ", | 350 " Invalid %escape %2 ", |
| 323 L"%2+"}, | 351 L"+Invalid %escape %2+"}, |
| 324 { "+%%%+%%%", | 352 {"Some random text %25%2dOK", |
| 325 "+%%%+%%%", | 353 "Some random text %25-OK", |
| 326 " %%% %%%", | 354 "Some random text %25-OK", |
| 327 L"+%%%+%%%"}, | 355 L"Some random text %25-OK"}, |
| 328 { "Don't escape anything", | 356 {"%01%02%03%04%05%06%07%08%09", |
| 329 "Don't escape anything", | 357 "%01%02%03%04%05%06%07%08%09", |
| 330 "Don't escape anything", | 358 "%01%02%03%04%05%06%07%08%09", |
| 331 L"Don't escape anything"}, | 359 L"%01%02%03%04%05%06%07%08%09"}, |
| 332 { "+Invalid %escape %2+", | 360 {"%E4%BD%A0+%E5%A5%BD", |
| 333 "+Invalid %escape %2+", | 361 "\xE4\xBD\xA0+\xE5\xA5\xBD", |
| 334 " Invalid %escape %2 ", | 362 "\xE4\xBD\xA0 \xE5\xA5\xBD", |
| 335 L"+Invalid %escape %2+"}, | 363 L"\x4f60+\x597d"}, |
| 336 { "Some random text %25%2dOK", | 364 {"%ED%ED", // Invalid UTF-8. |
| 337 "Some random text %25-OK", | 365 "\xED\xED", |
| 338 "Some random text %25-OK", | 366 "\xED\xED", |
| 339 L"Some random text %25-OK"}, | 367 L"%ED%ED"}, // Invalid UTF-8 -> kept unescaped. |
| 340 { "%01%02%03%04%05%06%07%08%09", | |
| 341 "%01%02%03%04%05%06%07%08%09", | |
| 342 "%01%02%03%04%05%06%07%08%09", | |
| 343 L"%01%02%03%04%05%06%07%08%09"}, | |
| 344 { "%E4%BD%A0+%E5%A5%BD", | |
| 345 "\xE4\xBD\xA0+\xE5\xA5\xBD", | |
| 346 "\xE4\xBD\xA0 \xE5\xA5\xBD", | |
| 347 L"\x4f60+\x597d"}, | |
| 348 { "%ED%ED", // Invalid UTF-8. | |
| 349 "\xED\xED", | |
| 350 "\xED\xED", | |
| 351 L"%ED%ED"}, // Invalid UTF-8 -> kept unescaped. | |
| 352 }; | 368 }; |
| 353 | 369 |
| 354 for (size_t i = 0; i < arraysize(unescape_cases); i++) { | 370 for (size_t i = 0; i < arraysize(unescape_cases); i++) { |
| 355 std::string unescaped = UnescapeURLComponent(unescape_cases[i].input, | 371 std::string unescaped = |
| 356 UnescapeRule::NORMAL); | 372 UnescapeURLComponent(unescape_cases[i].input, UnescapeRule::NORMAL); |
| 357 EXPECT_EQ(std::string(unescape_cases[i].url_unescaped), unescaped); | 373 EXPECT_EQ(std::string(unescape_cases[i].url_unescaped), unescaped); |
| 358 | 374 |
| 359 unescaped = UnescapeURLComponent(unescape_cases[i].input, | 375 unescaped = UnescapeURLComponent(unescape_cases[i].input, |
| 360 UnescapeRule::REPLACE_PLUS_WITH_SPACE); | 376 UnescapeRule::REPLACE_PLUS_WITH_SPACE); |
| 361 EXPECT_EQ(std::string(unescape_cases[i].query_unescaped), unescaped); | 377 EXPECT_EQ(std::string(unescape_cases[i].query_unescaped), unescaped); |
| 362 | 378 |
| 363 // TODO: Need to test unescape_spaces and unescape_percent. | 379 // TODO: Need to test unescape_spaces and unescape_percent. |
| 364 base::string16 decoded = UnescapeAndDecodeUTF8URLComponent( | 380 base::string16 decoded = UnescapeAndDecodeUTF8URLComponent( |
| 365 unescape_cases[i].input, UnescapeRule::NORMAL); | 381 unescape_cases[i].input, UnescapeRule::NORMAL); |
| 366 EXPECT_EQ(base::WideToUTF16(unescape_cases[i].decoded), decoded); | 382 EXPECT_EQ(base::WideToUTF16(unescape_cases[i].decoded), decoded); |
| 367 } | 383 } |
| 368 } | 384 } |
| 369 | 385 |
| 370 TEST(EscapeTest, AdjustOffset) { | 386 TEST(EscapeTest, AdjustOffset) { |
| 371 const AdjustOffsetCase adjust_cases[] = { | 387 const AdjustOffsetCase adjust_cases[] = { |
| 372 {"", 0, 0}, | 388 {"", 0, 0}, |
| 373 {"test", 0, 0}, | 389 {"test", 0, 0}, |
| 374 {"test", 2, 2}, | 390 {"test", 2, 2}, |
| 375 {"test", 4, 4}, | 391 {"test", 4, 4}, |
| 376 {"test", std::string::npos, std::string::npos}, | 392 {"test", std::string::npos, std::string::npos}, |
| 377 {"%2dtest", 6, 4}, | 393 {"%2dtest", 6, 4}, |
| 378 {"%2dtest", 3, 1}, | 394 {"%2dtest", 3, 1}, |
| 379 {"%2dtest", 2, std::string::npos}, | 395 {"%2dtest", 2, std::string::npos}, |
| 380 {"%2dtest", 1, std::string::npos}, | 396 {"%2dtest", 1, std::string::npos}, |
| 381 {"%2dtest", 0, 0}, | 397 {"%2dtest", 0, 0}, |
| 382 {"test%2d", 2, 2}, | 398 {"test%2d", 2, 2}, |
| 383 {"%E4%BD%A0+%E5%A5%BD", 9, 1}, | 399 {"%E4%BD%A0+%E5%A5%BD", 9, 1}, |
| 384 {"%E4%BD%A0+%E5%A5%BD", 6, std::string::npos}, | 400 {"%E4%BD%A0+%E5%A5%BD", 6, std::string::npos}, |
| 385 {"%E4%BD%A0+%E5%A5%BD", 0, 0}, | 401 {"%E4%BD%A0+%E5%A5%BD", 0, 0}, |
| 386 {"%E4%BD%A0+%E5%A5%BD", 10, 2}, | 402 {"%E4%BD%A0+%E5%A5%BD", 10, 2}, |
| 387 {"%E4%BD%A0+%E5%A5%BD", 19, 3}, | 403 {"%E4%BD%A0+%E5%A5%BD", 19, 3}, |
| 388 | 404 |
| 389 {"hi%41test%E4%BD%A0+%E5%A5%BD", 18, 8}, | 405 {"hi%41test%E4%BD%A0+%E5%A5%BD", 18, 8}, |
| 390 {"hi%41test%E4%BD%A0+%E5%A5%BD", 15, std::string::npos}, | 406 {"hi%41test%E4%BD%A0+%E5%A5%BD", 15, std::string::npos}, |
| 391 {"hi%41test%E4%BD%A0+%E5%A5%BD", 9, 7}, | 407 {"hi%41test%E4%BD%A0+%E5%A5%BD", 9, 7}, |
| 392 {"hi%41test%E4%BD%A0+%E5%A5%BD", 19, 9}, | 408 {"hi%41test%E4%BD%A0+%E5%A5%BD", 19, 9}, |
| 393 {"hi%41test%E4%BD%A0+%E5%A5%BD", 28, 10}, | 409 {"hi%41test%E4%BD%A0+%E5%A5%BD", 28, 10}, |
| 394 {"hi%41test%E4%BD%A0+%E5%A5%BD", 0, 0}, | 410 {"hi%41test%E4%BD%A0+%E5%A5%BD", 0, 0}, |
| 395 {"hi%41test%E4%BD%A0+%E5%A5%BD", 2, 2}, | 411 {"hi%41test%E4%BD%A0+%E5%A5%BD", 2, 2}, |
| 396 {"hi%41test%E4%BD%A0+%E5%A5%BD", 3, std::string::npos}, | 412 {"hi%41test%E4%BD%A0+%E5%A5%BD", 3, std::string::npos}, |
| 397 {"hi%41test%E4%BD%A0+%E5%A5%BD", 5, 3}, | 413 {"hi%41test%E4%BD%A0+%E5%A5%BD", 5, 3}, |
| 398 | 414 |
| 399 {"%E4%BD%A0+%E5%A5%BDhi%41test", 9, 1}, | 415 {"%E4%BD%A0+%E5%A5%BDhi%41test", 9, 1}, |
| 400 {"%E4%BD%A0+%E5%A5%BDhi%41test", 6, std::string::npos}, | 416 {"%E4%BD%A0+%E5%A5%BDhi%41test", 6, std::string::npos}, |
| 401 {"%E4%BD%A0+%E5%A5%BDhi%41test", 0, 0}, | 417 {"%E4%BD%A0+%E5%A5%BDhi%41test", 0, 0}, |
| 402 {"%E4%BD%A0+%E5%A5%BDhi%41test", 10, 2}, | 418 {"%E4%BD%A0+%E5%A5%BDhi%41test", 10, 2}, |
| 403 {"%E4%BD%A0+%E5%A5%BDhi%41test", 19, 3}, | 419 {"%E4%BD%A0+%E5%A5%BDhi%41test", 19, 3}, |
| 404 {"%E4%BD%A0+%E5%A5%BDhi%41test", 21, 5}, | 420 {"%E4%BD%A0+%E5%A5%BDhi%41test", 21, 5}, |
| 405 {"%E4%BD%A0+%E5%A5%BDhi%41test", 22, std::string::npos}, | 421 {"%E4%BD%A0+%E5%A5%BDhi%41test", 22, std::string::npos}, |
| 406 {"%E4%BD%A0+%E5%A5%BDhi%41test", 24, 6}, | 422 {"%E4%BD%A0+%E5%A5%BDhi%41test", 24, 6}, |
| 407 {"%E4%BD%A0+%E5%A5%BDhi%41test", 28, 10}, | 423 {"%E4%BD%A0+%E5%A5%BDhi%41test", 28, 10}, |
| 408 | 424 |
| 409 {"%ED%B0%80+%E5%A5%BD", 6, 6}, // not convertable to UTF-8 | 425 {"%ED%B0%80+%E5%A5%BD", 6, 6}, // not convertable to UTF-8 |
| 410 }; | 426 }; |
| 411 | 427 |
| 412 for (size_t i = 0; i < arraysize(adjust_cases); i++) { | 428 for (size_t i = 0; i < arraysize(adjust_cases); i++) { |
| 413 size_t offset = adjust_cases[i].input_offset; | 429 size_t offset = adjust_cases[i].input_offset; |
| 414 base::OffsetAdjuster::Adjustments adjustments; | 430 base::OffsetAdjuster::Adjustments adjustments; |
| 415 UnescapeAndDecodeUTF8URLComponentWithAdjustments( | 431 UnescapeAndDecodeUTF8URLComponentWithAdjustments( |
| 416 adjust_cases[i].input, UnescapeRule::NORMAL, &adjustments); | 432 adjust_cases[i].input, UnescapeRule::NORMAL, &adjustments); |
| 417 base::OffsetAdjuster::AdjustOffset(adjustments, &offset); | 433 base::OffsetAdjuster::AdjustOffset(adjustments, &offset); |
| 418 EXPECT_EQ(adjust_cases[i].output_offset, offset) | 434 EXPECT_EQ(adjust_cases[i].output_offset, offset) |
| 419 << "input=" << adjust_cases[i].input | 435 << "input=" << adjust_cases[i].input |
| 420 << " offset=" << adjust_cases[i].input_offset; | 436 << " offset=" << adjust_cases[i].input_offset; |
| 421 } | 437 } |
| 422 } | 438 } |
| 423 | 439 |
| 424 TEST(EscapeTest, EscapeForHTML) { | 440 TEST(EscapeTest, EscapeForHTML) { |
| 425 const EscapeForHTMLCase tests[] = { | 441 const EscapeForHTMLCase tests[] = { |
| 426 { "hello", "hello" }, | 442 {"hello", "hello"}, |
| 427 { "<hello>", "<hello>" }, | 443 {"<hello>", "<hello>"}, |
| 428 { "don\'t mess with me", "don't mess with me" }, | 444 {"don\'t mess with me", "don't mess with me"}, |
| 429 }; | 445 }; |
| 430 for (size_t i = 0; i < arraysize(tests); ++i) { | 446 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 431 std::string result = EscapeForHTML(std::string(tests[i].input)); | 447 std::string result = EscapeForHTML(std::string(tests[i].input)); |
| 432 EXPECT_EQ(std::string(tests[i].expected_output), result); | 448 EXPECT_EQ(std::string(tests[i].expected_output), result); |
| 433 } | 449 } |
| 434 } | 450 } |
| 435 | 451 |
| 436 TEST(EscapeTest, UnescapeForHTML) { | 452 TEST(EscapeTest, UnescapeForHTML) { |
| 437 const EscapeForHTMLCase tests[] = { | 453 const EscapeForHTMLCase tests[] = { |
| 438 { "", "" }, | 454 {"", ""}, |
| 439 { "<hello>", "<hello>" }, | 455 {"<hello>", "<hello>"}, |
| 440 { "don't mess with me", "don\'t mess with me" }, | 456 {"don't mess with me", "don\'t mess with me"}, |
| 441 { "<>&"'", "<>&\"'" }, | 457 {"<>&"'", "<>&\"'"}, |
| 442 { "& lt; & ; &; '", "& lt; & ; &; '" }, | 458 {"& lt; & ; &; '", "& lt; & ; &; '"}, |
| 443 { "&", "&" }, | 459 {"&", "&"}, |
| 444 { """, "\"" }, | 460 {""", "\""}, |
| 445 { "'", "'" }, | 461 {"'", "'"}, |
| 446 { "<", "<" }, | 462 {"<", "<"}, |
| 447 { ">", ">" }, | 463 {">", ">"}, |
| 448 { "& &", "& &" }, | 464 {"& &", "& &"}, |
| 449 }; | 465 }; |
| 450 for (size_t i = 0; i < arraysize(tests); ++i) { | 466 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 451 base::string16 result = UnescapeForHTML(base::ASCIIToUTF16(tests[i].input)); | 467 base::string16 result = UnescapeForHTML(base::ASCIIToUTF16(tests[i].input)); |
| 452 EXPECT_EQ(base::ASCIIToUTF16(tests[i].expected_output), result); | 468 EXPECT_EQ(base::ASCIIToUTF16(tests[i].expected_output), result); |
| 453 } | 469 } |
| 454 } | 470 } |
| 455 | 471 |
| 456 | |
| 457 } // namespace | 472 } // namespace |
| 458 } // namespace net | 473 } // namespace net |
| OLD | NEW |