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/string_util.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 }; | 21 }; |
22 | 22 |
23 } | 23 } // namespace |
24 | 24 |
25 TEST(StringEscapeTest, JsonDoubleQuoteNarrow) { | 25 TEST(StringEscapeTest, JsonDoubleQuoteNarrow) { |
26 for (size_t i = 0; i < arraysize(json_narrow_cases); ++i) { | 26 for (size_t i = 0; i < arraysize(json_narrow_cases); ++i) { |
27 std::string in = json_narrow_cases[i].to_escape; | 27 std::string in = json_narrow_cases[i].to_escape; |
28 std::string out; | 28 std::string out; |
29 JsonDoubleQuote(in, false, &out); | 29 JsonDoubleQuote(in, false, &out); |
30 EXPECT_EQ(std::string(json_narrow_cases[i].escaped), out); | 30 EXPECT_EQ(std::string(json_narrow_cases[i].escaped), out); |
31 } | 31 } |
32 | 32 |
33 std::string in = json_narrow_cases[0].to_escape; | 33 std::string in = json_narrow_cases[0].to_escape; |
(...skipping 23 matching lines...) Expand all Loading... |
57 const wchar_t* to_escape; | 57 const wchar_t* to_escape; |
58 const char* escaped; | 58 const char* escaped; |
59 } json_wide_cases[] = { | 59 } json_wide_cases[] = { |
60 {L"b\uffb1\u00ff", "b\\uFFB1\\u00FF"}, | 60 {L"b\uffb1\u00ff", "b\\uFFB1\\u00FF"}, |
61 {L"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, | 61 {L"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, |
62 {L"a\b\f\n\r\t\v\1\\.\"z", | 62 {L"a\b\f\n\r\t\v\1\\.\"z", |
63 "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"}, | 63 "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"}, |
64 {L"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"}, | 64 {L"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"}, |
65 }; | 65 }; |
66 | 66 |
67 } | 67 } // namespace |
68 | 68 |
69 TEST(StringEscapeTest, JsonDoubleQuoteWide) { | 69 TEST(StringEscapeTest, JsonDoubleQuoteWide) { |
70 | |
71 for (size_t i = 0; i < arraysize(json_wide_cases); ++i) { | 70 for (size_t i = 0; i < arraysize(json_wide_cases); ++i) { |
72 std::string out; | 71 std::string out; |
73 string16 in = WideToUTF16(json_wide_cases[i].to_escape); | 72 string16 in = WideToUTF16(json_wide_cases[i].to_escape); |
74 JsonDoubleQuote(in, false, &out); | 73 JsonDoubleQuote(in, false, &out); |
75 EXPECT_EQ(std::string(json_wide_cases[i].escaped), out); | 74 EXPECT_EQ(std::string(json_wide_cases[i].escaped), out); |
76 } | 75 } |
77 | 76 |
78 string16 in = WideToUTF16(json_wide_cases[0].to_escape); | 77 string16 in = WideToUTF16(json_wide_cases[0].to_escape); |
79 std::string out; | 78 std::string out; |
80 JsonDoubleQuote(in, false, &out); | 79 JsonDoubleQuote(in, false, &out); |
81 | 80 |
82 // test quoting | 81 // test quoting |
83 std::string out_quoted; | 82 std::string out_quoted; |
84 JsonDoubleQuote(in, true, &out_quoted); | 83 JsonDoubleQuote(in, true, &out_quoted); |
85 EXPECT_EQ(out.length() + 2, out_quoted.length()); | 84 EXPECT_EQ(out.length() + 2, out_quoted.length()); |
86 EXPECT_EQ(out_quoted.find(out), 1U); | 85 EXPECT_EQ(out_quoted.find(out), 1U); |
87 | 86 |
88 // now try with a NULL in the string | 87 // now try with a NULL in the string |
89 string16 null_prepend = WideToUTF16(L"test"); | 88 string16 null_prepend = WideToUTF16(L"test"); |
90 null_prepend.push_back(0); | 89 null_prepend.push_back(0); |
91 in = null_prepend + in; | 90 in = null_prepend + in; |
92 std::string expected = "test\\u0000"; | 91 std::string expected = "test\\u0000"; |
93 expected += json_wide_cases[0].escaped; | 92 expected += json_wide_cases[0].escaped; |
94 out.clear(); | 93 out.clear(); |
95 JsonDoubleQuote(in, false, &out); | 94 JsonDoubleQuote(in, false, &out); |
96 EXPECT_EQ(expected, out); | 95 EXPECT_EQ(expected, out); |
97 } | 96 } |
98 | 97 |
99 } // namespace base | 98 } // namespace base |
OLD | NEW |