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 220
403d8536b4Sopenharmony_ci#define TEST_COUNT 100
413d8536b4Sopenharmony_ci#define STACK_PORT_TCP_DUP_START 3000
423d8536b4Sopenharmony_ci
433d8536b4Sopenharmony_cistatic int g_portServer = STACK_PORT_TCP_DUP_START;
443d8536b4Sopenharmony_cistatic int g_portClient = STACK_PORT_TCP_DUP_START;
453d8536b4Sopenharmony_ci
463d8536b4Sopenharmony_cistatic char g_bufServer[BUF_SIZE + 1] = { 0 };
473d8536b4Sopenharmony_cistatic char g_bufClient[BUF_SIZE + 1] = { 0 };
483d8536b4Sopenharmony_ci
493d8536b4Sopenharmony_cistatic int SampleTcpServer()
503d8536b4Sopenharmony_ci{
513d8536b4Sopenharmony_ci    int sfd, lsfd;
523d8536b4Sopenharmony_ci    struct sockaddr_in srvAddr = { 0 };
533d8536b4Sopenharmony_ci    struct sockaddr_in clnAddr = { 0 };
543d8536b4Sopenharmony_ci    socklen_t clnAddrLen = sizeof(clnAddr);
553d8536b4Sopenharmony_ci    int ret;
563d8536b4Sopenharmony_ci
573d8536b4Sopenharmony_ci    /* tcp server */
583d8536b4Sopenharmony_ci    lsfd = socket(AF_INET, SOCK_STREAM, 0);
593d8536b4Sopenharmony_ci    ICUNIT_ASSERT_NOT_EQUAL(lsfd, -1, 1);
603d8536b4Sopenharmony_ci
613d8536b4Sopenharmony_ci    srvAddr.sin_family = AF_INET;
623d8536b4Sopenharmony_ci    srvAddr.sin_addr.s_addr = inet_addr(STACK_IP);
633d8536b4Sopenharmony_ci    srvAddr.sin_port = htons(g_portServer);
643d8536b4Sopenharmony_ci    g_portServer++;
653d8536b4Sopenharmony_ci    ret = bind(lsfd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
663d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, 2);
673d8536b4Sopenharmony_ci
683d8536b4Sopenharmony_ci    ret = listen(lsfd, 0);
693d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, 3);
703d8536b4Sopenharmony_ci
713d8536b4Sopenharmony_ci    sfd = accept(lsfd, (struct sockaddr*)&clnAddr, &clnAddrLen);
723d8536b4Sopenharmony_ci    ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, 4);
733d8536b4Sopenharmony_ci
743d8536b4Sopenharmony_ci    /* send */
753d8536b4Sopenharmony_ci    (void)memset_s(g_bufServer, sizeof(g_bufServer), 0, sizeof(g_bufServer));
763d8536b4Sopenharmony_ci    (void)strcpy_s(g_bufServer, sizeof(g_bufServer), SRV_MSG);
773d8536b4Sopenharmony_ci    ret = send(sfd, g_bufServer, strlen(SRV_MSG), 0);
783d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, strlen(SRV_MSG), 5);
793d8536b4Sopenharmony_ci
803d8536b4Sopenharmony_ci    ret = closesocket(sfd);
813d8536b4Sopenharmony_ci    ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 10);
823d8536b4Sopenharmony_ci    ret = closesocket(lsfd);
833d8536b4Sopenharmony_ci    ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 11);
843d8536b4Sopenharmony_ci
853d8536b4Sopenharmony_ci    return 0;
863d8536b4Sopenharmony_ci}
873d8536b4Sopenharmony_ci
883d8536b4Sopenharmony_cistatic int SampleTcpClient()
893d8536b4Sopenharmony_ci{
903d8536b4Sopenharmony_ci    int sfd;
913d8536b4Sopenharmony_ci    struct sockaddr_in srvAddr = { 0 };
923d8536b4Sopenharmony_ci    int ret;
933d8536b4Sopenharmony_ci
943d8536b4Sopenharmony_ci    /* tcp client connection */
953d8536b4Sopenharmony_ci    sfd = socket(AF_INET, SOCK_STREAM, 0);
963d8536b4Sopenharmony_ci    ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, 10);
973d8536b4Sopenharmony_ci
983d8536b4Sopenharmony_ci    srvAddr.sin_family = AF_INET;
993d8536b4Sopenharmony_ci    srvAddr.sin_addr.s_addr = inet_addr(PEER_IP);
1003d8536b4Sopenharmony_ci    srvAddr.sin_port = htons(g_portClient);
1013d8536b4Sopenharmony_ci    g_portClient++;
1023d8536b4Sopenharmony_ci    ret = connect(sfd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
1033d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, 11);
1043d8536b4Sopenharmony_ci
1053d8536b4Sopenharmony_ci    /* recv */
1063d8536b4Sopenharmony_ci    (void)memset_s(g_bufClient, sizeof(g_bufClient), 0, sizeof(g_bufClient));
1073d8536b4Sopenharmony_ci    ret = recv(sfd, g_bufClient, sizeof(g_bufClient), 0);
1083d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, strlen(SRV_MSG), 19);
1093d8536b4Sopenharmony_ci
1103d8536b4Sopenharmony_ci    ret = closesocket(sfd);
1113d8536b4Sopenharmony_ci    ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 23);
1123d8536b4Sopenharmony_ci    return 0;
1133d8536b4Sopenharmony_ci}
1143d8536b4Sopenharmony_ci
1153d8536b4Sopenharmony_cistatic void TcpServerRoutine(void *p)
1163d8536b4Sopenharmony_ci{
1173d8536b4Sopenharmony_ci    (void)p;
1183d8536b4Sopenharmony_ci    g_testCase++;
1193d8536b4Sopenharmony_ci
1203d8536b4Sopenharmony_ci    int i;
1213d8536b4Sopenharmony_ci    for (i = 0; i < TEST_COUNT; i++) {
1223d8536b4Sopenharmony_ci        (void)SampleTcpServer();
1233d8536b4Sopenharmony_ci    }
1243d8536b4Sopenharmony_ci    LogPrintln("tcp server g_portServer = %d", g_portServer);
1253d8536b4Sopenharmony_ci}
1263d8536b4Sopenharmony_ci
1273d8536b4Sopenharmony_cistatic void TcpClientRoutine(void *p)
1283d8536b4Sopenharmony_ci{
1293d8536b4Sopenharmony_ci    (void)p;
1303d8536b4Sopenharmony_ci    g_testCase++;
1313d8536b4Sopenharmony_ci
1323d8536b4Sopenharmony_ci    int i;
1333d8536b4Sopenharmony_ci    for (i = 0; i < TEST_COUNT; i++) {
1343d8536b4Sopenharmony_ci        (void)SampleTcpClient();
1353d8536b4Sopenharmony_ci    }
1363d8536b4Sopenharmony_ci    LogPrintln("tcp server g_portClient = %d", g_portClient);
1373d8536b4Sopenharmony_ci}
1383d8536b4Sopenharmony_ci
1393d8536b4Sopenharmony_civoid TcpTestDup()
1403d8536b4Sopenharmony_ci{
1413d8536b4Sopenharmony_ci    LogPrintln("net_socket_test_012.c enter");
1423d8536b4Sopenharmony_ci    g_testCase = TEST_CASE;
1433d8536b4Sopenharmony_ci    int ret;
1443d8536b4Sopenharmony_ci    ret = sys_thread_new("tcp_server_dup", TcpServerRoutine, NULL,
1453d8536b4Sopenharmony_ci        STACK_TEST_SIZE, TCPIP_THREAD_PRIO);
1463d8536b4Sopenharmony_ci    ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 23);
1473d8536b4Sopenharmony_ci
1483d8536b4Sopenharmony_ci    ret = sys_thread_new("tcp_client_dup", TcpClientRoutine, NULL,
1493d8536b4Sopenharmony_ci        STACK_TEST_SIZE, TCPIP_THREAD_PRIO);
1503d8536b4Sopenharmony_ci    ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 24);
1513d8536b4Sopenharmony_ci}
152