• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "updatehaptokenstub_fuzzer.h"
17 
18 #include <iostream>
19 #include <memory>
20 #include <string>
21 #include <thread>
22 #include <vector>
23 #undef private
24 #include "service/accesstoken_manager_service.h"
25 
26 using namespace std;
27 using namespace OHOS::Security::AccessToken;
28 
29 namespace OHOS {
UpdateHapTokenStubFuzzTest(const uint8_t * data,size_t size)30     bool UpdateHapTokenStubFuzzTest(const uint8_t* data, size_t size)
31     {
32         if ((data == nullptr) || (size == 0)) {
33             return false;
34         }
35         AccessTokenID tokenId = static_cast<AccessTokenID>(size);
36         std::string testName(reinterpret_cast<const char*>(data), size);
37         PermissionDef testPermDef = {.permissionName = testName,
38             .bundleName = testName,
39             .grantMode = 1,
40             .availableLevel = APL_NORMAL,
41             .label = testName,
42             .labelId = 1,
43             .description = testName,
44             .descriptionId = 1};
45         PermissionStateFull testState = {.permissionName = testName,
46             .isGeneral = true,
47             .resDeviceID = {testName},
48             .grantStatus = {PermissionState::PERMISSION_GRANTED},
49             .grantFlags = {1}};
50         HapPolicyParams policy = {.apl = APL_NORMAL,
51             .domain = testName,
52             .permList = {testPermDef},
53             .permStateList = {testState}};
54         int32_t apiVersion = 8;
55         HapPolicyParcel hapPolicyParcel;
56         hapPolicyParcel.hapPolicyParameter = policy;
57         MessageParcel datas;
58         datas.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
59         if (!datas.WriteUint32(tokenId)) {
60             return false;
61         }
62         if (!datas.WriteBool(false)) {
63             return false;
64         }
65         if (!datas.WriteString(testName)) {
66             return false;
67         }
68         if (!datas.WriteInt32(apiVersion)) {
69             return false;
70         }
71         if (!datas.WriteParcelable(&hapPolicyParcel)) {
72             return false;
73         }
74         uint32_t code = static_cast<uint32_t>(AccessTokenInterfaceCode::UPDATE_HAP_TOKEN);
75         MessageParcel reply;
76         MessageOption option;
77         DelayedSingleton<AccessTokenManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
78         return true;
79     }
80 }
81 
82 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
84 {
85     /* Run your code on data */
86     OHOS::UpdateHapTokenStubFuzzTest(data, size);
87     return 0;
88 }
89