1 /*
2 * Copyright (c) 2025 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 <gtest/hwext/gtest-multithread.h>
18
19 #define private public
20 #define protected public
21 #include "ets_environment.h"
22 #include "ets_runtime.h"
23 #undef private
24 #undef protected
25 #include "hilog_tag_wrapper.h"
26 #include "js_runtime.h"
27 #include "runtime.h"
28 #include "ets_environment.h"
29
30 using namespace testing;
31 using namespace testing::ext;
32 using namespace testing::mt;
33
34 namespace OHOS {
35 namespace AbilityRuntime {
36 namespace {
37 const std::string TEST_BUNDLE_NAME = "com.ohos.contactsdataability";
38 const std::string TEST_MODULE_NAME = ".ContactsDataAbility";
39 const std::string TEST_ABILITY_NAME = "ContactsDataAbility";
40 const std::string TEST_CODE_PATH = "/data/storage/el1/bundle";
41 const std::string TEST_HAP_PATH = "/system/app/com.ohos.contactsdataabilityContacts_DataAbility.hap";
42 const std::string TEST_LIB_PATH = "/data/storage/el1/bundle/lib/";
43 const std::string TEST_MODULE_PATH = "/data/storage/el1/bundle/curJsModulePath";
44 } // namespace
45
46 class EtsRuntimeTest : public testing::Test {
47 public:
48 static void SetUpTestCase();
49 static void TearDownTestCase();
50 void SetUp() override;
51 void TearDown() override;
52
53 Runtime::Options options_;
54 };
55
SetUpTestCase()56 void EtsRuntimeTest::SetUpTestCase() {}
57
TearDownTestCase()58 void EtsRuntimeTest::TearDownTestCase() {}
59
SetUp()60 void EtsRuntimeTest::SetUp()
61 {
62 Runtime::Options newOptions;
63 options_ = newOptions;
64 options_.bundleName = TEST_BUNDLE_NAME;
65 options_.codePath = TEST_CODE_PATH;
66 options_.loadAce = false;
67 options_.isBundle = true;
68 options_.preload = false;
69 std::shared_ptr<AppExecFwk::EventRunner> eventRunner = AppExecFwk::EventRunner::Create(TEST_ABILITY_NAME);
70 options_.eventRunner = eventRunner;
71 }
72
TearDown()73 void EtsRuntimeTest::TearDown() {}
74
75 /**
76 * @tc.name: Create_100
77 * @tc.desc: EtsRuntime test for Create Initialize failed.
78 * @tc.type: FUNC
79 */
80 HWTEST_F(EtsRuntimeTest, Create_100, TestSize.Level1)
81 {
82 options_.lang = Runtime::Language::JS;
83 options_.preload = true;
84 options_.isStageModel = false;
85 std::unique_ptr<JsRuntime> jsRuntime = JsRuntime::Create(options_);
86 auto etsRuntime = ETSRuntime::Create(options_, jsRuntime);
87 EXPECT_EQ(etsRuntime, nullptr);
88 options_.lang = Runtime::Language::ETS;
89 options_.preload = false;
90 options_.isStageModel = true;
91 }
92
93 /**
94 * @tc.name: SetAppLibPath_100
95 * @tc.desc: EtsRuntime test for SetAppLibPath.
96 * @tc.type: FUNC
97 */
98 HWTEST_F(EtsRuntimeTest, SetAppLibPath_100, TestSize.Level1)
99 {
100 std::map<std::string, std::vector<std::string>> testPathMap;
101 testPathMap["com.example.app"] = { "/data/abc", "/data/def" };
102 testPathMap["com.example.demo"] = { "/data/demo/es", "/data/demo/ts" };
103 std::map<std::string, std::string> abcPathsToBundleModuleNameMap;
104 std::unique_ptr<ETSRuntime> etsRuntime = std::make_unique<ETSRuntime>();
105 etsRuntime->SetAppLibPath(testPathMap, abcPathsToBundleModuleNameMap, false);
106 EXPECT_NE(testPathMap.size(), 0);
107 }
108
109 /**
110 * @tc.name: Initialize_100
111 * @tc.desc: EtsRuntime test for Initialize lang is not ETS.
112 * @tc.type: FUNC
113 */
114 HWTEST_F(EtsRuntimeTest, Initialize_100, TestSize.Level1)
115 {
116 options_.lang = Runtime::Language::JS;
117 std::unique_ptr<JsRuntime> jsRuntime = nullptr;
118 std::unique_ptr<ETSRuntime> etsRuntime = std::make_unique<ETSRuntime>();
119 bool result = etsRuntime->Initialize(options_, jsRuntime);
120 EXPECT_EQ(result, false);
121 options_.lang = Runtime::Language::ETS;
122 }
123
124 /**
125 * @tc.name: Initialize_200
126 * @tc.desc: EtsRuntime test for Initialize lang is not ETS.
127 * @tc.type: FUNC
128 */
129 HWTEST_F(EtsRuntimeTest, Initialize_200, TestSize.Level1)
130 {
131 Runtime::Options options;
132 options.lang = Runtime::Language::ETS;
133 options.arkNativeFilePath = "test_app/";
134 options.moduleName = "TestModule";
135 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
136 ASSERT_NE(jsRuntime, nullptr);
137 std::unique_ptr<ETSRuntime> etsRuntime = std::make_unique<ETSRuntime>();
138 ASSERT_NE(etsRuntime, nullptr);
139 bool result = etsRuntime->Initialize(options, jsRuntime);
140 EXPECT_EQ(result, false);
141 }
142
143 /**
144 * @tc.name: LoadModule_0100
145 * @tc.desc: LoadModule with non-empty hapPath should construct file path directly.
146 * @tc.type: FUNC
147 */
148 HWTEST_F(EtsRuntimeTest, LoadModule_0100, TestSize.Level1)
149 {
150 auto etsRuntime = std::make_unique<ETSRuntime>();
151 etsRuntime->codePath_ = "/test/code/path";
152 std::string moduleName = "abc";
153 std::string modulePath = "dir.test.module";
154 std::string hapPath = "/some/hap";
155 std::string srcEntrance = "main.ets";
156 etsRuntime->PreloadModule(moduleName, hapPath, false, false);
157 auto result = etsRuntime->LoadModule(moduleName, modulePath, hapPath, false, false, srcEntrance);
158 EXPECT_EQ(result, nullptr);
159 }
160
161 /**
162 * @tc.name: LoadModule_0200
163 * @tc.desc: LoadModule trims moduleName containing "::" correctly.
164 * @tc.type: FUNC
165 */
166 HWTEST_F(EtsRuntimeTest, LoadModule_0200, TestSize.Level1)
167 {
168 auto etsRuntime = std::make_unique<ETSRuntime>();
169 etsRuntime->codePath_ = "/code";
170 std::string moduleName = "lib::submod";
171 std::string modulePath = "m.js";
172 std::string hapPath = "/hap";
173 std::string srcEntrance = "main";
174 etsRuntime->LoadModule(moduleName, modulePath, hapPath, false, false, srcEntrance);
175 EXPECT_EQ(etsRuntime->moduleName_, "lib");
176 }
177
178 /**
179 * @tc.name: Deinitialize_100
180 * @tc.desc: EtsRuntime test for Deinitialize.
181 * @tc.type: FUNC
182 */
183 HWTEST_F(EtsRuntimeTest, Deinitialize_100, TestSize.Level1)
184 {
185 std::unique_ptr<ETSRuntime> etsRuntime = std::make_unique<ETSRuntime>();
186 etsRuntime->Deinitialize();
187 EXPECT_EQ(etsRuntime->jsRuntime_, nullptr);
188 }
189
190 /**
191 * @tc.name: GetAniEnv_100
192 * @tc.desc: EtsRuntime test for GetAniEnv.
193 * @tc.type: FUNC
194 */
195 HWTEST_F(EtsRuntimeTest, GetAniEnv_100, TestSize.Level1)
196 {
197 std::unique_ptr<ETSRuntime> etsRuntime = std::make_unique<ETSRuntime>();
198 auto env = etsRuntime->GetAniEnv();
199 EXPECT_EQ(env, nullptr);
200 }
201
202 /**
203 * @tc.name: LoadModule_100
204 * @tc.desc: EtsRuntime test for LoadModule.
205 * @tc.type: FUNC
206 */
207 HWTEST_F(EtsRuntimeTest, LoadModule_100, TestSize.Level1)
208 {
209 std::unique_ptr<ETSRuntime> etsRuntime = std::make_unique<ETSRuntime>();
210
211 std::string moduleName = TEST_MODULE_NAME;
212 moduleName += "::";
213 std::string modulePath = TEST_MODULE_PATH;
214 std::string hapPath = "";
215 bool esmodule = true;
216 bool useCommonChunk = false;
217 std::string srcEntrance = "";
218
219 auto env = etsRuntime->LoadModule(moduleName, modulePath, hapPath, esmodule, useCommonChunk, srcEntrance);
220 EXPECT_EQ(env, nullptr);
221
222 env = nullptr;
223 hapPath = TEST_HAP_PATH;
224 env = etsRuntime->LoadModule(moduleName, modulePath, hapPath, esmodule, useCommonChunk, srcEntrance);
225 EXPECT_EQ(env, nullptr);
226 }
227
228 /**
229 * @tc.name: LoadEtsModule_100
230 * @tc.desc: EtsRuntime test for LoadEtsModule.
231 * @tc.type: FUNC
232 */
233 HWTEST_F(EtsRuntimeTest, LoadEtsModule_100, TestSize.Level1)
234 {
235 std::unique_ptr<ETSRuntime> etsRuntime = std::make_unique<ETSRuntime>();
236
237 std::string moduleName = TEST_MODULE_NAME;
238 moduleName += "::";
239 std::string modulePath = TEST_MODULE_PATH;
240 std::string hapPath = "";
241 std::string srcEntrance = "";
242 auto env = etsRuntime->LoadEtsModule(moduleName, modulePath, hapPath, srcEntrance);
243 EXPECT_EQ(env, nullptr);
244 }
245
246 /**
247 * @tc.name: GetLanguage_100
248 * @tc.desc: EtsRuntime test for GetLanguage.
249 * @tc.type: FUNC
250 */
251 HWTEST_F(EtsRuntimeTest, GetLanguage_100, TestSize.Level1)
252 {
253 std::unique_ptr<ETSRuntime> etsRuntime = std::make_unique<ETSRuntime>();
254 auto language = etsRuntime->GetLanguage();
255 EXPECT_EQ(language, Runtime::Language::ETS);
256 }
257
258 /**
259 * @tc.name: PreFork_100
260 * @tc.desc: EtsRuntime test for PreFork.
261 * @tc.type: FUNC
262 */
263 HWTEST_F(EtsRuntimeTest, PreFork_100, TestSize.Level1)
264 {
265 Runtime::Options options;
266 options.lang = Runtime::Language::JS;
267 options.preload = true;
268 options.isStageModel = false;
269 std::unique_ptr<JsRuntime> jsRuntime = nullptr;
270 std::unique_ptr<ETSRuntime> etsRuntime = std::make_unique<ETSRuntime>();
271 auto instance = etsRuntime->PreFork(options, jsRuntime);
272 EXPECT_EQ(instance, nullptr);
273 }
274
275 /**
276 * @tc.name: PostFork_100
277 * @tc.desc: EtsRuntime test for PostFork.
278 * @tc.type: FUNC
279 */
280 HWTEST_F(EtsRuntimeTest, PostFork_100, TestSize.Level1)
281 {
282 Runtime::Options jsOptions;
283 jsOptions.lang = Runtime::Language::JS;
284 jsOptions.preload = true;
285 jsOptions.isStageModel = false;
286 std::unique_ptr<JsRuntime> jsRuntime = JsRuntime::Create(jsOptions);;
287 ASSERT_NE(jsRuntime, nullptr);
288
289 Runtime::Options etsOptions;
290 etsOptions.lang = Runtime::Language::ETS;
291 etsOptions.preload = false;
292 etsOptions.isStageModel = true;
293 std::unique_ptr<ETSRuntime> etsRuntime = std::make_unique<ETSRuntime>();
294 auto result = etsRuntime->PostFork(etsOptions, jsRuntime);
295 EXPECT_EQ(result, true);
296 }
297
298 /**
299 * @tc.name: PreloadSystemClass_100
300 * @tc.desc: EtsRuntime test for PreloadSystemClass.
301 * @tc.type: FUNC
302 */
303 HWTEST_F(EtsRuntimeTest, PreloadSystemClass_100, TestSize.Level1)
304 {
305 std::unique_ptr<ETSRuntime> etsRuntime = std::make_unique<ETSRuntime>();
306 std::string className = "className";
307 auto result = etsRuntime->PreloadSystemClass(className.c_str());
308 EXPECT_EQ(result, true);
309 }
310
311 /**
312 * @tc.name: SetModuleLoadChecker_100
313 * @tc.desc: EtsRuntime test for SetModuleLoadChecker.
314 * @tc.type: FUNC
315 */
316 HWTEST_F(EtsRuntimeTest, SetModuleLoadChecker_100, TestSize.Level1)
317 {
318 std::unique_ptr<ETSRuntime> etsRuntime = std::make_unique<ETSRuntime>();
319 std::string className = "className";
320 etsRuntime->SetModuleLoadChecker(nullptr);
321 EXPECT_EQ(etsRuntime->GetJsRuntime(), nullptr);
322 etsRuntime->jsRuntime_ = std::make_unique<JsRuntime>();
323 etsRuntime->SetModuleLoadChecker(nullptr);
324 EXPECT_NE(etsRuntime->GetJsRuntime(), nullptr);
325 }
326 } // namespace AbilityRuntime
327 } // namespace OHOS