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