1 /*
2 * Copyright (c) 2024 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 <cstdint>
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include <gtest/hwext/gtest-multithread.h>
20 #include <mutex>
21 #include <securec.h>
22 #include <string>
23 #include <thread>
24 #include <vector>
25 #include <unordered_map>
26
27 #include "aot_compiler_client.h"
28 #include "aot_compiler_service.h"
29 #include "aot_compiler_error_utils.h"
30 #include "aot_compiler_impl.h"
31 #include "aot_compiler_load_callback.h"
32 #include "iservice_registry.h"
33 #include "system_ability_definition.h"
34
35 using namespace testing::ext;
36 using namespace testing::mt;
37
38 namespace OHOS::ArkCompiler {
39 namespace {
40 constexpr int TEST_ERR_OK = 0;
41 constexpr int TEST_ERR_AN_EMPTY = 5;
42 constexpr int TEST_ERR_OTHERS = 100;
43 const std::string compilerPkgInfoValue =
44 "{\"abcName\":\"ets/modules.abc\","
45 "\"abcOffset\":\"0x1000\","
46 "\"abcSize\":\"0x60b058\","
47 "\"appIdentifier\":\"5765880207853624761\","
48 "\"bundleName\":\"com.ohos.contacts\","
49 "\"BundleUid\":\"0x1317b6f\","
50 "\"isEncryptedBundle\":\"0x0\","
51 "\"isScreenOff\":\"0x1\","
52 "\"moduleName\":\"entry\","
53 "\"pgoDir\":\"/data/app/el1/100/aot_compiler/ark_profile/com.ohos.contacts\","
54 "\"pkgPath\":\"/system/app/Contacts/Contacts.hap\","
55 "\"processUid\":\"0xbf4\"}";
56
57 const std::unordered_map<std::string, std::string> argsMapForTest {
58 {"target-compiler-mode", "partial"},
59 {"aot-file", "/data/app/el1/public/aot_compiler/ark_cache/com.ohos.contacts/arm64/entry"},
60 {"compiler-pkg-info", compilerPkgInfoValue},
61 {"compiler-external-pkg-info", "[]"},
62 {"compiler-opt-bc-range", ""},
63 {"compiler-device-state", "1"},
64 {"compiler-baseline-pgo", "0"},
65 {"ABC-Path", "/system/app/Contacts/Contacts.hap/ets/modules.abc"},
66 {"BundleUid", "20020079"},
67 {"BundleGid", "20020079"},
68 {"anFileName", "/data/app/el1/public/aot_compiler/ark_cache/com.ohos.contacts/arm64/entry.an"},
69 {"appIdentifier", "5765880207853624761"}
70 };
71 } // namespace ark_aot_compiler arguments
72
73 class AotCompilerImplMock : public AotCompilerImpl {
74 public:
75 AotCompilerImplMock() = default;
76 ~AotCompilerImplMock() = default;
77 AotCompilerImplMock(const AotCompilerImplMock&) = delete;
78 AotCompilerImplMock(AotCompilerImplMock&&) = delete;
79 AotCompilerImplMock& operator=(const AotCompilerImplMock&) = delete;
80 AotCompilerImplMock& operator=(AotCompilerImplMock&&) = delete;
81
PrintAOTCompilerResultMock(const int compilerStatus) const82 int32_t PrintAOTCompilerResultMock(const int compilerStatus) const
83 {
84 return PrintAOTCompilerResult(compilerStatus);
85 }
86
EcmascriptAotCompilerMock(const std::unordered_map<std::string,std::string> & argsMap,std::vector<int16_t> & sigData)87 int32_t EcmascriptAotCompilerMock(const std::unordered_map<std::string, std::string> &argsMap,
88 std::vector<int16_t> &sigData)
89 {
90 #ifdef CODE_SIGN_ENABLE
91 if (!allowAotCompiler_) {
92 return ERR_AOT_COMPILER_CALL_CANCELLED;
93 }
94
95 std::unique_ptr<AOTArgsHandler> argsHandler = std::make_unique<AOTArgsHandler>(argsMap);
96 if (argsHandler->Handle(0) != ERR_OK) {
97 return ERR_AOT_COMPILER_PARAM_FAILED;
98 }
99
100 int32_t ret = ERR_OK;
101 pid_t pid = fork();
102 if (pid == -1) {
103 return ERR_AOT_COMPILER_CALL_FAILED;
104 } else if (pid == 0) {
105 DropCapabilities();
106 sleep(2); // 2: mock ark aot compiler run time with 2s
107 ExecuteInChildProcess();
108 } else {
109 mockChildPid_ = pid;
110 ExecuteInParentProcess(pid, ret);
111 }
112 if (ret == ERR_OK_NO_AOT_FILE) {
113 return ERR_OK;
114 }
115 return ret != ERR_OK ? ret : AOTLocalCodeSign(sigData);
116 #else
117 return ERR_AOT_COMPILER_SIGNATURE_DISABLE;
118 #endif
119 }
120
AOTLocalCodeSignMock(std::vector<int16_t> & sigData) const121 int32_t AOTLocalCodeSignMock(std::vector<int16_t> &sigData) const
122 {
123 return AOTLocalCodeSign(sigData);
124 }
125
InitStateMock(const pid_t childPid)126 void InitStateMock(const pid_t childPid)
127 {
128 InitState(childPid);
129 }
130
ResetStateMock()131 void ResetStateMock()
132 {
133 ResetState();
134 }
135
PauseAotCompilerMock()136 void PauseAotCompilerMock()
137 {
138 PauseAotCompiler();
139 }
140
AllowAotCompilerMock()141 void AllowAotCompilerMock()
142 {
143 AllowAotCompiler();
144 }
145
GetChildPidMock()146 pid_t GetChildPidMock()
147 {
148 return mockChildPid_;
149 }
150
SetAOTArgsHandler(std::unique_ptr<AOTArgsHandler> argsHandler)151 void SetAOTArgsHandler(std::unique_ptr<AOTArgsHandler> argsHandler)
152 {
153 argsHandler_ = std::move(argsHandler);
154 }
155
156 private:
157 pid_t mockChildPid_ = -1;
158 };
159
160 class AotCompilerImplTest : public testing::Test {
161 public:
AotCompilerImplTest()162 AotCompilerImplTest() {}
~AotCompilerImplTest()163 virtual ~AotCompilerImplTest() {}
164
SetUpTestCase()165 static void SetUpTestCase() {}
TearDownTestCase()166 static void TearDownTestCase() {}
SetUp()167 void SetUp() override {}
TearDown()168 void TearDown() override {}
169 };
170
171 /**
172 * @tc.name: AotCompilerImplTest_001
173 * @tc.desc: AotCompilerImpl::GetInstance()
174 * @tc.type: Func
175 */
176 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_001, TestSize.Level0)
177 {
178 AotCompilerImpl *aotImplPtr = nullptr;
179 aotImplPtr = &AotCompilerImplMock::GetInstance();
180 EXPECT_NE(aotImplPtr, nullptr);
181 }
182
183 /**
184 * @tc.name: AotCompilerImplTest_002
185 * @tc.desc: AotCompilerImpl::EcmascriptAotCompiler() when compiler-check-pgo-version
186 * @tc.type: Func
187 */
188 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_002, TestSize.Level0)
189 {
190 AotCompilerImpl &aotImpl = AotCompilerImplMock::GetInstance();
191 std::unordered_map<std::string, std::string> argsMap(argsMapForTest);
192 std::string keyCheckPgoVersion = "compiler-check-pgo-version";
193 std::string valueCheckPgoVersion = "true";
194 argsMap.emplace(keyCheckPgoVersion, valueCheckPgoVersion);
195 std::vector<int16_t> sigData;
196 int32_t ret = aotImpl.EcmascriptAotCompiler(argsMap, sigData);
197 #ifdef CODE_SIGN_ENABLE
198 EXPECT_NE(ret, ERR_AOT_COMPILER_SIGNATURE_DISABLE);
199 #else
200 EXPECT_EQ(ret, ERR_AOT_COMPILER_SIGNATURE_DISABLE);
201 #endif
202 EXPECT_TRUE(sigData.empty());
203 }
204
205 /**
206 * @tc.name: AotCompilerImplTest_003
207 * @tc.desc: AotCompilerImpl::EcmascriptAotCompiler() when compile not any method
208 * @tc.type: Func
209 */
210 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_003, TestSize.Level0)
211 {
212 AotCompilerImpl &aotImpl = AotCompilerImplMock::GetInstance();
213 std::unordered_map<std::string, std::string> argsMap(argsMapForTest);
214 std::string keyCompileNoMethod = "compiler-methods-range";
215 std::string valueCompileNoMethod = "0:0";
216 argsMap.emplace(keyCompileNoMethod, valueCompileNoMethod);
217 std::vector<int16_t> sigData { 0, 1, 2, 3, 4, 5 };
218 int32_t ret = aotImpl.EcmascriptAotCompiler(argsMap, sigData);
219 #ifdef CODE_SIGN_ENABLE
220 EXPECT_NE(ret, ERR_AOT_COMPILER_SIGNATURE_DISABLE);
221 #else
222 EXPECT_EQ(ret, ERR_AOT_COMPILER_SIGNATURE_DISABLE);
223 #endif
224 EXPECT_FALSE(sigData.empty());
225 }
226
227 /**
228 * @tc.name: AotCompilerImplTest_004
229 * @tc.desc: AotCompilerImpl::StopAotCompiler()
230 * @tc.type: Func
231 */
232 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_004, TestSize.Level0)
233 {
234 AotCompilerImpl &aotImpl = AotCompilerImplMock::GetInstance();
235 int32_t ret = aotImpl.StopAotCompiler();
236 EXPECT_EQ(ret, ERR_AOT_COMPILER_STOP_FAILED);
237 }
238
239 /**
240 * @tc.name: AotCompilerImplTest_005
241 * @tc.desc: AotCompilerImpl::GetAOTVersion(std::string& sigData)
242 * @tc.type: Func
243 */
244 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_005, TestSize.Level0)
245 {
246 AotCompilerImpl &aotImpl = AotCompilerImplMock::GetInstance();
247 std::string sigData = "sig_data_for_test";
248 int32_t ret = aotImpl.GetAOTVersion(sigData);
249 EXPECT_EQ(ret, ERR_OK);
250 }
251
252 /**
253 * @tc.name: AotCompilerImplTest_006
254 * @tc.desc: AotCompilerImpl::NeedReCompile(const std::string& args, bool& sigData)
255 * @tc.type: Func
256 */
257 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_006, TestSize.Level0)
258 {
259 AotCompilerImpl &aotImpl = AotCompilerImplMock::GetInstance();
260 std::string args = "args_for_test";
261 bool sigData = true;
262 int32_t ret = aotImpl.NeedReCompile(args, sigData);
263 EXPECT_EQ(ret, ERR_OK);
264 }
265
266 /**
267 * @tc.name: AotCompilerImplTest_007
268 * @tc.desc: AotCompilerImpl::HandlePowerDisconnected()
269 * @tc.type: Func
270 */
271 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_007, TestSize.Level0)
272 {
273 AotCompilerImpl &aotImpl = AotCompilerImplMock::GetInstance();
274 bool viewData1 = true;
275 int32_t viewData2 = 101010;
276 std::string viewData3 = "101010";
277 aotImpl.HandlePowerDisconnected();
278 EXPECT_TRUE(viewData1);
279 EXPECT_EQ(viewData2, 101010);
280 EXPECT_STREQ(viewData3.c_str(), "101010");
281 }
282
283 /**
284 * @tc.name: AotCompilerImplTest_008
285 * @tc.desc: AotCompilerImpl::HandleScreenOn()
286 * @tc.type: Func
287 */
288 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_008, TestSize.Level0)
289 {
290 AotCompilerImpl &aotImpl = AotCompilerImplMock::GetInstance();
291 bool viewData1 = true;
292 int32_t viewData2 = 010101;
293 std::string viewData3 = "010101";
294 aotImpl.HandleScreenOn();
295 EXPECT_TRUE(viewData1);
296 EXPECT_EQ(viewData2, 010101);
297 EXPECT_STREQ(viewData3.c_str(), "010101");
298 }
299
300 /**
301 * @tc.name: AotCompilerImplTest_009
302 * @tc.desc: AotCompilerImpl::HandleThermalLevelChanged()
303 * @tc.type: Func
304 */
305 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_009, TestSize.Level0)
306 {
307 AotCompilerImpl &aotImpl = AotCompilerImplMock::GetInstance();
308 bool viewData1 = true;
309 int32_t viewData2 = 010101;
310 std::string viewData3 = "010101";
311 aotImpl.HandleThermalLevelChanged(1);
312 EXPECT_TRUE(viewData1);
313 EXPECT_EQ(viewData2, 010101);
314 EXPECT_STREQ(viewData3.c_str(), "010101");
315 }
316
317 /**
318 * @tc.name: AotCompilerImplTest_010
319 * @tc.desc: AotCompilerImpl::EcmascriptAotCompiler() when multi thread run
320 * @tc.type: Func
321 */
322 AotCompilerImpl >AotImpl = AotCompilerImplMock::GetInstance();
323 std::mutex aotCompilerMutex_;
324 bool g_retGlobal = true;
325
ExecuteAotCompilerTask(void)326 void ExecuteAotCompilerTask(void)
327 {
328 std::unordered_map<std::string, std::string> argsMap(argsMapForTest);
329 std::string keyCompileNoMethod = "compiler-methods-range";
330 std::string valueCompileNoMethod = "0:0";
331 std::string keyCheckPgoVersion = "compiler-check-pgo-version";
332 std::string valueCheckPgoVersion = "true";
333 argsMap.emplace(keyCompileNoMethod, valueCompileNoMethod);
334 argsMap.emplace(keyCheckPgoVersion, valueCheckPgoVersion);
335 std::vector<int16_t> sigData { 0, 1, 2, 3, 4, 5 };
336 {
337 std::lock_guard<std::mutex> lock(aotCompilerMutex_);
338 if (gtAotImpl.EcmascriptAotCompiler(argsMap, sigData) !=
339 ERR_AOT_COMPILER_SIGNATURE_DISABLE) {
340 g_retGlobal = false;
341 }
342 }
343 }
344
StopAotCompilerTask(void)345 void StopAotCompilerTask(void)
346 {
347 (void)gtAotImpl.StopAotCompiler();
348 }
349
350 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_010, TestSize.Level0)
351 {
352 g_retGlobal = true;
353 SET_THREAD_NUM(20); // THREAD_NUM = 20
354 GTEST_RUN_TASK(ExecuteAotCompilerTask);
355 GTEST_RUN_TASK(StopAotCompilerTask);
356 #ifdef CODE_SIGN_ENABLE
357 EXPECT_FALSE(g_retGlobal);
358 #else
359 EXPECT_TRUE(g_retGlobal);
360 #endif
361 }
362
363 /**
364 * @tc.name: AotCompilerImplTest_011
365 * @tc.desc: AotCompilerImpl::StopAotCompiler() while EcmascriptAotCompiler() is
366 * running regarding multi threads.
367 * @tc.type: Func
368 */
369 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_011, TestSize.Level0)
370 {
371 g_retGlobal = true;
372 SET_THREAD_NUM(40); // THREAD_NUM = 40
373 MTEST_ADD_TASK(RANDOM_THREAD_ID, ExecuteAotCompilerTask);
374 MTEST_ADD_TASK(RANDOM_THREAD_ID, StopAotCompilerTask);
375 EXPECT_TRUE(g_retGlobal);
376 }
377
378 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_012, TestSize.Level0)
379 {
380 g_retGlobal = true;
381 MTEST_POST_RUN();
382 #ifdef CODE_SIGN_ENABLE
383 EXPECT_FALSE(g_retGlobal);
384 #else
385 EXPECT_TRUE(g_retGlobal);
386 #endif
387 }
388
389 /**
390 * @tc.name: AotCompilerImplTest_013
391 * @tc.desc: AotCompilerImpl::PrintAOTCompilerResult(TEST_ERR_AN_EMPTY)
392 * @tc.type: Func
393 */
394 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_013, TestSize.Level0)
395 {
396 AotCompilerImplMock aotImplMock;
397 int32_t ret = aotImplMock.PrintAOTCompilerResultMock(TEST_ERR_OTHERS);
398 EXPECT_EQ(ret, ERR_AOT_COMPILER_CALL_FAILED);
399 ret = aotImplMock.PrintAOTCompilerResultMock(TEST_ERR_AN_EMPTY);
400 EXPECT_EQ(ret, ERR_AOT_COMPILER_CALL_FAILED);
401 ret = aotImplMock.PrintAOTCompilerResultMock(TEST_ERR_OK);
402 EXPECT_EQ(ret, ERR_OK);
403 }
404
405 /**
406 * @tc.name: AotCompilerImplTest_014
407 * @tc.desc: AotCompilerImpl::EcmascriptAotCompiler(argsMap, sigData)
408 * @tc.type: Func
409 */
410 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_014, TestSize.Level0)
411 {
412 AotCompilerImplMock aotImplMock;
413 std::unordered_map<std::string, std::string> argsMap;
414 std::vector<int16_t> sigData;
415 int32_t ret = ERR_FAIL;
416 #ifdef CODE_SIGN_ENABLE
417 aotImplMock.PauseAotCompilerMock();
418 ret = aotImplMock.EcmascriptAotCompiler(argsMap, sigData);
419 EXPECT_EQ(ret, ERR_AOT_COMPILER_CALL_CANCELLED);
420
421 aotImplMock.AllowAotCompilerMock();
422 ret = aotImplMock.EcmascriptAotCompiler(argsMap, sigData);
423 EXPECT_EQ(ret, ERR_AOT_COMPILER_PARAM_FAILED);
424
425 aotImplMock.AllowAotCompilerMock();
426 ret = aotImplMock.EcmascriptAotCompiler(argsMapForTest, sigData);
427 EXPECT_NE(ret, ERR_AOT_COMPILER_SIGNATURE_DISABLE);
428 #else
429 ret = aotImplMock.EcmascriptAotCompiler(argsMapForTest, sigData);
430 EXPECT_EQ(ret, ERR_AOT_COMPILER_SIGNATURE_DISABLE);
431 #endif
432 EXPECT_TRUE(sigData.empty());
433 }
434
435 /**
436 * @tc.name: AotCompilerImplTest_015
437 * @tc.desc: AotCompilerImpl::AOTLocalCodeSign(sigData)
438 * @tc.type: Func
439 */
440 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_015, TestSize.Level0)
441 {
442 AotCompilerImplMock aotImplMock;
443 std::string fileName = "/data/local/ark-cache/com.ohos.contacts/arm64/entry.an";
444 std::string appSignature = "5765880207853624761";
445 std::vector<int16_t> sigData;
446 std::unique_ptr<AOTArgsHandler> argsHandler = std::make_unique<AOTArgsHandler>(argsMapForTest);
447 int32_t ret = argsHandler->Handle(0);
448 EXPECT_EQ(ret, ERR_OK);
449 aotImplMock.SetAOTArgsHandler(std::move(argsHandler));
450 ret = aotImplMock.AOTLocalCodeSignMock(sigData);
451 #ifdef CODE_SIGN_ENABLE
452 EXPECT_NE(ret, ERR_AOT_COMPILER_SIGNATURE_DISABLE);
453 #else
454 EXPECT_EQ(ret, ERR_AOT_COMPILER_SIGNATURE_DISABLE);
455 #endif
456 }
457
458 /**
459 * @tc.name: AotCompilerImplTest_016
460 * @tc.desc: AotCompilerImpl::StopAotCompiler()
461 * @tc.type: Func
462 */
463 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_016, TestSize.Level0)
464 {
465 AotCompilerImplMock aotImplMock;
466 std::unique_ptr<AOTArgsHandler> argsHandler = std::make_unique<AOTArgsHandler>(argsMapForTest);
467 int32_t retHandle = argsHandler->Handle(0);
468 EXPECT_EQ(retHandle, ERR_OK);
469 aotImplMock.SetAOTArgsHandler(std::move(argsHandler));
470 aotImplMock.ResetStateMock();
471 int32_t ret = aotImplMock.StopAotCompiler();
472 EXPECT_EQ(ret, ERR_AOT_COMPILER_STOP_FAILED);
473
474 aotImplMock.InitStateMock(-1);
475 ret = aotImplMock.StopAotCompiler();
476 EXPECT_EQ(ret, ERR_AOT_COMPILER_STOP_FAILED);
477
478 aotImplMock.InitStateMock(123456789); // test_childPid = 123456789
479 ret = aotImplMock.StopAotCompiler();
480 EXPECT_EQ(ret, ERR_AOT_COMPILER_STOP_FAILED);
481 }
482
483 /**
484 * @tc.name: AotCompilerImplTest_017
485 * @tc.desc: AotCompilerImpl::HandlePowerDisconnected()
486 * @tc.type: Func
487 */
488 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_017, TestSize.Level0)
489 {
490 bool viewData = true;
491 AotCompilerImplMock aotImplMock;
492 aotImplMock.HandlePowerDisconnected();
493 EXPECT_TRUE(viewData);
494 }
495
496 /**
497 * @tc.name: AotCompilerImplTest_018
498 * @tc.desc: AotCompilerImpl::HandleScreenOn()
499 * @tc.type: Func
500 */
501 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_018, TestSize.Level0)
502 {
503 bool viewData = true;
504 AotCompilerImplMock aotImplMock;
505 aotImplMock.HandleScreenOn();
506 EXPECT_TRUE(viewData);
507 }
508
509 /**
510 * @tc.name: AotCompilerImplTest_019
511 * @tc.desc: AotCompilerImpl::HandleThermalLevelChanged(aotImpl.AOT_COMPILE_STOP_LEVEL)
512 * @tc.type: Func
513 */
514 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_019, TestSize.Level0)
515 {
516 bool viewData = true;
517 AotCompilerImplMock aotImplMock;
518 aotImplMock.HandleThermalLevelChanged(aotImplMock.AOT_COMPILE_STOP_LEVEL);
519 aotImplMock.HandleThermalLevelChanged(aotImplMock.AOT_COMPILE_STOP_LEVEL - 1);
520 EXPECT_TRUE(viewData);
521 }
522
523 /**
524 * @tc.name: AotCompilerImplTest_020
525 * @tc.desc: AotCompilerImpl::ExecuteInParentProcess(const pid_t childPid, int32_t &ret)
526 * child process terminate with signal SIGKILL;
527 * parent process receive SIGKILL signal;
528 * @tc.type: Func
529 */
530 AotCompilerImplMock g_aotImplMock;
531 int32_t g_aotRet = INVALID_ERR_CODE;
532
RunAotCompilerTask(void)533 void RunAotCompilerTask(void)
534 {
535 std::unordered_map<std::string, std::string> argsMap(argsMapForTest);
536 std::vector<int16_t> sigData;
537 {
538 std::lock_guard<std::mutex> lock(aotCompilerMutex_);
539 std::unique_ptr<AOTArgsHandler> argsHandler = std::make_unique<AOTArgsHandler>(argsMapForTest);
540 int32_t retHandle = argsHandler->Handle(0);
541 EXPECT_EQ(retHandle, ERR_OK);
542 g_aotImplMock.SetAOTArgsHandler(std::move(argsHandler));
543 int32_t aotRet = g_aotImplMock.EcmascriptAotCompilerMock(argsMap, sigData);
544 if (aotRet == ERR_AOT_COMPILER_CALL_CRASH ||
545 aotRet == ERR_AOT_COMPILER_CALL_CANCELLED ||
546 aotRet == ERR_AOT_COMPILER_SIGNATURE_DISABLE) {
547 g_aotRet = aotRet;
548 }
549 }
550 }
551
CancelAotCompilerTask(void)552 void CancelAotCompilerTask(void)
553 {
554 sleep(1); // 1: delay 1s
555 pid_t childPid = g_aotImplMock.GetChildPidMock();
556 if (childPid > 0) {
557 g_aotImplMock.InitStateMock(childPid);
558 (void)g_aotImplMock.StopAotCompiler();
559 }
560 }
561
TestCancelAotCompilerTask()562 void TestCancelAotCompilerTask()
563 {
564 std::thread([]() {
565 RunAotCompilerTask();
566 }).detach();
567 std::thread([]() {
568 CancelAotCompilerTask();
569 }).detach();
570 sleep(3); // 3: delay 3s
571 }
572
573 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_020, TestSize.Level0)
574 {
575 g_aotRet = INVALID_ERR_CODE;
576 TestCancelAotCompilerTask();
577 #ifdef CODE_SIGN_ENABLE
578 EXPECT_EQ(g_aotRet, ERR_AOT_COMPILER_CALL_CANCELLED);
579 #else
580 EXPECT_EQ(g_aotRet, ERR_AOT_COMPILER_SIGNATURE_DISABLE);
581 #endif
582 }
583
584 /**
585 * @tc.name: AotCompilerImplTest_021
586 * @tc.desc: AotCompilerImpl::ParseArkCacheFromArgs()
587 * @tc.type: Func
588 */
589 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_021, TestSize.Level0)
590 {
591 std::string pkgInfoValue =
592 "{\"abcName\":\"ets/modules.abc\","
593 "\"pgoDir\":\"/data/app/el1/100/aot_compiler/ark_profile/com.ohos.contacts\","
594 "\"processUid\":\"0xbf4\"}";
595 std::unordered_map<std::string, std::string> argsMap {
596 {"aot-file", "/data/app/el1/public/aot_compiler/ark_cache/com.ohos.contacts/arm64/entry"},
597 {"compiler-pkg-info", pkgInfoValue},
598 {"appIdentifier", "5765880207853624761"}
599 };
600 AotCompilerImpl &aotImpl = AotCompilerImplMock::GetInstance();
601 std::string arkCachePath = aotImpl.ParseArkCacheFromArgs(argsMap);
602 EXPECT_EQ(arkCachePath, "/data/app/el1/public/aot_compiler/ark_cache/com.ohos.contacts/arm64");
603 }
604
605 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_022, TestSize.Level0)
606 {
607 std::unordered_map<std::string, std::string> argsMap;
608 AotCompilerImpl &aotImpl = AotCompilerImplMock::GetInstance();
609 std::string arkCachePath = aotImpl.ParseArkCacheFromArgs(argsMap);
610 EXPECT_TRUE(arkCachePath.empty());
611 }
612
613 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_023, TestSize.Level0)
614 {
615 std::string pkgInfoValue =
616 "{\"abcName\":\"ets/modules.abc\","
617 "\"pgoDir\":\"/data/app/el1/100/aot_compiler/ark_profile/com.ohos.contacts\","
618 "\"processUid\":\"0xbf4\"}";
619 std::unordered_map<std::string, std::string> argsMap {
620 {"aot-file", "/data/app/el1/public/aot_compiler/ark_cache/com.ohos.contacts/arm64/entry"},
621 {"compiler-pkg-info", pkgInfoValue},
622 {"appIdentifier", "5765880207853624761"}
623 };
624 AotCompilerImpl &aotImpl = AotCompilerImplMock::GetInstance();
625 int32_t result = aotImpl.SendSysEvent(argsMap);
626 EXPECT_EQ(result, 0);
627 }
628 } // namespace OHOS::ArkCompiler