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