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