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 <string>
20 #include <vector>
21
22 #include "aot_compiler_client.h"
23 #include "aot_compiler_service.h"
24 #include "aot_compiler_error_utils.h"
25 #include "aot_compiler_interface_proxy.h"
26 #include "aot_compiler_interface_stub.h"
27 #include "aot_compiler_load_callback.h"
28 #include "iservice_registry.h"
29 #include "system_ability_definition.h"
30
31 using namespace testing::ext;
32
33 namespace OHOS::ArkCompiler {
34 namespace {
35 constexpr int32_t TEST_COMMAND_AOT_COMPILER = MIN_TRANSACTION_ID + 0;
36 constexpr int32_t TEST_COMMAND_STOP_AOT_COMPILER = MIN_TRANSACTION_ID + 1;
37 constexpr int32_t TEST_COMMAND_GET_AOT_VERSION = MIN_TRANSACTION_ID + 2;
38 constexpr int32_t TEST_COMMAND_NEED_RE_COMPILE = MIN_TRANSACTION_ID + 3;
39 constexpr int32_t TEST_COMMAND_INVALID = MIN_TRANSACTION_ID + 1000;
40 const unsigned long MAP_MAX_SIZE = 102400;
41 }
42
43 class AotCompilerStubMock : public AotCompilerInterfaceStub {
44 public:
45 AotCompilerStubMock() = default;
46 virtual ~AotCompilerStubMock() = default;
47
48 MOCK_METHOD(ErrCode, AotCompiler, ((const std::unordered_map<std::string, std::string> &argsMap),
49 std::vector<int16_t> &sigData), (override));
50 MOCK_METHOD(ErrCode, StopAotCompiler, (), (override));
51 MOCK_METHOD(ErrCode, GetAOTVersion, (std::string& sigData), (override));
52 MOCK_METHOD(ErrCode, NeedReCompile, (const std::string& args, bool& sigData), (override));
53 };
54
55 class AotCompilerStubTest : public testing::Test {
56 public:
AotCompilerStubTest()57 AotCompilerStubTest() {}
~AotCompilerStubTest()58 virtual ~AotCompilerStubTest() {}
59
SetUpTestCase()60 static void SetUpTestCase() {}
TearDownTestCase()61 static void TearDownTestCase() {}
62 void SetUp() override;
TearDown()63 void TearDown() override {}
64 sptr<AotCompilerStubMock> aotCompilerStub_ = nullptr;
65 };
66
SetUp()67 void AotCompilerStubTest::SetUp()
68 {
69 aotCompilerStub_ = new (std::nothrow) AotCompilerStubMock();
70 }
71
72 /**
73 * @tc.name: AotCompilerStubTest_001
74 * @tc.desc: AotCompilerInterfaceStub::CommandAOTCompiler()
75 * @tc.type: Func
76 */
77 HWTEST_F(AotCompilerStubTest, AotCompilerStubTest_001, TestSize.Level0)
78 {
79 uint32_t code = static_cast<uint32_t>(TEST_COMMAND_AOT_COMPILER);
80 MessageParcel data;
81 MessageParcel reply;
82 MessageOption option(MessageOption::TF_SYNC);
83 data.WriteInterfaceToken(AotCompilerInterfaceStub::GetDescriptor());
84 AotCompilerService aotService(AOT_COMPILER_SERVICE_ID, false);
85 int32_t ret = aotService.OnRemoteRequest(code, data, reply, option);
86 EXPECT_EQ(ret, ERR_NONE);
87 }
88
89 /**
90 * @tc.name: AotCompilerStubTest_002
91 * @tc.desc: AotCompilerInterfaceStub::CommandStopAOTCompiler()
92 * @tc.type: Func
93 */
94 HWTEST_F(AotCompilerStubTest, AotCompilerStubTest_002, TestSize.Level0)
95 {
96 uint32_t code = static_cast<uint32_t>(TEST_COMMAND_STOP_AOT_COMPILER);
97 MessageParcel data;
98 MessageParcel reply;
99 MessageOption option(MessageOption::TF_SYNC);
100 data.WriteInterfaceToken(AotCompilerInterfaceStub::GetDescriptor());
101 AotCompilerService aotService(AOT_COMPILER_SERVICE_ID, false);
102 int32_t ret = aotService.OnRemoteRequest(code, data, reply, option);
103 EXPECT_EQ(ret, ERR_NONE);
104 }
105
106 /**
107 * @tc.name: AotCompilerStubTest_003
108 * @tc.desc: AotCompilerInterfaceStub::CommandGetAOTVersion()
109 * @tc.type: Func
110 */
111 HWTEST_F(AotCompilerStubTest, AotCompilerStubTest_003, TestSize.Level0)
112 {
113 uint32_t code = static_cast<uint32_t>(TEST_COMMAND_GET_AOT_VERSION);
114 MessageParcel data;
115 MessageParcel reply;
116 MessageOption option(MessageOption::TF_SYNC);
117 data.WriteInterfaceToken(AotCompilerInterfaceStub::GetDescriptor());
118 AotCompilerService aotService(AOT_COMPILER_SERVICE_ID, false);
119 int32_t ret = aotService.OnRemoteRequest(code, data, reply, option);
120 EXPECT_EQ(ret, ERR_NONE);
121 }
122
123 /**
124 * @tc.name: AotCompilerStubTest_004
125 * @tc.desc: AotCompilerInterfaceStub::CommandNeedReCompile()
126 * @tc.type: Func
127 */
128 HWTEST_F(AotCompilerStubTest, AotCompilerStubTest_004, TestSize.Level0)
129 {
130 uint32_t code = static_cast<uint32_t>(TEST_COMMAND_NEED_RE_COMPILE);
131 MessageParcel data;
132 MessageParcel reply;
133 MessageOption option(MessageOption::TF_SYNC);
134 data.WriteInterfaceToken(AotCompilerInterfaceStub::GetDescriptor());
135 AotCompilerService aotService(AOT_COMPILER_SERVICE_ID, false);
136 int32_t ret = aotService.OnRemoteRequest(code, data, reply, option);
137 EXPECT_EQ(ret, ERR_NONE);
138 }
139
140 /**
141 * @tc.name: AotCompilerStubTest_005
142 * @tc.desc: AotCompilerInterfaceStub::OnRemoteRequest() when code is invalid
143 * @tc.type: Func
144 */
145 HWTEST_F(AotCompilerStubTest, AotCompilerStubTest_005, TestSize.Level0)
146 {
147 uint32_t code = static_cast<uint32_t>(TEST_COMMAND_INVALID);
148 MessageParcel data;
149 MessageParcel reply;
150 MessageOption option(MessageOption::TF_SYNC);
151 data.WriteInterfaceToken(AotCompilerInterfaceStub::GetDescriptor());
152 AotCompilerService aotService(AOT_COMPILER_SERVICE_ID, false);
153 int32_t ret = aotService.OnRemoteRequest(code, data, reply, option);
154 EXPECT_NE(ret, ERR_NONE);
155 }
156
157 /**
158 * @tc.name: AotCompilerStubTest_006
159 * @tc.desc: AotCompilerInterfaceStub::OnRemoteRequest() when descriptor mismatches
160 * @tc.type: Func
161 */
162 HWTEST_F(AotCompilerStubTest, AotCompilerStubTest_006, TestSize.Level0)
163 {
164 uint32_t code = static_cast<uint32_t>(TEST_COMMAND_AOT_COMPILER);
165 MessageParcel data;
166 MessageParcel reply;
167 MessageOption option(MessageOption::TF_SYNC);
168 data.WriteInterfaceToken(u"incorrect descriptor");
169
170 AotCompilerService aotService(AOT_COMPILER_SERVICE_ID, false);
171 int32_t ret = aotService.OnRemoteRequest(code, data, reply, option);
172 EXPECT_EQ(ret, ERR_TRANSACTION_FAILED);
173 }
174
175 /**
176 * @tc.name: AotCompilerStubTest_007
177 * @tc.desc: AotCompilerInterfaceStub::OnRemoteRequest() when MAP_MAX_SIZE invalid
178 * @tc.type: Func
179 */
180 HWTEST_F(AotCompilerStubTest, AotCompilerStubTest_007, TestSize.Level0)
181 {
182 uint32_t code = static_cast<uint32_t>(TEST_COMMAND_AOT_COMPILER);
183 MessageParcel data;
184 MessageParcel reply;
185 MessageOption option(MessageOption::TF_SYNC);
186 data.WriteInterfaceToken(AotCompilerInterfaceStub::GetDescriptor());
187 data.WriteInt32(MAP_MAX_SIZE + 1);
188
189 AotCompilerService aotService(AOT_COMPILER_SERVICE_ID, false);
190 int32_t ret = aotService.OnRemoteRequest(code, data, reply, option);
191 EXPECT_EQ(ret, ERR_INVALID_DATA);
192 }
193
194 /**
195 * @tc.name: AotCompilerStubTest_008
196 * @tc.desc: AotCompilerInterfaceStub::OnRemoteRequest() when AotCompiler success
197 * @tc.type: Func
198 */
199 HWTEST_F(AotCompilerStubTest, AotCompilerStubTest_008, TestSize.Level0)
200 {
201 uint32_t code = static_cast<uint32_t>(TEST_COMMAND_AOT_COMPILER);
202 MessageParcel data;
203 MessageParcel reply;
204 MessageOption option(MessageOption::TF_SYNC);
205 data.WriteInterfaceToken(AotCompilerInterfaceStub::GetDescriptor());
206
207 EXPECT_CALL(*aotCompilerStub_, AotCompiler(testing::_, testing::_)).
208 Times(1).WillOnce(testing::Return(ERR_OK));
209 int32_t result = aotCompilerStub_->OnRemoteRequest(code, data, reply, option);
210 EXPECT_EQ(result, ERR_NONE);
211 }
212
213 /**
214 * @tc.name: AotCompilerStubTest_009
215 * @tc.desc: AotCompilerInterfaceStub::OnRemoteRequest() when AotCompiler fail
216 * @tc.type: Func
217 */
218 HWTEST_F(AotCompilerStubTest, AotCompilerStubTest_009, TestSize.Level0)
219 {
220 uint32_t code = static_cast<uint32_t>(TEST_COMMAND_AOT_COMPILER);
221 MessageParcel data;
222 MessageParcel reply;
223 MessageOption option(MessageOption::TF_SYNC);
224 data.WriteInterfaceToken(AotCompilerInterfaceStub::GetDescriptor());
225
226 EXPECT_CALL(*aotCompilerStub_, AotCompiler(testing::_, testing::_)).
227 Times(1).WillOnce(testing::Return(ERR_FAIL));
228 int32_t result = aotCompilerStub_->OnRemoteRequest(code, data, reply, option);
229 EXPECT_EQ(result, ERR_NONE);
230 }
231
232 /**
233 * @tc.name: AotCompilerStubTest_010
234 * @tc.desc: AotCompilerInterfaceStub::OnRemoteRequest() when GetAOTVersion success
235 * @tc.type: Func
236 */
237 HWTEST_F(AotCompilerStubTest, AotCompilerStubTest_010, TestSize.Level0)
238 {
239 uint32_t code = static_cast<uint32_t>(TEST_COMMAND_GET_AOT_VERSION);
240 MessageParcel data;
241 MessageParcel reply;
242 MessageOption option(MessageOption::TF_SYNC);
243 data.WriteInterfaceToken(AotCompilerInterfaceStub::GetDescriptor());
244
245 EXPECT_CALL(*aotCompilerStub_, GetAOTVersion(testing::_)).
246 Times(1).WillOnce(testing::Return(ERR_OK));
247 int32_t result = aotCompilerStub_->OnRemoteRequest(code, data, reply, option);
248 EXPECT_EQ(result, ERR_NONE);
249 }
250
251 /**
252 * @tc.name: AotCompilerStubTest_011
253 * @tc.desc: AotCompilerInterfaceStub::OnRemoteRequest() when GetAOTVersion fail
254 * @tc.type: Func
255 */
256 HWTEST_F(AotCompilerStubTest, AotCompilerStubTest_011, TestSize.Level0)
257 {
258 uint32_t code = static_cast<uint32_t>(TEST_COMMAND_GET_AOT_VERSION);
259 MessageParcel data;
260 MessageParcel reply;
261 MessageOption option(MessageOption::TF_SYNC);
262 data.WriteInterfaceToken(AotCompilerInterfaceStub::GetDescriptor());
263
264 EXPECT_CALL(*aotCompilerStub_, GetAOTVersion(testing::_)).
265 Times(1).WillOnce(testing::Return(ERR_FAIL));
266 int32_t result = aotCompilerStub_->OnRemoteRequest(code, data, reply, option);
267 EXPECT_EQ(result, ERR_NONE);
268 }
269
270 /**
271 * @tc.name: AotCompilerStubTest_012
272 * @tc.desc: AotCompilerInterfaceStub::OnRemoteRequest() when NeedReCompile success
273 * @tc.type: Func
274 */
275 HWTEST_F(AotCompilerStubTest, AotCompilerStubTest_012, TestSize.Level0)
276 {
277 uint32_t code = static_cast<uint32_t>(TEST_COMMAND_NEED_RE_COMPILE);
278 MessageParcel data;
279 MessageParcel reply;
280 MessageOption option(MessageOption::TF_SYNC);
281 data.WriteInterfaceToken(AotCompilerInterfaceStub::GetDescriptor());
282
283 EXPECT_CALL(*aotCompilerStub_, NeedReCompile(testing::_, testing::_)).
284 Times(1).WillOnce(testing::Return(ERR_OK));
285 int32_t result = aotCompilerStub_->OnRemoteRequest(code, data, reply, option);
286 EXPECT_EQ(result, ERR_NONE);
287 }
288
289 /**
290 * @tc.name: AotCompilerStubTest_013
291 * @tc.desc: AotCompilerInterfaceStub::OnRemoteRequest() when NeedReCompile fail
292 * @tc.type: Func
293 */
294 HWTEST_F(AotCompilerStubTest, AotCompilerStubTest_013, TestSize.Level0)
295 {
296 uint32_t code = static_cast<uint32_t>(TEST_COMMAND_NEED_RE_COMPILE);
297 MessageParcel data;
298 MessageParcel reply;
299 MessageOption option(MessageOption::TF_SYNC);
300 data.WriteInterfaceToken(AotCompilerInterfaceStub::GetDescriptor());
301
302 EXPECT_CALL(*aotCompilerStub_, NeedReCompile(testing::_, testing::_)).
303 Times(1).WillOnce(testing::Return(ERR_FAIL));
304 int32_t result = aotCompilerStub_->OnRemoteRequest(code, data, reply, option);
305 EXPECT_EQ(result, ERR_NONE);
306 }
307 } // namespace OHOS::ArkCompiler