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 MSG "Hi, I am UDP"
46 #define BUF_SIZE (1024 * 8)
47
48 static char g_buf[BUF_SIZE + 1] = { 0 };
49
UdpTest(void)50 static int UdpTest(void)
51 {
52 int sfd;
53 struct sockaddr_in srvAddr = { 0 };
54 struct sockaddr_in clnAddr = { 0 };
55 socklen_t clnAddrLen = sizeof(clnAddr);
56 int ret = 0, i = 0;
57 struct msghdr msg = { 0 };
58 struct iovec iov[2] = { };
59
60 /* socket creation */
61 sfd = socket(AF_INET, SOCK_DGRAM, 0);
62 ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, sfd);
63
64 srvAddr.sin_family = AF_INET;
65 srvAddr.sin_addr.s_addr = inet_addr(STACK_IP);
66 srvAddr.sin_port = htons(STACK_PORT);
67 ret = bind(sfd, reinterpret_cast<struct sockaddr *>(&srvAddr), sizeof(srvAddr));
68 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
69
70 /* send */
71 clnAddr.sin_family = AF_INET;
72 clnAddr.sin_addr.s_addr = inet_addr(PEER_IP);
73 clnAddr.sin_port = htons(PEER_PORT);
74 ret = memset_s(g_buf, BUF_SIZE, 0, BUF_SIZE);
75 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
76 ret = strcpy_s(g_buf, BUF_SIZE, MSG);
77 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
78 ret = sendto(sfd, g_buf, strlen(MSG), 0, reinterpret_cast<struct sockaddr *>(&clnAddr),
79 static_cast<socklen_t>(sizeof(clnAddr)));
80 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret);
81
82 /* recv */
83 ret = memset_s(g_buf, BUF_SIZE, 0, BUF_SIZE);
84 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
85 ret = recvfrom(sfd, g_buf, sizeof(g_buf), 0, reinterpret_cast<struct sockaddr *>(&clnAddr),
86 &clnAddrLen);
87 ICUNIT_ASSERT_EQUAL(ret, strlen(MSG), ret);
88
89 /* sendmsg */
90 clnAddr.sin_family = AF_INET;
91 clnAddr.sin_addr.s_addr = inet_addr(PEER_IP);
92 clnAddr.sin_port = htons(PEER_PORT);
93 ret = memset_s(g_buf, BUF_SIZE, 0, BUF_SIZE);
94 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
95 ret = strcpy_s(g_buf, BUF_SIZE, MSG);
96 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
97 msg.msg_name = &clnAddr;
98 msg.msg_namelen = sizeof(clnAddr);
99 msg.msg_iov = iov;
100 msg.msg_iovlen = 2;
101 iov[0].iov_base = g_buf;
102 iov[0].iov_len = strlen(MSG);
103 iov[1].iov_base = g_buf;
104 iov[1].iov_len = strlen(MSG);
105 ret = sendmsg(sfd, &msg, 0);
106 ICUNIT_ASSERT_EQUAL(ret, 2 * strlen(MSG), ret);
107
108 /* recvmsg */
109 ret = memset_s(g_buf, BUF_SIZE, 0, BUF_SIZE);
110 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
111 ret = memset_s(&msg, sizeof(msg), 0, sizeof(msg));
112 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
113 msg.msg_name = &clnAddr;
114 msg.msg_namelen = sizeof(clnAddr);
115 msg.msg_iov = iov;
116 msg.msg_iovlen = 1;
117 iov[0].iov_base = g_buf;
118 iov[0].iov_len = sizeof(g_buf);
119 ret = recvmsg(sfd, &msg, 0);
120 ICUNIT_ASSERT_EQUAL(ret, 2 * strlen(MSG), ret);
121
122 /* close socket */
123 ret = close(sfd);
124 ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret);
125 return 0;
126 }
127
NetSocketTest002(void)128 void NetSocketTest002(void)
129 {
130 TEST_ADD_CASE(__FUNCTION__, UdpTest, TEST_POSIX, TEST_UDP, TEST_LEVEL0, TEST_FUNCTION);
131 }
132