1695b41eeSopenharmony_ci// Copyright 2021 Google Inc. All Rights Reserved. 2695b41eeSopenharmony_ci// 3695b41eeSopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License"); 4695b41eeSopenharmony_ci// you may not use this file except in compliance with the License. 5695b41eeSopenharmony_ci// You may obtain a copy of the License at 6695b41eeSopenharmony_ci// 7695b41eeSopenharmony_ci// http://www.apache.org/licenses/LICENSE-2.0 8695b41eeSopenharmony_ci// 9695b41eeSopenharmony_ci// Unless required by applicable law or agreed to in writing, software 10695b41eeSopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS, 11695b41eeSopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12695b41eeSopenharmony_ci// See the License for the specific language governing permissions and 13695b41eeSopenharmony_ci// limitations under the License. 14695b41eeSopenharmony_ci 15695b41eeSopenharmony_ci#include "json.h" 16695b41eeSopenharmony_ci 17695b41eeSopenharmony_ci#include "test.h" 18695b41eeSopenharmony_ci 19695b41eeSopenharmony_ciTEST(JSONTest, RegularAscii) { 20695b41eeSopenharmony_ci EXPECT_EQ(EncodeJSONString("foo bar"), "foo bar"); 21695b41eeSopenharmony_ci} 22695b41eeSopenharmony_ci 23695b41eeSopenharmony_ciTEST(JSONTest, EscapedChars) { 24695b41eeSopenharmony_ci EXPECT_EQ(EncodeJSONString("\"\\\b\f\n\r\t"), 25695b41eeSopenharmony_ci "\\\"" 26695b41eeSopenharmony_ci "\\\\" 27695b41eeSopenharmony_ci "\\b\\f\\n\\r\\t"); 28695b41eeSopenharmony_ci} 29695b41eeSopenharmony_ci 30695b41eeSopenharmony_ci// codepoints between 0 and 0x1f should be escaped 31695b41eeSopenharmony_ciTEST(JSONTest, ControlChars) { 32695b41eeSopenharmony_ci EXPECT_EQ(EncodeJSONString("\x01\x1f"), "\\u0001\\u001f"); 33695b41eeSopenharmony_ci} 34695b41eeSopenharmony_ci 35695b41eeSopenharmony_ci// Leave them alone as JSON accepts unicode literals 36695b41eeSopenharmony_ci// out of control character range 37695b41eeSopenharmony_ciTEST(JSONTest, UTF8) { 38695b41eeSopenharmony_ci const char* utf8str = "\xe4\xbd\xa0\xe5\xa5\xbd"; 39695b41eeSopenharmony_ci EXPECT_EQ(EncodeJSONString(utf8str), utf8str); 40695b41eeSopenharmony_ci} 41