1686862fbSopenharmony_ci/*
2686862fbSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3686862fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4686862fbSopenharmony_ci * you may not use this file except in compliance with the License.
5686862fbSopenharmony_ci * You may obtain a copy of the License at
6686862fbSopenharmony_ci *
7686862fbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8686862fbSopenharmony_ci *
9686862fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10686862fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11686862fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12686862fbSopenharmony_ci * See the License for the specific language governing permissions and
13686862fbSopenharmony_ci * limitations under the License.
14686862fbSopenharmony_ci */
15686862fbSopenharmony_ci
16686862fbSopenharmony_ci#include "abilityconnectionwrapperstub_fuzzer.h"
17686862fbSopenharmony_ci
18686862fbSopenharmony_ci#include "ability_connection_wrapper_stub.h"
19686862fbSopenharmony_ci#include "mock_distributed_sched.h"
20686862fbSopenharmony_ci#include "mock_fuzz_util.h"
21686862fbSopenharmony_ci#include "parcel_helper.h"
22686862fbSopenharmony_ci
23686862fbSopenharmony_cinamespace OHOS {
24686862fbSopenharmony_cinamespace DistributedSchedule {
25686862fbSopenharmony_ciusing namespace AAFwk;
26686862fbSopenharmony_ci
27686862fbSopenharmony_cibool OnAbilityConnectDoneFuzzTest(const uint8_t* data, size_t size)
28686862fbSopenharmony_ci{
29686862fbSopenharmony_ci    if ((data == nullptr) || (size < sizeof(int32_t))) {
30686862fbSopenharmony_ci        return false;
31686862fbSopenharmony_ci    }
32686862fbSopenharmony_ci    FuzzUtil::MockPermission();
33686862fbSopenharmony_ci    sptr<IRemoteObject> connection(new MockDistributedSched());
34686862fbSopenharmony_ci    std::string localDeviceId(reinterpret_cast<const char*>(data), size);
35686862fbSopenharmony_ci    std::shared_ptr<AbilityConnectionWrapperStub> abilityConnection_ =
36686862fbSopenharmony_ci        std::make_shared<AbilityConnectionWrapperStub>(connection, localDeviceId);
37686862fbSopenharmony_ci    uint32_t code = IAbilityConnection::ON_ABILITY_CONNECT_DONE;
38686862fbSopenharmony_ci    int32_t resultCode = *(reinterpret_cast<const int32_t*>(data));
39686862fbSopenharmony_ci    MessageParcel dataParcel;
40686862fbSopenharmony_ci    MessageParcel reply;
41686862fbSopenharmony_ci    MessageOption option;
42686862fbSopenharmony_ci    abilityConnection_->OnRemoteRequest(code, dataParcel, reply, option);
43686862fbSopenharmony_ci
44686862fbSopenharmony_ci    std::u16string descriptor = IAbilityConnection::GetDescriptor();
45686862fbSopenharmony_ci    dataParcel.WriteInterfaceToken(descriptor);
46686862fbSopenharmony_ci    abilityConnection_->OnRemoteRequest(code, dataParcel, reply, option);
47686862fbSopenharmony_ci
48686862fbSopenharmony_ci    std::string str1(reinterpret_cast<const char*>(data), size);
49686862fbSopenharmony_ci    std::string str2(reinterpret_cast<const char*>(data), size);
50686862fbSopenharmony_ci    std::string str3(reinterpret_cast<const char*>(data), size);
51686862fbSopenharmony_ci    AppExecFwk::ElementName element(str1, str2, str3);
52686862fbSopenharmony_ci    dataParcel.WriteParcelable(&element);
53686862fbSopenharmony_ci    abilityConnection_->OnRemoteRequest(code, dataParcel, reply, option);
54686862fbSopenharmony_ci
55686862fbSopenharmony_ci    dataParcel.WriteRemoteObject(connection);
56686862fbSopenharmony_ci    dataParcel.WriteInt32(resultCode);
57686862fbSopenharmony_ci    abilityConnection_->OnRemoteRequest(code, dataParcel, reply, option);
58686862fbSopenharmony_ci    abilityConnection_->OnAbilityConnectDone(element, connection, resultCode);
59686862fbSopenharmony_ci    return true;
60686862fbSopenharmony_ci}
61686862fbSopenharmony_ci
62686862fbSopenharmony_cibool OnAbilityDisconnectDoneFuzzTest(const uint8_t* data, size_t size)
63686862fbSopenharmony_ci{
64686862fbSopenharmony_ci    if ((data == nullptr) || (size < sizeof(int32_t))) {
65686862fbSopenharmony_ci        return false;
66686862fbSopenharmony_ci    }
67686862fbSopenharmony_ci    FuzzUtil::MockPermission();
68686862fbSopenharmony_ci    sptr<IRemoteObject> connection(new MockDistributedSched());
69686862fbSopenharmony_ci    std::shared_ptr<AbilityConnectionWrapperStub> abilityConnection_ =
70686862fbSopenharmony_ci        std::make_shared<AbilityConnectionWrapperStub>(connection);
71686862fbSopenharmony_ci    uint32_t code = IAbilityConnection::ON_ABILITY_DISCONNECT_DONE;
72686862fbSopenharmony_ci    int32_t resultCode = *(reinterpret_cast<const int32_t*>(data));
73686862fbSopenharmony_ci    MessageParcel dataParcel;
74686862fbSopenharmony_ci    MessageParcel reply;
75686862fbSopenharmony_ci    MessageOption option;
76686862fbSopenharmony_ci    std::string str1(reinterpret_cast<const char*>(data), size);
77686862fbSopenharmony_ci    std::string str2(reinterpret_cast<const char*>(data), size);
78686862fbSopenharmony_ci    std::string str3(reinterpret_cast<const char*>(data), size);
79686862fbSopenharmony_ci    std::u16string descriptor = IAbilityConnection::GetDescriptor();
80686862fbSopenharmony_ci    dataParcel.WriteInterfaceToken(descriptor);
81686862fbSopenharmony_ci    AppExecFwk::ElementName element(str1, str2, str3);
82686862fbSopenharmony_ci    dataParcel.WriteParcelable(&element);
83686862fbSopenharmony_ci    dataParcel.WriteRemoteObject(connection);
84686862fbSopenharmony_ci    dataParcel.WriteInt32(resultCode);
85686862fbSopenharmony_ci    abilityConnection_->OnRemoteRequest(code, dataParcel, reply, option);
86686862fbSopenharmony_ci    abilityConnection_->OnAbilityDisconnectDone(element, resultCode);
87686862fbSopenharmony_ci    return true;
88686862fbSopenharmony_ci}
89686862fbSopenharmony_ci}
90686862fbSopenharmony_ci}
91686862fbSopenharmony_ci
92686862fbSopenharmony_ci/* Fuzzer entry point */
93686862fbSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
94686862fbSopenharmony_ci{
95686862fbSopenharmony_ci    OHOS::DistributedSchedule::OnAbilityConnectDoneFuzzTest(data, size);
96686862fbSopenharmony_ci    OHOS::DistributedSchedule::OnAbilityDisconnectDoneFuzzTest(data, size);
97686862fbSopenharmony_ci    return 0;
98686862fbSopenharmony_ci}