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