1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/base/json_helper.h"
17 #include "ecmascript/tests/test_helper.h"
18
19 using namespace panda::ecmascript;
20 using namespace panda::ecmascript::base;
21
22 namespace panda::test {
23 class JsonHelperTest : public BaseTestWithScope<false> {
24 };
25
26 #if ENABLE_NEXT_OPTIMIZATION
27 /**
28 * @tc.name: IsFastValueToQuotedString
29 * @tc.desc: Check basic Ascii characters
30 * @tc.type: FUNC
31 * @tc.require:
32 */
HWTEST_F_L0(JsonHelperTest,IsFastValueToQuotedString_001)33 HWTEST_F_L0(JsonHelperTest, IsFastValueToQuotedString_001)
34 {
35 Span<const uint8_t> sp1(reinterpret_cast<const uint8_t*>("Hello World"), 11);
36 Span<const uint8_t> sp2(reinterpret_cast<const uint8_t*>("0123456789"), 10);
37 Span<const uint8_t> sp3(reinterpret_cast<const uint8_t*>("!#$%&'()*+,-./:;=?@[]^_`{|}~"), 28);
38 EXPECT_TRUE(JsonHelper::IsFastValueToQuotedString(sp1));
39 EXPECT_TRUE(JsonHelper::IsFastValueToQuotedString(sp2));
40 EXPECT_TRUE(JsonHelper::IsFastValueToQuotedString(sp3));
41 }
42
43 /**
44 * @tc.name: IsFastValueToQuotedString
45 * @tc.desc: Check Ascii characters need to escape, including Control characters
46 * @tc.type: FUNC
47 * @tc.require:
48 */
HWTEST_F_L0(JsonHelperTest,IsFastValueToQuotedString_002)49 HWTEST_F_L0(JsonHelperTest, IsFastValueToQuotedString_002)
50 {
51 Span<const uint8_t> sp1(reinterpret_cast<const uint8_t*>("\""), 1);
52 Span<const uint8_t> sp2(reinterpret_cast<const uint8_t*>("\\"), 1);
53 Span<const uint8_t> sp3(reinterpret_cast<const uint8_t*>("\b"), 1);
54 Span<const uint8_t> sp4(reinterpret_cast<const uint8_t*>("\f"), 1);
55 Span<const uint8_t> sp5(reinterpret_cast<const uint8_t*>("\n"), 1);
56 Span<const uint8_t> sp6(reinterpret_cast<const uint8_t*>("\r"), 1);
57 Span<const uint8_t> sp7(reinterpret_cast<const uint8_t*>("\t"), 1);
58 EXPECT_FALSE(JsonHelper::IsFastValueToQuotedString(sp1));
59 EXPECT_FALSE(JsonHelper::IsFastValueToQuotedString(sp2));
60 EXPECT_FALSE(JsonHelper::IsFastValueToQuotedString(sp3));
61 EXPECT_FALSE(JsonHelper::IsFastValueToQuotedString(sp4));
62 EXPECT_FALSE(JsonHelper::IsFastValueToQuotedString(sp5));
63 EXPECT_FALSE(JsonHelper::IsFastValueToQuotedString(sp6));
64 EXPECT_FALSE(JsonHelper::IsFastValueToQuotedString(sp7));
65
66
67 for (uint8_t c = 0; c < 32; c++) {
68 EXPECT_FALSE(JsonHelper::IsFastValueToQuotedString(
69 Span<const uint8_t>(reinterpret_cast<const uint8_t*>(&c), 1)));
70 }
71 }
72
73 /**
74 * @tc.name: IsFastValueToQuotedString
75 * @tc.desc: Check mixed content
76 * @tc.type: FUNC
77 * @tc.require:
78 */
HWTEST_F_L0(JsonHelperTest,IsFastValueToQuotedString_003)79 HWTEST_F_L0(JsonHelperTest, IsFastValueToQuotedString_003)
80 {
81 Span<const uint8_t> sp1(reinterpret_cast<const uint8_t*>("Hello\nWorld"), 11);
82 Span<const uint8_t> sp2(reinterpret_cast<const uint8_t*>("Test\"Quote"), 10);
83 Span<const uint8_t> sp3(reinterpret_cast<const uint8_t*>("Test\\BackSlash"), 14);
84
85 EXPECT_FALSE(JsonHelper::IsFastValueToQuotedString(sp1));
86 EXPECT_FALSE(JsonHelper::IsFastValueToQuotedString(sp2));
87 EXPECT_FALSE(JsonHelper::IsFastValueToQuotedString(sp3));
88 }
89 #endif
90 }
91