1 /*
2 * Copyright (c) 2023 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 "allochaptokenstub_fuzzer.h"
17 #include <iostream>
18 #include <string>
19 #include <thread>
20 #include <vector>
21 #undef private
22 #include "accesstoken_manager_service.h"
23 #include "hap_info_parcel.h"
24 #include "i_accesstoken_manager.h"
25
26 using namespace std;
27 using namespace OHOS::Security::AccessToken;
28
29 namespace OHOS {
AllocHapTokenStubFuzzTest(const uint8_t * data,size_t size)30 bool AllocHapTokenStubFuzzTest(const uint8_t* data, size_t size)
31 {
32 if ((data == nullptr) || (size == 0)) {
33 return false;
34 }
35
36 std::string testName(reinterpret_cast<const char *>(data), size);
37
38 PermissionDef TestPermDef = {
39 .permissionName = testName,
40 .bundleName = testName,
41 .grantMode = 1,
42 .availableLevel = APL_NORMAL,
43 .label = testName,
44 .labelId = 1,
45 .description = testName,
46 .descriptionId = 1};
47 PermissionStateFull TestState = {
48 .permissionName = testName,
49 .isGeneral = true,
50 .resDeviceID = {testName},
51 .grantStatus = {PermissionState::PERMISSION_GRANTED},
52 .grantFlags = {1},
53 };
54 HapInfoParams TestInfoParms = {
55 .userID = 1,
56 .bundleName = testName,
57 .instIndex = 0,
58 .appIDDesc = testName};
59 HapPolicyParams TestPolicyPrams = {
60 .apl = APL_NORMAL,
61 .domain = testName,
62 .permList = {TestPermDef},
63 .permStateList = {TestState}};
64
65 HapInfoParcel hapInfoParcel;
66 HapPolicyParcel hapPolicyParcel;
67 hapInfoParcel.hapInfoParameter = TestInfoParms;
68 hapPolicyParcel.hapPolicyParameter = TestPolicyPrams;
69
70 MessageParcel datas;
71 datas.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
72 if (!datas.WriteParcelable(&hapInfoParcel)) {
73 return false;
74 }
75 if (!datas.WriteParcelable(&hapPolicyParcel)) {
76 return false;
77 }
78
79 uint32_t code = static_cast<uint32_t>(
80 AccessTokenInterfaceCode::ALLOC_TOKEN_HAP);
81
82 MessageParcel reply;
83 MessageOption option;
84 DelayedSingleton<AccessTokenManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
85
86 return true;
87 }
88 } // namespace OHOS
89
90 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
92 {
93 /* Run your code on data */
94 OHOS::AllocHapTokenStubFuzzTest(data, size);
95 return 0;
96 }
97