• 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 <netinet/in.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <arpa/inet.h>
37 #include <unistd.h>
38 #include <osTest.h>
39 
40 #define localhost "127.0.0.1"
41 #define STACK_IP localhost
42 #define STACK_PORT 2277
43 #define PEER_PORT STACK_PORT
44 #define PEER_IP localhost
45 #define BUF_SIZE (1024 * 8)
46 #define SRV_MSG "Hi, I am TCP server"
47 #define CLI_MSG "Hi, I am TCP client"
48 
49 static pthread_barrier_t gBarrier;
50 #define Wait() pthread_barrier_wait(&gBarrier)
51 
SampleTcpServer()52 static int SampleTcpServer()
53 {
54     static char gBuf[BUF_SIZE + 1] = { 0 };
55     int sfd = -1, lsfd = -1;
56     struct sockaddr_in srvAddr = { 0 };
57     struct sockaddr_in clnAddr = { 0 };
58     socklen_t clnAddrLen = sizeof(clnAddr);
59     struct msghdr msg = { 0 };
60     struct iovec iov[2] = { };
61     int ret = 0, i = 0;
62 
63     /* tcp server */
64     lsfd = socket(AF_INET, SOCK_STREAM, 0);
65     LogPrintln("create listen socket inet stream: %d", lsfd);
66     ICUNIT_ASSERT_NOT_EQUAL(lsfd, -1, lsfd);
67 
68     srvAddr.sin_family = AF_INET;
69     srvAddr.sin_addr.s_addr = inet_addr(STACK_IP);
70     srvAddr.sin_port = htons(STACK_PORT);
71     ret = bind(lsfd, reinterpret_cast<struct sockaddr *>(&srvAddr), sizeof(srvAddr));
72     LogPrintln("bind socket %d to %s:%d: %d", lsfd, inet_ntoa(srvAddr.sin_addr), ntohs(srvAddr.sin_port), ret);
73     ICUNIT_ASSERT_EQUAL(ret, 0, Wait() + ret);
74 
75     ret = listen(lsfd, 0);
76     LogPrintln("listen socket %d: %d", lsfd, ret);
77     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
78 
79     Wait();
80 
81     sfd = accept(lsfd, reinterpret_cast<struct sockaddr *>(&clnAddr), &clnAddrLen);
82     LogPrintln("accept socket %d: %d <%s:%d>", lsfd, sfd, inet_ntoa(clnAddr.sin_addr), ntohs(clnAddr.sin_port));
83     ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, sfd);
84 
85     /* send */
86     ret = memset_s(gBuf, BUF_SIZE + 1, 0, BUF_SIZE + 1);
87     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
88     gBuf[BUF_SIZE - 1] = '\0';
89     ret = strcpy_s(gBuf, BUF_SIZE - 1, SRV_MSG);
90     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
91     gBuf[BUF_SIZE - 1] = '\0';
92     ret = send(sfd, gBuf, strlen(SRV_MSG), 0);
93     LogPrintln("send on socket %d: %d", sfd, ret);
94     ICUNIT_ASSERT_EQUAL(ret, strlen(SRV_MSG), ret);
95 
96     /* recv */
97     ret = memset_s(gBuf, BUF_SIZE + 1, 0, BUF_SIZE + 1);
98     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
99     gBuf[BUF_SIZE - 1] = '\0';
100     ret = recv(sfd, gBuf, sizeof(gBuf), 0);
101     LogPrintln("recv on socket %d: %d", sfd, ret);
102     ICUNIT_ASSERT_EQUAL(ret, strlen(CLI_MSG), ret);
103 
104     Wait();
105 
106     /* sendmsg */
107     clnAddr.sin_family = AF_INET;
108     clnAddr.sin_addr.s_addr = inet_addr(PEER_IP);
109     clnAddr.sin_port = htons(PEER_PORT);
110     ret = memset_s(gBuf, BUF_SIZE + 1, 0, BUF_SIZE + 1);
111     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
112     gBuf[BUF_SIZE - 1] = '\0';
113     ret = strcpy_s(gBuf, BUF_SIZE - 1, SRV_MSG);
114     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
115     gBuf[BUF_SIZE - 1] = '\0';
116     msg.msg_name = &clnAddr;
117     msg.msg_namelen = sizeof(clnAddr);
118     msg.msg_iov = iov;
119     msg.msg_iovlen = 2;
120     iov[0].iov_base = gBuf;
121     iov[0].iov_len = strlen(SRV_MSG);
122     iov[1].iov_base = gBuf;
123     iov[1].iov_len = strlen(SRV_MSG);
124     ret = sendmsg(sfd, &msg, 0);
125     LogPrintln("sendmsg on socket %d: %d", sfd, ret);
126     ICUNIT_ASSERT_EQUAL(ret, 2 * strlen(SRV_MSG), ret);
127 
128     Wait();
129 
130     /* recvmsg */
131     ret = memset_s(gBuf, BUF_SIZE + 1, 0, BUF_SIZE + 1);
132     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
133     gBuf[BUF_SIZE - 1] = '\0';
134     ret = memset_s(&msg, sizeof(msg), 0, sizeof(msg));
135     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
136     msg.msg_name = &clnAddr;
137     msg.msg_namelen = sizeof(clnAddr);
138     msg.msg_iov = iov;
139     msg.msg_iovlen = 1;
140     iov[0].iov_base = gBuf;
141     iov[0].iov_len = sizeof(gBuf);
142     ret = recvmsg(sfd, &msg, 0);
143     LogPrintln("recvmsg on socket %d: %d", sfd, ret);
144     ICUNIT_ASSERT_EQUAL(ret, 2 * strlen(CLI_MSG), ret);
145 
146     ret = shutdown(sfd, SHUT_RDWR);
147     LogPrintln("shutdown socket %d: %d", sfd, ret);
148     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
149 
150     close(sfd);
151     close(lsfd);
152     return 0;
153 }
154 
SampleTcpClient()155 static int SampleTcpClient()
156 {
157     static char gBuf[BUF_SIZE + 1] = { 0 };
158     int sfd = -1;
159     struct sockaddr_in srvAddr = { 0 };
160     struct sockaddr_in clnAddr = { 0 };
161     socklen_t clnAddrLen = sizeof(clnAddr);
162     int ret = 0, i = 0;
163     struct msghdr msg = { 0 };
164     struct iovec iov[2] = { };
165     struct sockaddr addr;
166     socklen_t addrLen = sizeof(addr);
167 
168     /* tcp client connection */
169     sfd = socket(AF_INET, SOCK_STREAM, 0);
170     LogPrintln("create client socket inet stream: %d", sfd);
171     ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, sfd);
172 
173     Wait();
174 
175     srvAddr.sin_family = AF_INET;
176     srvAddr.sin_addr.s_addr = inet_addr(PEER_IP);
177     srvAddr.sin_port = htons(PEER_PORT);
178     ret = connect(sfd, reinterpret_cast<struct sockaddr *>(&srvAddr), sizeof(srvAddr));
179     LogPrintln("connect socket %d to %s:%d: %d", sfd, inet_ntoa(srvAddr.sin_addr), ntohs(srvAddr.sin_port), ret);
180     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
181 
182     /* test getpeername */
183     ret = getpeername(sfd, &addr, &addrLen);
184     LogPrintln("getpeername %d %s:%d: %d", sfd, inet_ntoa((reinterpret_cast<struct sockaddr_in *>(&addr))->sin_addr),
185         ntohs((reinterpret_cast<struct sockaddr_in *>(&addr))->sin_port), ret);
186     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
187     ICUNIT_ASSERT_EQUAL(addrLen, sizeof(struct sockaddr_in), addrLen);
188     ICUNIT_ASSERT_EQUAL((reinterpret_cast<struct sockaddr_in *>(&addr))->sin_addr.s_addr,
189         inet_addr(PEER_IP), (static_cast<struct sockaddr_in *>(&addr))->sin_addr.s_addr);
190 
191     /* test getsockname */
192     ret = getsockname(sfd, &addr, &addrLen);
193     LogPrintln("getsockname %d %s:%d: %d", sfd, inet_ntoa((reinterpret_cast<struct sockaddr_in *>(&addr))->sin_addr),
194         ntohs((reinterpret_cast<struct sockaddr_in *>(&addr))->sin_port), ret);
195     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
196     ICUNIT_ASSERT_EQUAL(addrLen, sizeof(struct sockaddr_in), addrLen);
197     ICUNIT_ASSERT_EQUAL((reinterpret_cast<struct sockaddr_in *>(&addr))->sin_addr.s_addr,
198         inet_addr(STACK_IP), (static_cast<struct sockaddr_in *>(&addr))->sin_addr.s_addr);
199 
200     /* send */
201     ret = memset_s(gBuf, BUF_SIZE + 1, 0, BUF_SIZE + 1);
202     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
203     gBuf[BUF_SIZE - 1] = '\0';
204     ret = strcpy_s(gBuf, BUF_SIZE - 1, CLI_MSG);
205     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
206     gBuf[BUF_SIZE - 1] = '\0';
207     ret = send(sfd, gBuf, strlen(CLI_MSG), 0);
208     LogPrintln("send on socket %d: %d", sfd, ret);
209     ICUNIT_ASSERT_EQUAL(ret, strlen(CLI_MSG), ret);
210 
211     /* recv */
212     ret = memset_s(gBuf, BUF_SIZE + 1, 0, BUF_SIZE + 1);
213     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
214     gBuf[BUF_SIZE - 1] = '\0';
215     ret = recv(sfd, gBuf, sizeof(gBuf), 0);
216     LogPrintln("recv on socket %d: %d", sfd, ret);
217     ICUNIT_ASSERT_EQUAL(ret, strlen(SRV_MSG), ret);
218 
219     Wait();
220 
221     /* sendmsg */
222     clnAddr.sin_family = AF_INET;
223     clnAddr.sin_addr.s_addr = inet_addr(PEER_IP);
224     clnAddr.sin_port = htons(PEER_PORT);
225     ret = memset_s(gBuf, BUF_SIZE + 1, 0, BUF_SIZE + 1);
226     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
227     gBuf[BUF_SIZE - 1] = '\0';
228     ret = strcpy_s(gBuf, BUF_SIZE - 1, CLI_MSG);
229     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
230     gBuf[BUF_SIZE - 1] = '\0';
231     msg.msg_name = &clnAddr;
232     msg.msg_namelen = sizeof(clnAddr);
233     msg.msg_iov = iov;
234     msg.msg_iovlen = 2;
235     iov[0].iov_base = gBuf;
236     iov[0].iov_len = strlen(CLI_MSG);
237     iov[1].iov_base = gBuf;
238     iov[1].iov_len = strlen(CLI_MSG);
239     ret = sendmsg(sfd, &msg, 0);
240     LogPrintln("sendmsg on socket %d: %d", sfd, ret);
241     ICUNIT_ASSERT_EQUAL(ret, 2 * strlen(CLI_MSG), ret);
242 
243     Wait();
244 
245     /* recvmsg */
246     ret = memset_s(gBuf, BUF_SIZE + 1, 0, BUF_SIZE + 1);
247     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
248     gBuf[BUF_SIZE - 1] = '\0';
249     ret = memset_s(&msg, sizeof(msg), 0, sizeof(msg));
250     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
251     msg.msg_name = &clnAddr;
252     msg.msg_namelen = sizeof(clnAddr);
253     msg.msg_iov = iov;
254     msg.msg_iovlen = 1;
255     iov[0].iov_base = gBuf;
256     iov[0].iov_len = sizeof(gBuf);
257     ret = recvmsg(sfd, &msg, 0);
258     LogPrintln("recvmsg on socket %d: %d", sfd, ret);
259     ICUNIT_ASSERT_EQUAL(ret, 2 * strlen(SRV_MSG), ret);
260 
261     ret = shutdown(sfd, SHUT_RDWR);
262     LogPrintln("shutdown socket %d: %d", sfd, ret);
263     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
264 
265     close(sfd);
266     return 0;
267 }
268 
TcpServerRoutine(void * p)269 static void* TcpServerRoutine(void *p)
270 {
271     int ret = SampleTcpServer();
272     return reinterpret_cast<void *>(ret);
273 }
274 
TcpClientRoutine(void * p)275 static void* TcpClientRoutine(void *p)
276 {
277     int ret = SampleTcpClient();
278     return reinterpret_cast<void *>(ret);
279 }
280 
TcpTest()281 static int TcpTest()
282 {
283     int ret;
284     void *sret = NULL;
285     void *cret = NULL;
286     pthread_t srv, cli;
287     pthread_attr_t attr;
288 
289     ret = pthread_barrier_init(&gBarrier, 0, 2);
290     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
291 
292     ret = pthread_attr_init(&attr);
293     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
294 
295     ret = pthread_create(&srv, &attr, TcpServerRoutine, NULL);
296     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
297 
298     ret = pthread_create(&cli, &attr, TcpClientRoutine, NULL);
299     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
300 
301     ret = pthread_join(cli, &cret);
302     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
303 
304     LogPrintln("client finish");
305 
306     ret = pthread_join(srv, &sret);
307     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
308 
309     LogPrintln("server finish");
310 
311     ret = pthread_attr_destroy(&attr);
312     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
313 
314     ret = pthread_barrier_destroy(&gBarrier);
315     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
316 
317     return static_cast<int>(reinterpret_cast<intptr_t>(sret)) + static_cast<int>(reinterpret_cast<intptr_t>(cret));
318 }
319 
NetSocketTest003(void)320 void NetSocketTest003(void)
321 {
322     TEST_ADD_CASE(__FUNCTION__, TcpTest, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
323 }
324