1 /*
2 * Copyright (c) 2021-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 "clienttranssessionmanager_fuzzer.h"
17 #include <cstddef>
18 #include <cstdint>
19 #include "session.h"
20 #include "softbus_utils.h"
21 #include "client_trans_session_manager.h"
22 #include "softbus_trans_def.h"
23
24 namespace OHOS {
OnSessionOpened(int sessionId,int result)25 static int OnSessionOpened(int sessionId, int result)
26 {
27 return 0;
28 }
OnSessionClosed(int sessionId)29 static void OnSessionClosed(int sessionId) {}
30
OnBytesReceived(int sessionId,const void * data,unsigned int len)31 static void OnBytesReceived(int sessionId, const void* data, unsigned int len) {}
32
OnMessageReceived(int sessionId,const void * data,unsigned int len)33 static void OnMessageReceived(int sessionId, const void* data, unsigned int len) {}
34
35 static ISessionListener g_sessionlistener = {
36 .OnSessionOpened = OnSessionOpened,
37 .OnSessionClosed = OnSessionClosed,
38 .OnBytesReceived = OnBytesReceived,
39 .OnMessageReceived = OnMessageReceived,
40 };
41
ClientAddNewSessionTest(const uint8_t * data,size_t size)42 void ClientAddNewSessionTest(const uint8_t* data, size_t size)
43 {
44 if ((data == nullptr) || (size == 0)) {
45 return;
46 }
47 const char* testSessionName = reinterpret_cast<const char*>(data);
48 SessionInfo session;
49 ClientAddNewSession(testSessionName, &session);
50 }
51
ClientAddAuthSessionTest(const uint8_t * data,size_t size)52 void ClientAddAuthSessionTest(const uint8_t* data, size_t size)
53 {
54 #define SESSION_NAME_SIZE_MAX 256
55 if ((data == nullptr) || (size < SESSION_NAME_SIZE_MAX)) {
56 return;
57 }
58 int32_t sessionId;
59 ClientAddAuthSession(nullptr, &sessionId);
60 }
61
ClientDeleteSessionTest(const uint8_t * data,size_t size)62 void ClientDeleteSessionTest(const uint8_t* data, size_t size)
63 {
64 if ((data == nullptr) || (size < sizeof(int32_t))) {
65 return;
66 }
67 int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
68 ClientDeleteSession(sessionId);
69 }
70
ClientGetSessionDataTest(const uint8_t * data,size_t size)71 void ClientGetSessionDataTest(const uint8_t* data, size_t size)
72 {
73 if ((data == nullptr) || (size < sizeof(int32_t))) {
74 return;
75 }
76 int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
77 char* testData = const_cast<char*>(reinterpret_cast<const char*>(data));
78 int* testInt = const_cast<int*>(reinterpret_cast<const int*>(data));
79 ClientGetSessionDataById(sessionId, testData, size, KEY_SESSION_NAME);
80 ClientGetSessionIntegerDataById(sessionId, testInt, KEY_SESSION_NAME);
81 ClientGetSessionSide(sessionId);
82 }
83
ClientSetChannelBySessionIdTest(const uint8_t * data,size_t size)84 void ClientSetChannelBySessionIdTest(const uint8_t* data, size_t size)
85 {
86 if ((data == nullptr) || (size < sizeof(int32_t))) {
87 return;
88 }
89 int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
90 TransInfo transInfo = {
91 .channelId = 0,
92 .channelType = 0,
93 };
94
95 ClientSetChannelBySessionId(sessionId, &transInfo);
96 }
97
ClientGetSessionCallbackTest(const uint8_t * data,size_t size)98 void ClientGetSessionCallbackTest(const uint8_t* data, size_t size)
99 {
100 if ((data == nullptr) || (size < sizeof(int32_t))) {
101 return;
102 }
103 int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
104 const char* testSessionName = reinterpret_cast<const char*>(data);
105
106 ClientGetSessionCallbackById(sessionId, &g_sessionlistener);
107 ClientGetSessionCallbackByName(testSessionName, &g_sessionlistener);
108 }
109
ClientTransOnLinkDownTest(const uint8_t * data,size_t size)110 void ClientTransOnLinkDownTest(const uint8_t* data, size_t size)
111 {
112 if ((data == nullptr) || (size < sizeof(int32_t))) {
113 return;
114 }
115 const char* netWorkId = reinterpret_cast<const char*>(data);
116 int32_t routeType = *(reinterpret_cast<const int32_t*>(data));
117
118 ClientTransOnLinkDown(netWorkId, routeType);
119 }
120
ClientRemovePermissionTest(const uint8_t * data,size_t size)121 void ClientRemovePermissionTest(const uint8_t* data, size_t size)
122 {
123 if ((data == nullptr) || (size == 0)) {
124 return;
125 }
126 ClientRemovePermission(nullptr);
127 }
128
ClientGetFileConfigInfoByIdTest(const uint8_t * data,size_t size)129 void ClientGetFileConfigInfoByIdTest(const uint8_t* data, size_t size)
130 {
131 if ((data == nullptr) || (size < sizeof(int32_t))) {
132 return;
133 }
134 int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
135 int32_t* fileEncrypt = const_cast<int32_t*>(reinterpret_cast<const int32_t*>(data));
136 ClientGetFileConfigInfoById(sessionId, fileEncrypt, fileEncrypt, fileEncrypt);
137 }
138
GetEncryptByChannelIdTest(const uint8_t * data,size_t size)139 void GetEncryptByChannelIdTest(const uint8_t* data, size_t size)
140 {
141 if ((data == nullptr) || (size < sizeof(int32_t))) {
142 return;
143 }
144 int32_t channelId = *(reinterpret_cast<const int32_t*>(data));
145 int32_t channelType = *(reinterpret_cast<const int32_t*>(data));
146 int32_t encryp = 0;
147 GetEncryptByChannelId(channelId, channelType, &encryp);
148 }
149
ClientGetSessionIdByChannelIdTest(const uint8_t * data,size_t size)150 void ClientGetSessionIdByChannelIdTest(const uint8_t* data, size_t size)
151 {
152 if ((data == nullptr) || (size < sizeof(int32_t))) {
153 return;
154 }
155 int32_t channelId = *(reinterpret_cast<const int32_t*>(data));
156 int32_t channelType = *(reinterpret_cast<const int32_t*>(data));
157 int32_t sessionId;
158 ClientGetSessionIdByChannelId(channelId, channelType, &sessionId);
159 }
160
ClientEnableSessionByChannelIdTest(const uint8_t * data,size_t size)161 void ClientEnableSessionByChannelIdTest(const uint8_t* data, size_t size)
162 {
163 if ((data == nullptr) || (size < sizeof(int32_t))) {
164 return;
165 }
166 ChannelInfo channel;
167 int32_t sessionId;
168 ClientEnableSessionByChannelId(&channel, &sessionId);
169 }
170
171 } // namespace OHOS
172
173 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)174 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
175 {
176 OHOS::ClientAddNewSessionTest(data, size);
177 OHOS::ClientAddAuthSessionTest(data, size);
178 OHOS::ClientDeleteSessionTest(data, size);
179 OHOS::ClientGetSessionDataTest(data, size);
180 OHOS::ClientSetChannelBySessionIdTest(data, size);
181 OHOS::ClientGetSessionCallbackTest(data, size);
182 OHOS::ClientTransOnLinkDownTest(data, size);
183 OHOS::ClientRemovePermissionTest(data, size);
184 OHOS::ClientGetFileConfigInfoByIdTest(data, size);
185 OHOS::GetEncryptByChannelIdTest(data, size);
186 OHOS::ClientGetSessionIdByChannelIdTest(data, size);
187 OHOS::ClientEnableSessionByChannelIdTest(data, size);
188 return 0;
189 }
190