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