From ed10c9068e3e9d04292044f6868f0bc014989637 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Thu, 8 Aug 2019 16:29:25 +0300 Subject: [PATCH] tests: net: sendmsg: Verify that connected UDP sock works Add new tests that make sure that sendmsg() works when using connected UDP socket and when not setting msghdr destination address. Signed-off-by: Jukka Rissanen --- tests/net/socket/udp/src/main.c | 127 ++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/tests/net/socket/udp/src/main.c b/tests/net/socket/udp/src/main.c index 4c787df39cf8a7..a39509c3d55d2d 100644 --- a/tests/net/socket/udp/src/main.c +++ b/tests/net/socket/udp/src/main.c @@ -530,6 +530,131 @@ void test_v6_sendmsg_recvfrom(void) zassert_equal(rv, 0, "close failed"); } +void test_v4_sendmsg_recvfrom_connected(void) +{ + int rv; + int client_sock; + int server_sock; + struct sockaddr_in client_addr; + struct sockaddr_in server_addr; + struct msghdr msg; + struct cmsghdr *cmsg; + struct iovec io_vector[1]; + union { + struct cmsghdr hdr; + unsigned char buf[CMSG_SPACE(sizeof(int))]; + } cmsgbuf; + + prepare_sock_udp_v4(CONFIG_NET_CONFIG_MY_IPV4_ADDR, ANY_PORT, + &client_sock, &client_addr); + prepare_sock_udp_v4(CONFIG_NET_CONFIG_MY_IPV4_ADDR, SERVER_PORT, + &server_sock, &server_addr); + + rv = bind(server_sock, + (struct sockaddr *)&server_addr, + sizeof(server_addr)); + zassert_equal(rv, 0, "server bind failed"); + + rv = bind(client_sock, + (struct sockaddr *)&client_addr, + sizeof(client_addr)); + zassert_equal(rv, 0, "client bind failed"); + + rv = connect(client_sock, (struct sockaddr *)&server_addr, + sizeof(server_addr)); + zassert_equal(rv, 0, "connect failed"); + + io_vector[0].iov_base = TEST_STR_SMALL; + io_vector[0].iov_len = strlen(TEST_STR_SMALL); + + memset(&msg, 0, sizeof(msg)); + msg.msg_control = &cmsgbuf.buf; + msg.msg_controllen = sizeof(cmsgbuf.buf); + msg.msg_iov = io_vector; + msg.msg_iovlen = 1; + + cmsg = CMSG_FIRSTHDR(&msg); + cmsg->cmsg_len = CMSG_LEN(sizeof(int)); + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = 1122; + *(int *)CMSG_DATA(cmsg) = 42; + + comm_sendmsg_recvfrom(client_sock, + (struct sockaddr *)&client_addr, + sizeof(client_addr), + &msg, + server_sock, + (struct sockaddr *)&server_addr, + sizeof(server_addr)); + + rv = close(client_sock); + zassert_equal(rv, 0, "close failed"); + rv = close(server_sock); + zassert_equal(rv, 0, "close failed"); +} + +void test_v6_sendmsg_recvfrom_connected(void) +{ + int rv; + int client_sock; + int server_sock; + struct sockaddr_in6 client_addr; + struct sockaddr_in6 server_addr; + struct msghdr msg; + struct cmsghdr *cmsg; + struct iovec io_vector[1]; + union { + struct cmsghdr hdr; + unsigned char buf[CMSG_SPACE(sizeof(int))]; + } cmsgbuf; + + prepare_sock_udp_v6(CONFIG_NET_CONFIG_MY_IPV6_ADDR, ANY_PORT, + &client_sock, &client_addr); + prepare_sock_udp_v6(CONFIG_NET_CONFIG_MY_IPV6_ADDR, SERVER_PORT, + &server_sock, &server_addr); + + rv = bind(server_sock, + (struct sockaddr *)&server_addr, sizeof(server_addr)); + zassert_equal(rv, 0, "server bind failed"); + + rv = bind(client_sock, + (struct sockaddr *)&client_addr, + sizeof(client_addr)); + zassert_equal(rv, 0, "client bind failed"); + + rv = connect(client_sock, (struct sockaddr *)&server_addr, + sizeof(server_addr)); + zassert_equal(rv, 0, "connect failed"); + + io_vector[0].iov_base = TEST_STR_SMALL; + io_vector[0].iov_len = strlen(TEST_STR_SMALL); + + memset(&msg, 0, sizeof(msg)); + msg.msg_control = &cmsgbuf.buf; + msg.msg_controllen = sizeof(cmsgbuf.buf); + msg.msg_iov = io_vector; + msg.msg_iovlen = 1; + + cmsg = CMSG_FIRSTHDR(&msg); + cmsg->cmsg_len = CMSG_LEN(sizeof(int)); + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = 1122; + *(int *)CMSG_DATA(cmsg) = 42; + + comm_sendmsg_recvfrom(client_sock, + (struct sockaddr *)&client_addr, + sizeof(client_addr), + &msg, + server_sock, + (struct sockaddr *)&server_addr, + sizeof(server_addr)); + + rv = close(client_sock); + zassert_equal(rv, 0, "close failed"); + rv = close(server_sock); + zassert_equal(rv, 0, "close failed"); +} + void test_so_txtime(void) { struct sockaddr_in bind_addr4; @@ -795,6 +920,8 @@ void test_main(void) ztest_unit_test(test_so_txtime), ztest_unit_test(test_v4_sendmsg_recvfrom), ztest_unit_test(test_v6_sendmsg_recvfrom), + ztest_unit_test(test_v4_sendmsg_recvfrom_connected), + ztest_unit_test(test_v6_sendmsg_recvfrom_connected), ztest_unit_test(setup_eth), ztest_unit_test(test_v6_sendmsg_with_txtime), ztest_user_unit_test(test_v6_sendmsg_with_txtime)