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 "softbusclientmanager_fuzzer.h"
17 #include "client_bus_center_manager.h"
18 #include <cstddef>
19 
20 namespace OHOS {
21     constexpr size_t THRESHOLD = 10;
22     constexpr int32_t OFFSET = 4;
23     constexpr uint32_t NINE = 9;
24     enum  CmdId {
25         CMD_SOFTBUS_ONE,
26         CMD_SOFTBUS_TWO,
27         CMD_SOFTBUS_THREE,
28         CMD_SOFTBUS_FOUR,
29         CMD_SOFTBUS_FIVE,
30     };
31 
Convert2Uint32(const uint8_t * ptr)32     uint32_t Convert2Uint32(const uint8_t *ptr)
33     {
34         if (ptr == nullptr) {
35             return 0;
36         }
37         /*
38         * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left,
39         * and the third digit no left
40         */
41         return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
42     }
43 
SoftbusClientMagSwitch(uint32_t cmd,const uint8_t * rawData)44     static void SoftbusClientMagSwitch(uint32_t cmd, const uint8_t *rawData)
45     {
46         void *info = const_cast<void *>(reinterpret_cast<const void *>(rawData));
47         int32_t type = *const_cast<int32_t *>(reinterpret_cast<const int32_t *>(rawData));
48         cmd = cmd % NINE;
49         switch (cmd) {
50             case CMD_SOFTBUS_ONE: {
51                 bool isOnline = true;
52                 LnnOnNodeOnlineStateChanged("", isOnline, info);
53                 break;
54             }
55             case CMD_SOFTBUS_TWO: {
56                 LnnOnNodeBasicInfoChanged("", info, type);
57                 break;
58             }
59             case CMD_SOFTBUS_THREE: {
60                 LnnOnJoinResult(info, reinterpret_cast<const char *>(rawData), type);
61                 break;
62             }
63             case CMD_SOFTBUS_FOUR: {
64                 LnnOnLeaveResult(reinterpret_cast<const char *>(rawData), type);
65                 break;
66             }
67             case CMD_SOFTBUS_FIVE: {
68                 LnnOnTimeSyncResult(reinterpret_cast<const void *>(info), type);
69                 break;
70             }
71             default:
72                 break;
73         }
74     }
75 
DoSomethingInterestingWithMyAPI(const uint8_t * rawData,size_t size)76     bool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)
77     {
78         (void)size;
79 
80         if (rawData == nullptr) {
81             return false;
82         }
83         uint32_t cmd = Convert2Uint32(rawData);
84         rawData = rawData + OFFSET;
85 
86         SoftbusClientMagSwitch(cmd, rawData);
87 
88         return true;
89     }
90 }
91 
92 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)93 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
94 {
95     if (size < OHOS::THRESHOLD) {
96         return 0;
97     }
98 
99     /* Run your code on data */
100     OHOS::DoSomethingInterestingWithMyAPI(data, size);
101     return 0;
102 }