Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: base/string_escape_unittest.cc

Issue 113606: Add JSON-specific escaping, which has different rules from JS escaping. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/string_escape.cc ('k') | base/string_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "testing/gtest/include/gtest/gtest.h" 5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "base/string_escape.h" 6 #include "base/string_escape.h"
7 #include "base/string_util.h" 7 #include "base/string_util.h"
8 8
9 TEST(StringEscapeTest, JavascriptDoubleQuote) { 9 namespace {
10 static const char* kToEscape = "\b\001aZ\"\\wee";
11 static const char* kEscaped = "\\b\\x01aZ\\\"\\\\wee";
12 static const char* kEscapedQuoted = "\"\\b\\x01aZ\\\"\\\\wee\"";
13 static const wchar_t* kUToEscape = L"\b\x0001" L"a\x123fZ\"\\wee";
14 static const char* kUEscaped = "\\b\\x01a\\u123FZ\\\"\\\\wee";
15 static const char* kUEscapedQuoted = "\"\\b\\x01a\\u123FZ\\\"\\\\wee\"";
16 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;
17 std::string out; 32 std::string out;
33 string_escape::JsonDoubleQuote(in, false, &out);
18 34
19 // Test wide unicode escaping 35 // test quoting
20 out = "testy: "; 36 std::string out_quoted;
21 string_escape::JavascriptDoubleQuote(WideToUTF16(kUToEscape), false, &out); 37 string_escape::JsonDoubleQuote(in, true, &out_quoted);
22 ASSERT_EQ(std::string("testy: ") + kUEscaped, out); 38 EXPECT_EQ(out.length() + 2, out_quoted.length());
39 EXPECT_EQ(out_quoted.find(out), 1U);
23 40
24 out = "testy: "; 41 // now try with a NULL in the string
25 string_escape::JavascriptDoubleQuote(WideToUTF16(kUToEscape), true, &out); 42 std::string null_prepend = "test";
26 ASSERT_EQ(std::string("testy: ") + kUEscapedQuoted, out); 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 }
27 51
28 // Test null and high bit / negative unicode values 52 namespace {
29 string16 str16 = UTF8ToUTF16("TeSt");
30 str16.push_back(0);
31 str16.push_back(0xffb1);
32 str16.push_back(0x00ff);
33 53
34 out = "testy: "; 54 const struct json_wide_test_data {
35 string_escape::JavascriptDoubleQuote(str16, false, &out); 55 const wchar_t* to_escape;
36 ASSERT_EQ("testy: TeSt\\x00\\uFFB1\\xFF", out); 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 };
37 64
38 // Test escaping of 7bit ascii 65 }
39 out = "testy: ";
40 string_escape::JavascriptDoubleQuote(std::string(kToEscape), false, &out);
41 ASSERT_EQ(std::string("testy: ") + kEscaped, out);
42 66
43 out = "testy: "; 67 TEST(StringEscapeTest, JsonDoubleQuoteWide) {
44 string_escape::JavascriptDoubleQuote(std::string(kToEscape), true, &out);
45 ASSERT_EQ(std::string("testy: ") + kEscapedQuoted, out);
46 68
47 // Test null, non-printable, and non-7bit 69 for (size_t i = 0; i < arraysize(json_wide_cases); ++i) {
48 std::string str("TeSt"); 70 std::string out;
49 str.push_back(0); 71 string16 in = WideToUTF16(json_wide_cases[i].to_escape);
50 str.push_back(15); 72 string_escape::JsonDoubleQuote(in, false, &out);
51 str.push_back(127); 73 EXPECT_EQ(std::string(json_wide_cases[i].escaped), out);
52 str.push_back(-16); 74 }
53 str.push_back(-128);
54 str.push_back('!');
55 75
56 out = "testy: "; 76 string16 in = WideToUTF16(json_wide_cases[0].to_escape);
57 string_escape::JavascriptDoubleQuote(str, false, &out); 77 std::string out;
58 ASSERT_EQ("testy: TeSt\\x00\\x0F\\x7F\xf0\x80!", out); 78 string_escape::JsonDoubleQuote(in, false, &out);
59 79
60 // Test escape sequences 80 // test quoting
61 out = "testy: "; 81 std::string out_quoted;
62 string_escape::JavascriptDoubleQuote("a\b\f\n\r\t\v\1\\.\"z", false, &out); 82 string_escape::JsonDoubleQuote(in, true, &out_quoted);
63 ASSERT_EQ("testy: a\\b\\f\\n\\r\\t\\v\\x01\\\\.\\\"z", out); 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);
64 } 95 }
OLDNEW
« no previous file with comments | « base/string_escape.cc ('k') | base/string_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698