• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "distributedschedserviceeight_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 
38 std::string GetDExtensionName(std::string bundleName, int32_t userId);
39 std::string GetDExtensionProcess(std::string bundleName, int32_t userId);
40 
GetIsFreeInstallFuzzTest(const uint8_t * data,size_t size)41 void GetIsFreeInstallFuzzTest(const uint8_t* data, size_t size)
42 {
43     if (data == nullptr || size < sizeof(int32_t)) {
44         return;
45     }
46 
47     FuzzedDataProvider fdp(data, size);
48     int32_t missionId = fdp.ConsumeIntegral<int32_t>();
49 
50     DistributedSchedService::GetInstance().GetIsFreeInstall(missionId);
51 }
52 
StartContinuationFuzzTest(const uint8_t * data,size_t size)53 void StartContinuationFuzzTest(const uint8_t* data, size_t size)
54 {
55     if (data == nullptr || size < sizeof(int32_t) + sizeof(int32_t) + sizeof(int32_t) + sizeof(uint32_t)) {
56         return;
57     }
58 
59     FuzzedDataProvider fdp(data, size);
60     AAFwk::Want want;
61     std::string deviceId = fdp.ConsumeRandomLengthString();
62     std::string bundleName = fdp.ConsumeRandomLengthString();
63     std::string abilityName = fdp.ConsumeRandomLengthString();
64     AppExecFwk::ElementName element(deviceId, bundleName, abilityName);
65     want.SetElement(element);
66     want.SetFlags(fdp.ConsumeIntegral<uint32_t>());
67     int32_t missionId = fdp.ConsumeIntegral<int32_t>();
68     int32_t callerUid = fdp.ConsumeIntegral<int32_t>();
69     int32_t status = fdp.ConsumeIntegral<int32_t>();
70     uint32_t accessToken = fdp.ConsumeIntegral<uint32_t>();
71 
72     DistributedSchedService::GetInstance().StartContinuation(want, missionId, callerUid, status, accessToken);
73 }
74 
NotifyContinuationResultFromRemoteFuzzTest(const uint8_t * data,size_t size)75 void NotifyContinuationResultFromRemoteFuzzTest(const uint8_t* data, size_t size)
76 {
77     if (data == nullptr || size < sizeof(int32_t)) {
78         return;
79     }
80     FuzzedDataProvider fdp(data, size);
81     int32_t sessionId = fdp.ConsumeIntegralInRange<int32_t>(1, INT32_MAX);
82     std::string dstInfo = fdp.ConsumeRandomLengthString();
83     bool isSuccess = fdp.ConsumeBool();
84     DistributedSchedService::GetInstance().NotifyContinuationResultFromRemote(sessionId, isSuccess, dstInfo);
85 }
86 
NotifyDSchedEventResultFromRemoteFuzzTest(const uint8_t * data,size_t size)87 void NotifyDSchedEventResultFromRemoteFuzzTest(const uint8_t* data, size_t size)
88 {
89     if (data == nullptr || size < sizeof(int32_t)) {
90         return;
91     }
92     FuzzedDataProvider fdp(data, size);
93     std::string type = fdp.ConsumeRandomLengthString();
94     int32_t dSchedEventResult = fdp.ConsumeIntegral<int32_t>();
95     DistributedSchedService::GetInstance().NotifyDSchedEventResultFromRemote(type, dSchedEventResult);
96 }
97 
NotifyDSchedEventCallbackResultFuzzTest(const uint8_t * data,size_t size)98 void NotifyDSchedEventCallbackResultFuzzTest(const uint8_t* data, size_t size)
99 {
100     if (data == nullptr || size < sizeof(int32_t)) {
101         return;
102     }
103 
104     FuzzedDataProvider fdp(data, size);
105     int32_t resultCode = fdp.ConsumeIntegral<int32_t>();
106     EventNotify event;
107     event.eventResult_ = fdp.ConsumeIntegral<int32_t>();
108     event.srcNetworkId_ = fdp.ConsumeRandomLengthString();
109     event.dstNetworkId_ = fdp.ConsumeRandomLengthString();
110     event.srcBundleName_ = fdp.ConsumeRandomLengthString();
111     event.srcModuleName_ = fdp.ConsumeRandomLengthString();
112     event.srcAbilityName_ = fdp.ConsumeRandomLengthString();
113     event.destBundleName_ = fdp.ConsumeRandomLengthString();
114     event.destModuleName_ = fdp.ConsumeRandomLengthString();
115     event.destAbilityName_ = fdp.ConsumeRandomLengthString();
116     event.dSchedEventType_ = static_cast<DSchedEventType>(fdp.ConsumeIntegral<int32_t>());
117     event.state_ = static_cast<DSchedEventState>(fdp.ConsumeIntegral<int32_t>());
118 
119     DistributedSchedService::GetInstance().NotifyDSchedEventCallbackResult(resultCode, event);
120 }
121 }
122 }
123 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)124 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
125 {
126     OHOS::DistributedSchedule::GetIsFreeInstallFuzzTest(data, size);
127     OHOS::DistributedSchedule::StartContinuationFuzzTest(data, size);
128     OHOS::DistributedSchedule::NotifyContinuationResultFromRemoteFuzzTest(data, size);
129     OHOS::DistributedSchedule::NotifyDSchedEventResultFromRemoteFuzzTest(data, size);
130     OHOS::DistributedSchedule::NotifyDSchedEventCallbackResultFuzzTest(data, size);
131     return 0;
132 }
133