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 * @tc.require: IR/AR/SR
71 */
72 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_001, TestSize.Level0)
73 {
74 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
75 EXPECT_NE(aotClient.aotCompilerDiedRecipient_, nullptr);
76
77 auto &client_1 = AotCompilerClient::GetInstance();
78 auto &client_2 = AotCompilerClient::GetInstance();
79 EXPECT_EQ(&client_1, &client_2);
80 EXPECT_EQ(client_1.aotCompilerDiedRecipient_, client_2.aotCompilerDiedRecipient_);
81 }
82
83 /**
84 * @tc.name: AotCompilerClientTest_002
85 * @tc.desc: invoke aot_compiler service in client program.
86 * @tc.type: Func
87 * @tc.require: IR/AR/SR
88 */
89 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_002, TestSize.Level0)
90 {
91 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
92 std::unordered_map<std::string, std::string> argsMap;
93 std::vector<int16_t> sigData;
94 int32_t ret = aotClient.AotCompiler(argsMap, sigData);
95 EXPECT_EQ(ret, ERR_AOT_COMPILER_PARAM_FAILED);
96 EXPECT_TRUE(argsMap.empty());
97 EXPECT_TRUE(sigData.empty());
98 }
99
100 /**
101 * @tc.name: AotCompilerClientTest_003
102 * @tc.desc: LoadSystemAbility(AOT_COMPILER_SERVICE_ID, loadCallback)
103 * @tc.type: Func
104 * @tc.require: IR/AR/SR
105 */
106 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_003, TestSize.Level0)
107 {
108 auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
109 ASSERT_NE(systemAbilityMgr, nullptr);
110
111 sptr<AotCompilerLoadCallback> loadCallback = new (std::nothrow) AotCompilerLoadCallback();
112 EXPECT_NE(loadCallback, nullptr);
113
114 auto ret = systemAbilityMgr->LoadSystemAbility(AOT_COMPILER_SERVICE_ID, loadCallback);
115 EXPECT_EQ(ret, ERR_OK);
116 }
117
118 /**
119 * @tc.name: AotCompilerClientTest_004
120 * @tc.desc: aotClient.LoadAotCompilerService()
121 * @tc.type: Func
122 * @tc.require: IR/AR/SR
123 */
124 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_004, TestSize.Level0)
125 {
126 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
127 aotClient.SetAotCompiler(nullptr);
128 EXPECT_EQ(aotClient.GetAotCompiler(), nullptr);
129
130 bool retLoad = aotClient.LoadAotCompilerService();
131 EXPECT_TRUE(retLoad);
132 EXPECT_NE(aotClient.GetAotCompiler(), nullptr);
133 }
134
135 /**
136 * @tc.name: AotCompilerClientTest_005
137 * @tc.desc: aotClient.GetAotCompilerProxy()
138 * @tc.type: Func
139 * @tc.require: IR/AR/SR
140 */
141 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_005, TestSize.Level0)
142 {
143 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
144 sptr<IRemoteObject> remoteObject = GetAotRemoteObject(aotClient);
145 EXPECT_NE(aotClient.GetAotCompilerProxy(), nullptr);
146
147 sptr<IAotCompilerInterface> aotCompilerProxy_check = iface_cast<IAotCompilerInterface>(remoteObject);
148 EXPECT_NE(aotCompilerProxy_check, nullptr);
149 }
150
151 /**
152 * @tc.name: AotCompilerClientTest_006
153 * @tc.desc: aotClient.StopAotCompiler()
154 * @tc.type: Func
155 * @tc.require: IR/AR/SR
156 */
157 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_006, TestSize.Level0)
158 {
159 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
160 int32_t ret = aotClient.StopAotCompiler();
161 EXPECT_EQ(ret, ERR_AOT_COMPILER_STOP_FAILED);
162 }
163
164 /**
165 * @tc.name: AotCompilerClientTest_007
166 * @tc.desc: aotClient.loadSaFinished_
167 * @tc.type: Func
168 * @tc.require: IR/AR/SR
169 */
170 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_007, TestSize.Level0)
171 {
172 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
173 auto aotCompilerProxy = aotClient.GetAotCompilerProxy();
174 EXPECT_TRUE(aotClient.loadSaFinished_);
175 EXPECT_NE(aotClient.GetAotCompiler(), nullptr);
176 }
177
178 /**
179 * @tc.name: AotCompilerClientTest_008
180 * @tc.desc: aotClient.AotCompilerOnRemoteDied()
181 * @tc.type: Func
182 * @tc.require: IR/AR/SR
183 */
184 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_008, TestSize.Level0)
185 {
186 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
187 sptr<IRemoteObject> remoteObject = GetAotRemoteObject(aotClient);
188 EXPECT_NE(aotClient.GetAotCompiler(), nullptr);
189
190 wptr<IRemoteObject> remoteObject_weak = remoteObject;
191 aotClient.AotCompilerOnRemoteDied(remoteObject_weak);
192 EXPECT_EQ(aotClient.GetAotCompiler(), nullptr);
193 }
194
195 /**
196 * @tc.name: AotCompilerClientTest_009
197 * @tc.desc: callback.OnLoadSystemAbilitySuccess()
198 * @tc.type: Func
199 * @tc.require: IR/AR/SR
200 */
201 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_009, TestSize.Level0)
202 {
203 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
204 AotCompilerLoadCallback callback;
205 sptr<IRemoteObject> remoteObject = GetAotRemoteObject(aotClient);
206 aotClient.SetAotCompiler(nullptr);
207
208 callback.OnLoadSystemAbilitySuccess(AOT_COMPILER_SERVICE_ID, nullptr);
209 EXPECT_EQ(aotClient.GetAotCompiler(), nullptr);
210
211 callback.OnLoadSystemAbilitySuccess(AOT_COMPILER_SERVICE_ID - 1, remoteObject);
212 EXPECT_EQ(aotClient.GetAotCompiler(), nullptr);
213
214 callback.OnLoadSystemAbilitySuccess(AOT_COMPILER_SERVICE_ID + 1, remoteObject);
215 EXPECT_EQ(aotClient.GetAotCompiler(), nullptr);
216
217 callback.OnLoadSystemAbilitySuccess(AOT_COMPILER_SERVICE_ID, remoteObject);
218 EXPECT_NE(aotClient.GetAotCompiler(), nullptr);
219 }
220
221 /**
222 * @tc.name: AotCompilerClientTest_010
223 * @tc.desc: callback.OnLoadSystemAbilityFail()
224 * @tc.type: Func
225 * @tc.require: IR/AR/SR
226 */
227 HWTEST_F(AotCompilerClientTest, AotCompilerClientTest_010, TestSize.Level0)
228 {
229 AotCompilerClient &aotClient = AotCompilerClient::GetInstance();
230 AotCompilerLoadCallback callback;
231 sptr<IRemoteObject> remoteObject = GetAotRemoteObject(aotClient);
232 aotClient.SetAotCompiler(remoteObject);
233
234 callback.OnLoadSystemAbilityFail(AOT_COMPILER_SERVICE_ID - 1);
235 EXPECT_NE(aotClient.GetAotCompiler(), nullptr);
236
237 callback.OnLoadSystemAbilityFail(AOT_COMPILER_SERVICE_ID + 1);
238 EXPECT_NE(aotClient.GetAotCompiler(), nullptr);
239
240 callback.OnLoadSystemAbilityFail(AOT_COMPILER_SERVICE_ID);
241 EXPECT_EQ(aotClient.GetAotCompiler(), nullptr);
242 }
243 } // namespace OHOS::ArkCompiler