15ba71b47Sopenharmony_ci/*
25ba71b47Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
35ba71b47Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
45ba71b47Sopenharmony_ci * you may not use this file except in compliance with the License.
55ba71b47Sopenharmony_ci * You may obtain a copy of the License at
65ba71b47Sopenharmony_ci *
75ba71b47Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
85ba71b47Sopenharmony_ci *
95ba71b47Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
105ba71b47Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
115ba71b47Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125ba71b47Sopenharmony_ci * See the License for the specific language governing permissions and
135ba71b47Sopenharmony_ci * limitations under the License.
145ba71b47Sopenharmony_ci */
155ba71b47Sopenharmony_ci
165ba71b47Sopenharmony_ci#include "unsubscribesystemability_fuzzer.h"
175ba71b47Sopenharmony_ci
185ba71b47Sopenharmony_ci#include "fuzztest_utils.h"
195ba71b47Sopenharmony_ci#include "samgr_ipc_interface_code.h"
205ba71b47Sopenharmony_cinamespace OHOS {
215ba71b47Sopenharmony_cinamespace Samgr {
225ba71b47Sopenharmony_cinamespace {
235ba71b47Sopenharmony_ciconstexpr size_t THRESHOLD = 4;
245ba71b47Sopenharmony_ciconst std::u16string SAMGR_INTERFACE_TOKEN = u"ohos.samgr.accessToken";
255ba71b47Sopenharmony_ci}
265ba71b47Sopenharmony_ci
275ba71b47Sopenharmony_civoid FuzzSubscribeSystemAbility(const uint8_t *data, size_t size)
285ba71b47Sopenharmony_ci{
295ba71b47Sopenharmony_ci    sptr<MockSystemAbilityStatusChange> listener = new(std::nothrow) MockSystemAbilityStatusChange();
305ba71b47Sopenharmony_ci    if (listener == nullptr) {
315ba71b47Sopenharmony_ci        return;
325ba71b47Sopenharmony_ci    }
335ba71b47Sopenharmony_ci    MessageParcel parcelData;
345ba71b47Sopenharmony_ci    parcelData.WriteInterfaceToken(SAMGR_INTERFACE_TOKEN);
355ba71b47Sopenharmony_ci    int32_t systemAbilityId = FuzzTestUtils::BuildInt32FromData(data, size);
365ba71b47Sopenharmony_ci    parcelData.WriteInt32(systemAbilityId);
375ba71b47Sopenharmony_ci    parcelData.WriteRemoteObject(listener);
385ba71b47Sopenharmony_ci    FuzzTestUtils::FuzzTestRemoteRequest(parcelData,
395ba71b47Sopenharmony_ci        static_cast<uint32_t>(SamgrInterfaceCode::SUBSCRIBE_SYSTEM_ABILITY_TRANSACTION));
405ba71b47Sopenharmony_ci}
415ba71b47Sopenharmony_ci
425ba71b47Sopenharmony_civoid FuzzUnSubscribeSystemAbility(const uint8_t *data, size_t size)
435ba71b47Sopenharmony_ci{
445ba71b47Sopenharmony_ci    sptr<MockSystemAbilityStatusChange> listener = new(std::nothrow) MockSystemAbilityStatusChange();
455ba71b47Sopenharmony_ci    if (listener == nullptr) {
465ba71b47Sopenharmony_ci        return;
475ba71b47Sopenharmony_ci    }
485ba71b47Sopenharmony_ci    MessageParcel parcelData;
495ba71b47Sopenharmony_ci    parcelData.WriteInterfaceToken(SAMGR_INTERFACE_TOKEN);
505ba71b47Sopenharmony_ci    int32_t systemAbilityId = FuzzTestUtils::BuildInt32FromData(data, size);
515ba71b47Sopenharmony_ci    parcelData.WriteInt32(systemAbilityId);
525ba71b47Sopenharmony_ci    parcelData.WriteRemoteObject(listener);
535ba71b47Sopenharmony_ci    FuzzTestUtils::FuzzTestRemoteRequest(parcelData,
545ba71b47Sopenharmony_ci        static_cast<uint32_t>(SamgrInterfaceCode::UNSUBSCRIBE_SYSTEM_ABILITY_TRANSACTION));
555ba71b47Sopenharmony_ci}
565ba71b47Sopenharmony_ci}
575ba71b47Sopenharmony_ci}
585ba71b47Sopenharmony_ci/* Fuzzer entry point */
595ba71b47Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
605ba71b47Sopenharmony_ci{
615ba71b47Sopenharmony_ci    if (size < OHOS::Samgr::THRESHOLD) {
625ba71b47Sopenharmony_ci        return 0;
635ba71b47Sopenharmony_ci    }
645ba71b47Sopenharmony_ci    OHOS::Samgr::FuzzSubscribeSystemAbility(data, size);
655ba71b47Sopenharmony_ci    OHOS::Samgr::FuzzUnSubscribeSystemAbility(data, size);
665ba71b47Sopenharmony_ci    return 0;
675ba71b47Sopenharmony_ci}