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 "bgtasksubscriberstubonremote_fuzzer.h"
17 #include "ibackground_task_subscriber.h"
18 #include "securec.h"
19 
20 #include "background_task_subscriber_stub.h"
21 #include "background_task_subscriber.h"
22 #include "ibackground_task_subscriber_ipc_interface_code.h"
23 
24 namespace OHOS {
25 namespace BackgroundTaskMgr {
26     constexpr int32_t U32_AT_SIZE = 4;
27 
28     const std::u16string BACKGROUND_TASK_MGR_SUBSCRIBER_TOKEN = u"ohos.resourceschedule.IBackgroundTaskSubscriber";
29 class TestBackgroundTaskSubscriber : public BackgroundTaskSubscriber {
30 public:
TestBackgroundTaskSubscriber()31     TestBackgroundTaskSubscriber() : BackgroundTaskSubscriber() {}
32 };
33 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)34     bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
35     {
36         MessageParcel datas;
37         datas.WriteInterfaceToken(BACKGROUND_TASK_MGR_SUBSCRIBER_TOKEN);
38         datas.WriteBuffer(data, size);
39         datas.RewindRead(0);
40         MessageParcel reply;
41         MessageOption option;
42         auto subscriber = TestBackgroundTaskSubscriber();
43         auto subscriberImpl = std::make_shared<BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl>(subscriber);
44 
45         uint32_t code1 = static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_CONNECTED);
46         subscriberImpl->OnRemoteRequest(code1, datas, reply, option);
47 
48         uint32_t code2 = static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_DISCONNECTED);
49         subscriberImpl->OnRemoteRequest(code2, datas, reply, option);
50 
51         uint32_t code3 = static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_TRANSIENT_TASK_START);
52         subscriberImpl->OnRemoteRequest(code3, datas, reply, option);
53 
54         uint32_t code4 = static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_TRANSIENT_TASK_END);
55         subscriberImpl->OnRemoteRequest(code4, datas, reply, option);
56 
57         uint32_t code15 = static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_TRANSIENT_TASK_ERR);
58         subscriberImpl->OnRemoteRequest(code15, datas, reply, option);
59 
60         uint32_t code5 = static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_APP_TRANSIENT_TASK_START);
61         subscriberImpl->OnRemoteRequest(code5, datas, reply, option);
62 
63         uint32_t code6 = static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_APP_TRANSIENT_TASK_END);
64         subscriberImpl->OnRemoteRequest(code6, datas, reply, option);
65 
66         uint32_t code7 = static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_CONTINUOUS_TASK_START);
67         subscriberImpl->OnRemoteRequest(code7, datas, reply, option);
68 
69         uint32_t code8 = static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_CONTINUOUS_TASK_UPDATE);
70         subscriberImpl->OnRemoteRequest(code8, datas, reply, option);
71 
72         uint32_t code9 = static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_CONTINUOUS_TASK_STOP);
73         subscriberImpl->OnRemoteRequest(code9, datas, reply, option);
74 
75         uint32_t code10 = static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_APP_CONTINUOUS_TASK_STOP);
76         subscriberImpl->OnRemoteRequest(code10, datas, reply, option);
77 
78         uint32_t code11 =
79             static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_APP_EFFICIENCY_RESOURCES_APPLY);
80         subscriberImpl->OnRemoteRequest(code11, datas, reply, option);
81 
82         uint32_t code12 =
83             static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_APP_EFFICIENCY_RESOURCES_RESET);
84         subscriberImpl->OnRemoteRequest(code12, datas, reply, option);
85 
86         uint32_t code13 =
87             static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_PROC_EFFICIENCY_RESOURCES_APPLY);
88         subscriberImpl->OnRemoteRequest(code13, datas, reply, option);
89 
90         uint32_t code14 =
91             static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_PROC_EFFICIENCY_RESOURCES_RESET);
92         subscriberImpl->OnRemoteRequest(code14, datas, reply, option);
93         return true;
94     }
95 } // BackgroundTaskMgr
96 } // OHOS
97 
98 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)99 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
100 {
101     /* Run your code on data */
102     if (data == nullptr) {
103         return 0;
104     }
105 
106     if (size < OHOS::BackgroundTaskMgr::U32_AT_SIZE) {
107         return 0;
108     }
109 
110     char* ch = static_cast<char *>(malloc(size + 1));
111     if (ch == nullptr) {
112         return 0;
113     }
114 
115     (void)memset_s(ch, size + 1, 0x00, size + 1);
116     if (memcpy_s(ch, size + 1, data, size) != EOK) {
117         free(ch);
118         ch = nullptr;
119         return 0;
120     }
121 
122     OHOS::BackgroundTaskMgr::DoSomethingInterestingWithMyAPI(ch, size);
123     free(ch);
124     ch = nullptr;
125     return 0;
126 }
127