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 #include <atomic>
16 #include <iostream>
17 #include <fstream>
18 #include <queue>
19 #include <string>
20 #include <thread>
21 #include "gtest/gtest.h"
22 #include "common_tool.h"
23 #include "avcodec_audio_avbuffer_decoder_demo.h"
24
25 using namespace std;
26 using namespace testing::ext;
27 using namespace OHOS::MediaAVCodec;
28 using namespace OHOS;
29 using namespace OHOS::MediaAVCodec::AudioBufferDemo;
30
31 namespace {
32 class FuzzTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp() override;
37 void TearDown() override;
38 };
39
SetUpTestCase()40 void FuzzTest::SetUpTestCase() {}
TearDownTestCase()41 void FuzzTest::TearDownTestCase() {}
SetUp()42 void FuzzTest::SetUp() {}
TearDown()43 void FuzzTest::TearDown() {}
44
45 } // namespace
46
47 /**
48 * @tc.number : FUZZ_TEST_001
49 * @tc.name : CreateByMime - mime fuzz test
50 * @tc.desc : param fuzz test
51 */
52 HWTEST_F(FuzzTest, FUZZ_TEST_001, TestSize.Level2)
53 {
54 OH_AVCodec *codec = nullptr;
55 OH_AVErrCode result0;
56 ADecBufferDemo *aDecBufferDemo = new ADecBufferDemo();
57 CommonTool *commonTool = new CommonTool();
58 for (int i = 0; i < FUZZ_TEST_NUM; i++) {
59 codec = aDecBufferDemo->CreateByMime(commonTool->GetRandString().c_str());
60 result0 = aDecBufferDemo->Destroy(codec);
61 cout << "cur run times is " << i << ", result is " << result0 << endl;
62 }
63 delete commonTool;
64 delete aDecBufferDemo;
65 }
66
67 /**
68 * @tc.number : FUZZ_TEST_002
69 * @tc.name : CreateByName - mime fuzz test
70 * @tc.desc : param fuzz test
71 */
72 HWTEST_F(FuzzTest, FUZZ_TEST_002, TestSize.Level2)
73 {
74 OH_AVCodec *codec = nullptr;
75 OH_AVErrCode result0;
76 ADecBufferDemo *aDecBufferDemo = new ADecBufferDemo();
77 CommonTool *commonTool = new CommonTool();
78 for (int i = 0; i < FUZZ_TEST_NUM; i++) {
79 codec = aDecBufferDemo->CreateByName(commonTool->GetRandString().c_str());
80 result0 = aDecBufferDemo->Destroy(codec);
81 cout << "cur run times is " << i << ", result is " << result0 << endl;
82 }
83 delete commonTool;
84 delete aDecBufferDemo;
85 }
86
87 /**
88 * @tc.number : FUZZ_TEST_003
89 * @tc.name : Configure - channel fuzz test
90 * @tc.desc : param fuzz test
91 */
92 HWTEST_F(FuzzTest, FUZZ_TEST_003, TestSize.Level2)
93 {
94 OH_AVCodec *codec = nullptr;
95 OH_AVFormat *format = OH_AVFormat_Create();
96 int32_t channel = 1;
97 int32_t sampleRate = 8000;
98 OH_AVErrCode result0;
99 ADecBufferDemo *aDecBufferDemo = new ADecBufferDemo();
100 CommonTool *commonTool = new CommonTool();
101 codec = aDecBufferDemo->CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_G711MU);
102 ASSERT_NE(codec, nullptr);
103 result0 = aDecBufferDemo->SetCallback(codec);
104 ASSERT_EQ(result0, AV_ERR_OK);
105 for (int i = 0; i < FUZZ_TEST_NUM; i++) {
106 channel = commonTool->GetRandInt();
107 result0 = aDecBufferDemo->Configure(codec, format, channel, sampleRate);
108 }
109 result0 = aDecBufferDemo->Destroy(codec);
110 ASSERT_EQ(result0, AV_ERR_OK);
111 delete commonTool;
112 delete aDecBufferDemo;
113 }
114
115 /**
116 * @tc.number : FUZZ_TEST_004
117 * @tc.name : Configure - sampleRate fuzz test
118 * @tc.desc : param fuzz test
119 */
120 HWTEST_F(FuzzTest, FUZZ_TEST_004, TestSize.Level2)
121 {
122 OH_AVCodec *codec = nullptr;
123 OH_AVFormat *format = OH_AVFormat_Create();
124 int32_t channel = 1;
125 int32_t sampleRate = 8000;
126 OH_AVErrCode result0;
127 ADecBufferDemo *aDecBufferDemo = new ADecBufferDemo();
128 CommonTool *commonTool = new CommonTool();
129 codec = aDecBufferDemo->CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_G711MU);
130 ASSERT_NE(codec, nullptr);
131 result0 = aDecBufferDemo->SetCallback(codec);
132 ASSERT_EQ(result0, AV_ERR_OK);
133 for (int i = 0; i < FUZZ_TEST_NUM; i++) {
134 sampleRate = commonTool->GetRandInt();
135 result0 = aDecBufferDemo->Configure(codec, format, channel, sampleRate);
136 }
137 result0 = aDecBufferDemo->Destroy(codec);
138 ASSERT_EQ(result0, AV_ERR_OK);
139 delete commonTool;
140 delete aDecBufferDemo;
141 }
142
143 /**
144 * @tc.number : FUZZ_TEST_005
145 * @tc.name : PushInputData - index fuzz test
146 * @tc.desc : param fuzz test
147 */
148 HWTEST_F(FuzzTest, FUZZ_TEST_005, TestSize.Level2)
149 {
150 OH_AVCodec *codec = nullptr;
151 OH_AVFormat *format = OH_AVFormat_Create();
152 int32_t channel = 1;
153 int32_t sampleRate = 8000;
154 uint32_t index;
155 OH_AVErrCode result0;
156 ADecBufferDemo *aDecBufferDemo = new ADecBufferDemo();
157 CommonTool *commonTool = new CommonTool();
158 codec = aDecBufferDemo->CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_G711MU);
159 ASSERT_NE(codec, nullptr);
160 result0 = aDecBufferDemo->SetCallback(codec);
161 ASSERT_EQ(result0, AV_ERR_OK);
162 result0 = aDecBufferDemo->Configure(codec, format, channel, sampleRate);
163 ASSERT_EQ(result0, AV_ERR_OK);
164 result0 = aDecBufferDemo->Start(codec);
165 ASSERT_EQ(result0, AV_ERR_OK);
166 index = aDecBufferDemo->GetInputIndex();
167 for (int i = 0; i < FUZZ_TEST_NUM; i++) {
168 index = commonTool->GetRandInt();
169 result0 = aDecBufferDemo->PushInputData(codec, index);
170 }
171 result0 = aDecBufferDemo->Destroy(codec);
172 ASSERT_EQ(result0, AV_ERR_OK);
173 delete commonTool;
174 delete aDecBufferDemo;
175 }
176
177 /**
178 * @tc.number : FUZZ_TEST_006
179 * @tc.name : FreeOutputData - index fuzz test
180 * @tc.desc : param fuzz test
181 */
182 HWTEST_F(FuzzTest, FUZZ_TEST_006, TestSize.Level2)
183 {
184 OH_AVCodec *codec = nullptr;
185 OH_AVFormat *format = OH_AVFormat_Create();
186 int32_t channel = 1;
187 int32_t sampleRate = 8000;
188 uint32_t index;
189 OH_AVErrCode result0;
190 ADecBufferDemo *aDecBufferDemo = new ADecBufferDemo();
191 CommonTool *commonTool = new CommonTool();
192 codec = aDecBufferDemo->CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_G711MU);
193 ASSERT_NE(codec, nullptr);
194 result0 = aDecBufferDemo->SetCallback(codec);
195 ASSERT_EQ(result0, AV_ERR_OK);
196 result0 = aDecBufferDemo->Configure(codec, format, channel, sampleRate);
197 ASSERT_EQ(result0, AV_ERR_OK);
198 result0 = aDecBufferDemo->Start(codec);
199 ASSERT_EQ(result0, AV_ERR_OK);
200 index = aDecBufferDemo->GetInputIndex();
201 result0 = aDecBufferDemo->PushInputDataEOS(codec, index);
202 ASSERT_EQ(result0, AV_ERR_OK);
203 index = aDecBufferDemo->GetOutputIndex();
204 for (int i = 0; i < FUZZ_TEST_NUM; i++) {
205 index = commonTool->GetRandInt();
206 result0 = aDecBufferDemo->FreeOutputData(codec, index);
207 }
208 result0 = aDecBufferDemo->Destroy(codec);
209 ASSERT_EQ(result0, AV_ERR_OK);
210 delete commonTool;
211 delete aDecBufferDemo;
212 }
213
214 /**
215 * @tc.number : FUZZ_TEST_007
216 * @tc.name : PushInputDataEOS - index fuzz test
217 * @tc.desc : param fuzz test
218 */
219 HWTEST_F(FuzzTest, FUZZ_TEST_007, TestSize.Level2)
220 {
221 OH_AVCodec *codec = nullptr;
222 OH_AVFormat *format = OH_AVFormat_Create();
223 int32_t channel = 1;
224 int32_t sampleRate = 8000;
225 uint32_t index;
226 OH_AVErrCode result0;
227 ADecBufferDemo *aDecBufferDemo = new ADecBufferDemo();
228 CommonTool *commonTool = new CommonTool();
229 codec = aDecBufferDemo->CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_G711MU);
230 ASSERT_NE(codec, nullptr);
231 result0 = aDecBufferDemo->SetCallback(codec);
232 ASSERT_EQ(result0, AV_ERR_OK);
233 result0 = aDecBufferDemo->Configure(codec, format, channel, sampleRate);
234 ASSERT_EQ(result0, AV_ERR_OK);
235 result0 = aDecBufferDemo->Start(codec);
236 ASSERT_EQ(result0, AV_ERR_OK);
237 index = aDecBufferDemo->GetInputIndex();
238 for (int i = 0; i < FUZZ_TEST_NUM; i++) {
239 index = commonTool->GetRandInt();
240 result0 = aDecBufferDemo->PushInputDataEOS(codec, index);
241 }
242 result0 = aDecBufferDemo->Destroy(codec);
243 ASSERT_EQ(result0, AV_ERR_OK);
244 delete commonTool;
245 delete aDecBufferDemo;
246 }
247