OLD | NEW |
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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 | 15 |
16 TEST(JSONStringEscapeTest, EscapeUTF8) { | 16 TEST(JSONStringEscapeTest, EscapeUTF8) { |
17 const struct { | 17 const struct { |
18 const char* to_escape; | 18 const char* to_escape; |
19 const char* escaped; | 19 const char* escaped; |
20 } cases[] = { | 20 } cases[] = { |
21 {"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, | 21 {"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"}, |
22 {"a\b\f\n\r\t\v\1\\.\"z", | 22 {"a\b\f\n\r\t\v\1\\.\"z", "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"}, |
23 "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"}, | 23 {"b\x0f\x7f\xf0\xff!", // \xf0\xff is not a valid UTF-8 unit. |
24 {"b\x0f\x7f\xf0\xff!", // \xf0\xff is not a valid UTF-8 unit. | 24 "b\\u000F\x7F\xEF\xBF\xBD\xEF\xBF\xBD!"}, |
25 "b\\u000F\x7F\xEF\xBF\xBD\xEF\xBF\xBD!"}, | 25 {"c<>d", "c\\u003C>d"}, |
26 {"c<>d", "c\\u003C>d"}, | 26 {"Hello\xe2\x80\xa8world", "Hello\\u2028world"}, |
27 {"Hello\xe2\x80\xa8world", "Hello\\u2028world"}, | 27 {"\xe2\x80\xa9purple", "\\u2029purple"}, |
28 {"\xe2\x80\xa9purple", "\\u2029purple"}, | 28 {"\xF3\xBF\xBF\xBF", "\xEF\xBF\xBD"}, |
29 }; | 29 }; |
30 | 30 |
31 for (size_t i = 0; i < arraysize(cases); ++i) { | 31 for (size_t i = 0; i < arraysize(cases); ++i) { |
32 const char* in_ptr = cases[i].to_escape; | 32 const char* in_ptr = cases[i].to_escape; |
33 std::string in_str = in_ptr; | 33 std::string in_str = in_ptr; |
34 | 34 |
35 std::string out; | 35 std::string out; |
36 EscapeJSONString(in_ptr, false, &out); | 36 EscapeJSONString(in_ptr, false, &out); |
37 EXPECT_EQ(std::string(cases[i].escaped), out); | 37 EXPECT_EQ(std::string(cases[i].escaped), out); |
38 EXPECT_TRUE(IsStringUTF8(out)); | 38 EXPECT_TRUE(IsStringUTF8(out)); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 } | 180 } |
181 | 181 |
182 const char kEmbedNull[] = { '\xab', '\x39', '\0', '\x9f', '\xab' }; | 182 const char kEmbedNull[] = { '\xab', '\x39', '\0', '\x9f', '\xab' }; |
183 std::string in(kEmbedNull, arraysize(kEmbedNull)); | 183 std::string in(kEmbedNull, arraysize(kEmbedNull)); |
184 EXPECT_FALSE(IsStringUTF8(in)); | 184 EXPECT_FALSE(IsStringUTF8(in)); |
185 EXPECT_EQ(std::string("\\u00AB9\\u0000\\u009F\\u00AB"), | 185 EXPECT_EQ(std::string("\\u00AB9\\u0000\\u009F\\u00AB"), |
186 EscapeBytesAsInvalidJSONString(in, false)); | 186 EscapeBytesAsInvalidJSONString(in, false)); |
187 } | 187 } |
188 | 188 |
189 } // namespace base | 189 } // namespace base |
OLD | NEW |