| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "net/base/escape.h" | 7 #include "net/base/escape.h" |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/i18n/icu_string_conversions.h" | 10 #include "base/i18n/icu_string_conversions.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 } // namespace | 51 } // namespace |
| 52 | 52 |
| 53 TEST(EscapeTest, EscapeTextForFormSubmission) { | 53 TEST(EscapeTest, EscapeTextForFormSubmission) { |
| 54 const EscapeCase escape_cases[] = { | 54 const EscapeCase escape_cases[] = { |
| 55 {L"foo", L"foo"}, | 55 {L"foo", L"foo"}, |
| 56 {L"foo bar", L"foo+bar"}, | 56 {L"foo bar", L"foo+bar"}, |
| 57 {L"foo++", L"foo%2B%2B"} | 57 {L"foo++", L"foo%2B%2B"} |
| 58 }; | 58 }; |
| 59 for (size_t i = 0; i < arraysize(escape_cases); ++i) { | 59 for (size_t i = 0; i < arraysize(escape_cases); ++i) { |
| 60 EscapeCase value = escape_cases[i]; | 60 EscapeCase value = escape_cases[i]; |
| 61 EXPECT_EQ(value.output, EscapeQueryParamValueUTF8(value.input)); | 61 EXPECT_EQ(value.output, EscapeQueryParamValueUTF8(value.input, true)); |
| 62 } |
| 63 |
| 64 const EscapeCase escape_cases_no_plus[] = { |
| 65 {L"foo", L"foo"}, |
| 66 {L"foo bar", L"foo%20bar"}, |
| 67 {L"foo++", L"foo%2B%2B"} |
| 68 }; |
| 69 for (size_t i = 0; i < arraysize(escape_cases_no_plus); ++i) { |
| 70 EscapeCase value = escape_cases_no_plus[i]; |
| 71 EXPECT_EQ(value.output, EscapeQueryParamValueUTF8(value.input, false)); |
| 62 } | 72 } |
| 63 | 73 |
| 64 // Test all the values in we're supposed to be escaping. | 74 // Test all the values in we're supposed to be escaping. |
| 65 const std::string no_escape( | 75 const std::string no_escape( |
| 66 "abcdefghijklmnopqrstuvwxyz" | 76 "abcdefghijklmnopqrstuvwxyz" |
| 67 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 77 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 68 "0123456789" | 78 "0123456789" |
| 69 "!'()*-._~"); | 79 "!'()*-._~"); |
| 70 for (int i = 0; i < 256; ++i) { | 80 for (int i = 0; i < 256; ++i) { |
| 71 std::string in; | 81 std::string in; |
| 72 in.push_back(i); | 82 in.push_back(i); |
| 73 std::string out = EscapeQueryParamValue(in); | 83 std::string out = EscapeQueryParamValue(in, true); |
| 74 if (0 == i) { | 84 if (0 == i) { |
| 75 EXPECT_EQ(out, std::string("%00")); | 85 EXPECT_EQ(out, std::string("%00")); |
| 76 } else if (32 == i) { | 86 } else if (32 == i) { |
| 77 // Spaces are plus escaped like web forms. | 87 // Spaces are plus escaped like web forms. |
| 78 EXPECT_EQ(out, std::string("+")); | 88 EXPECT_EQ(out, std::string("+")); |
| 79 } else if (no_escape.find(in) == std::string::npos) { | 89 } else if (no_escape.find(in) == std::string::npos) { |
| 80 // Check %hex escaping | 90 // Check %hex escaping |
| 81 std::string expected = StringPrintf("%%%02X", i); | 91 std::string expected = StringPrintf("%%%02X", i); |
| 82 EXPECT_EQ(expected, out); | 92 EXPECT_EQ(expected, out); |
| 83 } else { | 93 } else { |
| 84 // No change for things in the no_escape list. | 94 // No change for things in the no_escape list. |
| 85 EXPECT_EQ(out, in); | 95 EXPECT_EQ(out, in); |
| 86 } | 96 } |
| 87 } | 97 } |
| 88 | 98 |
| 89 // Check to see if EscapeQueryParamValueUTF8 is the same as | 99 // Check to see if EscapeQueryParamValueUTF8 is the same as |
| 90 // EscapeQueryParamValue(..., kCodepageUTF8,) | 100 // EscapeQueryParamValue(..., kCodepageUTF8,) |
| 91 string16 test_str; | 101 string16 test_str; |
| 92 test_str.reserve(5000); | 102 test_str.reserve(5000); |
| 93 for (int i = 1; i < 5000; ++i) { | 103 for (int i = 1; i < 5000; ++i) { |
| 94 test_str.push_back(i); | 104 test_str.push_back(i); |
| 95 } | 105 } |
| 96 string16 wide; | 106 string16 wide; |
| 97 EXPECT_TRUE(EscapeQueryParamValue(test_str, base::kCodepageUTF8, &wide)); | 107 EXPECT_TRUE(EscapeQueryParamValue(test_str, base::kCodepageUTF8, true, |
| 108 &wide)); |
| 98 EXPECT_EQ(UTF16ToWideHack(wide), | 109 EXPECT_EQ(UTF16ToWideHack(wide), |
| 99 EscapeQueryParamValueUTF8(UTF16ToWideHack(test_str))); | 110 EscapeQueryParamValueUTF8(UTF16ToWideHack(test_str), true)); |
| 111 EXPECT_TRUE(EscapeQueryParamValue(test_str, base::kCodepageUTF8, false, |
| 112 &wide)); |
| 113 EXPECT_EQ(UTF16ToWideHack(wide), |
| 114 EscapeQueryParamValueUTF8(UTF16ToWideHack(test_str), false)); |
| 100 } | 115 } |
| 101 | 116 |
| 102 TEST(EscapeTest, EscapePath) { | 117 TEST(EscapeTest, EscapePath) { |
| 103 ASSERT_EQ( | 118 ASSERT_EQ( |
| 104 // Most of the character space we care about, un-escaped | 119 // Most of the character space we care about, un-escaped |
| 105 EscapePath( | 120 EscapePath( |
| 106 "\x02\n\x1d !\"#$%&'()*+,-./0123456789:;" | 121 "\x02\n\x1d !\"#$%&'()*+,-./0123456789:;" |
| 107 "<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 122 "<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 108 "[\\]^_`abcdefghijklmnopqrstuvwxyz" | 123 "[\\]^_`abcdefghijklmnopqrstuvwxyz" |
| 109 "{|}~\x7f\x80\xff"), | 124 "{|}~\x7f\x80\xff"), |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 const EscapeForHTMLCase tests[] = { | 293 const EscapeForHTMLCase tests[] = { |
| 279 { "hello", "hello" }, | 294 { "hello", "hello" }, |
| 280 { "<hello>", "<hello>" }, | 295 { "<hello>", "<hello>" }, |
| 281 { "don\'t mess with me", "don't mess with me" }, | 296 { "don\'t mess with me", "don't mess with me" }, |
| 282 }; | 297 }; |
| 283 for (size_t i = 0; i < arraysize(tests); ++i) { | 298 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 284 std::string result = EscapeForHTML(std::string(tests[i].input)); | 299 std::string result = EscapeForHTML(std::string(tests[i].input)); |
| 285 EXPECT_EQ(std::string(tests[i].expected_output), result); | 300 EXPECT_EQ(std::string(tests[i].expected_output), result); |
| 286 } | 301 } |
| 287 } | 302 } |
| OLD | NEW |