OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "base/json/string_escape.h" | 5 #include "base/json/string_escape.h" |
6 #include "base/utf_string_conversions.h" | 6 #include "base/utf_string_conversions.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 | 8 |
9 namespace base { | 9 namespace base { |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
13 const struct json_narrow_test_data { | 13 const struct json_narrow_test_data { |
14 const char* to_escape; | 14 const char* to_escape; |
15 const char* escaped; | 15 const char* escaped; |
16 } json_narrow_cases[] = { | 16 } json_narrow_cases[] = { |
17 {"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, | 17 {"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, |
18 {"a\b\f\n\r\t\v\1\\.\"z", | 18 {"a\b\f\n\r\t\v\1\\.\"z", |
19 "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"}, | 19 "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"}, |
20 {"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"}, | 20 {"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"}, |
| 21 {"c<>d", "c\\u003C\\u003Ed"}, |
21 }; | 22 }; |
22 | 23 |
23 } // namespace | 24 } // namespace |
24 | 25 |
25 TEST(StringEscapeTest, JsonDoubleQuoteNarrow) { | 26 TEST(StringEscapeTest, JsonDoubleQuoteNarrow) { |
26 for (size_t i = 0; i < arraysize(json_narrow_cases); ++i) { | 27 for (size_t i = 0; i < arraysize(json_narrow_cases); ++i) { |
27 std::string in = json_narrow_cases[i].to_escape; | 28 std::string in = json_narrow_cases[i].to_escape; |
28 std::string out; | 29 std::string out; |
29 JsonDoubleQuote(in, false, &out); | 30 JsonDoubleQuote(in, false, &out); |
30 EXPECT_EQ(std::string(json_narrow_cases[i].escaped), out); | 31 EXPECT_EQ(std::string(json_narrow_cases[i].escaped), out); |
(...skipping 24 matching lines...) Expand all Loading... |
55 | 56 |
56 const struct json_wide_test_data { | 57 const struct json_wide_test_data { |
57 const wchar_t* to_escape; | 58 const wchar_t* to_escape; |
58 const char* escaped; | 59 const char* escaped; |
59 } json_wide_cases[] = { | 60 } json_wide_cases[] = { |
60 {L"b\uffb1\u00ff", "b\\uFFB1\\u00FF"}, | 61 {L"b\uffb1\u00ff", "b\\uFFB1\\u00FF"}, |
61 {L"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, | 62 {L"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, |
62 {L"a\b\f\n\r\t\v\1\\.\"z", | 63 {L"a\b\f\n\r\t\v\1\\.\"z", |
63 "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"}, | 64 "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"}, |
64 {L"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"}, | 65 {L"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"}, |
| 66 {L"c<>d", "c\\u003C\\u003Ed"}, |
65 }; | 67 }; |
66 | 68 |
67 } // namespace | 69 } // namespace |
68 | 70 |
69 TEST(StringEscapeTest, JsonDoubleQuoteWide) { | 71 TEST(StringEscapeTest, JsonDoubleQuoteWide) { |
70 for (size_t i = 0; i < arraysize(json_wide_cases); ++i) { | 72 for (size_t i = 0; i < arraysize(json_wide_cases); ++i) { |
71 std::string out; | 73 std::string out; |
72 string16 in = WideToUTF16(json_wide_cases[i].to_escape); | 74 string16 in = WideToUTF16(json_wide_cases[i].to_escape); |
73 JsonDoubleQuote(in, false, &out); | 75 JsonDoubleQuote(in, false, &out); |
74 EXPECT_EQ(std::string(json_wide_cases[i].escaped), out); | 76 EXPECT_EQ(std::string(json_wide_cases[i].escaped), out); |
(...skipping 14 matching lines...) Expand all Loading... |
89 null_prepend.push_back(0); | 91 null_prepend.push_back(0); |
90 in = null_prepend + in; | 92 in = null_prepend + in; |
91 std::string expected = "test\\u0000"; | 93 std::string expected = "test\\u0000"; |
92 expected += json_wide_cases[0].escaped; | 94 expected += json_wide_cases[0].escaped; |
93 out.clear(); | 95 out.clear(); |
94 JsonDoubleQuote(in, false, &out); | 96 JsonDoubleQuote(in, false, &out); |
95 EXPECT_EQ(expected, out); | 97 EXPECT_EQ(expected, out); |
96 } | 98 } |
97 | 99 |
98 } // namespace base | 100 } // namespace base |
OLD | NEW |