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" |
| 6 #include "base/string_util.h" |
5 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
6 #include "base/string_escape.h" | 8 |
7 #include "base/string_util.h" | 9 namespace base { |
8 | 10 |
9 namespace { | 11 namespace { |
10 | 12 |
11 const struct json_narrow_test_data { | 13 const struct json_narrow_test_data { |
12 const char* to_escape; | 14 const char* to_escape; |
13 const char* escaped; | 15 const char* escaped; |
14 } json_narrow_cases[] = { | 16 } json_narrow_cases[] = { |
15 {"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, | 17 {"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, |
16 {"a\b\f\n\r\t\v\1\\.\"z", | 18 {"a\b\f\n\r\t\v\1\\.\"z", |
17 "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"}, | 19 "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"}, |
18 {"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"}, | 20 {"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"}, |
19 }; | 21 }; |
20 | 22 |
21 } | 23 } |
22 | 24 |
23 TEST(StringEscapeTest, JsonDoubleQuoteNarrow) { | 25 TEST(StringEscapeTest, JsonDoubleQuoteNarrow) { |
24 for (size_t i = 0; i < arraysize(json_narrow_cases); ++i) { | 26 for (size_t i = 0; i < arraysize(json_narrow_cases); ++i) { |
25 std::string in = json_narrow_cases[i].to_escape; | 27 std::string in = json_narrow_cases[i].to_escape; |
26 std::string out; | 28 std::string out; |
27 string_escape::JsonDoubleQuote(in, false, &out); | 29 JsonDoubleQuote(in, false, &out); |
28 EXPECT_EQ(std::string(json_narrow_cases[i].escaped), out); | 30 EXPECT_EQ(std::string(json_narrow_cases[i].escaped), out); |
29 } | 31 } |
30 | 32 |
31 std::string in = json_narrow_cases[0].to_escape; | 33 std::string in = json_narrow_cases[0].to_escape; |
32 std::string out; | 34 std::string out; |
33 string_escape::JsonDoubleQuote(in, false, &out); | 35 JsonDoubleQuote(in, false, &out); |
34 | 36 |
35 // test quoting | 37 // test quoting |
36 std::string out_quoted; | 38 std::string out_quoted; |
37 string_escape::JsonDoubleQuote(in, true, &out_quoted); | 39 JsonDoubleQuote(in, true, &out_quoted); |
38 EXPECT_EQ(out.length() + 2, out_quoted.length()); | 40 EXPECT_EQ(out.length() + 2, out_quoted.length()); |
39 EXPECT_EQ(out_quoted.find(out), 1U); | 41 EXPECT_EQ(out_quoted.find(out), 1U); |
40 | 42 |
41 // now try with a NULL in the string | 43 // now try with a NULL in the string |
42 std::string null_prepend = "test"; | 44 std::string null_prepend = "test"; |
43 null_prepend.push_back(0); | 45 null_prepend.push_back(0); |
44 in = null_prepend + in; | 46 in = null_prepend + in; |
45 std::string expected = "test\\u0000"; | 47 std::string expected = "test\\u0000"; |
46 expected += json_narrow_cases[0].escaped; | 48 expected += json_narrow_cases[0].escaped; |
47 out.clear(); | 49 out.clear(); |
48 string_escape::JsonDoubleQuote(in, false, &out); | 50 JsonDoubleQuote(in, false, &out); |
49 EXPECT_EQ(expected, out); | 51 EXPECT_EQ(expected, out); |
50 } | 52 } |
51 | 53 |
52 namespace { | 54 namespace { |
53 | 55 |
54 const struct json_wide_test_data { | 56 const struct json_wide_test_data { |
55 const wchar_t* to_escape; | 57 const wchar_t* to_escape; |
56 const char* escaped; | 58 const char* escaped; |
57 } json_wide_cases[] = { | 59 } json_wide_cases[] = { |
58 {L"b\uffb1\u00ff", "b\\uFFB1\\u00FF"}, | 60 {L"b\uffb1\u00ff", "b\\uFFB1\\u00FF"}, |
59 {L"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, | 61 {L"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, |
60 {L"a\b\f\n\r\t\v\1\\.\"z", | 62 {L"a\b\f\n\r\t\v\1\\.\"z", |
61 "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"}, | 63 "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"}, |
62 {L"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"}, | 64 {L"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"}, |
63 }; | 65 }; |
64 | 66 |
65 } | 67 } |
66 | 68 |
67 TEST(StringEscapeTest, JsonDoubleQuoteWide) { | 69 TEST(StringEscapeTest, JsonDoubleQuoteWide) { |
68 | 70 |
69 for (size_t i = 0; i < arraysize(json_wide_cases); ++i) { | 71 for (size_t i = 0; i < arraysize(json_wide_cases); ++i) { |
70 std::string out; | 72 std::string out; |
71 string16 in = WideToUTF16(json_wide_cases[i].to_escape); | 73 string16 in = WideToUTF16(json_wide_cases[i].to_escape); |
72 string_escape::JsonDoubleQuote(in, false, &out); | 74 JsonDoubleQuote(in, false, &out); |
73 EXPECT_EQ(std::string(json_wide_cases[i].escaped), out); | 75 EXPECT_EQ(std::string(json_wide_cases[i].escaped), out); |
74 } | 76 } |
75 | 77 |
76 string16 in = WideToUTF16(json_wide_cases[0].to_escape); | 78 string16 in = WideToUTF16(json_wide_cases[0].to_escape); |
77 std::string out; | 79 std::string out; |
78 string_escape::JsonDoubleQuote(in, false, &out); | 80 JsonDoubleQuote(in, false, &out); |
79 | 81 |
80 // test quoting | 82 // test quoting |
81 std::string out_quoted; | 83 std::string out_quoted; |
82 string_escape::JsonDoubleQuote(in, true, &out_quoted); | 84 JsonDoubleQuote(in, true, &out_quoted); |
83 EXPECT_EQ(out.length() + 2, out_quoted.length()); | 85 EXPECT_EQ(out.length() + 2, out_quoted.length()); |
84 EXPECT_EQ(out_quoted.find(out), 1U); | 86 EXPECT_EQ(out_quoted.find(out), 1U); |
85 | 87 |
86 // now try with a NULL in the string | 88 // now try with a NULL in the string |
87 string16 null_prepend = WideToUTF16(L"test"); | 89 string16 null_prepend = WideToUTF16(L"test"); |
88 null_prepend.push_back(0); | 90 null_prepend.push_back(0); |
89 in = null_prepend + in; | 91 in = null_prepend + in; |
90 std::string expected = "test\\u0000"; | 92 std::string expected = "test\\u0000"; |
91 expected += json_wide_cases[0].escaped; | 93 expected += json_wide_cases[0].escaped; |
92 out.clear(); | 94 out.clear(); |
93 string_escape::JsonDoubleQuote(in, false, &out); | 95 JsonDoubleQuote(in, false, &out); |
94 EXPECT_EQ(expected, out); | 96 EXPECT_EQ(expected, out); |
95 } | 97 } |
| 98 |
| 99 } // namespace base |
OLD | NEW |