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 "registerpermstatechangecallback_fuzzer.h"
17
18 #include <string>
19 #include <vector>
20 #include <thread>
21 #include "accesstoken_kit.h"
22
23 using namespace std;
24 using namespace OHOS::Security::AccessToken;
25
26 class CbCustomizeTest2 : public PermStateChangeCallbackCustomize {
27 public:
CbCustomizeTest2(const PermStateChangeScope & scopeInfo)28 explicit CbCustomizeTest2(const PermStateChangeScope &scopeInfo)
29 : PermStateChangeCallbackCustomize(scopeInfo)
30 {
31 }
32
~CbCustomizeTest2()33 ~CbCustomizeTest2()
34 {}
35
PermStateChangeCallback(PermStateChangeInfo & result)36 virtual void PermStateChangeCallback(PermStateChangeInfo& result)
37 {
38 ready_ = true;
39 }
40
41 bool ready_ = false;
42 };
43
44 namespace OHOS {
RegisterPermStateChangeCallbackFuzzTest(const uint8_t * data,size_t size)45 bool RegisterPermStateChangeCallbackFuzzTest(const uint8_t* data, size_t size)
46 {
47 int32_t result = RET_FAILED;
48 if ((data == nullptr) || (size == 0)) {
49 return false;
50 }
51
52 PermStateChangeScope scopeInfo;
53 std::string testName(reinterpret_cast<const char*>(data), size);
54 AccessTokenID TOKENID = static_cast<AccessTokenID>(size);
55 scopeInfo.permList = { testName };
56 scopeInfo.tokenIDs = { TOKENID };
57 auto callbackPtr = std::make_shared<CbCustomizeTest2>(scopeInfo);
58 result = AccessTokenKit::RegisterPermStateChangeCallback(callbackPtr);
59
60 return result == RET_SUCCESS;
61 }
62 }
63
64 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)65 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
66 {
67 /* Run your code on data */
68 OHOS::RegisterPermStateChangeCallbackFuzzTest(data, size);
69 return 0;
70 }
71