• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "addsystemability_fuzzer.h"
17 
18 #include <fuzzer/FuzzedDataProvider.h>
19 #include "fuzztest_utils.h"
20 #include "string_ex.h"
21 #include "itest_transaction_service.h"
22 #include "samgr_ipc_interface_code.h"
23 namespace OHOS {
24 namespace Samgr {
25 namespace {
26 constexpr size_t THRESHOLD = 4;
27 const std::u16string SAMGR_INTERFACE_TOKEN = u"ohos.samgr.accessToken";
28 }
FuzzAddSystemAbility(const uint8_t * data,size_t size)29 void FuzzAddSystemAbility(const uint8_t *data, size_t size)
30 {
31     FuzzedDataProvider fdp(data, size);
32     auto systemAbilityId = fdp.ConsumeIntegral<int32_t>();
33     auto isDistributed = fdp.ConsumeBool();
34     sptr<IRemoteObject> saObj = new(std::nothrow) MockSystemAbilityStatusChange();
35     if (saObj == nullptr) {
36         return;
37     }
38     MessageParcel parcelData;
39     parcelData.WriteInterfaceToken(SAMGR_INTERFACE_TOKEN);
40     parcelData.WriteInt32(systemAbilityId);
41     parcelData.WriteRemoteObject(saObj);
42     parcelData.WriteBool(isDistributed);
43     auto dumpFlags = fdp.ConsumeIntegral<int32_t>();
44     parcelData.WriteInt32(dumpFlags);
45     auto capability = u"";
46     parcelData.WriteString16(capability);
47     auto permission = u"";
48     parcelData.WriteString16(permission);
49     FuzzTestUtils::FuzzTestRemoteRequest(parcelData,
50         static_cast<uint32_t>(SamgrInterfaceCode::ADD_SYSTEM_ABILITY_TRANSACTION));
51 }
52 }
53 }
54 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)55 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
56 {
57     if (size < OHOS::Samgr::THRESHOLD) {
58         return 0;
59     }
60     OHOS::Samgr::FuzzAddSystemAbility(data, size);
61     return 0;
62 }