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 "lwip_test.h" 33#include "lwipopts.h" 34#include <arch/sys_arch.h> 35#include <lwip/sys.h> 36 37#define TEST_CASE 230 38#define STACK_PORT_TCP_LONG 2231 39#define TCP_LONG_BUF_SIZE (2 * 1024) 40 41static int SampleTcpServer() 42{ 43 int sfd, lsfd; 44 struct sockaddr_in srvAddr = { 0 }; 45 struct sockaddr_in clnAddr = { 0 }; 46 socklen_t clnAddrLen = sizeof(clnAddr); 47 int ret; 48 static char serverBuf[TCP_LONG_BUF_SIZE] = {0}; 49 50 /* tcp server */ 51 lsfd = socket(AF_INET, SOCK_STREAM, 0); 52 ICUNIT_ASSERT_NOT_EQUAL(lsfd, -1, 1); 53 54 srvAddr.sin_family = AF_INET; 55 srvAddr.sin_addr.s_addr = inet_addr(STACK_IP); 56 srvAddr.sin_port = htons(STACK_PORT_TCP_LONG); 57 ret = bind(lsfd, (struct sockaddr*)&srvAddr, sizeof(srvAddr)); 58 ICUNIT_ASSERT_EQUAL(ret, 0, 2); 59 60 ret = listen(lsfd, 0); 61 ICUNIT_ASSERT_EQUAL(ret, 0, 3); 62 63 sfd = accept(lsfd, (struct sockaddr*)&clnAddr, &clnAddrLen); 64 ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, 4); 65 66 /* send */ 67 ret = send(sfd, serverBuf, TCP_LONG_BUF_SIZE, 0); 68 ICUNIT_ASSERT_EQUAL(ret, TCP_LONG_BUF_SIZE, 6); 69 70 ret = closesocket(sfd); 71 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 7); 72 ret = closesocket(lsfd); 73 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 8); 74 75 return 0; 76} 77 78static int SampleTcpClient() 79{ 80 int sfd; 81 struct sockaddr_in srvAddr = { 0 }; 82 int ret, i; 83 int recvCount = 0; 84 static char clientBuf[TCP_LONG_BUF_SIZE] = {0}; 85 86 /* tcp client connection */ 87 sfd = socket(AF_INET, SOCK_STREAM, 0); 88 ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, 9); 89 90 srvAddr.sin_family = AF_INET; 91 srvAddr.sin_addr.s_addr = inet_addr(PEER_IP); 92 srvAddr.sin_port = htons(STACK_PORT_TCP_LONG); 93 ret = connect(sfd, (struct sockaddr*)&srvAddr, sizeof(srvAddr)); 94 ICUNIT_ASSERT_EQUAL(ret, 0, 10); 95 96 /* recv */ 97 for (i = 0; i < TCP_LONG_BUF_SIZE; i++) { 98 ret = recv(sfd, clientBuf, TCP_LONG_BUF_SIZE, 0); 99 recvCount += ret; 100 if (recvCount >= TCP_LONG_BUF_SIZE) { 101 LogPrintln("client recv on socket %d: %d, i = %d", sfd, recvCount, i); 102 break; 103 } 104 } 105 ICUNIT_ASSERT_NOT_EQUAL(i, TCP_LONG_BUF_SIZE, 12); 106 ret = closesocket(sfd); 107 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 13); 108 return 0; 109} 110 111static void TcpServerRoutine(void *p) 112{ 113 (void)p; 114 g_testCase++; 115 (void)SampleTcpServer(); 116} 117 118static void TcpClientRoutine(void *p) 119{ 120 (void)p; 121 g_testCase++; 122 (void)SampleTcpClient(); 123} 124 125void TcpTestLong() 126{ 127 LogPrintln("net_socket_test_013.c enter"); 128 g_testCase = TEST_CASE; 129 int ret; 130 ret = sys_thread_new("tcp_server_long", TcpServerRoutine, NULL, 131 STACK_TEST_SIZE, TCPIP_THREAD_PRIO); 132 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 23); 133 134 ret = sys_thread_new("tcp_client_long", TcpClientRoutine, NULL, 135 STACK_TEST_SIZE, TCPIP_THREAD_PRIO); 136 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 24); 137} 138