Skip to content

Commit

Permalink
tests: net: sendmsg: Verify that connected UDP sock works
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jukkar committed Aug 8, 2019
1 parent c80407d commit ed10c90
Showing 1 changed file with 127 additions and 0 deletions.
127 changes: 127 additions & 0 deletions tests/net/socket/udp/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ed10c90

Please sign in to comment.