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_encoder_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::AudioAacEncDemo;
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 AudioBufferAacEncDemo *audioBufferAacEncDemo = new AudioBufferAacEncDemo();
57 CommonTool *commonTool = new CommonTool();
58 for (int i = 0; i < FUZZ_TEST_NUM; i++) {
59 codec = audioBufferAacEncDemo->CreateByMime(commonTool->GetRandString().c_str());
60 result0 = audioBufferAacEncDemo->Destroy(codec);
61 cout << "cur run times is " << i << ", result is " << result0 << endl;
62 }
63 delete commonTool;
64 delete audioBufferAacEncDemo;
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 AudioBufferAacEncDemo *audioBufferAacEncDemo = new AudioBufferAacEncDemo();
77 CommonTool *commonTool = new CommonTool();
78 for (int i = 0; i < FUZZ_TEST_NUM; i++) {
79 codec = audioBufferAacEncDemo->CreateByName(commonTool->GetRandString().c_str());
80 result0 = audioBufferAacEncDemo->Destroy(codec);
81 cout << "cur run times is " << i << ", result is " << result0 << endl;
82 }
83 delete commonTool;
84 delete audioBufferAacEncDemo;
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; //channel1
97 int32_t sampleRate = 8000; // 8000hz
98 int64_t bitRate = 64000; //64K
99 int32_t sampleFormat = SAMPLE_S16LE;
100 int32_t sampleBit = 16;
101 int32_t complexity = 10;
102 OH_AVErrCode result0;
103 AudioBufferAacEncDemo *audioBufferAacEncDemo = new AudioBufferAacEncDemo();
104 CommonTool *commonTool = new CommonTool();
105 codec = audioBufferAacEncDemo->CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_G711MU);
106 ASSERT_NE(codec, nullptr);
107 result0 = audioBufferAacEncDemo->SetCallback(codec);
108 ASSERT_EQ(result0, AV_ERR_OK);
109 for (int i = 0; i < FUZZ_TEST_NUM; i++) {
110 channel = commonTool->GetRandInt();
111 result0 = audioBufferAacEncDemo->Configure(codec, format, channel, sampleRate, bitRate, sampleFormat, sampleBit,
112 complexity);
113 }
114 result0 = audioBufferAacEncDemo->Destroy(codec);
115 ASSERT_EQ(result0, AV_ERR_OK);
116 delete commonTool;
117 delete audioBufferAacEncDemo;
118 }
119
120 /**
121 * @tc.number : FUZZ_TEST_004
122 * @tc.name : Configure - sampleRate fuzz test
123 * @tc.desc : param fuzz test
124 */
125 HWTEST_F(FuzzTest, FUZZ_TEST_004, TestSize.Level2)
126 {
127 OH_AVCodec *codec = nullptr;
128 OH_AVFormat *format = OH_AVFormat_Create();
129 int32_t channel = 1; //channel1
130 int32_t sampleRate = 8000; // 8000hz
131 int64_t bitRate = 64000; //64K
132 int32_t sampleFormat = SAMPLE_S16LE;
133 int32_t sampleBit = 16;
134 int32_t complexity = 10;
135 OH_AVErrCode result0;
136 AudioBufferAacEncDemo *audioBufferAacEncDemo = new AudioBufferAacEncDemo();
137 CommonTool *commonTool = new CommonTool();
138 codec = audioBufferAacEncDemo->CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_G711MU);
139 ASSERT_NE(codec, nullptr);
140 result0 = audioBufferAacEncDemo->SetCallback(codec);
141 ASSERT_EQ(result0, AV_ERR_OK);
142 for (int i = 0; i < FUZZ_TEST_NUM; i++) {
143 sampleRate = commonTool->GetRandInt();
144 result0 = audioBufferAacEncDemo->Configure(codec, format, channel, sampleRate, bitRate, sampleFormat, sampleBit,
145 complexity);
146 }
147 result0 = audioBufferAacEncDemo->Destroy(codec);
148 ASSERT_EQ(result0, AV_ERR_OK);
149 delete commonTool;
150 delete audioBufferAacEncDemo;
151 }
152
153 /**
154 * @tc.number : FUZZ_TEST_005
155 * @tc.name : Configure - bitRate fuzz test
156 * @tc.desc : param fuzz test
157 */
158 HWTEST_F(FuzzTest, FUZZ_TEST_005, TestSize.Level2)
159 {
160 OH_AVCodec *codec = nullptr;
161 OH_AVFormat *format = OH_AVFormat_Create();
162 int32_t channel = 1; //channel1
163 int32_t sampleRate = 8000; // 8000hz
164 int64_t bitRate = 64000; //64K
165 int32_t sampleFormat = SAMPLE_S16LE;
166 int32_t sampleBit = 16;
167 int32_t complexity = 10;
168 OH_AVErrCode result0;
169 AudioBufferAacEncDemo *audioBufferAacEncDemo = new AudioBufferAacEncDemo();
170 CommonTool *commonTool = new CommonTool();
171 codec = audioBufferAacEncDemo->CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_G711MU);
172 ASSERT_NE(codec, nullptr);
173 result0 = audioBufferAacEncDemo->SetCallback(codec);
174 ASSERT_EQ(result0, AV_ERR_OK);
175 for (int i = 0; i < FUZZ_TEST_NUM; i++) {
176 bitRate = commonTool->GetRandInt();
177 result0 = audioBufferAacEncDemo->Configure(codec, format, channel, sampleRate, bitRate, sampleFormat, sampleBit,
178 complexity);
179 }
180 result0 = audioBufferAacEncDemo->Destroy(codec);
181 ASSERT_EQ(result0, AV_ERR_OK);
182 delete commonTool;
183 delete audioBufferAacEncDemo;
184 }
185
186 /**
187 * @tc.number : FUZZ_TEST_006
188 * @tc.name : Configure - sampleFormat fuzz test
189 * @tc.desc : param fuzz test
190 */
191 HWTEST_F(FuzzTest, FUZZ_TEST_006, TestSize.Level2)
192 {
193 OH_AVCodec *codec = nullptr;
194 OH_AVFormat *format = OH_AVFormat_Create();
195 int32_t channel = 1; //channel1
196 int32_t sampleRate = 8000; // 8000hz
197 int64_t bitRate = 64000; //64K
198 int32_t sampleFormat = SAMPLE_S16LE;
199 int32_t sampleBit = 16;
200 int32_t complexity = 10;
201 OH_AVErrCode result0;
202 AudioBufferAacEncDemo *audioBufferAacEncDemo = new AudioBufferAacEncDemo();
203 CommonTool *commonTool = new CommonTool();
204 codec = audioBufferAacEncDemo->CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_G711MU);
205 ASSERT_NE(codec, nullptr);
206 result0 = audioBufferAacEncDemo->SetCallback(codec);
207 ASSERT_EQ(result0, AV_ERR_OK);
208 for (int i = 0; i < FUZZ_TEST_NUM; i++) {
209 sampleFormat = commonTool->GetRandInt();
210 result0 = audioBufferAacEncDemo->Configure(codec, format, channel, sampleRate, bitRate, sampleFormat, sampleBit,
211 complexity);
212 }
213 result0 = audioBufferAacEncDemo->Destroy(codec);
214 ASSERT_EQ(result0, AV_ERR_OK);
215 delete commonTool;
216 delete audioBufferAacEncDemo;
217 }
218
219 /**
220 * @tc.number : FUZZ_TEST_007
221 * @tc.name : Configure - complexity fuzz test
222 * @tc.desc : param fuzz test
223 */
224 HWTEST_F(FuzzTest, FUZZ_TEST_007, TestSize.Level2)
225 {
226 OH_AVCodec *codec = nullptr;
227 OH_AVFormat *format = OH_AVFormat_Create();
228 int32_t channel = 1; //channel1
229 int32_t sampleRate = 8000; // 8000hz
230 int64_t bitRate = 64000; //64K
231 int32_t sampleFormat = SAMPLE_S16LE;
232 int32_t sampleBit = 16;
233 int32_t complexity = 10;
234 OH_AVErrCode result0;
235 AudioBufferAacEncDemo *audioBufferAacEncDemo = new AudioBufferAacEncDemo();
236 CommonTool *commonTool = new CommonTool();
237 codec = audioBufferAacEncDemo->CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_G711MU);
238 ASSERT_NE(codec, nullptr);
239 result0 = audioBufferAacEncDemo->SetCallback(codec);
240 ASSERT_EQ(result0, AV_ERR_OK);
241 for (int i = 0; i < FUZZ_TEST_NUM; i++) {
242 complexity = commonTool->GetRandInt();
243 result0 = audioBufferAacEncDemo->Configure(codec, format, channel, sampleRate, bitRate, sampleFormat, sampleBit,
244 complexity);
245 }
246 result0 = audioBufferAacEncDemo->Destroy(codec);
247 ASSERT_EQ(result0, AV_ERR_OK);
248 delete commonTool;
249 delete audioBufferAacEncDemo;
250 }
251
252 /**
253 * @tc.number : FUZZ_TEST_008
254 * @tc.name : PushInputData - index fuzz test
255 * @tc.desc : param fuzz test
256 */
257 HWTEST_F(FuzzTest, FUZZ_TEST_008, TestSize.Level2)
258 {
259 OH_AVCodec *codec = nullptr;
260 OH_AVFormat *format = OH_AVFormat_Create();
261 int32_t channel = 1; //channel1
262 int32_t sampleRate = 8000; // 8000hz
263 int64_t bitRate = 64000; //64K
264 int32_t sampleFormat = SAMPLE_S16LE;
265 int32_t sampleBit = 16;
266 int32_t complexity = 10;
267 uint32_t index;
268 OH_AVErrCode result0;
269 AudioBufferAacEncDemo *audioBufferAacEncDemo = new AudioBufferAacEncDemo();
270 CommonTool *commonTool = new CommonTool();
271 codec = audioBufferAacEncDemo->CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_G711MU);
272 ASSERT_NE(codec, nullptr);
273 result0 = audioBufferAacEncDemo->SetCallback(codec);
274 ASSERT_EQ(result0, AV_ERR_OK);
275 result0 = audioBufferAacEncDemo->Configure(codec, format, channel, sampleRate, bitRate, sampleFormat, sampleBit,
276 complexity);
277 ASSERT_EQ(result0, AV_ERR_OK);
278 result0 = audioBufferAacEncDemo->Start(codec);
279 ASSERT_EQ(result0, AV_ERR_OK);
280 index = audioBufferAacEncDemo->GetInputIndex();
281 for (int i = 0; i < FUZZ_TEST_NUM; i++) {
282 index = commonTool->GetRandInt();
283 result0 = audioBufferAacEncDemo->PushInputData(codec, index);
284 }
285 result0 = audioBufferAacEncDemo->Destroy(codec);
286 ASSERT_EQ(result0, AV_ERR_OK);
287 delete commonTool;
288 delete audioBufferAacEncDemo;
289 }
290
291 /**
292 * @tc.number : FUZZ_TEST_009
293 * @tc.name : PushInputDataEOS - index fuzz test
294 * @tc.desc : param fuzz test
295 */
296 HWTEST_F(FuzzTest, FUZZ_TEST_009, TestSize.Level2)
297 {
298 OH_AVCodec *codec = nullptr;
299 OH_AVFormat *format = OH_AVFormat_Create();
300 int32_t channel = 1; //channel1
301 int32_t sampleRate = 8000; // 8000hz
302 int64_t bitRate = 64000; //64K
303 int32_t sampleFormat = SAMPLE_S16LE;
304 int32_t sampleBit = 16;
305 int32_t complexity = 10;
306 uint32_t index;
307 OH_AVErrCode result0;
308 AudioBufferAacEncDemo *audioBufferAacEncDemo = new AudioBufferAacEncDemo();
309 CommonTool *commonTool = new CommonTool();
310 codec = audioBufferAacEncDemo->CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_G711MU);
311 ASSERT_NE(codec, nullptr);
312 result0 = audioBufferAacEncDemo->SetCallback(codec);
313 ASSERT_EQ(result0, AV_ERR_OK);
314 result0 = audioBufferAacEncDemo->Configure(codec, format, channel, sampleRate, bitRate, sampleFormat, sampleBit,
315 complexity);
316 ASSERT_EQ(result0, AV_ERR_OK);
317 result0 = audioBufferAacEncDemo->Start(codec);
318 ASSERT_EQ(result0, AV_ERR_OK);
319 index = audioBufferAacEncDemo->GetInputIndex();
320 for (int i = 0; i < FUZZ_TEST_NUM; i++) {
321 index = commonTool->GetRandInt();
322 result0 = audioBufferAacEncDemo->PushInputDataEOS(codec, index);
323 }
324 result0 = audioBufferAacEncDemo->Destroy(codec);
325 ASSERT_EQ(result0, AV_ERR_OK);
326 delete commonTool;
327 delete audioBufferAacEncDemo;
328 }
329
330 /**
331 * @tc.number : FUZZ_TEST_010
332 * @tc.name : FreeOutputData - index fuzz test
333 * @tc.desc : param fuzz test
334 */
335 HWTEST_F(FuzzTest, FUZZ_TEST_010, TestSize.Level2)
336 {
337 OH_AVCodec *codec = nullptr;
338 OH_AVFormat *format = OH_AVFormat_Create();
339 int32_t channel = 1; //channel1
340 int32_t sampleRate = 8000; // 8000hz
341 int64_t bitRate = 64000; //64K
342 int32_t sampleFormat = SAMPLE_S16LE;
343 int32_t sampleBit = 16;
344 int32_t complexity = 10;
345 uint32_t index;
346 OH_AVErrCode result0;
347 AudioBufferAacEncDemo *audioBufferAacEncDemo = new AudioBufferAacEncDemo();
348 CommonTool *commonTool = new CommonTool();
349 codec = audioBufferAacEncDemo->CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_G711MU);
350 ASSERT_NE(codec, nullptr);
351 result0 = audioBufferAacEncDemo->SetCallback(codec);
352 ASSERT_EQ(result0, AV_ERR_OK);
353 result0 = audioBufferAacEncDemo->Configure(codec, format, channel, sampleRate, bitRate, sampleFormat, sampleBit,
354 complexity);
355 ASSERT_EQ(result0, AV_ERR_OK);
356 result0 = audioBufferAacEncDemo->Start(codec);
357 ASSERT_EQ(result0, AV_ERR_OK);
358 index = audioBufferAacEncDemo->GetInputIndex();
359 result0 = audioBufferAacEncDemo->PushInputDataEOS(codec, index);
360 ASSERT_EQ(result0, AV_ERR_OK);
361 index = audioBufferAacEncDemo->GetOutputIndex();
362 for (int i = 0; i < FUZZ_TEST_NUM; i++) {
363 index = commonTool->GetRandInt();
364 result0 = audioBufferAacEncDemo->FreeOutputData(codec, index);
365 }
366 result0 = audioBufferAacEncDemo->Destroy(codec);
367 ASSERT_EQ(result0, AV_ERR_OK);
368 delete commonTool;
369 delete audioBufferAacEncDemo;
370 }
371