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
21 #define private public
22 #define protected public
23 #include "aot_compiler_client.h"
24 #include "aot_compiler_service.h"
25 #include "aot_compiler_error_utils.h"
26 #include "aot_compiler_load_callback.h"
27 #undef protected
28 #undef private
29 #include "iservice_registry.h"
30 #include "system_ability_definition.h"
31
32 using namespace testing::ext;
33
34 namespace OHOS::ArkCompiler {
35 class AotCompilerClientTest : public testing::Test {
36 public:
AotCompilerClientTest()37 AotCompilerClientTest() {}
~AotCompilerClientTest()38 virtual ~AotCompilerClientTest() {}
39
SetUpTestCase()40 static void SetUpTestCase() {}
TearDownTestCase()41 static void TearDownTestCase() {}
SetUp()42 void SetUp() override {}
43 void TearDown() override;
44 sptr<IRemoteObject> GetAotRemoteObject(AotCompilerClient &aotClient);
45 };
46
TearDown()47 void AotCompilerClientTest::TearDown()
48 {
49 sptr<ISystemAbilityManager> samgr =
50 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
51 if (samgr != nullptr) {
52 (void)samgr->UnloadSystemAbility(AOT_COMPILER_SERVICE_ID);
53 }
54 }
55
GetAotRemoteObject(AotCompilerClient & aotClient)56 sptr<IRemoteObject> AotCompilerClientTest::GetAotRemoteObject(AotCompilerClient &aotClient)
57 {
58 (void)aotClient.GetAotCompilerProxy();
59 auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
60 if (systemAbilityMgr == nullptr) {
61 return nullptr;
62 }
63 return systemAbilityMgr->CheckSystemAbility(AOT_COMPILER_SERVICE_ID);
64 }
65
66 /**
67 * @tc.name: AotCompilerClientTest_001
68 * @tc.desc: AotCompilerClient::GetInstance()
69 * @tc.type: Func
70 */
71 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_001, TestSize.Level0)
72 {
73 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
74 EXPECT_NE(aotClient.aotCompilerDiedRecipient_, nullptr);
75
76 auto &client_1 = AotCompilerClient::GetInstance();
77 auto &client_2 = AotCompilerClient::GetInstance();
78 EXPECT_EQ(&client_1, &client_2);
79 EXPECT_EQ(client_1.aotCompilerDiedRecipient_, client_2.aotCompilerDiedRecipient_);
80 }
81
82 /**
83 * @tc.name: AotCompilerClientTest_002
84 * @tc.desc: invoke aot_compiler service in client program.
85 * @tc.type: Func
86 */
87 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_002, TestSize.Level0)
88 {
89 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
90 std::unordered_map<std::string, std::string> argsMap;
91 std::vector<int16_t> sigData;
92 int32_t ret = aotClient.AotCompiler(argsMap, sigData);
93 EXPECT_EQ(ret, ERR_AOT_COMPILER_PARAM_FAILED);
94 EXPECT_TRUE(argsMap.empty());
95 EXPECT_TRUE(sigData.empty());
96 }
97
98 /**
99 * @tc.name: AotCompilerClientTest_003
100 * @tc.desc: LoadSystemAbility(AOT_COMPILER_SERVICE_ID, loadCallback)
101 * @tc.type: Func
102 */
103 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_003, TestSize.Level0)
104 {
105 auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
106 ASSERT_NE(systemAbilityMgr, nullptr);
107
108 sptr<AotCompilerLoadCallback> loadCallback = new (std::nothrow) AotCompilerLoadCallback();
109 EXPECT_NE(loadCallback, nullptr);
110
111 auto ret = systemAbilityMgr->LoadSystemAbility(AOT_COMPILER_SERVICE_ID, loadCallback);
112 EXPECT_EQ(ret, ERR_OK);
113 }
114
115 /**
116 * @tc.name: AotCompilerClientTest_004
117 * @tc.desc: aotClient.LoadAotCompilerService()
118 * @tc.type: Func
119 */
120 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_004, TestSize.Level0)
121 {
122 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
123 aotClient.SetAotCompiler(nullptr);
124 EXPECT_EQ(aotClient.GetAotCompiler(), nullptr);
125
126 bool retLoad = aotClient.LoadAotCompilerService();
127 EXPECT_TRUE(retLoad);
128 EXPECT_NE(aotClient.GetAotCompiler(), nullptr);
129 }
130
131 /**
132 * @tc.name: AotCompilerClientTest_005
133 * @tc.desc: aotClient.GetAotCompilerProxy()
134 * @tc.type: Func
135 */
136 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_005, TestSize.Level0)
137 {
138 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
139 sptr<IRemoteObject> remoteObject = GetAotRemoteObject(aotClient);
140 EXPECT_NE(aotClient.GetAotCompilerProxy(), nullptr);
141
142 sptr<IAotCompilerInterface> aotCompilerProxy_check = iface_cast<IAotCompilerInterface>(remoteObject);
143 EXPECT_NE(aotCompilerProxy_check, nullptr);
144 }
145
146 /**
147 * @tc.name: AotCompilerClientTest_006
148 * @tc.desc: aotClient.StopAotCompiler()
149 * @tc.type: Func
150 */
151 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_006, TestSize.Level0)
152 {
153 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
154 int32_t ret = aotClient.StopAotCompiler();
155 EXPECT_EQ(ret, ERR_AOT_COMPILER_STOP_FAILED);
156 }
157
158 /**
159 * @tc.name: AotCompilerClientTest_007
160 * @tc.desc: aotClient.loadSaFinished_
161 * @tc.type: Func
162 */
163 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_007, TestSize.Level0)
164 {
165 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
166 auto aotCompilerProxy = aotClient.GetAotCompilerProxy();
167 EXPECT_TRUE(aotClient.loadSaFinished_);
168 EXPECT_NE(aotClient.GetAotCompiler(), nullptr);
169 }
170
171 /**
172 * @tc.name: AotCompilerClientTest_008
173 * @tc.desc: aotClient.AotCompilerOnRemoteDied()
174 * @tc.type: Func
175 */
176 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_008, TestSize.Level0)
177 {
178 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
179 sptr<IRemoteObject> remoteObject = GetAotRemoteObject(aotClient);
180 EXPECT_NE(aotClient.GetAotCompiler(), nullptr);
181
182 wptr<IRemoteObject> remoteObject_weak = remoteObject;
183 aotClient.AotCompilerOnRemoteDied(remoteObject_weak);
184 EXPECT_EQ(aotClient.GetAotCompiler(), nullptr);
185 }
186
187 /**
188 * @tc.name: AotCompilerClientTest_009
189 * @tc.desc: callback.OnLoadSystemAbilitySuccess()
190 * @tc.type: Func
191 */
192 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_009, TestSize.Level0)
193 {
194 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
195 AotCompilerLoadCallback callback;
196 sptr<IRemoteObject> remoteObject = GetAotRemoteObject(aotClient);
197 aotClient.SetAotCompiler(nullptr);
198
199 callback.OnLoadSystemAbilitySuccess(AOT_COMPILER_SERVICE_ID, nullptr);
200 EXPECT_EQ(aotClient.GetAotCompiler(), nullptr);
201
202 callback.OnLoadSystemAbilitySuccess(AOT_COMPILER_SERVICE_ID - 1, remoteObject);
203 EXPECT_EQ(aotClient.GetAotCompiler(), nullptr);
204
205 callback.OnLoadSystemAbilitySuccess(AOT_COMPILER_SERVICE_ID + 1, remoteObject);
206 EXPECT_EQ(aotClient.GetAotCompiler(), nullptr);
207
208 callback.OnLoadSystemAbilitySuccess(AOT_COMPILER_SERVICE_ID, remoteObject);
209 EXPECT_NE(aotClient.GetAotCompiler(), nullptr);
210 }
211
212 /**
213 * @tc.name: AotCompilerClientTest_010
214 * @tc.desc: callback.OnLoadSystemAbilityFail()
215 * @tc.type: Func
216 */
217 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_010, TestSize.Level0)
218 {
219 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
220 AotCompilerLoadCallback callback;
221 sptr<IRemoteObject> remoteObject = GetAotRemoteObject(aotClient);
222 aotClient.SetAotCompiler(remoteObject);
223
224 callback.OnLoadSystemAbilityFail(AOT_COMPILER_SERVICE_ID - 1);
225 EXPECT_NE(aotClient.GetAotCompiler(), nullptr);
226
227 callback.OnLoadSystemAbilityFail(AOT_COMPILER_SERVICE_ID + 1);
228 EXPECT_NE(aotClient.GetAotCompiler(), nullptr);
229
230 callback.OnLoadSystemAbilityFail(AOT_COMPILER_SERVICE_ID);
231 EXPECT_EQ(aotClient.GetAotCompiler(), nullptr);
232 }
233
234 /**
235 * @tc.name: AotCompilerClientTest_011
236 * @tc.desc: aotClient.NeedReCompile(oldVersion, sigData)
237 * @tc.type: Func
238 */
239 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_011, TestSize.Level0)
240 {
241 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
242 std::string oldVersion = "4.0.0.0";
243 bool sigData = false;
244 int32_t ret = aotClient.NeedReCompile(oldVersion, sigData);
245 EXPECT_EQ(ret, ERR_OK);
246 }
247
248 /**
249 * @tc.name: AotCompilerClientTest_012
250 * @tc.desc: aotClient.GetAOTVersion(sigData)
251 * @tc.type: Func
252 */
253 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_012, TestSize.Level0)
254 {
255 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
256 std::string sigData = "test_string";
257 int32_t ret = aotClient.GetAOTVersion(sigData);
258 EXPECT_EQ(ret, ERR_OK);
259 EXPECT_STRNE(sigData.c_str(), "test_string");
260 }
261
262 /**
263 * @tc.name: AotCompilerClientTest_013
264 * @tc.desc: aotClient.OnRemoteDied(const wptr<IRemoteObject> &remote)
265 * @tc.type: Func
266 */
267 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_013, TestSize.Level0)
268 {
269 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
270 aotClient.aotCompilerDiedRecipient_->OnRemoteDied(nullptr);
271 EXPECT_EQ(aotClient.GetAotCompiler(), nullptr);
272
273 wptr<IRemoteObject> remoteObject_weak = GetAotRemoteObject(aotClient);
274 EXPECT_NE(remoteObject_weak, nullptr);
275 aotClient.aotCompilerDiedRecipient_->OnRemoteDied(remoteObject_weak);
276 EXPECT_EQ(aotClient.GetAotCompiler(), nullptr);
277 }
278 } // namespace OHOS::ArkCompiler