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 "addsystemability_fuzzer.h"
175ba71b47Sopenharmony_ci
185ba71b47Sopenharmony_ci#include "fuzztest_utils.h"
195ba71b47Sopenharmony_ci#include "string_ex.h"
205ba71b47Sopenharmony_ci#include "itest_transaction_service.h"
215ba71b47Sopenharmony_ci#include "samgr_ipc_interface_code.h"
225ba71b47Sopenharmony_cinamespace OHOS {
235ba71b47Sopenharmony_cinamespace Samgr {
245ba71b47Sopenharmony_cinamespace {
255ba71b47Sopenharmony_ciconstexpr size_t THRESHOLD = 4;
265ba71b47Sopenharmony_ciconst std::u16string SAMGR_INTERFACE_TOKEN = u"ohos.samgr.accessToken";
275ba71b47Sopenharmony_ci}
285ba71b47Sopenharmony_civoid FuzzAddSystemAbility(const uint8_t *data, size_t size)
295ba71b47Sopenharmony_ci{
305ba71b47Sopenharmony_ci    auto systemAbilityId = FuzzTestUtils::BuildInt32FromData(data, size);
315ba71b47Sopenharmony_ci    auto isDistributed = FuzzTestUtils::BuildBoolFromData(data, size);
325ba71b47Sopenharmony_ci    sptr<IRemoteObject> saObj = new(std::nothrow) MockSystemAbilityStatusChange();
335ba71b47Sopenharmony_ci    if (saObj == nullptr) {
345ba71b47Sopenharmony_ci        return;
355ba71b47Sopenharmony_ci    }
365ba71b47Sopenharmony_ci    MessageParcel parcelData;
375ba71b47Sopenharmony_ci    parcelData.WriteInterfaceToken(SAMGR_INTERFACE_TOKEN);
385ba71b47Sopenharmony_ci    parcelData.WriteInt32(systemAbilityId);
395ba71b47Sopenharmony_ci    parcelData.WriteRemoteObject(saObj);
405ba71b47Sopenharmony_ci    parcelData.WriteBool(isDistributed);
415ba71b47Sopenharmony_ci    auto dumpFlags = FuzzTestUtils::BuildInt32FromData(data, size);
425ba71b47Sopenharmony_ci    parcelData.WriteInt32(dumpFlags);
435ba71b47Sopenharmony_ci    auto capability = Str8ToStr16(FuzzTestUtils::BuildStringFromData(data, size));
445ba71b47Sopenharmony_ci    parcelData.WriteString16(capability);
455ba71b47Sopenharmony_ci    auto permission = Str8ToStr16(FuzzTestUtils::BuildStringFromData(data, size));
465ba71b47Sopenharmony_ci    parcelData.WriteString16(permission);
475ba71b47Sopenharmony_ci    FuzzTestUtils::FuzzTestRemoteRequest(parcelData,
485ba71b47Sopenharmony_ci        static_cast<uint32_t>(SamgrInterfaceCode::ADD_SYSTEM_ABILITY_TRANSACTION));
495ba71b47Sopenharmony_ci}
505ba71b47Sopenharmony_ci}
515ba71b47Sopenharmony_ci}
525ba71b47Sopenharmony_ci/* Fuzzer entry point */
535ba71b47Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
545ba71b47Sopenharmony_ci{
555ba71b47Sopenharmony_ci    if (size < OHOS::Samgr::THRESHOLD) {
565ba71b47Sopenharmony_ci        return 0;
575ba71b47Sopenharmony_ci    }
585ba71b47Sopenharmony_ci    OHOS::Samgr::FuzzAddSystemAbility(data, size);
595ba71b47Sopenharmony_ci    return 0;
605ba71b47Sopenharmony_ci}