14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#include "ecmascript/base/json_parser.h" 174514f5e3Sopenharmony_ci#include "ecmascript/base/json_helper.h" 184514f5e3Sopenharmony_ci#include "ecmascript/ecma_string.h" 194514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h" 204514f5e3Sopenharmony_ci 214514f5e3Sopenharmony_ciusing namespace panda::ecmascript; 224514f5e3Sopenharmony_ciusing namespace panda::ecmascript::base; 234514f5e3Sopenharmony_ci 244514f5e3Sopenharmony_cinamespace panda::test { 254514f5e3Sopenharmony_ciclass AsonParserTest : public BaseTestWithScope<false> { 264514f5e3Sopenharmony_cipublic: 274514f5e3Sopenharmony_ci using BigIntMode = base::JsonHelper::BigIntMode; 284514f5e3Sopenharmony_ci using ParseOptions = base::JsonHelper::ParseOptions; 294514f5e3Sopenharmony_ci using ParseReturnType = base::JsonHelper::ParseReturnType; 304514f5e3Sopenharmony_ci using TransformType = base::JsonHelper::TransformType; 314514f5e3Sopenharmony_ci 324514f5e3Sopenharmony_ci bool CheckSendableConstraint(JSTaggedValue value) const 334514f5e3Sopenharmony_ci { 344514f5e3Sopenharmony_ci if (!value.IsHeapObject()) { 354514f5e3Sopenharmony_ci // tagged value always follow sendable constraint. 364514f5e3Sopenharmony_ci return true; 374514f5e3Sopenharmony_ci } 384514f5e3Sopenharmony_ci TaggedObject *obj = value.IsWeak() ? value.GetTaggedWeakRef() : value.GetTaggedObject(); 394514f5e3Sopenharmony_ci auto *jsHClass = obj->GetClass(); 404514f5e3Sopenharmony_ci if (!jsHClass->IsJSShared()) { 414514f5e3Sopenharmony_ci return false; 424514f5e3Sopenharmony_ci } 434514f5e3Sopenharmony_ci if (jsHClass->IsExtensible()) { 444514f5e3Sopenharmony_ci LOG_ECMA(ERROR) << "sendable check failed. obj is extensible"; 454514f5e3Sopenharmony_ci value.D(); 464514f5e3Sopenharmony_ci return false; 474514f5e3Sopenharmony_ci } 484514f5e3Sopenharmony_ci if (!CheckSendableProps(value, obj)) { 494514f5e3Sopenharmony_ci return false; 504514f5e3Sopenharmony_ci } 514514f5e3Sopenharmony_ci // trace proto chain 524514f5e3Sopenharmony_ci auto proto = jsHClass->GetPrototype(); 534514f5e3Sopenharmony_ci if (!proto.IsNull() && !proto.IsJSShared()) { 544514f5e3Sopenharmony_ci LOG_ECMA(ERROR) << "sendable check failed. proto is not sendable."; 554514f5e3Sopenharmony_ci value.D(); 564514f5e3Sopenharmony_ci return false; 574514f5e3Sopenharmony_ci } 584514f5e3Sopenharmony_ci return true; 594514f5e3Sopenharmony_ci } 604514f5e3Sopenharmony_ci 614514f5e3Sopenharmony_ci bool CheckSendableProps(JSTaggedValue value, TaggedObject *obj) const 624514f5e3Sopenharmony_ci { 634514f5e3Sopenharmony_ci auto *jsHClass = obj->GetClass(); 644514f5e3Sopenharmony_ci auto layoutValue = jsHClass->GetLayout(); 654514f5e3Sopenharmony_ci if (layoutValue.IsNull()) { 664514f5e3Sopenharmony_ci return true; 674514f5e3Sopenharmony_ci } 684514f5e3Sopenharmony_ci auto *layoutInfo = LayoutInfo::Cast(layoutValue.GetTaggedObject()); 694514f5e3Sopenharmony_ci auto *jsObj = JSObject::Cast(obj); 704514f5e3Sopenharmony_ci auto *propsValue = TaggedArray::Cast(jsObj->GetProperties()); 714514f5e3Sopenharmony_ci if (propsValue->IsDictionaryMode()) { 724514f5e3Sopenharmony_ci for (int idx = 0; idx < static_cast<int>(jsHClass->NumberOfProps()); idx++) { 734514f5e3Sopenharmony_ci auto attr = layoutInfo->GetAttr(idx); 744514f5e3Sopenharmony_ci if (attr.IsInlinedProps()) { 754514f5e3Sopenharmony_ci // Do not check inline props 764514f5e3Sopenharmony_ci continue; 774514f5e3Sopenharmony_ci } 784514f5e3Sopenharmony_ci if (attr.IsWritable()) { 794514f5e3Sopenharmony_ci LOG_ECMA(ERROR) << "sendable check failed. supposed to be un-writable. idx: " << idx; 804514f5e3Sopenharmony_ci value.D(); 814514f5e3Sopenharmony_ci return false; 824514f5e3Sopenharmony_ci } 834514f5e3Sopenharmony_ci auto val = propsValue->Get(thread, idx - jsHClass->GetInlinedProperties()); 844514f5e3Sopenharmony_ci if (!CheckSendableConstraint(val)) { 854514f5e3Sopenharmony_ci LOG_ECMA(ERROR) << "sendable check failed. supposed to be sendable. idx: " << idx; 864514f5e3Sopenharmony_ci value.D(); 874514f5e3Sopenharmony_ci return false; 884514f5e3Sopenharmony_ci } 894514f5e3Sopenharmony_ci } 904514f5e3Sopenharmony_ci } 914514f5e3Sopenharmony_ci return true; 924514f5e3Sopenharmony_ci } 934514f5e3Sopenharmony_ci}; 944514f5e3Sopenharmony_ci 954514f5e3Sopenharmony_ciHWTEST_F_L0(AsonParserTest, Parser_001) 964514f5e3Sopenharmony_ci{ 974514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 984514f5e3Sopenharmony_ci Utf8JsonParser parser(thread, TransformType::SENDABLE); 994514f5e3Sopenharmony_ci // JSON Number 1004514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg2(factory->NewFromASCII("1234")); 1014514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr2(JSTaggedValue::ToString(thread, handleMsg2)); 1024514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result2 = parser.Parse(handleStr2); 1034514f5e3Sopenharmony_ci EXPECT_EQ(result2->GetNumber(), 1234); 1044514f5e3Sopenharmony_ci // JSON Literal 1054514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg3(factory->NewFromASCII("true")); 1064514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr3(JSTaggedValue::ToString(thread, handleMsg3)); 1074514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result3 = parser.Parse(handleStr3); 1084514f5e3Sopenharmony_ci EXPECT_EQ(result3.GetTaggedValue(), JSTaggedValue::True()); 1094514f5e3Sopenharmony_ci // JSON LiteraF 1104514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg4(factory->NewFromASCII("false")); 1114514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr4(JSTaggedValue::ToString(thread, handleMsg4)); 1124514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result4 = parser.Parse(handleStr4); 1134514f5e3Sopenharmony_ci EXPECT_EQ(result4.GetTaggedValue(), JSTaggedValue::False()); 1144514f5e3Sopenharmony_ci // JSON Unexpected 1154514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg5(factory->NewFromASCII("trus")); 1164514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr5(JSTaggedValue::ToString(thread, handleMsg5)); 1174514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result5 = parser.Parse(handleStr5); 1184514f5e3Sopenharmony_ci EXPECT_EQ(result5.GetTaggedValue(), JSTaggedValue::Exception()); 1194514f5e3Sopenharmony_ci} 1204514f5e3Sopenharmony_ci 1214514f5e3Sopenharmony_ciHWTEST_F_L0(AsonParserTest, Parser_002) 1224514f5e3Sopenharmony_ci{ 1234514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1244514f5e3Sopenharmony_ci Utf16JsonParser parser(thread, TransformType::SENDABLE); 1254514f5e3Sopenharmony_ci 1264514f5e3Sopenharmony_ci // JSON Number 1274514f5e3Sopenharmony_ci uint16_t array1Utf16[] = {0x31, 0x32, 0x33, 0x34}; // "1234" 1284514f5e3Sopenharmony_ci uint32_t array1Utf16Len = sizeof(array1Utf16) / sizeof(array1Utf16[0]); 1294514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg2(factory->NewFromUtf16(&array1Utf16[0], array1Utf16Len)); 1304514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr2(JSTaggedValue::ToString(thread, handleMsg2)); 1314514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result2 = parser.Parse(*handleStr2); 1324514f5e3Sopenharmony_ci EXPECT_EQ(result2->GetNumber(), 1234); 1334514f5e3Sopenharmony_ci // JSON Literal 1344514f5e3Sopenharmony_ci uint16_t array2Utf16[] = {0x74, 0x72, 0x75, 0x65}; // "true" 1354514f5e3Sopenharmony_ci uint32_t array2Utf16Len = sizeof(array2Utf16) / sizeof(array2Utf16[0]); 1364514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg3(factory->NewFromUtf16(&array2Utf16[0], array2Utf16Len)); 1374514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr3(JSTaggedValue::ToString(thread, handleMsg3)); 1384514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result3 = parser.Parse(*handleStr3); 1394514f5e3Sopenharmony_ci EXPECT_EQ(result3.GetTaggedValue(), JSTaggedValue::True()); 1404514f5e3Sopenharmony_ci // JSON String 1414514f5e3Sopenharmony_ci uint16_t array3Utf16[] = {0x22, 0x73, 0x74, 0x72, 0x69, 0x6E, 0X67, 0x22}; // "string" 1424514f5e3Sopenharmony_ci uint32_t array3Utf16Len = sizeof(array3Utf16) / sizeof(array3Utf16[0]); 1434514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg4(factory->NewFromUtf16(&array3Utf16[0], array3Utf16Len)); 1444514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr4(JSTaggedValue::ToString(thread, handleMsg4)); 1454514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result4 = parser.Parse(*handleStr4); 1464514f5e3Sopenharmony_ci JSHandle<EcmaString> handleEcmaStr(result4); 1474514f5e3Sopenharmony_ci EXPECT_STREQ("string", EcmaStringAccessor(handleEcmaStr).ToCString().c_str()); 1484514f5e3Sopenharmony_ci} 1494514f5e3Sopenharmony_ci 1504514f5e3Sopenharmony_ciHWTEST_F_L0(AsonParserTest, Parser_003) 1514514f5e3Sopenharmony_ci{ 1524514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1534514f5e3Sopenharmony_ci Utf8JsonParser parser(thread, TransformType::SENDABLE); 1544514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg(factory->NewFromASCII( 1554514f5e3Sopenharmony_ci "\t\r \n{\t\r \n \"json\"\t\r\n:\t\r \n{\t\r \n}\t\r \n,\t\r \n \"prop2\"\t\r \n:\t\r \n [\t\r \nfalse\t\r" 1564514f5e3Sopenharmony_ci "\n,\t\r \nnull\t\r, \ntrue\t\r,123.456\t\r \n]\t\r \n}\t\r \n")); 1574514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr(JSTaggedValue::ToString(thread, handleMsg)); // JSON Object 1584514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result = parser.Parse(handleStr); 1594514f5e3Sopenharmony_ci result->D(); 1604514f5e3Sopenharmony_ci EXPECT_TRUE(CheckSendableConstraint(result.GetTaggedValue())); 1614514f5e3Sopenharmony_ci} 1624514f5e3Sopenharmony_ci 1634514f5e3Sopenharmony_ciHWTEST_F_L0(AsonParserTest, Parser_004) 1644514f5e3Sopenharmony_ci{ 1654514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1664514f5e3Sopenharmony_ci Utf8JsonParser parser(thread, TransformType::SENDABLE); 1674514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg(factory->NewFromASCII("[100,2.5,\"abc\"]")); 1684514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr(JSTaggedValue::ToString(thread, handleMsg)); // JSON Array 1694514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result = parser.Parse(handleStr); 1704514f5e3Sopenharmony_ci result->D(); 1714514f5e3Sopenharmony_ci EXPECT_TRUE(CheckSendableConstraint(result.GetTaggedValue())); 1724514f5e3Sopenharmony_ci} 1734514f5e3Sopenharmony_ci 1744514f5e3Sopenharmony_ciHWTEST_F_L0(AsonParserTest, Parser_005) 1754514f5e3Sopenharmony_ci{ 1764514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1774514f5e3Sopenharmony_ci Utf8JsonParser parser(thread, TransformType::SENDABLE); 1784514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg(factory->NewFromASCII("{\"epf\":100,\"key1\":400}")); 1794514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr(JSTaggedValue::ToString(thread, handleMsg)); 1804514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result = parser.Parse(handleStr); 1814514f5e3Sopenharmony_ci EXPECT_TRUE(CheckSendableConstraint(result.GetTaggedValue())); 1824514f5e3Sopenharmony_ci} 1834514f5e3Sopenharmony_ci 1844514f5e3Sopenharmony_ciHWTEST_F_L0(AsonParserTest, Parser_006) 1854514f5e3Sopenharmony_ci{ 1864514f5e3Sopenharmony_ci Utf8JsonParser parser(thread, TransformType::SENDABLE); 1874514f5e3Sopenharmony_ci JSHandle<EcmaString> emptyString(thread->GlobalConstants()->GetHandledEmptyString()); 1884514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result = parser.Parse(emptyString); 1894514f5e3Sopenharmony_ci EXPECT_TRUE(result->IsException()); 1904514f5e3Sopenharmony_ci} 1914514f5e3Sopenharmony_ci 1924514f5e3Sopenharmony_ciHWTEST_F_L0(AsonParserTest, Parser_007) 1934514f5e3Sopenharmony_ci{ 1944514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1954514f5e3Sopenharmony_ci Utf8JsonParser parser(thread, TransformType::SENDABLE); 1964514f5e3Sopenharmony_ci 1974514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr(factory->NewFromASCII("\"\"")); 1984514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result = parser.Parse(handleStr); 1994514f5e3Sopenharmony_ci EXPECT_FALSE(result->IsException()); 2004514f5e3Sopenharmony_ci} 2014514f5e3Sopenharmony_ci 2024514f5e3Sopenharmony_ciHWTEST_F_L0(AsonParserTest, Parser_008) 2034514f5e3Sopenharmony_ci{ 2044514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2054514f5e3Sopenharmony_ci Utf8JsonParser parser(thread, TransformType::SENDABLE); 2064514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg( 2074514f5e3Sopenharmony_ci factory->NewFromASCII(R"({"innerEntry": {"x":1, "y":"abc", "str": "innerStr"}, "x": 1, "str": "outerStr"})")); 2084514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr(JSTaggedValue::ToString(thread, handleMsg)); 2094514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result = parser.Parse(handleStr); 2104514f5e3Sopenharmony_ci result->D(); 2114514f5e3Sopenharmony_ci EXPECT_FALSE(result->IsException()); 2124514f5e3Sopenharmony_ci EXPECT_TRUE(CheckSendableConstraint(result.GetTaggedValue())); 2134514f5e3Sopenharmony_ci} 2144514f5e3Sopenharmony_ci 2154514f5e3Sopenharmony_ciHWTEST_F_L0(AsonParserTest, Parser_009) 2164514f5e3Sopenharmony_ci{ 2174514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2184514f5e3Sopenharmony_ci Utf8JsonParser parser(thread, TransformType::SENDABLE); 2194514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg(factory->NewFromASCII(R"({})")); 2204514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr(JSTaggedValue::ToString(thread, handleMsg)); 2214514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result = parser.Parse(handleStr); 2224514f5e3Sopenharmony_ci result->D(); 2234514f5e3Sopenharmony_ci EXPECT_TRUE(CheckSendableConstraint(result.GetTaggedValue())); 2244514f5e3Sopenharmony_ci} 2254514f5e3Sopenharmony_ci 2264514f5e3Sopenharmony_ciHWTEST_F_L0(AsonParserTest, Parser_010) 2274514f5e3Sopenharmony_ci{ 2284514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2294514f5e3Sopenharmony_ci Utf8JsonParser parser(thread, TransformType::SENDABLE); 2304514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg(factory->NewFromASCII(R"([])")); 2314514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr(JSTaggedValue::ToString(thread, handleMsg)); 2324514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result = parser.Parse(handleStr); 2334514f5e3Sopenharmony_ci result->D(); 2344514f5e3Sopenharmony_ci EXPECT_TRUE(CheckSendableConstraint(result.GetTaggedValue())); 2354514f5e3Sopenharmony_ci} 2364514f5e3Sopenharmony_ci 2374514f5e3Sopenharmony_ciHWTEST_F_L0(AsonParserTest, Parser_011) 2384514f5e3Sopenharmony_ci{ 2394514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2404514f5e3Sopenharmony_ci Utf8JsonParser parser(thread, TransformType::SENDABLE); 2414514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg(factory->NewFromASCII(R"([1, 2, 3])")); 2424514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr(JSTaggedValue::ToString(thread, handleMsg)); 2434514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result = parser.Parse(handleStr); 2444514f5e3Sopenharmony_ci result->D(); 2454514f5e3Sopenharmony_ci EXPECT_TRUE(CheckSendableConstraint(result.GetTaggedValue())); 2464514f5e3Sopenharmony_ci} 2474514f5e3Sopenharmony_ci 2484514f5e3Sopenharmony_ciHWTEST_F_L0(AsonParserTest, Parser_012) 2494514f5e3Sopenharmony_ci{ 2504514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2514514f5e3Sopenharmony_ci Utf8JsonParser parser(thread, TransformType::SENDABLE); 2524514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg( 2534514f5e3Sopenharmony_ci factory->NewFromASCII(R"({"innerEntry": {"array": [1, 2, 3]}, "x": 1, "str": "outerStr"})")); 2544514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr(JSTaggedValue::ToString(thread, handleMsg)); 2554514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result = parser.Parse(handleStr); 2564514f5e3Sopenharmony_ci result->D(); 2574514f5e3Sopenharmony_ci EXPECT_TRUE(CheckSendableConstraint(result.GetTaggedValue())); 2584514f5e3Sopenharmony_ci} 2594514f5e3Sopenharmony_ci 2604514f5e3Sopenharmony_ciHWTEST_F_L0(AsonParserTest, Parser_013) 2614514f5e3Sopenharmony_ci{ 2624514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 2634514f5e3Sopenharmony_ci JSMutableHandle<JSFunction> constructor(thread, JSTaggedValue::Undefined()); 2644514f5e3Sopenharmony_ci constructor.Update(env->GetSObjectFunction()); 2654514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2664514f5e3Sopenharmony_ci JSHandle<JSObject> root = factory->NewJSObjectByConstructor(constructor); 2674514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> rootName(factory->GetEmptyString()); 2684514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result; 2694514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> undefined = JSHandle<JSTaggedValue>(thread, JSTaggedValue::Undefined()); 2704514f5e3Sopenharmony_ci result = Internalize::InternalizeJsonProperty(thread, root, rootName, undefined, TransformType::SENDABLE); 2714514f5e3Sopenharmony_ci ASSERT_TRUE(!result->IsUndefined()); 2724514f5e3Sopenharmony_ci} 2734514f5e3Sopenharmony_ci 2744514f5e3Sopenharmony_ciHWTEST_F_L0(AsonParserTest, Parser_014) 2754514f5e3Sopenharmony_ci{ 2764514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2774514f5e3Sopenharmony_ci Utf8JsonParser parser(thread, TransformType::SENDABLE); 2784514f5e3Sopenharmony_ci // JSON Unexpected 2794514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg1(factory->NewFromASCII("tr")); 2804514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr1(JSTaggedValue::ToString(thread, handleMsg1)); 2814514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result1 = parser.Parse(handleStr1); 2824514f5e3Sopenharmony_ci EXPECT_EQ(result1.GetTaggedValue(), JSTaggedValue::Exception()); 2834514f5e3Sopenharmony_ci // JSON Unexpected 2844514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg2(factory->NewFromASCII("fa")); 2854514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr2(JSTaggedValue::ToString(thread, handleMsg2)); 2864514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result2 = parser.Parse(handleStr2); 2874514f5e3Sopenharmony_ci EXPECT_EQ(result2.GetTaggedValue(), JSTaggedValue::Exception()); 2884514f5e3Sopenharmony_ci // JSON Unexpected 2894514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg3(factory->NewFromASCII("falss")); 2904514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr3(JSTaggedValue::ToString(thread, handleMsg3)); 2914514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result3 = parser.Parse(handleStr3); 2924514f5e3Sopenharmony_ci EXPECT_EQ(result3.GetTaggedValue(), JSTaggedValue::Exception()); 2934514f5e3Sopenharmony_ci} 2944514f5e3Sopenharmony_ci 2954514f5e3Sopenharmony_ciHWTEST_F_L0(AsonParserTest, Parser_015) 2964514f5e3Sopenharmony_ci{ 2974514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2984514f5e3Sopenharmony_ci Utf8JsonParser parser(thread, TransformType::SENDABLE); 2994514f5e3Sopenharmony_ci // JSON Unexpected 3004514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg1(factory->NewFromASCII(R"([1, 2, 3})")); 3014514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr1(JSTaggedValue::ToString(thread, handleMsg1)); 3024514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result1 = parser.Parse(handleStr1); 3034514f5e3Sopenharmony_ci EXPECT_EQ(result1.GetTaggedValue(), JSTaggedValue::Exception()); 3044514f5e3Sopenharmony_ci // JSON Unexpected 3054514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg2(factory->NewFromASCII(R"({"innerEntry""entry"})")); 3064514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr2(JSTaggedValue::ToString(thread, handleMsg2)); 3074514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result2 = parser.Parse(handleStr2); 3084514f5e3Sopenharmony_ci EXPECT_EQ(result2.GetTaggedValue(), JSTaggedValue::Exception()); 3094514f5e3Sopenharmony_ci // JSON Unexpected 3104514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg3(factory->NewFromASCII("1s2")); 3114514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr3(JSTaggedValue::ToString(thread, handleMsg3)); 3124514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result3 = parser.Parse(handleStr3); 3134514f5e3Sopenharmony_ci EXPECT_EQ(result3.GetTaggedValue(), JSTaggedValue::Exception()); 3144514f5e3Sopenharmony_ci // JSON Unexpected 3154514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg4(factory->NewFromASCII("122-")); 3164514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr4(JSTaggedValue::ToString(thread, handleMsg4)); 3174514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result4 = parser.Parse(handleStr4); 3184514f5e3Sopenharmony_ci EXPECT_EQ(result4.GetTaggedValue(), JSTaggedValue::Exception()); 3194514f5e3Sopenharmony_ci // JSON Unexpected 3204514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg5(factory->NewFromASCII("122+")); 3214514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr5(JSTaggedValue::ToString(thread, handleMsg5)); 3224514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result5 = parser.Parse(handleStr5); 3234514f5e3Sopenharmony_ci EXPECT_EQ(result5.GetTaggedValue(), JSTaggedValue::Exception()); 3244514f5e3Sopenharmony_ci} 3254514f5e3Sopenharmony_ci 3264514f5e3Sopenharmony_ciHWTEST_F_L0(AsonParserTest, Parser_016) 3274514f5e3Sopenharmony_ci{ 3284514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 3294514f5e3Sopenharmony_ci ParseOptions options1; 3304514f5e3Sopenharmony_ci Utf8JsonParser parser1(thread, TransformType::SENDABLE, options1); 3314514f5e3Sopenharmony_ci 3324514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg1( 3334514f5e3Sopenharmony_ci factory->NewFromASCII(R"({"small":1234})")); 3344514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr1(JSTaggedValue::ToString(thread, handleMsg1)); 3354514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result1 = parser1.Parse(handleStr1); 3364514f5e3Sopenharmony_ci EXPECT_NE(result1.GetTaggedValue(), JSTaggedValue::Exception()); 3374514f5e3Sopenharmony_ci 3384514f5e3Sopenharmony_ci ParseOptions options2; 3394514f5e3Sopenharmony_ci options2.bigIntMode = BigIntMode::PARSE_AS_BIGINT; 3404514f5e3Sopenharmony_ci Utf8JsonParser parser2(thread, TransformType::SENDABLE, options2); 3414514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg2(factory->NewFromASCII(R"({"big":1122334455667788999})")); 3424514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr2(JSTaggedValue::ToString(thread, handleMsg2)); 3434514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result2 = parser2.Parse(handleStr2); 3444514f5e3Sopenharmony_ci EXPECT_NE(result2.GetTaggedValue(), JSTaggedValue::Exception()); 3454514f5e3Sopenharmony_ci 3464514f5e3Sopenharmony_ci ParseOptions options3; 3474514f5e3Sopenharmony_ci options3.bigIntMode = BigIntMode::ALWAYS_PARSE_AS_BIGINT; 3484514f5e3Sopenharmony_ci Utf8JsonParser parser3(thread, TransformType::SENDABLE, options3); 3494514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg3(factory->NewFromASCII(R"({"large":1122334455667788999})")); 3504514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr3(JSTaggedValue::ToString(thread, handleMsg3)); 3514514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result3 = parser3.Parse(handleStr3); 3524514f5e3Sopenharmony_ci EXPECT_NE(result3.GetTaggedValue(), JSTaggedValue::Exception()); 3534514f5e3Sopenharmony_ci} 3544514f5e3Sopenharmony_ci 3554514f5e3Sopenharmony_ciHWTEST_F_L0(AsonParserTest, Parser_017) 3564514f5e3Sopenharmony_ci{ 3574514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 3584514f5e3Sopenharmony_ci ParseOptions options; 3594514f5e3Sopenharmony_ci options.returnType = ParseReturnType::MAP; 3604514f5e3Sopenharmony_ci Utf8JsonParser parser(thread, TransformType::SENDABLE, options); 3614514f5e3Sopenharmony_ci 3624514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg1(factory->NewFromASCII(R"({})")); 3634514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr1(JSTaggedValue::ToString(thread, handleMsg1)); 3644514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result1 = parser.Parse(handleStr1); 3654514f5e3Sopenharmony_ci EXPECT_NE(result1.GetTaggedValue(), JSTaggedValue::Exception()); 3664514f5e3Sopenharmony_ci 3674514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg2(factory->NewFromASCII(R"({"innerEntry""entry"})")); 3684514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr2(JSTaggedValue::ToString(thread, handleMsg2)); 3694514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result2 = parser.Parse(handleStr2); 3704514f5e3Sopenharmony_ci EXPECT_EQ(result2.GetTaggedValue(), JSTaggedValue::Exception()); 3714514f5e3Sopenharmony_ci 3724514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg3(factory->NewFromASCII(R"({"innerEntry"})")); 3734514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr3(JSTaggedValue::ToString(thread, handleMsg3)); 3744514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result3 = parser.Parse(handleStr3); 3754514f5e3Sopenharmony_ci EXPECT_EQ(result3.GetTaggedValue(), JSTaggedValue::Exception()); 3764514f5e3Sopenharmony_ci 3774514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg4(factory->NewFromASCII(R"({)")); 3784514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr4(JSTaggedValue::ToString(thread, handleMsg4)); 3794514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result4 = parser.Parse(handleStr4); 3804514f5e3Sopenharmony_ci EXPECT_EQ(result4.GetTaggedValue(), JSTaggedValue::Exception()); 3814514f5e3Sopenharmony_ci} 3824514f5e3Sopenharmony_ci 3834514f5e3Sopenharmony_ciHWTEST_F_L0(AsonParserTest, Parser_018) 3844514f5e3Sopenharmony_ci{ 3854514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 3864514f5e3Sopenharmony_ci ParseOptions options; 3874514f5e3Sopenharmony_ci options.returnType = ParseReturnType::MAP; 3884514f5e3Sopenharmony_ci Utf8JsonParser parser(thread, TransformType::NORMAL, options); 3894514f5e3Sopenharmony_ci 3904514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg1(factory->NewFromASCII(R"({})")); 3914514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr1(JSTaggedValue::ToString(thread, handleMsg1)); 3924514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result1 = parser.Parse(handleStr1); 3934514f5e3Sopenharmony_ci EXPECT_NE(result1.GetTaggedValue(), JSTaggedValue::Exception()); 3944514f5e3Sopenharmony_ci 3954514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg2( 3964514f5e3Sopenharmony_ci factory->NewFromASCII(R"({"innerEntry": {"array": [1, 2, 3]}, "x": 1, "str": "outerStr"})")); 3974514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr2(JSTaggedValue::ToString(thread, handleMsg2)); 3984514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result2 = parser.Parse(handleStr2); 3994514f5e3Sopenharmony_ci EXPECT_NE(result2.GetTaggedValue(), JSTaggedValue::Exception()); 4004514f5e3Sopenharmony_ci} 4014514f5e3Sopenharmony_ci 4024514f5e3Sopenharmony_ciHWTEST_F_L0(AsonParserTest, Parser_019) 4034514f5e3Sopenharmony_ci{ 4044514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 4054514f5e3Sopenharmony_ci ParseOptions options; 4064514f5e3Sopenharmony_ci options.bigIntMode = BigIntMode::PARSE_AS_BIGINT; 4074514f5e3Sopenharmony_ci Utf8JsonParser parser(thread, TransformType::NORMAL, options); 4084514f5e3Sopenharmony_ci 4094514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg1(factory->NewFromASCII(R"({"shortExp":1.79e+308})")); 4104514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr1(JSTaggedValue::ToString(thread, handleMsg1)); 4114514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result1 = parser.Parse(handleStr1); 4124514f5e3Sopenharmony_ci EXPECT_NE(result1.GetTaggedValue(), JSTaggedValue::Exception()); 4134514f5e3Sopenharmony_ci 4144514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg2( 4154514f5e3Sopenharmony_ci factory->NewFromASCII(R"({"longExp":1.7976931348623157e+308})")); 4164514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr2(JSTaggedValue::ToString(thread, handleMsg2)); 4174514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result2 = parser.Parse(handleStr2); 4184514f5e3Sopenharmony_ci EXPECT_NE(result2.GetTaggedValue(), JSTaggedValue::Exception()); 4194514f5e3Sopenharmony_ci} 4204514f5e3Sopenharmony_ci 4214514f5e3Sopenharmony_ciHWTEST_F_L0(AsonParserTest, Parser_020) 4224514f5e3Sopenharmony_ci{ 4234514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 4244514f5e3Sopenharmony_ci ParseOptions options; 4254514f5e3Sopenharmony_ci options.returnType = ParseReturnType::MAP; 4264514f5e3Sopenharmony_ci Utf8JsonParser parser(thread, TransformType::SENDABLE, options); 4274514f5e3Sopenharmony_ci 4284514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> handleMsg( 4294514f5e3Sopenharmony_ci factory->NewFromASCII(R"({"innerEntry": {"array": [1, 2, 3]}, "x": 1, "str": "outerStr"})")); 4304514f5e3Sopenharmony_ci JSHandle<EcmaString> handleStr(JSTaggedValue::ToString(thread, handleMsg)); 4314514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> result = parser.Parse(handleStr); 4324514f5e3Sopenharmony_ci EXPECT_NE(result.GetTaggedValue(), JSTaggedValue::Exception()); 4334514f5e3Sopenharmony_ci} 4344514f5e3Sopenharmony_ci 4354514f5e3Sopenharmony_ci} // namespace panda::test 436