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
40 #define TEST_CASE 200
41 #define STACK_PORT_10 2230
42 #define POLL_OPEN_MAX 10
43 #define TIME_OUT (1000 * 10) // timeout 10s
44 static char g_buf[BUF_SIZE + 1] = { 0 };
45
SampleTcpServer()46 static int SampleTcpServer()
47 {
48 #if LWIP_SOCKET_POLL
49 g_testCase++;
50 int sfd, lsfd;
51 struct sockaddr_in srvAddr = { 0 };
52 struct sockaddr_in clnAddr = { 0 };
53 socklen_t clnAddrLen = sizeof(clnAddr);
54 struct pollfd client[POLL_OPEN_MAX] = {0};
55 int ret;
56
57 /* tcp server */
58 lsfd = socket(AF_INET, SOCK_STREAM, 0);
59 LogPrintln("create server socket inet stream: %d", lsfd);
60 ICUNIT_ASSERT_NOT_EQUAL(lsfd, -1, 1);
61
62 srvAddr.sin_family = AF_INET;
63 srvAddr.sin_addr.s_addr = inet_addr(STACK_IP);
64 srvAddr.sin_port = htons(STACK_PORT_10);
65 ret = bind(lsfd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
66 LogPrintln("bind socket %d to %s:%d: %d", lsfd, inet_ntoa(srvAddr.sin_addr), ntohs(srvAddr.sin_port), ret);
67 ICUNIT_ASSERT_EQUAL(ret, 0, 2);
68
69 ret = listen(lsfd, 0);
70 LogPrintln("listen socket %d: %d", lsfd, ret);
71 ICUNIT_ASSERT_EQUAL(ret, 0, 3);
72
73 client[0].fd = lsfd;
74 client[0].events = POLLIN;
75 ret = poll(client, 1, TIME_OUT);
76 if (ret < 0) {
77 LogPrintln("poll error");
78 ICUNIT_ASSERT_EQUAL(-1, 0, 4);
79 } else if (ret == 0) {
80 LogPrintln("poll timeout");
81 ICUNIT_ASSERT_EQUAL(-1, 0, 5);
82 }
83
84 sfd = accept(lsfd, (struct sockaddr*)&clnAddr, &clnAddrLen);
85 LogPrintln("accept socket %d: %d <%s:%d>", lsfd, sfd, inet_ntoa(clnAddr.sin_addr), ntohs(clnAddr.sin_port));
86 ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, 6);
87
88 /* recv */
89 (void)memset_s(g_buf, sizeof(g_buf), 0, sizeof(g_buf));
90 ret = recv(sfd, g_buf, sizeof(g_buf), 0);
91 LogPrintln("server recv on socket %d: %d", sfd, ret);
92 LogPrintln("ser:%s", g_buf);
93 ICUNIT_ASSERT_EQUAL(ret, strlen(CLI_MSG), 7);
94
95 ret = closesocket(sfd);
96 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 8);
97 ret = closesocket(lsfd);
98 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 9);
99 #endif
100 return 0;
101 }
102
SampleTcpClient()103 static int SampleTcpClient()
104 {
105 #if LWIP_SOCKET_POLL
106 g_testCase++;
107 int sfd;
108 struct sockaddr_in srvAddr = { 0 };
109 int ret;
110
111 /* tcp client connection */
112 sfd = socket(AF_INET, SOCK_STREAM, 0);
113 LogPrintln("create client socket inet stream: %d", sfd);
114 ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, 10);
115
116 srvAddr.sin_family = AF_INET;
117 srvAddr.sin_addr.s_addr = inet_addr(PEER_IP);
118 srvAddr.sin_port = htons(STACK_PORT_10);
119 ret = connect(sfd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
120 LogPrintln("connect socket %d to %s:%d: %d", sfd, inet_ntoa(srvAddr.sin_addr), ntohs(srvAddr.sin_port), ret);
121 ICUNIT_ASSERT_EQUAL(ret, 0, 11);
122
123 /* send */
124 (void)memset_s(g_buf, sizeof(g_buf), 0, sizeof(g_buf));
125 (void)strcpy_s(g_buf, sizeof(g_buf), CLI_MSG);
126 ret = send(sfd, g_buf, strlen(CLI_MSG), 0);
127 LogPrintln("client send on socket %d: %d", sfd, ret);
128 ICUNIT_ASSERT_EQUAL(ret, strlen(CLI_MSG), 12);
129
130 ret = closesocket(sfd);
131 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 13);
132 #endif
133 return 0;
134 }
135
TcpServerRoutine(void * p)136 static void TcpServerRoutine(void *p)
137 {
138 (void)p;
139 (void)SampleTcpServer();
140 }
141
TcpClientRoutine(void * p)142 static void TcpClientRoutine(void *p)
143 {
144 (void)p;
145 (void)SampleTcpClient();
146 }
147
TcpTestPoll()148 void TcpTestPoll()
149 {
150 LogPrintln("net_socket_test_010.c enter");
151 g_testCase = TEST_CASE;
152 int ret;
153 ret = sys_thread_new("tcp_server_poll", TcpServerRoutine, NULL,
154 STACK_TEST_SIZE, TCPIP_THREAD_PRIO);
155 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 23);
156
157 ret = sys_thread_new("tcp_client_poll", TcpClientRoutine, NULL,
158 STACK_TEST_SIZE, TCPIP_THREAD_PRIO);
159 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 24);
160 }
161