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 int ret = strncpy_s(callerId, callerIdLen, (const char *)data, size >= callerIdLen ? callerIdLen - 1 : size);
72 if (ret != EOK) {
73 return;
74 }
75 };
76
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)77 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
78 {
79 if (data == nullptr || size == 0) {
80 return true;
81 }
82 GenRanDiscInfo(data, size);
83 ShiftLNNGear(TEST_PKG_NAME1, callerId, networkId, &g_mode);
84 if (callerId != nullptr) {
85 free(callerId);
86 callerId = nullptr;
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 /* Run your code on data */
96 SetAceessTokenPermission("shiftLnnGearFuzzTest");
97 OHOS::DoSomethingInterestingWithMyAPI(data, size);
98 return 0;
99 }
100