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 "shiftlnngear_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstring>
20 #include <securec.h>
21 
22 #include "softbus_access_token_test.h"
23 #include "softbus_bus_center.h"
24 #include "softbus_errcode.h"
25 
26 namespace OHOS {
27     static const int32_t MAX_SIZE_WAKEUP_MODE = 2;
28     static const int32_t MAX_SIZE_GEARMODE_MODE = 3;
29     static const int32_t GEARMODE_MODE_TYPE_0 = 0;
30     static const int32_t GEARMODE_MODE_TYPE_1 = 1;
31     static const int32_t GEARMODE_MODE_TYPE_2 = 2;
32     static char *callerId = nullptr;
33     static constexpr char *networkId = nullptr;
34     static constexpr char TEST_PKG_NAME1[] = "com.softbus.test";
35     static GearMode g_mode;
36 
GenRanDiscInfo(const uint8_t* data, size_t size)37     static void GenRanDiscInfo(const uint8_t* data, size_t size)
38     {
39         switch (size % MAX_SIZE_GEARMODE_MODE) {
40             case GEARMODE_MODE_TYPE_0:
41                 g_mode.cycle = HIGH_FREQ_CYCLE;
42                 break;
43             case GEARMODE_MODE_TYPE_1:
44                 g_mode.cycle = MID_FREQ_CYCLE;
45                 break;
46             case GEARMODE_MODE_TYPE_2:
47                 g_mode.cycle = LOW_FREQ_CYCLE;
48                 break;
49             default:
50                 break;
51         }
52         switch (size % MAX_SIZE_GEARMODE_MODE) {
53             case GEARMODE_MODE_TYPE_0:
54                 g_mode.duration = DEFAULT_DURATION;
55                 break;
56             case GEARMODE_MODE_TYPE_1:
57                 g_mode.duration = NORMAL_DURATION;
58                 break;
59             case GEARMODE_MODE_TYPE_2:
60                 g_mode.duration = LONG_DURATION;
61                 break;
62             default:
63                 break;
64         }
65         g_mode.wakeupFlag = (size % MAX_SIZE_WAKEUP_MODE) ? true : false;
66         size_t callerIdLen = size % CALLER_ID_MAX_LEN + 1;
67         callerId = (char *)malloc(callerIdLen);
68         if (callerId == nullptr) {
69             return;
70         }
71         int32_t ret = strncpy_s(callerId,
72                                 callerIdLen, (const char *)data, size >= callerIdLen ? callerIdLen - 1 : size);
73         if (ret != EOK) {
74             return;
75         }
76     };
77 
DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)78     bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
79     {
80         if (data == nullptr || size == 0) {
81             return true;
82         }
83         GenRanDiscInfo(data, size);
84         ShiftLNNGear(TEST_PKG_NAME1, callerId, networkId, &g_mode);
85         if (callerId != nullptr) {
86             free(callerId);
87             callerId = nullptr;
88         }
89         return true;
90     }
91 }
92 
93 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)94 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
95 {
96     /* Run your code on data */
97     SetAceessTokenPermission("shiftLnnGearFuzzTest");
98     OHOS::DoSomethingInterestingWithMyAPI(data, size);
99     return 0;
100 }
101