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 "nfccontroller_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "nfc_controller.h"
22 #include "nfc_sdk_common.h"
23
24 namespace OHOS {
25 using namespace OHOS::NFC::KITS;
26
27 class INfcControllerCallbackImpl : public NFC::INfcControllerCallback {
28 public:
INfcControllerCallbackImpl()29 INfcControllerCallbackImpl() {}
30
~INfcControllerCallbackImpl()31 virtual ~INfcControllerCallbackImpl() {}
32
33 public:
OnNfcStateChanged(int nfcState)34 void OnNfcStateChanged(int nfcState) override
35 {
36 }
37
AsObject()38 OHOS::sptr<OHOS::IRemoteObject> AsObject() override
39 {
40 return nullptr;
41 }
42 };
43
44 constexpr const auto INT_TO_BOOL_DIVISOR = 2;
45
FuzzIsNfcOpen(const uint8_t * data,size_t size)46 void FuzzIsNfcOpen(const uint8_t* data, size_t size)
47 {
48 NfcController ctrl = NfcController::GetInstance();
49 bool isOpen = data[0] % INT_TO_BOOL_DIVISOR;
50 ctrl.IsNfcOpen(isOpen);
51 }
52
FuzzRegListener(const uint8_t * data,size_t size)53 void FuzzRegListener(const uint8_t* data, size_t size)
54 {
55 NfcController ctrl = NfcController::GetInstance();
56 std::string type = NfcSdkCommon::BytesVecToHexString(data, size);
57 sptr<INfcControllerCallbackImpl> iNfcControllerCallbackImpl =
58 sptr<INfcControllerCallbackImpl>(new (std::nothrow) INfcControllerCallbackImpl());
59 ctrl.RegListener(iNfcControllerCallbackImpl, type);
60 }
61
FuzzUnregListener(const uint8_t * data,size_t size)62 void FuzzUnregListener(const uint8_t* data, size_t size)
63 {
64 NfcController ctrl = NfcController::GetInstance();
65 std::string type = NfcSdkCommon::BytesVecToHexString(data, size);
66 ctrl.UnregListener(type);
67 }
68 }
69
70 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)71 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
72 {
73 /* Run your code on data */
74 OHOS::FuzzUnregListener(data, size);
75 return 0;
76 }
77
78