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 "abilityconnectionwrapperstub_fuzzer.h"
17
18 #include <fuzzer/FuzzedDataProvider.h>
19
20 #include "ability_connection_wrapper_stub.h"
21 #include "mock_distributed_sched.h"
22 #include "mock_fuzz_util.h"
23 #include "parcel_helper.h"
24
25 namespace OHOS {
26 namespace DistributedSchedule {
27 using namespace AAFwk;
28
OnAbilityConnectDoneFuzzTest(const uint8_t * data,size_t size)29 bool OnAbilityConnectDoneFuzzTest(const uint8_t* data, size_t size)
30 {
31 if ((data == nullptr) || (size < sizeof(int32_t))) {
32 return false;
33 }
34 FuzzUtil::MockPermission();
35 FuzzedDataProvider fdp(data, size);
36 std::shared_ptr<AbilityConnectionWrapperStub> abilityConnection =
37 std::make_shared<AbilityConnectionWrapperStub>();
38 sptr<IRemoteObject> connection(new MockDistributedSched());
39 std::string localDeviceId = fdp.ConsumeRandomLengthString();
40 std::shared_ptr<AbilityConnectionWrapperStub> abilityConnection_ =
41 std::make_shared<AbilityConnectionWrapperStub>(connection, localDeviceId);
42 uint32_t code = IAbilityConnection::ON_ABILITY_CONNECT_DONE;
43 int32_t resultCode = fdp.ConsumeIntegral<int32_t>();
44 MessageParcel dataParcel;
45 MessageParcel reply;
46 MessageOption option;
47 abilityConnection_->OnRemoteRequest(code, dataParcel, reply, option);
48
49 std::u16string descriptor = IAbilityConnection::GetDescriptor();
50 dataParcel.WriteInterfaceToken(descriptor);
51 abilityConnection_->OnRemoteRequest(code, dataParcel, reply, option);
52
53 std::string str1 = fdp.ConsumeRandomLengthString();
54 std::string str2 = fdp.ConsumeRandomLengthString();
55 std::string str3 = fdp.ConsumeRandomLengthString();
56 AppExecFwk::ElementName element(str1, str2, str3);
57 dataParcel.WriteParcelable(&element);
58 abilityConnection_->OnRemoteRequest(code, dataParcel, reply, option);
59
60 dataParcel.WriteRemoteObject(connection);
61 dataParcel.WriteInt32(resultCode);
62 abilityConnection_->OnRemoteRequest(code, dataParcel, reply, option);
63 abilityConnection_->OnAbilityConnectDone(element, connection, resultCode);
64 return true;
65 }
66
OnAbilityDisconnectDoneFuzzTest(const uint8_t * data,size_t size)67 bool OnAbilityDisconnectDoneFuzzTest(const uint8_t* data, size_t size)
68 {
69 if ((data == nullptr) || (size < sizeof(int32_t))) {
70 return false;
71 }
72 FuzzUtil::MockPermission();
73 FuzzedDataProvider fdp(data, size);
74 sptr<IRemoteObject> connection(new MockDistributedSched());
75 std::shared_ptr<AbilityConnectionWrapperStub> abilityConnection_ =
76 std::make_shared<AbilityConnectionWrapperStub>(connection);
77 uint32_t code = IAbilityConnection::ON_ABILITY_DISCONNECT_DONE;
78 int32_t resultCode = fdp.ConsumeIntegral<int32_t>();
79 MessageParcel dataParcel;
80 MessageParcel reply;
81 MessageOption option;
82 std::string str1 = fdp.ConsumeRandomLengthString();
83 std::string str2 = fdp.ConsumeRandomLengthString();
84 std::string str3 = fdp.ConsumeRandomLengthString();
85 std::u16string descriptor = IAbilityConnection::GetDescriptor();
86 dataParcel.WriteInterfaceToken(descriptor);
87 AppExecFwk::ElementName element(str1, str2, str3);
88 dataParcel.WriteParcelable(&element);
89 dataParcel.WriteRemoteObject(connection);
90 dataParcel.WriteInt32(resultCode);
91 abilityConnection_->OnRemoteRequest(code, dataParcel, reply, option);
92 abilityConnection_->OnAbilityDisconnectDone(element, resultCode);
93 return true;
94 }
95 }
96 }
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 OHOS::DistributedSchedule::OnAbilityConnectDoneFuzzTest(data, size);
102 OHOS::DistributedSchedule::OnAbilityDisconnectDoneFuzzTest(data, size);
103 return 0;
104 }