1 /*
2 * Copyright (c) 2022-2025 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
22 #include "accesstoken_callbacks.h"
23 #include "accesstoken_kit.h"
24 #define private public
25 #include "accesstoken_manager_client.h"
26 #undef private
27 #include "fuzzer/FuzzedDataProvider.h"
28
29 using namespace std;
30 using namespace OHOS::Security::AccessToken;
31
32 class CbCustomizeTest2 : public PermStateChangeCallbackCustomize {
33 public:
CbCustomizeTest2(const PermStateChangeScope & scopeInfo)34 explicit CbCustomizeTest2(const PermStateChangeScope &scopeInfo)
35 : PermStateChangeCallbackCustomize(scopeInfo)
36 {
37 }
38
~CbCustomizeTest2()39 ~CbCustomizeTest2()
40 {}
41
PermStateChangeCallback(PermStateChangeInfo & result)42 virtual void PermStateChangeCallback(PermStateChangeInfo& result)
43 {
44 ready_ = true;
45 }
46
47 bool ready_ = false;
48 };
49
50 namespace OHOS {
RegisterPermStateChangeCallbackFuzzTest(const uint8_t * data,size_t size)51 bool RegisterPermStateChangeCallbackFuzzTest(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 PermStateChangeScope scopeInfo;
59 scopeInfo.permList = { provider.ConsumeRandomLengthString() };
60 scopeInfo.tokenIDs = { provider.ConsumeIntegral<AccessTokenID>() };
61 auto callbackPtr = std::make_shared<CbCustomizeTest2>(scopeInfo);
62 AccessTokenKit::RegisterPermStateChangeCallback(callbackPtr);
63 auto callback = new (std::nothrow) PermissionStateChangeCallback(callbackPtr);
64 if (callback == nullptr) {
65 return true;
66 }
67 AccessTokenManagerClient::GetInstance().callbackMap_[callbackPtr] = callback;
68 AccessTokenKit::UnRegisterPermStateChangeCallback(callbackPtr);
69 auto callbackPtr2 = std::make_shared<CbCustomizeTest2>(scopeInfo);
70 AccessTokenKit::RegisterSelfPermStateChangeCallback(callbackPtr2);
71 AccessTokenKit::UnRegisterSelfPermStateChangeCallback(callbackPtr);
72
73 return true;
74 }
75 }
76
77 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)78 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
79 {
80 /* Run your code on data */
81 OHOS::RegisterPermStateChangeCallbackFuzzTest(data, size);
82 return 0;
83 }
84