13d8536b4Sopenharmony_ci/* 23d8536b4Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. 33d8536b4Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. 43d8536b4Sopenharmony_ci * 53d8536b4Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification, 63d8536b4Sopenharmony_ci * are permitted provided that the following conditions are met: 73d8536b4Sopenharmony_ci * 83d8536b4Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of 93d8536b4Sopenharmony_ci * conditions and the following disclaimer. 103d8536b4Sopenharmony_ci * 113d8536b4Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list 123d8536b4Sopenharmony_ci * of conditions and the following disclaimer in the documentation and/or other materials 133d8536b4Sopenharmony_ci * provided with the distribution. 143d8536b4Sopenharmony_ci * 153d8536b4Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used 163d8536b4Sopenharmony_ci * to endorse or promote products derived from this software without specific prior written 173d8536b4Sopenharmony_ci * permission. 183d8536b4Sopenharmony_ci * 193d8536b4Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 203d8536b4Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 213d8536b4Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 223d8536b4Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 233d8536b4Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 243d8536b4Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 253d8536b4Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 263d8536b4Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 273d8536b4Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 283d8536b4Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 293d8536b4Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 303d8536b4Sopenharmony_ci */ 313d8536b4Sopenharmony_ci 323d8536b4Sopenharmony_ci#include "lwip_test.h" 333d8536b4Sopenharmony_ci#include "lwipopts.h" 343d8536b4Sopenharmony_ci#include <arch/sys_arch.h> 353d8536b4Sopenharmony_ci#include <lwip/sys.h> 363d8536b4Sopenharmony_ci 373d8536b4Sopenharmony_ci#define SRV_MSG "Hi, I am TCP server" 383d8536b4Sopenharmony_ci#define CLI_MSG "Hi, I am TCP client" 393d8536b4Sopenharmony_ci#define TEST_CASE 130 403d8536b4Sopenharmony_ci 413d8536b4Sopenharmony_cistatic char g_buf[BUF_SIZE + 1] = { 0 }; 423d8536b4Sopenharmony_ci 433d8536b4Sopenharmony_cistatic int SampleTcpServer() 443d8536b4Sopenharmony_ci{ 453d8536b4Sopenharmony_ci g_testCase++; 463d8536b4Sopenharmony_ci int sfd, lsfd; 473d8536b4Sopenharmony_ci struct sockaddr_in srvAddr = { 0 }; 483d8536b4Sopenharmony_ci struct sockaddr_in clnAddr = { 0 }; 493d8536b4Sopenharmony_ci socklen_t clnAddrLen = sizeof(clnAddr); 503d8536b4Sopenharmony_ci struct msghdr msg = { 0 }; 513d8536b4Sopenharmony_ci struct iovec iov[IOV_LENGTH] = { 0 }; 523d8536b4Sopenharmony_ci int ret; 533d8536b4Sopenharmony_ci 543d8536b4Sopenharmony_ci /* tcp server */ 553d8536b4Sopenharmony_ci lsfd = socket(AF_INET, SOCK_STREAM, 0); 563d8536b4Sopenharmony_ci LogPrintln("create server socket inet stream: %d", lsfd); 573d8536b4Sopenharmony_ci ICUNIT_ASSERT_NOT_EQUAL(lsfd, -1, 1); 583d8536b4Sopenharmony_ci 593d8536b4Sopenharmony_ci srvAddr.sin_family = AF_INET; 603d8536b4Sopenharmony_ci srvAddr.sin_addr.s_addr = inet_addr(STACK_IP); 613d8536b4Sopenharmony_ci srvAddr.sin_port = htons(STACK_PORT); 623d8536b4Sopenharmony_ci ret = bind(lsfd, (struct sockaddr*)&srvAddr, sizeof(srvAddr)); 633d8536b4Sopenharmony_ci LogPrintln("bind socket %d to %s:%d: %d", lsfd, inet_ntoa(srvAddr.sin_addr), ntohs(srvAddr.sin_port), ret); 643d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, 2); 653d8536b4Sopenharmony_ci 663d8536b4Sopenharmony_ci ret = listen(lsfd, 0); 673d8536b4Sopenharmony_ci LogPrintln("listen socket %d: %d", lsfd, ret); 683d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, 3); 693d8536b4Sopenharmony_ci 703d8536b4Sopenharmony_ci sfd = accept(lsfd, (struct sockaddr*)&clnAddr, &clnAddrLen); 713d8536b4Sopenharmony_ci LogPrintln("accept socket %d: %d <%s:%d>", lsfd, sfd, inet_ntoa(clnAddr.sin_addr), ntohs(clnAddr.sin_port)); 723d8536b4Sopenharmony_ci ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, 4); 733d8536b4Sopenharmony_ci 743d8536b4Sopenharmony_ci /* send */ 753d8536b4Sopenharmony_ci (void)memset_s(g_buf, sizeof(g_buf), 0, sizeof(g_buf)); 763d8536b4Sopenharmony_ci (void)strcpy_s(g_buf, sizeof(g_buf), SRV_MSG); 773d8536b4Sopenharmony_ci ret = send(sfd, g_buf, strlen(SRV_MSG), 0); 783d8536b4Sopenharmony_ci LogPrintln("server send on socket %d: %d", sfd, ret); 793d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, strlen(SRV_MSG), 5); 803d8536b4Sopenharmony_ci 813d8536b4Sopenharmony_ci /* recv */ 823d8536b4Sopenharmony_ci (void)memset_s(g_buf, sizeof(g_buf), 0, sizeof(g_buf)); 833d8536b4Sopenharmony_ci ret = recv(sfd, g_buf, sizeof(g_buf), 0); 843d8536b4Sopenharmony_ci LogPrintln("server recv on socket %d: %d", sfd, ret); 853d8536b4Sopenharmony_ci LogPrintln("ser:%s", g_buf); 863d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, strlen(CLI_MSG), 6); 873d8536b4Sopenharmony_ci 883d8536b4Sopenharmony_ci /* sendmsg */ 893d8536b4Sopenharmony_ci clnAddr.sin_family = AF_INET; 903d8536b4Sopenharmony_ci clnAddr.sin_addr.s_addr = inet_addr(PEER_IP); 913d8536b4Sopenharmony_ci clnAddr.sin_port = htons(PEER_PORT); 923d8536b4Sopenharmony_ci (void)memset_s(g_buf, sizeof(g_buf), 0, sizeof(g_buf)); 933d8536b4Sopenharmony_ci (void)strcpy_s(g_buf, sizeof(g_buf), SRV_MSG); 943d8536b4Sopenharmony_ci msg.msg_name = &clnAddr; 953d8536b4Sopenharmony_ci msg.msg_namelen = sizeof(clnAddr); 963d8536b4Sopenharmony_ci msg.msg_iov = iov; 973d8536b4Sopenharmony_ci msg.msg_iovlen = IOV_LENGTH; 983d8536b4Sopenharmony_ci iov[0].iov_base = g_buf; 993d8536b4Sopenharmony_ci iov[0].iov_len = strlen(SRV_MSG); 1003d8536b4Sopenharmony_ci iov[1].iov_base = g_buf; 1013d8536b4Sopenharmony_ci iov[1].iov_len = strlen(SRV_MSG); 1023d8536b4Sopenharmony_ci ret = sendmsg(sfd, &msg, 0); 1033d8536b4Sopenharmony_ci LogPrintln("sendmsg on socket %d: %d", sfd, ret); 1043d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, IOV_LENGTH * strlen(SRV_MSG), 7); 1053d8536b4Sopenharmony_ci 1063d8536b4Sopenharmony_ci /* recvmsg */ 1073d8536b4Sopenharmony_ci (void)memset_s(g_buf, sizeof(g_buf), 0, sizeof(g_buf)); 1083d8536b4Sopenharmony_ci (void)memset_s(&msg, sizeof(msg), 0, sizeof(msg)); 1093d8536b4Sopenharmony_ci msg.msg_name = &clnAddr; 1103d8536b4Sopenharmony_ci msg.msg_namelen = sizeof(clnAddr); 1113d8536b4Sopenharmony_ci msg.msg_iov = iov; 1123d8536b4Sopenharmony_ci msg.msg_iovlen = 1; 1133d8536b4Sopenharmony_ci iov[0].iov_base = g_buf; 1143d8536b4Sopenharmony_ci iov[0].iov_len = sizeof(g_buf); 1153d8536b4Sopenharmony_ci ret = recvmsg(sfd, &msg, 0); 1163d8536b4Sopenharmony_ci LogPrintln("recvmsg on socket %d: %d", sfd, ret); 1173d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, IOV_LENGTH * strlen(CLI_MSG), 8); 1183d8536b4Sopenharmony_ci 1193d8536b4Sopenharmony_ci ret = shutdown(sfd, SHUT_RDWR); 1203d8536b4Sopenharmony_ci LogPrintln("shutdown socket %d: %d", sfd, ret); 1213d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, 9); 1223d8536b4Sopenharmony_ci 1233d8536b4Sopenharmony_ci ret = closesocket(sfd); 1243d8536b4Sopenharmony_ci ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 10); 1253d8536b4Sopenharmony_ci ret = closesocket(lsfd); 1263d8536b4Sopenharmony_ci ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 11); 1273d8536b4Sopenharmony_ci 1283d8536b4Sopenharmony_ci return 0; 1293d8536b4Sopenharmony_ci} 1303d8536b4Sopenharmony_ci 1313d8536b4Sopenharmony_cistatic int SampleTcpClient() 1323d8536b4Sopenharmony_ci{ 1333d8536b4Sopenharmony_ci g_testCase++; 1343d8536b4Sopenharmony_ci int sfd; 1353d8536b4Sopenharmony_ci struct sockaddr_in srvAddr = { 0 }; 1363d8536b4Sopenharmony_ci struct sockaddr_in clnAddr = { 0 }; 1373d8536b4Sopenharmony_ci int ret; 1383d8536b4Sopenharmony_ci struct msghdr msg = { 0 }; 1393d8536b4Sopenharmony_ci struct iovec iov[IOV_LENGTH] = { 0 }; 1403d8536b4Sopenharmony_ci struct sockaddr addr; 1413d8536b4Sopenharmony_ci socklen_t addrLen = sizeof(addr); 1423d8536b4Sopenharmony_ci 1433d8536b4Sopenharmony_ci /* tcp client connection */ 1443d8536b4Sopenharmony_ci sfd = socket(AF_INET, SOCK_STREAM, 0); 1453d8536b4Sopenharmony_ci LogPrintln("create client socket inet stream: %d", sfd); 1463d8536b4Sopenharmony_ci ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, 10); 1473d8536b4Sopenharmony_ci 1483d8536b4Sopenharmony_ci srvAddr.sin_family = AF_INET; 1493d8536b4Sopenharmony_ci srvAddr.sin_addr.s_addr = inet_addr(PEER_IP); 1503d8536b4Sopenharmony_ci srvAddr.sin_port = htons(PEER_PORT); 1513d8536b4Sopenharmony_ci ret = connect(sfd, (struct sockaddr*)&srvAddr, sizeof(srvAddr)); 1523d8536b4Sopenharmony_ci LogPrintln("connect socket %d to %s:%d: %d", sfd, inet_ntoa(srvAddr.sin_addr), ntohs(srvAddr.sin_port), ret); 1533d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, 11); 1543d8536b4Sopenharmony_ci 1553d8536b4Sopenharmony_ci /* test getpeername */ 1563d8536b4Sopenharmony_ci ret = getpeername(sfd, &addr, &addrLen); 1573d8536b4Sopenharmony_ci LogPrintln("getpeername %d %s:%d: %d", \ 1583d8536b4Sopenharmony_ci sfd, inet_ntoa(((struct sockaddr_in*)&addr)->sin_addr), ntohs(((struct sockaddr_in*)&addr)->sin_port), ret); 1593d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, 12); 1603d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(addrLen, sizeof(struct sockaddr_in), 13); 1613d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(((struct sockaddr_in*)&addr)->sin_addr.s_addr, \ 1623d8536b4Sopenharmony_ci inet_addr(PEER_IP), 14); 1633d8536b4Sopenharmony_ci 1643d8536b4Sopenharmony_ci /* test getsockname */ 1653d8536b4Sopenharmony_ci ret = getsockname(sfd, &addr, &addrLen); 1663d8536b4Sopenharmony_ci LogPrintln("getsockname %d %s:%d: %d", \ 1673d8536b4Sopenharmony_ci sfd, inet_ntoa(((struct sockaddr_in*)&addr)->sin_addr), ntohs(((struct sockaddr_in*)&addr)->sin_port), ret); 1683d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, 15); 1693d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(addrLen, sizeof(struct sockaddr_in), 16); 1703d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(((struct sockaddr_in*)&addr)->sin_addr.s_addr, \ 1713d8536b4Sopenharmony_ci inet_addr(STACK_IP), 17); 1723d8536b4Sopenharmony_ci 1733d8536b4Sopenharmony_ci /* send */ 1743d8536b4Sopenharmony_ci (void)memset_s(g_buf, sizeof(g_buf), 0, sizeof(g_buf)); 1753d8536b4Sopenharmony_ci (void)strcpy_s(g_buf, sizeof(g_buf), CLI_MSG); 1763d8536b4Sopenharmony_ci ret = send(sfd, g_buf, strlen(CLI_MSG), 0); 1773d8536b4Sopenharmony_ci LogPrintln("client send on socket %d: %d", sfd, ret); 1783d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, strlen(CLI_MSG), 18); 1793d8536b4Sopenharmony_ci 1803d8536b4Sopenharmony_ci /* recv */ 1813d8536b4Sopenharmony_ci (void)memset_s(g_buf, sizeof(g_buf), 0, sizeof(g_buf)); 1823d8536b4Sopenharmony_ci ret = recv(sfd, g_buf, sizeof(g_buf), 0); 1833d8536b4Sopenharmony_ci LogPrintln("client recv on socket %d: %d", sfd, ret); 1843d8536b4Sopenharmony_ci LogPrintln("cli:%s", g_buf); 1853d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, strlen(SRV_MSG), 19); 1863d8536b4Sopenharmony_ci 1873d8536b4Sopenharmony_ci /* sendmsg */ 1883d8536b4Sopenharmony_ci clnAddr.sin_family = AF_INET; 1893d8536b4Sopenharmony_ci clnAddr.sin_addr.s_addr = inet_addr(PEER_IP); 1903d8536b4Sopenharmony_ci clnAddr.sin_port = htons(PEER_PORT); 1913d8536b4Sopenharmony_ci (void)memset_s(g_buf, sizeof(g_buf), 0, sizeof(g_buf)); 1923d8536b4Sopenharmony_ci (void)strcpy_s(g_buf, sizeof(g_buf), CLI_MSG); 1933d8536b4Sopenharmony_ci msg.msg_name = &clnAddr; 1943d8536b4Sopenharmony_ci msg.msg_namelen = sizeof(clnAddr); 1953d8536b4Sopenharmony_ci msg.msg_iov = iov; 1963d8536b4Sopenharmony_ci msg.msg_iovlen = IOV_LENGTH; 1973d8536b4Sopenharmony_ci iov[0].iov_base = g_buf; 1983d8536b4Sopenharmony_ci iov[0].iov_len = strlen(CLI_MSG); 1993d8536b4Sopenharmony_ci iov[1].iov_base = g_buf; 2003d8536b4Sopenharmony_ci iov[1].iov_len = strlen(CLI_MSG); 2013d8536b4Sopenharmony_ci ret = sendmsg(sfd, &msg, 0); 2023d8536b4Sopenharmony_ci LogPrintln("sendmsg on socket %d: %d", sfd, ret); 2033d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 2 * strlen(CLI_MSG), 20); 2043d8536b4Sopenharmony_ci 2053d8536b4Sopenharmony_ci /* recvmsg */ 2063d8536b4Sopenharmony_ci (void)memset_s(g_buf, sizeof(g_buf), 0, sizeof(g_buf)); 2073d8536b4Sopenharmony_ci (void)memset_s(&msg, sizeof(msg), 0, sizeof(msg)); 2083d8536b4Sopenharmony_ci msg.msg_name = &clnAddr; 2093d8536b4Sopenharmony_ci msg.msg_namelen = sizeof(clnAddr); 2103d8536b4Sopenharmony_ci msg.msg_iov = iov; 2113d8536b4Sopenharmony_ci msg.msg_iovlen = 1; 2123d8536b4Sopenharmony_ci iov[0].iov_base = g_buf; 2133d8536b4Sopenharmony_ci iov[0].iov_len = sizeof(g_buf); 2143d8536b4Sopenharmony_ci ret = recvmsg(sfd, &msg, 0); 2153d8536b4Sopenharmony_ci LogPrintln("recvmsg on socket %d: %d", sfd, ret); 2163d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, IOV_LENGTH * strlen(SRV_MSG), 21); 2173d8536b4Sopenharmony_ci 2183d8536b4Sopenharmony_ci ret = shutdown(sfd, SHUT_RDWR); 2193d8536b4Sopenharmony_ci LogPrintln("shutdown socket %d: %d", sfd, ret); 2203d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, 22); 2213d8536b4Sopenharmony_ci 2223d8536b4Sopenharmony_ci ret = closesocket(sfd); 2233d8536b4Sopenharmony_ci ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 23); 2243d8536b4Sopenharmony_ci return 0; 2253d8536b4Sopenharmony_ci} 2263d8536b4Sopenharmony_ci 2273d8536b4Sopenharmony_cistatic void TcpServerRoutine(void *p) 2283d8536b4Sopenharmony_ci{ 2293d8536b4Sopenharmony_ci (void)p; 2303d8536b4Sopenharmony_ci (void)SampleTcpServer(); 2313d8536b4Sopenharmony_ci} 2323d8536b4Sopenharmony_ci 2333d8536b4Sopenharmony_cistatic void TcpClientRoutine(void *p) 2343d8536b4Sopenharmony_ci{ 2353d8536b4Sopenharmony_ci (void)p; 2363d8536b4Sopenharmony_ci (void)SampleTcpClient(); 2373d8536b4Sopenharmony_ci} 2383d8536b4Sopenharmony_ci 2393d8536b4Sopenharmony_civoid TcpTest() 2403d8536b4Sopenharmony_ci{ 2413d8536b4Sopenharmony_ci LogPrintln("net_socket_test_003.c enter"); 2423d8536b4Sopenharmony_ci g_testCase = TEST_CASE; 2433d8536b4Sopenharmony_ci int ret; 2443d8536b4Sopenharmony_ci ret = sys_thread_new("tcp_server", TcpServerRoutine, NULL, 2453d8536b4Sopenharmony_ci STACK_TEST_SIZE, TCPIP_THREAD_PRIO); 2463d8536b4Sopenharmony_ci ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 23); 2473d8536b4Sopenharmony_ci 2483d8536b4Sopenharmony_ci ret = sys_thread_new("tcp_client", TcpClientRoutine, NULL, 2493d8536b4Sopenharmony_ci STACK_TEST_SIZE, TCPIP_THREAD_PRIO); 2503d8536b4Sopenharmony_ci ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 24); 2513d8536b4Sopenharmony_ci} 252