13d8536b4Sopenharmony_ci/* 23d8536b4Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. 33d8536b4Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. 43d8536b4Sopenharmony_ci * 53d8536b4Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification, 63d8536b4Sopenharmony_ci * are permitted provided that the following conditions are met: 73d8536b4Sopenharmony_ci * 83d8536b4Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of 93d8536b4Sopenharmony_ci * conditions and the following disclaimer. 103d8536b4Sopenharmony_ci * 113d8536b4Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list 123d8536b4Sopenharmony_ci * of conditions and the following disclaimer in the documentation and/or other materials 133d8536b4Sopenharmony_ci * provided with the distribution. 143d8536b4Sopenharmony_ci * 153d8536b4Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used 163d8536b4Sopenharmony_ci * to endorse or promote products derived from this software without specific prior written 173d8536b4Sopenharmony_ci * permission. 183d8536b4Sopenharmony_ci * 193d8536b4Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 203d8536b4Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 213d8536b4Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 223d8536b4Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 233d8536b4Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 243d8536b4Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 253d8536b4Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 263d8536b4Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 273d8536b4Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 283d8536b4Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 293d8536b4Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 303d8536b4Sopenharmony_ci */ 313d8536b4Sopenharmony_ci 323d8536b4Sopenharmony_ci#include "lwip_test.h" 333d8536b4Sopenharmony_ci#include "lwipopts.h" 343d8536b4Sopenharmony_ci#include <arch/sys_arch.h> 353d8536b4Sopenharmony_ci#include <lwip/sys.h> 363d8536b4Sopenharmony_ci 373d8536b4Sopenharmony_ci#define MSG "Hi, I am UDP" 383d8536b4Sopenharmony_ci#define TEST_CASE 120 393d8536b4Sopenharmony_ci 403d8536b4Sopenharmony_cistatic char g_buf[BUF_SIZE + 1] = { 0 }; 413d8536b4Sopenharmony_ci 423d8536b4Sopenharmony_civoid UdpTestTask(void *p) 433d8536b4Sopenharmony_ci{ 443d8536b4Sopenharmony_ci (void)p; 453d8536b4Sopenharmony_ci LogPrintln("net_socket_test_002.c enter"); 463d8536b4Sopenharmony_ci g_testCase = TEST_CASE; 473d8536b4Sopenharmony_ci int sfd; 483d8536b4Sopenharmony_ci struct sockaddr_in srvAddr = { 0 }; 493d8536b4Sopenharmony_ci struct sockaddr_in clnAddr = { 0 }; 503d8536b4Sopenharmony_ci socklen_t clnAddrLen = sizeof(clnAddr); 513d8536b4Sopenharmony_ci int ret; 523d8536b4Sopenharmony_ci struct msghdr msg = { 0 }; 533d8536b4Sopenharmony_ci struct iovec iov[IOV_LENGTH] = { 0 }; 543d8536b4Sopenharmony_ci int recvCount = 0; 553d8536b4Sopenharmony_ci 563d8536b4Sopenharmony_ci /* socket creation */ 573d8536b4Sopenharmony_ci sfd = socket(AF_INET, SOCK_DGRAM, 0); 583d8536b4Sopenharmony_ci LWIP_ASSERT("socket invalid param.", sfd != -1); 593d8536b4Sopenharmony_ci 603d8536b4Sopenharmony_ci srvAddr.sin_family = AF_INET; 613d8536b4Sopenharmony_ci srvAddr.sin_addr.s_addr = inet_addr(STACK_IP); 623d8536b4Sopenharmony_ci srvAddr.sin_port = htons(STACK_PORT); 633d8536b4Sopenharmony_ci ret = bind(sfd, (struct sockaddr*)&srvAddr, sizeof(srvAddr)); 643d8536b4Sopenharmony_ci LWIP_ASSERT("socket invalid param.", ret == 0); 653d8536b4Sopenharmony_ci 663d8536b4Sopenharmony_ci /* send */ 673d8536b4Sopenharmony_ci clnAddr.sin_family = AF_INET; 683d8536b4Sopenharmony_ci clnAddr.sin_addr.s_addr = inet_addr(PEER_IP); 693d8536b4Sopenharmony_ci clnAddr.sin_port = htons(PEER_PORT); 703d8536b4Sopenharmony_ci (void)memset_s(g_buf, sizeof(g_buf), 0, sizeof(g_buf)); 713d8536b4Sopenharmony_ci (void)strcpy_s(g_buf, sizeof(g_buf), MSG); 723d8536b4Sopenharmony_ci ret = sendto(sfd, g_buf, strlen(MSG), 0, (struct sockaddr*)&clnAddr, 733d8536b4Sopenharmony_ci (socklen_t)sizeof(clnAddr)); 743d8536b4Sopenharmony_ci LWIP_ASSERT("socket invalid param.", ret != -1); 753d8536b4Sopenharmony_ci 763d8536b4Sopenharmony_ci ret = ioctlsocket(sfd, FIONREAD, &recvCount); 773d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, 1); 783d8536b4Sopenharmony_ci LogPrintln("udp recv count %d", recvCount); 793d8536b4Sopenharmony_ci 803d8536b4Sopenharmony_ci /* recv */ 813d8536b4Sopenharmony_ci (void)memset_s(g_buf, sizeof(g_buf), 0, sizeof(g_buf)); 823d8536b4Sopenharmony_ci ret = recvfrom(sfd, g_buf, sizeof(g_buf), 0, (struct sockaddr*)&clnAddr, 833d8536b4Sopenharmony_ci &clnAddrLen); 843d8536b4Sopenharmony_ci LWIP_ASSERT("socket invalid param.", ret == strlen(MSG)); 853d8536b4Sopenharmony_ci 863d8536b4Sopenharmony_ci /* sendmsg */ 873d8536b4Sopenharmony_ci clnAddr.sin_family = AF_INET; 883d8536b4Sopenharmony_ci clnAddr.sin_addr.s_addr = inet_addr(PEER_IP); 893d8536b4Sopenharmony_ci clnAddr.sin_port = htons(PEER_PORT); 903d8536b4Sopenharmony_ci (void)memset_s(g_buf, sizeof(g_buf), 0, sizeof(g_buf)); 913d8536b4Sopenharmony_ci (void)strcpy_s(g_buf, sizeof(g_buf), MSG); 923d8536b4Sopenharmony_ci msg.msg_name = &clnAddr; 933d8536b4Sopenharmony_ci msg.msg_namelen = sizeof(clnAddr); 943d8536b4Sopenharmony_ci msg.msg_iov = iov; 953d8536b4Sopenharmony_ci msg.msg_iovlen = IOV_LENGTH; 963d8536b4Sopenharmony_ci iov[0].iov_base = g_buf; 973d8536b4Sopenharmony_ci iov[0].iov_len = strlen(MSG); 983d8536b4Sopenharmony_ci iov[1].iov_base = g_buf; 993d8536b4Sopenharmony_ci iov[1].iov_len = strlen(MSG); 1003d8536b4Sopenharmony_ci ret = sendmsg(sfd, &msg, 0); 1013d8536b4Sopenharmony_ci LWIP_ASSERT("socket invalid param.", ret == IOV_LENGTH * strlen(MSG)); 1023d8536b4Sopenharmony_ci 1033d8536b4Sopenharmony_ci /* recvmsg */ 1043d8536b4Sopenharmony_ci (void)memset_s(g_buf, sizeof(g_buf), 0, sizeof(g_buf)); 1053d8536b4Sopenharmony_ci (void)memset_s(&msg, sizeof(msg), 0, sizeof(msg)); 1063d8536b4Sopenharmony_ci msg.msg_name = &clnAddr; 1073d8536b4Sopenharmony_ci msg.msg_namelen = sizeof(clnAddr); 1083d8536b4Sopenharmony_ci msg.msg_iov = iov; 1093d8536b4Sopenharmony_ci msg.msg_iovlen = 1; 1103d8536b4Sopenharmony_ci iov[0].iov_base = g_buf; 1113d8536b4Sopenharmony_ci iov[0].iov_len = sizeof(g_buf); 1123d8536b4Sopenharmony_ci ret = recvmsg(sfd, &msg, 0); 1133d8536b4Sopenharmony_ci LWIP_ASSERT("socket invalid param.", ret == IOV_LENGTH * strlen(MSG)); 1143d8536b4Sopenharmony_ci 1153d8536b4Sopenharmony_ci /* close socket */ 1163d8536b4Sopenharmony_ci ret = closesocket(sfd); 1173d8536b4Sopenharmony_ci LWIP_ASSERT("socket invalid param.", ret != -1); 1183d8536b4Sopenharmony_ci return; 1193d8536b4Sopenharmony_ci} 1203d8536b4Sopenharmony_ci 1213d8536b4Sopenharmony_ciint UdpTest() 1223d8536b4Sopenharmony_ci{ 1233d8536b4Sopenharmony_ci int ret = sys_thread_new("udp_test", UdpTestTask, NULL, 1243d8536b4Sopenharmony_ci STACK_TEST_SIZE, TCPIP_THREAD_PRIO); 1253d8536b4Sopenharmony_ci ICUNIT_ASSERT_NOT_EQUAL(ret, -1, 23); 1263d8536b4Sopenharmony_ci return ret; 1273d8536b4Sopenharmony_ci} 128