1 /*
2 * Copyright (c) 2022 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 "acodecfile_fuzzer.h"
17 #include <iostream>
18 #include "string_ex.h"
19 #include "media_errors.h"
20 #include "directory_ex.h"
21 #include "ui/rs_surface_node.h"
22 #include "window_option.h"
23 #include "aw_common.h"
24
25 using namespace std;
26 using namespace OHOS;
27 using namespace OHOS::Media;
28 using namespace OHOS::Media::ACodecTestParam;
29 using namespace OHOS::Media::PlayerTestParam;
30
ACodecFileFuzzer()31 ACodecFileFuzzer::ACodecFileFuzzer()
32 {
33 }
34
~ACodecFileFuzzer()35 ACodecFileFuzzer::~ACodecFileFuzzer()
36 {
37 }
38
FuzzAudioFile(uint8_t * data,size_t size)39 bool ACodecFileFuzzer::FuzzAudioFile(uint8_t *data, size_t size)
40 {
41 constexpr int32_t WAITTING_TIME = 2;
42 std::shared_ptr<ACodecSignal> acodecSignal = std::make_shared<ACodecSignal>();
43 adecCallback_ = std::make_shared<ADecCallbackTest>(acodecSignal);
44 CHECK_INSTANCE_AND_RETURN_RET(adecCallback_, false);
45 aencCallback_ = std::make_shared<AEncCallbackTest>(acodecSignal);
46 CHECK_INSTANCE_AND_RETURN_RET(aencCallback_, false);
47 audioCodec_ = std::make_shared<ACodecMock>(acodecSignal);
48 CHECK_INSTANCE_AND_RETURN_RET(audioCodec_, false);
49 CHECK_BOOL_AND_RETURN_RET(audioCodec_->CreateAudioEncMockByMime("audio/mp4a-latm"), false);
50 CHECK_STATE_AND_RETURN_RET(audioCodec_->SetCallbackEnc(aencCallback_), false);
51 CHECK_BOOL_AND_RETURN_RET(audioCodec_->CreateAudioDecMockByMime("audio/mp4a-latm"), false);
52 CHECK_STATE_AND_RETURN_RET(audioCodec_->SetCallbackDec(adecCallback_), false);
53
54 defaultFormat_ = AVCodecMockFactory::CreateFormat();
55 CHECK_INSTANCE_AND_RETURN_RET(defaultFormat_, false);
56 (void)defaultFormat_->PutIntValue("channel_count", 2); // 2 common channel count
57 (void)defaultFormat_->PutIntValue("sample_rate", 44100); // 44100 common sample rate
58 (void)defaultFormat_->PutIntValue("audio_sample_format", 1); // 1 AudioStandard::SAMPLE_S16LE
59
60 audioCodec_->SetOutPath("/data/test/media/aac_filefuzz_out.es");
61 const string path = "/data/test/media/AAC_48000_32_1.aac";
62 CHECK_STATE_AND_RETURN_RET(WriteDataToFile(path, data, size), false);
63
64 audioCodec_->ConfigureEnc(defaultFormat_);
65 audioCodec_->ConfigureDec(defaultFormat_);
66 audioCodec_->PrepareDec();
67 audioCodec_->PrepareEnc();
68 audioCodec_->StartDec();
69 audioCodec_->StartEnc();
70 sleep(WAITTING_TIME);
71 if (audioCodec_ != nullptr) {
72 CHECK_STATE_AND_RETURN_RET(audioCodec_->ReleaseDec(), false);
73 CHECK_STATE_AND_RETURN_RET(audioCodec_->ReleaseEnc(), false);
74 }
75 return true;
76 }
77
FuzzACodecFile(uint8_t * data,size_t size)78 bool OHOS::Media::FuzzACodecFile(uint8_t *data, size_t size)
79 {
80 auto codecfuzzer = std::make_unique<ACodecFileFuzzer>();
81 if (codecfuzzer == nullptr) {
82 cout << "codecfuzzer is null" << endl;
83 return 0;
84 }
85 if (size < sizeof(int32_t)) {
86 return 0;
87 }
88 return codecfuzzer->FuzzAudioFile(data, size);
89 }
90
91 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)92 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
93 {
94 /* Run your code on data */
95 OHOS::Media::FuzzACodecFile(data, size);
96 return 0;
97 }
98
99