summaryrefslogtreecommitdiffstats
path: root/base/json/string_escape_unittest.cc
diff options
context:
space:
mode:
authorantrim@chromium.org <antrim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-12-11 11:52:22 +0000
committerantrim@chromium.org <antrim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-12-11 11:52:22 +0000
commit11b366da9588a954343196434e00822aa3cf9b93 (patch)
tree0c8d3d6b521be4308c4f979ad19e1526ca65bc8e /base/json/string_escape_unittest.cc
parent3fa8fe590f29d504ce1284d5b4814c64861b4ab7 (diff)
downloadchromium_src-11b366da9588a954343196434e00822aa3cf9b93.zip
chromium_src-11b366da9588a954343196434e00822aa3cf9b93.tar.gz
chromium_src-11b366da9588a954343196434e00822aa3cf9b93.tar.bz2
Revert of https://codereview.chromium.org/100823007/
Reason for revert: This patchset breaks at least displayment of Russian localized strings on Chromeos login screen. TBR=mark@chromium.org,jshin@chromium.org,thakis@chromium.org,asanka@chromium.org,zea@chromium.org,bauerb@chromium.org,rsesek@chromium.org NOTREECHECKS=true NOTRY=true Review URL: https://codereview.chromium.org/106793004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@240082 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/json/string_escape_unittest.cc')
-rw-r--r--base/json/string_escape_unittest.cc184
1 files changed, 53 insertions, 131 deletions
diff --git a/base/json/string_escape_unittest.cc b/base/json/string_escape_unittest.cc
index 7d82f9b..f921994 100644
--- a/base/json/string_escape_unittest.cc
+++ b/base/json/string_escape_unittest.cc
@@ -1,182 +1,104 @@
-// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/json/string_escape.h"
-
-#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
-TEST(JSONStringEscapeTest, EscapeUTF8) {
- const struct {
- const char* to_escape;
- const char* escaped;
- } cases[] = {
- {"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"},
- {"a\b\f\n\r\t\v\1\\.\"z",
- "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"},
- {"b\x0f\x7f\xf0\xff!", // \xf0\xff is not a valid UTF-8 unit.
- "b\\u000F\x7F\xEF\xBF\xBD\xEF\xBF\xBD!"},
- {"c<>d", "c\\u003C>d"},
- };
-
- for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
- const char* in_ptr = cases[i].to_escape;
- std::string in_str = in_ptr;
+namespace {
- std::string out;
- EscapeJSONString(in_ptr, false, &out);
- EXPECT_EQ(std::string(cases[i].escaped), out);
- EXPECT_TRUE(IsStringUTF8(out));
+const struct json_narrow_test_data {
+ const char* to_escape;
+ const char* escaped;
+} json_narrow_cases[] = {
+ {"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"},
+ {"a\b\f\n\r\t\v\1\\.\"z",
+ "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"},
+ {"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"},
+ {"c<>d", "c\\u003C\\u003Ed"},
+};
+} // namespace
+
+TEST(StringEscapeTest, JsonDoubleQuoteNarrow) {
+ for (size_t i = 0; i < arraysize(json_narrow_cases); ++i) {
+ const char* in_ptr = json_narrow_cases[i].to_escape;
+ std::string in_str = in_ptr;
+ std::string out;
+ JsonDoubleQuote(in_ptr, false, &out);
+ EXPECT_EQ(std::string(json_narrow_cases[i].escaped), out);
out.erase();
- bool convert_ok = EscapeJSONString(in_str, false, &out);
- EXPECT_EQ(std::string(cases[i].escaped), out);
- EXPECT_TRUE(IsStringUTF8(out));
-
- if (convert_ok) {
- std::string fooout = GetQuotedJSONString(in_str);
- EXPECT_EQ("\"" + std::string(cases[i].escaped) + "\"", fooout);
- EXPECT_TRUE(IsStringUTF8(out));
- }
+ JsonDoubleQuote(in_str, false, &out);
+ EXPECT_EQ(std::string(json_narrow_cases[i].escaped), out);
}
- std::string in = cases[0].to_escape;
+ std::string in = json_narrow_cases[0].to_escape;
std::string out;
- EscapeJSONString(in, false, &out);
- EXPECT_TRUE(IsStringUTF8(out));
+ JsonDoubleQuote(in, false, &out);
// test quoting
std::string out_quoted;
- EscapeJSONString(in, true, &out_quoted);
+ JsonDoubleQuote(in, true, &out_quoted);
EXPECT_EQ(out.length() + 2, out_quoted.length());
EXPECT_EQ(out_quoted.find(out), 1U);
- EXPECT_TRUE(IsStringUTF8(out_quoted));
// now try with a NULL in the string
std::string null_prepend = "test";
null_prepend.push_back(0);
in = null_prepend + in;
std::string expected = "test\\u0000";
- expected += cases[0].escaped;
+ expected += json_narrow_cases[0].escaped;
out.clear();
- EscapeJSONString(in, false, &out);
+ JsonDoubleQuote(in, false, &out);
EXPECT_EQ(expected, out);
- EXPECT_TRUE(IsStringUTF8(out));
}
-TEST(JSONStringEscapeTest, EscapeUTF16) {
- const struct {
- const wchar_t* to_escape;
- const char* escaped;
- } cases[] = {
- {L"b\uffb1\u00ff", "b\xEF\xBE\xB1\xC3\xBF"},
- {L"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"},
- {L"a\b\f\n\r\t\v\1\\.\"z",
- "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"},
- {L"b\x0f\x7f\xf0\xff!", "b\\u000F\x7F\xC3\xB0\xC3\xBF!"},
- {L"c<>d", "c\\u003C>d"},
- };
-
- for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
- string16 in = WideToUTF16(cases[i].to_escape);
+namespace {
- std::string out;
- EscapeJSONString(in, false, &out);
- EXPECT_EQ(std::string(cases[i].escaped), out);
- EXPECT_TRUE(IsStringUTF8(out));
+const struct json_wide_test_data {
+ const wchar_t* to_escape;
+ const char* escaped;
+} json_wide_cases[] = {
+ {L"b\uffb1\u00ff", "b\\uFFB1\\u00FF"},
+ {L"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"},
+ {L"a\b\f\n\r\t\v\1\\.\"z",
+ "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"},
+ {L"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"},
+ {L"c<>d", "c\\u003C\\u003Ed"},
+};
- out = GetQuotedJSONString(in);
- EXPECT_EQ("\"" + std::string(cases[i].escaped) + "\"", out);
- EXPECT_TRUE(IsStringUTF8(out));
+} // namespace
+
+TEST(StringEscapeTest, JsonDoubleQuoteWide) {
+ for (size_t i = 0; i < arraysize(json_wide_cases); ++i) {
+ std::string out;
+ string16 in = WideToUTF16(json_wide_cases[i].to_escape);
+ JsonDoubleQuote(in, false, &out);
+ EXPECT_EQ(std::string(json_wide_cases[i].escaped), out);
}
- string16 in = WideToUTF16(cases[0].to_escape);
+ string16 in = WideToUTF16(json_wide_cases[0].to_escape);
std::string out;
- EscapeJSONString(in, false, &out);
- EXPECT_TRUE(IsStringUTF8(out));
+ JsonDoubleQuote(in, false, &out);
// test quoting
std::string out_quoted;
- EscapeJSONString(in, true, &out_quoted);
+ JsonDoubleQuote(in, true, &out_quoted);
EXPECT_EQ(out.length() + 2, out_quoted.length());
EXPECT_EQ(out_quoted.find(out), 1U);
- EXPECT_TRUE(IsStringUTF8(out));
// now try with a NULL in the string
string16 null_prepend = WideToUTF16(L"test");
null_prepend.push_back(0);
in = null_prepend + in;
std::string expected = "test\\u0000";
- expected += cases[0].escaped;
+ expected += json_wide_cases[0].escaped;
out.clear();
- EscapeJSONString(in, false, &out);
+ JsonDoubleQuote(in, false, &out);
EXPECT_EQ(expected, out);
- EXPECT_TRUE(IsStringUTF8(out));
-}
-
-TEST(JSONStringEscapeTest, EscapeUTF16OutsideBMP) {
- {
- // {a, U+10300, !}, SMP.
- string16 test;
- test.push_back('a');
- test.push_back(0xD800);
- test.push_back(0xDF00);
- test.push_back('!');
- std::string actual;
- EXPECT_TRUE(EscapeJSONString(test, false, &actual));
- EXPECT_EQ("a\xF0\x90\x8C\x80!", actual);
- }
- {
- // {U+20021, U+2002B}, SIP.
- string16 test;
- test.push_back(0xD840);
- test.push_back(0xDC21);
- test.push_back(0xD840);
- test.push_back(0xDC2B);
- std::string actual;
- EXPECT_TRUE(EscapeJSONString(test, false, &actual));
- EXPECT_EQ("\xF0\xA0\x80\xA1\xF0\xA0\x80\xAB", actual);
- }
- {
- // {?, U+D800, @}, lone surrogate.
- string16 test;
- test.push_back('?');
- test.push_back(0xD800);
- test.push_back('@');
- std::string actual;
- EXPECT_FALSE(EscapeJSONString(test, false, &actual));
- EXPECT_EQ("?\xEF\xBF\xBD@", actual);
- }
-}
-
-TEST(JSONStringEscapeTest, EscapeBytes) {
- const struct {
- const char* to_escape;
- const char* escaped;
- } cases[] = {
- {"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"},
- {"\xe5\xc4\x4f\x05\xb6\xfd\0", "\\u00E5\\u00C4O\\u0005\\u00B6\\u00FD"},
- };
-
- for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
- std::string in = std::string(cases[i].to_escape);
- EXPECT_FALSE(IsStringUTF8(in));
-
- EXPECT_EQ(std::string(cases[i].escaped),
- EscapeBytesAsInvalidJSONString(in, false));
- EXPECT_EQ("\"" + std::string(cases[i].escaped) + "\"",
- EscapeBytesAsInvalidJSONString(in, true));
- }
-
- const char kEmbedNull[] = { '\xab', '\x39', '\0', '\x9f', '\xab' };
- std::string in(kEmbedNull, ARRAYSIZE_UNSAFE(kEmbedNull));
- EXPECT_FALSE(IsStringUTF8(in));
- EXPECT_EQ(std::string("\\u00AB9\\u0000\\u009F\\u00AB"),
- EscapeBytesAsInvalidJSONString(in, false));
}
} // namespace base