• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SRV_MSG "Hi, I am TCP server"
38 #define CLI_MSG "Hi, I am TCP client"
39 #define TEST_CASE 220
40 #define TEST_COUNT 100
41 #define STACK_PORT_TCP_DUP_START 3000
42 
43 static int g_portServer = STACK_PORT_TCP_DUP_START;
44 static int g_portClient = STACK_PORT_TCP_DUP_START;
45 
46 static char g_bufServer[BUF_SIZE + 1] = { 0 };
47 static char g_bufClient[BUF_SIZE + 1] = { 0 };
48 
SampleTcpServer()49 static int SampleTcpServer()
50 {
51     int sfd, lsfd;
52     struct sockaddr_in srvAddr = { 0 };
53     struct sockaddr_in clnAddr = { 0 };
54     socklen_t clnAddrLen = sizeof(clnAddr);
55     int ret;
56 
57     /* tcp server */
58     lsfd = socket(AF_INET, SOCK_STREAM, 0);
59     ICUNIT_ASSERT_NOT_EQUAL(lsfd, -1, 1);
60 
61     srvAddr.sin_family = AF_INET;
62     srvAddr.sin_addr.s_addr = inet_addr(STACK_IP);
63     srvAddr.sin_port = htons(g_portServer);
64     g_portServer++;
65     ret = bind(lsfd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
66     ICUNIT_ASSERT_EQUAL(ret, 0, 2);
67 
68     ret = listen(lsfd, 0);
69     ICUNIT_ASSERT_EQUAL(ret, 0, 3);
70 
71     sfd = accept(lsfd, (struct sockaddr*)&clnAddr, &clnAddrLen);
72     ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, 4);
73 
74     /* send */
75     (void)memset_s(g_bufServer, sizeof(g_bufServer), 0, sizeof(g_bufServer));
76     (void)strcpy_s(g_bufServer, sizeof(g_bufServer), SRV_MSG);
77     ret = send(sfd, g_bufServer, strlen(SRV_MSG), 0);
78     ICUNIT_ASSERT_EQUAL(ret, strlen(SRV_MSG), 5);
79 
80     ret = closesocket(sfd);
81     ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 10);
82     ret = closesocket(lsfd);
83     ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 11);
84 
85     return 0;
86 }
87 
SampleTcpClient()88 static int SampleTcpClient()
89 {
90     int sfd;
91     struct sockaddr_in srvAddr = { 0 };
92     int ret;
93 
94     /* tcp client connection */
95     sfd = socket(AF_INET, SOCK_STREAM, 0);
96     ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, 10);
97 
98     srvAddr.sin_family = AF_INET;
99     srvAddr.sin_addr.s_addr = inet_addr(PEER_IP);
100     srvAddr.sin_port = htons(g_portClient);
101     g_portClient++;
102     ret = connect(sfd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
103     ICUNIT_ASSERT_EQUAL(ret, 0, 11);
104 
105     /* recv */
106     (void)memset_s(g_bufClient, sizeof(g_bufClient), 0, sizeof(g_bufClient));
107     ret = recv(sfd, g_bufClient, sizeof(g_bufClient), 0);
108     ICUNIT_ASSERT_EQUAL(ret, strlen(SRV_MSG), 19);
109 
110     ret = closesocket(sfd);
111     ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 23);
112     return 0;
113 }
114 
TcpServerRoutine(void * p)115 static void TcpServerRoutine(void *p)
116 {
117     (void)p;
118     g_testCase++;
119 
120     int i;
121     for (i = 0; i < TEST_COUNT; i++) {
122         (void)SampleTcpServer();
123     }
124     LogPrintln("tcp server g_portServer = %d", g_portServer);
125 }
126 
TcpClientRoutine(void * p)127 static void TcpClientRoutine(void *p)
128 {
129     (void)p;
130     g_testCase++;
131 
132     int i;
133     for (i = 0; i < TEST_COUNT; i++) {
134         (void)SampleTcpClient();
135     }
136     LogPrintln("tcp server g_portClient = %d", g_portClient);
137 }
138 
TcpTestDup()139 void TcpTestDup()
140 {
141     LogPrintln("net_socket_test_012.c enter");
142     g_testCase = TEST_CASE;
143     int ret;
144     ret = sys_thread_new("tcp_server_dup", TcpServerRoutine, NULL,
145         STACK_TEST_SIZE, TCPIP_THREAD_PRIO);
146     ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 23);
147 
148     ret = sys_thread_new("tcp_client_dup", TcpClientRoutine, NULL,
149         STACK_TEST_SIZE, TCPIP_THREAD_PRIO);
150     ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 24);
151 }
152