• 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 <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     void CreateCodecServiceStub(std::shared_ptr<AVCodecServer> &server);
51 
52     std::shared_ptr<SystemAbilityMock> saMock_;
53     std::shared_ptr<MemMgrClientMock> memMgrMock_;
54     std::shared_ptr<AVCodecServiceStubMock> avcodecStubMock_;
55     std::shared_ptr<CodecServiceStubMock> codecStubMock_;
56     std::shared_ptr<CodecListServiceStubMock> codeclistStubMock_;
57 
58 private:
59     std::vector<std::pair<AVCodecServerManager::StubType, sptr<IRemoteObject>>> stubList_;
60 };
61 
SetUpTestCase(void)62 void SaAVCodecUnitTest::SetUpTestCase(void) {}
63 
TearDownTestCase(void)64 void SaAVCodecUnitTest::TearDownTestCase(void) {}
65 
SetUp(void)66 void SaAVCodecUnitTest::SetUp(void)
67 {
68     saMock_ = std::make_shared<SystemAbilityMock>();
69     SystemAbility::RegisterMock(saMock_);
70     memMgrMock_ = std::make_shared<MemMgrClientMock>();
71     MemMgrClient::RegisterMock(memMgrMock_);
72     avcodecStubMock_ = std::make_shared<AVCodecServiceStubMock>();
73     AVCodecServiceStub::RegisterMock(avcodecStubMock_);
74     codecStubMock_ = std::make_shared<CodecServiceStubMock>();
75     CodecServiceStub::RegisterMock(codecStubMock_);
76     codeclistStubMock_ = std::make_shared<CodecListServiceStubMock>();
77     CodecListServiceStub::RegisterMock(codeclistStubMock_);
78 }
79 
TearDown(void)80 void SaAVCodecUnitTest::TearDown(void)
81 {
82     AVCodecServerManager &manager = AVCodecServerManager::GetInstance();
83     for (auto &val : stubList_) {
84         manager.DestroyStubObject(val.first, val.second);
85     }
86     stubList_.clear();
87     saMock_ = nullptr;
88     codecStubMock_ = nullptr;
89     codeclistStubMock_ = nullptr;
90 }
91 
CreateCodecServiceStub(std::shared_ptr<AVCodecServer> & server)92 void SaAVCodecUnitTest::CreateCodecServiceStub(std::shared_ptr<AVCodecServer> &server)
93 {
94     sptr<IRemoteObject> listener = sptr<IRemoteObject>(new AVCodecListenerStub());
95     EXPECT_CALL(*saMock_, SystemAbilityCtor(AV_CODEC_SERVICE_ID, true)).Times(1);
96     EXPECT_CALL(*saMock_, SystemAbilityDtor()).Times(1);
97     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubCtor()).Times(1);
98     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubDtor()).Times(1);
99     EXPECT_CALL(*avcodecStubMock_, SetDeathListener(listener)).Times(1).WillOnce(Return(AVCS_ERR_OK));
100     EXPECT_CALL(*codecStubMock_, Create()).Times(AtLeast(1)).WillOnce(Return(new CodecServiceStub()));
101     EXPECT_CALL(*codecStubMock_, CodecServiceStubDtor()).Times(AtLeast(1));
102 
103     server = std::make_shared<AVCodecServer>(AV_CODEC_SERVICE_ID, true);
104     EXPECT_NE(server, nullptr);
105     sptr<IRemoteObject> codecStub = nullptr;
106     int32_t ret = server->GetSubSystemAbility(IStandardAVCodecService::AVCODEC_CODEC, listener, codecStub);
107     EXPECT_NE(codecStub, nullptr);
108     EXPECT_EQ(ret, AVCS_ERR_OK);
109     stubList_.emplace_back(AVCodecServerManager::StubType::CODEC, codecStub);
110 }
111 
112 /**
113  * @tc.name: AVCodec_Server_Constructor_001
114  * @tc.desc: id and bool is matched
115  */
116 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_Constructor_001, TestSize.Level1)
117 {
118     EXPECT_CALL(*saMock_, SystemAbilityCtor(AV_CODEC_SERVICE_ID, true)).Times(1);
119     EXPECT_CALL(*saMock_, SystemAbilityDtor()).Times(1);
120     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubCtor()).Times(1);
121     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubDtor()).Times(1);
122 
123     auto server = std::make_shared<AVCodecServer>(AV_CODEC_SERVICE_ID, true);
124     EXPECT_NE(server, nullptr);
125     server = nullptr;
126 }
127 
128 /**
129  * @tc.name: AVCodec_Server_Constructor_002
130  * @tc.desc: id and bool is matched
131  */
132 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_Constructor_002, TestSize.Level1)
133 {
134     EXPECT_CALL(*saMock_, SystemAbilityCtor(AV_CODEC_SERVICE_ID, false)).Times(1);
135     EXPECT_CALL(*saMock_, SystemAbilityDtor()).Times(1);
136     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubCtor()).Times(1);
137     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubDtor()).Times(1);
138 
139     auto server = std::make_shared<AVCodecServer>(AV_CODEC_SERVICE_ID, false);
140     EXPECT_NE(server, nullptr);
141     server = nullptr;
142 }
143 
144 /**
145  * @tc.name: AVCodec_Server_Constructor_003
146  * @tc.desc: id and bool is matched
147  */
148 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_Constructor_003, TestSize.Level1)
149 {
150     EXPECT_CALL(*saMock_, SystemAbilityCtor(1, false)).Times(1);
151     EXPECT_CALL(*saMock_, SystemAbilityDtor()).Times(1);
152     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubCtor()).Times(1);
153     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubDtor()).Times(1);
154 
155     auto server = std::make_shared<AVCodecServer>(1, false);
156     EXPECT_NE(server, nullptr);
157     server = nullptr;
158 }
159 
160 /**
161  * @tc.name: AVCodec_Server_OnStart_001
162  * @tc.desc: 1. will once publish success
163  */
164 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_OnStart_001, TestSize.Level1)
165 {
166     EXPECT_CALL(*saMock_, SystemAbilityCtor(AV_CODEC_SERVICE_ID, true)).Times(1);
167     EXPECT_CALL(*saMock_, SystemAbilityDtor()).Times(1);
168     EXPECT_CALL(*saMock_, AddSystemAbilityListener(MEMORY_MANAGER_SA_ID)).Times(1);
169     EXPECT_CALL(*saMock_, AddSystemAbilityListener(SUSPEND_MANAGER_SYSTEM_ABILITY_ID)).Times(1);
170     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubCtor()).Times(1);
171     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubDtor()).Times(1);
172 
173     auto server = std::make_shared<AVCodecServer>(AV_CODEC_SERVICE_ID, true);
174     EXPECT_NE(server, nullptr);
175 
176     EXPECT_CALL(*saMock_, Publish(server.get())).Times(1).WillOnce(Return(true));
177     server->OnStart();
178     server = nullptr;
179 }
180 
181 /**
182  * @tc.name: AVCodec_Server_OnStart_002
183  * @tc.desc: 1. will once publish fail
184  */
185 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_OnStart_002, TestSize.Level1)
186 {
187     EXPECT_CALL(*saMock_, SystemAbilityCtor(AV_CODEC_SERVICE_ID, true)).Times(1);
188     EXPECT_CALL(*saMock_, SystemAbilityDtor()).Times(1);
189     EXPECT_CALL(*saMock_, AddSystemAbilityListener(MEMORY_MANAGER_SA_ID)).Times(1);
190     EXPECT_CALL(*saMock_, AddSystemAbilityListener(SUSPEND_MANAGER_SYSTEM_ABILITY_ID)).Times(1);
191     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubCtor()).Times(1);
192     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubDtor()).Times(1);
193 
194     auto server = std::make_shared<AVCodecServer>(AV_CODEC_SERVICE_ID, true);
195     EXPECT_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     EXPECT_CALL(*saMock_, SystemAbilityCtor(AV_CODEC_SERVICE_ID, true)).Times(1);
209     EXPECT_CALL(*saMock_, SystemAbilityDtor()).Times(1);
210     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubCtor()).Times(1);
211     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubDtor()).Times(1);
212 
213     auto server = std::make_shared<AVCodecServer>(AV_CODEC_SERVICE_ID, true);
214     EXPECT_NE(server, nullptr);
215 
216     server->OnStop();
217     server = nullptr;
218 }
219 
220 /**
221  * @tc.name: AVCodec_Server_OnAddSystemAbility_001
222  * @tc.desc: 1. will once NotifyProcessStatus = 1 success
223  */
224 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_OnAddSystemAbility_001, TestSize.Level1)
225 {
226     EXPECT_CALL(*saMock_, SystemAbilityCtor(AV_CODEC_SERVICE_ID, true)).Times(1);
227     EXPECT_CALL(*saMock_, SystemAbilityDtor()).Times(1);
228     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubCtor()).Times(1);
229     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubDtor()).Times(1);
230 
231     auto server = std::make_shared<AVCodecServer>(AV_CODEC_SERVICE_ID, true);
232     EXPECT_NE(server, nullptr);
233 
234     server->OnAddSystemAbility(MEMORY_MANAGER_SA_ID, "testId");
235     server = nullptr;
236 }
237 
238 /**
239  * @tc.name: AVCodec_Server_GetSubSystemAbility_001
240  * @tc.desc: GetSubSystemAbility will twice SetDeathListener succuss
241  */
242 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_GetSubSystemAbility_001, TestSize.Level1)
243 {
244     AVCodecServerManager &manager = AVCodecServerManager::GetInstance();
245     IStandardAVCodecService::AVCodecSystemAbility subSystemId;
246     sptr<IRemoteObject> listener = sptr<IRemoteObject>(new AVCodecListenerStub());
247 
248     EXPECT_CALL(*saMock_, SystemAbilityCtor(AV_CODEC_SERVICE_ID, true)).Times(1);
249     EXPECT_CALL(*saMock_, SystemAbilityDtor()).Times(1);
250     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubCtor()).Times(1);
251     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubDtor()).Times(1);
252     EXPECT_CALL(*avcodecStubMock_, SetDeathListener(listener)).Times(2).WillRepeatedly(Return(AVCS_ERR_OK));
253 
254     EXPECT_CALL(*codecStubMock_, Create()).Times(1).WillOnce(Return(new CodecServiceStub()));
255     EXPECT_CALL(*codecStubMock_, CodecServiceStubDtor()).Times(1);
256     EXPECT_CALL(*codeclistStubMock_, Create()).Times(1).WillOnce(Return(new CodecListServiceStub()));
257     EXPECT_CALL(*codeclistStubMock_, CodecListServiceStubDtor()).Times(1);
258     auto server = std::make_shared<AVCodecServer>(AV_CODEC_SERVICE_ID, true);
259     EXPECT_NE(server, nullptr);
260 
261     subSystemId = IStandardAVCodecService::AVCODEC_CODEC;
262     sptr<IRemoteObject> codecStub = nullptr;
263     int32_t ret = server->GetSubSystemAbility(subSystemId, listener, codecStub);
264     EXPECT_NE(codecStub, nullptr);
265     EXPECT_EQ(ret, AVCS_ERR_OK);
266     manager.DestroyStubObject(AVCodecServerManager::StubType::CODEC, codecStub);
267 
268     subSystemId = IStandardAVCodecService::AVCODEC_CODECLIST;
269     sptr<IRemoteObject> codeclistStub = nullptr;
270     ret = server->GetSubSystemAbility(subSystemId, listener, codeclistStub);
271     EXPECT_NE(codeclistStub, nullptr);
272     EXPECT_EQ(ret, AVCS_ERR_OK);
273     manager.DestroyStubObject(AVCodecServerManager::StubType::CODECLIST, codeclistStub);
274 
275     server = nullptr;
276 }
277 
278 /**
279  * @tc.name: AVCodec_Server_GetSubSystemAbility_002
280  * @tc.desc: GetSubSystemAbility will twice SetDeathListener fail
281  */
282 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_GetSubSystemAbility_002, TestSize.Level1)
283 {
284     IStandardAVCodecService::AVCodecSystemAbility subSystemId;
285     sptr<IRemoteObject> listener = sptr<IRemoteObject>(new AVCodecListenerStub());
286 
287     EXPECT_CALL(*saMock_, SystemAbilityCtor(AV_CODEC_SERVICE_ID, true)).Times(1);
288     EXPECT_CALL(*saMock_, SystemAbilityDtor()).Times(1);
289     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubCtor()).Times(1);
290     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubDtor()).Times(1);
291     EXPECT_CALL(*avcodecStubMock_, SetDeathListener(listener))
292         .Times(2)
293         .WillRepeatedly(Return(AVCS_ERR_INVALID_OPERATION));
294     auto server = std::make_shared<AVCodecServer>(AV_CODEC_SERVICE_ID, true);
295 
296     EXPECT_CALL(*codecStubMock_, Create()).Times(1).WillOnce(Return(new CodecServiceStub()));
297     EXPECT_CALL(*codecStubMock_, CodecServiceStubDtor()).Times(1);
298     EXPECT_CALL(*codeclistStubMock_, Create()).Times(1).WillOnce(Return(new CodecListServiceStub()));
299     EXPECT_CALL(*codeclistStubMock_, CodecListServiceStubDtor()).Times(1);
300     EXPECT_NE(server, nullptr);
301 
302     subSystemId = IStandardAVCodecService::AVCODEC_CODEC;
303     sptr<IRemoteObject> codecStub = nullptr;
304     int32_t ret = server->GetSubSystemAbility(subSystemId, listener, codecStub);
305     EXPECT_EQ(codecStub, nullptr);
306     EXPECT_NE(ret, AVCS_ERR_OK);
307 
308     subSystemId = IStandardAVCodecService::AVCODEC_CODECLIST;
309     sptr<IRemoteObject> codeclistStub = nullptr;
310     ret = server->GetSubSystemAbility(subSystemId, listener, codeclistStub);
311     EXPECT_EQ(codeclistStub, nullptr);
312     EXPECT_NE(ret, AVCS_ERR_OK);
313     server = nullptr;
314 }
315 
316 /**
317  * @tc.name: AVCodec_Server_GetSubSystemAbility_003
318  * @tc.desc: GetSubSystemAbility will twice Stub create fail
319  */
320 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_GetSubSystemAbility_003, TestSize.Level1)
321 {
322     IStandardAVCodecService::AVCodecSystemAbility subSystemId;
323     sptr<IRemoteObject> listener = sptr<IRemoteObject>(new AVCodecListenerStub());
324     EXPECT_CALL(*saMock_, SystemAbilityCtor(AV_CODEC_SERVICE_ID, true)).Times(1);
325     EXPECT_CALL(*saMock_, SystemAbilityDtor()).Times(1);
326     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubCtor()).Times(1);
327     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubDtor()).Times(1);
328 
329     EXPECT_CALL(*codecStubMock_, Create()).Times(1).WillOnce(nullptr);
330     EXPECT_CALL(*codeclistStubMock_, Create()).Times(1).WillOnce(nullptr);
331     auto server = std::make_shared<AVCodecServer>(AV_CODEC_SERVICE_ID, true);
332     EXPECT_NE(server, nullptr);
333 
334     subSystemId = IStandardAVCodecService::AVCODEC_CODEC;
335     sptr<IRemoteObject> codecStub = nullptr;
336     int32_t ret = server->GetSubSystemAbility(subSystemId, listener, codecStub);
337     EXPECT_EQ(codecStub, nullptr);
338     EXPECT_NE(ret, AVCS_ERR_OK);
339 
340     subSystemId = IStandardAVCodecService::AVCODEC_CODECLIST;
341     sptr<IRemoteObject> codeclistStub = nullptr;
342     ret = server->GetSubSystemAbility(subSystemId, listener, codeclistStub);
343     EXPECT_EQ(codeclistStub, nullptr);
344     EXPECT_NE(ret, AVCS_ERR_OK);
345     server = nullptr;
346 }
347 
348 /**
349  * @tc.name: AVCodec_Server_GetSubSystemAbility_004
350  * @tc.desc: GetSubSystemAbility invalid subSystemId
351  */
352 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_GetSubSystemAbility_004, TestSize.Level1)
353 {
354     auto subSystemId = static_cast<IStandardAVCodecService::AVCodecSystemAbility>(INVALID_NUM);
355     sptr<IRemoteObject> listener = sptr<IRemoteObject>(new AVCodecListenerStub());
356     EXPECT_CALL(*saMock_, SystemAbilityCtor(AV_CODEC_SERVICE_ID, true)).Times(1);
357     EXPECT_CALL(*saMock_, SystemAbilityDtor()).Times(1);
358     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubCtor()).Times(1);
359     EXPECT_CALL(*avcodecStubMock_, AVCodecServiceStubDtor()).Times(1);
360 
361     EXPECT_CALL(*codecStubMock_, Create()).Times(0);
362     EXPECT_CALL(*codeclistStubMock_, Create()).Times(0);
363     auto server = std::make_shared<AVCodecServer>(AV_CODEC_SERVICE_ID, true);
364     EXPECT_NE(server, nullptr);
365 
366     sptr<IRemoteObject> codecStub = nullptr;
367     int32_t ret = server->GetSubSystemAbility(subSystemId, listener, codecStub);
368     EXPECT_EQ(codecStub, nullptr);
369     EXPECT_NE(ret, AVCS_ERR_OK);
370     server = nullptr;
371 }
372 
373 /**
374  * @tc.name: AVCodec_Server_GetSubSystemAbility_005
375  * @tc.desc: server manager createobject
376  */
377 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_GetSubSystemAbility_005, TestSize.Level1)
378 {
379     AVCodecServerManager &manager = AVCodecServerManager::GetInstance();
380     sptr<IRemoteObject> codecStub = nullptr;
381     EXPECT_NE(manager.CreateStubObject(static_cast<AVCodecServerManager::StubType>(INVALID_NUM), codecStub),
382               AVCS_ERR_OK);
383     EXPECT_EQ(codecStub, nullptr);
384 }
385 
PrintAndCloseFd(int32_t fd)386 void PrintAndCloseFd(int32_t fd)
387 {
388     int32_t fileSize = lseek(fd, 0, SEEK_END);
389     std::string str = std::string(fileSize + 1, ' ');
390     lseek(fd, 0, SEEK_SET);
391     read(fd, str.data(), fileSize);
392     std::cout << "dump str: \n==========\n" << str << "\n==========\n";
393     close(fd);
394 }
395 
396 /**
397  * @tc.name: AVCodec_Server_Dump_001
398  * @tc.desc: DumpInfo will once Dump success
399  */
400 HWTEST_F(SaAVCodecUnitTest, AVCodec_Server_Dump_001, TestSize.Level1)
401 {
402     std::shared_ptr<AVCodecServer> server = nullptr;
403     std::vector<std::u16string> args = {u"All"};
404     int32_t fileFd = open(DUMP_FILE_PATH.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP);
405     EXPECT_CALL(*codecStubMock_, Dump).Times(AtLeast(1)).WillRepeatedly(Return(OHOS::NO_ERROR));
406 
407     CreateCodecServiceStub(server);
408     EXPECT_EQ(server->Dump(fileFd, args), OHOS::NO_ERROR);
409     PrintAndCloseFd(fileFd);
410 }
411 } // namespace