| 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", UnescapeRule::NONE, |
| 160 "Some%20random text %25%2dOK"}, | 155 "Some%20random text %25%2dOK"}, |
| 161 {"Some%20random text %25%2dOK", UnescapeRule::NORMAL, | 156 {"Some%20random text %25%2dOK", UnescapeRule::NORMAL, |
| 162 "Some%20random text %25-OK"}, | 157 "Some%20random text %25-OK"}, |
| 163 {"Some%20random text %25%2dOK", UnescapeRule::SPACES, | 158 {"Some%20random text %25%2dOK", UnescapeRule::SPACES, |
| 164 "Some random text %25-OK"}, | 159 "Some random text %25-OK"}, |
| 165 {"Some%20random text %25%2dOK", UnescapeRule::URL_SPECIAL_CHARS, | 160 {"Some%20random text %25%2dOK", UnescapeRule::URL_SPECIAL_CHARS, |
| 166 "Some%20random text %-OK"}, | 161 "Some%20random text %-OK"}, |
| 167 {"Some%20random text %25%2dOK", | 162 {"Some%20random text %25%2dOK", |
| 168 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS, | 163 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS, |
| 169 "Some random text %-OK"}, | 164 "Some random text %-OK"}, |
| 170 {"%A0%B1%C2%D3%E4%F5", UnescapeRule::NORMAL, "\xA0\xB1\xC2\xD3\xE4\xF5"}, | 165 {"%A0%B1%C2%D3%E4%F5", UnescapeRule::NORMAL, "\xA0\xB1\xC2\xD3\xE4\xF5"}, |
| 171 {"%Aa%Bb%Cc%Dd%Ee%Ff", UnescapeRule::NORMAL, "\xAa\xBb\xCc\xDd\xEe\xFf"}, | 166 {"%Aa%Bb%Cc%Dd%Ee%Ff", UnescapeRule::NORMAL, "\xAa\xBb\xCc\xDd\xEe\xFf"}, |
| 172 // Certain URL-sensitive characters should not be unescaped unless asked. | 167 // Certain URL-sensitive characters should not be unescaped unless asked. |
| 173 {"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", UnescapeRule::SPACES, | 168 {"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", |
| 174 "Hello %13%10world %23# %3F? %3D= %26& %25% %2B+"}, | 169 UnescapeRule::SPACES, "Hello %13%10world %23# %3F? %3D= %26& %25% %2B+"}, |
| 175 {"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", | 170 {"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", |
| 176 UnescapeRule::URL_SPECIAL_CHARS, | 171 UnescapeRule::URL_SPECIAL_CHARS, |
| 177 "Hello%20%13%10world ## ?? == && %% ++"}, | 172 "Hello%20%13%10world ## ?? == && %% ++"}, |
| 178 // We can neither escape nor unescape '@' since some websites expect it to | 173 // We can neither escape nor unescape '@' since some websites expect it to |
| 179 // be preserved as either '@' or "%40". | 174 // be preserved as either '@' or "%40". |
| 180 // See http://b/996720 and http://crbug.com/23933 . | 175 // See http://b/996720 and http://crbug.com/23933 . |
| 181 {"me@my%40example", UnescapeRule::NORMAL, "me@my%40example"}, | 176 {"me@my%40example", UnescapeRule::NORMAL, "me@my%40example"}, |
| 182 // Control characters. | 177 // Control characters. |
| 183 {"%01%02%03%04%05%06%07%08%09 %25", UnescapeRule::URL_SPECIAL_CHARS, | 178 {"%01%02%03%04%05%06%07%08%09 %25", UnescapeRule::URL_SPECIAL_CHARS, |
| 184 "%01%02%03%04%05%06%07%08%09 %"}, | 179 "%01%02%03%04%05%06%07%08%09 %"}, |
| 185 {"%01%02%03%04%05%06%07%08%09 %25", UnescapeRule::CONTROL_CHARS, | 180 {"%01%02%03%04%05%06%07%08%09 %25", UnescapeRule::CONTROL_CHARS, |
| 186 "\x01\x02\x03\x04\x05\x06\x07\x08\x09 %25"}, | 181 "\x01\x02\x03\x04\x05\x06\x07\x08\x09 %25"}, |
| 187 {"Hello%20%13%10%02", UnescapeRule::SPACES, "Hello %13%10%02"}, | 182 {"Hello%20%13%10%02", UnescapeRule::SPACES, "Hello %13%10%02"}, |
| 188 {"Hello%20%13%10%02", UnescapeRule::CONTROL_CHARS, "Hello%20\x13\x10\x02"}, | 183 {"Hello%20%13%10%02", UnescapeRule::CONTROL_CHARS, |
| 184 "Hello%20\x13\x10\x02"}, |
| 189 }; | 185 }; |
| 190 | 186 |
| 191 for (size_t i = 0; i < arraysize(unescape_cases); i++) { | 187 for (size_t i = 0; i < arraysize(unescape_cases); i++) { |
| 192 std::string str(unescape_cases[i].input); | 188 std::string str(unescape_cases[i].input); |
| 193 EXPECT_EQ(std::string(unescape_cases[i].output), | 189 EXPECT_EQ(std::string(unescape_cases[i].output), |
| 194 UnescapeURLComponent(str, unescape_cases[i].rules)); | 190 UnescapeURLComponent(str, unescape_cases[i].rules)); |
| 195 } | 191 } |
| 196 | 192 |
| 197 // Test the NULL character unescaping (which wouldn't work above since those | 193 // Test the NULL character unescaping (which wouldn't work above since those |
| 198 // are just char pointers). | 194 // are just char pointers). |
| (...skipping 10 matching lines...) Expand all Loading... |
| 209 | 205 |
| 210 // When we're not unescaping NULLs. | 206 // When we're not unescaping NULLs. |
| 211 expected = "Null"; | 207 expected = "Null"; |
| 212 expected.push_back(0); | 208 expected.push_back(0); |
| 213 expected.append("%009Test"); | 209 expected.append("%009Test"); |
| 214 EXPECT_EQ(expected, UnescapeURLComponent(input, UnescapeRule::NORMAL)); | 210 EXPECT_EQ(expected, UnescapeURLComponent(input, UnescapeRule::NORMAL)); |
| 215 } | 211 } |
| 216 | 212 |
| 217 TEST(EscapeTest, UnescapeURLComponent) { | 213 TEST(EscapeTest, UnescapeURLComponent) { |
| 218 const UnescapeURLCase unescape_cases[] = { | 214 const UnescapeURLCase unescape_cases[] = { |
| 219 {L"", UnescapeRule::NORMAL, L""}, | 215 {L"", UnescapeRule::NORMAL, L""}, |
| 220 {L"%2", UnescapeRule::NORMAL, L"%2"}, | 216 {L"%2", UnescapeRule::NORMAL, L"%2"}, |
| 221 {L"%%%%%%", UnescapeRule::NORMAL, L"%%%%%%"}, | 217 {L"%%%%%%", UnescapeRule::NORMAL, L"%%%%%%"}, |
| 222 {L"Don't escape anything", UnescapeRule::NORMAL, L"Don't escape anything"}, | 218 {L"Don't escape anything", UnescapeRule::NORMAL, |
| 223 {L"Invalid %escape %2", UnescapeRule::NORMAL, L"Invalid %escape %2"}, | 219 L"Don't escape anything"}, |
| 224 {L"Some%20random text %25%2dOK", UnescapeRule::NONE, | 220 {L"Invalid %escape %2", UnescapeRule::NORMAL, L"Invalid %escape %2"}, |
| 225 L"Some%20random text %25%2dOK"}, | 221 {L"Some%20random text %25%2dOK", UnescapeRule::NONE, |
| 226 {L"Some%20random text %25%2dOK", UnescapeRule::NORMAL, | 222 L"Some%20random text %25%2dOK"}, |
| 227 L"Some%20random text %25-OK"}, | 223 {L"Some%20random text %25%2dOK", UnescapeRule::NORMAL, |
| 228 {L"Some%20random text %25%E2%80", UnescapeRule::NORMAL, | 224 L"Some%20random text %25-OK"}, |
| 229 L"Some%20random text %25\xE2\x80"}, | 225 {L"Some%20random text %25%E2%80", UnescapeRule::NORMAL, |
| 230 {L"Some%20random text %25%E2%80OK", UnescapeRule::NORMAL, | 226 L"Some%20random text %25\xE2\x80"}, |
| 231 L"Some%20random text %25\xE2\x80OK"}, | 227 {L"Some%20random text %25%E2%80OK", UnescapeRule::NORMAL, |
| 232 {L"Some%20random text %25%E2%80%84OK", UnescapeRule::NORMAL, | 228 L"Some%20random text %25\xE2\x80OK"}, |
| 233 L"Some%20random text %25\xE2\x80\x84OK"}, | 229 {L"Some%20random text %25%E2%80%84OK", UnescapeRule::NORMAL, |
| 230 L"Some%20random text %25\xE2\x80\x84OK"}, |
| 234 | 231 |
| 235 // BiDi Control characters should not be unescaped. | 232 // BiDi Control characters should not be unescaped. |
| 236 {L"Some%20random text %25%D8%9COK", UnescapeRule::NORMAL, | 233 {L"Some%20random text %25%D8%9COK", UnescapeRule::NORMAL, |
| 237 L"Some%20random text %25%D8%9COK"}, | 234 L"Some%20random text %25%D8%9COK"}, |
| 238 {L"Some%20random text %25%E2%80%8EOK", UnescapeRule::NORMAL, | 235 {L"Some%20random text %25%E2%80%8EOK", UnescapeRule::NORMAL, |
| 239 L"Some%20random text %25%E2%80%8EOK"}, | 236 L"Some%20random text %25%E2%80%8EOK"}, |
| 240 {L"Some%20random text %25%E2%80%8FOK", UnescapeRule::NORMAL, | 237 {L"Some%20random text %25%E2%80%8FOK", UnescapeRule::NORMAL, |
| 241 L"Some%20random text %25%E2%80%8FOK"}, | 238 L"Some%20random text %25%E2%80%8FOK"}, |
| 242 {L"Some%20random text %25%E2%80%AAOK", UnescapeRule::NORMAL, | 239 {L"Some%20random text %25%E2%80%AAOK", UnescapeRule::NORMAL, |
| 243 L"Some%20random text %25%E2%80%AAOK"}, | 240 L"Some%20random text %25%E2%80%AAOK"}, |
| 244 {L"Some%20random text %25%E2%80%ABOK", UnescapeRule::NORMAL, | 241 {L"Some%20random text %25%E2%80%ABOK", UnescapeRule::NORMAL, |
| 245 L"Some%20random text %25%E2%80%ABOK"}, | 242 L"Some%20random text %25%E2%80%ABOK"}, |
| 246 {L"Some%20random text %25%E2%80%AEOK", UnescapeRule::NORMAL, | 243 {L"Some%20random text %25%E2%80%AEOK", UnescapeRule::NORMAL, |
| 247 L"Some%20random text %25%E2%80%AEOK"}, | 244 L"Some%20random text %25%E2%80%AEOK"}, |
| 248 {L"Some%20random text %25%E2%81%A6OK", UnescapeRule::NORMAL, | 245 {L"Some%20random text %25%E2%81%A6OK", UnescapeRule::NORMAL, |
| 249 L"Some%20random text %25%E2%81%A6OK"}, | 246 L"Some%20random text %25%E2%81%A6OK"}, |
| 250 {L"Some%20random text %25%E2%81%A9OK", UnescapeRule::NORMAL, | 247 {L"Some%20random text %25%E2%81%A9OK", UnescapeRule::NORMAL, |
| 251 L"Some%20random text %25%E2%81%A9OK"}, | 248 L"Some%20random text %25%E2%81%A9OK"}, |
| 252 | 249 {L"Some%20random text %25%2dOK", UnescapeRule::SPACES, |
| 253 {L"Some%20random text %25%2dOK", UnescapeRule::SPACES, | 250 L"Some random text %25-OK"}, |
| 254 L"Some random text %25-OK"}, | 251 {L"Some%20random text %25%2dOK", UnescapeRule::URL_SPECIAL_CHARS, |
| 255 {L"Some%20random text %25%2dOK", UnescapeRule::URL_SPECIAL_CHARS, | 252 L"Some%20random text %-OK"}, |
| 256 L"Some%20random text %-OK"}, | 253 {L"Some%20random text %25%2dOK", |
| 257 {L"Some%20random text %25%2dOK", | 254 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS, |
| 258 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS, | 255 L"Some random text %-OK"}, |
| 259 L"Some random text %-OK"}, | 256 {L"%A0%B1%C2%D3%E4%F5", UnescapeRule::NORMAL, |
| 260 {L"%A0%B1%C2%D3%E4%F5", UnescapeRule::NORMAL, L"\xA0\xB1\xC2\xD3\xE4\xF5"}, | 257 L"\xA0\xB1\xC2\xD3\xE4\xF5"}, |
| 261 {L"%Aa%Bb%Cc%Dd%Ee%Ff", UnescapeRule::NORMAL, L"\xAa\xBb\xCc\xDd\xEe\xFf"}, | 258 {L"%Aa%Bb%Cc%Dd%Ee%Ff", UnescapeRule::NORMAL, |
| 262 // Certain URL-sensitive characters should not be unescaped unless asked. | 259 L"\xAa\xBb\xCc\xDd\xEe\xFf"}, |
| 263 {L"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", UnescapeRule::SPACES, | 260 // Certain URL-sensitive characters should not be unescaped unless asked. |
| 264 L"Hello %13%10world %23# %3F? %3D= %26& %25% %2B+"}, | 261 {L"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", |
| 265 {L"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", | 262 UnescapeRule::SPACES, |
| 266 UnescapeRule::URL_SPECIAL_CHARS, | 263 L"Hello %13%10world %23# %3F? %3D= %26& %25% %2B+"}, |
| 267 L"Hello%20%13%10world ## ?? == && %% ++"}, | 264 {L"Hello%20%13%10world %23# %3F? %3D= %26& %25% %2B+", |
| 268 // We can neither escape nor unescape '@' since some websites expect it to | 265 UnescapeRule::URL_SPECIAL_CHARS, |
| 269 // be preserved as either '@' or "%40". | 266 L"Hello%20%13%10world ## ?? == && %% ++"}, |
| 270 // See http://b/996720 and http://crbug.com/23933 . | 267 // We can neither escape nor unescape '@' since some websites expect it to |
| 271 {L"me@my%40example", UnescapeRule::NORMAL, L"me@my%40example"}, | 268 // be preserved as either '@' or "%40". |
| 272 // Control characters. | 269 // See http://b/996720 and http://crbug.com/23933 . |
| 273 {L"%01%02%03%04%05%06%07%08%09 %25", UnescapeRule::URL_SPECIAL_CHARS, | 270 {L"me@my%40example", UnescapeRule::NORMAL, L"me@my%40example"}, |
| 274 L"%01%02%03%04%05%06%07%08%09 %"}, | 271 // Control characters. |
| 275 {L"%01%02%03%04%05%06%07%08%09 %25", UnescapeRule::CONTROL_CHARS, | 272 {L"%01%02%03%04%05%06%07%08%09 %25", UnescapeRule::URL_SPECIAL_CHARS, |
| 276 L"\x01\x02\x03\x04\x05\x06\x07\x08\x09 %25"}, | 273 L"%01%02%03%04%05%06%07%08%09 %"}, |
| 277 {L"Hello%20%13%10%02", UnescapeRule::SPACES, L"Hello %13%10%02"}, | 274 {L"%01%02%03%04%05%06%07%08%09 %25", UnescapeRule::CONTROL_CHARS, |
| 278 {L"Hello%20%13%10%02", UnescapeRule::CONTROL_CHARS, | 275 L"\x01\x02\x03\x04\x05\x06\x07\x08\x09 %25"}, |
| 279 L"Hello%20\x13\x10\x02"}, | 276 {L"Hello%20%13%10%02", UnescapeRule::SPACES, L"Hello %13%10%02"}, |
| 280 {L"Hello\x9824\x9827", UnescapeRule::CONTROL_CHARS, | 277 {L"Hello%20%13%10%02", UnescapeRule::CONTROL_CHARS, |
| 281 L"Hello\x9824\x9827"}, | 278 L"Hello%20\x13\x10\x02"}, |
| 279 {L"Hello\x9824\x9827", UnescapeRule::CONTROL_CHARS, L"Hello\x9824\x9827"}, |
| 282 }; | 280 }; |
| 283 | 281 |
| 284 for (size_t i = 0; i < arraysize(unescape_cases); i++) { | 282 for (size_t i = 0; i < arraysize(unescape_cases); i++) { |
| 285 base::string16 str(base::WideToUTF16(unescape_cases[i].input)); | 283 base::string16 str(base::WideToUTF16(unescape_cases[i].input)); |
| 286 EXPECT_EQ(base::WideToUTF16(unescape_cases[i].output), | 284 EXPECT_EQ(base::WideToUTF16(unescape_cases[i].output), |
| 287 UnescapeURLComponent(str, unescape_cases[i].rules)); | 285 UnescapeURLComponent(str, unescape_cases[i].rules)); |
| 288 } | 286 } |
| 289 | 287 |
| 290 // Test the NULL character unescaping (which wouldn't work above since those | 288 // Test the NULL character unescaping (which wouldn't work above since those |
| 291 // are just char pointers). | 289 // are just char pointers). |
| (...skipping 10 matching lines...) Expand all Loading... |
| 302 | 300 |
| 303 // When we're not unescaping NULLs. | 301 // When we're not unescaping NULLs. |
| 304 expected = base::WideToUTF16(L"Null"); | 302 expected = base::WideToUTF16(L"Null"); |
| 305 expected.push_back(0); | 303 expected.push_back(0); |
| 306 expected.append(base::WideToUTF16(L"%009Test")); | 304 expected.append(base::WideToUTF16(L"%009Test")); |
| 307 EXPECT_EQ(expected, UnescapeURLComponent(input, UnescapeRule::NORMAL)); | 305 EXPECT_EQ(expected, UnescapeURLComponent(input, UnescapeRule::NORMAL)); |
| 308 } | 306 } |
| 309 | 307 |
| 310 TEST(EscapeTest, UnescapeAndDecodeUTF8URLComponent) { | 308 TEST(EscapeTest, UnescapeAndDecodeUTF8URLComponent) { |
| 311 const UnescapeAndDecodeCase unescape_cases[] = { | 309 const UnescapeAndDecodeCase unescape_cases[] = { |
| 312 { "%", | 310 {"%", "%", "%", L"%"}, |
| 313 "%", | 311 {"+", "+", " ", L"+"}, |
| 314 "%", | 312 {"%2+", "%2+", "%2 ", L"%2+"}, |
| 315 L"%"}, | 313 {"+%%%+%%%", "+%%%+%%%", " %%% %%%", L"+%%%+%%%"}, |
| 316 { "+", | 314 {"Don't escape anything", "Don't escape anything", |
| 317 "+", | 315 "Don't escape anything", L"Don't escape anything"}, |
| 318 " ", | 316 {"+Invalid %escape %2+", "+Invalid %escape %2+", " Invalid %escape %2 ", |
| 319 L"+"}, | 317 L"+Invalid %escape %2+"}, |
| 320 { "%2+", | 318 {"Some random text %25%2dOK", "Some random text %25-OK", |
| 321 "%2+", | 319 "Some random text %25-OK", L"Some random text %25-OK"}, |
| 322 "%2 ", | 320 {"%01%02%03%04%05%06%07%08%09", "%01%02%03%04%05%06%07%08%09", |
| 323 L"%2+"}, | 321 "%01%02%03%04%05%06%07%08%09", L"%01%02%03%04%05%06%07%08%09"}, |
| 324 { "+%%%+%%%", | 322 {"%E4%BD%A0+%E5%A5%BD", "\xE4\xBD\xA0+\xE5\xA5\xBD", |
| 325 "+%%%+%%%", | 323 "\xE4\xBD\xA0 \xE5\xA5\xBD", L"\x4f60+\x597d"}, |
| 326 " %%% %%%", | 324 {"%ED%ED", // Invalid UTF-8. |
| 327 L"+%%%+%%%"}, | 325 "\xED\xED", "\xED\xED", L"%ED%ED"}, // Invalid UTF-8 -> kept unescaped. |
| 328 { "Don't escape anything", | |
| 329 "Don't escape anything", | |
| 330 "Don't escape anything", | |
| 331 L"Don't escape anything"}, | |
| 332 { "+Invalid %escape %2+", | |
| 333 "+Invalid %escape %2+", | |
| 334 " Invalid %escape %2 ", | |
| 335 L"+Invalid %escape %2+"}, | |
| 336 { "Some random text %25%2dOK", | |
| 337 "Some random text %25-OK", | |
| 338 "Some random text %25-OK", | |
| 339 L"Some random text %25-OK"}, | |
| 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 }; | 326 }; |
| 353 | 327 |
| 354 for (size_t i = 0; i < arraysize(unescape_cases); i++) { | 328 for (size_t i = 0; i < arraysize(unescape_cases); i++) { |
| 355 std::string unescaped = UnescapeURLComponent(unescape_cases[i].input, | 329 std::string unescaped = |
| 356 UnescapeRule::NORMAL); | 330 UnescapeURLComponent(unescape_cases[i].input, UnescapeRule::NORMAL); |
| 357 EXPECT_EQ(std::string(unescape_cases[i].url_unescaped), unescaped); | 331 EXPECT_EQ(std::string(unescape_cases[i].url_unescaped), unescaped); |
| 358 | 332 |
| 359 unescaped = UnescapeURLComponent(unescape_cases[i].input, | 333 unescaped = UnescapeURLComponent(unescape_cases[i].input, |
| 360 UnescapeRule::REPLACE_PLUS_WITH_SPACE); | 334 UnescapeRule::REPLACE_PLUS_WITH_SPACE); |
| 361 EXPECT_EQ(std::string(unescape_cases[i].query_unescaped), unescaped); | 335 EXPECT_EQ(std::string(unescape_cases[i].query_unescaped), unescaped); |
| 362 | 336 |
| 363 // TODO: Need to test unescape_spaces and unescape_percent. | 337 // TODO: Need to test unescape_spaces and unescape_percent. |
| 364 base::string16 decoded = UnescapeAndDecodeUTF8URLComponent( | 338 base::string16 decoded = UnescapeAndDecodeUTF8URLComponent( |
| 365 unescape_cases[i].input, UnescapeRule::NORMAL); | 339 unescape_cases[i].input, UnescapeRule::NORMAL); |
| 366 EXPECT_EQ(base::WideToUTF16(unescape_cases[i].decoded), decoded); | 340 EXPECT_EQ(base::WideToUTF16(unescape_cases[i].decoded), decoded); |
| 367 } | 341 } |
| 368 } | 342 } |
| 369 | 343 |
| 370 TEST(EscapeTest, AdjustOffset) { | 344 TEST(EscapeTest, AdjustOffset) { |
| 371 const AdjustOffsetCase adjust_cases[] = { | 345 const AdjustOffsetCase adjust_cases[] = { |
| 372 {"", 0, 0}, | 346 {"", 0, 0}, |
| 373 {"test", 0, 0}, | 347 {"test", 0, 0}, |
| 374 {"test", 2, 2}, | 348 {"test", 2, 2}, |
| 375 {"test", 4, 4}, | 349 {"test", 4, 4}, |
| 376 {"test", std::string::npos, std::string::npos}, | 350 {"test", std::string::npos, std::string::npos}, |
| 377 {"%2dtest", 6, 4}, | 351 {"%2dtest", 6, 4}, |
| 378 {"%2dtest", 3, 1}, | 352 {"%2dtest", 3, 1}, |
| 379 {"%2dtest", 2, std::string::npos}, | 353 {"%2dtest", 2, std::string::npos}, |
| 380 {"%2dtest", 1, std::string::npos}, | 354 {"%2dtest", 1, std::string::npos}, |
| 381 {"%2dtest", 0, 0}, | 355 {"%2dtest", 0, 0}, |
| 382 {"test%2d", 2, 2}, | 356 {"test%2d", 2, 2}, |
| 383 {"%E4%BD%A0+%E5%A5%BD", 9, 1}, | 357 {"%E4%BD%A0+%E5%A5%BD", 9, 1}, |
| 384 {"%E4%BD%A0+%E5%A5%BD", 6, std::string::npos}, | 358 {"%E4%BD%A0+%E5%A5%BD", 6, std::string::npos}, |
| 385 {"%E4%BD%A0+%E5%A5%BD", 0, 0}, | 359 {"%E4%BD%A0+%E5%A5%BD", 0, 0}, |
| 386 {"%E4%BD%A0+%E5%A5%BD", 10, 2}, | 360 {"%E4%BD%A0+%E5%A5%BD", 10, 2}, |
| 387 {"%E4%BD%A0+%E5%A5%BD", 19, 3}, | 361 {"%E4%BD%A0+%E5%A5%BD", 19, 3}, |
| 388 | 362 {"hi%41test%E4%BD%A0+%E5%A5%BD", 18, 8}, |
| 389 {"hi%41test%E4%BD%A0+%E5%A5%BD", 18, 8}, | 363 {"hi%41test%E4%BD%A0+%E5%A5%BD", 15, std::string::npos}, |
| 390 {"hi%41test%E4%BD%A0+%E5%A5%BD", 15, std::string::npos}, | 364 {"hi%41test%E4%BD%A0+%E5%A5%BD", 9, 7}, |
| 391 {"hi%41test%E4%BD%A0+%E5%A5%BD", 9, 7}, | 365 {"hi%41test%E4%BD%A0+%E5%A5%BD", 19, 9}, |
| 392 {"hi%41test%E4%BD%A0+%E5%A5%BD", 19, 9}, | 366 {"hi%41test%E4%BD%A0+%E5%A5%BD", 28, 10}, |
| 393 {"hi%41test%E4%BD%A0+%E5%A5%BD", 28, 10}, | 367 {"hi%41test%E4%BD%A0+%E5%A5%BD", 0, 0}, |
| 394 {"hi%41test%E4%BD%A0+%E5%A5%BD", 0, 0}, | 368 {"hi%41test%E4%BD%A0+%E5%A5%BD", 2, 2}, |
| 395 {"hi%41test%E4%BD%A0+%E5%A5%BD", 2, 2}, | 369 {"hi%41test%E4%BD%A0+%E5%A5%BD", 3, std::string::npos}, |
| 396 {"hi%41test%E4%BD%A0+%E5%A5%BD", 3, std::string::npos}, | 370 {"hi%41test%E4%BD%A0+%E5%A5%BD", 5, 3}, |
| 397 {"hi%41test%E4%BD%A0+%E5%A5%BD", 5, 3}, | 371 {"%E4%BD%A0+%E5%A5%BDhi%41test", 9, 1}, |
| 398 | 372 {"%E4%BD%A0+%E5%A5%BDhi%41test", 6, std::string::npos}, |
| 399 {"%E4%BD%A0+%E5%A5%BDhi%41test", 9, 1}, | 373 {"%E4%BD%A0+%E5%A5%BDhi%41test", 0, 0}, |
| 400 {"%E4%BD%A0+%E5%A5%BDhi%41test", 6, std::string::npos}, | 374 {"%E4%BD%A0+%E5%A5%BDhi%41test", 10, 2}, |
| 401 {"%E4%BD%A0+%E5%A5%BDhi%41test", 0, 0}, | 375 {"%E4%BD%A0+%E5%A5%BDhi%41test", 19, 3}, |
| 402 {"%E4%BD%A0+%E5%A5%BDhi%41test", 10, 2}, | 376 {"%E4%BD%A0+%E5%A5%BDhi%41test", 21, 5}, |
| 403 {"%E4%BD%A0+%E5%A5%BDhi%41test", 19, 3}, | 377 {"%E4%BD%A0+%E5%A5%BDhi%41test", 22, std::string::npos}, |
| 404 {"%E4%BD%A0+%E5%A5%BDhi%41test", 21, 5}, | 378 {"%E4%BD%A0+%E5%A5%BDhi%41test", 24, 6}, |
| 405 {"%E4%BD%A0+%E5%A5%BDhi%41test", 22, std::string::npos}, | 379 {"%E4%BD%A0+%E5%A5%BDhi%41test", 28, 10}, |
| 406 {"%E4%BD%A0+%E5%A5%BDhi%41test", 24, 6}, | 380 {"%ED%B0%80+%E5%A5%BD", 6, 6}, // not convertable to UTF-8 |
| 407 {"%E4%BD%A0+%E5%A5%BDhi%41test", 28, 10}, | |
| 408 | |
| 409 {"%ED%B0%80+%E5%A5%BD", 6, 6}, // not convertable to UTF-8 | |
| 410 }; | 381 }; |
| 411 | 382 |
| 412 for (size_t i = 0; i < arraysize(adjust_cases); i++) { | 383 for (size_t i = 0; i < arraysize(adjust_cases); i++) { |
| 413 size_t offset = adjust_cases[i].input_offset; | 384 size_t offset = adjust_cases[i].input_offset; |
| 414 base::OffsetAdjuster::Adjustments adjustments; | 385 base::OffsetAdjuster::Adjustments adjustments; |
| 415 UnescapeAndDecodeUTF8URLComponentWithAdjustments( | 386 UnescapeAndDecodeUTF8URLComponentWithAdjustments( |
| 416 adjust_cases[i].input, UnescapeRule::NORMAL, &adjustments); | 387 adjust_cases[i].input, UnescapeRule::NORMAL, &adjustments); |
| 417 base::OffsetAdjuster::AdjustOffset(adjustments, &offset); | 388 base::OffsetAdjuster::AdjustOffset(adjustments, &offset); |
| 418 EXPECT_EQ(adjust_cases[i].output_offset, offset) | 389 EXPECT_EQ(adjust_cases[i].output_offset, offset) |
| 419 << "input=" << adjust_cases[i].input | 390 << "input=" << adjust_cases[i].input |
| 420 << " offset=" << adjust_cases[i].input_offset; | 391 << " offset=" << adjust_cases[i].input_offset; |
| 421 } | 392 } |
| 422 } | 393 } |
| 423 | 394 |
| 424 TEST(EscapeTest, EscapeForHTML) { | 395 TEST(EscapeTest, EscapeForHTML) { |
| 425 const EscapeForHTMLCase tests[] = { | 396 const EscapeForHTMLCase tests[] = { |
| 426 { "hello", "hello" }, | 397 {"hello", "hello"}, |
| 427 { "<hello>", "<hello>" }, | 398 {"<hello>", "<hello>"}, |
| 428 { "don\'t mess with me", "don't mess with me" }, | 399 {"don\'t mess with me", "don't mess with me"}, |
| 429 }; | 400 }; |
| 430 for (size_t i = 0; i < arraysize(tests); ++i) { | 401 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 431 std::string result = EscapeForHTML(std::string(tests[i].input)); | 402 std::string result = EscapeForHTML(std::string(tests[i].input)); |
| 432 EXPECT_EQ(std::string(tests[i].expected_output), result); | 403 EXPECT_EQ(std::string(tests[i].expected_output), result); |
| 433 } | 404 } |
| 434 } | 405 } |
| 435 | 406 |
| 436 TEST(EscapeTest, UnescapeForHTML) { | 407 TEST(EscapeTest, UnescapeForHTML) { |
| 437 const EscapeForHTMLCase tests[] = { | 408 const EscapeForHTMLCase tests[] = { |
| 438 { "", "" }, | 409 {"", ""}, |
| 439 { "<hello>", "<hello>" }, | 410 {"<hello>", "<hello>"}, |
| 440 { "don't mess with me", "don\'t mess with me" }, | 411 {"don't mess with me", "don\'t mess with me"}, |
| 441 { "<>&"'", "<>&\"'" }, | 412 {"<>&"'", "<>&\"'"}, |
| 442 { "& lt; & ; &; '", "& lt; & ; &; '" }, | 413 {"& lt; & ; &; '", "& lt; & ; &; '"}, |
| 443 { "&", "&" }, | 414 {"&", "&"}, |
| 444 { """, "\"" }, | 415 {""", "\""}, |
| 445 { "'", "'" }, | 416 {"'", "'"}, |
| 446 { "<", "<" }, | 417 {"<", "<"}, |
| 447 { ">", ">" }, | 418 {">", ">"}, |
| 448 { "& &", "& &" }, | 419 {"& &", "& &"}, |
| 449 }; | 420 }; |
| 450 for (size_t i = 0; i < arraysize(tests); ++i) { | 421 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 451 base::string16 result = UnescapeForHTML(base::ASCIIToUTF16(tests[i].input)); | 422 base::string16 result = UnescapeForHTML(base::ASCIIToUTF16(tests[i].input)); |
| 452 EXPECT_EQ(base::ASCIIToUTF16(tests[i].expected_output), result); | 423 EXPECT_EQ(base::ASCIIToUTF16(tests[i].expected_output), result); |
| 453 } | 424 } |
| 454 } | 425 } |
| 455 | 426 |
| 456 | |
| 457 } // namespace | 427 } // namespace |
| 458 } // namespace net | 428 } // namespace net |
| OLD | NEW |