1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <securec.h>
33 #include <time.h>
34 #include "ctype.h"
35 #include "stdlib.h"
36 #include "string.h"
37 #include "socket.h"
38 #include "in.h"
39 #include "los_task.h"
40 #include "ohos_types.h"
41 #include "los_config.h"
42 #include "Public.h"
43 #include "PCommon.h"
44 #include "fuzz_posix.h"
45
46 #ifndef TASK_PRIO_TEST
47 #define TASK_PRIO_TEST 2
48 #endif
49
50 #ifndef TASK_STACK_SIZE_TEST
51 #define TASK_STACK_SIZE_TEST 0x400
52 #endif
53 #define LOS_TASK_STATUS_DETACHED 0x0100
54
55 extern S_ElementInit g_element[ELEMENT_LEN];
56 extern int g_iteration;
57
58 static UINT32 g_testTaskID01;
59
60 #define BUF_SIZE 40
61 static char g_udpIp[16];
62 static UINT16 g_udpPort;
63 #define UDPMSG "ABCDEEFG"
64 #define SLEEP_SECONDS 2
65
SampleUdpServer()66 static void *SampleUdpServer()
67 {
68 int fd;
69 struct sockaddr_in srvAddr = { 0 };
70
71 struct sockaddr_in clnAddr = { 0 };
72 socklen_t clnAddrLen = sizeof(clnAddr);
73 char buf[BUF_SIZE] = { 0 };
74 int flag;
75 struct msghdr msg = { 0 };
76 struct iovec iov[2] = { };
77
78 printf("s0\r\n");
79 fd = socket(AF_INET, SOCK_DGRAM, 0);
80
81 printf("s1\r\n");
82 srvAddr.sin_family = AF_INET;
83 srvAddr.sin_addr.s_addr = inet_addr(g_udpIp);
84 srvAddr.sin_port = htons(g_udpPort);
85 int ret = bind(fd, (struct sockaddr *)&srvAddr, sizeof(srvAddr));
86
87 printf("s1.1, ret = %d\r\n", ret);
88 (void)recvfrom(fd, buf, sizeof(buf), 0x40, (struct sockaddr *)&clnAddr, &clnAddrLen);
89
90 printf("s2, buf = %s\r\n", buf);
91 usleep(SLEEP_SECONDS);
92 msg.msg_name = &clnAddr;
93 msg.msg_namelen = sizeof(clnAddr);
94 msg.msg_iov = iov;
95 msg.msg_iovlen = 1;
96 iov[0].iov_base = buf;
97 iov[0].iov_len = strlen(buf);
98 (void)sendmsg(fd, &msg, 0);
99
100 printf("s3\r\n");
101 close(fd);
102 return NULL;
103 }
104
SampleUdpClient()105 static void *SampleUdpClient()
106 {
107 int fd = 0;
108 const int sleepSec = 5;
109 struct sockaddr_in clnAddr = { 0 };
110
111 char buf[BUF_SIZE] = { 0 };
112 int flag;
113
114 struct msghdr msg;
115 struct iovec iov[2];
116
117 printf("c1\r\n");
118 fd = socket(AF_INET, SOCK_DGRAM, 0);
119
120 printf("c2\r\n");
121 clnAddr.sin_family = AF_INET;
122 clnAddr.sin_addr.s_addr = inet_addr(g_udpIp);
123 clnAddr.sin_port = htons(g_udpPort);
124
125 printf("c3\r\n");
126 (void)strcpy_s(buf, sizeof(buf), UDPMSG);
127
128 usleep(sleepSec);
129 printf("c4, %s, %d\r\n", buf, strlen(buf));
130 (void)sendto(fd, buf, strlen(buf), 0, (struct sockaddr *)&clnAddr, (socklen_t)sizeof(clnAddr));
131
132 msg.msg_name = &clnAddr;
133 msg.msg_namelen = sizeof(clnAddr);
134 msg.msg_iov = iov;
135 msg.msg_iovlen = 1;
136 iov[0].iov_base = buf;
137 iov[0].iov_len = sizeof(buf);
138 (void)recvmsg(fd, &msg, 0x40);
139 printf("c6\r\n");
140
141 close(fd);
142 return NULL;
143 }
144
UdpFun(VOID)145 static UINT32 UdpFun(VOID)
146 {
147 int i;
148
149 UINT8 num1;
150 UINT8 num2;
151 UINT8 num3;
152 UINT8 num4;
153 UINT8 num5;
154
155 INIT_FuzzEnvironment();
156 printf("UDPFun starts\n");
157
158 for (i = 0; i < g_iteration; i++) {
159 num1 = *(UINT8 *)DT_SetGetU8(&g_element[NUM_0_INDEX], 1);
160 num2 = *(UINT8 *)DT_SetGetU8(&g_element[NUM_1_INDEX], 1);
161 num3 = *(UINT8 *)DT_SetGetU8(&g_element[NUM_2_INDEX], 1);
162 num4 = *(UINT8 *)DT_SetGetU8(&g_element[NUM_3_INDEX], 1);
163 num5 = *(UINT8 *)DT_SetGetU8(&g_element[NUM_4_INDEX], 1);
164
165 (void)snprintf_s(g_udpIp, sizeof(g_udpIp), sizeof(g_udpIp) - 1, "%d.%d.%d.%d", num1, num2, num3, num4);
166 g_udpPort = *(UINT16 *)DT_SetGetU16(&g_element[NUM_5_INDEX], 0);
167
168 {
169 TSK_INIT_PARAM_S stTask1 = { 0 };
170 stTask1.pfnTaskEntry = (TSK_ENTRY_FUNC)SampleUdpServer;
171 stTask1.uwStackSize = TASK_STACK_SIZE_TEST;
172 stTask1.pcName = "Tsk001A";
173 stTask1.usTaskPrio = TASK_PRIO_TEST;
174 stTask1.uwResved = LOS_TASK_STATUS_DETACHED;
175 (void)LOS_TaskCreate(&g_testTaskID01, &stTask1);
176 }
177
178 {
179 TSK_INIT_PARAM_S stTask1 = { 0 };
180 stTask1.pfnTaskEntry = (TSK_ENTRY_FUNC)SampleUdpClient;
181 stTask1.uwStackSize = TASK_STACK_SIZE_TEST;
182 stTask1.pcName = "Tsk001A";
183 stTask1.usTaskPrio = TASK_PRIO_TEST;
184 stTask1.uwResved = LOS_TASK_STATUS_DETACHED;
185 (void)LOS_TaskCreate(&g_testTaskID01, &stTask1);
186 }
187 }
188
189 DT_Clear(g_element);
190 CLOSE_Log();
191 CLEAR_FuzzEnvironment();
192 printf("UDPFun end ....\n");
193
194 return 0;
195 }
196
197 #define LOCAL_HOST "127.0.0.1"
198 #define STACK_IP LOCAL_HOST
199 #define STACK_PORT 2277
200 #define PEER_PORT STACK_PORT
201 #define PEER_IP LOCAL_HOST
202 #define SRV_MSG "Hi, I am TCP server"
203 #define CLI_MSG "Hi, I am TCP client"
204
SampleTcpServer()205 static void *SampleTcpServer()
206 {
207 char buf[BUF_SIZE + 1] = { 0 };
208 int sfd;
209 int lsfd;
210 struct sockaddr_in srvAddr = { 0 };
211 struct sockaddr_in clnAddr = { 0 };
212 int ret;
213
214 lsfd = socket(AF_INET, SOCK_STREAM, 0);
215
216 srvAddr.sin_family = AF_INET;
217 srvAddr.sin_addr.s_addr = inet_addr(STACK_IP);
218 srvAddr.sin_port = htons(STACK_PORT);
219 (void)bind(lsfd, (struct sockaddr *)&srvAddr, sizeof(srvAddr));
220
221 int num1 = *(int *)DT_SetGetS32(&g_element[NUM_0_INDEX], 0);
222
223 (void)listen(lsfd, num1);
224
225 int num2 = *(int *)DT_SetGetS32(&g_element[NUM_1_INDEX], 0);
226 sfd = accept(lsfd, (struct sockaddr *)&clnAddr, &num2);
227
228 int num3 = *(int *)DT_SetGetS32(&g_element[NUM_2_INDEX], 0);
229 (void)recv(lsfd, buf, sizeof(buf), num3);
230
231 int num4 = *(int *)DT_SetGetS32(&g_element[NUM_3_INDEX], 0);
232 ret = shutdown(sfd, num4);
233 if (ret != 0) {
234 shutdown(sfd, SHUT_RDWR);
235 }
236
237 close(sfd);
238 close(lsfd);
239 return (void *)(intptr_t)ret;
240 }
241
SampleTcpClient()242 static void *SampleTcpClient()
243 {
244 char buf[BUF_SIZE + 1] = { 0 };
245 int sfd = -1;
246 struct sockaddr_in srvAddr = { 0 };
247 struct sockaddr_in clnAddr = { 0 };
248 int ret;
249
250 struct sockaddr addr;
251 socklen_t addrLen = sizeof(addr);
252 /* tcp client connection */
253 sfd = socket(AF_INET, SOCK_STREAM, 0);
254 srvAddr.sin_family = AF_INET;
255 srvAddr.sin_addr.s_addr = inet_addr(PEER_IP);
256 srvAddr.sin_port = htons(PEER_PORT);
257 connect(sfd, (struct sockaddr *)&srvAddr, sizeof(srvAddr));
258
259 int num1 = *(int *)DT_SetGetS32(&g_element[0], 0);
260 (void)getpeername(num1, &addr, &addrLen);
261 (void)getsockname(num1, &addr, &addrLen);
262
263 /* send */
264 ret = memset_s(buf, sizeof(buf), 0, BUF_SIZE);
265 if (err != EOK) {
266 close(sfd);
267 return (void *)(intptr_t)ret;
268 }
269 (void)strcpy_s(buf, sizeof(buf), CLI_MSG);
270
271 int num2 = *(int *)DT_SetGetS32(&g_element[1], 0);
272 send(num1, buf, strlen(CLI_MSG), num2);
273
274 ret = shutdown(sfd, SHUT_RDWR);
275
276 close(sfd);
277 return (void *)(intptr_t)ret;
278 }
279
TcpFun(VOID)280 static UINT32 TcpFun(VOID)
281 {
282 int i;
283
284 INIT_FuzzEnvironment();
285 printf("TcpFun starts\n");
286
287 for (i = 0; i < g_iteration; i++) {
288 {
289 TSK_INIT_PARAM_S stTask1 = { 0 };
290 stTask1.pfnTaskEntry = (TSK_ENTRY_FUNC)SampleTcpServer;
291 stTask1.uwStackSize = TASK_STACK_SIZE_TEST;
292 stTask1.pcName = "Tsk001A";
293 stTask1.usTaskPrio = TASK_PRIO_TEST;
294 stTask1.uwResved = LOS_TASK_STATUS_DETACHED;
295 (void)LOS_TaskCreate(&g_testTaskID01, &stTask1);
296 }
297
298 {
299 TSK_INIT_PARAM_S stTask1 = { 0 };
300 stTask1.pfnTaskEntry = (TSK_ENTRY_FUNC)SampleTcpClient;
301 stTask1.uwStackSize = TASK_STACK_SIZE_TEST;
302 stTask1.pcName = "Tsk001A";
303 stTask1.usTaskPrio = TASK_PRIO_TEST;
304 stTask1.uwResved = LOS_TASK_STATUS_DETACHED;
305 (void)LOS_TaskCreate(&g_testTaskID01, &stTask1);
306 }
307 }
308
309 DT_Clear(g_element);
310 CLOSE_Log();
311 CLEAR_FuzzEnvironment();
312 printf("TcpFun end ....\n");
313
314 return 0;
315 }
316
NetFuzzTest(void)317 void NetFuzzTest(void)
318 {
319 DnCompFun();
320
321 UdpFun();
322 TcpFun();
323
324 InetFun();
325
326 ProtoentFun();
327 ConvertFun();
328
329 SocketFun();
330 }
331