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 "ark_native_engine.h"
17 #include "gtest/gtest.h"
18 #include "module_checker_delegate.h"
19 #include "napi/native_api.h"
20 #include "napi/native_common.h"
21 #include "napi/native_node_api.h"
22 #include "native_utils.h"
23 #include "securec.h"
24 #include "test.h"
25 #include "test_common.h"
26 #include "utils/log.h"
27
28 using panda::ArrayRef;
29 using panda::JSValueRef;
30 using panda::Local;
31 using panda::ObjectRef;
32
33 class ArkApiAllowlistTest : public NativeEngineTest {
34 public:
SetUpTestCase()35 static void SetUpTestCase()
36 {
37 GTEST_LOG_(INFO) << "ArkApiAllowlistTest SetUpTestCase";
38 }
39
TearDownTestCase()40 static void TearDownTestCase()
41 {
42 GTEST_LOG_(INFO) << "ArkApiAllowlistTest TearDownTestCase";
43 }
44
SetUp()45 void SetUp() override {}
TearDown()46 void TearDown() override {}
47 };
48
49
TestFunction(napi_env env,napi_callback_info info)50 napi_value TestFunction(napi_env env, napi_callback_info info)
51 {
52 HILOG_INFO("this is TestFunction");
53 return nullptr;
54 }
55
CheckPropertyNames(Local<ObjectRef> & obj,const EcmaVM * vm,uint32_t & filter,std::unordered_map<std::string,bool> & keyToCond)56 void CheckPropertyNames(Local<ObjectRef> &obj,
57 const EcmaVM* vm,
58 uint32_t &filter,
59 std::unordered_map<std::string, bool> &keyToCond)
60 {
61 Local<ArrayRef> propertyNamesArrayVal = obj->GetAllPropertyNames(vm, filter);
62 for (uint32_t i = 0; i < propertyNamesArrayVal->Length(vm); ++i) {
63 Local<JSValueRef> nameValue = ArrayRef::GetValueAt(vm, propertyNamesArrayVal, i);
64 std::string keyname = nameValue->ToString(vm)->ToString(vm);
65 HILOG_INFO("exportCopy->system->function:%{public}s", keyname.c_str());
66 if (keyToCond.find(keyname) != keyToCond.end()) {
67 keyToCond[keyname] = true;
68 }
69 }
70 }
71
Test001(const EcmaVM * vm,Local<ObjectRef> & exportCopy,uint32_t & filter)72 bool Test001(const EcmaVM* vm, Local<ObjectRef> &exportCopy, uint32_t &filter)
73 {
74 bool condition1 = false;
75 bool condition2 = false;
76 bool condition3 = true;
77 Local<ArrayRef> propertyNamesArrayVal = exportCopy->GetAllPropertyNames(vm, filter);
78 for (uint32_t i = 0; i < propertyNamesArrayVal->Length(vm); ++i) {
79 Local<JSValueRef> nameValue = ArrayRef::GetValueAt(vm, propertyNamesArrayVal, i);
80 std::string keyname = nameValue->ToString(vm)->ToString(vm);
81 HILOG_INFO("exportCopy->function:%{public}s", keyname.c_str());
82 if (keyname == "System") {
83 Local<ObjectRef> obj = exportCopy->Get(vm, nameValue);
84 std::unordered_map<std::string, bool> keyToCond;
85 keyToCond["getSystemLanguage"] = condition1;
86 keyToCond["is24HourClock"] = condition2;
87 CheckPropertyNames(obj, vm, filter, keyToCond);
88 condition1 = keyToCond["getSystemLanguage"];
89 condition2 = keyToCond["is24HourClock"];
90 } else {
91 condition3 = false;
92 }
93 }
94 return condition1 && condition2 && condition3;
95 }
96
Test002(const EcmaVM * vm,Local<ObjectRef> & exportCopy,uint32_t & filter)97 bool Test002(const EcmaVM* vm, Local<ObjectRef> &exportCopy, uint32_t &filter)
98 {
99 bool condition1 = false;
100 bool condition2 = false;
101 bool condition3 = true;
102 bool condition4 = false;
103 bool condition5 = false;
104 bool condition6 = false;
105 bool condition7 = false;
106 Local<ArrayRef> propertyNamesArrayVal = exportCopy->GetAllPropertyNames(vm, filter);
107 for (uint32_t i = 0; i < propertyNamesArrayVal->Length(vm); ++i) {
108 Local<JSValueRef> nameValue = ArrayRef::GetValueAt(vm, propertyNamesArrayVal, i);
109 std::string keyname = nameValue->ToString(vm)->ToString(vm);
110 HILOG_INFO("exportCopy->function:%{public}s", keyname.c_str());
111 if (keyname == "Locale") {
112 condition1 = true;
113 Local<ObjectRef> obj = exportCopy->Get(vm, nameValue);
114 std::unordered_map<std::string, bool> keyToCond;
115 keyToCond["function001"] = condition4;
116 keyToCond["systemOtherFunction"] = condition5;
117 CheckPropertyNames(obj, vm, filter, keyToCond);
118 condition4 = keyToCond["function001"];
119 condition5 = keyToCond["systemOtherFunction"];
120 } else if (keyname == "DateTimeFormat") {
121 condition2 = true;
122 Local<ObjectRef> obj = exportCopy->Get(vm, nameValue);
123 std::unordered_map<std::string, bool> keyToCond;
124 keyToCond["function001"] = condition6;
125 keyToCond["systemOtherFunction"] = condition7;
126 CheckPropertyNames(obj, vm, filter, keyToCond);
127 condition6 = keyToCond["function001"];
128 condition7 = keyToCond["systemOtherFunction"];
129 } else if (nameValue->Typeof(vm)->ToString(vm) == "object") {
130 condition3 = false;
131 }
132 }
133 return condition1 && condition2 && condition3 && condition4 && condition5 && condition6 && condition7;
134 }
135