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