• 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 <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 VECTOR_MAX_SIZE = 102400;
41 }
42 
43 class MockAotCompilerStub : public IRemoteStub<IAotCompilerInterface> {
44 public:
45     MockAotCompilerStub() = default;
46     virtual ~MockAotCompilerStub() = default;
47 
48     MOCK_METHOD(int32_t, CommandAOTCompiler, (MessageParcel &data, MessageParcel &reply));
49     MOCK_METHOD(int32_t, CommandStopAOTCompiler, (MessageParcel &reply));
50     MOCK_METHOD(int32_t, CommandGetAOTVersion, (MessageParcel& reply));
51     MOCK_METHOD(int32_t, CommandNeedReCompile, (MessageParcel& data, MessageParcel& reply));
52     MOCK_METHOD(ErrCode, AotCompiler, ((const std::unordered_map<std::string, std::string> &argsMap),
53         std::vector<int16_t> &sigData), (override));
54     MOCK_METHOD(ErrCode, StopAotCompiler, (), (override));
55     MOCK_METHOD(ErrCode, GetAOTVersion, (std::string& sigData), (override));
56     MOCK_METHOD(ErrCode, NeedReCompile, (const std::string& args, bool& sigData), (override));
57 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)58     int32_t OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
59     {
60         std::u16string localDescriptor = GetDescriptor();
61         std::u16string remoteDescriptor = data.ReadInterfaceToken();
62         if (localDescriptor != remoteDescriptor) {
63             return TEST_COMMAND_INVALID;
64         }
65         switch (code) {
66             case TEST_COMMAND_AOT_COMPILER:
67                 return CommandAOTCompiler(data, reply);
68             case TEST_COMMAND_STOP_AOT_COMPILER:
69                 return CommandStopAOTCompiler(reply);
70             case TEST_COMMAND_GET_AOT_VERSION:
71                 return CommandGetAOTVersion(reply);
72             case TEST_COMMAND_NEED_RE_COMPILE:
73                 return CommandNeedReCompile(data, reply);
74             default:
75                 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
76         }
77         return TEST_COMMAND_INVALID;
78     }
79 };
80 
81 class AotCompilerProxyMock : public AotCompilerInterfaceProxy {
82 public:
AotCompilerProxyMock(const sptr<IRemoteObject> & remote)83     explicit AotCompilerProxyMock(const sptr<IRemoteObject>& remote): AotCompilerInterfaceProxy(remote) {}
84     virtual ~AotCompilerProxyMock() = default;
85 
Remote()86     IRemoteObject* Remote()
87     {
88         return nullptr;
89     }
90 };
91 
92 class AotCompilerProxyTest : public testing::Test {
93 public:
AotCompilerProxyTest()94     AotCompilerProxyTest() {}
~AotCompilerProxyTest()95     virtual ~AotCompilerProxyTest() {}
96 
SetUpTestCase()97     static void SetUpTestCase() {}
TearDownTestCase()98     static void TearDownTestCase() {}
99     void SetUp() override;
TearDown()100     void TearDown() override {}
101     sptr<MockAotCompilerStub> mockStub_ = nullptr;
102     sptr<AotCompilerInterfaceProxy> proxy_ = nullptr;
103 };
104 
SetUp()105 void AotCompilerProxyTest::SetUp()
106 {
107     mockStub_ = new (std::nothrow) MockAotCompilerStub();
108     proxy_ = new (std::nothrow) AotCompilerInterfaceProxy(mockStub_->AsObject());
109 }
110 
111 /**
112  * @tc.name: AotCompilerProxyTest_001
113  * @tc.desc: AotCompilerProxy::AotCompiler(argsMap, sigData) when SendRequest fail
114  * @tc.type: Func
115 */
116 HWTEST_F(AotCompilerProxyTest, AotCompilerProxyTest_001, TestSize.Level0)
117 {
118     sptr<IRemoteObject> remote = nullptr;
119     sptr<AotCompilerProxyMock> mockProxy_ = new AotCompilerProxyMock(remote);
120     std::unordered_map<std::string, std::string> argsMap;
121     std::vector<int16_t> sigData;
122     int32_t result = mockProxy_->AotCompiler(argsMap, sigData);
123     EXPECT_EQ(result, ERR_INVALID_DATA);
124 }
125 
126 /**
127  * @tc.name: AotCompilerProxyTest_002
128  * @tc.desc: AotCompilerProxy::StopAotCompiler() when SendRequest fail
129  * @tc.type: Func
130 */
131 HWTEST_F(AotCompilerProxyTest, AotCompilerProxyTest_002, TestSize.Level0)
132 {
133     sptr<IRemoteObject> remote = nullptr;
134     sptr<AotCompilerProxyMock> mockProxy_ = new AotCompilerProxyMock(remote);
135     int32_t result = mockProxy_->StopAotCompiler();
136     EXPECT_EQ(result, ERR_INVALID_DATA);
137 }
138 
139 /**
140  * @tc.name: AotCompilerProxyTest_003
141  * @tc.desc: AotCompilerProxy::GetAOTVersion(sigData) when SendRequest fail
142  * @tc.type: Func
143 */
144 HWTEST_F(AotCompilerProxyTest, AotCompilerProxyTest_003, TestSize.Level0)
145 {
146     sptr<IRemoteObject> remote = nullptr;
147     sptr<AotCompilerProxyMock> mockProxy_ = new AotCompilerProxyMock(remote);
148     std::string sigData = "===";
149     int32_t result = mockProxy_->GetAOTVersion(sigData);
150     EXPECT_EQ(result, ERR_INVALID_DATA);
151 }
152 
153 /**
154  * @tc.name: AotCompilerProxyTest_004
155  * @tc.desc: AotCompilerProxy::NeedReCompile(args, sigData) when SendRequest fail
156  * @tc.type: Func
157 */
158 HWTEST_F(AotCompilerProxyTest, AotCompilerProxyTest_004, TestSize.Level0)
159 {
160     sptr<IRemoteObject> remote = nullptr;
161     sptr<AotCompilerProxyMock> mockProxy_ = new AotCompilerProxyMock(remote);
162     std::string args = "--aot";
163     bool sigData = false;
164     int32_t result = mockProxy_->NeedReCompile(args, sigData);
165     EXPECT_EQ(result, ERR_INVALID_DATA);
166 }
167 
168 /**
169  * @tc.name: AotCompilerProxyTest_005
170  * @tc.desc: AotCompilerProxy::AotCompiler(argsMap, sigData) when connect fail
171  * @tc.type: Func
172 */
173 HWTEST_F(AotCompilerProxyTest, AotCompilerProxyTest_005, TestSize.Level0)
174 {
175     std::unordered_map<std::string, std::string> argsMap;
176     std::vector<int16_t> sigData;
177 
178     EXPECT_CALL(*mockStub_, CommandAOTCompiler(testing::_, testing::_)).
179         Times(1).WillOnce(testing::Return(ERR_AOT_COMPILER_CONNECT_FAILED));
180     int32_t result = proxy_->AotCompiler(argsMap, sigData);
181     EXPECT_EQ(result, ERR_AOT_COMPILER_CONNECT_FAILED);
182 }
183 
184 /**
185  * @tc.name: AotCompilerProxyTest_006
186  * @tc.desc: AotCompilerProxy::AotCompiler(argsMap, sigData) when compile fail
187  * @tc.type: Func
188 */
189 HWTEST_F(AotCompilerProxyTest, AotCompilerProxyTest_006, TestSize.Level0)
190 {
191     std::unordered_map<std::string, std::string> argsMap;
192     std::vector<int16_t> sigData;
193 
__anon1258fdc70202(MessageParcel &data, MessageParcel &reply) 194     auto func = [](MessageParcel &data, MessageParcel &reply) -> int32_t {
195         reply.WriteInt32(ERR_AOT_COMPILER_CALL_FAILED);
196         return ERR_OK;
197     };
198     EXPECT_CALL(*mockStub_, CommandAOTCompiler(testing::_, testing::_)).
199         Times(1).WillOnce(testing::Invoke(func));
200     int32_t result = proxy_->AotCompiler(argsMap, sigData);
201     EXPECT_EQ(result, ERR_AOT_COMPILER_CALL_FAILED);
202 }
203 
204 /**
205  * @tc.name: AotCompilerProxyTest_007
206  * @tc.desc: AotCompilerProxy::AotCompiler(argsMap, sigData) when VECTOR_MAX_SIZE invalid
207  * @tc.type: Func
208 */
209 HWTEST_F(AotCompilerProxyTest, AotCompilerProxyTest_007, TestSize.Level0)
210 {
211     std::unordered_map<std::string, std::string> argsMap;
212     std::vector<int16_t> sigData;
213 
__anon1258fdc70302(MessageParcel &data, MessageParcel &reply) 214     auto func = [](MessageParcel &data, MessageParcel &reply) -> int32_t {
215         reply.WriteInt32(ERR_OK);
216         reply.WriteInt32(VECTOR_MAX_SIZE + 1);
217         return ERR_OK;
218     };
219     EXPECT_CALL(*mockStub_, CommandAOTCompiler(testing::_, testing::_)).
220         Times(1).WillOnce(testing::Invoke(func));
221     int32_t result = proxy_->AotCompiler(argsMap, sigData);
222     EXPECT_EQ(result, ERR_INVALID_DATA);
223 }
224 
225 /**
226  * @tc.name: AotCompilerProxyTest_008
227  * @tc.desc: AotCompilerProxy::AotCompiler(argsMap, sigData) success
228  * @tc.type: Func
229 */
230 HWTEST_F(AotCompilerProxyTest, AotCompilerProxyTest_008, TestSize.Level0)
231 {
232     std::unordered_map<std::string, std::string> argsMap;
233     std::vector<int16_t> sigData;
234 
__anon1258fdc70402(MessageParcel &data, MessageParcel &reply) 235     auto func = [](MessageParcel &data, MessageParcel &reply) -> int32_t {
236         reply.WriteInt32(ERR_OK);
237         reply.WriteInt32(0);
238         return ERR_OK;
239     };
240     EXPECT_CALL(*mockStub_, CommandAOTCompiler(testing::_, testing::_)).
241         Times(1).WillOnce(testing::Invoke(func));
242     int32_t result = proxy_->AotCompiler(argsMap, sigData);
243     EXPECT_EQ(result, ERR_OK);
244 }
245 
246 /**
247  * @tc.name: AotCompilerProxyTest_009
248  * @tc.desc: AotCompilerProxy::StopAotCompiler() when connect fail
249  * @tc.type: Func
250 */
251 HWTEST_F(AotCompilerProxyTest, AotCompilerProxyTest_009, TestSize.Level0)
252 {
253     EXPECT_CALL(*mockStub_, CommandStopAOTCompiler(testing::_)).
254         Times(1).WillOnce(testing::Return(ERR_AOT_COMPILER_CONNECT_FAILED));
255     int32_t result = proxy_->StopAotCompiler();
256     EXPECT_EQ(result, ERR_AOT_COMPILER_CONNECT_FAILED);
257 }
258 
259 /**
260  * @tc.name: AotCompilerProxyTest_010
261  * @tc.desc: AotCompilerProxy::StopAotCompiler() when stop fail
262  * @tc.type: Func
263 */
264 HWTEST_F(AotCompilerProxyTest, AotCompilerProxyTest_010, TestSize.Level0)
265 {
__anon1258fdc70502(MessageParcel &reply) 266     auto func = [](MessageParcel &reply) -> int32_t {
267         reply.WriteInt32(ERR_AOT_COMPILER_CALL_FAILED);
268         return ERR_OK;
269     };
270     EXPECT_CALL(*mockStub_, CommandStopAOTCompiler(testing::_)).
271         Times(1).WillOnce(testing::Invoke(func));
272     int32_t result = proxy_->StopAotCompiler();
273     EXPECT_EQ(result, ERR_AOT_COMPILER_CALL_FAILED);
274 }
275 
276 /**
277  * @tc.name: AotCompilerProxyTest_011
278  * @tc.desc: AotCompilerProxy::StopAotCompiler() when stop success
279  * @tc.type: Func
280 */
281 HWTEST_F(AotCompilerProxyTest, AotCompilerProxyTest_011, TestSize.Level0)
282 {
__anon1258fdc70602(MessageParcel &reply) 283     auto func = [](MessageParcel &reply) -> int32_t {
284         reply.WriteInt32(ERR_OK);
285         return ERR_OK;
286     };
287     EXPECT_CALL(*mockStub_, CommandStopAOTCompiler(testing::_)).
288         Times(1).WillOnce(testing::Invoke(func));
289     int32_t result = proxy_->StopAotCompiler();
290     EXPECT_EQ(result, ERR_OK);
291 }
292 
293 /**
294  * @tc.name: AotCompilerProxyTest_012
295  * @tc.desc: AotCompilerProxy::GetAOTVersion(sigData) when connect fail
296  * @tc.type: Func
297 */
298 HWTEST_F(AotCompilerProxyTest, AotCompilerProxyTest_012, TestSize.Level0)
299 {
300     std::string sigData;
301     EXPECT_CALL(*mockStub_, CommandGetAOTVersion(testing::_)).
302         Times(1).WillOnce(testing::Return(ERR_AOT_COMPILER_CONNECT_FAILED));
303     int32_t result = proxy_->GetAOTVersion(sigData);
304     EXPECT_EQ(result, ERR_AOT_COMPILER_CONNECT_FAILED);
305 }
306 
307 /**
308  * @tc.name: AotCompilerProxyTest_013
309  * @tc.desc: AotCompilerProxy::GetAOTVersion(sigData) when get error
310  * @tc.type: Func
311 */
312 HWTEST_F(AotCompilerProxyTest, AotCompilerProxyTest_013, TestSize.Level0)
313 {
314     std::string sigData;
__anon1258fdc70702(MessageParcel &reply) 315     auto func = [](MessageParcel &reply) -> int32_t {
316         reply.WriteInt32(ERR_AOT_COMPILER_CALL_FAILED);
317         return ERR_OK;
318     };
319     EXPECT_CALL(*mockStub_, CommandGetAOTVersion(testing::_)).
320         Times(1).WillOnce(testing::Invoke(func));
321     int32_t result = proxy_->GetAOTVersion(sigData);
322     EXPECT_EQ(result, ERR_AOT_COMPILER_CALL_FAILED);
323 }
324 
325 /**
326  * @tc.name: AotCompilerProxyTest_014
327  * @tc.desc: AotCompilerProxy::GetAOTVersion(sigData) when get success
328  * @tc.type: Func
329 */
330 HWTEST_F(AotCompilerProxyTest, AotCompilerProxyTest_014, TestSize.Level0)
331 {
332     std::string sigData;
333     std::string testData = "test";
__anon1258fdc70802(MessageParcel &reply) 334     auto func = [testData](MessageParcel &reply) -> int32_t {
335         reply.WriteInt32(ERR_OK);
336         reply.WriteString16(Str8ToStr16(testData));
337         return ERR_OK;
338     };
339     EXPECT_CALL(*mockStub_, CommandGetAOTVersion(testing::_)).
340         Times(1).WillOnce(testing::Invoke(func));
341     int32_t result = proxy_->GetAOTVersion(sigData);
342     EXPECT_EQ(result, ERR_OK);
343     EXPECT_STREQ(sigData.c_str(), testData.c_str());
344 }
345 
346 /**
347  * @tc.name: AotCompilerProxyTest_015
348  * @tc.desc: AotCompilerProxy::NeedReCompile(args, sigData) when connect fail
349  * @tc.type: Func
350 */
351 HWTEST_F(AotCompilerProxyTest, AotCompilerProxyTest_015, TestSize.Level0)
352 {
353     std::string args;
354     bool sigData = false;
355 
356     EXPECT_CALL(*mockStub_, CommandNeedReCompile(testing::_, testing::_)).
357         Times(1).WillOnce(testing::Return(ERR_AOT_COMPILER_CONNECT_FAILED));
358     int32_t result = proxy_->NeedReCompile(args, sigData);
359     EXPECT_EQ(result, ERR_AOT_COMPILER_CONNECT_FAILED);
360 }
361 
362 /**
363  * @tc.name: AotCompilerProxyTest_016
364  * @tc.desc: AotCompilerProxy::NeedReCompile(args, sigData) when run fail
365  * @tc.type: Func
366 */
367 HWTEST_F(AotCompilerProxyTest, AotCompilerProxyTest_016, TestSize.Level0)
368 {
369     std::string args;
370     bool sigData = false;
371 
__anon1258fdc70902(MessageParcel &data, MessageParcel &reply) 372     auto func = [](MessageParcel &data, MessageParcel &reply) -> int32_t {
373         reply.WriteInt32(ERR_AOT_COMPILER_CALL_FAILED);
374         return ERR_OK;
375     };
376     EXPECT_CALL(*mockStub_, CommandNeedReCompile(testing::_, testing::_)).
377         Times(1).WillOnce(testing::Invoke(func));
378     int32_t result = proxy_->NeedReCompile(args, sigData);
379     EXPECT_EQ(result, ERR_AOT_COMPILER_CALL_FAILED);
380 }
381 
382 /**
383  * @tc.name: AotCompilerProxyTest_017
384  * @tc.desc: AotCompilerProxy::NeedReCompile(args, sigData) when success
385  * @tc.type: Func
386 */
387 HWTEST_F(AotCompilerProxyTest, AotCompilerProxyTest_017, TestSize.Level0)
388 {
389     std::string args;
390     bool sigData = false;
391     bool testData = true;
__anon1258fdc70a02(MessageParcel &data, MessageParcel &reply) 392     auto func = [testData](MessageParcel &data, MessageParcel &reply) -> int32_t {
393         reply.WriteInt32(ERR_OK);
394         reply.WriteBool(testData);
395         return ERR_OK;
396     };
397     EXPECT_CALL(*mockStub_, CommandNeedReCompile(testing::_, testing::_)).
398         Times(1).WillOnce(testing::Invoke(func));
399     int32_t result = proxy_->NeedReCompile(args, sigData);
400     EXPECT_EQ(result, ERR_OK);
401     EXPECT_TRUE(sigData);
402 }
403 } // namespace OHOS::ArkCompiler