svn commit: r292914 - in head: tests/sys/kern tools/regression/sockets/unix_passfd

Garrett Cooper ngie at FreeBSD.org
Wed Dec 30 11:15:09 UTC 2015


Author: ngie
Date: Wed Dec 30 11:15:07 2015
New Revision: 292914
URL: https://svnweb.freebsd.org/changeset/base/292914

Log:
  Integrate tools/regression/sockets/unix_passfd into the FreeBSD test
  suite as tests/sys/kern/unix_passfd_test
  
  - Convert testcases to ATF
  - Fix an alignment issues
  - Mark rights_creds_payload(..) as an expected failure (see PR # 181741)
  
  Based [in part] on the following Differential Revision:
  https://reviews.freebsd.org/D689
  
  MFC after: 1 week
  Submitted by: markj
  Sponsored by: EMC / Isilon Storage Division

Added:
  head/tests/sys/kern/unix_passfd_test.c
     - copied, changed from r292913, head/tools/regression/sockets/unix_passfd/unix_passfd.c
Deleted:
  head/tools/regression/sockets/unix_passfd/
Modified:
  head/tests/sys/kern/Makefile

Modified: head/tests/sys/kern/Makefile
==============================================================================
--- head/tests/sys/kern/Makefile	Wed Dec 30 10:23:25 2015	(r292913)
+++ head/tests/sys/kern/Makefile	Wed Dec 30 11:15:07 2015	(r292914)
@@ -8,6 +8,7 @@ ATF_TESTS_C+=	kern_copyin
 ATF_TESTS_C+=	kern_descrip_test
 ATF_TESTS_C+=	ptrace_test
 ATF_TESTS_C+=	unix_seqpacket_test
+ATF_TESTS_C+=	unix_passfd_test
 TEST_METADATA.unix_seqpacket_test+=	timeout="15"
 
 LIBADD.ptrace_test+=			pthread

Copied and modified: head/tests/sys/kern/unix_passfd_test.c (from r292913, head/tools/regression/sockets/unix_passfd/unix_passfd.c)
==============================================================================
--- head/tools/regression/sockets/unix_passfd/unix_passfd.c	Wed Dec 30 10:23:25 2015	(r292913, copy source)
+++ head/tests/sys/kern/unix_passfd_test.c	Wed Dec 30 11:15:07 2015	(r292914)
@@ -33,7 +33,7 @@
 #include <sys/sysctl.h>
 #include <sys/un.h>
 
-#include <err.h>
+#include <errno.h>
 #include <fcntl.h>
 #include <limits.h>
 #include <stdio.h>
@@ -41,6 +41,8 @@
 #include <string.h>
 #include <unistd.h>
 
+#include <atf-c.h>
+
 /*
  * UNIX domain sockets allow file descriptors to be passed via "ancillary
  * data", or control messages.  This regression test is intended to exercise
@@ -51,11 +53,11 @@
  */
 
 static void
-domainsocketpair(const char *test, int *fdp)
+domainsocketpair(int *fdp)
 {
 
-	if (socketpair(PF_UNIX, SOCK_STREAM, 0, fdp) < 0)
-		err(-1, "%s: socketpair(PF_UNIX, SOCK_STREAM)", test);
+	ATF_REQUIRE_MSG(socketpair(PF_UNIX, SOCK_STREAM, 0, fdp) != -1,
+	    "socketpair(PF_UNIX, SOCK_STREAM) failed: %s", strerror(errno));
 }
 
 static void
@@ -67,51 +69,47 @@ closesocketpair(int *fdp)
 }
 
 static void
-devnull(const char *test, int *fdp)
+devnull(int *fdp)
 {
 	int fd;
 
 	fd = open("/dev/null", O_RDONLY);
-	if (fd < 0)
-		err(-1, "%s: open(/dev/null)", test);
+	ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
 	*fdp = fd;
 }
 
 static void
