1 /*
2  * Copyright (c) 2022-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 "softbusadapterhisysevent_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <securec.h>
21 #include "softbus_error_code.h"
22 #include "softbus_adapter_hisysevent.h"
23 #include "softbus_hisysevt_connreporter.h"
24 
25 namespace OHOS {
CreateOpenSessionCntMsg(SoftBusEvtReportMsg * msg)26 static void CreateOpenSessionCntMsg(SoftBusEvtReportMsg* msg)
27 {
28     (void)strcpy_s(msg->evtName, SOFTBUS_HISYSEVT_NAME_LEN, "TRANS_OPEN_SESSION_CNT");
29     msg->evtType = SOFTBUS_EVT_TYPE_STATISTIC;
30     msg->paramNum = SOFTBUS_EVT_PARAM_ONE;
31 
32     SoftBusEvtParam* param = &msg->paramArray[SOFTBUS_EVT_PARAM_ZERO];
33     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, "SUCCESS_CNT");
34     param->paramType = SOFTBUS_EVT_PARAMTYPE_UINT32;
35     param->paramValue.u32v = 0;
36 }
37 
SoftBusAdapterHiSysEventFuzzTest(const uint8_t * data,size_t size)38 void SoftBusAdapterHiSysEventFuzzTest(const uint8_t* data, size_t size)
39 {
40     if ((data == nullptr) || (size == 0)) {
41         return;
42     }
43 
44     int32_t tmpParam = *(reinterpret_cast<const int32_t *>(data));
45     SoftBusEvtReportMsg* msg = SoftbusCreateEvtReportMsg(tmpParam);
46     if (msg == nullptr) {
47         return;
48     }
49     CreateOpenSessionCntMsg(msg);
50     SoftbusWriteHisEvt(msg);
51     SoftbusFreeEvtReportMsg(msg);
52 }
53 } // namespace OHOS
54 
55 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)56 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
57 {
58     if (data == nullptr || size < sizeof(int32_t)) {
59         return 0;
60     }
61 
62     OHOS::SoftBusAdapterHiSysEventFuzzTest(data, size);
63 
64     return 0;
65 }