1 /*
2 * Copyright (c) 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 "registertokensynccallback_fuzzer.h"
17
18 #include "accesstoken_kit.h"
19 #include "fuzzer/FuzzedDataProvider.h"
20 #include "token_setproc.h"
21 #include "token_sync_kit_interface.h"
22
23 using namespace std;
24 using namespace OHOS::Security::AccessToken;
25 #ifdef TOKEN_SYNC_ENABLE
26 static constexpr int32_t NUMBER_TWO = 2;
27 #endif
28 namespace {
29 class TokenSyncCallback : public TokenSyncKitInterface {
30 public:
31 ~TokenSyncCallback() = default;
GetRemoteHapTokenInfo(const std::string & deviceID,AccessTokenID tokenID) const32 int32_t GetRemoteHapTokenInfo(const std::string& deviceID, AccessTokenID tokenID) const override
33 {
34 return TokenSyncError::TOKEN_SYNC_OPENSOURCE_DEVICE; // TOKEN_SYNC_OPENSOURCE_DEVICE is a test
35 };
36
DeleteRemoteHapTokenInfo(AccessTokenID tokenID) const37 int32_t DeleteRemoteHapTokenInfo(AccessTokenID tokenID) const override
38 {
39 return TokenSyncError::TOKEN_SYNC_SUCCESS; // TOKEN_SYNC_SUCCESS is a test
40 };
41
UpdateRemoteHapTokenInfo(const HapTokenInfoForSync & tokenInfo) const42 int32_t UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& tokenInfo) const override
43 {
44 return TokenSyncError::TOKEN_SYNC_SUCCESS; // TOKEN_SYNC_SUCCESS is a test
45 };
46 };
47
NativeTokenGet()48 bool NativeTokenGet()
49 {
50 AccessTokenID token = AccessTokenKit::GetNativeTokenId("token_sync_service");
51 if (token == 0) {
52 return false;
53 }
54 SetSelfTokenID(token);
55 return true;
56 }
57 };
58
59 namespace OHOS {
RegisterTokenSyncCallbackFuzzTest(const uint8_t * data,size_t size)60 bool RegisterTokenSyncCallbackFuzzTest(const uint8_t* data, size_t size)
61 {
62 if ((data == nullptr) || (size == 0)) {
63 return false;
64 }
65 #ifdef TOKEN_SYNC_ENABLE
66 if (!NativeTokenGet()) {
67 return false;
68 }
69 FuzzedDataProvider provider(data, size);
70 if ((provider.ConsumeIntegral<int32_t>() % NUMBER_TWO) == 0) {
71 std::shared_ptr<TokenSyncKitInterface> callback = std::make_shared<TokenSyncCallback>();
72 AccessTokenKit::RegisterTokenSyncCallback(callback);
73 } else {
74 AccessTokenKit::UnRegisterTokenSyncCallback();
75 }
76 #endif // TOKEN_SYNC_ENABLE
77 return true;
78 }
79 }
80
81 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)82 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
83 {
84 /* Run your code on data */
85 OHOS::RegisterTokenSyncCallbackFuzzTest(data, size);
86 return 0;
87 }