1 /*
2 * Copyright (c) 2021 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/builtins/builtins_global.h"
17
18 #include "ecmascript/js_hclass.h"
19 #include "ecmascript/ecma_runtime_call_info.h"
20 #include "ecmascript/ecma_vm.h"
21 #include "ecmascript/global_env.h"
22 #include "ecmascript/js_tagged_value-inl.h"
23 #include "ecmascript/js_thread.h"
24 #include "ecmascript/object_factory.h"
25 #include "ecmascript/tests/test_helper.h"
26
27 using namespace panda::ecmascript;
28 using namespace panda::ecmascript::builtins;
29 using BuiltinsBase = panda::ecmascript::base::BuiltinsBase;
30
31 namespace panda::test {
32 class BuiltinsGlobalTest : public BaseTestWithScope<false> {
33 public:
NewJSObject(JSThread * thread,ObjectFactory * factory,JSHandle<GlobalEnv> globalEnv)34 static JSHandle<JSObject> NewJSObject(JSThread *thread, ObjectFactory *factory, JSHandle<GlobalEnv> globalEnv)
35 {
36 JSFunction *jsFunc = globalEnv->GetObjectFunction().GetObject<JSFunction>();
37 JSHandle<JSTaggedValue> jsFunc1(thread, jsFunc);
38 JSHandle<JSObject> jsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(jsFunc1), jsFunc1);
39 return jsObj;
40 }
41 };
42
HWTEST_F_L0(BuiltinsGlobalTest,DecodeURIComponent)43 HWTEST_F_L0(BuiltinsGlobalTest, DecodeURIComponent)
44 {
45 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
46 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
47 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
48 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(123));
49
50 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
51 JSTaggedValue result = BuiltinsGlobal::DecodeURIComponent(ecmaRuntimeCallInfo);
52 TestHelper::TearDownFrame(thread, prev);
53 EXPECT_TRUE(result.IsString());
54 JSHandle<EcmaString> ecmaStrHandle(thread, result);
55 EXPECT_STREQ("123", EcmaStringAccessor(ecmaStrHandle).ToCString().c_str());
56 }
57
HWTEST_F_L0(BuiltinsGlobalTest,EncodeURIComponent)58 HWTEST_F_L0(BuiltinsGlobalTest, EncodeURIComponent)
59 {
60 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
61 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
62 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
63 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(123));
64
65 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
66 JSTaggedValue result = BuiltinsGlobal::EncodeURIComponent(ecmaRuntimeCallInfo);
67 TestHelper::TearDownFrame(thread, prev);
68 EXPECT_TRUE(result.IsString());
69 JSHandle<EcmaString> ecmaStrHandle(thread, result);
70 EXPECT_STREQ("123", EcmaStringAccessor(ecmaStrHandle).ToCString().c_str());
71 }
72
HWTEST_F_L0(BuiltinsGlobalTest,PrintEntrypointAndPrintString)73 HWTEST_F_L0(BuiltinsGlobalTest, PrintEntrypointAndPrintString)
74 {
75 // msg == nullptr
76 JSTaggedValue result = BuiltinsGlobal::PrintEntrypoint(nullptr);
77 EXPECT_EQ(result, JSTaggedValue::Undefined());
78 }
79
HWTEST_F_L0(BuiltinsGlobalTest,CallJsProxy)80 HWTEST_F_L0(BuiltinsGlobalTest, CallJsProxy)
81 {
82 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
83 auto ecmaVM = thread->GetEcmaVM();
84 JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
85 JSHandle<JSTaggedValue> emptyObj(thread, NewJSObject(thread, factory, env).GetTaggedValue());
86 JSHandle<JSProxy> proxy = factory->NewJSProxy(emptyObj, emptyObj);
87 proxy->SetCallable(false);
88 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
89 ecmaRuntimeCallInfo->SetFunction(proxy.GetTaggedValue());
90 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
91
92 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
93 JSTaggedValue result = BuiltinsGlobal::CallJsProxy(ecmaRuntimeCallInfo);
94 TestHelper::TearDownFrame(thread, prev);
95 EXPECT_TRUE(thread->HasPendingException());
96 EXPECT_EQ(result, JSTaggedValue::Undefined());
97 thread->ClearException();
98 }
99
HWTEST_F_L0(BuiltinsGlobalTest,Escape)100 HWTEST_F_L0(BuiltinsGlobalTest, Escape)
101 {
102 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
103 JSHandle<EcmaString> str1 = factory->NewFromASCII("?!=()#%&");
104 auto ecmaRuntimeCallInfo1 =
105 TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // NOLINT, 6 means 3 paras
106 ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
107 ecmaRuntimeCallInfo1->SetThis(JSTaggedValue::Undefined());
108 ecmaRuntimeCallInfo1->SetCallArg(0, str1.GetTaggedValue());
109
110 [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
111 JSTaggedValue result1 = BuiltinsGlobal::Escape(ecmaRuntimeCallInfo1);
112 TestHelper::TearDownFrame(thread, prev1);
113 EXPECT_TRUE(result1.IsString());
114 JSHandle<EcmaString> ecmaStrHandle1(thread, result1);
115 EXPECT_STREQ("%3F%21%3D%28%29%23%25%26", EcmaStringAccessor(ecmaStrHandle1).ToCString().c_str()); // NOLINT
116
117 JSHandle<EcmaString> str2 = factory->NewFromASCII("%u%u0%u9%ua%uF%u00%u09%u0f%u0F%u000%u00a%u00F");
118 auto ecmaRuntimeCallInfo2 =
119 TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // NOLINT
120 ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
121 ecmaRuntimeCallInfo2->SetThis(JSTaggedValue::Undefined());
122 ecmaRuntimeCallInfo2->SetCallArg(0, str2.GetTaggedValue()); // NOLINT
123
124 [[maybe_unused]] auto prev2 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
125 JSTaggedValue result2 = BuiltinsGlobal::Escape(ecmaRuntimeCallInfo2);
126 TestHelper::TearDownFrame(thread, prev2);
127 EXPECT_TRUE(result2.IsString());
128 JSHandle<EcmaString> ecmaStrHandle2(thread, result2);
129 EXPECT_STREQ("%25u%25u0%25u9%25ua%25uF%25u00%25u09%25u0f%25u0F%25u000%25u00a%25u00F", // NOLINT special value
130 EcmaStringAccessor(ecmaStrHandle2).ToCString().c_str());
131
132 JSHandle<EcmaString> str3 = factory->NewFromASCII("Hello World!");
133 std::vector<JSTaggedValue> args{str3.GetTaggedValue()};
134 auto ecmaRuntimeCallInfo3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, args, 6);
135
136 [[maybe_unused]] auto prev3 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo3);
137 JSTaggedValue result3 = BuiltinsGlobal::Escape(ecmaRuntimeCallInfo3);
138 TestHelper::TearDownFrame(thread, prev3);
139 EXPECT_TRUE(result3.IsString());
140 JSHandle<EcmaString> ecmaStrHandle3(thread, result3);
141 EXPECT_STREQ("Hello%20World%21", EcmaStringAccessor(ecmaStrHandle3).ToCString().c_str());
142 }
143
HWTEST_F_L0(BuiltinsGlobalTest,Unescape)144 HWTEST_F_L0(BuiltinsGlobalTest, Unescape)
145 {
146 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
147 JSHandle<EcmaString> str1 = factory->NewFromASCII("");
148 auto ecmaRuntimeCallInfo1 =
149 TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // NOLINT
150 ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
151 ecmaRuntimeCallInfo1->SetThis(JSTaggedValue::Undefined());
152 ecmaRuntimeCallInfo1->SetCallArg(0, str1.GetTaggedValue());
153
154 [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
155 JSTaggedValue result1 = BuiltinsGlobal::Unescape(ecmaRuntimeCallInfo1);
156 TestHelper::TearDownFrame(thread, prev1);
157 EXPECT_TRUE(result1.IsString());
158 JSHandle<EcmaString> ecmaStrHandle1(thread, result1);
159 EXPECT_STREQ("", EcmaStringAccessor(ecmaStrHandle1).ToCString().c_str());
160
161 JSHandle<EcmaString> str2 = factory->NewFromASCII("%u%u0%u9%ua%uF%u00%u09%u0f%u0F%u000%u00a%u00F");
162 auto ecmaRuntimeCallInfo2 =
163 TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // NOLINT
164 ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
165 ecmaRuntimeCallInfo2->SetThis(JSTaggedValue::Undefined());
166 ecmaRuntimeCallInfo2->SetCallArg(0, str2.GetTaggedValue()); // NOLINT
167
168 [[maybe_unused]] auto prev2 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
169 JSTaggedValue result2 = BuiltinsGlobal::Unescape(ecmaRuntimeCallInfo2);
170 TestHelper::TearDownFrame(thread, prev2);
171 EXPECT_TRUE(result2.IsString());
172 JSHandle<EcmaString> ecmaStrHandle2(thread, result2);
173 EXPECT_STREQ("%u%u0%u9%ua%uF%u00%u09%u0f%u0F%u000%u00a%u00F",
174 EcmaStringAccessor(ecmaStrHandle2).ToCString().c_str());
175
176 JSHandle<EcmaString> str3 = factory->NewFromASCII("Hello%20World%21");
177 std::vector<JSTaggedValue> args{str3.GetTaggedValue()};
178 auto ecmaRuntimeCallInfo3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, args, 6);
179
180 [[maybe_unused]] auto prev3 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo3);
181 JSTaggedValue result3 = BuiltinsGlobal::Escape(ecmaRuntimeCallInfo3);
182 TestHelper::TearDownFrame(thread, prev3);
183 EXPECT_TRUE(result3.IsString());
184 JSHandle<EcmaString> ecmaStrHandle3(thread, result3);
185 EXPECT_STREQ("Hello%2520World%2521", EcmaStringAccessor(ecmaStrHandle3).ToCString().c_str());
186 }
187 } // namespace panda::test
188