1 /**
2 * Copyright (c) 2021 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 "securec.h"
17 #include "gtest/gtest.h"
18 #include "log.h"
19 #include "utils.h"
20 #include <errno.h>
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
23 #include <bits/ioctl.h>
24 #include <arpa/inet.h>
25 #include <netinet/tcp.h>
26 #include <netinet/in.h>
27 #include <netinet/ether.h>
28 #include <net/if.h>
29 #include <netdb.h>
30 #include <resolv.h>
31 #include <arpa/nameser.h>
32 #include <poll.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36
37 using namespace std;
38 using namespace testing::ext;
39
40 #define PROTOCOL_COUNT 36
41 #define TEST_FD_COUNT 10
42 #define STACK_PORT 2288
43 #define PEER_PORT 2288
44 #define BUF_SIZE (100)
45 static const char* g_udpMsg = "Hi, this is UDP";
46 static const char* g_srvMsg = "Hi, this is TCP server";
47 static const char* g_cliMsg = "Hello, this is TCP client";
48 static const char* g_localHost = "127.0.0.1";
49 static char g_protocolList[PROTOCOL_COUNT][11] = {
50 "ip", "icmp", "igmp", "ggp", "ipencap", "st", "tcp",
51 "egp", "pup", "udp", "hmp", "xns-idp", "rdp", "iso-tp4", "xtp", "ddp", "idpr-cmtp", "ipv6",
52 "ipv6-route", "ipv6-frag", "idrp", "rsvp", "gre", "esp", "ah", "skip", "ipv6-icmp", "ipv6-nonxt",
53 "ipv6-opts", "rspf", "vmtp", "ospf", "ipip", "encap", "pim", "raw"
54 };
55 static int g_protocolNumber[PROTOCOL_COUNT] = {
56 0, 1, 2, 3, 4, 5, 6, 8, 12, 17, 20, 22, 27, 29, 36, 37,
57 38, 41, 43, 44, 45, 46, 47, 50, 51, 57, 58, 59, 60, 73, 81, 89, 94, 98, 103, 255
58 };
59
60 static pthread_barrier_t g_barrier;
61 #define WAIT() pthread_barrier_wait(&g_barrier)
62
63 class ActsNetTest : public testing::Test {
64 protected:
65 // SetUpTestCase: Testsuit setup, run before 1st testcase
SetUpTestCase(void)66 static void SetUpTestCase(void) {}
67 // TearDownTestCase: Testsuit teardown, run after last testcase
TearDownTestCase(void)68 static void TearDownTestCase(void) {}
69 // Testcase setup
SetUp()70 virtual void SetUp() {}
71 // Testcase teardown
TearDown()72 virtual void TearDown() {}
73 };
74
75 /* return fd */
CommInitTcpServer(int port)76 static int CommInitTcpServer(int port)
77 {
78 int srvFd = socket(AF_INET, SOCK_STREAM, 0);
79 if (srvFd == -1) {
80 printf("[###]CommInitTcpServer]socket fail!\n");
81 return -1;
82 }
83 int flag = 1;
84 int ret = setsockopt(srvFd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(int));
85 if (ret != 0) {
86 printf("[###][CommInitTcpServer]setsockopt fail, ret[%d]!\n", ret);
87 }
88 struct sockaddr_in srvAddr = {0};
89 srvAddr.sin_family = AF_INET;
90 srvAddr.sin_addr.s_addr = inet_addr(g_localHost);
91 srvAddr.sin_port = htons(port);
92 ret = ::bind(srvFd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
93 if (ret != 0) {
94 printf("[###][CommInitTcpServer]::bind fail!\n");
95 close(srvFd);
96 return -1;
97 }
98 ret = listen(srvFd, 5);
99 if (ret != 0) {
100 printf("[###][CommInitTcpServer]listen fail!\n");
101 close(srvFd);
102 return -1;
103 }
104 printf("[###][CommInitTcpServer]success,Fd[%d]\n", srvFd);
105 return srvFd;
106 }
107
CommTcpClientTask(void * param)108 static void* CommTcpClientTask(void *param)
109 {
110 int* ret = (int *)malloc(sizeof(int));
111 int memRet = -1;
112 if (ret == nullptr) {
113 printf("[---][comm client]malloc fail\n");
114 return nullptr;
115 }
116 *ret = 0;
117 int clnFd = socket(AF_INET, SOCK_STREAM, 0);
118 if (clnFd == -1) {
119 printf("[@@@][comm client]socket fail\n");
120 *ret = -1;
121 return ret;
122 }
123 struct sockaddr_in srvAddr = {0};
124 srvAddr.sin_family = AF_INET;
125 srvAddr.sin_addr.s_addr = inet_addr(g_localHost);
126 srvAddr.sin_port = htons(PEER_PORT + 1);
127 int rst = connect(clnFd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
128 if (rst != 0) {
129 printf("[@@@][comm client]connect fail\n");
130 close(clnFd);
131 *ret = -1;
132 return ret;
133 }
134 int sleepSecond = *((int *)param);
135 char dataBuf[50] = {0};
136 char sendMsgList[5][50] = {
137 "Hi, I'm client,FD:[%d]", "client:123456789abcdefg,FD:[%d]",
138 "client:!!@@##$$%%^^&&**(()),FD:[%d]", "client:(((112233445566778899))),FD:[%d]", "bye"
139 };
140 for (int i = 0; i < 4; i++) {
141 sleep(sleepSecond);
142 memRet = memset_s(dataBuf, sizeof(dataBuf), 0, sizeof(dataBuf));
143 EXPECT_EQ(0, memRet);
144 rst = sprintf_s(dataBuf, sizeof(dataBuf), sendMsgList[i], clnFd);
145 if (rst < 0) {
146 printf("[@@@][comm client]sprintf_s error!\n");
147 continue;
148 }
149 rst = send(clnFd, dataBuf, strlen(dataBuf), 0);
150 if (rst <= 0) {
151 *ret = -1;
152 printf("[@@@][comm client][%d]send fail\n", clnFd);
153 }
154 memRet = memset_s(dataBuf, sizeof(dataBuf), 0, sizeof(dataBuf));
155 EXPECT_EQ(0, memRet);
156 rst = recv(clnFd, dataBuf, sizeof(dataBuf), 0);
157 if (rst > 0) {
158 printf("[@@@][comm client][%d]recv msg[%s]\n", clnFd, dataBuf);
159 } else {
160 printf("[@@@][comm client][%d]recv no data\n", clnFd);
161 }
162 }
163 // send 'bye', finish
164 rst = send(clnFd, sendMsgList[4], strlen(sendMsgList[4]), 0);
165 if (rst <= 0) {
166 *ret = -1;
167 printf("[@@@][comm client][%d]send fail\n", clnFd);
168 }
169 rst = close(clnFd);
170 if (rst != 0) {
171 *ret = -1;
172 printf("[@@@][comm client][%d]close fd fail\n", clnFd);
173 }
174 return ret;
175 }
176
SampleTcpServerTask(void * p)177 static void* SampleTcpServerTask(void *p)
178 {
179 int memRet = -1;
180 int srvFd = CommInitTcpServer(STACK_PORT);
181 EXPECT_NE(-1, srvFd);
182
183 WAIT();
184 struct sockaddr_in clnAddr = {0};
185 socklen_t clnAddrLen = sizeof(clnAddr);
186 int clnFd = accept(srvFd, (struct sockaddr*)&clnAddr, &clnAddrLen);
187 printf("[tcp server]accept <%s:%d>\n", inet_ntoa(clnAddr.sin_addr), ntohs(clnAddr.sin_port));
188 EXPECT_NE(-1, clnFd);
189 printf("[tcp server]lsfd[%d], sfd[%d]\n", srvFd, clnFd);
190
191 struct sockaddr addr = {0};
192 socklen_t addrLen = sizeof(addr);
193 int ret = getsockname(clnFd, &addr, &addrLen);
194 printf("[tcp server]getsockname %s:%d\n", inet_ntoa(((struct sockaddr_in*)&addr)->sin_addr),
195 ntohs(((struct sockaddr_in*)&addr)->sin_port));
196 EXPECT_EQ(0, ret);
197 EXPECT_EQ(inet_addr(g_localHost), ((struct sockaddr_in*)&addr)->sin_addr.s_addr);
198
199 ret = getpeername(clnFd, &addr, &addrLen);
200 printf("[tcp server]getpeername %s:%d\n", inet_ntoa(((struct sockaddr_in*)&addr)->sin_addr),
201 ntohs(((struct sockaddr_in*)&addr)->sin_port));
202 EXPECT_EQ(0, ret);
203 EXPECT_EQ(inet_addr(g_localHost), ((struct sockaddr_in*)&addr)->sin_addr.s_addr);
204
205 static char buf[BUF_SIZE + 1] = {0};
206 unsigned int srvMsgLen = strlen(g_srvMsg);
207 memRet = memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
208 EXPECT_EQ(0, memRet);
209 ret = strcpy_s(buf, BUF_SIZE, g_srvMsg);
210 EXPECT_EQ(0, ret);
211 ret = send(clnFd, buf, srvMsgLen, 0);
212 printf("[tcp server]send, ret=%d\n", ret);
213 EXPECT_EQ(srvMsgLen, (unsigned int)ret);
214
215 memRet =memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
216 EXPECT_EQ(0, memRet);
217 ret = recv(clnFd, buf, sizeof(buf), 0);
218 printf("[tcp server]recv, ret=%d, msg[%s]\n", ret, buf);
219 EXPECT_EQ(strlen(g_cliMsg), (unsigned int)ret);
220
221 WAIT();
222 int len = 2;
223 struct msghdr msg = {nullptr};
224 struct iovec iov[2] = {nullptr};
225 clnAddr.sin_family = AF_INET;
226 clnAddr.sin_addr.s_addr = inet_addr(g_localHost);
227 clnAddr.sin_port = htons(PEER_PORT);
228 memRet = memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
229 EXPECT_EQ(0, memRet);
230 ret = strcpy_s(buf, BUF_SIZE, g_srvMsg);
231 EXPECT_EQ(0, ret);
232 msg.msg_name = &clnAddr;
233 msg.msg_namelen = sizeof(clnAddr);
234 msg.msg_iov = iov;
235 msg.msg_iovlen = len;
236 iov[0].iov_base = buf;
237 iov[0].iov_len = srvMsgLen;
238 iov[1].iov_base = buf;
239 iov[1].iov_len = srvMsgLen;
240 ret = sendmsg(clnFd, &msg, 0);
241 printf("[tcp server]sendmsg, ret=%d\n", ret);
242 EXPECT_EQ(len * srvMsgLen, (unsigned int)ret);
243
244 WAIT();
245 static char bufrec[BUF_SIZE + 1] = {0};
246 memRet = memset_s(bufrec, sizeof(bufrec), 0, BUF_SIZE);
247 EXPECT_EQ(0, memRet);
248 memRet = memset_s(&msg, sizeof(msg), 0, sizeof(msg));
249 EXPECT_EQ(0, memRet);
250 msg.msg_name = &clnAddr;
251 msg.msg_namelen = sizeof(clnAddr);
252 msg.msg_iov = iov;
253 msg.msg_iovlen = 1;
254 iov[0].iov_base = bufrec;
255 iov[0].iov_len = sizeof(bufrec);
256 ret = recvmsg(clnFd, &msg, 0);
257 if (len * strlen(g_cliMsg) == (unsigned int)ret && ret >= 0) {
258 bufrec[ret] = 0;
259 printf("[tcp server]recvmsg on socket %d:%d, msg[%s]\n", clnFd, ret, buf);
260 } else {
261 EXPECT_TRUE(false);
262 printf("[tcp server] recvmsg on socket %d: %d\n", clnFd, ret);
263 }
264
265 ret = shutdown(clnFd, SHUT_RDWR);
266 printf("[tcp server]shutdown\n");
267 EXPECT_EQ(0, ret);
268 ret = close(clnFd);
269 EXPECT_NE(-1, ret);
270 ret = close(srvFd);
271 EXPECT_NE(-1, ret);
272 return nullptr;
273 }
274
SampleTcpClientTask(void * p)275 static void* SampleTcpClientTask(void *p)
276 {
277 int memRet = -1;
278 int clnFd = socket(AF_INET, SOCK_STREAM, 0);
279 printf("[tcp client]create socket\n");
280 EXPECT_NE(-1, clnFd);
281
282 WAIT();
283 struct sockaddr_in srvAddr = {0};
284 srvAddr.sin_family = AF_INET;
285 srvAddr.sin_addr.s_addr = inet_addr(g_localHost);
286 srvAddr.sin_port = htons(PEER_PORT);
287 int ret = connect(clnFd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
288 printf("[tcp client]connect %s:%d, fd[%d]\n", inet_ntoa(srvAddr.sin_addr), ntohs(srvAddr.sin_port), clnFd);
289 EXPECT_EQ(0, ret);
290
291 struct sockaddr addr = {0};
292 socklen_t addrLen = sizeof(addr);
293 ret = getpeername(clnFd, &addr, &addrLen);
294 printf("[tcp client]getpeername %s:%d\n", inet_ntoa(((struct sockaddr_in*)&addr)->sin_addr),
295 ntohs(((struct sockaddr_in*)&addr)->sin_port));
296 EXPECT_EQ(0, ret);
297 EXPECT_EQ(inet_addr(g_localHost), ((struct sockaddr_in*)&addr)->sin_addr.s_addr);
298
299 ret = getsockname(clnFd, &addr, &addrLen);
300 printf("[tcp client]getsockname %s:%d\n", inet_ntoa(((struct sockaddr_in*)&addr)->sin_addr),
301 ntohs(((struct sockaddr_in*)&addr)->sin_port));
302 EXPECT_EQ(0, ret);
303 EXPECT_EQ(inet_addr(g_localHost), ((struct sockaddr_in*)&addr)->sin_addr.s_addr);
304
305 static char buf[BUF_SIZE + 1] = {0};
306 unsigned int cliMsgLen = strlen(g_cliMsg);
307 memRet = memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
308 EXPECT_EQ(0, memRet);
309 ret = strcpy_s(buf, BUF_SIZE, g_cliMsg);
310 EXPECT_EQ(0, ret);
311 ret = send(clnFd, buf, cliMsgLen, 0);
312 printf("[tcp client]send, ret=%d\n", ret);
313 EXPECT_EQ(cliMsgLen, (unsigned int)ret);
314
315 memRet = memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
316 EXPECT_EQ(0, memRet);
317 ret = recv(clnFd, buf, sizeof(buf), 0);
318 printf("[tcp client]recv, ret=%d, msg[%s]\n", ret, buf);
319 EXPECT_EQ(strlen(g_srvMsg), (unsigned int)ret);
320
321 WAIT();
322 int len = 2;
323 struct msghdr msg = {nullptr};
324 struct iovec iov[2] = {nullptr};
325 struct sockaddr_in clnAddr = {0};
326 clnAddr.sin_family = AF_INET;
327 clnAddr.sin_addr.s_addr = inet_addr(g_localHost);
328 clnAddr.sin_port = htons(PEER_PORT);
329 memRet = memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
330 EXPECT_EQ(0, memRet);
331 ret = strcpy_s(buf, BUF_SIZE, g_cliMsg);
332 EXPECT_EQ(0, ret);
333 msg.msg_name = &clnAddr;
334 msg.msg_namelen = sizeof(clnAddr);
335 msg.msg_iov = iov;
336 msg.msg_iovlen = len;
337 iov[0].iov_base = buf;
338 iov[0].iov_len = cliMsgLen;
339 iov[1].iov_base = buf;
340 iov[1].iov_len = cliMsgLen;
341 ret = sendmsg(clnFd, &msg, 0);
342 printf("[tcp client]sendmsg, ret=%d\n", ret);
343 EXPECT_EQ(len * cliMsgLen, (unsigned int)ret);
344
345 WAIT();
346 static char bufrec[BUF_SIZE + 1] = {0};
347 memRet = memset_s(bufrec, sizeof(bufrec), 0, BUF_SIZE);
348 EXPECT_EQ(0, memRet);
349 memRet = memset_s(&msg, sizeof(msg), 0, sizeof(msg));
350 EXPECT_EQ(0, memRet);
351 msg.msg_name = &clnAddr;
352 msg.msg_namelen = sizeof(clnAddr);
353 msg.msg_iov = iov;
354 msg.msg_iovlen = 1;
355 iov[0].iov_base = bufrec;
356 iov[0].iov_len = sizeof(bufrec);
357 ret = recvmsg(clnFd, &msg, 0);
358 if (len * strlen(g_srvMsg) == (unsigned int)ret && ret >= 0) {
359 bufrec[ret] = 0;
360 printf("[tcp client]recvmsg, ret=%d, msg[%s]\n", ret, bufrec);
361 } else {
362 EXPECT_TRUE(false);
363 printf("[tcp clien]recvmsg, ret=%d\n", ret);
364 }
365
366 ret = shutdown(clnFd, SHUT_RDWR);
367 printf("[tcp client]shutdown\n");
368 EXPECT_EQ(0, ret);
369 ret = close(clnFd);
370 EXPECT_NE(-1, ret);
371 return nullptr;
372 }
373
TcpServerLoopTask(void * p)374 static void *TcpServerLoopTask(void *p)
375 {
376 int srvFd = CommInitTcpServer(STACK_PORT);
377 EXPECT_NE(-1, srvFd);
378 if (srvFd == -1)
379 {
380 LOG("CommInitTcpServer errno = %d\n", errno);
381 ADD_FAILURE();
382 }
383 int i = 0;
384 int clientFds[36];
385 struct sockaddr_in clnAddr = {0};
386 socklen_t clnAddrLen = sizeof(clnAddr);
387 while (i < 30)
388 {
389 clientFds[i] = accept(srvFd, (struct sockaddr *)&clnAddr, &clnAddrLen);
390 printf("[***---][tcp server loop]accept <%s:%d>, fd[%d]i[%d]\n", inet_ntoa(clnAddr.sin_addr),
391 ntohs(clnAddr.sin_port), clientFds[i], i);
392 EXPECT_NE(-1, clientFds[i]);
393 i++;
394 }
395
396 i--;
397 int ret;
398 while (i >= 0) {
399 ret = shutdown(clientFds[i], SHUT_RDWR);
400 EXPECT_EQ(0, ret);
401 ret = close(clientFds[i]);
402 EXPECT_NE(-1, ret) << "[***---][tcp server loop] close fd index[" << i << "]";
403 i--;
404 }
405 ret = close(srvFd);
406 EXPECT_NE(-1, ret);
407 return nullptr;
408 }
409
TcpClientOnlyConnectTask(void * p)410 static void* TcpClientOnlyConnectTask(void *p)
411 {
412 int tId = *((int *)p);
413 struct sockaddr addr = {0};
414 socklen_t addrLen = sizeof(addr);
415 struct sockaddr_in srvAddr = {0};
416 srvAddr.sin_family = AF_INET;
417 srvAddr.sin_addr.s_addr = inet_addr(g_localHost);
418 srvAddr.sin_port = htons(PEER_PORT);
419
420 int ret;
421 int index;
422 int clientFds[10];
423 for (index = 0; index < 10; index++) {
424 clientFds[index] = socket(AF_INET, SOCK_STREAM, 0);
425 printf("[------][tcp client[%d]]create socket fd[%d],index[%d]\n", tId, clientFds[index], index);
426 EXPECT_NE(-1, clientFds[index]);
427 ret = connect(clientFds[index], (struct sockaddr*)&srvAddr, sizeof(srvAddr));
428 EXPECT_EQ(0, ret);
429 if (ret != 0) {
430 break;
431 }
432
433 ret = getsockname(clientFds[index], &addr, &addrLen);
434 printf("[------][tcp client[%d]]getsockname %s:%d\n", tId, inet_ntoa(((struct sockaddr_in*)&addr)->sin_addr),
435 ntohs(((struct sockaddr_in*)&addr)->sin_port));
436 EXPECT_EQ(0, ret);
437 EXPECT_EQ(inet_addr(g_localHost), ((struct sockaddr_in*)&addr)->sin_addr.s_addr);
438 ret = getpeername(clientFds[index], &addr, &addrLen);
439 EXPECT_EQ(0, ret);
440 EXPECT_EQ(inet_addr(g_localHost), ((struct sockaddr_in*)&addr)->sin_addr.s_addr);
441 sleep(1);
442 }
443
444 index--;
445 for (int i = index; i >= 0; i--) {
446 ret = shutdown(clientFds[i], SHUT_RDWR);
447 EXPECT_EQ(0, ret);
448 ret = close(clientFds[i]);
449 EXPECT_NE(-1, ret) << "[------][tcp client] close fd index[" << i << "]";
450 }
451 return nullptr;
452 }
453
TcpServerForFork()454 static int TcpServerForFork()
455 {
456 int memRet = -1;
457 int timeout = 10;
458 char dataBuf[50] = {0};
459 int srvFd = CommInitTcpServer(STACK_PORT + 1);
460 if (srvFd == -1) {
461 return -1;
462 }
463 printf("[---][server process]wait client...\n");
464 int ret = -1;
465 struct sockaddr_in clnAddr = {0};
466 socklen_t clnAddrLen = sizeof(clnAddr);
467 int cliFd = accept(srvFd, (struct sockaddr*)&clnAddr, &clnAddrLen);
468 if (cliFd == -1) {
469 printf("[---][server process]accept fail!\n");
470 goto FINALLY;
471 }
472 while (timeout > 0) {
473 memRet = memset_s(dataBuf, sizeof(dataBuf), 0, sizeof(dataBuf));
474 EXPECT_EQ(0, memRet);
475 ret = recv(cliFd, dataBuf, sizeof(dataBuf), 0);
476 if (ret > 0) {
477 printf("[---][server process]recv msg[%s]\n", dataBuf);
478 if (strcmp(dataBuf, "bye") == 0) {
479 printf("[---][server process]break\n");
480 break;
481 }
482 } else {
483 printf("[---][server process]recv no data\n");
484 }
485 memRet = memset_s(dataBuf, sizeof(dataBuf), 0, sizeof(dataBuf));
486 EXPECT_EQ(0, memRet);
487 sprintf_s(dataBuf, sizeof(dataBuf), "server timeout:%d", timeout);
488 ret = send(cliFd, dataBuf, strlen(dataBuf), 0);
489 if (ret > 0) {
490 printf("[---][server process]send success, msg[%s]\n", dataBuf);
491 } else {
492 printf("[---][server process]send fail!\n");
493 }
494 timeout--;
495 }
496 close(cliFd);
497 printf("[---][server process]over\n");
498
499 FINALLY:
500 close(srvFd);
501 return ret;
502 }
503
SelectServerForFork(unsigned int timeoutSec)504 static int SelectServerForFork(unsigned int timeoutSec)
505 {
506 int memRet = -1;
507 int srvFd = CommInitTcpServer(STACK_PORT + 1);
508 if (srvFd == -1) {
509 return -1;
510 }
511 int ret;
512 int cliCount = 0;
513 int maxFd = srvFd;
514 fd_set readSet;
515 char dataBuf[50] = {0};
516 int fds[TEST_FD_COUNT] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
517 fds[0] = srvFd;
518 struct timeval timev = {.tv_sec = timeoutSec, .tv_usec = 0};
519 struct sockaddr_in clnAddr = {0};
520 socklen_t clnAddrLen = sizeof(clnAddr);
521 while (1) {
522 FD_ZERO(&readSet);
523 for (int i = 0; i < TEST_FD_COUNT; i++) {
524 if (fds[i] != -1) {
525 FD_SET(fds[i], &readSet);
526 if (maxFd < fds[i]) {
527 maxFd = fds[i];
528 }
529 printf("[&&&][select process]fd info[%d:%d]\n", i, fds[i]);
530 }
531 }
532 ret = select(maxFd + 1, &readSet, nullptr, nullptr, &timev);
533 if (ret == 0) {
534 printf("[---][select process]select timeout!\n");
535 ret = -2;
536 break;
537 }
538 if (ret == -1) {
539 fprintf(stderr, "[---][select process]select fail[%s]\n", strerror(errno));
540 break;
541 }
542 if (FD_ISSET(srvFd, &readSet)) {
543 int cliFd = accept(srvFd, (struct sockaddr*)&clnAddr, &clnAddrLen);
544 if (cliFd == -1) {
545 printf("[---][select process]accept fail!\n");
546 ret = -1;
547 break;
548 } else {
549 for (int i = 0; i < TEST_FD_COUNT; i++) {
550 if (fds[i] == -1) {
551 fds[i] = cliFd;
552 break;
553 }
554 }
555 cliCount++;
556 printf("[***][select process]accept success, cliCount[%d]\n", cliCount);
557 }
558 continue;
559 }
560 for (int i = 0; i < TEST_FD_COUNT; i++) {
561 if (fds[i] == -1) {
562 continue;
563 }
564 if (FD_ISSET(fds[i], &readSet)) {
565 memRet = memset_s(dataBuf, sizeof(dataBuf), 0, sizeof(dataBuf));
566 EXPECT_EQ(0, memRet);
567 int len = recv(fds[i], dataBuf, sizeof(dataBuf), 0);
568 if (len > 0) {
569 printf("[***][select process][%d]recv msg[%s]\n", fds[i], dataBuf);
570 if (strcmp(dataBuf, "bye") == 0) {
571 printf("[###][select process][%d]client bye,cliCount[%d]\n", fds[i], cliCount);
572 FD_CLR(fds[i], &readSet);
573 close(fds[i]);
574 fds[i] = -1;
575 cliCount--;
576 continue;
577 }
578 len = send(fds[i], dataBuf, strlen(dataBuf), 0);
579 if (len > 0) {
580 printf("[***][select process][%d]send success\n", fds[i]);
581 } else {
582 printf("[---][select process][%d]send fail\n", fds[i]);
583 }
584 }
585 }
586 }
587 if (cliCount == 0) {
588 ret = 0;
589 printf("[###][select process]cliCount=0, over!\n");
590 break;
591 }
592 }
593 close(srvFd);
594 return ret;
595 }
596
PollServerForFork(int timeoutSec)597 static int PollServerForFork(int timeoutSec)
598 {
599 int srvFd = CommInitTcpServer(STACK_PORT + 1);
600 if (srvFd == -1) {
601 return -1;
602 }
603 int ret;
604 int cliCount = 0;
605 int fdCount = 1;
606 char dataBuf[50] = {0};
607 struct pollfd fds[TEST_FD_COUNT];
608 int memRet = -1;
609 fds[0].events = POLLIN | POLLPRI;
610 fds[0].fd = srvFd;
611 for (int i = 1; i < TEST_FD_COUNT; i++) {
612 fds[i].fd = -1;
613 }
614 struct sockaddr_in clnAddr = {0};
615 socklen_t clnAddrLen = sizeof(clnAddr);
616 while (1) {
617 ret = poll(fds, fdCount, timeoutSec);
618 if (ret == 0) {
619 printf("[---][poll process]poll timeout!\n");
620 ret = -2;
621 break;
622 }
623 if (ret == -1) {
624 fprintf(stderr, "[---][poll process]poll fail[%s]\n", strerror(errno));
625 break;
626 }
627 if (fds[0].revents & POLLIN) {
628 int cliFd = accept(srvFd, (struct sockaddr*)&clnAddr, &clnAddrLen);
629 if (cliFd == -1) {
630 printf("[---][poll process]accept fail!\n");
631 ret = -1;
632 break;
633 } else {
634 for (int i = 1; i < TEST_FD_COUNT; i++) {
635 if (fds[i].fd == -1) {
636 fds[i].fd = cliFd;
637 fds[i].events = POLLIN | POLLPRI;
638 break;
639 }
640 }
641 cliCount++;
642 fdCount++;
643 printf("[***][poll process]accept success, cliCount[%d]\n", cliCount);
644 }
645 continue;
646 }
647 for (int i = 0; i < TEST_FD_COUNT; i++) {
648 if (fds[i].fd == -1) {
649 continue;
650 }
651 if (fds[i].revents & POLLIN) {
652 memRet = memset_s(dataBuf, sizeof(dataBuf), 0, sizeof(dataBuf));
653 EXPECT_EQ(0, memRet);
654 int len = recv(fds[i].fd, dataBuf, sizeof(dataBuf), 0);
655 if (len > 0) {
656 printf("[***][poll process][%d]recv msg[%s]\n", fds[i].fd, dataBuf);
657 if (strcmp(dataBuf, "bye") == 0) {
658 printf("[###][select process][%d]client bye,cliCount[%d]\n", fds[i].fd, cliCount);
659 cliCount--;
660 continue;
661 }
662 len = send(fds[i].fd, dataBuf, strlen(dataBuf), 0);
663 if (len > 0) {
664 printf("[***][poll process][%d]send success\n", fds[i].fd);
665 } else {
666 printf("[---][poll process][%d]send fail\n", fds[i].fd);
667 }
668 }
669 }
670 }
671 if (cliCount == 0) {
672 ret = 0;
673 printf("[###][poll process]cliCount=0, over!\n");
674 break;
675 }
676 }
677 for (int i = 0; i < TEST_FD_COUNT; i++) {
678 if (fds[i].fd != -1) {
679 close(fds[i].fd);
680 }
681 }
682 close(srvFd);
683 return ret;
684 }
685
686 /**
687 * @tc.number : SUB_KERNEL_NET_0100
688 * @tc.name : sample test tcp
689 * @tc.desc : [C- SOFTWARE -0200]
690 */
691 HWTEST_F(ActsNetTest, testTcpSample, Function | MediumTest | Level2)
692 {
693 pthread_t srv;
694 pthread_t cli;
695
696 int ret = pthread_barrier_init(&g_barrier, 0, 2);
697 EXPECT_EQ(0, ret);
698
699 ret = pthread_create(&srv, nullptr, SampleTcpServerTask, nullptr);
700 EXPECT_EQ(0, ret);
701 ret = pthread_create(&cli, nullptr, SampleTcpClientTask, nullptr);
702 EXPECT_EQ(0, ret);
703
704 ret = pthread_join(cli, nullptr);
705 EXPECT_EQ(0, ret);
706 ret = pthread_join(srv, nullptr);
707 EXPECT_EQ(0, ret);
708
709 ret = pthread_barrier_destroy(&g_barrier);
710 EXPECT_EQ(0, ret);
711 }
712
713 /**
714 * @tc.number : SUB_KERNEL_NET_0200
715 * @tc.name : test tcp concurrent connect
716 * @tc.desc : [C- SOFTWARE -0200]
717 */
718 HWTEST_F(ActsNetTest, testTcpConcurrentConnect, Function | MediumTest | Level2)
719 {
720 pthread_t srv;
721 int ret = pthread_create(&srv, nullptr, TcpServerLoopTask, nullptr);
722 EXPECT_EQ(0, ret);
723
724 sleep(2);
725 pthread_t cliOne;
726 int tIdOne = 1;
727 ret = pthread_create(&cliOne, nullptr, TcpClientOnlyConnectTask, &tIdOne);
728 EXPECT_EQ(0, ret);
729 pthread_t cliTwo;
730 int tIdTwo = 2;
731 ret = pthread_create(&cliTwo, nullptr, TcpClientOnlyConnectTask, &tIdTwo);
732 EXPECT_EQ(0, ret);
733 pthread_t cliThree;
734 int tIdThree = 3;
735 ret = pthread_create(&cliThree, nullptr, TcpClientOnlyConnectTask, &tIdThree);
736 EXPECT_EQ(0, ret);
737
738 ret = pthread_join(srv, nullptr);
739 EXPECT_EQ(0, ret);
740 ret = pthread_join(cliOne, nullptr);
741 EXPECT_EQ(0, ret);
742 printf("[******]testTcpConnect thread[1] finish!\n");
743 ret = pthread_join(cliTwo, nullptr);
744 EXPECT_EQ(0, ret);
745 printf("[******]testTcpConnect thread[2] finish!\n");
746 ret = pthread_join(cliThree, nullptr);
747 EXPECT_EQ(0, ret);
748 printf("[******]testTcpConnect thread[3] finish!\n");
749 }
750
751 /**
752 * @tc.number : SUB_KERNEL_NET_0300
753 * @tc.name : sample test upd
754 * @tc.desc : [C- SOFTWARE -0200]
755 */
756 HWTEST_F(ActsNetTest, testUdpSample, Function | MediumTest | Level2)
757 {
758 int len = 2;
759 char buf[BUF_SIZE + 1];
760 int memRet = -1;
761 struct msghdr msg = {nullptr};
762 struct iovec iov[2] = {nullptr};
763 struct sockaddr_in srvAddr = {0};
764 struct sockaddr_in clnAddr = {0};
765 socklen_t clnAddrLen = sizeof(clnAddr);
766
767 int sfd = socket(AF_INET, SOCK_DGRAM, 0);
768 ASSERT_NE(-1, sfd);
769 srvAddr.sin_family = AF_INET;
770 srvAddr.sin_addr.s_addr = inet_addr(g_localHost);
771 srvAddr.sin_port = htons(STACK_PORT);
772 int ret = ::bind(sfd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
773 ASSERT_EQ(0, ret);
774
775 clnAddr.sin_family = AF_INET;
776 clnAddr.sin_addr.s_addr = inet_addr(g_localHost);
777 clnAddr.sin_port = htons(PEER_PORT);
778 memRet = memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
779 EXPECT_EQ(0, memRet);
780 ret = strcpy_s(buf, BUF_SIZE, g_udpMsg);
781 EXPECT_EQ(0, ret);
782 ret = sendto(sfd, buf, strlen(g_udpMsg), 0, (struct sockaddr*)&clnAddr, (socklen_t)sizeof(clnAddr));
783 EXPECT_NE(-1, ret);
784
785 memRet = memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
786 EXPECT_EQ(0, memRet);
787 ret = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&clnAddr, &clnAddrLen);
788 EXPECT_EQ(strlen(g_udpMsg), (unsigned int)ret);
789
790 clnAddr.sin_family = AF_INET;
791 clnAddr.sin_addr.s_addr = inet_addr(g_localHost);
792 clnAddr.sin_port = htons(PEER_PORT);
793 memRet = memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
794 EXPECT_EQ(0, memRet);
795 ret = strcpy_s(buf, BUF_SIZE, g_udpMsg);
796 EXPECT_EQ(0, ret);
797 msg.msg_name = &clnAddr;
798 msg.msg_namelen = sizeof(clnAddr);
799 msg.msg_iov = iov;
800 msg.msg_iovlen = len;
801 iov[0].iov_base = buf;
802 iov[0].iov_len = strlen(g_udpMsg);
803 iov[1].iov_base = buf;
804 iov[1].iov_len = strlen(g_udpMsg);
805 ret = sendmsg(sfd, &msg, 0);
806 EXPECT_EQ(len*strlen(g_udpMsg), (unsigned int)ret);
807
808 ret = close(sfd);
809 EXPECT_NE(-1, ret);
810 }
811
812 /**
813 * @tc.number : SUB_KERNEL_NET_0400
814 * @tc.name : test inter-process network communication
815 * @tc.desc : [C- SOFTWARE -0200]
816 */
817 HWTEST_F(ActsNetTest, testInterProcessComm, Function | MediumTest | Level2)
818 {
819 pid_t pid = fork();
820 ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
821 if (pid == 0) {
822 // child:tcp server
823 int exitCode = 0;
824 int ret = TcpServerForFork();
825 if (ret == -1) {
826 printf("[tcp server]run fail\n");
827 exitCode = 1;
828 }
829 exit(exitCode);
830 } else {
831 // parent:tcp client
832 sleep(2);
833 int second = 1;
834 void *ret = CommTcpClientTask(&second);
835 if (ret == nullptr) {
836 printf("[test select]CommClientTask ret is null\n");
837 EXPECT_TRUE(0);
838 } else {
839 printf("[test select]CommClientTask ret...\n");
840 int rst = *((int *)ret);
841 printf("[test select]CommClientTask ret [%d]\n", rst);
842 EXPECT_EQ(0, rst);
843 free(ret);
844 }
845 WaitProcExitedOK(pid);
846 }
847 }
848
849 /**
850 * @tc.number : SUB_KERNEL_NET_0500
851 * @tc.name : test select timeout
852 * @tc.desc : [C- SOFTWARE -0200]
853 */
854 HWTEST_F(ActsNetTest, testSelectTimeout, Function | MediumTest | Level2)
855 {
856 pid_t pid = fork();
857 ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
858 if (pid == 0) {
859 // child: server
860 int ret = SelectServerForFork(2);
861 if (ret == -2) {
862 exit(0);
863 } else {
864 printf("[select server]fail, ret is[%d]\n", ret);
865 exit(1);
866 }
867 } else {
868 WaitProcExitedOK(pid);
869 }
870 }
871
872 /**
873 * @tc.number : SUB_KERNEL_NET_0510
874 * @tc.name : test select with one client
875 * @tc.desc : [C- SOFTWARE -0200]
876 */
877 HWTEST_F(ActsNetTest, testSelectOneClient, Function | MediumTest | Level2)
878 {
879 pid_t pid = fork();
880 ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
881 if (pid == 0) {
882 // child: server
883 int exitCode = 0;
884 int ret = SelectServerForFork(5);
885 if (ret != 0) {
886 printf("[###][select server]run fail\n");
887 exitCode = 1;
888 }
889 printf("[###][select server]ret[%d]\n", ret);
890 exit(exitCode);
891 } else {
892 // parent: client
893 sleep(2);
894 int second = 1;
895 void* ret = CommTcpClientTask(&second);
896 if (ret == nullptr) {
897 printf("[###][test select]CommClientTask ret is null\n");
898 EXPECT_TRUE(0);
899 } else {
900 int rst = *((int *)ret);
901 printf("[###][test select]CommClientTask ret [%d]\n", rst);
902 EXPECT_EQ(0, rst);
903 free(ret);
904 }
905 WaitProcExitedOK(pid);
906 }
907 }
908
909 /**
910 * @tc.number : SUB_KERNEL_NET_0520
911 * @tc.name : test select with multi clients
912 * @tc.desc : [C- SOFTWARE -0200]
913 */
914 HWTEST_F(ActsNetTest, testSelectMultiClients, Function | MediumTest | Level2)
915 {
916 pid_t pid = fork();
917 ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
918 if (pid == 0) {
919 // child: server
920 int exitCode = 0;
921 int ret = SelectServerForFork(5);
922 if (ret != 0) {
923 printf("[###][select server]run fail\n");
924 exitCode = 1;
925 }
926 printf("[###][select server]ret[%d]\n", ret);
927 exit(exitCode);
928 } else {
929 // parent: client
930 sleep(2);
931 int ret;
932 pthread_t pCli[4];
933 int pEroId[4] = {0};
934 int interval[4] = {2, 1, 3, 1};
935 for (int i = 0; i < 4; i++) {
936 ret = pthread_create(&pCli[i], nullptr, CommTcpClientTask, &interval[i]);
937 if (ret == -1) {
938 pEroId[i] = -1;
939 } else {
940 EXPECT_EQ(0, ret) << "Errinfo:pthread_create index is [" << i << "]";
941 }
942 }
943
944 void* rst = nullptr;
945 for (int i = 0; i < 4; i++)
946 {
947 if (pEroId[i] == 0)
948 {
949 ret = pthread_join(pCli[i], &rst);
950 EXPECT_EQ(0, ret) << "Errinfo:pthread_join index is [" << i << "]";
951 if (rst != nullptr)
952 {
953 printf("[###][test select]client thread[%d] return[%d]\n", i, *((int *)rst));
954 EXPECT_EQ(0, *((int *)rst));
955 free(rst);
956 }
957 }
958 }
959 WaitProcExitedOK(pid);
960 }
961 }
962
963 /**
964 * @tc.number : SUB_KERNEL_NET_0600
965 * @tc.name : test poll timeout
966 * @tc.desc : [C- SOFTWARE -0200]
967 */
968 HWTEST_F(ActsNetTest, testPollTimeout, Function | MediumTest | Level2)
969 {
970 pid_t pid = fork();
971 ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
972 if (pid == 0) {
973 // child: server
974 int ret = PollServerForFork(2000);
975 if (ret == -2) {
976 exit(0);
977 } else {
978 printf("[###][poll server]fail, ret is[%d]\n", ret);
979 exit(1);
980 }
981 } else {
982 WaitProcExitedOK(pid);
983 }
984 }
985
986 /**
987 * @tc.number : SUB_KERNEL_NET_0610
988 * @tc.name : test poll with one client
989 * @tc.desc : [C- SOFTWARE -0200]
990 */
991 HWTEST_F(ActsNetTest, testPollOneClient, Function | MediumTest | Level2)
992 {
993 pid_t pid = fork();
994 ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
995 if (pid == 0) {
996 // child: server
997 int exitCode = 0;
998 int ret = PollServerForFork(5000);
999 if (ret != 0) {
1000 printf("[###][poll server]run fail\n");
1001 exitCode = 1;
1002 }
1003 printf("[###][poll server]ret[%d]\n", ret);
1004 exit(exitCode);
1005 } else {
1006 // parent: client
1007 sleep(2);
1008 int second = 1;
1009 void* ret = CommTcpClientTask(&second);
1010 if (ret == nullptr) {
1011 printf("[###][test poll]CommClientTask ret is null\n");
1012 EXPECT_TRUE(0);
1013 } else {
1014 int rst = *((int *)ret);
1015 printf("[###][test poll]CommClientTask ret [%d]\n", rst);
1016 EXPECT_EQ(0, rst);
1017 free(ret);
1018 }
1019 WaitProcExitedOK(pid);
1020 }
1021 }
1022
1023 /**
1024 * @tc.number : SUB_KERNEL_NET_0620
1025 * @tc.name : test poll with multi clients
1026 * @tc.desc : [C- SOFTWARE -0200]
1027 */
1028 HWTEST_F(ActsNetTest, testPollMultiClients, Function | MediumTest | Level2)
1029 {
1030 pid_t pid = fork();
1031 ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
1032 if (pid == 0) {
1033 // child: server
1034 int exitCode = 0;
1035 int ret = PollServerForFork(5000);
1036 if (ret != 0) {
1037 printf("[###][poll server]run fail\n");
1038 exitCode = 1;
1039 }
1040 printf("[###][poll server]ret[%d]\n", ret);
1041 exit(exitCode);
1042 } else {
1043 // parent: client
1044 sleep(2);
1045 int ret;
1046 pthread_t pCli[6];
1047 int pEroId[6] = {0};
1048 int interval[6] = {2, 1, 3, 1, 2, 1};
1049 for (int i = 0; i < 6; i++)
1050 {
1051 ret = pthread_create(&pCli[i], nullptr, CommTcpClientTask, &interval[i]);
1052 if (ret == -1)
1053 {
1054 pEroId[i] = -1;
1055 }
1056 else
1057 {
1058 EXPECT_EQ(0, ret) << "Errinfo:pthread_create index is [" << i << "]";
1059 }
1060 }
1061
1062 void* rst = nullptr;
1063 for (int i = 0; i < 6; i++)
1064 {
1065 if (pEroId[i] == 0)
1066 {
1067 ret = pthread_join(pCli[i], &rst);
1068 EXPECT_EQ(0, ret) << "Errinfo:pthread_join index is [" << i << "]";
1069 if (rst != nullptr)
1070 {
1071 printf("[###][test poll]client thread[%d] return[%d]\n", i, *((int *)rst));
1072 EXPECT_EQ(0, *((int *)rst));
1073 free(rst);
1074 }
1075 }
1076 }
1077 WaitProcExitedOK(pid);
1078 }
1079 }
1080
1081 /**
1082 * @tc.number : SUB_KERNEL_NET_0700
1083 * @tc.name : test ioctl SIOCGIFCONF
1084 * @tc.desc : [C- SOFTWARE -0200]
1085 */
1086 HWTEST_F(ActsNetTest, testIoctlIfconf, Function | MediumTest | Level2)
1087 {
1088 int memRet = -1;
1089 int udpFd = socket(AF_INET, SOCK_DGRAM, 0);
1090 ASSERT_NE(-1, udpFd);
1091
1092 struct ifreq ifr[5];
1093 struct ifconf ifc = {0};
1094 memRet = memset_s(&ifc, sizeof(struct ifconf), 0, sizeof(struct ifconf));
1095 EXPECT_EQ(0, memRet);
1096 ifc.ifc_len = 5 * sizeof(struct ifreq);
1097 ifc.ifc_buf = (char *)ifr;
1098 int ret = ioctl(udpFd, SIOCGIFCONF, (char *)&ifc);
1099 ASSERT_EQ(0, ret) << "ioctl fail[SIOCGIFCONF], errinfo[" << strerror(errno) << "]";
1100 int ifrCount = ifc.ifc_len / sizeof(struct ifreq);
1101 EXPECT_TRUE(ifrCount >= 2);
1102 for (int i = 0; i < ifrCount; i++) {
1103 printf("[###]interface name is %s\n", ifr[i].ifr_name);
1104 }
1105 ret = close(udpFd);
1106 EXPECT_EQ(0, ret);
1107 }
1108
1109 /**
1110 * @tc.number : SUB_KERNEL_NET_0710
1111 * @tc.name : test ioctl get set delete IFADDR,FIONREAD, localhost
1112 * @tc.desc : [C- SOFTWARE -0200]
1113 */
1114 HWTEST_F(ActsNetTest, testIoctlIfnetAddrLocal, Function | MediumTest | Level2)
1115 {
1116 int udpFd = socket(AF_INET, SOCK_DGRAM, 0);
1117 ASSERT_NE(-1, udpFd);
1118
1119 // get ifnet address
1120 struct ifreq ifr = {0};
1121 const char *deviceLo = "lo";
1122 int ret = strcpy_s(ifr.ifr_name, sizeof(ifr.ifr_name), deviceLo);
1123 EXPECT_EQ(0, ret) << "strcpy_s fail!";
1124 ret = ioctl(udpFd, SIOCGIFADDR, &ifr);
1125 ASSERT_EQ(0, ret) << "ioctl fail[SIOCGIFADDR], errinfo[" << strerror(errno) << "]";
1126 struct sockaddr_in *srvAddr = (struct sockaddr_in *)&ifr.ifr_addr;
1127 EXPECT_STREQ(g_localHost, inet_ntoa(srvAddr->sin_addr));
1128 srvAddr->sin_family = AF_INET;
1129 srvAddr->sin_port = htons(STACK_PORT);
1130
1131 // get read buffer bytes
1132 ret = ::bind(udpFd, (struct sockaddr*)srvAddr, sizeof(sockaddr_in));
1133 struct sockaddr_in clnAddr = {0};
1134 clnAddr.sin_family = AF_INET;
1135 clnAddr.sin_port = htons(PEER_PORT);
1136 clnAddr.sin_addr.s_addr = inet_addr(g_localHost);
1137 const char *msg = "Hi, test ioctl...";
1138 unsigned int msgLen = strlen(msg);
1139 ret = sendto(udpFd, msg, msgLen, 0, (struct sockaddr*)&clnAddr, (socklen_t)sizeof(clnAddr));
1140 EXPECT_NE(-1, ret);
1141 EXPECT_EQ(msgLen, (unsigned int)ret);
1142 int bytes;
1143 ret = ioctl(udpFd, FIONREAD, &bytes);
1144 EXPECT_EQ(0, ret) << "ioctl fail[FIONREAD], errinfo[" << strerror(errno) << "]";
1145 EXPECT_EQ(msgLen, (unsigned int)bytes);
1146
1147 // delete lo address, will fail
1148 ret = ioctl(udpFd, SIOCDIFADDR, &ifr);
1149 EXPECT_EQ(-1, ret);
1150 EXPECT_STREQ("Operation not permitted", strerror(errno));
1151 // set lo address, will fail
1152 srvAddr->sin_addr.s_addr = inet_addr(g_localHost);
1153 ret = ioctl(udpFd, SIOCSIFADDR, &ifr);
1154 EXPECT_STREQ("Operation not permitted", strerror(errno));
1155
1156 ret = close(udpFd);
1157 EXPECT_EQ(0, ret);
1158 }
1159
1160 /**
1161 * @tc.number : SUB_KERNEL_NET_0720
1162 * @tc.name : test ioctl get and set IFADDR others
1163 * @tc.desc : [C- SOFTWARE -0200]
1164 */
1165 HWTEST_F(ActsNetTest, testIoctlIfnetAddrOthers, Function | MediumTest | Level2)
1166 {
1167 int udpFd = socket(AF_INET, SOCK_DGRAM, 0);
1168 ASSERT_NE(-1, udpFd);
1169 int memRet = -1;
1170
1171 struct ifreq ifr[5];
1172 struct ifconf ifc = {0};
1173 memRet = memset_s(&ifc, sizeof(struct ifconf), 0, sizeof(struct ifconf));
1174 EXPECT_EQ(0, memRet);
1175 ifc.ifc_len = 5 * sizeof(struct ifreq);
1176 ifc.ifc_buf = (char *)ifr;
1177 int ret = ioctl(udpFd, SIOCGIFCONF, (char *)&ifc);
1178 ASSERT_EQ(0, ret) << "ioctl fail[SIOCGIFCONF], errinfo[" << strerror(errno) << "]";
1179
1180 struct ifreq ifrTmp = {0};
1181 struct sockaddr_in *addr1 = nullptr;
1182 struct sockaddr_in *addr2 = nullptr;
1183 int ifrCount = ifc.ifc_len / sizeof(struct ifreq);
1184 EXPECT_TRUE(ifrCount >= 2);
1185 for (int i = 0; i < ifrCount; i++) {
1186 addr1 = (struct sockaddr_in *)&ifr[i].ifr_addr;
1187 if (strcmp("lo", ifr[i].ifr_name) != 0) {
1188 // get inet addr
1189 memRet = memset_s(&ifrTmp, sizeof(struct ifreq), 0, sizeof(struct ifreq));
1190 EXPECT_EQ(0, memRet);
1191 ret = strcpy_s(ifrTmp.ifr_name, sizeof(ifrTmp.ifr_name), ifr[i].ifr_name);
1192 ret = ioctl(udpFd, SIOCGIFADDR, &ifrTmp);
1193 ASSERT_EQ(0, ret) << "ioctl fail[SIOCGIFADDR], errinfo[" << strerror(errno) << "]";
1194 addr2 = (struct sockaddr_in *)&ifrTmp.ifr_addr;
1195 EXPECT_EQ(addr1->sin_addr.s_addr, addr2->sin_addr.s_addr);
1196 printf("[###]get %s\n", ifr[i].ifr_name);
1197
1198 // set inet addr
1199 addr2->sin_addr.s_addr = addr1->sin_addr.s_addr;
1200 ret = ioctl(udpFd, SIOCSIFADDR, &ifrTmp);
1201 if (strcmp("0.0.0.0", inet_ntoa(addr1->sin_addr)) == 0) {
1202 // can't set 0.0.0.0
1203 EXPECT_NE(0, ret) << "ioctl[0.0.0.0] fail[SIOCSIFADDR], errinfo[" << strerror(errno) << "]";
1204 } else {
1205 EXPECT_EQ(0, ret) << "ioctl fail[SIOCSIFADDR], errinfo[" << strerror(errno) << "]";
1206 }
1207 // get again
1208 ret = ioctl(udpFd, SIOCGIFADDR, &ifrTmp);
1209 EXPECT_EQ(0, ret) << "ioctl fail[SIOCGIFADDR], errinfo[" << strerror(errno) << "]";
1210 EXPECT_EQ(addr1->sin_addr.s_addr, addr2->sin_addr.s_addr);
1211 }
1212 }
1213 ret = close(udpFd);
1214 EXPECT_EQ(0, ret);
1215 }
1216
1217 /**
1218 * @tc.number : SUB_KERNEL_NET_0730
1219 * @tc.name : test ioctl get and set IFHWADDR
1220 * @tc.desc : [C- SOFTWARE -0200]
1221 */
1222 HWTEST_F(ActsNetTest, testIoctlIfhwAddr, Function | MediumTest | Level2)
1223 {
1224 int udpFd = socket(AF_INET, SOCK_DGRAM, 0);
1225 ASSERT_NE(-1, udpFd);
1226 int memRet = -1;
1227
1228 struct ifreq ifr[5];
1229 struct ifconf ifc = {0};
1230 memRet = memset_s(&ifc, sizeof(struct ifconf), 0, sizeof(struct ifconf));
1231 EXPECT_EQ(0, memRet);
1232 ifc.ifc_len = 5 * sizeof(struct ifreq);
1233 ifc.ifc_buf = (char *)ifr;
1234 int ret = ioctl(udpFd, SIOCGIFCONF, (char *)&ifc);
1235 ASSERT_EQ(0, ret) << "ioctl fail[SIOCGIFCONF], errinfo[" << strerror(errno) << "]";
1236
1237 char rst1[18];
1238
1239 char* macPtr = nullptr;
1240 struct ifreq ifrTmp = {0};
1241 struct sockaddr_in *addr = nullptr;
1242 int ifrCount = ifc.ifc_len / sizeof(struct ifreq);
1243 EXPECT_TRUE(ifrCount >= 2);
1244 for (int i = 0; i < ifrCount; i++) {
1245 addr = (struct sockaddr_in *)&ifr[i].ifr_addr;
1246 if (strcmp("lo", ifr[i].ifr_name) != 0) {
1247 // get ifhwaddr
1248 memRet = memset_s(&ifrTmp, sizeof(struct ifreq), 0, sizeof(struct ifreq));
1249 EXPECT_EQ(0, memRet);
1250 ret = strcpy_s(ifrTmp.ifr_name, sizeof(ifrTmp.ifr_name), ifr[i].ifr_name);
1251 EXPECT_EQ(0, ret) << "strcpy_s error";
1252 ret = ioctl(udpFd, SIOCGIFHWADDR, &ifrTmp);
1253 ASSERT_EQ(0, ret) << "ioctl fail[SIOCGIFHWADDR], errinfo[" << strerror(errno) << "]";
1254 macPtr = ifrTmp.ifr_hwaddr.sa_data;
1255 ret = sprintf_s(rst1, sizeof(rst1), "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", *macPtr, *(macPtr+1),
1256 *(macPtr+2), *(macPtr+3), *(macPtr+4), *(macPtr+5));
1257 EXPECT_EQ(strlen(rst1), (unsigned int)ret) << "sprintf_s error";
1258 printf("[###]get %s \n", ifrTmp.ifr_name);
1259 }
1260 }
1261 ret = close(udpFd);
1262 EXPECT_EQ(0, ret);
1263 }
1264
1265 /**
1266 * @tc.number : SUB_KERNEL_NET_1000
1267 * @tc.name : test socket operation
1268 * @tc.desc : [C- SOFTWARE -0200]
1269 */
1270 HWTEST_F(ActsNetTest, testSocketOpt, Function | MediumTest | Level2)
1271 {
1272 socklen_t len;
1273 int memRet = -1;
1274 struct timeval timeout = {0};
1275 int fd = socket(AF_INET, SOCK_STREAM, 0);
1276 EXPECT_NE(-1, fd);
1277
1278 int error = -1;
1279 len = sizeof(error);
1280 int ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
1281 EXPECT_EQ(0, ret);
1282 EXPECT_EQ(0, error);
1283
1284 len = sizeof(timeout);
1285 ret = getsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, &len);
1286 EXPECT_EQ(0, ret);
1287
1288 timeout.tv_sec = 1000;
1289 len = sizeof(timeout);
1290 ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, len);
1291 EXPECT_EQ(0, ret);
1292
1293 memRet = memset_s(&timeout, len, 0, len);
1294 EXPECT_EQ(0, memRet);
1295 ret = getsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, &len);
1296 EXPECT_EQ(0, ret);
1297 EXPECT_EQ(1000, timeout.tv_sec);
1298
1299 int flag = 1;
1300 ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag));
1301 EXPECT_EQ(0, ret);
1302
1303 flag = 0;
1304 len = sizeof(flag);
1305 ret = getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flag, &len);
1306 EXPECT_EQ(0, ret);
1307 EXPECT_EQ(1, flag);
1308
1309 error = -1;
1310 len = sizeof(error);
1311 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
1312 EXPECT_EQ(0, ret);
1313 EXPECT_EQ(0, error);
1314
1315 ret = close(fd);
1316 EXPECT_EQ(0, ret);
1317 }
1318
1319 /**
1320 * @tc.number : SUB_KERNEL_NET_1100
1321 * @tc.name : test getsockname and getpeername invalid input
1322 * @tc.desc : [C- SOFTWARE -0200]
1323 */
1324 HWTEST_F(ActsNetTest, testGetSocketNameInvalidInput, Function | MediumTest | Level3)
1325 {
1326 struct sockaddr addr = {0};
1327 socklen_t addrLen = sizeof(addr);
1328 int ret = getsockname(-1, &addr, &addrLen);
1329 EXPECT_EQ(-1, ret);
1330 ret = getpeername(-1, &addr, &addrLen);
1331 EXPECT_EQ(-1, ret);
1332 ret = getsockname(0, &addr, &addrLen);
1333 EXPECT_EQ(-1, ret);
1334 ret = getpeername(0, &addr, &addrLen);
1335 EXPECT_EQ(-1, ret);
1336 ret = getsockname(1, &addr, &addrLen);
1337 EXPECT_EQ(-1, ret);
1338 ret = getpeername(1, &addr, &addrLen);
1339 EXPECT_EQ(-1, ret);
1340 ret = getsockname(130, &addr, &addrLen);
1341 EXPECT_EQ(-1, ret);
1342 ret = getpeername(130, &addr, &addrLen);
1343 EXPECT_EQ(-1, ret);
1344 ret = getsockname(10, nullptr, &addrLen);
1345 EXPECT_EQ(-1, ret);
1346 ret = getpeername(10, nullptr, &addrLen);
1347 EXPECT_EQ(-1, ret);
1348 ret = getsockname(10, &addr, nullptr);
1349 EXPECT_EQ(-1, ret);
1350 ret = getpeername(10, &addr, nullptr);
1351 EXPECT_EQ(-1, ret);
1352 }
1353
1354 /**
1355 * @tc.number : SUB_KERNEL_NET_1200
1356 * @tc.name : test domain name compress and expand normally
1357 * @tc.desc : [C- SOFTWARE -0200]
1358 */
1359 HWTEST_F(ActsNetTest, testDnCompExpandNormal, Function | MediumTest | Level2)
1360 {
1361 const char* domain1 = "www.testohos.com";
1362 unsigned char buf[MAXDNAME] = {0};
1363 unsigned char* bufPtr = buf;
1364 int ret = dn_comp(domain1, bufPtr, MAXDNAME, nullptr, nullptr);
1365 EXPECT_NE(-1, ret) << "dn_comp fail, buf is '" << buf << "'";
1366 int len = ret + 4;
1367
1368 char expandDn[MAXDNAME];
1369 unsigned char* eomorig = buf + MAXDNAME;
1370 ret = dn_expand(buf, eomorig, buf, expandDn, MAXDNAME);
1371 EXPECT_NE(-1, ret);
1372 EXPECT_STREQ(domain1, expandDn);
1373 printf("dn_expand ret=%d, expandDn is '%s'\n", ret, expandDn);
1374
1375 const char* domain2 = "www.baidu.com";
1376 unsigned char* dnptrs[5] = {bufPtr, nullptr};
1377 unsigned char** lastdnptr = dnptrs + 5;
1378 ret = dn_comp(domain2, bufPtr + len, MAXDNAME - len, dnptrs, lastdnptr);
1379 EXPECT_NE(-1, ret) << "dn_comp(with dnptrs) fail, buf+len is '" << bufPtr + len << "'";
1380
1381 char expandDnWithDnptrs[MAXDNAME];
1382 unsigned char* eomorigWithDnptrs = buf + MAXDNAME;
1383 ret = dn_expand(dnptrs[1], eomorigWithDnptrs, dnptrs[1], expandDnWithDnptrs, MAXDNAME);
1384 EXPECT_NE(-1, ret);
1385 EXPECT_STREQ(domain2, expandDnWithDnptrs);
1386 }
1387
1388 /**
1389 * @tc.number : SUB_KERNEL_NET_1300
1390 * @tc.name : test domain name compress and expand abnormally
1391 * @tc.desc : [C- SOFTWARE -0200]
1392 */
1393 HWTEST_F(ActsNetTest, testDnCompExpandAbnormal, Function | MediumTest | Level3)
1394 {
1395 const char* src = "!#%^.)(-+{}\r\n";
1396 unsigned char destComp[MAXDNAME];
1397 int ret = dn_comp(src, destComp, 10, nullptr, nullptr);
1398 EXPECT_EQ(-1, ret);
1399 printf("dn_comp ret=%d, compDn is '%s'\n", ret, destComp);
1400
1401 ret = dn_comp("www.baidu.com", destComp, MAXDNAME, nullptr, nullptr);
1402 EXPECT_NE(-1, ret);
1403
1404 // dest array too small
1405 char destExpand[5];
1406 unsigned char *eomorig = destComp + 10;
1407 ret = dn_expand(destComp, eomorig, destComp, destExpand, MAXDNAME);
1408 EXPECT_EQ(-1, ret);
1409 printf("dn_expand ret=%d, expandDn is '%s'\n", ret, destExpand);
1410
1411 // compress info is 0
1412 unsigned char destCompError[20] = {0};
1413 eomorig = destCompError + 20;
1414 ret = dn_expand(destCompError, eomorig, destCompError, destExpand, MAXDNAME);
1415 EXPECT_EQ(1, ret);
1416 printf("dn_expand ret=%d, expandDn is '%s'\n", ret, destExpand);
1417 }
1418
1419 /**
1420 * @tc.number : SUB_KERNEL_NET_1400
1421 * @tc.name : test convert 48 bit Ethernet host address normally
1422 * @tc.desc : [C- SOFTWARE -0200]
1423 */
1424 HWTEST_F(ActsNetTest, testEtherConvertNormal, Function | MediumTest | Level2)
1425 {
1426 char atonByteRst[24];
1427 char* ntoaRst = nullptr;
1428 struct ether_addr *atonRst = nullptr;
1429 char addrHex[3][18] = {"FF:FF:FF:FF:FF:FF", "00:00:00:00:00:00", "0A:1B:2C:3D:4E:5F"};
1430 char addrByte[3][24] = {"255.255.255.255.255.255", "0.0.0.0.0.0", "10.27.44.61.78.95"};
1431 for (int i = 0; i < 3; i++) {
1432 atonRst = nullptr;
1433 atonRst = ether_aton(addrHex[i]);
1434 ASSERT_TRUE(atonRst != nullptr);
1435 int ret = sprintf_s(atonByteRst, sizeof(atonByteRst), "%u.%u.%u.%u.%u.%u",
1436 atonRst->ether_addr_octet[0], atonRst->ether_addr_octet[1],
1437 atonRst->ether_addr_octet[2], atonRst->ether_addr_octet[3],
1438 atonRst->ether_addr_octet[4], atonRst->ether_addr_octet[5]);
1439 EXPECT_STREQ(addrByte[i], atonByteRst) << "ErrInfo:result '" << atonByteRst << "',ret=" << ret;
1440
1441 ntoaRst = nullptr;
1442 ntoaRst = ether_ntoa(atonRst);
1443 EXPECT_TRUE(ntoaRst != nullptr);
1444 EXPECT_STREQ(addrHex[i], ntoaRst) << "ErrInfo:result '" << ntoaRst << "'";
1445 }
1446 }
1447
1448 /**
1449 * @tc.number : SUB_KERNEL_NET_1500
1450 * @tc.name : test convert 48 bit Ethernet host address abnormally
1451 * @tc.desc : [C- SOFTWARE -0200]
1452 */
1453 HWTEST_F(ActsNetTest, testEtherConvertAbnormal, Function | MediumTest | Level3)
1454 {
1455 char* addrError = (char *)":::F:Z:";
1456 struct ether_addr *atonRst = nullptr;
1457 atonRst = ether_aton(addrError);
1458 EXPECT_TRUE(atonRst == nullptr);
1459 }
1460
1461 /**
1462 * @tc.number : SUB_KERNEL_NET_1600
1463 * @tc.name : test thread safe convert 48 bit Ethernet host address normally
1464 * @tc.desc : [C- SOFTWARE -0200]
1465 */
1466 HWTEST_F(ActsNetTest, testEtherConvertNormalWithThreadSafe, Function | MediumTest | Level2)
1467 {
1468 int ret;
1469 char ntoaDataRst[18];
1470 char* ntoaPointRst = nullptr;
1471 struct ether_addr *atonPointRst = nullptr;
1472 struct ether_addr *atonDataRst = (ether_addr *)malloc(sizeof(ether_addr));
1473 ASSERT_TRUE(atonDataRst != nullptr);
1474 char addrHex[3][18] = {"FF:FF:FF:FF:FF:FF", "00:00:00:00:00:00", "5F:4E:2C:3D:1B:0A"};
1475 char addrByte[3][24] = {"255.255.255.255.255.255", "0.0.0.0.0.0", "95.78.44.61.27.10"};
1476 for (int i = 0; i < 3; i++) {
1477 atonPointRst = nullptr;
1478 atonPointRst = ether_aton_r(addrHex[i], atonDataRst);
1479 ASSERT_TRUE(atonPointRst != nullptr);
1480 char byteRst[24];
1481 ret = sprintf_s(byteRst, sizeof(byteRst), "%u.%u.%u.%u.%u.%u",
1482 atonDataRst->ether_addr_octet[0], atonDataRst->ether_addr_octet[1],
1483 atonDataRst->ether_addr_octet[2], atonDataRst->ether_addr_octet[3],
1484 atonDataRst->ether_addr_octet[4], atonDataRst->ether_addr_octet[5]);
1485 EXPECT_STREQ(addrByte[i], byteRst) << "ErrInfo:ether_aton_r rst[" << byteRst << "], ret[" << ret << "]";
1486 ret = sprintf_s(byteRst, sizeof(byteRst), "%u.%u.%u.%u.%u.%u",
1487 atonPointRst->ether_addr_octet[0], atonPointRst->ether_addr_octet[1],
1488 atonPointRst->ether_addr_octet[2], atonPointRst->ether_addr_octet[3],
1489 atonPointRst->ether_addr_octet[4], atonPointRst->ether_addr_octet[5]);
1490 EXPECT_STREQ(addrByte[i], byteRst) << "ErrInfo:ether_aton_r rst[" << byteRst << "], ret[" << ret << "]";
1491
1492 ntoaPointRst = nullptr;
1493 ntoaPointRst = ether_ntoa_r(atonDataRst, ntoaDataRst);
1494 EXPECT_STREQ(addrHex[i], ntoaDataRst);
1495 EXPECT_STREQ(addrHex[i], ntoaPointRst);
1496 printf("ether_ntoa_r result '%s', '%s'\n", ntoaDataRst, ntoaPointRst);
1497 }
1498
1499 free(atonDataRst);
1500 }
1501
1502 /**
1503 * @tc.number : SUB_KERNEL_NET_1700
1504 * @tc.name : test get protocals by normal name and number
1505 * @tc.desc : [C- SOFTWARE -0200]
1506 */
1507 HWTEST_F(ActsNetTest, testGetProtoByNormal, Function | MediumTest | Level2)
1508 {
1509 int number;
1510 struct protoent *protoPtr = nullptr;
1511 for (int i = 0; i < PROTOCOL_COUNT; i++) {
1512 protoPtr = getprotobyname(g_protocolList[i]);
1513 if (protoPtr == nullptr) {
1514 printf("getprotobyname is NULL! [%s]\n", g_protocolList[i]);
1515 continue;
1516 }
1517 EXPECT_STREQ(g_protocolList[i], protoPtr->p_name);
1518 number = protoPtr->p_proto;
1519
1520 protoPtr = nullptr;
1521 protoPtr = getprotobynumber(number);
1522 if (protoPtr == nullptr) {
1523 EXPECT_TRUE(protoPtr != NULL) << "ErrInfo:getprotobynumber NULL '" << g_protocolList[i] << "'";
1524 continue;
1525 }
1526 EXPECT_STREQ(g_protocolList[i], protoPtr->p_name);
1527 protoPtr = nullptr;
1528 }
1529 }
1530
1531 /**
1532 * @tc.number : SUB_KERNEL_NET_1800
1533 * @tc.name : test get protocals by invalid name and number
1534 * @tc.desc : [C- SOFTWARE -0200]
1535 */
1536 HWTEST_F(ActsNetTest, testGetProtoByAbNormal, Function | MediumTest | Level2)
1537 {
1538 struct protoent* protoPtr = nullptr;
1539 protoPtr = getprotobyname("ipv4");
1540 EXPECT_TRUE(protoPtr == NULL);
1541 protoPtr = getprotobyname("idpr-cmtp-s");
1542 EXPECT_TRUE(protoPtr == NULL);
1543 protoPtr = getprotobyname("12&^%$\n\0?><");
1544 EXPECT_TRUE(protoPtr == NULL);
1545
1546 protoPtr = getprotobynumber(-1);
1547 EXPECT_TRUE(protoPtr == NULL);
1548 protoPtr = getprotobynumber(256);
1549 EXPECT_TRUE(protoPtr == NULL);
1550 }
1551
1552 /**
1553 * @tc.number : SUB_KERNEL_NET_1900
1554 * @tc.name : test getprotoent
1555 * @tc.desc : [C- SOFTWARE -0200]
1556 */
1557 HWTEST_F(ActsNetTest, testGetProtoent, Function | MediumTest | Level2)
1558 {
1559 int index = 0;
1560 struct protoent* protoPtr = nullptr;
1561 setprotoent(0);
1562 while (index < PROTOCOL_COUNT) {
1563 protoPtr = getprotoent();
1564 if (protoPtr == NULL) {
1565 EXPECT_TRUE(protoPtr != NULL) << "ErrInfo:getprotoent NULL, index[" << index << "]";
1566 } else {
1567 EXPECT_STREQ(g_protocolList[index], protoPtr->p_name);
1568 EXPECT_EQ(g_protocolNumber[index], protoPtr->p_proto);
1569 }
1570 protoPtr = nullptr;
1571 index ++;
1572 }
1573
1574 protoPtr = getprotoent();
1575 EXPECT_TRUE(protoPtr == NULL);
1576 }
1577
1578 /**
1579 * @tc.number : SUB_KERNEL_NET_2000
1580 * @tc.name : test setprotoent
1581 * @tc.desc : [C- SOFTWARE -0200]
1582 */
1583 HWTEST_F(ActsNetTest, testSetProtoent, Function | MediumTest | Level2)
1584 {
1585 int index = 0;
1586 struct protoent* protoPtr = nullptr;
1587 setprotoent(0);
1588 while (index < PROTOCOL_COUNT) {
1589 protoPtr = getprotoent();
1590 if (protoPtr == nullptr) {
1591 EXPECT_TRUE(protoPtr != NULL) << "ErrInfo:getprotoent NULL, index[" << index << "]";
1592 } else {
1593 EXPECT_STREQ(g_protocolList[index], protoPtr->p_name);
1594 EXPECT_EQ(g_protocolNumber[index], protoPtr->p_proto);
1595 }
1596 protoPtr = nullptr;
1597 index ++;
1598 }
1599 protoPtr = getprotoent();
1600 EXPECT_TRUE(protoPtr == NULL);
1601
1602 setprotoent(0);
1603 protoPtr = nullptr;
1604 protoPtr = getprotoent();
1605 ASSERT_TRUE(protoPtr != NULL);
1606 EXPECT_STREQ(g_protocolList[0], protoPtr->p_name);
1607 EXPECT_EQ(g_protocolNumber[0], protoPtr->p_proto);
1608
1609 index = 0;
1610 protoPtr = nullptr;
1611 setprotoent(0);
1612 while (index < PROTOCOL_COUNT) {
1613 protoPtr = getprotoent();
1614 if (protoPtr == nullptr) {
1615 EXPECT_TRUE(protoPtr != NULL) << "ErrInfo:getprotoent NULL, index[" << index << "]";
1616 } else {
1617 EXPECT_STREQ(g_protocolList[index], protoPtr->p_name);
1618 EXPECT_EQ(g_protocolNumber[index], protoPtr->p_proto);
1619 }
1620 protoPtr = nullptr;
1621 index ++;
1622 }
1623 }
1624
1625 /**
1626 * @tc.number : SUB_KERNEL_NET_2100
1627 * @tc.name : test herror,no output and return value, so no check point
1628 * @tc.desc : [C- SOFTWARE -0200]
1629 */
1630 HWTEST_F(ActsNetTest, testHerror, Function | MediumTest | Level2)
1631 {
1632 const char* fileName = "/storage/test_herror";
1633 FILE* fp = freopen(fileName, "w", stderr);
1634 ASSERT_NE(nullptr, fp);
1635
1636 const char *msg = "herror msg";
1637 herror(msg);
1638 const int errorCode[5] = {HOST_NOT_FOUND, TRY_AGAIN, NO_RECOVERY, NO_DATA, NO_ADDRESS};
1639 for (int i = 0; i < 5; i++) {
1640 h_errno = errorCode[i];
1641 herror(msg);
1642 }
1643 h_errno = -1;
1644 herror(msg);
1645 h_errno = 0;
1646 herror(msg);
1647 herror(msg);
1648 fclose(stderr);
1649
1650 char buffer[256];
1651 const char* expectStr = "herror msg: Unknown error\nherror msg: Host not found\nherror msg: \
1652 Try again\nherror msg: Non-recoverable error\nherror msg: Address not available\nherror msg: \
1653 Address not available\nherror msg: Unknown error\nherror msg: Unknown error\nherror msg: Unknown error\n";
1654 FILE* fpRead = fopen(fileName, "rb");
1655 size_t bytes = fread(buffer, 1, sizeof(buffer), fpRead);
1656 buffer[bytes] = 0;
1657 EXPECT_STREQ(expectStr, buffer);
1658
1659 int ret = fclose(fpRead);
1660 EXPECT_EQ(0, ret);
1661 ret = remove(fileName);
1662 EXPECT_EQ(0, ret);
1663 }
1664
1665 /**
1666 * @tc.number : SUB_KERNEL_NET_2200
1667 * @tc.name : test hstrerror normal
1668 * @tc.desc : [C- SOFTWARE -0200]
1669 */
1670 HWTEST_F(ActsNetTest, testHstrerrorNormal, Function | MediumTest | Level2)
1671 {
1672 const char errorMsgs[5][22] = {"Host not found", "Try again", "Non-recoverable error", "Address not available",
1673 "Address not available"};
1674 const int errorCode[5] = {HOST_NOT_FOUND, TRY_AGAIN, NO_RECOVERY, NO_DATA, NO_ADDRESS};
1675 for (int i = 0; i < 5; i++) {
1676 EXPECT_STREQ(errorMsgs[i], hstrerror(errorCode[i]));
1677 }
1678 }
1679
1680 /**
1681 * @tc.number : SUB_KERNEL_NET_2300
1682 * @tc.name : test hstrerror abnormal
1683 * @tc.desc : [C- SOFTWARE -0200]
1684 */
1685 HWTEST_F(ActsNetTest, testHstrerrorAbnormal, Function | MediumTest | Level2)
1686 {
1687 const char* expectStr = "Unknown error";
1688 EXPECT_STREQ(expectStr, hstrerror(0));
1689 EXPECT_STREQ(expectStr, hstrerror(-1));
1690 EXPECT_STREQ(expectStr, hstrerror(16));
1691 }
1692
1693 /**
1694 * @tc.number : SUB_KERNEL_NET_2400
1695 * @tc.name : test convert value from host to network byte order
1696 * @tc.desc : [C- SOFTWARE -0200]
1697 */
1698 HWTEST_F(ActsNetTest, testHostToNetwork, Function | MediumTest | Level2)
1699 {
1700 uint32_t intInput1 = 0;
1701 uint32_t intRst1 = htonl(intInput1);
1702 uint32_t intInput2 = 65536;
1703 uint32_t intRst2 = htonl(intInput2);
1704
1705 uint16_t shortInput1 = 0;
1706 uint16_t shortRst1 = htons(shortInput1);
1707 uint16_t shortInput2 = 255;
1708 uint16_t shortRst2 = htons(shortInput2);
1709
1710 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1711 uint32_t expectZero = 0;
1712 uint32_t expectForIinput2 = 256;
1713 uint32_t expectForSinput2 = 65280;
1714 EXPECT_EQ(expectZero, intRst1);
1715 EXPECT_EQ(expectForIinput2, intRst2);
1716 EXPECT_EQ(expectZero, shortRst1);
1717 EXPECT_EQ(expectForSinput2, shortRst2);
1718 #else
1719 EXPECT_EQ(intInput1, intRst1);
1720 EXPECT_EQ(intInput2, intRst2);
1721 EXPECT_EQ(shortInput1, shortRst1);
1722 EXPECT_EQ(shortInput2, shortRst2);
1723 #endif
1724 }
1725
1726 /**
1727 * @tc.number : SUB_KERNEL_NET_2500
1728 * @tc.name : test convert value from network to host byte order
1729 * @tc.desc : [C- SOFTWARE -0200]
1730 */
1731 HWTEST_F(ActsNetTest, testNetworkToHost, Function | MediumTest | Level2)
1732 {
1733 uint32_t intInput1 = 0;
1734 uint32_t intRst1 = ntohl(intInput1);
1735 uint32_t intInput2 = 65536;
1736 uint32_t intRst2 = ntohl(intInput2);
1737 uint16_t shortInput1 = 0;
1738 uint16_t shortRst1 = ntohs(shortInput1);
1739 uint16_t shortInput2 = 255;
1740 uint16_t shortRst2 = ntohs(shortInput2);
1741
1742 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1743 uint32_t expectZero = 0;
1744 uint32_t expectForIinput2 = 256;
1745 uint32_t expectForSinput2 = 65280;
1746 EXPECT_EQ(expectZero, intRst1);
1747 EXPECT_EQ(expectForIinput2, intRst2);
1748 EXPECT_EQ(expectZero, shortRst1);
1749 EXPECT_EQ(expectForSinput2, shortRst2);
1750 #else
1751 EXPECT_EQ(intInput1, intRst1);
1752 EXPECT_EQ(intInput2, intRst2);
1753 EXPECT_EQ(shortInput1, shortRst1);
1754 EXPECT_EQ(shortInput2, shortRst2);
1755 #endif
1756 }
1757
1758 /**
1759 * @tc.number : SUB_KERNEL_NET_2600
1760 * @tc.name : test inet_pton IPv4 normal
1761 * @tc.desc : [C- SOFTWARE -0200]
1762 */
1763 HWTEST_F(ActsNetTest, testInetPtonIpv4Normal, Function | MediumTest | Level2)
1764 {
1765 int ret;
1766 struct in_addr rst = {0};
1767 char cpAddrs[4][16] = {"10.58.212.100", "0.0.0.0", "255.0.0.0", "255.255.255.255"};
1768 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1769 unsigned int expectLittle[4] = {1691630090, 0, 255, 4294967295};
1770 #else
1771 unsigned int expectBig[4] = {171627620, 0, 4278190080, 4294967295};
1772 #endif
1773
1774 for (int i = 0; i < 4; i++) {
1775 ret = inet_pton(AF_INET, cpAddrs[i], &rst);
1776 EXPECT_EQ(1, ret);
1777 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1778 EXPECT_EQ(expectLittle[i], rst.s_addr);
1779 #else
1780 EXPECT_EQ(expectBig[i], rst.s_addr);
1781 #endif
1782 printf("[###]inet_pton : un[%u],s[%d],hex[%x]\n", rst.s_addr, rst.s_addr, rst.s_addr);
1783 }
1784 }
1785
1786 /**
1787 * @tc.number : SUB_KERNEL_NET_2700
1788 * @tc.name : test inet_pton IPv4 abnormal
1789 * @tc.desc : [C- SOFTWARE -0200]
1790 */
1791 HWTEST_F(ActsNetTest, testInetPtonIpv4Abnormal, Function | MediumTest | Level2)
1792 {
1793 int ret;
1794 struct in_addr rst = {0};
1795 char cpAddrs[10][16] = {"255", "256", "255.0", "255.0.0", "256.0.0.1", "a.a.a.a", "....", "#",
1796 "127.0.0.f", "0:0:0:0:0:0:0:1"};
1797 for (int i = 0; i < 10; i++) {
1798 ret = inet_pton(AF_INET, cpAddrs[i], &rst);
1799 EXPECT_EQ(0, ret) << "ErrInfo:inet_pton abnormal [" << cpAddrs[i] << "]";
1800 }
1801 }
1802
1803 /**
1804 * @tc.number : SUB_KERNEL_NET_2800
1805 * @tc.name : test inet_pton IPv6 normal
1806 * @tc.desc : [C- SOFTWARE -0200]
1807 */
1808 HWTEST_F(ActsNetTest, testInetPtonIpv6Normal, Function | MediumTest | Level2)
1809 {
1810 int ret;
1811 struct in6_addr rst = {0};
1812 char cpAddrs[6][40] = {"0101:0101:0101:0101:1010:1010:1010:1010", "0:0:0:0:0:0:0:0",
1813 "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", "::", "1::", "0011:0011:0011:0011:11:11:11:11"};
1814 for (int i = 0; i < 6; i++) {
1815 ret = inet_pton(AF_INET6, cpAddrs[i], &rst);
1816 EXPECT_EQ(1, ret) << "ErrInfo:inet_pton [" << cpAddrs[i] << "]";
1817 }
1818 }
1819
1820 /**
1821 * @tc.number : SUB_KERNEL_NET_2900
1822 * @tc.name : test inet_pton IPv6 abnormal
1823 * @tc.desc : [C- SOFTWARE -0200]
1824 */
1825 HWTEST_F(ActsNetTest, testInetPtonIpv6Abnormal, Function | MediumTest | Level2)
1826 {
1827 int ret;
1828 struct in6_addr rst = {0};
1829 char cpAddrs[7][40] = {"127.0.0.1", "f", ":", "0:0", "1:::", ":::::::",
1830 "1111:1111:1111:1111:1111:1111:1111:111G"};
1831 for (int i = 0; i < 7; i++) {
1832 ret = inet_pton(AF_INET6, cpAddrs[i], &rst);
1833 EXPECT_EQ(0, ret) << "ErrInfo:inet_pton abnormal [" << cpAddrs[i] << "]";
1834 }
1835 }
1836
1837 /**
1838 * @tc.number : SUB_KERNEL_NET_3000
1839 * @tc.name : test inet_pton with invalid family
1840 * @tc.desc : [C- SOFTWARE -0200]
1841 */
1842 HWTEST_F(ActsNetTest, testInetPtonInvalidFamily, Function | MediumTest | Level2)
1843 {
1844 struct in_addr rst = {0};
1845 int ret = inet_pton(AF_IPX, "127.0.0.1", &rst);
1846 EXPECT_EQ(-1, ret);
1847 ret = inet_pton(-1, "127.0.0.1", &rst);
1848 EXPECT_EQ(-1, ret);
1849 }
1850
1851 /**
1852 * @tc.number : SUB_KERNEL_NET_3100
1853 * @tc.name : test inet_ntop IPv4 normal
1854 * @tc.desc : [C- SOFTWARE -0200]
1855 */
1856 HWTEST_F(ActsNetTest, testInetNtopIpv4Normal, Function | MediumTest | Level2)
1857 {
1858 const char* ret = nullptr;
1859 struct in_addr inputAddr = {0};
1860 char rstBuff[INET_ADDRSTRLEN];
1861 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1862 unsigned int inputLittle[4] = {0x64d43a0a, 0, 255, 4294967295};
1863 #else
1864 unsigned int inputBig[4] = {171627620, 0, 4278190080, 4294967295};
1865 #endif
1866
1867 char expectAddrs[4][16] = {"10.58.212.100", "0.0.0.0", "255.0.0.0", "255.255.255.255"};
1868 for (int i = 0; i < 4; i++) {
1869 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1870 inputAddr.s_addr = inputLittle[i];
1871 #else
1872 inputAddr.s_addr = inputBig[i];
1873 #endif
1874 ret = inet_ntop(AF_INET, &inputAddr, rstBuff, sizeof(rstBuff));
1875 if (ret == nullptr) {
1876 EXPECT_TRUE(ret != NULL) << "ErrInfo:inet_ntop NULL [" << expectAddrs[i] << "]";
1877 } else {
1878 printf("[###]inet_ntop expect [%s]: ret[%s], buf[%s]\n", expectAddrs[i], ret, rstBuff);
1879 EXPECT_STREQ(expectAddrs[i], ret);
1880 EXPECT_STREQ(expectAddrs[i], rstBuff);
1881 }
1882 }
1883 }
1884
1885 /**
1886 * @tc.number : SUB_KERNEL_NET_3200
1887 * @tc.name : test inet_ntop IPv4 boundary input
1888 * @tc.desc : [C- SOFTWARE -0200]
1889 */
1890 HWTEST_F(ActsNetTest, testInetNtopIpv4Abnormal, Function | MediumTest | Level2)
1891 {
1892 const char* ret = nullptr;
1893 struct in_addr inputAddr = {0};
1894 char rstBuff[INET_ADDRSTRLEN];
1895 char expectStr[2][16] = {"255.255.255.255", "0.0.0.0"};
1896 for (int i = 0; i < 2; i++) {
1897 inputAddr.s_addr = in_addr_t(i == 0 ? -1 : 4294967296);
1898 ret = inet_ntop(AF_INET, &inputAddr, rstBuff, sizeof(rstBuff));
1899 ASSERT_TRUE(ret != NULL);
1900 EXPECT_STREQ(expectStr[i], ret);
1901 EXPECT_STREQ(expectStr[i], rstBuff);
1902 }
1903 }
1904
1905 /**
1906 * @tc.number : SUB_KERNEL_NET_3300
1907 * @tc.name : test inet_ntop IPv6 normal
1908 * @tc.desc : [C- SOFTWARE -0200]
1909 */
1910 HWTEST_F(ActsNetTest, testInetNtopIpv6Normal, Function | MediumTest | Level2)
1911 {
1912 int iret;
1913 const char* ret = nullptr;
1914 struct in6_addr inputAddr = {0};
1915 char rstBuff[INET6_ADDRSTRLEN];
1916 char inputAddrs[6][40] = {"0101:0101:0101:0101:1010:1010:1010:1010", "0:0:0:0:0:0:0:0",
1917 "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", "::", "1::", "0011:0011:0011:0011:11:11:11:11"};
1918 char expectAddrs[6][40] = {"101:101:101:101:1010:1010:1010:1010", "::",
1919 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "::", "1::", "11:11:11:11:11:11:11:11"};
1920 for (int i = 0; i < 6; i++) {
1921 iret = inet_pton(AF_INET6, inputAddrs[i], &inputAddr);
1922 ASSERT_EQ(1, iret);
1923 ret = inet_ntop(AF_INET6, &inputAddr, rstBuff, sizeof(rstBuff));
1924 if (ret == nullptr) {
1925 EXPECT_TRUE(ret != NULL) << "ErrInfo:inet_ntop NULL [" << expectAddrs[i] << "]";
1926 } else {
1927 printf("[###]inet_ntop expect [%s]: ret[%s], buf[%s]\n", expectAddrs[i], ret, rstBuff);
1928 EXPECT_STREQ(expectAddrs[i], ret);
1929 EXPECT_STREQ(expectAddrs[i], rstBuff);
1930 }
1931 }
1932 }
1933
1934 /**
1935 * @tc.number : SUB_KERNEL_NET_3400
1936 * @tc.name : test inet_ntop IPv6 boundary input
1937 * @tc.desc : [C- SOFTWARE -0200]
1938 */
1939 HWTEST_F(ActsNetTest, testInetNtopIpv6Abnormal, Function | MediumTest | Level2)
1940 {
1941 const char* ret = nullptr;
1942 struct in6_addr inputAddr = {0};
1943 char rstBuff[INET6_ADDRSTRLEN];
1944 char expectStr[2][40] = {"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "::"};
1945 for (int i = 0; i < 2; i++) {
1946 for (int j = 0; j < 16; j++) {
1947 inputAddr.s6_addr[j] = (i == 0 ? -1 : 256);
1948 }
1949 ret = inet_ntop(AF_INET6, &inputAddr, rstBuff, sizeof(rstBuff));
1950 ASSERT_TRUE(ret != NULL);
1951 EXPECT_STREQ(expectStr[i], ret);
1952 EXPECT_STREQ(expectStr[i], rstBuff);
1953 }
1954 }
1955
1956 /**
1957 * @tc.number : SUB_KERNEL_NET_3500
1958 * @tc.name : test inet_ntop with invalid family
1959 * @tc.desc : [C- SOFTWARE -0200]
1960 */
1961 HWTEST_F(ActsNetTest, testInetNtopInvalidFamily, Function | MediumTest | Level2)
1962 {
1963 int iret;
1964 const char* ret = nullptr;
1965 struct in6_addr inputAddr = {0};
1966 char rstBuff[INET6_ADDRSTRLEN];
1967
1968 iret = inet_pton(AF_INET6, "1::", &inputAddr);
1969 ASSERT_EQ(1, iret);
1970 ret = inet_ntop(AF_IPX, &inputAddr, rstBuff, sizeof(rstBuff));
1971 EXPECT_TRUE(ret == NULL);
1972 ret = inet_ntop(-1, &inputAddr, rstBuff, sizeof(rstBuff));
1973 EXPECT_TRUE(ret == NULL);
1974 }
1975
1976 /**
1977 * @tc.number : SUB_KERNEL_NET_3600
1978 * @tc.name : test inet_addr normal
1979 * @tc.desc : [C- SOFTWARE -0200]
1980 */
1981 HWTEST_F(ActsNetTest, testInetAddrNormal, Function | MediumTest | Level2)
1982 {
1983 in_addr_t rst;
1984 char cpAddrs[7][16] = {"10.58.212.100", "0.0.0.0", "255", "0.255.0", "255.0.0.0",
1985 "255.255.255.255", "257"};
1986 unsigned int expectList[7] = {1691630090, 0, 4278190080, 65280, 255, 4294967295, 16842752};
1987 for (int i = 0; i < 7; i++) {
1988 rst = inet_addr(cpAddrs[i]);
1989 EXPECT_EQ(expectList[i], rst);
1990 printf("[###]inet_addr %s: uns[%u],s[%d],hex[%x]\n", cpAddrs[i], rst, rst, rst);
1991 }
1992 }
1993
1994 /**
1995 * @tc.number : SUB_KERNEL_NET_3700
1996 * @tc.name : test inet_addr abnormal
1997 * @tc.desc : [C- SOFTWARE -0200]
1998 */
1999 HWTEST_F(ActsNetTest, testInetAddrAbnormal, Function | MediumTest | Level2)
2000 {
2001 in_addr_t rst;
2002 unsigned int expect = 4294967295;
2003 char invalidAddrs[4][10] = {"-1", "a", "%^", "257.1.1.1"};
2004 for (int i = 0; i < 4; i++) {
2005 rst = inet_addr(invalidAddrs[i]);
2006 EXPECT_EQ(expect, rst) << "ErrInfo:inet_addr [" << invalidAddrs[i] << "]";
2007 }
2008 }
2009
2010 /**
2011 * @tc.number : SUB_KERNEL_NET_3800
2012 * @tc.name : test inet_aton normal
2013 * @tc.desc : [C- SOFTWARE -0200]
2014 */
2015 HWTEST_F(ActsNetTest, testInetAtonNormal, Function | MediumTest | Level2)
2016 {
2017 int ret;
2018 struct in_addr addr = {0};
2019 char cpAddrs[7][16] = {"10.58.212.100", "0.0.0.0", "255", "0.255.0", "255.0.0.0",
2020 "255.255.255.255", "257"};
2021 unsigned int expectList[7] = {1691630090, 0, 4278190080, 65280, 255, 4294967295, 16842752};
2022 for (int i = 0; i < 7; i++) {
2023 ret = inet_aton(cpAddrs[i], &addr);
2024 EXPECT_EQ(1, ret);
2025 EXPECT_EQ(expectList[i], addr.s_addr);
2026 printf("##inet_aton %s: uns[%u],s[%d],hex[%x]\n", cpAddrs[i], addr.s_addr, addr.s_addr, addr.s_addr);
2027 }
2028 }
2029
2030 /**
2031 * @tc.number : SUB_KERNEL_NET_3900
2032 * @tc.name : test inet_aton abnormal
2033 * @tc.desc : [C- SOFTWARE -0200]
2034 */
2035 HWTEST_F(ActsNetTest, testInetAtonAbnormal, Function | MediumTest | Level2)
2036 {
2037 int ret;
2038 struct in_addr addr = {0};
2039 char invalidAddrs[4][10] = {"-1", "a", "%^", "257.1.1.1"};
2040 for (int i = 0; i < 4; i++) {
2041 ret = inet_aton(invalidAddrs[i], &addr);
2042 EXPECT_EQ(0, ret) << "ErrInfo:inet_addr [" << invalidAddrs[i] << "]";
2043 }
2044 }
2045
2046 /**
2047 * @tc.number : SUB_KERNEL_NET_4000
2048 * @tc.name : test inet_ntoa normal
2049 * @tc.desc : [C- SOFTWARE -0200]
2050 */
2051 HWTEST_F(ActsNetTest, testInetNtoaNormal, Function | MediumTest | Level2)
2052 {
2053 char *rst = nullptr;
2054 struct in_addr addr = {0};
2055 char expectAddrs[6][16] = {"10.58.212.100", "0.0.0.0", "255.255.255.255", "0.0.0.255", "0.255.0.0", "0.0.1.1"};
2056 unsigned int inputList[6] = {1691630090, 0, 4294967295, 4278190080, 65280, 16842752};
2057 for (int i = 0; i < 6; i++) {
2058 addr.s_addr = inputList[i];
2059 rst = inet_ntoa(addr);
2060 if (rst == nullptr) {
2061 EXPECT_TRUE(rst != NULL);
2062 } else {
2063 EXPECT_STREQ(expectAddrs[i], rst);
2064 }
2065 printf("##inet_ntoa %u: rst is [%s]\n", inputList[i], expectAddrs[i]);
2066 }
2067 }
2068
2069 /**
2070 * @tc.number : SUB_KERNEL_NET_4100
2071 * @tc.name : test inet_ntoa boundary input
2072 * @tc.desc : [C- SOFTWARE -0200]
2073 */
2074 HWTEST_F(ActsNetTest, testInetNtoaAbnormal, Function | MediumTest | Level2)
2075 {
2076 const char* ret = nullptr;
2077 struct in_addr addrInput = {0};
2078 char expectStr[2][16] = {"255.255.255.255", "0.0.0.0"};
2079 for (int i = 0; i < 2; i++) {
2080 addrInput.s_addr = (i == 0 ? -1 : 4294967296);
2081 ret = inet_ntoa(addrInput);
2082 ASSERT_TRUE(ret != NULL);
2083 EXPECT_STREQ(expectStr[i], ret);
2084 }
2085 }
2086
2087 /**
2088 * @tc.number : SUB_KERNEL_NET_4200
2089 * @tc.name : test inet_network normal
2090 * @tc.desc : [C- SOFTWARE -0200]
2091 */
2092 HWTEST_F(ActsNetTest, testInetNetworkNormal, Function | MediumTest | Level2)
2093 {
2094 in_addr_t rst;
2095 char cpAddrs[7][16] = {"10.58.212.100", "0.0.0.0", "255", "0.255.0", "255.0.0.0",
2096 "255.255.255.255", "257"};
2097 unsigned int expectList[7] = {171627620, 0, 255, 16711680, 4278190080, 4294967295, 257};
2098 for (int i = 0; i < 7; i++) {
2099 rst = inet_network(cpAddrs[i]);
2100 EXPECT_EQ(expectList[i], rst);
2101 printf("##inet_network %s: uns[%u],s[%d],hex[%x]\n", cpAddrs, rst, rst, rst);
2102 }
2103 }
2104
2105 /**
2106 * @tc.number : SUB_KERNEL_NET_4300
2107 * @tc.name : test inet_network abnormal
2108 * @tc.desc : [C- SOFTWARE -0200]
2109 */
2110 HWTEST_F(ActsNetTest, testInetNetworkAbnormal, Function | MediumTest | Level2)
2111 {
2112 in_addr_t rst;
2113 unsigned int expect = 4294967295;
2114 char invalidAddrs[4][10] = {"-1", "a", "%^", "257.1.1.1"};
2115 for (int i = 0; i < 4; i++) {
2116 rst = inet_network(invalidAddrs[i]);
2117 EXPECT_EQ(expect, rst) << "ErrInfo:inet_network [" << invalidAddrs[i] << "]";
2118 }
2119 }
2120
2121 /**
2122 * @tc.number : SUB_KERNEL_NET_4400
2123 * @tc.name : test inet_lnaof normal
2124 * @tc.desc : [C- SOFTWARE -0200]
2125 */
2126 HWTEST_F(ActsNetTest, testInetLnaofNormal, Function | MediumTest | Level2)
2127 {
2128 in_addr_t rst;
2129 struct in_addr addr = {0};
2130 char cpAddrs[7][16] = {"10.58.212.100", "0.0.0.0", "255", "0.255.0", "255.0.0.0",
2131 "255.255.255.255", "257"};
2132 unsigned int expectList[7] = {13908490, 0, 0, 65280, 255, 255, 65536};
2133 for (int i = 0; i < 7; i++) {
2134 addr.s_addr = inet_network(cpAddrs[i]);
2135 rst = inet_lnaof(addr);
2136 EXPECT_EQ(expectList[i], rst);
2137 printf("[###]inet_lnaof %s: uns[%u],s[%d],hex[%x]\n", cpAddrs[i], rst, rst, rst);
2138 }
2139 }
2140
2141 /**
2142 * @tc.number : SUB_KERNEL_NET_4500
2143 * @tc.name : test inet_netof normal
2144 * @tc.desc : [C- SOFTWARE -0200]
2145 */
2146 HWTEST_F(ActsNetTest, testInetNetofNormal, Function | MediumTest | Level2)
2147 {
2148 in_addr_t rst;
2149 struct in_addr addr = {0};
2150 char cpAddrs[7][16] = {"10.58.212.100", "0.0.0.0", "255", "0.255.0", "255.0.0.0",
2151 "255.255.255.255", "257"};
2152 unsigned int expectList[7] = {100, 0, 16711680, 0, 0, 16777215, 1};
2153 for (int i = 0; i < 7; i++) {
2154 addr.s_addr = inet_network(cpAddrs[i]);
2155 rst = inet_netof(addr);
2156 EXPECT_EQ(expectList[i], rst);
2157 printf("[###]inet_netof %s: uns[%u],s[%d],hex[%x]\n", cpAddrs[i], rst, rst, rst);
2158 }
2159 }
2160
2161 /**
2162 * @tc.number : SUB_KERNEL_NET_4600
2163 * @tc.name : test inet_makeaddr normal
2164 * @tc.desc : [C- SOFTWARE -0200]
2165 */
2166 HWTEST_F(ActsNetTest, testInetMakeaddrNormal, Function | MediumTest | Level2)
2167 {
2168 struct in_addr addrRst = {0};
2169 int hostList[7] = {13908490, 0, 0, 65280, 255, 255, 65536};
2170 int netList[7] = {100, 0, 16711680, 0, 0, 16777215, 1};
2171 unsigned int expectList[7] = {171627620, 0, 255, 16711680, 4278190080, 4294967295, 257};
2172 for (int i = 0; i < 7; i++) {
2173 addrRst = inet_makeaddr(netList[i], hostList[i]);
2174 EXPECT_EQ(expectList[i], addrRst.s_addr);
2175 printf("[###]inet_makeaddr: uns[%u],s[%d],hex[%x]\n", addrRst.s_addr, addrRst.s_addr, addrRst.s_addr);
2176 }
2177 }
2178
2179 /**
2180 * @tc.number : SUB_KERNEL_NET_4700
2181 * @tc.name : test invalid parameter
2182 * @tc.desc : [C- SOFTWARE -0200]
2183 */
2184 HWTEST_F(ActsNetTest, testInvalidParameter, Function | MediumTest | Level3)
2185 {
2186 int ret;
2187 int fdFail = -1;
2188 int fdSuccess = -1;
2189
2190 fdFail = socket(0, 0, 0);
2191 EXPECT_EQ(fdFail, -1);
2192 fdSuccess = socket(AF_INET, SOCK_STREAM, 0);
2193 EXPECT_NE(fdSuccess, -1);
2194
2195 ret = ::bind(fdFail, nullptr, sizeof(struct sockaddr_in));
2196 EXPECT_EQ(-1, ret);
2197 ret = ::bind(fdSuccess, nullptr, sizeof(struct sockaddr_in));
2198 EXPECT_EQ(-1, ret);
2199
2200 ret = connect(fdFail, nullptr, sizeof(struct sockaddr));
2201 EXPECT_EQ(-1, ret);
2202 ret = connect(fdSuccess, nullptr, sizeof(struct sockaddr));
2203 EXPECT_EQ(-1, ret);
2204
2205 ret = accept(fdFail, nullptr, nullptr);
2206 EXPECT_EQ(-1, ret);
2207 ret = accept(fdSuccess, nullptr, nullptr);
2208 EXPECT_EQ(-1, ret);
2209
2210 ret = getsockname(fdFail, nullptr, nullptr);
2211 EXPECT_EQ(-1, ret);
2212 ret = getsockname(fdSuccess, nullptr, nullptr);
2213 EXPECT_EQ(-1, ret);
2214
2215 ret = getpeername(fdFail, nullptr, nullptr);
2216 EXPECT_EQ(-1, ret);
2217 ret = getpeername(fdSuccess, nullptr, nullptr);
2218 EXPECT_EQ(-1, ret);
2219
2220 ret = send(fdFail, nullptr, strlen(g_srvMsg), 0);
2221 EXPECT_EQ(-1, ret);
2222 ret = send(fdSuccess, nullptr, strlen(g_srvMsg), 0);
2223 EXPECT_EQ(-1, ret);
2224
2225 ret = sendto(fdFail, nullptr, strlen(g_srvMsg), 0, nullptr, (socklen_t)sizeof(struct sockaddr_in));
2226 EXPECT_EQ(-1, ret);
2227 ret = sendto(fdSuccess, nullptr, strlen(g_srvMsg), 0, nullptr, (socklen_t)sizeof(struct sockaddr_in));
2228 EXPECT_EQ(-1, ret);
2229
2230 ret = recv(fdFail, nullptr, sizeof(g_srvMsg), 0);
2231 EXPECT_EQ(-1, ret);
2232 ret = recv(fdSuccess, nullptr, sizeof(g_srvMsg), 0);
2233 EXPECT_EQ(-1, ret);
2234
2235 struct msghdr msg = {nullptr};
2236 ret = recvmsg(fdFail, &msg, 0);
2237 EXPECT_EQ(-1, ret);
2238 ret = recvmsg(fdSuccess, &msg, 0);
2239 EXPECT_EQ(-1, ret);
2240
2241 ret = recvfrom(fdFail, nullptr, sizeof(g_srvMsg), 0, nullptr, nullptr);
2242 EXPECT_EQ(-1, ret);
2243 ret = recvfrom(fdSuccess, nullptr, sizeof(g_srvMsg), 0, nullptr, nullptr);
2244 EXPECT_EQ(-1, ret);
2245
2246 ret = setsockopt(fdFail, SOL_SOCKET, SO_RCVTIMEO, nullptr, (socklen_t)sizeof(struct timeval));
2247 EXPECT_EQ(-1, ret);
2248 ret = setsockopt(fdSuccess, SOL_SOCKET, SO_RCVTIMEO, nullptr, (socklen_t)sizeof(struct timeval));
2249 EXPECT_EQ(-1, ret);
2250 ret = getsockopt(fdFail, SOL_SOCKET, SO_RCVTIMEO, nullptr, (socklen_t*)sizeof(struct timeval));
2251 EXPECT_EQ(-1, ret);
2252 ret = getsockopt(fdSuccess, SOL_SOCKET, SO_RCVTIMEO, nullptr, (socklen_t*)sizeof(struct timeval));
2253 EXPECT_EQ(-1, ret);
2254
2255 ret = sendmsg(fdFail, nullptr, 0);
2256 EXPECT_EQ(-1, ret);
2257 ret = sendmsg(fdSuccess, nullptr, 0);
2258 EXPECT_EQ(-1, ret);
2259
2260 ret = listen(fdFail, 0);
2261 EXPECT_EQ(-1, ret);
2262 ret = select(fdFail, nullptr, nullptr, nullptr, nullptr);
2263 EXPECT_EQ(-1, ret);
2264
2265 ret = shutdown(fdFail, SHUT_RD);
2266 EXPECT_EQ(-1, ret);
2267 ret = shutdown(fdSuccess, -1);
2268 EXPECT_EQ(-1, ret);
2269
2270 ret = close(fdSuccess);
2271 EXPECT_EQ(0, ret);
2272 }
2273