1 /*
2 * Copyright (c) 2024 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 "mediaavsessionadapterimpl_fuzzer.h"
17
18 #define private public
19 #include <cstdlib>
20 #include <ctime>
21 #include <securec.h>
22 #include <fuzzer/FuzzedDataProvider.h>
23
24 #include "media_avsession_adapter.h"
25 #include "media_avsession_adapter_impl.h"
26
27 using namespace OHOS::NWeb;
28
29 namespace OHOS {
30 constexpr int MAX_SET_NUMBER = 1000;
31
32 class MediaAVSessionCallbackAdapterMock : public MediaAVSessionCallbackAdapter {
33 public:
34 MediaAVSessionCallbackAdapterMock() = default;
Play()35 void Play() {};
Pause()36 void Pause() {};
Stop()37 void Stop() {};
SeekTo(int64_t millisTime)38 void SeekTo(int64_t millisTime) {};
39 };
40
MediaAVSessionAdapterImplFuzzTest(const uint8_t * data,size_t size)41 bool MediaAVSessionAdapterImplFuzzTest(const uint8_t* data, size_t size)
42 {
43 std::shared_ptr<MediaAVSessionCallbackAdapterMock> mediaAvSessionCB;
44 std::shared_ptr<MediaAVSessionCallbackImpl> callbackImpl =
45 std::make_shared<MediaAVSessionCallbackImpl>(mediaAvSessionCB);
46
47 FuzzedDataProvider dataProvider(data, size);
48 int64_t time = dataProvider.ConsumeIntegralInRange<int64_t>(0, MAX_SET_NUMBER);
49
50 callbackImpl->OnPlay();
51 callbackImpl->OnPause();
52 callbackImpl->OnStop();
53 callbackImpl->OnSeek(time);
54
55 std::shared_ptr<MediaAVSessionKey> key = std::make_shared<MediaAVSessionKey>();
56 key->Init();
57 key->GetPID();
58 key->GetElement();
59 key->GetType();
60 key->ToString();
61
62 std::shared_ptr<MediaAVSessionAdapterImpl> avSessionAdapter = std::make_shared<MediaAVSessionAdapterImpl>();
63 auto mediaAVSessionCallbackAdapterMock = std::make_shared<MediaAVSessionCallbackAdapterMock>();
64 auto avSessionKey = std::make_shared<MediaAVSessionKey>();
65 avSessionAdapter->avSessionKey_ = avSessionKey;
66 avSessionAdapter->avSessionKey_->Init();
67 avSessionAdapter->avSession_ = nullptr;
68 auto type = MediaAVSessionType::MEDIA_TYPE_INVALID;
69 avSessionAdapter->CreateAVSession(type);
70 type = MediaAVSessionType::MEDIA_TYPE_AUDIO;
71 avSessionAdapter->CreateAVSession(type);
72
73 avSessionAdapter->DestroyAVSession();
74 avSessionAdapter->RegistCallback(mediaAVSessionCallbackAdapterMock);
75 avSessionAdapter->IsActivated();
76 avSessionAdapter->Activate();
77 avSessionAdapter->DeActivate();
78
79 return true;
80 }
81 } // namespace OHOS
82
83 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)84 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
85 {
86 /* Run your code on data */
87 OHOS::MediaAVSessionAdapterImplFuzzTest(data, size);
88 return 0;
89 }