• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "codeccommon_fuzzer.h"
17 
18 using namespace OHOS::HDI::Codec::V1_0;
19 
20 namespace OHOS {
21 namespace Codec {
22     static const int32_t DATA_BUFFERID = 10;
23     static const int32_t DATA_SIZE = 20;
24     static const int32_t DATA_VERSION_NVERSION = 30;
25     static const int32_t DATA_BUFFERTYPE = 40;
26     static const int32_t DATA_ALLOCLEN = 60;
27     static const int32_t DATA_FILLEDLEN = 70;
28     static const int32_t DATA_OFFSET = 80;
29     static const int32_t DATA_FENCEFD = 90;
30     static const int32_t DATA_TYPE = 100;
31     static const int32_t DATA_PTS = 200;
32     static const int32_t DATA_FLAG = 300;
33     static const int32_t TESTING_APP_DATA = 33;
34 
35     uint32_t g_componentId = 0;
36     static int32_t g_appData = TESTING_APP_DATA;
37 
Release()38     void Release()
39     {
40         g_component = nullptr;
41         g_callback = nullptr;
42         g_manager = nullptr;
43     }
44 
FillDataOmxCodecBuffer(struct OmxCodecBuffer * dataFuzz)45     void FillDataOmxCodecBuffer(struct OmxCodecBuffer *dataFuzz)
46     {
47         dataFuzz->bufferId = DATA_BUFFERID;
48         dataFuzz->size = DATA_SIZE;
49         dataFuzz->version.nVersion = DATA_VERSION_NVERSION;
50         dataFuzz->bufferType = static_cast<enum CodecBufferType>(DATA_BUFFERTYPE);
51         dataFuzz->bufferhandle = nullptr;
52         dataFuzz->fd = -1;
53         dataFuzz->allocLen = DATA_ALLOCLEN;
54         dataFuzz->filledLen = DATA_FILLEDLEN;
55         dataFuzz->offset = DATA_OFFSET;
56         dataFuzz->fenceFd = DATA_FENCEFD;
57         dataFuzz->type = static_cast<enum ShareMemTypes>(DATA_TYPE);
58         dataFuzz->pts = DATA_PTS;
59         dataFuzz->flag = DATA_FLAG;
60     }
61 
Preconditions()62     bool Preconditions()
63     {
64         g_manager = ICodecComponentManager::Get(false);
65         if (g_manager == nullptr) {
66             HDF_LOGE("%{public}s: ICodecComponentManager failed", __func__);
67             return false;
68         }
69 
70         g_callback = new CodecCallbackFuzz();
71         if (g_callback == nullptr) {
72             HDF_LOGE("%{public}s: codeccallback_fuzzer failed", __func__);
73             Release();
74             return false;
75         }
76 
77         int32_t count = 0;
78         auto err = g_manager->GetComponentNum(count);
79         if (err != HDF_SUCCESS || count <= 0) {
80             HDF_LOGE("%{public}s GetComponentNum return %{public}d, count = %{public}d", __func__, err, count);
81             Release();
82             return HDF_FAILURE;
83         }
84 
85         std::vector<CodecCompCapability> caps;
86         err = g_manager->GetComponentCapabilityList(caps, count);
87         if (err != HDF_SUCCESS) {
88             HDF_LOGE("%{public}s GetComponentCapabilityList return %{public}d", __func__, err);
89             Release();
90             return err;
91         }
92 
93         int32_t ret = g_manager->CreateComponent(g_component, g_componentId, caps[0].compName, g_appData, g_callback);
94         if (ret != HDF_SUCCESS) {
95             HDF_LOGE("%{public}s: CreateComponent failed\n", __func__);
96             Release();
97             return false;
98         }
99 
100         return true;
101     }
102 
Destroy()103     bool Destroy()
104     {
105         if (g_manager == nullptr) {
106             HDF_LOGE("%{public}s: ICodecComponentManager failed", __func__);
107             return false;
108         }
109 
110         int32_t ret = g_manager->DestroyComponent(g_componentId);
111         if (ret != HDF_SUCCESS) {
112             HDF_LOGE("%{public}s: DestroyComponent failed\n", __func__);
113             Release();
114             return false;
115         }
116         Release();
117         return true;
118     }
119 } // namespace codec
120 } // namespace OHOS
121