OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 }; | 61 }; |
62 | 62 |
63 TEST(EscapeTest, EscapeTextForFormSubmission) { | 63 TEST(EscapeTest, EscapeTextForFormSubmission) { |
64 const EscapeCase escape_cases[] = { | 64 const EscapeCase escape_cases[] = { |
65 {"foo", "foo"}, | 65 {"foo", "foo"}, |
66 {"foo bar", "foo+bar"}, | 66 {"foo bar", "foo+bar"}, |
67 {"foo++", "foo%2B%2B"} | 67 {"foo++", "foo%2B%2B"} |
68 }; | 68 }; |
69 for (size_t i = 0; i < arraysize(escape_cases); ++i) { | 69 for (size_t i = 0; i < arraysize(escape_cases); ++i) { |
70 EscapeCase value = escape_cases[i]; | 70 EscapeCase value = escape_cases[i]; |
71 EXPECT_EQ(UTF8ToUTF16(value.output), | 71 EXPECT_EQ(value.output, EscapeQueryParamValue(value.input, true)); |
72 EscapeQueryParamValueUTF8(UTF8ToUTF16(value.input), true)); | |
73 } | 72 } |
74 | 73 |
75 const EscapeCase escape_cases_no_plus[] = { | 74 const EscapeCase escape_cases_no_plus[] = { |
76 {"foo", "foo"}, | 75 {"foo", "foo"}, |
77 {"foo bar", "foo%20bar"}, | 76 {"foo bar", "foo%20bar"}, |
78 {"foo++", "foo%2B%2B"} | 77 {"foo++", "foo%2B%2B"} |
79 }; | 78 }; |
80 for (size_t i = 0; i < arraysize(escape_cases_no_plus); ++i) { | 79 for (size_t i = 0; i < arraysize(escape_cases_no_plus); ++i) { |
81 EscapeCase value = escape_cases_no_plus[i]; | 80 EscapeCase value = escape_cases_no_plus[i]; |
82 EXPECT_EQ(ASCIIToUTF16(value.output), | 81 EXPECT_EQ(value.output, EscapeQueryParamValue(value.input, false)); |
83 EscapeQueryParamValueUTF8(ASCIIToUTF16(value.input), false)); | |
84 } | 82 } |
85 | 83 |
86 // Test all the values in we're supposed to be escaping. | 84 // Test all the values in we're supposed to be escaping. |
87 const std::string no_escape( | 85 const std::string no_escape( |
88 "abcdefghijklmnopqrstuvwxyz" | 86 "abcdefghijklmnopqrstuvwxyz" |
89 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 87 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
90 "0123456789" | 88 "0123456789" |
91 "!'()*-._~"); | 89 "!'()*-._~"); |
92 for (int i = 0; i < 256; ++i) { | 90 for (int i = 0; i < 256; ++i) { |
93 std::string in; | 91 std::string in; |
94 in.push_back(i); | 92 in.push_back(i); |
95 std::string out = EscapeQueryParamValue(in, true); | 93 std::string out = EscapeQueryParamValue(in, true); |
96 if (0 == i) { | 94 if (0 == i) { |
97 EXPECT_EQ(out, std::string("%00")); | 95 EXPECT_EQ(out, std::string("%00")); |
98 } else if (32 == i) { | 96 } else if (32 == i) { |
99 // Spaces are plus escaped like web forms. | 97 // Spaces are plus escaped like web forms. |
100 EXPECT_EQ(out, std::string("+")); | 98 EXPECT_EQ(out, std::string("+")); |
101 } else if (no_escape.find(in) == std::string::npos) { | 99 } else if (no_escape.find(in) == std::string::npos) { |
102 // Check %hex escaping | 100 // Check %hex escaping |
103 std::string expected = base::StringPrintf("%%%02X", i); | 101 std::string expected = base::StringPrintf("%%%02X", i); |
104 EXPECT_EQ(expected, out); | 102 EXPECT_EQ(expected, out); |
105 } else { | 103 } else { |
106 // No change for things in the no_escape list. | 104 // No change for things in the no_escape list. |
107 EXPECT_EQ(out, in); | 105 EXPECT_EQ(out, in); |
108 } | 106 } |
109 } | 107 } |
110 | |
111 // Check to see if EscapeQueryParamValueUTF8 is the same as | |
112 // EscapeQueryParamValue(..., kCodepageUTF8,) | |
113 string16 test_str; | |
114 test_str.reserve(5000); | |
115 for (int i = 1; i < 5000; ++i) { | |
116 test_str.push_back(i); | |
117 } | |
118 string16 utf16; | |
119 EXPECT_TRUE(EscapeQueryParamValue(test_str, base::kCodepageUTF8, true, | |
120 &utf16)); | |
121 EXPECT_EQ(utf16, EscapeQueryParamValueUTF8(test_str, true)); | |
122 EXPECT_TRUE(EscapeQueryParamValue(test_str, base::kCodepageUTF8, false, | |
123 &utf16)); | |
124 EXPECT_EQ(utf16, EscapeQueryParamValueUTF8(test_str, false)); | |
125 } | 108 } |
126 | 109 |
127 TEST(EscapeTest, EscapePath) { | 110 TEST(EscapeTest, EscapePath) { |
128 ASSERT_EQ( | 111 ASSERT_EQ( |
129 // Most of the character space we care about, un-escaped | 112 // Most of the character space we care about, un-escaped |
130 EscapePath( | 113 EscapePath( |
131 "\x02\n\x1d !\"#$%&'()*+,-./0123456789:;" | 114 "\x02\n\x1d !\"#$%&'()*+,-./0123456789:;" |
132 "<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 115 "<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
133 "[\\]^_`abcdefghijklmnopqrstuvwxyz" | 116 "[\\]^_`abcdefghijklmnopqrstuvwxyz" |
134 "{|}~\x7f\x80\xff"), | 117 "{|}~\x7f\x80\xff"), |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 internal::AdjustEncodingOffset(adjustments)); | 421 internal::AdjustEncodingOffset(adjustments)); |
439 size_t expected_2[] = {0, kNpos, kNpos, 1, 2, 3, 4, kNpos, kNpos, 5, kNpos, | 422 size_t expected_2[] = {0, kNpos, kNpos, 1, 2, 3, 4, kNpos, kNpos, 5, kNpos, |
440 kNpos, 6, 7, 8, 9, kNpos, kNpos}; | 423 kNpos, 6, 7, 8, 9, kNpos, kNpos}; |
441 EXPECT_EQ(offsets.size(), arraysize(expected_2)); | 424 EXPECT_EQ(offsets.size(), arraysize(expected_2)); |
442 for (size_t i = 0; i < arraysize(expected_2); ++i) | 425 for (size_t i = 0; i < arraysize(expected_2); ++i) |
443 EXPECT_EQ(expected_2[i], offsets[i]); | 426 EXPECT_EQ(expected_2[i], offsets[i]); |
444 } | 427 } |
445 | 428 |
446 } // namespace | 429 } // namespace |
447 } // namespace net | 430 } // namespace net |
OLD | NEW |