• 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 MSG "Hi, I am UDP"
38 #define TEST_CASE 120
39 
40 static char g_buf[BUF_SIZE + 1] = { 0 };
41 
UdpTestTask(void * p)42 void UdpTestTask(void *p)
43 {
44     (void)p;
45     LogPrintln("net_socket_test_002.c enter");
46     g_testCase = TEST_CASE;
47     int sfd;
48     struct sockaddr_in srvAddr = { 0 };
49     struct sockaddr_in clnAddr = { 0 };
50     socklen_t clnAddrLen = sizeof(clnAddr);
51     int ret;
52     struct msghdr msg = { 0 };
53     struct iovec iov[IOV_LENGTH] = { 0 };
54     int recvCount = 0;
55 
56     /* socket creation */
57     sfd = socket(AF_INET, SOCK_DGRAM, 0);
58     LWIP_ASSERT("socket invalid param.", sfd != -1);
59 
60     srvAddr.sin_family = AF_INET;
61     srvAddr.sin_addr.s_addr = inet_addr(STACK_IP);
62     srvAddr.sin_port = htons(STACK_PORT);
63     ret = bind(sfd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
64     LWIP_ASSERT("socket invalid param.", ret == 0);
65 
66     /* send */
67     clnAddr.sin_family = AF_INET;
68     clnAddr.sin_addr.s_addr = inet_addr(PEER_IP);
69     clnAddr.sin_port = htons(PEER_PORT);
70     (void)memset_s(g_buf, sizeof(g_buf), 0, sizeof(g_buf));
71     (void)strcpy_s(g_buf, sizeof(g_buf), MSG);
72     ret = sendto(sfd, g_buf, strlen(MSG), 0, (struct sockaddr*)&clnAddr,
73         (socklen_t)sizeof(clnAddr));
74     LWIP_ASSERT("socket invalid param.", ret != -1);
75 
76     ret = ioctlsocket(sfd, FIONREAD, &recvCount);
77     ICUNIT_ASSERT_EQUAL(ret, 0, 1);
78     LogPrintln("udp recv count %d", recvCount);
79 
80     /* recv */
81     (void)memset_s(g_buf, sizeof(g_buf), 0, sizeof(g_buf));
82     ret = recvfrom(sfd, g_buf, sizeof(g_buf), 0, (struct sockaddr*)&clnAddr,
83         &clnAddrLen);
84     LWIP_ASSERT("socket invalid param.", ret == strlen(MSG));
85 
86     /* sendmsg */
87     clnAddr.sin_family = AF_INET;
88     clnAddr.sin_addr.s_addr = inet_addr(PEER_IP);
89     clnAddr.sin_port = htons(PEER_PORT);
90     (void)memset_s(g_buf, sizeof(g_buf), 0, sizeof(g_buf));
91     (void)strcpy_s(g_buf, sizeof(g_buf), MSG);
92     msg.msg_name = &clnAddr;
93     msg.msg_namelen = sizeof(clnAddr);
94     msg.msg_iov = iov;
95     msg.msg_iovlen = IOV_LENGTH;
96     iov[0].iov_base = g_buf;
97     iov[0].iov_len = strlen(MSG);
98     iov[1].iov_base = g_buf;
99     iov[1].iov_len = strlen(MSG);
100     ret = sendmsg(sfd, &msg, 0);
101     LWIP_ASSERT("socket invalid param.", ret == IOV_LENGTH * strlen(MSG));
102 
103     /* recvmsg */
104     (void)memset_s(g_buf, sizeof(g_buf), 0, sizeof(g_buf));
105     (void)memset_s(&msg, sizeof(msg), 0, sizeof(msg));
106     msg.msg_name = &clnAddr;
107     msg.msg_namelen = sizeof(clnAddr);
108     msg.msg_iov = iov;
109     msg.msg_iovlen = 1;
110     iov[0].iov_base = g_buf;
111     iov[0].iov_len = sizeof(g_buf);
112     ret = recvmsg(sfd, &msg, 0);
113     LWIP_ASSERT("socket invalid param.", ret == IOV_LENGTH * strlen(MSG));
114 
115     /* close socket */
116     ret = closesocket(sfd);
117     LWIP_ASSERT("socket invalid param.", ret != -1);
118     return;
119 }
120 
UdpTest()121 int UdpTest()
122 {
123     int ret = sys_thread_new("udp_test", UdpTestTask, NULL,
124         STACK_TEST_SIZE, TCPIP_THREAD_PRIO);
125     ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 23);
126     return ret;
127 }
128