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 <gtest/gtest.h> 18 #include <string> 19 #include <vector> 20 #include <unordered_map> 21 22 #include "aot_compiler_client.h" 23 #include "aot_compiler_service.h" 24 #include "aot_compiler_error_utils.h" 25 #include "aot_compiler_impl.h" 26 #include "aot_compiler_load_callback.h" 27 #include "iservice_registry.h" 28 #include "system_ability_definition.h" 29 30 using namespace testing::ext; 31 32 namespace OHOS::ArkCompiler { 33 namespace { 34 const std::string compilerPkgInfoValue = 35 "{\"abcName\":\"ets/modules.abc\"," 36 "\"abcOffset\":\"0x1000\"," 37 "\"abcSize\":\"0x60b058\"," 38 "\"appIdentifier\":\"5765880207853624761\"," 39 "\"bundleName\":\"com.ohos.contacts\"," 40 "\"BundleUid\":\"0x1317b6f\"," 41 "\"isEncryptedBundle\":\"0x0\"," 42 "\"isScreenOff\":\"0x1\"," 43 "\"moduleName\":\"entry\"," 44 "\"pgoDir\":\"/data/local/ark-profile/100/com.ohos.contacts\"," 45 "\"pkgPath\":\"/system/app/Contacts/Contacts.hap\"," 46 "\"processUid\":\"0xbf4\"}"; 47 48 const std::unordered_map<std::string, std::string> argsMapForTest { 49 {"target-compiler-mode", "partial"}, 50 {"aot-file", "/data/local/ark-cache/com.ohos.contacts/arm64/entry"}, 51 {"compiler-pkg-info", compilerPkgInfoValue}, 52 {"compiler-external-pkg-info", "[]"}, 53 {"compiler-opt-bc-range", ""}, 54 {"compiler-device-state", "1"}, 55 {"compiler-baseline-pgo", "0"}, 56 {"ABC-Path", "/system/app/Contacts/Contacts.hap/ets/modules.abc"}, 57 {"BundleUid", "20020079"}, 58 {"BundleGid", "20020079"}, 59 {"anFileName", "/data/local/ark-cache/com.ohos.contacts/arm64/entry.an"}, 60 {"appIdentifier", "5765880207853624761"} 61 }; 62 } // namespace ark_aot_compiler arguments 63 64 class AotCompilerImplTest : public testing::Test { 65 public: AotCompilerImplTest()66 AotCompilerImplTest() {} ~AotCompilerImplTest()67 virtual ~AotCompilerImplTest() {} 68 SetUpTestCase()69 static void SetUpTestCase() {} TearDownTestCase()70 static void TearDownTestCase() {} SetUp()71 void SetUp() override {} TearDown()72 void TearDown() override {} 73 }; 74 75 /** 76 * @tc.name: AotCompilerImplTest_001 77 * @tc.desc: AotCompilerImpl::GetInstance() 78 * @tc.type: Func 79 * @tc.require: IR/AR/SR 80 */ 81 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_001, TestSize.Level0) 82 { 83 AotCompilerImpl *aotImplPtr = nullptr; 84 aotImplPtr = &AotCompilerImpl::GetInstance(); 85 EXPECT_NE(aotImplPtr, nullptr); 86 } 87 88 /** 89 * @tc.name: AotCompilerImplTest_002 90 * @tc.desc: AotCompilerImpl::EcmascriptAotCompiler() when compiler-check-pgo-version 91 * @tc.type: Func 92 * @tc.require: IR/AR/SR 93 */ 94 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_002, TestSize.Level0) 95 { 96 AotCompilerImpl &aotImpl = AotCompilerImpl::GetInstance(); 97 std::unordered_map<std::string, std::string> argsMap(argsMapForTest); 98 std::string keyCheckPgoVersion = "compiler-check-pgo-version"; 99 std::string valueCheckPgoVersion = "true"; 100 argsMap.emplace(keyCheckPgoVersion, valueCheckPgoVersion); 101 std::vector<int16_t> sigData; 102 int32_t ret = aotImpl.EcmascriptAotCompiler(argsMap, sigData); 103 #ifdef CODE_SIGN_ENABLE 104 EXPECT_NE(ret, ERR_AOT_COMPILER_SIGNATURE_DISABLE); 105 #else 106 EXPECT_EQ(ret, ERR_AOT_COMPILER_SIGNATURE_DISABLE); 107 #endif 108 EXPECT_TRUE(sigData.empty()); 109 } 110 111 /** 112 * @tc.name: AotCompilerImplTest_003 113 * @tc.desc: AotCompilerImpl::EcmascriptAotCompiler() when compile not any method 114 * @tc.type: Func 115 * @tc.require: IR/AR/SR 116 */ 117 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_003, TestSize.Level0) 118 { 119 AotCompilerImpl &aotImpl = AotCompilerImpl::GetInstance(); 120 std::unordered_map<std::string, std::string> argsMap(argsMapForTest); 121 std::string keyCompileNoMethod = "compiler-methods-range"; 122 std::string valueCompileNoMethod = "0:0"; 123 argsMap.emplace(keyCompileNoMethod, valueCompileNoMethod); 124 std::vector<int16_t> sigData { 0, 1, 2, 3, 4, 5 }; 125 int32_t ret = aotImpl.EcmascriptAotCompiler(argsMap, sigData); 126 EXPECT_NE(ret, ERR_OK); 127 EXPECT_FALSE(sigData.empty()); 128 } 129 130 /** 131 * @tc.name: AotCompilerImplTest_004 132 * @tc.desc: AotCompilerImpl::StopAotCompiler() 133 * @tc.type: Func 134 * @tc.require: IR/AR/SR 135 */ 136 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_004, TestSize.Level0) 137 { 138 AotCompilerImpl &aotImpl = AotCompilerImpl::GetInstance(); 139 int32_t ret = aotImpl.StopAotCompiler(); 140 EXPECT_EQ(ret, ERR_AOT_COMPILER_STOP_FAILED); 141 } 142 143 /** 144 * @tc.name: AotCompilerImplTest_005 145 * @tc.desc: AotCompilerImpl::GetAOTVersion(std::string& sigData) 146 * @tc.type: Func 147 * @tc.require: IR/AR/SR 148 */ 149 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_005, TestSize.Level0) 150 { 151 AotCompilerImpl &aotImpl = AotCompilerImpl::GetInstance(); 152 std::string sigData = "sig_data_for_test"; 153 int32_t ret = aotImpl.GetAOTVersion(sigData); 154 EXPECT_EQ(sigData.size(), 0); 155 EXPECT_EQ(ret, ERR_OK); 156 } 157 158 /** 159 * @tc.name: AotCompilerImplTest_006 160 * @tc.desc: AotCompilerImpl::NeedReCompile(const std::string& args, bool& sigData) 161 * @tc.type: Func 162 * @tc.require: IR/AR/SR 163 */ 164 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_006, TestSize.Level0) 165 { 166 AotCompilerImpl &aotImpl = AotCompilerImpl::GetInstance(); 167 std::string args = "args_for_test"; 168 bool sigData = true; 169 int32_t ret = aotImpl.NeedReCompile(args, sigData); 170 EXPECT_FALSE(sigData); 171 EXPECT_EQ(ret, ERR_OK); 172 } 173 174 /** 175 * @tc.name: AotCompilerImplTest_007 176 * @tc.desc: AotCompilerImpl::HandlePowerDisconnected() 177 * @tc.type: Func 178 * @tc.require: IR/AR/SR 179 */ 180 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_007, TestSize.Level0) 181 { 182 AotCompilerImpl &aotImpl = AotCompilerImpl::GetInstance(); 183 bool viewData1 = true; 184 int32_t viewData2 = 101010; 185 std::string viewData3 = "101010"; 186 aotImpl.HandlePowerDisconnected(); 187 EXPECT_TRUE(viewData1); 188 EXPECT_EQ(viewData2, 101010); 189 EXPECT_STREQ(viewData3.c_str(), "101010"); 190 } 191 192 /** 193 * @tc.name: AotCompilerImplTest_008 194 * @tc.desc: AotCompilerImpl::HandleScreenOn() 195 * @tc.type: Func 196 * @tc.require: IR/AR/SR 197 */ 198 HWTEST_F(AotCompilerImplTest, AotCompilerImplTest_008, TestSize.Level0) 199 { 200 AotCompilerImpl &aotImpl = AotCompilerImpl::GetInstance(); 201 bool viewData1 = true; 202 int32_t viewData2 = 010101; 203 std::string viewData3 = "010101"; 204 aotImpl.HandleScreenOn(); 205 EXPECT_TRUE(viewData1); 206 EXPECT_EQ(viewData2, 010101); 207 EXPECT_STREQ(viewData3.c_str(), "010101"); 208 } 209 } // namespace OHOS::ArkCompiler