1 /*
2 * Copyright (c) 2022 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 "startusingpermission001_fuzzer.h"
17
18 #include <iostream>
19 #include <thread>
20 #include <string>
21 #include <vector>
22
23 #include "fuzzer/FuzzedDataProvider.h"
24 #undef private
25 #include "privacy_kit.h"
26
27 using namespace std;
28 using namespace OHOS::Security::AccessToken;
29 static bool g_realTokenFlag = true;
30 // hap tokenID 536907816
31 static constexpr AccessTokenID g_realTokenId = static_cast<AccessTokenID>(536907816);
32
33 class CbCustomizeTest : public StateCustomizedCbk {
34 public:
CbCustomizeTest()35 explicit CbCustomizeTest() : StateCustomizedCbk()
36 {
37 }
38
~CbCustomizeTest()39 ~CbCustomizeTest()
40 {}
41
StateChangeNotify(AccessTokenID tokenId,bool isShowing)42 virtual void StateChangeNotify(AccessTokenID tokenId, bool isShowing)
43 {
44 isShowing_ = true;
45 }
46
47 bool isShowing_ = false;
48 };
49
50 namespace OHOS {
StartUsingPermission001FuzzTest(const uint8_t * data,size_t size)51 bool StartUsingPermission001FuzzTest(const uint8_t* data, size_t size)
52 {
53 if ((data == nullptr) || (size == 0)) {
54 return false;
55 }
56
57 FuzzedDataProvider provider(data, size);
58 auto callback = std::make_shared<CbCustomizeTest>();
59 if (callback == nullptr) {
60 return false;
61 }
62 AccessTokenID tokenID = provider.ConsumeIntegral<AccessTokenID>();
63 if (g_realTokenFlag) {
64 tokenID = g_realTokenId;
65 g_realTokenFlag = false;
66 }
67
68 PermissionUsedType type = static_cast<PermissionUsedType>(provider.ConsumeIntegralInRange<uint32_t>(
69 0, static_cast<uint32_t>(PermissionUsedType::PERM_USED_TYPE_BUTT)));
70 return PrivacyKit::StartUsingPermission(tokenID, provider.ConsumeRandomLengthString(), callback,
71 provider.ConsumeIntegral<int32_t>(), type) == 0;
72 }
73 }
74
75 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)76 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
77 {
78 /* Run your code on data */
79 OHOS::StartUsingPermission001FuzzTest(data, size);
80 return 0;
81 }
82