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 "playersetvolume_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 
24 using namespace std;
25 using namespace OHOS;
26 using namespace OHOS::Media;
27 
PlayerSetVolumeFuzzer()28 PlayerSetVolumeFuzzer::PlayerSetVolumeFuzzer()
29 {
30 }
31 
~PlayerSetVolumeFuzzer()32 PlayerSetVolumeFuzzer::~PlayerSetVolumeFuzzer()
33 {
34 }
35 
36 namespace OHOS {
37 namespace Media {
FuzzSetVolume(uint8_t * data,size_t size)38 bool PlayerSetVolumeFuzzer::FuzzSetVolume(uint8_t* data, size_t size)
39 {
40     player_ = OHOS::Media::PlayerFactory::CreatePlayer();
41     if (player_ == nullptr) {
42         cout << "player_ is null" << endl;
43         return false;
44     }
45     std::shared_ptr<TestPlayerCallback> cb = std::make_shared<TestPlayerCallback>();
46     player_->SetPlayerCallback(cb);
47     const string path = "/data/test/media/H264_AAC.mp4";
48     SetFdSource(path);
49     sptr<Surface> producerSurface = nullptr;
50     producerSurface = GetVideoSurface();
51     player_->SetVideoSurface(producerSurface);
52     player_->PrepareAsync();
53     sleep(1);
54     player_->Play();
55     if (size >= sizeof(float)) {
56         player_->SetVolume(*reinterpret_cast<float *>(data), *reinterpret_cast<float *>(data));
57         sleep(1);
58     }
59     player_->Release();
60     return true;
61 }
62 }
63 
FuzzPlayerSetVolume(uint8_t * data,size_t size)64 bool FuzzPlayerSetVolume(uint8_t* data, size_t size)
65 {
66     auto player = std::make_unique<PlayerSetVolumeFuzzer>();
67     if (player == nullptr) {
68         cout << "player is null" << endl;
69         return true;
70     }
71     return player->FuzzSetVolume(data, size);
72 }
73 }
74 
75 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)76 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
77 {
78     /* Run your code on data */
79     FuzzPlayerSetVolume(data, size);
80     return 0;
81 }
82 
83