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 "distributedschedservicetwo_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 GET_DEXTENSION_PROCESS_PARAM_COUNT = 6;
38 constexpr int32_t ON_DEMAND_REASON_ID_COUNT_FOUR = 4;
39
40 std::string GetDExtensionName(std::string bundleName, int32_t userId);
41 std::string GetDExtensionProcess(std::string bundleName, int32_t userId);
42
GetDExtensionNameFuzzTest(const uint8_t * data,size_t size)43 void GetDExtensionNameFuzzTest(const uint8_t* data, size_t size)
44 {
45 if ((data == nullptr) || (size < sizeof(int32_t))) {
46 return;
47 }
48
49 FuzzedDataProvider fdp(data, size);
50 std::string bundleName = fdp.ConsumeRandomLengthString();
51 int32_t userId = fdp.ConsumeIntegral<int32_t>();
52 OHOS::DistributedSchedule::GetDExtensionName(bundleName, userId);
53 }
54
GetDExtensionProcessFuzzTest(const uint8_t * data,size_t size)55 void GetDExtensionProcessFuzzTest(const uint8_t* data, size_t size)
56 {
57 if ((data == nullptr) || (size < GET_DEXTENSION_PROCESS_PARAM_COUNT * sizeof(int32_t))) {
58 return;
59 }
60
61 FuzzedDataProvider fdp(data, size);
62 std::string bundleName = fdp.ConsumeRandomLengthString();
63 int32_t userId = fdp.ConsumeIntegral<int32_t>();
64 OHOS::DistributedSchedule::GetDExtensionProcess(bundleName, userId);
65 }
66
ConnectDExtensionFromRemoteFuzzTest(const uint8_t * data,size_t size)67 void ConnectDExtensionFromRemoteFuzzTest(const uint8_t* data, size_t size)
68 {
69 if ((data == nullptr) || (size < ON_DEMAND_REASON_ID_COUNT_FOUR * sizeof(int32_t))) {
70 return;
71 }
72
73 FuzzedDataProvider fdp(data, size);
74 DExtSourceInfo sourceInfo;
75 sourceInfo.deviceId = fdp.ConsumeRandomLengthString();
76 sourceInfo.networkId = fdp.ConsumeRandomLengthString();
77 sourceInfo.bundleName = fdp.ConsumeRandomLengthString();
78 sourceInfo.moduleName = fdp.ConsumeRandomLengthString();
79 sourceInfo.abilityName = fdp.ConsumeRandomLengthString();
80
81 DExtSinkInfo sinkInfo;
82 sinkInfo.userId = fdp.ConsumeIntegral<int32_t>();
83 sinkInfo.pid = fdp.ConsumeIntegral<int32_t>();
84 sinkInfo.bundleName = fdp.ConsumeRandomLengthString();
85 sinkInfo.moduleName = fdp.ConsumeRandomLengthString();
86 sinkInfo.abilityName = fdp.ConsumeRandomLengthString();
87 sinkInfo.serviceName = fdp.ConsumeRandomLengthString();
88 std::string tokenId = fdp.ConsumeRandomLengthString();
89 std::string delegatee = fdp.ConsumeRandomLengthString();
90 DExtConnectInfo connectInfo(sourceInfo, sinkInfo, tokenId, delegatee);
91
92 int32_t resultEnum = fdp.ConsumeIntegralInRange<int32_t>(0, static_cast<int32_t>(DExtConnectResult::FAILED));
93 DExtConnectResult result = static_cast<DExtConnectResult>(resultEnum);
94 int32_t errCode = fdp.ConsumeIntegral<int32_t>();
95 DExtConnectResultInfo resultInfo(connectInfo, result, errCode);
96 DistributedSchedService::GetInstance().ConnectDExtensionFromRemote(connectInfo, resultInfo);
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::GetDExtensionNameFuzzTest(data, size);
104 OHOS::DistributedSchedule::GetDExtensionProcessFuzzTest(data, size);
105 OHOS::DistributedSchedule::ConnectDExtensionFromRemoteFuzzTest(data, size);
106 return 0;
107 }
108