• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
FindArgsIdxToIntegerMock(const std::unordered_map<std::string,std::string> & argsMap,const std::string & keyName,int32_t & bundleID)82     int32_t FindArgsIdxToIntegerMock(const std::unordered_map<std::string, std::string> &argsMap,
83         const std::string &keyName, int32_t &bundleID)
84     {
85         return FindArgsIdxToInteger(argsMap, keyName, bundleID);
86     }
87 
FindArgsIdxToStringMock(const std::unordered_map<std::string,std::string> & argsMap,const std::string & keyName,std::string & bundleArg)88     int32_t FindArgsIdxToStringMock(const std::unordered_map<std::string, std::string> &argsMap,
89         const std::string &keyName, std::string &bundleArg)
90     {
91         return FindArgsIdxToString(argsMap, keyName, bundleArg);
92     }
93 
PrepareArgsMock(const std::unordered_map<std::string,std::string> & argsMap)94     int32_t PrepareArgsMock(const std::unordered_map<std::string, std::string> &argsMap)
95     {
96         return PrepareArgs(argsMap);
97     }
98 
PrintAOTCompilerResultMock(const int compilerStatus) const99     int32_t PrintAOTCompilerResultMock(const int compilerStatus) const
100     {
101         return PrintAOTCompilerResult(compilerStatus);
102     }
103 
AddExpandArgsMock(std::vector<std::string> & argVector)104     void AddExpandArgsMock(std::vector<std::string> &argVector)
105     {
106         AddExpandArgs(argVector);
107     }
108 
EcmascriptAotCompilerMock(const std::unordered_map<std::string,std::string> & argsMap,std::vector<int16_t> & sigData)109     int32_t EcmascriptAotCompilerMock(const std::unordered_map<std::string, std::string> &argsMap,
110                                       std::vector<int16_t> &sigData)
111     {
112     #ifdef CODE_SIGN_ENABLE
113         if (!allowAotCompiler_) {
114             return ERR_AOT_COMPILER_CALL_CANCELLED;
115         }
116         if (argsMap.empty() || (PrepareArgs(argsMap) != ERR_OK)) {
117             return ERR_AOT_COMPILER_PARAM_FAILED;
118         }
119         int32_t ret = ERR_OK;
120         pid_t pid = fork();
121         if (pid == -1) {
122             return ERR_AOT_COMPILER_CALL_FAILED;
123         } else if (pid == 0) {
124             DropCapabilities();
125             sleep(2);  // 2: mock ark aot compiler run time with 2s
126             ExecuteInChildProcess();
127         } else {
128             mockChildPid_ = pid;
129             ExecuteInParentProcess(pid, ret);
130         }
131         if (ret == ERR_OK_NO_AOT_FILE) {
132             return ERR_OK;
133         }
134         return ret != ERR_OK ? ret : AOTLocalCodeSign(sigData);
135     #else
136         return ERR_AOT_COMPILER_SIGNATURE_DISABLE;
137     #endif
138     }
139 
AOTLocalCodeSignMock(std::vector<int16_t> & sigData) const140     int32_t AOTLocalCodeSignMock(std::vector<int16_t> &sigData) const
141     {
142         return AOTLocalCodeSign(sigData);
143     }
144 
InitStateMock(const pid_t childPid)145     void InitStateMock(const pid_t childPid)
146     {
147         InitState(childPid);
148     }
149 
ResetStateMock()150     void ResetStateMock()
151     {
152         ResetState();
153     }
154 
PauseAotCompilerMock()155     void PauseAotCompilerMock()
156     {
157         PauseAotCompiler();
158     }
159 
AllowAotCompilerMock()160     void AllowAotCompilerMock()
161     {
162         AllowAotCompiler();
163     }
164 
GetChildPidMock()165     pid_t GetChildPidMock()
166     {
167         return mockChildPid_;
168     }
169 
170 private:
171     pid_t mockChildPid_ = -1;
172 };
173 
174 class AotCompilerImplTest : public testing::Test {
175 public:
AotCompilerImplTest()176     AotCompilerImplTest() {}
~AotCompilerImplTest()177     virtual ~AotCompilerImplTest() {}
178 
SetUpTestCase()179     static void SetUpTestCase() {}
TearDownTestCase()180     static void TearDownTestCase() {}
SetUp()181     void SetUp() override {}
TearDown()182     void TearDown() override {}
183 };
184 
185 /**
186  * @tc.name: AotCompilerImplTest_001
187  * @tc.desc: AotCompilerImpl::GetInstance()
188  * @tc.type: Func
189 */
190 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_001, TestSize.Level0)
191 {
192     AotCompilerImpl *aotImplPtr = nullptr;
193     aotImplPtr = &AotCompilerImplMock::GetInstance();
194     EXPECT_NE(aotImplPtr, nullptr);
195 }
196 
197 /**
198  * @tc.name: AotCompilerImplTest_002
199  * @tc.desc: AotCompilerImpl::EcmascriptAotCompiler() when compiler-check-pgo-version
200  * @tc.type: Func
201 */
202 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_002, TestSize.Level0)
203 {
204     AotCompilerImpl &aotImpl = AotCompilerImplMock::GetInstance();
205     std::unordered_map<std::string, std::string> argsMap(argsMapForTest);
206     std::string keyCheckPgoVersion = "compiler-check-pgo-version";
207     std::string valueCheckPgoVersion = "true";
208     argsMap.emplace(keyCheckPgoVersion, valueCheckPgoVersion);
209     std::vector<int16_t> sigData;
210     int32_t ret = aotImpl.EcmascriptAotCompiler(argsMap, sigData);
211 #ifdef CODE_SIGN_ENABLE
212     EXPECT_NE(ret, ERR_AOT_COMPILER_SIGNATURE_DISABLE);
213 #else
214     EXPECT_EQ(ret, ERR_AOT_COMPILER_SIGNATURE_DISABLE);
215 #endif
216     EXPECT_TRUE(sigData.empty());
217 }
218 
219 /**
220  * @tc.name: AotCompilerImplTest_003
221  * @tc.desc: AotCompilerImpl::EcmascriptAotCompiler() when compile not any method
222  * @tc.type: Func
223 */
224 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_003, TestSize.Level0)
225 {
226     AotCompilerImpl &aotImpl = AotCompilerImplMock::GetInstance();
227     std::unordered_map<std::string, std::string> argsMap(argsMapForTest);
228     std::string keyCompileNoMethod = "compiler-methods-range";
229     std::string valueCompileNoMethod = "0:0";
230     argsMap.emplace(keyCompileNoMethod, valueCompileNoMethod);
231     std::vector<int16_t> sigData { 0, 1, 2, 3, 4, 5 };
232     int32_t ret = aotImpl.EcmascriptAotCompiler(argsMap, sigData);
233 #ifdef CODE_SIGN_ENABLE
234     EXPECT_NE(ret, ERR_AOT_COMPILER_SIGNATURE_DISABLE);
235 #else
236     EXPECT_EQ(ret, ERR_AOT_COMPILER_SIGNATURE_DISABLE);
237 #endif
238     EXPECT_FALSE(sigData.empty());
239 }
240 
241 /**
242  * @tc.name: AotCompilerImplTest_004
243  * @tc.desc: AotCompilerImpl::StopAotCompiler()
244  * @tc.type: Func
245 */
246 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_004, TestSize.Level0)
247 {
248     AotCompilerImpl &aotImpl = AotCompilerImplMock::GetInstance();
249     int32_t ret = aotImpl.StopAotCompiler();
250     EXPECT_EQ(ret, ERR_AOT_COMPILER_STOP_FAILED);
251 }
252 
253 /**
254  * @tc.name: AotCompilerImplTest_005
255  * @tc.desc: AotCompilerImpl::GetAOTVersion(std::string& sigData)
256  * @tc.type: Func
257 */
258 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_005, TestSize.Level0)
259 {
260     AotCompilerImpl &aotImpl = AotCompilerImplMock::GetInstance();
261     std::string sigData = "sig_data_for_test";
262     int32_t ret = aotImpl.GetAOTVersion(sigData);
263     EXPECT_EQ(sigData.size(), 0);
264     EXPECT_EQ(ret, ERR_OK);
265 }
266 
267 /**
268  * @tc.name: AotCompilerImplTest_006
269  * @tc.desc: AotCompilerImpl::NeedReCompile(const std::string& args, bool& sigData)
270  * @tc.type: Func
271 */
272 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_006, TestSize.Level0)
273 {
274     AotCompilerImpl &aotImpl = AotCompilerImplMock::GetInstance();
275     std::string args = "args_for_test";
276     bool sigData = true;
277     int32_t ret = aotImpl.NeedReCompile(args, sigData);
278     EXPECT_FALSE(sigData);
279     EXPECT_EQ(ret, ERR_OK);
280 }
281 
282 /**
283  * @tc.name: AotCompilerImplTest_007
284  * @tc.desc: AotCompilerImpl::HandlePowerDisconnected()
285  * @tc.type: Func
286 */
287 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_007, TestSize.Level0)
288 {
289     AotCompilerImpl &aotImpl = AotCompilerImplMock::GetInstance();
290     bool viewData1 = true;
291     int32_t viewData2 = 101010;
292     std::string viewData3 = "101010";
293     aotImpl.HandlePowerDisconnected();
294     EXPECT_TRUE(viewData1);
295     EXPECT_EQ(viewData2, 101010);
296     EXPECT_STREQ(viewData3.c_str(), "101010");
297 }
298 
299 /**
300  * @tc.name: AotCompilerImplTest_008
301  * @tc.desc: AotCompilerImpl::HandleScreenOn()
302  * @tc.type: Func
303 */
304 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_008, TestSize.Level0)
305 {
306     AotCompilerImpl &aotImpl = AotCompilerImplMock::GetInstance();
307     bool viewData1 = true;
308     int32_t viewData2 = 010101;
309     std::string viewData3 = "010101";
310     aotImpl.HandleScreenOn();
311     EXPECT_TRUE(viewData1);
312     EXPECT_EQ(viewData2, 010101);
313     EXPECT_STREQ(viewData3.c_str(), "010101");
314 }
315 
316 /**
317  * @tc.name: AotCompilerImplTest_009
318  * @tc.desc: AotCompilerImpl::HandleThermalLevelChanged()
319  * @tc.type: Func
320 */
321 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_009, TestSize.Level0)
322 {
323     AotCompilerImpl &aotImpl = AotCompilerImplMock::GetInstance();
324     bool viewData1 = true;
325     int32_t viewData2 = 010101;
326     std::string viewData3 = "010101";
327     aotImpl.HandleThermalLevelChanged(1);
328     EXPECT_TRUE(viewData1);
329     EXPECT_EQ(viewData2, 010101);
330     EXPECT_STREQ(viewData3.c_str(), "010101");
331 }
332 
333 /**
334  * @tc.name: AotCompilerImplTest_010
335  * @tc.desc: AotCompilerImpl::EcmascriptAotCompiler() when multi thread run
336  * @tc.type: Func
337 */
338 AotCompilerImpl &gtAotImpl = AotCompilerImplMock::GetInstance();
339 std::mutex aotCompilerMutex_;
340 bool g_retGlobal = true;
341 
ExecuteAotCompilerTask(void)342 void ExecuteAotCompilerTask(void)
343 {
344     std::unordered_map<std::string, std::string> argsMap(argsMapForTest);
345     std::string keyCompileNoMethod = "compiler-methods-range";
346     std::string valueCompileNoMethod = "0:0";
347     std::string keyCheckPgoVersion = "compiler-check-pgo-version";
348     std::string valueCheckPgoVersion = "true";
349     argsMap.emplace(keyCompileNoMethod, valueCompileNoMethod);
350     argsMap.emplace(keyCheckPgoVersion, valueCheckPgoVersion);
351     std::vector<int16_t> sigData { 0, 1, 2, 3, 4, 5 };
352     {
353         std::lock_guard<std::mutex> lock(aotCompilerMutex_);
354         if (gtAotImpl.EcmascriptAotCompiler(argsMap, sigData) !=
355             ERR_AOT_COMPILER_SIGNATURE_DISABLE) {
356             g_retGlobal = false;
357         }
358     }
359 }
360 
StopAotCompilerTask(void)361 void StopAotCompilerTask(void)
362 {
363     (void)gtAotImpl.StopAotCompiler();
364 }
365 
366 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_010, TestSize.Level0)
367 {
368     g_retGlobal = true;
369     SET_THREAD_NUM(20);  // THREAD_NUM = 20
370     GTEST_RUN_TASK(ExecuteAotCompilerTask);
371     GTEST_RUN_TASK(StopAotCompilerTask);
372 #ifdef CODE_SIGN_ENABLE
373     EXPECT_FALSE(g_retGlobal);
374 #else
375     EXPECT_TRUE(g_retGlobal);
376 #endif
377 }
378 
379 /**
380  * @tc.name: AotCompilerImplTest_011
381  * @tc.desc: AotCompilerImpl::StopAotCompiler() while EcmascriptAotCompiler() is
382  *           running regarding multi threads.
383  * @tc.type: Func
384 */
385 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_011, TestSize.Level0)
386 {
387     g_retGlobal = true;
388     SET_THREAD_NUM(40); // THREAD_NUM = 40
389     MTEST_ADD_TASK(RANDOM_THREAD_ID, ExecuteAotCompilerTask);
390     MTEST_ADD_TASK(RANDOM_THREAD_ID, StopAotCompilerTask);
391     EXPECT_TRUE(g_retGlobal);
392 }
393 
394 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_012, TestSize.Level0)
395 {
396     g_retGlobal = true;
397     MTEST_POST_RUN();
398 #ifdef CODE_SIGN_ENABLE
399     EXPECT_FALSE(g_retGlobal);
400 #else
401     EXPECT_TRUE(g_retGlobal);
402 #endif
403 }
404 
405 /**
406  * @tc.name: AotCompilerImplTest_013
407  * @tc.desc: AotCompilerImpl::FindArgsIdxToInteger(argsMap, keyName, bundleID)
408  * @tc.type: Func
409 */
410 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_013, TestSize.Level0)
411 {
412     AotCompilerImplMock aotImplMock;
413     std::unordered_map<std::string, std::string> argsMap;
414     std::string keyName = "compiler-pkg-info";
415     int32_t bundleID = 0;
416     int32_t ret = aotImplMock.FindArgsIdxToIntegerMock(argsMap, keyName, bundleID);
417     EXPECT_EQ(ret, ERR_AOT_COMPILER_PARAM_FAILED);
418 }
419 
420 /**
421  * @tc.name: AotCompilerImplTest_014
422  * @tc.desc: AotCompilerImpl::FindArgsIdxToInteger(argsMap, keyName, bundleID)
423  * @tc.type: Func
424 */
425 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_014, TestSize.Level0)
426 {
427     AotCompilerImplMock aotImplMock;
428     std::unordered_map<std::string, std::string> argsMap;
429     argsMap.emplace("argKey", "argValueNotInteger");
430     std::string keyName = "argKey";
431     int32_t bundleID = 0;
432     int32_t ret = aotImplMock.FindArgsIdxToIntegerMock(argsMap, keyName, bundleID);
433     EXPECT_EQ(ret, ERR_AOT_COMPILER_PARAM_FAILED);
434 }
435 
436 /**
437  * @tc.name: AotCompilerImplTest_015
438  * @tc.desc: AotCompilerImpl::FindArgsIdxToInteger(argsMap, keyName, bundleID)
439  * @tc.type: Func
440 */
441 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_015, TestSize.Level0)
442 {
443     AotCompilerImplMock aotImplMock;
444     std::unordered_map<std::string, std::string> argsMap;
445     argsMap.emplace("argKey", "123456_argValueNotInteger");
446     std::string keyName = "argKey";
447     int32_t bundleID = 0;
448     int32_t ret = aotImplMock.FindArgsIdxToIntegerMock(argsMap, keyName, bundleID);
449     EXPECT_EQ(ret, ERR_AOT_COMPILER_PARAM_FAILED);
450 }
451 
452 /**
453  * @tc.name: AotCompilerImplTest_016
454  * @tc.desc: AotCompilerImpl::FindArgsIdxToInteger(argsMap, keyName, bundleID)
455  * @tc.type: Func
456 */
457 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_016, TestSize.Level0)
458 {
459     AotCompilerImplMock aotImplMock;
460     std::unordered_map<std::string, std::string> argsMap;
461     argsMap.emplace("argKey", "20020079");
462     std::string keyName = "argKey";
463     int32_t bundleID = 0;
464     int32_t ret = aotImplMock.FindArgsIdxToIntegerMock(argsMap, keyName, bundleID);
465     EXPECT_EQ(ret, ERR_OK);
466     EXPECT_EQ(bundleID, 20020079);
467 }
468 
469 /**
470  * @tc.name: AotCompilerImplTest_017
471  * @tc.desc: AotCompilerImpl::FindArgsIdxToString(argsMap, keyName, bundleArg)
472  * @tc.type: Func
473 */
474 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_017, TestSize.Level0)
475 {
476     AotCompilerImplMock aotImplMock;
477     std::unordered_map<std::string, std::string> argsMap;
478     std::string keyName = "argKey";
479     std::string bundleArg = "";
480     int32_t ret = aotImplMock.FindArgsIdxToStringMock(argsMap, keyName, bundleArg);
481     EXPECT_EQ(ret, ERR_AOT_COMPILER_PARAM_FAILED);
482 }
483 
484 /**
485  * @tc.name: AotCompilerImplTest_018
486  * @tc.desc: AotCompilerImpl::FindArgsIdxToString(argsMap, keyName, bundleArg)
487  * @tc.type: Func
488 */
489 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_018, TestSize.Level0)
490 {
491     AotCompilerImplMock aotImplMock;
492     std::unordered_map<std::string, std::string> argsMap;
493     argsMap.emplace("argKey", "com.ohos.contacts");
494     std::string keyName = "argKey";
495     std::string bundleArg = "";
496     int32_t ret = aotImplMock.FindArgsIdxToStringMock(argsMap, keyName, bundleArg);
497     EXPECT_EQ(ret, ERR_OK);
498     EXPECT_STREQ(bundleArg.c_str(), "com.ohos.contacts");
499 }
500 
501 /**
502  * @tc.name: AotCompilerImplTest_019
503  * @tc.desc: AotCompilerImpl::PrepareArgs(argsMapForTest)
504  * @tc.type: Func
505 */
506 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_019, TestSize.Level0)
507 {
508     AotCompilerImplMock aotImplMock;
509     std::unordered_map<std::string, std::string> argsMap;
510     int32_t ret = aotImplMock.PrepareArgsMock(argsMap);
511     EXPECT_EQ(ret, ERR_AOT_COMPILER_PARAM_FAILED);
512     ret = aotImplMock.PrepareArgsMock(argsMapForTest);
513     EXPECT_EQ(ret, ERR_OK);
514 }
515 
516 /**
517  * @tc.name: AotCompilerImplTest_020
518  * @tc.desc: AotCompilerImpl::AddExpandArgs(aotVector)
519  * @tc.type: Func
520 */
521 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_020, TestSize.Level0)
522 {
523     bool viewData = true;
524     AotCompilerImplMock aotImplMock;
525     std::vector<std::string> aotVector;
526     aotImplMock.AddExpandArgsMock(aotVector);
527     EXPECT_TRUE(viewData);
528 }
529 
530 /**
531  * @tc.name: AotCompilerImplTest_021
532  * @tc.desc: AotCompilerImpl::PrintAOTCompilerResult(TEST_ERR_AN_EMPTY)
533  * @tc.type: Func
534 */
535 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_021, TestSize.Level0)
536 {
537     AotCompilerImplMock aotImplMock;
538     int32_t ret = aotImplMock.PrintAOTCompilerResultMock(TEST_ERR_OTHERS);
539     EXPECT_EQ(ret, ERR_AOT_COMPILER_CALL_FAILED);
540     ret = aotImplMock.PrintAOTCompilerResultMock(TEST_ERR_AN_EMPTY);
541     EXPECT_EQ(ret, ERR_AOT_COMPILER_CALL_FAILED);
542     ret = aotImplMock.PrintAOTCompilerResultMock(TEST_ERR_OK);
543     EXPECT_EQ(ret, ERR_OK);
544 }
545 
546 /**
547  * @tc.name: AotCompilerImplTest_022
548  * @tc.desc: AotCompilerImpl::EcmascriptAotCompiler(argsMap, sigData)
549  * @tc.type: Func
550 */
551 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_022, TestSize.Level0)
552 {
553     AotCompilerImplMock aotImplMock;
554     std::unordered_map<std::string, std::string> argsMap;
555     std::vector<int16_t> sigData;
556     int32_t ret = ERR_FAIL;
557 #ifdef CODE_SIGN_ENABLE
558     aotImplMock.PauseAotCompilerMock();
559     ret = aotImplMock.EcmascriptAotCompiler(argsMap, sigData);
560     EXPECT_EQ(ret, ERR_AOT_COMPILER_CALL_CANCELLED);
561 
562     aotImplMock.AllowAotCompilerMock();
563     ret = aotImplMock.EcmascriptAotCompiler(argsMap, sigData);
564     EXPECT_EQ(ret, ERR_AOT_COMPILER_PARAM_FAILED);
565 
566     aotImplMock.AllowAotCompilerMock();
567     ret = aotImplMock.EcmascriptAotCompiler(argsMapForTest, sigData);
568     EXPECT_NE(ret, ERR_AOT_COMPILER_SIGNATURE_DISABLE);
569 #else
570     ret = aotImplMock.EcmascriptAotCompiler(argsMapForTest, sigData);
571     EXPECT_EQ(ret, ERR_AOT_COMPILER_SIGNATURE_DISABLE);
572 #endif
573     EXPECT_TRUE(sigData.empty());
574 }
575 
576 /**
577  * @tc.name: AotCompilerImplTest_023
578  * @tc.desc: AotCompilerImpl::AOTLocalCodeSign(sigData)
579  * @tc.type: Func
580 */
581 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_023, TestSize.Level0)
582 {
583     AotCompilerImplMock aotImplMock;
584     std::string fileName = "/data/app/el1/public/aot_compiler/ark_cache/com.ohos.contacts/arm64/entry.an";
585     std::string appSignature = "5765880207853624761";
586     std::vector<int16_t> sigData;
587     int32_t ret = aotImplMock.PrepareArgsMock(argsMapForTest);
588     EXPECT_EQ(ret, ERR_OK);
589     ret = aotImplMock.AOTLocalCodeSignMock(sigData);
590 #ifdef CODE_SIGN_ENABLE
591     EXPECT_NE(ret, ERR_AOT_COMPILER_SIGNATURE_DISABLE);
592 #else
593     EXPECT_EQ(ret, ERR_AOT_COMPILER_SIGNATURE_DISABLE);
594 #endif
595 }
596 
597 /**
598  * @tc.name: AotCompilerImplTest_024
599  * @tc.desc: AotCompilerImpl::StopAotCompiler()
600  * @tc.type: Func
601 */
602 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_024, TestSize.Level0)
603 {
604     AotCompilerImplMock aotImplMock;
605     aotImplMock.ResetStateMock();
606     int32_t ret = aotImplMock.StopAotCompiler();
607     EXPECT_EQ(ret, ERR_AOT_COMPILER_STOP_FAILED);
608 
609     aotImplMock.InitStateMock(-1);
610     ret = aotImplMock.StopAotCompiler();
611     EXPECT_EQ(ret, ERR_AOT_COMPILER_STOP_FAILED);
612 
613     aotImplMock.InitStateMock(123456789);   // test_childPid = 123456789
614     ret = aotImplMock.StopAotCompiler();
615     EXPECT_EQ(ret, ERR_AOT_COMPILER_STOP_FAILED);
616 }
617 
618 /**
619  * @tc.name: AotCompilerImplTest_025
620  * @tc.desc: AotCompilerImpl::HandlePowerDisconnected()
621  * @tc.type: Func
622 */
623 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_025, TestSize.Level0)
624 {
625     bool viewData = true;
626     AotCompilerImplMock aotImplMock;
627     aotImplMock.HandlePowerDisconnected();
628     EXPECT_TRUE(viewData);
629 }
630 
631 /**
632  * @tc.name: AotCompilerImplTest_026
633  * @tc.desc: AotCompilerImpl::HandleScreenOn()
634  * @tc.type: Func
635 */
636 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_026, TestSize.Level0)
637 {
638     bool viewData = true;
639     AotCompilerImplMock aotImplMock;
640     aotImplMock.HandleScreenOn();
641     EXPECT_TRUE(viewData);
642 }
643 
644 /**
645  * @tc.name: AotCompilerImplTest_027
646  * @tc.desc: AotCompilerImpl::HandleThermalLevelChanged(aotImpl.AOT_COMPILE_STOP_LEVEL)
647  * @tc.type: Func
648 */
649 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_027, TestSize.Level0)
650 {
651     bool viewData = true;
652     AotCompilerImplMock aotImplMock;
653     aotImplMock.HandleThermalLevelChanged(aotImplMock.AOT_COMPILE_STOP_LEVEL);
654     aotImplMock.HandleThermalLevelChanged(aotImplMock.AOT_COMPILE_STOP_LEVEL - 1);
655     EXPECT_TRUE(viewData);
656 }
657 
658 /**
659  * @tc.name: AotCompilerImplTest_028
660  * @tc.desc: AotCompilerImpl::ExecuteInParentProcess(const pid_t childPid, int32_t &ret)
661  *              child process terminate with signal SIGKILL;
662  *                  parent process receive SIGKILL signal;
663  * @tc.type: Func
664 */
665 AotCompilerImplMock g_aotImplMock;
666 int32_t g_aotRet = INVALID_ERR_CODE;
667 
RunAotCompilerTask(void)668 void RunAotCompilerTask(void)
669 {
670     std::unordered_map<std::string, std::string> argsMap(argsMapForTest);
671     std::vector<int16_t> sigData;
672     {
673         std::lock_guard<std::mutex> lock(aotCompilerMutex_);
674         int32_t aotRet = g_aotImplMock.EcmascriptAotCompilerMock(argsMap, sigData);
675         if (aotRet == ERR_AOT_COMPILER_CALL_CRASH     ||
676             aotRet == ERR_AOT_COMPILER_CALL_CANCELLED ||
677             aotRet == ERR_AOT_COMPILER_SIGNATURE_DISABLE) {
678             g_aotRet = aotRet;
679         }
680     }
681 }
682 
CancelAotCompilerTask(void)683 void CancelAotCompilerTask(void)
684 {
685     sleep(1); // 1: delay 1s
686     pid_t childPid = g_aotImplMock.GetChildPidMock();
687     if (childPid > 0) {
688         g_aotImplMock.InitStateMock(childPid);
689         (void)g_aotImplMock.StopAotCompiler();
690     }
691 }
692 
TestCancelAotCompilerTask()693 void TestCancelAotCompilerTask()
694 {
695     std::thread([]() {
696         RunAotCompilerTask();
697     }).detach();
698     std::thread([]() {
699         CancelAotCompilerTask();
700     }).detach();
701     sleep(3); // 3: delay 3s
702 }
703 
704 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_028, TestSize.Level0)
705 {
706     g_aotRet = INVALID_ERR_CODE;
707     TestCancelAotCompilerTask();
708 #ifdef CODE_SIGN_ENABLE
709     EXPECT_EQ(g_aotRet, ERR_AOT_COMPILER_CALL_CANCELLED);
710 #else
711     EXPECT_EQ(g_aotRet, ERR_AOT_COMPILER_SIGNATURE_DISABLE);
712 #endif
713 }
714 } // namespace OHOS::ArkCompiler