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 <fcntl.h>
17 #include <gtest/gtest.h>
18 #include <iostream>
19 #include <memory>
20 #include <vector>
21 #include "avcodec_errors.h"
22 #include "avcodec_server.h"
23 #include "avcodec_server_manager.h"
24 #include "codec_service_stub.h"
25 #include "codeclist_service_stub.h"
26 #include "iservice_registry.h"
27 #include "mem_mgr_client.h"
28 #include "system_ability.h"
29 #include "system_ability_definition.h"
30
31 using namespace OHOS;
32 using namespace OHOS::Memory;
33 using namespace OHOS::MediaAVCodec;
34 using namespace testing;
35 using namespace testing::ext;
36
37 namespace {
38 const std::string DUMP_FILE_PATH = "/data/test/media/dump.txt";
39 constexpr int32_t INVALID_NUM = -1;
40 } // namespace
41
42 namespace {
43 class SaAVCodecUnitTest : public testing::Test {
44 public:
45 static void SetUpTestCase(void);
46 static void TearDownTestCase(void);
47 void SetUp(void);
48 void TearDown(void);
49
50 std::shared_ptr<AVCodecServer> CreateAVCodecServer();
51 sptr<IRemoteObject> CreateCodecServiceStub(std::shared_ptr<AVCodecServer> &server);
52
53 std::shared_ptr<SystemAbilityMock> saMock_;
54 std::shared_ptr<MemMgrClientMock> memMgrMock_;
55 std::shared_ptr<AVCodecServiceStubMock> avcodecStubMock_;
56 std::shared_ptr<CodecServiceStubMock> codecStubMock_;
57 std::shared_ptr<CodecListServiceStubMock> codeclistStubMock_;
58
59 private:
60 std::vector<std::pair<AVCodecServerManager::StubType, sptr<IRemoteObject>>> stubList_;
61 };
62
SetUpTestCase(void)63 void SaAVCodecUnitTest::SetUpTestCase(void) {}
64
TearDownTestCase(void)65 void SaAVCodecUnitTest::TearDownTestCase(void) {}
66
SetUp(void)67 void SaAVCodecUnitTest::SetUp(void)
68 {
69 saMock_ = std::make_shared<SystemAbilityMock>();
70 SystemAbility::RegisterMock(saMock_);
71 memMgrMock_ = std::make_shared<MemMgrClientMock>();
72 MemMgrClient::RegisterMock(memMgrMock_);
73 avcodecStubMock_ = std::make_shared<AVCodecServiceStubMock>();
74 AVCodecServiceStub::RegisterMock(avcodecStubMock_);
75 codecStubMock_ = std::make_shared<CodecServiceStubMock>();
76 CodecServiceStub::RegisterMock(codecStubMock_);
77 codeclistStubMock_ = std::make_shared<CodecListServiceStubMock>();
78 CodecListServiceStub::RegisterMock(codeclistStubMock_);
79 }
80
TearDown(void)81 void SaAVCodecUnitTest::TearDown(void)
82 {
83 AVCodecServerManager &manager = AVCodecServerManager::GetInstance();
84 for (auto &val : stubList_) {
85 manager.DestroyStubObject(val.first, val.second);
86 }
87 stubList_.clear();
88 saMock_ = nullptr;
89 codecStubMock_ = nullptr;
90 codeclistStubMock_ = nullptr;
91 }
92
CreateAVCodecServer()93 std::shared_ptr<AVCodecServer> SaAVCodecUnitTest::CreateAVCodecServer()
94 {
95 std::shared_ptr<AVCodecServer> server = nullptr;
96 EXPECT_CALL(*saMock_, SystemAbilityCtor(AV_CODEC_SERVICE_ID, true)).Times(1);
97 EXPECT_CALL(*saMock_, SystemAbilityDtor()).Times(1);
98 EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubCtor()).Times(1);
99 EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubDtor()).Times(1);
100
101 server = std::make_shared<AVCodecServer>(AV_CODEC_SERVICE_ID, true);
102 EXPECT_NE(server, nullptr);
103 return server;
104 }
105
CreateCodecServiceStub(std::shared_ptr<AVCodecServer> & server)106 sptr<IRemoteObject> SaAVCodecUnitTest::CreateCodecServiceStub(std::shared_ptr<AVCodecServer> &server)
107 {
108 EXPECT_NE(server, nullptr);
109 if (server == nullptr) {
110 return nullptr;
111 }
112 sptr<IRemoteObject> listener = sptr<IRemoteObject>(new AVCodecListenerStub());
113 sptr<IRemoteObject> codecStub = nullptr;
114 EXPECT_CALL(*avcodecStubMock_, SetDeathListener(listener)).Times(1).WillOnce(Return(AVCS_ERR_OK));
115 EXPECT_CALL(*codecStubMock_, Create()).Times(AtLeast(1)).WillOnce(Return(new CodecServiceStub()));
116 EXPECT_CALL(*codecStubMock_, CodecServiceStubDtor()).Times(AtLeast(1));
117 int32_t ret = server->GetSubSystemAbility(IStandardAVCodecService::AVCODEC_CODEC, listener, codecStub);
118 EXPECT_NE(codecStub, nullptr);
119 EXPECT_EQ(ret, AVCS_ERR_OK);
120 stubList_.emplace_back(AVCodecServerManager::StubType::CODEC, codecStub);
121 return codecStub;
122 }
123
124 /**
125 * @tc.name: AVCodec_Server_Constructor_001
126 * @tc.desc: id and bool is matched
127 */
128 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_Constructor_001, TestSize.Level1)
129 {
130 EXPECT_CALL(*saMock_, SystemAbilityCtor(AV_CODEC_SERVICE_ID, true)).Times(1);
131 EXPECT_CALL(*saMock_, SystemAbilityDtor()).Times(1);
132 EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubCtor()).Times(1);
133 EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubDtor()).Times(1);
134
135 auto server = std::make_shared<AVCodecServer>(AV_CODEC_SERVICE_ID, true);
136 EXPECT_NE(server, nullptr);
137 server = nullptr;
138 }
139
140 /**
141 * @tc.name: AVCodec_Server_Constructor_002
142 * @tc.desc: id and bool is matched
143 */
144 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_Constructor_002, TestSize.Level1)
145 {
146 EXPECT_CALL(*saMock_, SystemAbilityCtor(AV_CODEC_SERVICE_ID, false)).Times(1);
147 EXPECT_CALL(*saMock_, SystemAbilityDtor()).Times(1);
148 EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubCtor()).Times(1);
149 EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubDtor()).Times(1);
150
151 auto server = std::make_shared<AVCodecServer>(AV_CODEC_SERVICE_ID, false);
152 EXPECT_NE(server, nullptr);
153 server = nullptr;
154 }
155
156 /**
157 * @tc.name: AVCodec_Server_Constructor_003
158 * @tc.desc: id and bool is matched
159 */
160 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_Constructor_003, TestSize.Level1)
161 {
162 EXPECT_CALL(*saMock_, SystemAbilityCtor(1, false)).Times(1);
163 EXPECT_CALL(*saMock_, SystemAbilityDtor()).Times(1);
164 EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubCtor()).Times(1);
165 EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubDtor()).Times(1);
166
167 auto server = std::make_shared<AVCodecServer>(1, false);
168 EXPECT_NE(server, nullptr);
169 server = nullptr;
170 }
171
172 /**
173 * @tc.name: AVCodec_Server_OnStart_001
174 * @tc.desc: 1. will once publish success
175 */
176 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_OnStart_001, TestSize.Level1)
177 {
178 EXPECT_CALL(*saMock_, AddSystemAbilityListener(MEMORY_MANAGER_SA_ID)).Times(1);
179 auto server = CreateAVCodecServer();
180 ASSERT_NE(server, nullptr);
181
182 EXPECT_CALL(*saMock_, Publish(server.get())).Times(1).WillOnce(Return(true));
183 server->OnStart();
184 server = nullptr;
185 }
186
187 /**
188 * @tc.name: AVCodec_Server_OnStart_002
189 * @tc.desc: 1. will once publish fail
190 */
191 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_OnStart_002, TestSize.Level1)
192 {
193 EXPECT_CALL(*saMock_, AddSystemAbilityListener(MEMORY_MANAGER_SA_ID)).Times(1);
194 auto server = CreateAVCodecServer();
195 ASSERT_NE(server, nullptr);
196
197 EXPECT_CALL(*saMock_, Publish(server.get())).Times(1).WillOnce(Return(false));
198 server->OnStart();
199 server = nullptr;
200 }
201
202 /**
203 * @tc.name: AVCodec_Server_OnStop_001
204 * @tc.desc: 1. will once NotifyProcessStatus = 0 success
205 */
206 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_OnStop_001, TestSize.Level1)
207 {
208 auto server = CreateAVCodecServer();
209 ASSERT_NE(server, nullptr);
210
211 server->OnStop();
212 server = nullptr;
213 }
214
215 /**
216 * @tc.name: AVCodec_Server_OnAddSystemAbility_001
217 * @tc.desc: 1. will once NotifyProcessStatus = 1 success
218 */
219 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_OnAddSystemAbility_001, TestSize.Level1)
220 {
221 auto server = CreateAVCodecServer();
222 ASSERT_NE(server, nullptr);
223
224 server->OnAddSystemAbility(MEMORY_MANAGER_SA_ID, "testId");
225 server = nullptr;
226 }
227
228 /**
229 * @tc.name: AVCodec_Server_GetSubSystemAbility_001
230 * @tc.desc: GetSubSystemAbility will twice SetDeathListener succuss
231 */
232 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_GetSubSystemAbility_001, TestSize.Level1)
233 {
234 AVCodecServerManager &manager = AVCodecServerManager::GetInstance();
235 IStandardAVCodecService::AVCodecSystemAbility subSystemId;
236 sptr<IRemoteObject> listener = sptr<IRemoteObject>(new AVCodecListenerStub());
237
238 EXPECT_CALL(*avcodecStubMock_, SetDeathListener(listener)).Times(2).WillRepeatedly(Return(AVCS_ERR_OK));
239 EXPECT_CALL(*codecStubMock_, Create()).Times(1).WillOnce(Return(new CodecServiceStub()));
240 EXPECT_CALL(*codecStubMock_, CodecServiceStubDtor()).Times(1);
241 EXPECT_CALL(*codeclistStubMock_, Create()).Times(1).WillOnce(Return(new CodecListServiceStub()));
242 EXPECT_CALL(*codeclistStubMock_, CodecListServiceStubDtor()).Times(1);
243
244 auto server = CreateAVCodecServer();
245 ASSERT_NE(server, nullptr);
246
247 subSystemId = IStandardAVCodecService::AVCODEC_CODEC;
248 sptr<IRemoteObject> codecStub = nullptr;
249 int32_t ret = server->GetSubSystemAbility(subSystemId, listener, codecStub);
250 EXPECT_NE(codecStub, nullptr);
251 EXPECT_EQ(ret, AVCS_ERR_OK);
252 manager.DestroyStubObject(AVCodecServerManager::StubType::CODEC, codecStub);
253
254 subSystemId = IStandardAVCodecService::AVCODEC_CODECLIST;
255 sptr<IRemoteObject> codeclistStub = nullptr;
256 ret = server->GetSubSystemAbility(subSystemId, listener, codeclistStub);
257 EXPECT_NE(codeclistStub, nullptr);
258 EXPECT_EQ(ret, AVCS_ERR_OK);
259 manager.DestroyStubObject(AVCodecServerManager::StubType::CODECLIST, codeclistStub);
260
261 server = nullptr;
262 }
263
264 /**
265 * @tc.name: AVCodec_Server_GetSubSystemAbility_002
266 * @tc.desc: GetSubSystemAbility will twice SetDeathListener fail
267 */
268 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_GetSubSystemAbility_002, TestSize.Level1)
269 {
270 IStandardAVCodecService::AVCodecSystemAbility subSystemId;
271 sptr<IRemoteObject> listener = sptr<IRemoteObject>(new AVCodecListenerStub());
272 EXPECT_CALL(*avcodecStubMock_, SetDeathListener(listener))
273 .Times(2)
274 .WillRepeatedly(Return(AVCS_ERR_INVALID_OPERATION));
275
276 EXPECT_CALL(*codecStubMock_, Create()).Times(1).WillOnce(Return(new CodecServiceStub()));
277 EXPECT_CALL(*codecStubMock_, CodecServiceStubDtor()).Times(1);
278 EXPECT_CALL(*codeclistStubMock_, Create()).Times(1).WillOnce(Return(new CodecListServiceStub()));
279 EXPECT_CALL(*codeclistStubMock_, CodecListServiceStubDtor()).Times(1);
280
281 auto server = CreateAVCodecServer();
282 ASSERT_NE(server, nullptr);
283
284 subSystemId = IStandardAVCodecService::AVCODEC_CODEC;
285 sptr<IRemoteObject> codecStub = nullptr;
286 int32_t ret = server->GetSubSystemAbility(subSystemId, listener, codecStub);
287 EXPECT_EQ(codecStub, nullptr);
288 EXPECT_NE(ret, AVCS_ERR_OK);
289
290 subSystemId = IStandardAVCodecService::AVCODEC_CODECLIST;
291 sptr<IRemoteObject> codeclistStub = nullptr;
292 ret = server->GetSubSystemAbility(subSystemId, listener, codeclistStub);
293 EXPECT_EQ(codeclistStub, nullptr);
294 EXPECT_NE(ret, AVCS_ERR_OK);
295 server = nullptr;
296 }
297
298 /**
299 * @tc.name: AVCodec_Server_GetSubSystemAbility_003
300 * @tc.desc: GetSubSystemAbility will twice Stub create fail
301 */
302 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_GetSubSystemAbility_003, TestSize.Level1)
303 {
304 IStandardAVCodecService::AVCodecSystemAbility subSystemId;
305 sptr<IRemoteObject> listener = sptr<IRemoteObject>(new AVCodecListenerStub());
306
307 EXPECT_CALL(*codecStubMock_, Create()).Times(1).WillOnce(nullptr);
308 EXPECT_CALL(*codeclistStubMock_, Create()).Times(1).WillOnce(nullptr);
309
310 auto server = CreateAVCodecServer();
311 ASSERT_NE(server, nullptr);
312
313 subSystemId = IStandardAVCodecService::AVCODEC_CODEC;
314 sptr<IRemoteObject> codecStub = nullptr;
315 int32_t ret = server->GetSubSystemAbility(subSystemId, listener, codecStub);
316 EXPECT_EQ(codecStub, nullptr);
317 EXPECT_NE(ret, AVCS_ERR_OK);
318
319 subSystemId = IStandardAVCodecService::AVCODEC_CODECLIST;
320 sptr<IRemoteObject> codeclistStub = nullptr;
321 ret = server->GetSubSystemAbility(subSystemId, listener, codeclistStub);
322 EXPECT_EQ(codeclistStub, nullptr);
323 EXPECT_NE(ret, AVCS_ERR_OK);
324 server = nullptr;
325 }
326
327 /**
328 * @tc.name: AVCodec_Server_GetSubSystemAbility_004
329 * @tc.desc: GetSubSystemAbility invalid subSystemId
330 */
331 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_GetSubSystemAbility_004, TestSize.Level1)
332 {
333 auto subSystemId = static_cast<IStandardAVCodecService::AVCodecSystemAbility>(INVALID_NUM);
334 sptr<IRemoteObject> listener = sptr<IRemoteObject>(new AVCodecListenerStub());
335
336 EXPECT_CALL(*codecStubMock_, Create()).Times(0);
337 EXPECT_CALL(*codeclistStubMock_, Create()).Times(0);
338
339 auto server = CreateAVCodecServer();
340 ASSERT_NE(server, nullptr);
341
342 sptr<IRemoteObject> codecStub = nullptr;
343 int32_t ret = server->GetSubSystemAbility(subSystemId, listener, codecStub);
344 EXPECT_EQ(codecStub, nullptr);
345 EXPECT_NE(ret, AVCS_ERR_OK);
346 server = nullptr;
347 }
348
349 /**
350 * @tc.name: AVCodec_Server_GetSubSystemAbility_005
351 * @tc.desc: server manager createobject
352 */
353 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_GetSubSystemAbility_005, TestSize.Level1)
354 {
355 AVCodecServerManager &manager = AVCodecServerManager::GetInstance();
356 sptr<IRemoteObject> codecStub = nullptr;
357 EXPECT_NE(manager.CreateStubObject(static_cast<AVCodecServerManager::StubType>(INVALID_NUM), codecStub),
358 AVCS_ERR_OK);
359 EXPECT_EQ(codecStub, nullptr);
360 }
361
PrintAndCloseFd(int32_t fd)362 void PrintAndCloseFd(int32_t fd)
363 {
364 int32_t fileSize = lseek(fd, 0, SEEK_END);
365 std::string str = std::string(fileSize + 1, ' ');
366 lseek(fd, 0, SEEK_SET);
367 read(fd, str.data(), fileSize);
368 std::cout << "dump str: \n==========\n" << str << "\n==========\n";
369 close(fd);
370 }
371
372 /**
373 * @tc.name: AVCodec_Server_Dump_001
374 * @tc.desc: DumpInfo will once Dump success
375 */
376 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_Dump_001, TestSize.Level1)
377 {
378 auto server = CreateAVCodecServer();
379 std::vector<std::u16string> args = {u"All"};
380 int32_t fileFd = open(DUMP_FILE_PATH.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP);
381 EXPECT_CALL(*codecStubMock_, Dump).Times(AtLeast(1)).WillRepeatedly(Return(OHOS::NO_ERROR));
382
383 (void)CreateCodecServiceStub(server);
384 EXPECT_EQ(server->Dump(fileFd, args), OHOS::NO_ERROR);
385 PrintAndCloseFd(fileFd);
386 }
387
388 /**
389 * @tc.name: AVCodec_ServerManager_DestroyStubObjectForPid_001
390 * @tc.desc:
391 * 1. server manager DestroyStubObjectForPid
392 * 2. video decoder without forward caller
393 */
394 HWTEST_F(SaAVCodecUnitTest, AVCodec_ServerManager_DestroyStubObjectForPid_001, TestSize.Level1)
395 {
396 AVCodecServerManager &manager = AVCodecServerManager::GetInstance();
397 const int32_t callerId = 123;
398 InstanceInfo vdecInfo = {
399 .codecType = AVCODEC_TYPE_VIDEO_DECODER,
400 .caller = {.pid = callerId},
401 };
402 auto server = CreateAVCodecServer();
403 ASSERT_NE(server, nullptr);
404 manager.codecStubMap_.emplace(callerId, std::make_pair(nullptr, vdecInfo));
405 manager.DestroyStubObjectForPid(INVALID_NUM);
406 manager.DestroyStubObjectForPid(callerId);
407 }
408
409 /**
410 * @tc.name: AVCodec_ServerManager_DestroyStubObjectForPid_002
411 * @tc.desc:
412 * 1. server manager DestroyStubObjectForPid
413 * 2. video decoder with forward caller
414 */
415 HWTEST_F(SaAVCodecUnitTest, AVCodec_ServerManager_DestroyStubObjectForPid_002, TestSize.Level1)
416 {
417 AVCodecServerManager &manager = AVCodecServerManager::GetInstance();
418 const int32_t callerId = 123;
419 const int32_t fowardCallerId = 1234;
420 InstanceInfo vdecInfo = {
421 .codecType = AVCODEC_TYPE_VIDEO_DECODER,
422 .caller = {.pid = callerId},
423 .forwardCaller = {.pid = fowardCallerId},
424 };
425 auto server = CreateAVCodecServer();
426 ASSERT_NE(server, nullptr);
427 manager.codecStubMap_.emplace(callerId, std::make_pair(nullptr, vdecInfo));
428 manager.DestroyStubObjectForPid(INVALID_NUM);
429 manager.DestroyStubObjectForPid(callerId);
430 }
431
432 /**
433 * @tc.name: AVCodec_ServerManager_DestroyStubObjectForPid_003
434 * @tc.desc:
435 * 1. server manager DestroyStubObjectForPid
436 * 2. video encoder without forward caller
437 */
438 HWTEST_F(SaAVCodecUnitTest, AVCodec_ServerManager_DestroyStubObjectForPid_003, TestSize.Level1)
439 {
440 AVCodecServerManager &manager = AVCodecServerManager::GetInstance();
441 const int32_t callerId = 123;
442 InstanceInfo vdecInfo = {
443 .codecType = AVCODEC_TYPE_VIDEO_ENCODER,
444 .caller = {.pid = callerId},
445 };
446 auto server = CreateAVCodecServer();
447 ASSERT_NE(server, nullptr);
448 manager.codecStubMap_.emplace(callerId, std::make_pair(nullptr, vdecInfo));
449 manager.DestroyStubObjectForPid(INVALID_NUM);
450 manager.DestroyStubObjectForPid(callerId);
451 }
452
453 /**
454 * @tc.name: AVCodec_ServerManager_DestroyStubObjectForPid_004
455 * @tc.desc:
456 * 1. server manager DestroyStubObjectForPid
457 * 2. video encoder with forward caller
458 */
459 HWTEST_F(SaAVCodecUnitTest, AVCodec_ServerManager_DestroyStubObjectForPid_004, TestSize.Level1)
460 {
461 AVCodecServerManager &manager = AVCodecServerManager::GetInstance();
462 const int32_t callerId = 123;
463 const int32_t fowardCallerId = 1234;
464 InstanceInfo vdecInfo = {
465 .codecType = AVCODEC_TYPE_VIDEO_ENCODER,
466 .caller = {.pid = callerId},
467 .forwardCaller = {.pid = fowardCallerId},
468 };
469 auto server = CreateAVCodecServer();
470 ASSERT_NE(server, nullptr);
471 manager.codecStubMap_.emplace(callerId, std::make_pair(nullptr, vdecInfo));
472 manager.DestroyStubObjectForPid(INVALID_NUM);
473 manager.DestroyStubObjectForPid(callerId);
474 }
475 } // namespace