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 testing::Test {
33 public:
SetUpTestCase()34 static void SetUpTestCase()
35 {
36 GTEST_LOG_(INFO) << "SetUpTestCase";
37 }
38
TearDownTestCase()39 static void TearDownTestCase()
40 {
41 GTEST_LOG_(INFO) << "TearDownCase";
42 }
43
SetUp()44 void SetUp() override
45 {
46 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
47 }
48
TearDown()49 void TearDown() override
50 {
51 TestHelper::DestroyEcmaVMWithScope(instance, scope);
52 }
53
54 EcmaVM *instance {nullptr};
55 EcmaHandleScope *scope {nullptr};
56 JSThread *thread {nullptr};
57
NewJSObject(JSThread * thread,ObjectFactory * factory,JSHandle<GlobalEnv> globalEnv)58 static JSHandle<JSObject> NewJSObject(JSThread *thread, ObjectFactory *factory, JSHandle<GlobalEnv> globalEnv)
59 {
60 JSFunction *jsFunc = globalEnv->GetObjectFunction().GetObject<JSFunction>();
61 JSHandle<JSTaggedValue> jsFunc1(thread, jsFunc);
62 JSHandle<JSObject> jsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(jsFunc1), jsFunc1);
63 return jsObj;
64 }
65 };
66
HWTEST_F_L0(BuiltinsGlobalTest,DecodeURIComponent)67 HWTEST_F_L0(BuiltinsGlobalTest, DecodeURIComponent)
68 {
69 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
70 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
71 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
72 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(123));
73
74 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
75 JSTaggedValue result = BuiltinsGlobal::DecodeURIComponent(ecmaRuntimeCallInfo);
76 TestHelper::TearDownFrame(thread, prev);
77 EXPECT_TRUE(result.IsString());
78 JSHandle<EcmaString> ecmaStrHandle(thread, result);
79 EXPECT_STREQ("123", EcmaStringAccessor(ecmaStrHandle).ToCString().c_str());
80 }
81
HWTEST_F_L0(BuiltinsGlobalTest,EncodeURIComponent)82 HWTEST_F_L0(BuiltinsGlobalTest, EncodeURIComponent)
83 {
84 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
85 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
86 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
87 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(123));
88
89 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
90 JSTaggedValue result = BuiltinsGlobal::EncodeURIComponent(ecmaRuntimeCallInfo);
91 TestHelper::TearDownFrame(thread, prev);
92 EXPECT_TRUE(result.IsString());
93 JSHandle<EcmaString> ecmaStrHandle(thread, result);
94 EXPECT_STREQ("123", EcmaStringAccessor(ecmaStrHandle).ToCString().c_str());
95 }
96
HWTEST_F_L0(BuiltinsGlobalTest,PrintEntrypointAndPrintString)97 HWTEST_F_L0(BuiltinsGlobalTest, PrintEntrypointAndPrintString)
98 {
99 // msg == nullptr
100 JSTaggedValue result = BuiltinsGlobal::PrintEntrypoint(nullptr);
101 EXPECT_EQ(result, JSTaggedValue::Undefined());
102 }
103
HWTEST_F_L0(BuiltinsGlobalTest,CallJsProxy)104 HWTEST_F_L0(BuiltinsGlobalTest, CallJsProxy)
105 {
106 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
107 auto ecmaVM = thread->GetEcmaVM();
108 JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
109 JSHandle<JSTaggedValue> emptyObj(thread, NewJSObject(thread, factory, env).GetTaggedValue());
110 JSHandle<JSProxy> proxy = factory->NewJSProxy(emptyObj, emptyObj);
111 proxy->SetCallable(false);
112 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
113 ecmaRuntimeCallInfo->SetFunction(proxy.GetTaggedValue());
114 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
115
116 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
117 JSTaggedValue result = BuiltinsGlobal::CallJsProxy(ecmaRuntimeCallInfo);
118 TestHelper::TearDownFrame(thread, prev);
119 EXPECT_TRUE(thread->HasPendingException());
120 EXPECT_EQ(result, JSTaggedValue::Undefined());
121 thread->ClearException();
122 }
123
HWTEST_F_L0(BuiltinsGlobalTest,Escape)124 HWTEST_F_L0(BuiltinsGlobalTest, Escape)
125 {
126 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
127 JSHandle<EcmaString> str1 = factory->NewFromASCII("?!=()#%&");
128 auto ecmaRuntimeCallInfo1 =
129 TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // NOLINT, 6 means 3 paras
130 ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
131 ecmaRuntimeCallInfo1->SetThis(JSTaggedValue::Undefined());
132 ecmaRuntimeCallInfo1->SetCallArg(0, str1.GetTaggedValue());
133
134 [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
135 JSTaggedValue result1 = BuiltinsGlobal::Escape(ecmaRuntimeCallInfo1);
136 TestHelper::TearDownFrame(thread, prev1);
137 EXPECT_TRUE(result1.IsString());
138 JSHandle<EcmaString> ecmaStrHandle1(thread, result1);
139 EXPECT_STREQ("%3F%21%3D%28%29%23%25%26", EcmaStringAccessor(ecmaStrHandle1).ToCString().c_str()); // NOLINT
140
141 JSHandle<EcmaString> str2 = factory->NewFromASCII("%u%u0%u9%ua%uF%u00%u09%u0f%u0F%u000%u00a%u00F");
142 auto ecmaRuntimeCallInfo2 =
143 TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // NOLINT
144 ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
145 ecmaRuntimeCallInfo2->SetThis(JSTaggedValue::Undefined());
146 ecmaRuntimeCallInfo2->SetCallArg(0, str2.GetTaggedValue()); // NOLINT
147
148 [[maybe_unused]] auto prev2 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
149 JSTaggedValue result2 = BuiltinsGlobal::Escape(ecmaRuntimeCallInfo2);
150 TestHelper::TearDownFrame(thread, prev2);
151 EXPECT_TRUE(result2.IsString());
152 JSHandle<EcmaString> ecmaStrHandle2(thread, result2);
153 EXPECT_STREQ("%25u%25u0%25u9%25ua%25uF%25u00%25u09%25u0f%25u0F%25u000%25u00a%25u00F", // NOLINT special value
154 EcmaStringAccessor(ecmaStrHandle2).ToCString().c_str());
155
156 JSHandle<EcmaString> str3 = factory->NewFromASCII("Hello World!");
157 auto ecmaRuntimeCallInfo3 =
158 TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // NOLINT
159 ecmaRuntimeCallInfo3->SetFunction(JSTaggedValue::Undefined());
160 ecmaRuntimeCallInfo3->SetThis(JSTaggedValue::Undefined());
161 ecmaRuntimeCallInfo3->SetCallArg(0, str3.GetTaggedValue());
162
163 [[maybe_unused]] auto prev3 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo3);
164 JSTaggedValue result3 = BuiltinsGlobal::Escape(ecmaRuntimeCallInfo3);
165 TestHelper::TearDownFrame(thread, prev3);
166 EXPECT_TRUE(result3.IsString());
167 JSHandle<EcmaString> ecmaStrHandle3(thread, result3);
168 EXPECT_STREQ("Hello%20World%21", EcmaStringAccessor(ecmaStrHandle3).ToCString().c_str());
169 }
170
HWTEST_F_L0(BuiltinsGlobalTest,Unescape)171 HWTEST_F_L0(BuiltinsGlobalTest, Unescape)
172 {
173 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
174 JSHandle<EcmaString> str1 = factory->NewFromASCII("");
175 auto ecmaRuntimeCallInfo1 =
176 TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // NOLINT
177 ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
178 ecmaRuntimeCallInfo1->SetThis(JSTaggedValue::Undefined());
179 ecmaRuntimeCallInfo1->SetCallArg(0, str1.GetTaggedValue());
180
181 [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
182 JSTaggedValue result1 = BuiltinsGlobal::Unescape(ecmaRuntimeCallInfo1);
183 TestHelper::TearDownFrame(thread, prev1);
184 EXPECT_TRUE(result1.IsString());
185 JSHandle<EcmaString> ecmaStrHandle1(thread, result1);
186 EXPECT_STREQ("", EcmaStringAccessor(ecmaStrHandle1).ToCString().c_str());
187
188 JSHandle<EcmaString> str2 = factory->NewFromASCII("%u%u0%u9%ua%uF%u00%u09%u0f%u0F%u000%u00a%u00F");
189 auto ecmaRuntimeCallInfo2 =
190 TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // NOLINT
191 ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
192 ecmaRuntimeCallInfo2->SetThis(JSTaggedValue::Undefined());
193 ecmaRuntimeCallInfo2->SetCallArg(0, str2.GetTaggedValue()); // NOLINT
194
195 [[maybe_unused]] auto prev2 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
196 JSTaggedValue result2 = BuiltinsGlobal::Unescape(ecmaRuntimeCallInfo2);
197 TestHelper::TearDownFrame(thread, prev2);
198 EXPECT_TRUE(result2.IsString());
199 JSHandle<EcmaString> ecmaStrHandle2(thread, result2);
200 EXPECT_STREQ("%u%u0%u9%ua%uF%u00%u09%u0f%u0F%u000%u00a%u00F",
201 EcmaStringAccessor(ecmaStrHandle2).ToCString().c_str());
202
203 JSHandle<EcmaString> str3 = factory->NewFromASCII("Hello%20World%21");
204 auto ecmaRuntimeCallInfo3 =
205 TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // NOLINT 6 means 3 paras
206 ecmaRuntimeCallInfo3->SetFunction(JSTaggedValue::Undefined());
207 ecmaRuntimeCallInfo3->SetThis(JSTaggedValue::Undefined());
208 ecmaRuntimeCallInfo3->SetCallArg(0, str3.GetTaggedValue());
209
210 [[maybe_unused]] auto prev3 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo3);
211 JSTaggedValue result3 = BuiltinsGlobal::Escape(ecmaRuntimeCallInfo3);
212 TestHelper::TearDownFrame(thread, prev3);
213 EXPECT_TRUE(result3.IsString());
214 JSHandle<EcmaString> ecmaStrHandle3(thread, result3);
215 EXPECT_STREQ("Hello%2520World%2521", EcmaStringAccessor(ecmaStrHandle3).ToCString().c_str());
216 }
217 } // namespace panda::test
218