1 /*
2 * Copyright (c) 2025 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 "distributedschedservice_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21 #include <singleton.h>
22
23 #include "distributed_sched_interface.h"
24 #include "distributed_sched_service.h"
25 #include "distributed_sched_stub.h"
26 #include "distributedWant/distributed_want.h"
27 #include "mock_fuzz_util.h"
28 #include "mock_distributed_sched.h"
29 #include "parcel_helper.h"
30 #include "dms_continue_time_dumper.h"
31
32 using namespace OHOS::AAFwk;
33 using namespace OHOS::AppExecFwk;
34
35 namespace OHOS {
36 namespace DistributedSchedule {
37 constexpr int32_t ON_DEMAND_REASON_ID_COUNT = 7;
38
39 std::string GetDExtensionName(std::string bundleName, int32_t userId);
40 std::string GetDExtensionProcess(std::string bundleName, int32_t userId);
OnStopFuzzTest(const uint8_t * data,size_t size)41 void OnStopFuzzTest(const uint8_t* data, size_t size)
42 {
43 if ((data == nullptr) || (size < sizeof(int32_t) + sizeof(int32_t))) {
44 return;
45 }
46 FuzzedDataProvider fdp(data, size);
47 std::string reasonName = fdp.ConsumeRandomLengthString();
48 std::string reasonValue = fdp.ConsumeRandomLengthString();
49 int32_t enumIdx = fdp.ConsumeIntegral<int32_t>() % ON_DEMAND_REASON_ID_COUNT;
50 OnDemandReasonId reasonId = static_cast<OnDemandReasonId>(enumIdx);
51 int32_t extraDataId = fdp.ConsumeIntegral<int32_t>();
52 SystemAbilityOnDemandReason reason(reasonId, reasonName, reasonValue, extraDataId);
53 DistributedSchedService::GetInstance().OnStop(reason);
54 }
55
OnActiveFuzzTest(const uint8_t * data,size_t size)56 void OnActiveFuzzTest(const uint8_t* data, size_t size)
57 {
58 if ((data == nullptr) || (size < sizeof(int32_t) + sizeof(int32_t))) {
59 return;
60 }
61 FuzzedDataProvider fdp(data, size);
62 std::string reasonName = fdp.ConsumeRandomLengthString();
63 std::string reasonValue = fdp.ConsumeRandomLengthString();
64 int32_t enumIdx = fdp.ConsumeIntegral<int32_t>() % ON_DEMAND_REASON_ID_COUNT;
65 OnDemandReasonId reasonId = static_cast<OnDemandReasonId>(enumIdx);
66 int32_t extraDataId = fdp.ConsumeIntegral<int32_t>();
67 SystemAbilityOnDemandReason reason(reasonId, reasonName, reasonValue, extraDataId);
68 DistributedSchedService::GetInstance().OnActive(reason);
69 DistributedSchedService::GetInstance().OnIdle(reason);
70 }
71
HandleBootStartFuzzTest(const uint8_t * data,size_t size)72 void HandleBootStartFuzzTest(const uint8_t* data, size_t size)
73 {
74 if ((data == nullptr) || (size < sizeof(int32_t) + sizeof(int32_t))) {
75 return;
76 }
77 FuzzedDataProvider fdp(data, size);
78 std::string reasonName = fdp.ConsumeRandomLengthString();
79 std::string reasonValue = fdp.ConsumeRandomLengthString();
80
81 int32_t enumIdx = fdp.ConsumeIntegral<int32_t>() % ON_DEMAND_REASON_ID_COUNT;
82 OnDemandReasonId reasonId = static_cast<OnDemandReasonId>(enumIdx);
83 int32_t extraDataId = fdp.ConsumeIntegral<int32_t>();
84 SystemAbilityOnDemandReason reason(reasonId, reasonName, reasonValue, extraDataId);
85 DistributedSchedService::GetInstance().HandleBootStart(reason);
86 }
87
DeviceOnlineNotifyFuzzTest(const uint8_t * data,size_t size)88 void DeviceOnlineNotifyFuzzTest(const uint8_t* data, size_t size)
89 {
90 if ((data == nullptr) || (size < sizeof(int32_t))) {
91 return;
92 }
93
94 FuzzedDataProvider fdp(data, size);
95 std::string networkId = fdp.ConsumeRandomLengthString();
96 DistributedSchedService::GetInstance().DeviceOnlineNotify(networkId);
97 }
98 }
99 }
100 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)101 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
102 {
103 OHOS::DistributedSchedule::OnStopFuzzTest(data, size);
104 OHOS::DistributedSchedule::OnActiveFuzzTest(data, size);
105 OHOS::DistributedSchedule::HandleBootStartFuzzTest(data, size);
106 OHOS::DistributedSchedule::DeviceOnlineNotifyFuzzTest(data, size);
107 return 0;
108 }
109