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 "background_audio_controller.h"
17 #include "avsession_log.h"
18 #include "bkgraudiocontroller_fuzzer.h"
19 #include "securec.h"
20
21 using namespace std;
22 using namespace OHOS;
23 using namespace OHOS::AVSession;
24
25 static const int32_t MAX_CODE_LEN = 512;
26 static const int32_t MIN_SIZE_NUM = 4;
27
28
29 static const uint8_t *RAW_DATA = nullptr;
30 static size_t g_dataSize = 0;
31 static size_t g_pos;
32
33 template<class T>
GetData()34 T GetData()
35 {
36 T object {};
37 size_t objectSize = sizeof(object);
38 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
39 return object;
40 }
41 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
42 if (ret != EOK) {
43 return {};
44 }
45 g_pos += objectSize;
46 return object;
47 }
48
BackgroundAudioControllerTest01(uint8_t * data,size_t size)49 void OHOS::AVSession::BackgroundAudioControllerTest01(uint8_t *data, size_t size)
50 {
51 if ((data == nullptr) || (size > MAX_CODE_LEN) || (size < MIN_SIZE_NUM)) {
52 return;
53 }
54 vector<int32_t> uids = { 1001, 1002, 1003};
55 vector<int32_t> pids = { 2023, 2024, 2025};
56 vector<bool> flags = { true, false };
57 int32_t uid = uids[GetData<uint8_t>() % uids.size()];
58 int32_t pid = pids[GetData<uint8_t>() % pids.size()];
59 bool flag = flags[GetData<uint8_t>() % flags.size()];
60
61 BackgroundAudioController backGroundAudioController;
62 backGroundAudioController.HandleAppMuteState(uid, pid, flag);
63 }
64
65 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)66 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
67 {
68 /* Run your code on data */
69 RAW_DATA = data;
70 g_dataSize = size;
71 g_pos = 0;
72 OHOS::AVSession::BackgroundAudioControllerTest01(data, size);
73 return 0;
74 }