xref: /xnu-10002.61.3/tests/sendmsg_x_test.c (revision 0f4c859e951fba394238ab619495c4e1d54d0f34)
1 /*
2  * Copyright (c) 2022 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <sys/errno.h>
30 #include <sys/fcntl.h>
31 #include <sys/socket.h>
32 #include <stdio.h>
33 #include <stdint.h>
34 #include <string.h>
35 #include <sysexits.h>
36 #include <unistd.h>
37 #include <darwintest.h>
38 #include <darwintest_utils.h>
39 
40 /* -*- compile-command: "xcrun --sdk macosx.internal make -C tests sendmsg_x_test" -*- */
41 
42 T_GLOBAL_META(T_META_NAMESPACE("xnu.net"));
43 
44 #define DATAGRAM_SIZE 64
45 
46 T_DECL(sendmsg_x_test, "exercise sendmsg_x() return value")
47 {
48 	int socket_fds[2];
49 
50 	T_EXPECT_POSIX_SUCCESS(socketpair(AF_UNIX, SOCK_DGRAM, 0, socket_fds), "socketpair");
51 
52 	/*
53 	 * The receive buffer size is too small for more than one datagram.
54 	 */
55 	int send_buffer_size = DATAGRAM_SIZE;
56 	int receive_buffer_size = DATAGRAM_SIZE + (DATAGRAM_SIZE >> 1);
57 	socklen_t opt_len = sizeof(int);
58 
59 	T_EXPECT_POSIX_SUCCESS(setsockopt(socket_fds[0], SOL_SOCKET, SO_SNDBUF, &send_buffer_size, opt_len), "setsockopt() SO_SNDBUF");
60 	T_EXPECT_POSIX_SUCCESS(setsockopt(socket_fds[0], SOL_SOCKET, SO_RCVBUF, &receive_buffer_size, opt_len), "setsockopt() SO_RCVBUF");
61 	T_EXPECT_POSIX_SUCCESS(setsockopt(socket_fds[1], SOL_SOCKET, SO_SNDBUF, &send_buffer_size, opt_len), "setsockopt() SO_SNDBUF");
62 	T_EXPECT_POSIX_SUCCESS(setsockopt(socket_fds[1], SOL_SOCKET, SO_RCVBUF, &receive_buffer_size, opt_len), "setsockopt() SO_RCVBUF");
63 
64 	/*
65 	 * Send two datagram at once, only one must be sent and received
66 	 */
67 	struct msghdr_x message_headers[2] = {};
68 
69 	uint8_t buffer1[DATAGRAM_SIZE];
70 	memset(buffer1, 0x12, DATAGRAM_SIZE);
71 
72 	struct iovec iovec1 = {
73 		.iov_base = buffer1,
74 		.iov_len = DATAGRAM_SIZE,
75 	};
76 	message_headers[0].msg_iov = &iovec1;
77 	message_headers[0].msg_iovlen = 1;
78 
79 	uint8_t buffer2[DATAGRAM_SIZE];
80 	memset(buffer2, 0x34, DATAGRAM_SIZE);
81 
82 	struct iovec iovec2 = {
83 		.iov_base = buffer2,
84 		.iov_len = DATAGRAM_SIZE,
85 	};
86 
87 	message_headers[1].msg_iov = &iovec2;
88 	message_headers[1].msg_iovlen = 1;
89 
90 	ssize_t sendmsg_x_result = sendmsg_x(socket_fds[0], message_headers, 2, 0);
91 	if (sendmsg_x_result < 0) {
92 		T_FAIL("sendmsg_x() failed: %s", strerror(errno));
93 	}
94 	if (sendmsg_x_result != 1) {
95 		T_FAIL("sendmsg_x() failed: return %zd instead of 1", sendmsg_x_result);
96 	}
97 	T_PASS("sendmsg_x() result: %zd == 1\n", sendmsg_x_result);
98 
99 	/*
100 	 * Receive the datagram we expect
101 	 */
102 	uint8_t receive_buffer1[DATAGRAM_SIZE * 2];
103 	ssize_t recv_result = recv(socket_fds[1], receive_buffer1, sizeof(receive_buffer1), 0);
104 	T_EXPECT_POSIX_SUCCESS(recv_result, "first recv() recv_result %zd errno %d", recv_result, errno);
105 
106 	if (recv_result != DATAGRAM_SIZE) {
107 		T_FAIL("recv() failed: return %zd instead of DATAGRAM_SIZE", recv_result);
108 	}
109 	T_PASS("recv() result: %zd == DATAGRAM_SIZE\n", recv_result);
110 
111 	int i;
112 	for (i = 0; i < DATAGRAM_SIZE; i++) {
113 		if (receive_buffer1[i] != 0x12) {
114 			T_FAIL("receive_buffer1[%d] 0x%x == 0x12", i, receive_buffer1[i]);
115 		}
116 	}
117 	T_PASS("First datagram successfully received.\n");
118 
119 	/*
120 	 * Set the receive socket as non-blocking and verify no more data is pending
121 	 */
122 	int flags = fcntl(socket_fds[1], F_GETFL, 0);
123 	T_EXPECT_POSIX_SUCCESS(fcntl(socket_fds[1], F_SETFL, flags | O_NONBLOCK), "fcntl() O_NONBLOCK");
124 
125 	recv_result = recv(socket_fds[1], receive_buffer1, sizeof(receive_buffer1), 0);
126 	T_EXPECT_POSIX_ERROR(errno, EWOULDBLOCK, "second recv() recv_result %zd errno %d", recv_result, errno);
127 
128 	close(socket_fds[0]);
129 	close(socket_fds[1]);
130 }
131