-tempfile(const char *test, int *fdp)
+tempfile(int *fdp)
 {
 	char path[PATH_MAX];
 	int fd;
 
-	snprintf(path, PATH_MAX, "/tmp/unix_passfd.XXXXXXXXXXXXXXX");
+	snprintf(path, PATH_MAX, "%s/unix_passfd.XXXXXXXXXXXXXXX",
+	    getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR"));
 	fd = mkstemp(path);
-	if (fd < 0)
-		err(-1, "%s: mkstemp(%s)", test, path);
+	ATF_REQUIRE_MSG(fd != -1, "mkstemp(%s) failed", path);
 	(void)unlink(path);
 	*fdp = fd;
 }
 
 static void
-dofstat(const char *test, int fd, struct stat *sb)
+dofstat(int fd, struct stat *sb)
 {
 
-	if (fstat(fd, sb) < 0)
-		err(-1, "%s: fstat", test);
+	ATF_REQUIRE_MSG(fstat(fd, sb) == 0,
+	    "fstat failed: %s", strerror(errno));
 }
 
 static void
-samefile(const char *test, struct stat *sb1, struct stat *sb2)
+samefile(struct stat *sb1, struct stat *sb2)
 {
 
-	if (sb1->st_dev != sb2->st_dev)
-		errx(-1, "%s: samefile: different device", test);
-	if (sb1->st_ino != sb2->st_ino)
-		errx(-1, "%s: samefile: different inode", test);
+	ATF_REQUIRE_MSG(sb1->st_dev == sb2->st_dev, "different device");
+	ATF_REQUIRE_MSG(sb1->st_ino == sb2->st_ino, "different inode");
 }
 
 static void
-sendfd_payload(const char *test, int sockfd, int sendfd,
-    void *payload, size_t paylen)
+sendfd_payload(int sockfd, int sendfd, void *payload, size_t paylen)
 {
 	struct iovec iovec;
 	char message[CMSG_SPACE(sizeof(int))];
@@ -131,30 +129,29 @@ sendfd_payload(const char *test, int soc
 	msghdr.msg_iov = &iovec;
 	msghdr.msg_iovlen = 1;
 
-	cmsghdr = (struct cmsghdr *)message;
+	cmsghdr = (struct cmsghdr *)(void*)message;
 	cmsghdr->cmsg_len = CMSG_LEN(sizeof(int));
 	cmsghdr->cmsg_level = SOL_SOCKET;
 	cmsghdr->cmsg_type = SCM_RIGHTS;
-	*(int *)CMSG_DATA(cmsghdr) = sendfd;
+	memcpy(CMSG_DATA(cmsghdr), &sendfd, sizeof(int));
 
 	len = sendmsg(sockfd, &msghdr, 0);
-	if (len < 0)
-		err(-1, "%s: sendmsg", test);
-	if ((size_t)len != paylen)
-		errx(-1, "%s: sendmsg: %zd bytes sent", test, len);
+	ATF_REQUIRE_MSG(len != -1, "sendmsg failed: %s", strerror(errno));
+	ATF_REQUIRE_MSG((size_t)len == paylen,
+	    "sendmsg: %zd messages sent; expected: %zu; %s", len, paylen,
+	    strerror(errno));
 }
 
 static void
-sendfd(const char *test, int sockfd, int sendfd)
+sendfd(int sockfd, int sendfd)
 {
 	char ch = 0;
 
-	return (sendfd_payload(test, sockfd, sendfd, &ch, sizeof(ch)));
+	return (sendfd_payload(sockfd, sendfd, &ch, sizeof(ch)));
 }
 
 static void
-recvfd_payload(const char *test, int sockfd, int *recvfd,
-    void *buf, size_t buflen)
+recvfd_payload(int sockfd, int *recvfd, void *buf, size_t buflen)
 {
 	struct cmsghdr *cmsghdr;
 	char message[CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + sizeof(int)];
@@ -174,217 +171,226 @@ recvfd_payload(const char *test, int soc
 	msghdr.msg_iovlen = 1;
 
 	len = recvmsg(sockfd, &msghdr, 0);
-	if (len < 0)
-		err(-1, "%s: recvmsg", test);
-	if ((size_t)len != buflen)
-		errx(-1, "%s: recvmsg: %zd bytes received", test, len);
+	ATF_REQUIRE_MSG(len != -1, "recvmsg failed: %s", strerror(errno));
+	ATF_REQUIRE_MSG((size_t)len == buflen,
+	    "recvmsg: %zd bytes received; expected %zd", len, buflen);
 
 	cmsghdr = CMSG_FIRSTHDR(&msghdr);
-	if (cmsghdr == NULL)
-		errx(-1, "%s: recvmsg: did not receive control message", test);
+	ATF_REQUIRE_MSG(cmsghdr != NULL,
+	    "recvmsg: did not receive control message");
 	*recvfd = -1;
 	for (; cmsghdr != NULL; cmsghdr = CMSG_NXTHDR(&msghdr, cmsghdr)) {
 		if (cmsghdr->cmsg_level == SOL_SOCKET &&
 		    cmsghdr->cmsg_type == SCM_RIGHTS &&
 		    cmsghdr->cmsg_len == CMSG_LEN(sizeof(int))) {
-			*recvfd = *(int *)CMSG_DATA(cmsghdr);
-			if (*recvfd == -1)
-				errx(-1, "%s: recvmsg: received fd -1", test);
+			memcpy(recvfd, CMSG_DATA(cmsghdr), sizeof(int));
+			ATF_REQUIRE(*recvfd != -1);
 		}
 	}
-	if (*recvfd == -1)
-		errx(-1, "%s: recvmsg: did not receive single-fd message",
-		    test);
+	ATF_REQUIRE_MSG(*recvfd != -1,
+	    "recvmsg: did not receive single-fd message");
 }
 
 static void
-recvfd(const char *test, int sockfd, int *recvfd)
+recvfd(int sockfd, int *recvfd)
 {
 	char ch = 0;
 
-	return (recvfd_payload(test, sockfd, recvfd, &ch, sizeof(ch)));
+	return (recvfd_payload(sockfd, recvfd, &ch, sizeof(ch)));
 }
 
-int
-main(void)
+/*
+ * Put a temporary file into a UNIX domain socket, then take it out and make
+ * sure it's the same file.  First time around, don't close the reference
+ * after sending.
+ */
+ATF_TC_WITHOUT_HEAD(simple_send_fd);
+ATF_TC_BODY(simple_send_fd, tc)
 {
-	struct stat putfd_1_stat, putfd_2_stat, getfd_1_stat, getfd_2_stat;
-	int fd[2], putfd_1, putfd_2, getfd_1, getfd_2;
-	const char *test;
-
-	/*
-	 * First test: put a temporary file into a UNIX domain socket, then
-	 * take it out and make sure it's the same file.  First time around,
-	 * don't close the reference after sending.
-	 */
-	test = "test1-simplesendfd";
-	printf("beginning %s\n", test);
-
-	domainsocketpair(test, fd);
-	tempfile(test, &putfd_1);
-	dofstat(test, putfd_1, &putfd_1_stat);
-	sendfd(test, fd[0], putfd_1);
-	recvfd(test, fd[1], &getfd_1);
-	dofstat(test, getfd_1, &getfd_1_stat);
-	samefile(test, &putfd_1_stat, &getfd_1_stat);
-	close(putfd_1);
-	close(getfd_1);
+	struct stat getfd_stat, putfd_stat;
+	int fd[2], getfd, putfd;
+
+	domainsocketpair(fd);
+	tempfile(&putfd);
+	dofstat(putfd, &putfd_stat);
+	sendfd(fd[0], putfd);
+	recvfd(fd[1], &getfd);
+	dofstat(getfd, &getfd_stat);
+	samefile(&putfd_stat, &getfd_stat);
+	close(putfd);
+	close(getfd);
 	closesocketpair(fd);
+}
 
-	printf("%s passed\n", test);
+/*
+ * Same as simple_send_fd, only close the file reference after sending, so that
+ * the only reference is the descriptor in the UNIX domain socket buffer.
+ */
+ATF_TC_WITHOUT_HEAD(send_and_close);
+ATF_TC_BODY(send_and_close, tc)
+{
+	struct stat getfd_stat, putfd_stat;
+	int fd[2], getfd, putfd;
 
-	/*
-	 * Second test: same as first, only close the file reference after
-	 * sending, so that the only reference is the descriptor in the UNIX
-	 * domain socket buffer.
-	 */
-	test = "test2-sendandclose";
-	printf("beginning %s\n", test);
-
-	domainsocketpair(test, fd);
-	tempfile(test, &putfd_1);
-	dofstat(test, putfd_1, &putfd_1_stat);
-	sendfd(test, fd[0], putfd_1);
-	close(putfd_1);
-	recvfd(test, fd[1], &getfd_1);
-	dofstat(test, getfd_1, &getfd_1_stat);
-	samefile(test, &putfd_1_stat, &getfd_1_stat);
-	close(getfd_1);
+	domainsocketpair(fd);
+	tempfile(&putfd);
+	dofstat(putfd, &putfd_stat);
+	sendfd(fd[0], putfd);
+	close(putfd);
+	recvfd(fd[1], &getfd);
+	dofstat(getfd, &getfd_stat);
+	samefile(&putfd_stat, &getfd_stat);
+	close(getfd);
 	closesocketpair(fd);
+}
 
-	printf("%s passed\n", test);
+/*
+ * Put a temporary file into a UNIX domain socket, then close both endpoints
+ * causing garbage collection to kick off.
+ */
+ATF_TC_WITHOUT_HEAD(send_and_cancel);
+ATF_TC_BODY(send_and_cancel, tc)
+{
+	int fd[2], putfd;
 
-	/*
-	 * Third test: put a temporary file into a UNIX domain socket, then
-	 * close both endpoints causing garbage collection to kick off.
-	 */
-	test = "test3-sendandcancel";
-	printf("beginning %s\n", test);
-
-	domainsocketpair(test, fd);
-	tempfile(test, &putfd_1);
-	sendfd(test, fd[0], putfd_1);
-	close(putfd_1);
+	domainsocketpair(fd);
+	tempfile(&putfd);
+	sendfd(fd[0], putfd);
+	close(putfd);
 	closesocketpair(fd);
+}
 
-	printf("%s passed\n", test);
+/*
+ * Send two files.  Then receive them.  Make sure they are returned in the
+ * right order, and both get there.
+ */
+ATF_TC_WITHOUT_HEAD(two_files);
+ATF_TC_BODY(two_files, tc)
+{
+	struct stat getfd_1_stat, getfd_2_stat, putfd_1_stat, putfd_2_stat;
+	int fd[2], getfd_1, getfd_2, putfd_1, putfd_2;
 
-	/*
-	 * Send two files.  Then receive them.  Make sure they are returned
-	 * in the right order, and both get there.
-	 */
-
-	test = "test4-twofile";
-	printf("beginning %s\n", test);
-
-	domainsocketpair(test, fd);
-	tempfile(test, &putfd_1);
-	tempfile(test, &putfd_2);
-	dofstat(test, putfd_1, &putfd_1_stat);
-	dofstat(test, putfd_2, &putfd_2_stat);
-	sendfd(test, fd[0], putfd_1);
-	sendfd(test, fd[0], putfd_2);
+	domainsocketpair(fd);
+	tempfile(&putfd_1);
+	tempfile(&putfd_2);
+	dofstat(putfd_1, &putfd_1_stat);
+	dofstat(putfd_2, &putfd_2_stat);
+	sendfd(fd[0], putfd_1);
+	sendfd(fd[0], putfd_2);
 	close(putfd_1);
 	close(putfd_2);
-	recvfd(test, fd[1], &getfd_1);
-	recvfd(test, fd[1], &getfd_2);
-	dofstat(test, getfd_1, &getfd_1_stat);
-	dofstat(test, getfd_2, &getfd_2_stat);
-	samefile(test, &putfd_1_stat, &getfd_1_stat);
-	samefile(test, &putfd_2_stat, &getfd_2_stat);
+	recvfd(fd[1], &getfd_1);
+	recvfd(fd[1], &getfd_2);
+	dofstat(getfd_1, &getfd_1_stat);
+	dofstat(getfd_2, &getfd_2_stat);
+	samefile(&putfd_1_stat, &getfd_1_stat);
+	samefile(&putfd_2_stat, &getfd_2_stat);
 	close(getfd_1);
 	close(getfd_2);
 	closesocketpair(fd);
+}
 
-	printf("%s passed\n", test);
-
-	/*
-	 * Big bundling test.  Send an endpoint of the UNIX domain socket
-	 * over itself, closing the door behind it.
-	 */
-
-	test = "test5-bundle";
-	printf("beginning %s\n", test);
+/*
+ * Big bundling test.  Send an endpoint of the UNIX domain socket over itself,
+ * closing the door behind it.
+ */
+ATF_TC_WITHOUT_HEAD(bundle);
+ATF_TC_BODY(bundle, tc)
+{
+	int fd[2], getfd;
 
-	domainsocketpair(test, fd);
+	domainsocketpair(fd);
 
-	sendfd(test, fd[0], fd[0]);
+	sendfd(fd[0], fd[0]);
 	close(fd[0]);
-	recvfd(test, fd[1], &getfd_1);
-	close(getfd_1);
+	recvfd(fd[1], &getfd);
+	close(getfd);
 	close(fd[1]);
+}
 
-	printf("%s passed\n", test);
+/*
+ * Big bundling test part two: Send an endpoint of the UNIX domain socket over
+ * itself, close the door behind it, and never remove it from the other end.
+ */
+ATF_TC_WITHOUT_HEAD(bundle_cancel);
+ATF_TC_BODY(bundle_cancel, tc)
+{
+	int fd[2];
 
-	/*
-	 * Big bundling test part two: Send an endpoint of the UNIX domain
-	 * socket over itself, close the door behind it, and never remove it
-	 * from the other end.
-	 */
-
-	test = "test6-bundlecancel";
-	printf("beginning %s\n", test);
-
-	domainsocketpair(test, fd);
-	sendfd(test, fd[0], fd[0]);
-	sendfd(test, fd[1], fd[0]);
+	domainsocketpair(fd);
+	sendfd(fd[0], fd[0]);
+	sendfd(fd[1], fd[0]);
 	closesocketpair(fd);
+}
 
-	printf("%s passed\n", test);
+/*
+ * Test for PR 151758: Send an character device over the UNIX domain socket
+ * and then close both sockets to orphan the device.
+ */
+ATF_TC_WITHOUT_HEAD(devfs_orphan);
+ATF_TC_BODY(devfs_orphan, tc)
+{
+	int fd[2], putfd;
 
-	/*
-	 * Test for PR 151758: Send an character device over the UNIX
-	 * domain socket and then close both sockets to orphan the
-	 * device.
-	 */
-
-	test = "test7-devfsorphan";
-	printf("beginning %s\n", test);
-
-	domainsocketpair(test, fd);
-	devnull(test, &putfd_1);
-	sendfd(test, fd[0], putfd_1);
-	close(putfd_1);
+	domainsocketpair(fd);
+	devnull(&putfd);
+	sendfd(fd[0], putfd);
+	close(putfd);
 	closesocketpair(fd);
+}
 
-	printf("%s passed\n", test);
+#define	LOCAL_SENDSPACE_SYSCTL	"net.local.stream.sendspace"
 
-	/*
-	 * Test for PR 181741. Receiver sets LOCAL_CREDS, and kernel
-	 * prepends a control message to the data. Sender sends large
-	 * payload. Payload + SCM_RIGHTS + LOCAL_CREDS hit socket buffer
-	 * limit, and receiver receives truncated data.
-	 */
-	test = "test8-rights+creds+payload";
-	printf("beginning %s\n", test);
-
-	{
-		const int on = 1;
-		u_long sendspace;
-		size_t len;
-		void *buf;
-
-		len = sizeof(sendspace);
-		if (sysctlbyname("net.local.stream.sendspace", &sendspace,
-		    &len, NULL, 0) < 0)
-			err(-1, "%s: sysctlbyname(net.local.stream.sendspace)",
-			    test);
-
-		if ((buf = calloc(1, sendspace)) == NULL)
-			err(-1, "%s: calloc", test);
-
-		domainsocketpair(test, fd);
-		if (setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on)) < 0)
-			err(-1, "%s: setsockopt(LOCAL_CREDS)", test);
-		tempfile(test, &putfd_1);
-		sendfd_payload(test, fd[0], putfd_1, buf, sendspace);
-		recvfd_payload(test, fd[1], &getfd_1, buf, sendspace);
-		close(putfd_1);
-		close(getfd_1);
-		closesocketpair(fd);
-	}
+/*
+ * Test for PR 181741. Receiver sets LOCAL_CREDS, and kernel prepends a
+ * control message to the data. Sender sends large payload.
+ * Payload + SCM_RIGHTS + LOCAL_CREDS hit socket buffer limit, and receiver
+ * receives truncated data.
+ */
+ATF_TC_WITHOUT_HEAD(rights_creds_payload);
+ATF_TC_BODY(rights_creds_payload, tc)
+{
+	const int on = 1;
+	u_long sendspace;
+	size_t len;
+	void *buf;
+	int fd[2], getfd, putfd, rc;
+
+	atf_tc_expect_fail("PR 181741: Packet loss when 'control' messages "
+	    "are present with large data");
+
+	len = sizeof(sendspace);
+	rc = sysctlbyname(LOCAL_SENDSPACE_SYSCTL, &sendspace,
+	    &len, NULL, 0);
+	ATF_REQUIRE_MSG(rc != -1,
+	    "sysctl %s failed: %s", LOCAL_SENDSPACE_SYSCTL, strerror(errno));
+
+	buf = calloc(1, sendspace);
+	ATF_REQUIRE(buf != NULL);
+
+	domainsocketpair(fd);
+	rc = setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on));
+	ATF_REQUIRE_MSG(rc != -1, "setsockopt(LOCAL_CREDS) failed: %s",
+	    strerror(errno));
+	tempfile(&putfd);
+	sendfd_payload(fd[0], putfd, buf, sendspace);
+	recvfd_payload(fd[1], &getfd, buf, sendspace);
+	close(putfd);
+	close(getfd);
+	closesocketpair(fd);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
 
-	printf("%s passed\n", test);
+	ATF_TP_ADD_TC(tp, simple_send_fd);
+	ATF_TP_ADD_TC(tp, send_and_close);
+	ATF_TP_ADD_TC(tp, send_and_cancel);
+	ATF_TP_ADD_TC(tp, two_files);
+	ATF_TP_ADD_TC(tp, bundle);
+	ATF_TP_ADD_TC(tp, bundle_cancel);
+	ATF_TP_ADD_TC(tp, devfs_orphan);
+	ATF_TP_ADD_TC(tp, rights_creds_payload);
 
-	return (0);
+	return (atf_no_error());
 }


More information about the svn-src-head mailing list