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

Side by Side Diff: json/string_escape_unittest.cc

Issue 2050803003: Update to Chromium //base at Chromium commit e3a753f17bac62738b0dbf0b36510f767b081e4b. (Closed) Base URL: https://github.com/domokit/base.git@master
Patch Set: Created 4 years, 6 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
« no previous file with comments | « json/string_escape.cc ('k') | logging.cc » ('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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 6
7 #include "base/strings/string_util.h" 7 #include "base/strings/string_util.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
11 namespace base { 11 namespace base {
12 12
13 TEST(JSONStringEscapeTest, EscapeUTF8) { 13 TEST(JSONStringEscapeTest, EscapeUTF8) {
14 const struct { 14 const struct {
15 const char* to_escape; 15 const char* to_escape;
16 const char* escaped; 16 const char* escaped;
17 } cases[] = { 17 } cases[] = {
18 {"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, 18 {"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"},
19 {"a\b\f\n\r\t\v\1\\.\"z", 19 {"a\b\f\n\r\t\v\1\\.\"z", "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"},
20 "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"}, 20 {"b\x0f\x7f\xf0\xff!", // \xf0\xff is not a valid UTF-8 unit.
21 {"b\x0f\x7f\xf0\xff!", // \xf0\xff is not a valid UTF-8 unit. 21 "b\\u000F\x7F\xEF\xBF\xBD\xEF\xBF\xBD!"},
22 "b\\u000F\x7F\xEF\xBF\xBD\xEF\xBF\xBD!"}, 22 {"c<>d", "c\\u003C>d"},
23 {"c<>d", "c\\u003C>d"}, 23 {"Hello\xe2\x80\xa8world", "Hello\\u2028world"},
24 {"\xe2\x80\xa9purple", "\\u2029purple"},
24 }; 25 };
25 26
26 for (size_t i = 0; i < arraysize(cases); ++i) { 27 for (size_t i = 0; i < arraysize(cases); ++i) {
27 const char* in_ptr = cases[i].to_escape; 28 const char* in_ptr = cases[i].to_escape;
28 std::string in_str = in_ptr; 29 std::string in_str = in_ptr;
29 30
30 std::string out; 31 std::string out;
31 EscapeJSONString(in_ptr, false, &out); 32 EscapeJSONString(in_ptr, false, &out);
32 EXPECT_EQ(std::string(cases[i].escaped), out); 33 EXPECT_EQ(std::string(cases[i].escaped), out);
33 EXPECT_TRUE(IsStringUTF8(out)); 34 EXPECT_TRUE(IsStringUTF8(out));
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 EscapeJSONString(in, false, &out); 67 EscapeJSONString(in, false, &out);
67 EXPECT_EQ(expected, out); 68 EXPECT_EQ(expected, out);
68 EXPECT_TRUE(IsStringUTF8(out)); 69 EXPECT_TRUE(IsStringUTF8(out));
69 } 70 }
70 71
71 TEST(JSONStringEscapeTest, EscapeUTF16) { 72 TEST(JSONStringEscapeTest, EscapeUTF16) {
72 const struct { 73 const struct {
73 const wchar_t* to_escape; 74 const wchar_t* to_escape;
74 const char* escaped; 75 const char* escaped;
75 } cases[] = { 76 } cases[] = {
76 {L"b\uffb1\u00ff", "b\xEF\xBE\xB1\xC3\xBF"}, 77 {L"b\uffb1\u00ff", "b\xEF\xBE\xB1\xC3\xBF"},
77 {L"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, 78 {L"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"},
78 {L"a\b\f\n\r\t\v\1\\.\"z", 79 {L"a\b\f\n\r\t\v\1\\.\"z", "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"},
79 "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"}, 80 {L"b\x0f\x7f\xf0\xff!", "b\\u000F\x7F\xC3\xB0\xC3\xBF!"},
80 {L"b\x0f\x7f\xf0\xff!", "b\\u000F\x7F\xC3\xB0\xC3\xBF!"}, 81 {L"c<>d", "c\\u003C>d"},
81 {L"c<>d", "c\\u003C>d"}, 82 {L"Hello\u2028world", "Hello\\u2028world"},
83 {L"\u2029purple", "\\u2029purple"},
82 }; 84 };
83 85
84 for (size_t i = 0; i < arraysize(cases); ++i) { 86 for (size_t i = 0; i < arraysize(cases); ++i) {
85 string16 in = WideToUTF16(cases[i].to_escape); 87 string16 in = WideToUTF16(cases[i].to_escape);
86 88
87 std::string out; 89 std::string out;
88 EscapeJSONString(in, false, &out); 90 EscapeJSONString(in, false, &out);
89 EXPECT_EQ(std::string(cases[i].escaped), out); 91 EXPECT_EQ(std::string(cases[i].escaped), out);
90 EXPECT_TRUE(IsStringUTF8(out)); 92 EXPECT_TRUE(IsStringUTF8(out));
91 93
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 } 175 }
174 176
175 const char kEmbedNull[] = { '\xab', '\x39', '\0', '\x9f', '\xab' }; 177 const char kEmbedNull[] = { '\xab', '\x39', '\0', '\x9f', '\xab' };
176 std::string in(kEmbedNull, arraysize(kEmbedNull)); 178 std::string in(kEmbedNull, arraysize(kEmbedNull));
177 EXPECT_FALSE(IsStringUTF8(in)); 179 EXPECT_FALSE(IsStringUTF8(in));
178 EXPECT_EQ(std::string("\\u00AB9\\u0000\\u009F\\u00AB"), 180 EXPECT_EQ(std::string("\\u00AB9\\u0000\\u009F\\u00AB"),
179 EscapeBytesAsInvalidJSONString(in, false)); 181 EscapeBytesAsInvalidJSONString(in, false));
180 } 182 }
181 183
182 } // namespace base 184 } // namespace base
OLDNEW
« no previous file with comments | « json/string_escape.cc ('k') | logging.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698