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 "resschedadapter_fuzz.h"
17 
18 #include <cstdint>
19 #include <cstring>
20 #include <mutex>
21 #include <securec.h>
22 #include <unordered_map>
23 #include <vector>
24 
25 #include "res_sched_client_adapter.h"
26 
27 using namespace OHOS::NWeb;
28 
29 namespace OHOS {
30 namespace NWeb {
31 
ResSchedAdapterFuzzTest(const uint8_t * data,size_t size)32 bool ResSchedAdapterFuzzTest(const uint8_t* data, size_t size)
33 {
34     if ((data == nullptr) || (size == 0)) {
35         return false;
36     }
37 
38     pid_t pid = 1;
39     pid_t tid = 1;
40     int32_t nwebId = static_cast<int32_t>(size);
41     uint32_t windowId = static_cast<uint32_t>(size);
42 
43     std::vector<ResSchedStatusAdapter> statuses = { ResSchedStatusAdapter::WEB_ACTIVE,
44         ResSchedStatusAdapter::WEB_INACTIVE, ResSchedStatusAdapter::THREAD_CREATED,
45         ResSchedStatusAdapter::THREAD_DESTROYED, ResSchedStatusAdapter::VIDEO_PLAYING_START,
46         ResSchedStatusAdapter::VIDEO_PLAYING_STOP, ResSchedStatusAdapter::SCREEN_CAPTURE_START,
47         ResSchedStatusAdapter::SCREEN_CAPTURE_STOP, ResSchedStatusAdapter::AUDIO_STATUS_START,
48         ResSchedStatusAdapter::AUDIO_STATUS_STOP, ResSchedStatusAdapter::WEB_SCENE_ENTER,
49         ResSchedStatusAdapter::WEB_SCENE_EXIT };
50 
51     std::vector<ResSchedRoleAdapter> roles = { ResSchedRoleAdapter::USER_INTERACT, ResSchedRoleAdapter::NORMAL_DISPLAY,
52         ResSchedRoleAdapter::IMPORTANT_DISPLAY, ResSchedRoleAdapter::NORMAL_AUDIO, ResSchedRoleAdapter::IMPORTANT_AUDIO,
53         ResSchedRoleAdapter::IMAGE_DECODE };
54 
55     ResSchedClientAdapter adapter;
56 
57     for (auto status : statuses) {
58         adapter.ReportVideoPlaying(status, pid);
59         adapter.ReportScreenCapture(status, pid);
60         adapter.ReportRenderProcessStatus(status, pid);
61         adapter.ReportNWebInit(status, nwebId);
62         adapter.ReportWindowId(windowId, nwebId);
63 
64         for (auto role : roles) {
65             adapter.ReportKeyThread(status, pid, tid, role);
66         }
67 
68         adapter.ReportAudioData(status, pid, tid);
69         adapter.ReportProcessInUse(pid);
70         adapter.ReportSiteIsolationMode(true);
71         adapter.ReportWindowStatus(status, pid, windowId, nwebId);
72         adapter.ReportScene(status, ResSchedSceneAdapter::CLICK, nwebId);
73         adapter.ReportScene(status, ResSchedSceneAdapter::LOAD_URL, nwebId);
74     }
75 
76     adapter.ReportProcessInUse(0);
77     adapter.ReportWindowId(0, nwebId);
78     adapter.ReportKeyThread(statuses[0], pid, tid, static_cast<ResSchedRoleAdapter>(-1));
79     adapter.ReportAudioData(statuses[0], pid, 0);
80     adapter.ReportSiteIsolationMode(true);
81 
82     adapter.ReportWindowId(0, nwebId);
83     adapter.ReportKeyThread(statuses[0], -1, tid, ResSchedRoleAdapter::USER_INTERACT);
84     adapter.ReportAudioData(statuses[0], pid, -1);
85     adapter.ReportWindowStatus(statuses[0], pid, windowId, -1);
86     adapter.ReportNWebInit(ResSchedStatusAdapter::WEB_SCENE_EXIT, -1);
87 
88     return true;
89 }
90 } // namespace NWeb
91 } // namespace OHOS
92 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)93 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
94 {
95     OHOS::NWeb::ResSchedAdapterFuzzTest(data, size);
96     return 0;
97 }
98