xref: /xnu-12377.61.12/tests/sendmsg_test.c (revision 4d495c6e23c53686cf65f45067f79024cf5dcee8)
1 /*
2  * Copyright (c) 2025 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/socket.h>
30 #include <sys/errno.h>
31 #include <sys/socketvar.h>
32 #include <sys/unpcb.h>
33 
34 #include <err.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <sysexits.h>
40 #include <unistd.h>
41 
42 #include <darwintest/darwintest.h>
43 
44 T_GLOBAL_META(
45 	T_META_NAMESPACE("xnu.net"),
46 	T_META_RADAR_COMPONENT_NAME("xnu"),
47 	T_META_RADAR_COMPONENT_VERSION("networking"),
48 	T_META_CHECK_LEAKS(false));
49 
50 static void
do_test(int type)51 do_test(int type)
52 {
53 	ssize_t retval;
54 	int sock[2];
55 
56 	T_LOG("test socket type %d", type);
57 
58 	T_ASSERT_POSIX_SUCCESS(socketpair(PF_LOCAL, type, 0, sock), "socketpair()");
59 
60 	T_LOG("socketpair: [%d, %d]", sock[0], sock[1]);
61 
62 	int optval = 1;
63 	T_ASSERT_POSIX_SUCCESS(setsockopt(sock[0], SOL_SOCKET, SO_DEBUG, &optval, sizeof(optval)), "setsockopt(SO_DEBUG)");
64 
65 	struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
66 	T_ASSERT_POSIX_SUCCESS(setsockopt(sock[0], SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval)), "setsockopt(SO_RCVTIMEO)");
67 
68 	struct iovec iov0 = { .iov_base = NULL, .iov_len = 0 };
69 
70 	char cmsg_buf[CMSG_SPACE(sizeof(int))];
71 	struct msghdr msghdr1 = {
72 		.msg_name = NULL,
73 		.msg_namelen = 0,
74 		.msg_iov = &iov0,
75 		.msg_iovlen = 1,
76 		.msg_control = cmsg_buf,
77 		.msg_controllen = sizeof(cmsg_buf),
78 		.msg_flags = 0
79 	};
80 
81 	struct cmsghdr * cmsg = CMSG_FIRSTHDR(&msghdr1);
82 	cmsg->cmsg_len = CMSG_LEN(sizeof(int));
83 	cmsg->cmsg_level = SOL_SOCKET;
84 	cmsg->cmsg_type = SCM_RIGHTS;
85 	*((int *) CMSG_DATA(cmsg)) = sock[0];
86 
87 	retval = sendmsg(sock[1], &msghdr1, 0);
88 	if (retval == -1) {
89 		T_LOG("sendmsg(msghdr1) error: %s", strerror(errno));
90 	} else {
91 		T_LOG("sendmsg msghdr1 %ld", retval);
92 	}
93 
94 	struct iovec iov1 = { .iov_base = NULL, .iov_len = 0 };
95 	struct msghdr msghdr2 = {
96 		.msg_name = NULL,
97 		.msg_namelen = 0,
98 		.msg_iov = &iov1,
99 		.msg_iovlen = 1,
100 		.msg_control = NULL,
101 		.msg_controllen = 0,
102 		.msg_flags = 0
103 	};
104 
105 	retval = recvmsg(sock[0], &msghdr2, MSG_WAITALL);
106 	if (retval == -1) {
107 		T_LOG("recvmsg(msghdr2) error: %s", strerror(errno));
108 	} else {
109 		T_LOG("recvmsg msghdr2 %ld", retval);
110 	}
111 
112 	char * buf[0x10] = { 0 };
113 	struct iovec iov2 = {
114 		.iov_base = buf,
115 		.iov_len = sizeof(buf),
116 	};
117 
118 	struct msghdr msghdr3 = {
119 		.msg_name = NULL,
120 		.msg_namelen = 0,
121 		.msg_iov = &iov2,
122 		.msg_iovlen = 1,
123 		.msg_control = NULL,
124 		.msg_controllen = 0,
125 		.msg_flags = 0
126 	};
127 
128 	retval = recvmsg(sock[0], &msghdr3, MSG_WAITALL);
129 	if (retval == -1) {
130 		T_LOG("recvmsg(msghdr3) error: %s", strerror(errno));
131 	} else {
132 		T_LOG("recvmsg msghdr3 %ld", retval);
133 	}
134 
135 	close(sock[0]);
136 	close(sock[1]);
137 
138 	T_PASS("%s", __func__);
139 }
140 
141 T_DECL(send_zero_payload_dgram, "repro-124040738 SOCK_DGRAM", T_META_ASROOT(true))
142 {
143 	do_test(SOCK_DGRAM);
144 }
145 
146 T_DECL(send_zero_payload_stream, "repro-124040738 SOCK_STREAM", T_META_ASROOT(true))
147 {
148 	do_test(SOCK_STREAM);
149 }
150