• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gtest/gtest.h"
17 #include "napi/native_api.h"
18 #include "napi/native_common.h"
19 #include "napi/native_node_api.h"
20 #include "module_checker_delegate.h"
21 #include "form_module_checker.h"
22 #include "ark_native_engine.h"
23 #include "napi/native_engine/native_utils.h"
24 #include "securec.h"
25 #include "test.h"
26 #include "test_common.h"
27 #include "utils/log.h"
28 
29 
30 class ArkApiAllowlistTest : public NativeEngineTest {
31 public:
SetUpTestCase()32     static void SetUpTestCase()
33     {
34         GTEST_LOG_(INFO) << "ArkApiAllowlistTest SetUpTestCase";
35     }
36 
TearDownTestCase()37     static void TearDownTestCase()
38     {
39         GTEST_LOG_(INFO) << "ArkApiAllowlistTest TearDownTestCase";
40     }
41 
SetUp()42     void SetUp() override {}
TearDown()43     void TearDown() override {}
44 };
45 
46 
TestFunction01(napi_env env,napi_callback_info info)47 napi_value TestFunction01(napi_env env, napi_callback_info info)
48 {
49     HILOG_INFO("this is TestFunction01");
50     return nullptr;
51 }
52 
53 
54 /**
55  * @tc.name: CopyPropertyApiFilterTest002
56  * @tc.desc: Test CopyPropertyApiFilter Functional Logic.
57  * @tc.type: FUNC
58  */
59 HWTEST_F(ArkApiAllowlistTest, CopyPropertyApiFilterTest001, testing::ext::TestSize.Level1)
60 {
61     ArkNativeEngine* arkNativeEngine = static_cast<ArkNativeEngine*>(engine_);
62     const EcmaVM* vm = arkNativeEngine->GetEcmaVm();
63     napi_env env = reinterpret_cast<napi_env>(arkNativeEngine);
64     std::unique_ptr<ApiAllowListChecker> apiAllowListFilter = nullptr;
65     std::shared_ptr<FormModuleChecker> formChecker = std::make_shared<FormModuleChecker>();
66     formChecker->CheckModuleLoadable("i18n", apiAllowListFilter);
67 
68     napi_property_descriptor systemProperties[] = {
69         DECLARE_NAPI_FUNCTION("getSystemLanguage", TestFunction01),
70         DECLARE_NAPI_FUNCTION("is24HourClock", TestFunction01),
71         DECLARE_NAPI_FUNCTION("systemOtherFunction", TestFunction01),
72     };
73     napi_value system;
74     napi_create_object(env, &system);
75     napi_define_properties(env, system, sizeof(systemProperties) / sizeof(napi_property_descriptor), systemProperties);
76 
77     napi_property_descriptor i18nProperties[] = {
78         DECLARE_NAPI_FUNCTION("getSystemLanguage", TestFunction01),
79         DECLARE_NAPI_FUNCTION("i18nOtherFunction", TestFunction01),
80         DECLARE_NAPI_PROPERTY("System", system),
81     };
82     napi_value i18n;
83     napi_create_object(env, &i18n);
84     napi_define_properties(env, i18n, sizeof(i18nProperties) / sizeof(napi_property_descriptor), i18nProperties);
85     panda::Local<panda::ObjectRef> i18nObj = LocalValueFromJsValue(i18n);
86 
87     panda::Local<panda::ObjectRef> exportCopy = panda::ObjectRef::New(vm);
88 
89     if (apiAllowListFilter != nullptr) {
90         std::string apiPath = "i18n";
91         if ((*apiAllowListFilter)(apiPath)) {
92             ArkNativeEngine::CopyPropertyApiFilter(apiAllowListFilter, vm, i18nObj, exportCopy, apiPath);
93         }
94     }
95 
96     uint32_t filter = NATIVE_DEFAULT;
97     panda::Local<panda::ArrayRef> propertyNamesArrayVal01 = i18nObj->GetAllPropertyNames(vm, filter);
98     for (int i = propertyNamesArrayVal01->Length(vm) - 1; i >= 0; i--) {
99         HILOG_INFO("exportObj->function:%{public}s",
100             panda::ArrayRef::GetValueAt(vm, propertyNamesArrayVal01, i)->ToString(vm)->ToString().c_str());
101     }
102     panda::Local<panda::ArrayRef> propertyNamesArrayVal02 = exportCopy->GetAllPropertyNames(vm, filter);
103     bool condition1 = false;
104     bool condition2 = false;
105     bool condition3 = true;
106     for (int i = propertyNamesArrayVal02->Length(vm) - 1; i >= 0; i--) {
107         panda::Local<panda::JSValueRef> nameValue = panda::ArrayRef::GetValueAt(vm, propertyNamesArrayVal02, i);
108         std::string keyname = nameValue->ToString(vm)->ToString();
109         HILOG_INFO("exportCopy->function:%{public}s", keyname.c_str());
110         if (keyname == "System") {
111             panda::Local<panda::ObjectRef> obj = exportCopy->Get(vm, nameValue);
112             panda::Local<panda::ArrayRef> propertyNamesArrayVal03 = obj->GetAllPropertyNames(vm, filter);
113             for (int j = propertyNamesArrayVal03->Length(vm) - 1; j >= 0; j--) {
114                 nameValue = panda::ArrayRef::GetValueAt(vm, propertyNamesArrayVal03, j);
115                 keyname = nameValue->ToString(vm)->ToString();
116                 HILOG_INFO("exportCopy->system->function:%{public}s", keyname.c_str());
117                 if (keyname == "getSystemLanguage") {
118                     condition1 = true;
119                 }
120                 if (keyname == "is24HourClock") {
121                     condition2 = true;
122                 }
123             }
124         } else {
125             condition3 = false;
126         }
127     }
128     ASSERT_EQ(condition1 && condition2 && condition3, true);
129 }
130 
131 
132 /**
133  * @tc.name: CopyPropertyApiFilterTest001
134  * @tc.desc: Test CopyPropertyApiFilter Functional Logic.
135  * @tc.type: FUNC
136  */
137 HWTEST_F(ArkApiAllowlistTest, CopyPropertyApiFilterTest002, testing::ext::TestSize.Level1)
138 {
139     ArkNativeEngine* arkNativeEngine = static_cast<ArkNativeEngine*>(engine_);
140     const EcmaVM* vm = arkNativeEngine->GetEcmaVm();
141     napi_env env = reinterpret_cast<napi_env>(arkNativeEngine);
142     std::unique_ptr<ApiAllowListChecker> apiAllowListFilter = nullptr;
143     std::shared_ptr<FormModuleChecker> formChecker = std::make_shared<FormModuleChecker>();
144     formChecker->CheckModuleLoadable("intl", apiAllowListFilter);
145 
146     napi_property_descriptor l2Properties[] = {
147         DECLARE_NAPI_FUNCTION("function001", TestFunction01),
148         DECLARE_NAPI_FUNCTION("function002", TestFunction01),
149         DECLARE_NAPI_FUNCTION("systemOtherFunction", TestFunction01),
150     };
151     napi_value l2obj;
152     napi_create_object(env, &l2obj);
153     napi_define_properties(env, l2obj, sizeof(l2Properties) / sizeof(napi_property_descriptor), l2Properties);
154 
155     napi_property_descriptor intlProperties[] = {
156         DECLARE_NAPI_FUNCTION("getSystemLanguage", TestFunction01),
157         DECLARE_NAPI_FUNCTION("i18nOtherFunction", TestFunction01),
158         DECLARE_NAPI_PROPERTY("Locale2", l2obj),
159         DECLARE_NAPI_PROPERTY("Locale", l2obj),
160         DECLARE_NAPI_PROPERTY("DateTimeFormat", l2obj),
161     };
162     napi_value intl;
163     napi_create_object(env, &intl);
164     napi_define_properties(env, intl, sizeof(intlProperties) / sizeof(napi_property_descriptor), intlProperties);
165     panda::Local<panda::ObjectRef> intlObj = LocalValueFromJsValue(intl);
166 
167     panda::Local<panda::ObjectRef> exportCopy = panda::ObjectRef::New(vm);
168 
169     if (apiAllowListFilter != nullptr) {
170         std::string apiPath = "intl";
171         if ((*apiAllowListFilter)(apiPath)) {
172             ArkNativeEngine::CopyPropertyApiFilter(apiAllowListFilter, vm, intlObj, exportCopy, apiPath);
173         }
174     }
175 
176     uint32_t filter = NATIVE_DEFAULT;
177     panda::Local<panda::ArrayRef> propertyNamesArrayVal01 = intlObj->GetAllPropertyNames(vm, filter);
178     for (int i = propertyNamesArrayVal01->Length(vm) - 1; i >= 0; i--) {
179         HILOG_INFO("exportObj->function:%{public}s",
180             panda::ArrayRef::GetValueAt(vm, propertyNamesArrayVal01, i)->ToString(vm)->ToString().c_str());
181     }
182     panda::Local<panda::ArrayRef> propertyNamesArrayVal02 = exportCopy->GetAllPropertyNames(vm, filter);
183     bool condition1 = false;
184     bool condition2 = false;
185     bool condition3 = true;
186     bool condition4 = false;
187     bool condition5 = false;
188     bool condition6 = false;
189     bool condition7 = false;
190     for (int i = propertyNamesArrayVal02->Length(vm) - 1; i >= 0; i--) {
191         panda::Local<panda::JSValueRef> nameValue = panda::ArrayRef::GetValueAt(vm, propertyNamesArrayVal02, i);
192         std::string keyname = nameValue->ToString(vm)->ToString();
193         HILOG_INFO("exportCopy->function:%{public}s", keyname.c_str());
194         if (keyname == "Locale") {
195             condition1 = true;
196             panda::Local<panda::ObjectRef> obj = exportCopy->Get(vm, nameValue);
197             panda::Local<panda::ArrayRef> propertyNamesArrayVal03 = obj->GetAllPropertyNames(vm, filter);
198             for (int j = propertyNamesArrayVal03->Length(vm) - 1; j >= 0; j--) {
199                 nameValue = panda::ArrayRef::GetValueAt(vm, propertyNamesArrayVal03, j);
200                 keyname = nameValue->ToString(vm)->ToString();
201                 HILOG_INFO("exportCopy->system->function:%{public}s", keyname.c_str());
202                 if (keyname == "function001") {
203                     condition4 = true;
204                 }
205                 if (keyname == "systemOtherFunction") {
206                     condition5 = true;
207                 }
208             }
209         } else if (keyname == "DateTimeFormat") {
210             condition2 = true;
211             panda::Local<panda::ObjectRef> obj = exportCopy->Get(vm, nameValue);
212             panda::Local<panda::ArrayRef> propertyNamesArrayVal03 = obj->GetAllPropertyNames(vm, filter);
213             for (int j = propertyNamesArrayVal03->Length(vm) - 1; j >= 0; j--) {
214                 nameValue = panda::ArrayRef::GetValueAt(vm, propertyNamesArrayVal03, j);
215                 keyname = nameValue->ToString(vm)->ToString();
216                 HILOG_INFO("exportCopy->system->function:%{public}s", keyname.c_str());
217                 if (keyname == "function001") {
218                     condition6 = true;
219                 }
220                 if (keyname == "systemOtherFunction") {
221                     condition7 = true;
222                 }
223             }
224         } else if (nameValue->Typeof(vm)->ToString() == "object") {
225             condition3 = false;
226         }
227     }
228     ASSERT_EQ(condition1 && condition2 && condition3 && condition4 && condition5 && condition6 && condition7, true);
229 }
230