• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 namespace OHOS {
24 namespace BackgroundTaskMgr {
25     constexpr int32_t U32_AT_SIZE = 4;
26 
27     const std::u16string BACKGROUND_TASK_MGR_SUBSCRIBER_TOKEN = u"ohos.resourceschedule.IBackgroundTaskSubscriber";
28 class TestBackgroundTaskSubscriber : public BackgroundTaskSubscriber {
29 public:
TestBackgroundTaskSubscriber()30     TestBackgroundTaskSubscriber() : BackgroundTaskSubscriber() {}
31 };
32 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)33     bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
34     {
35         MessageParcel datas;
36         datas.WriteInterfaceToken(BACKGROUND_TASK_MGR_SUBSCRIBER_TOKEN);
37         datas.WriteBuffer(data, size);
38         datas.RewindRead(0);
39         MessageParcel reply;
40         MessageOption option;
41         auto subscriber = TestBackgroundTaskSubscriber();
42         auto subscriberImpl = std::make_shared<BackgroundTaskSubscriber::BackgroundTaskSubscriberImpl>(subscriber);
43 
44         uint32_t code1 = static_cast<uint32_t>(IBackgroundTaskSubscriberIpcCode::COMMAND_ON_CONNECTED);
45         subscriberImpl->OnRemoteRequest(code1, datas, reply, option);
46 
47         uint32_t code2 = static_cast<uint32_t>(IBackgroundTaskSubscriberIpcCode::COMMAND_ON_DISCONNECTED);
48         subscriberImpl->OnRemoteRequest(code2, datas, reply, option);
49 
50         uint32_t code3 = static_cast<uint32_t>(IBackgroundTaskSubscriberIpcCode::COMMAND_ON_TRANSIENT_TASK_START);
51         subscriberImpl->OnRemoteRequest(code3, datas, reply, option);
52 
53         uint32_t code4 = static_cast<uint32_t>(IBackgroundTaskSubscriberIpcCode::COMMAND_ON_TRANSIENT_TASK_END);
54         subscriberImpl->OnRemoteRequest(code4, datas, reply, option);
55 
56         uint32_t code15 = static_cast<uint32_t>(IBackgroundTaskSubscriberIpcCode::COMMAND_ON_TRANSIENT_TASK_ERR);
57         subscriberImpl->OnRemoteRequest(code15, datas, reply, option);
58 
59         uint32_t code5 = static_cast<uint32_t>(IBackgroundTaskSubscriberIpcCode::COMMAND_ON_APP_TRANSIENT_TASK_START);
60         subscriberImpl->OnRemoteRequest(code5, datas, reply, option);
61 
62         uint32_t code6 = static_cast<uint32_t>(IBackgroundTaskSubscriberIpcCode::COMMAND_ON_APP_TRANSIENT_TASK_END);
63         subscriberImpl->OnRemoteRequest(code6, datas, reply, option);
64 
65         uint32_t code7 = static_cast<uint32_t>(IBackgroundTaskSubscriberIpcCode::COMMAND_ON_CONTINUOUS_TASK_START);
66         subscriberImpl->OnRemoteRequest(code7, datas, reply, option);
67 
68         uint32_t code8 = static_cast<uint32_t>(IBackgroundTaskSubscriberIpcCode::COMMAND_ON_CONTINUOUS_TASK_UPDATE);
69         subscriberImpl->OnRemoteRequest(code8, datas, reply, option);
70 
71         uint32_t code9 = static_cast<uint32_t>(IBackgroundTaskSubscriberIpcCode::COMMAND_ON_CONTINUOUS_TASK_STOP);
72         subscriberImpl->OnRemoteRequest(code9, datas, reply, option);
73 
74         uint32_t code10 = static_cast<uint32_t>(IBackgroundTaskSubscriberIpcCode::COMMAND_ON_APP_CONTINUOUS_TASK_STOP);
75         subscriberImpl->OnRemoteRequest(code10, datas, reply, option);
76 
77         uint32_t code11 =
78             static_cast<uint32_t>(IBackgroundTaskSubscriberIpcCode::COMMAND_ON_APP_EFFICIENCY_RESOURCES_APPLY);
79         subscriberImpl->OnRemoteRequest(code11, datas, reply, option);
80 
81         uint32_t code12 =
82             static_cast<uint32_t>(IBackgroundTaskSubscriberIpcCode::COMMAND_ON_APP_EFFICIENCY_RESOURCES_RESET);
83         subscriberImpl->OnRemoteRequest(code12, datas, reply, option);
84 
85         uint32_t code13 =
86             static_cast<uint32_t>(IBackgroundTaskSubscriberIpcCode::COMMAND_ON_PROC_EFFICIENCY_RESOURCES_APPLY);
87         subscriberImpl->OnRemoteRequest(code13, datas, reply, option);
88 
89         uint32_t code14 =
90             static_cast<uint32_t>(IBackgroundTaskSubscriberIpcCode::COMMAND_ON_PROC_EFFICIENCY_RESOURCES_RESET);
91         subscriberImpl->OnRemoteRequest(code14, datas, reply, option);
92         return true;
93     }
94 } // BackgroundTaskMgr
95 } // OHOS
96 
97 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)98 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
99 {
100     /* Run your code on data */
101     if (data == nullptr) {
102         return 0;
103     }
104 
105     if (size < OHOS::BackgroundTaskMgr::U32_AT_SIZE) {
106         return 0;
107     }
108 
109     char* ch = static_cast<char *>(malloc(size + 1));
110     if (ch == nullptr) {
111         return 0;
112     }
113 
114     (void)memset_s(ch, size + 1, 0x00, size + 1);
115     if (memcpy_s(ch, size + 1, data, size) != EOK) {
116         free(ch);
117         ch = nullptr;
118         return 0;
119     }
120 
121     OHOS::BackgroundTaskMgr::DoSomethingInterestingWithMyAPI(ch, size);
122     free(ch);
123     ch = nullptr;
124     return 0;
125 }
126