Convert unix_passfd_test to ATF

Mark test 8 as an expected failure so it no longer counts as an unexpected one
This commit is contained in:
ngie 2015-04-15 11:48:41 +00:00
parent ea6ad5f1a5
commit a98cd7cd0e
2 changed files with 203 additions and 198 deletions

View File

@ -31,8 +31,7 @@ PLAIN_TESTS_C+= unix_close_race_test
TAP_TESTS_SH+= unix_cmsg_test TAP_TESTS_SH+= unix_cmsg_test
# unix_gc: twosome_drop1: sendfd: before 0 after 0 # unix_gc: twosome_drop1: sendfd: before 0 after 0
PLAIN_TESTS_C+= unix_gc_test PLAIN_TESTS_C+= unix_gc_test
# unix_passfd: test8-rights+creds+payload: recvmsg: 24 bytes received ATF_TESTS_C+= unix_passfd_test
PLAIN_TESTS_C+= unix_passfd_test
PLAIN_TESTS_C+= unix_sendtorace_test PLAIN_TESTS_C+= unix_sendtorace_test
# unix_socket: socket(PF_LOCAL, SOCK_RAW, 0): Protocol wrong type for socket # unix_socket: socket(PF_LOCAL, SOCK_RAW, 0): Protocol wrong type for socket
PLAIN_TESTS_C+= unix_socket_test PLAIN_TESTS_C+= unix_socket_test

View File

@ -32,14 +32,17 @@
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <sys/un.h> #include <sys/un.h>
#include <err.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <limits.h> #include <limits.h>
#include <paths.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <atf-c.h>
/* /*
* UNIX domain sockets allow file descriptors to be passed via "ancillary * UNIX domain sockets allow file descriptors to be passed via "ancillary
* data", or control messages. This regression test is intended to exercise * data", or control messages. This regression test is intended to exercise
@ -50,11 +53,12 @@
*/ */
static void static void
domainsocketpair(const char *test, int *fdp) domainsocketpair(int *fdp)
{ {
if (socketpair(PF_UNIX, SOCK_STREAM, 0, fdp) < 0) ATF_REQUIRE_MSG(socketpair(PF_UNIX, SOCK_STREAM, 0, fdp) != -1,
err(-1, "%s: socketpair(PF_UNIX, SOCK_STREAM)", test); "socketpair(PF_UNIX, SOCK_STREAM, ..) failed: %s, ",
strerror(errno));
} }
static void static void
@ -66,50 +70,38 @@ closesocketpair(int *fdp)
} }
static void static void
devnull(const char *test, int *fdp) tempfile(int *fdp)
{
int fd;
fd = open("/dev/null", O_RDONLY);
if (fd < 0)
err(-1, "%s: open(/dev/null)", test);
*fdp = fd;
}
static void
tempfile(const char *test, int *fdp)
{ {
char path[PATH_MAX]; char path[PATH_MAX];
int fd; int fd;
snprintf(path, PATH_MAX, "/tmp/unix_passfd.XXXXXXXXXXXXXXX"); snprintf(path, PATH_MAX, "unix_passfd.XXXXXXXXXXXXXXX");
fd = mkstemp(path); fd = mkstemp(path);
if (fd < 0) ATF_REQUIRE_MSG(fd != -1, "mkstemp failed: %s", strerror(errno));
err(-1, "%s: mkstemp(%s)", test, path);
(void)unlink(path); (void)unlink(path);
*fdp = fd; *fdp = fd;
} }
static void static void
dofstat(const char *test, int fd, struct stat *sb) dofstat(int fd, struct stat *sb)
{ {
if (fstat(fd, sb) < 0) ATF_REQUIRE_MSG(fstat(fd, sb) != -1, "fstat failed: %s",
err(-1, "%s: fstat", test); strerror(errno));
} }
static void 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) ATF_REQUIRE_EQ_MSG(sb1->st_dev, sb2->st_dev,
errx(-1, "%s: samefile: different device", test); "different devices (%d != %d)", sb1->st_dev, sb2->st_dev);
if (sb1->st_ino != sb2->st_ino) ATF_REQUIRE_EQ_MSG(sb1->st_dev, sb2->st_dev,
errx(-1, "%s: samefile: different inode", test); "different inodes (%u != %u)", sb1->st_ino, sb2->st_ino);
} }
static void static void
sendfd_payload(const char *test, int sockfd, int sendfd, sendfd_payload(int sockfd, int sendfd,
void *payload, size_t paylen) void *payload, size_t paylen)
{ {
struct iovec iovec; struct iovec iovec;
@ -137,23 +129,22 @@ sendfd_payload(const char *test, int sockfd, int sendfd,
*(int *)CMSG_DATA(cmsghdr) = sendfd; *(int *)CMSG_DATA(cmsghdr) = sendfd;
len = sendmsg(sockfd, &msghdr, 0); len = sendmsg(sockfd, &msghdr, 0);
if (len < 0) ATF_REQUIRE_MSG(len != -1, "sendmsg failed: %s", strerror(errno));
err(-1, "%s: sendmsg", test); ATF_REQUIRE_MSG((size_t)len == paylen,
if ((size_t)len != paylen) "mismatch with amount of data sent via sendmsg (%zd != %zu)",
errx(-1, "%s: sendmsg: %zd bytes sent", test, len); len, paylen);
} }
static void static void
sendfd(const char *test, int sockfd, int sendfd) sendfd(int sockfd, int sendfd)
{ {
char ch; char ch;
return (sendfd_payload(test, sockfd, sendfd, &ch, sizeof(ch))); return (sendfd_payload(sockfd, sendfd, &ch, sizeof(ch)));
} }
static void static void
recvfd_payload(const char *test, int sockfd, int *recvfd, recvfd_payload(int sockfd, int *recvfd, void *buf, size_t buflen)
void *buf, size_t buflen)
{ {
struct cmsghdr *cmsghdr; struct cmsghdr *cmsghdr;
char message[CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + sizeof(int)]; char message[CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + sizeof(int)];
@ -173,217 +164,232 @@ recvfd_payload(const char *test, int sockfd, int *recvfd,
msghdr.msg_iovlen = 1; msghdr.msg_iovlen = 1;
len = recvmsg(sockfd, &msghdr, 0); len = recvmsg(sockfd, &msghdr, 0);
if (len < 0) ATF_REQUIRE_MSG(len != -1, "recvmsg failed: %s", strerror(errno));
err(-1, "%s: recvmsg", test); ATF_REQUIRE_MSG((size_t)len == buflen,
if ((size_t)len != buflen) "mismatch with amount of data sent via recvmsg (%zd != %zu)",
errx(-1, "%s: recvmsg: %zd bytes received", test, len); len, buflen);
cmsghdr = CMSG_FIRSTHDR(&msghdr); cmsghdr = CMSG_FIRSTHDR(&msghdr);
if (cmsghdr == NULL) ATF_REQUIRE_MSG(cmsghdr != NULL, "did not receive control message");
errx(-1, "%s: recvmsg: did not receive control message", test);
*recvfd = -1; *recvfd = -1;
for (; cmsghdr != NULL; cmsghdr = CMSG_NXTHDR(&msghdr, cmsghdr)) { for (; cmsghdr != NULL; cmsghdr = CMSG_NXTHDR(&msghdr, cmsghdr)) {
if (cmsghdr->cmsg_level == SOL_SOCKET && if (cmsghdr->cmsg_level == SOL_SOCKET &&
cmsghdr->cmsg_type == SCM_RIGHTS && cmsghdr->cmsg_type == SCM_RIGHTS &&
cmsghdr->cmsg_len == CMSG_LEN(sizeof(int))) { cmsghdr->cmsg_len == CMSG_LEN(sizeof(int))) {
*recvfd = *(int *)CMSG_DATA(cmsghdr); *recvfd = *(int *)CMSG_DATA(cmsghdr);
if (*recvfd == -1) ATF_REQUIRE(*recvfd != -1);
errx(-1, "%s: recvmsg: received fd -1", test);
} }
} }
if (*recvfd == -1) ATF_REQUIRE_MSG(*recvfd != -1,
errx(-1, "%s: recvmsg: did not receive single-fd message", "recvmsg did not receive a single fd message");
test);
} }
static void static void
recvfd(const char *test, int sockfd, int *recvfd) recvfd(int sockfd, int *recvfd)
{ {
char ch; char ch;
return (recvfd_payload(test, sockfd, recvfd, &ch, sizeof(ch))); return (recvfd_payload(sockfd, recvfd, &ch, sizeof(ch)));
} }
int /*
main(void) * 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.
*/
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; struct stat getfd_1_stat, putfd_1_stat;
int fd[2], putfd_1, putfd_2, getfd_1, getfd_2; int fd[2], getfd_1, putfd_1;
const char *test;
/* domainsocketpair(fd);
* First test: put a temporary file into a UNIX domain socket, then tempfile(&putfd_1);
* take it out and make sure it's the same file. First time around, dofstat(putfd_1, &putfd_1_stat);
* don't close the reference after sending. sendfd(fd[0], putfd_1);
*/ recvfd(fd[1], &getfd_1);
test = "test1-simplesendfd"; dofstat(getfd_1, &getfd_1_stat);
printf("beginning %s\n", test); samefile(&putfd_1_stat, &getfd_1_stat);
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(putfd_1);
close(getfd_1); close(getfd_1);
closesocketpair(fd); closesocketpair(fd);
}
printf("%s passed\n", test); /*
* 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.
*/
ATF_TC_WITHOUT_HEAD(send_and_close);
ATF_TC_BODY(send_and_close, tc)
{
struct stat getfd_1_stat, putfd_1_stat;
int fd[2], getfd_1, putfd_1;
/* domainsocketpair(fd);
* Second test: same as first, only close the file reference after tempfile(&putfd_1);
* sending, so that the only reference is the descriptor in the UNIX dofstat(putfd_1, &putfd_1_stat);
* domain socket buffer. sendfd(fd[0], putfd_1);
*/
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); close(putfd_1);
recvfd(test, fd[1], &getfd_1); recvfd(fd[1], &getfd_1);
dofstat(test, getfd_1, &getfd_1_stat); dofstat(getfd_1, &getfd_1_stat);
samefile(test, &putfd_1_stat, &getfd_1_stat); samefile(&putfd_1_stat, &getfd_1_stat);
close(getfd_1); close(getfd_1);
closesocketpair(fd); closesocketpair(fd);
printf("%s passed\n", test); }
/* /*
* Third test: put a temporary file into a UNIX domain socket, then * Third test: put a temporary file into a UNIX domain socket, then
* close both endpoints causing garbage collection to kick off. * close both endpoints causing garbage collection to kick off.
*/ */
test = "test3-sendandcancel"; ATF_TC_WITHOUT_HEAD(send_and_cancel);
printf("beginning %s\n", test); ATF_TC_BODY(send_and_cancel, tc)
{
int fd[2], putfd_1;
domainsocketpair(test, fd); domainsocketpair(fd);
tempfile(test, &putfd_1); tempfile(&putfd_1);
sendfd(test, fd[0], putfd_1); sendfd(fd[0], putfd_1);
close(putfd_1); close(putfd_1);
closesocketpair(fd); 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;
/* domainsocketpair(fd);
* Send two files. Then receive them. Make sure they are returned tempfile(&putfd_1);
* in the right order, and both get there. tempfile(&putfd_2);
*/ dofstat(putfd_1, &putfd_1_stat);
dofstat(putfd_2, &putfd_2_stat);
test = "test4-twofile"; sendfd(fd[0], putfd_1);
printf("beginning %s\n", test); sendfd(fd[0], putfd_2);
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);
close(putfd_1); close(putfd_1);
close(putfd_2); close(putfd_2);
recvfd(test, fd[1], &getfd_1); recvfd(fd[1], &getfd_1);
recvfd(test, fd[1], &getfd_2); recvfd(fd[1], &getfd_2);
dofstat(test, getfd_1, &getfd_1_stat); dofstat(getfd_1, &getfd_1_stat);
dofstat(test, getfd_2, &getfd_2_stat); dofstat(getfd_2, &getfd_2_stat);
samefile(test, &putfd_1_stat, &getfd_1_stat); samefile(&putfd_1_stat, &getfd_1_stat);
samefile(test, &putfd_2_stat, &getfd_2_stat); samefile(&putfd_2_stat, &getfd_2_stat);
close(getfd_1); close(getfd_1);
close(getfd_2); close(getfd_2);
closesocketpair(fd); 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.
*/
ATF_TC_WITHOUT_HEAD(bundle);
ATF_TC_BODY(bundle, tc)
{
int fd[2], getfd_1;
/* domainsocketpair(fd);
* Big bundling test. Send an endpoint of the UNIX domain socket
* over itself, closing the door behind it.
*/
test = "test5-bundle"; sendfd(fd[0], fd[0]);
printf("beginning %s\n", test);
domainsocketpair(test, fd);
sendfd(test, fd[0], fd[0]);
close(fd[0]); close(fd[0]);
recvfd(test, fd[1], &getfd_1); recvfd(fd[1], &getfd_1);
close(getfd_1); close(getfd_1);
close(fd[1]); 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];
/* domainsocketpair(fd);
* Big bundling test part two: Send an endpoint of the UNIX domain sendfd(fd[0], fd[0]);
* socket over itself, close the door behind it, and never remove it sendfd(fd[1], fd[0]);
* 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]);
closesocketpair(fd); 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_1;
/* domainsocketpair(fd);
* Test for PR 151758: Send an character device over the UNIX putfd_1 = open(_PATH_DEVNULL, O_RDONLY);
* domain socket and then close both sockets to orphan the ATF_REQUIRE_MSG(putfd_1 != -1,
* device. "opening %s failed: %s", _PATH_DEVNULL, strerror(errno));
*/ sendfd(fd[0], putfd_1);
test = "test7-devfsorphan";
printf("beginning %s\n", test);
domainsocketpair(test, fd);
devnull(test, &putfd_1);
sendfd(test, fd[0], putfd_1);
close(putfd_1); close(putfd_1);
closesocketpair(fd); closesocketpair(fd);
}
printf("%s passed\n", test);
#define LOCAL_STREAM_SENDSPACE "net.local.stream.sendspace"
/*
* Test for PR 181741. Receiver sets LOCAL_CREDS, and kernel /*
* prepends a control message to the data. Sender sends large * Test for PR 181741. Receiver sets LOCAL_CREDS, and kernel
* payload. Payload + SCM_RIGHTS + LOCAL_CREDS hit socket buffer * prepends a control message to the data. Sender sends large
* limit, and receiver receives truncated data. * 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); ATF_TC_WITHOUT_HEAD(rights_with_LOCAL_CREDS_and_large_payload);
ATF_TC_BODY(rights_with_LOCAL_CREDS_and_large_payload, tc)
{ {
const int on = 1; void *buf;
u_long sendspace; int fd[2];
size_t len; size_t len;
void *buf; u_long sendspace;
const int on = 1;
len = sizeof(sendspace); int getfd_1, putfd_1, rc;
if (sysctlbyname("net.local.stream.sendspace", &sendspace,
&len, NULL, 0) < 0) atf_tc_expect_fail("Bug 181741 has not been fixed yet");
err(-1, "%s: sysctlbyname(net.local.stream.sendspace)",
test); len = sizeof(sendspace);
rc = sysctlbyname(LOCAL_STREAM_SENDSPACE, &sendspace, &len, NULL, 0);
if ((buf = malloc(sendspace)) == NULL) ATF_REQUIRE_MSG(rc == 0, "sysctlbyname %s failed: %s",
err(-1, "%s: malloc", test); LOCAL_STREAM_SENDSPACE, strerror(errno));
domainsocketpair(test, fd); ATF_REQUIRE((buf = malloc(sendspace)) != NULL);
if (setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on)) < 0)
err(-1, "%s: setsockopt(LOCAL_CREDS)", test); domainsocketpair(fd);
tempfile(test, &putfd_1); rc = setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on));
sendfd_payload(test, fd[0], putfd_1, buf, sendspace); ATF_REQUIRE_MSG(rc == 0, "setsockopt failed: %s", strerror(errno));
recvfd_payload(test, fd[1], &getfd_1, buf, sendspace);
close(putfd_1); tempfile(&putfd_1);
close(getfd_1);
closesocketpair(fd); sendfd_payload(fd[0], putfd_1, buf, sendspace);
} recvfd_payload(fd[1], &getfd_1, buf, sendspace);
printf("%s passed\n", test); close(putfd_1);
close(getfd_1);
return (0); closesocketpair(fd);
}
ATF_TP_ADD_TCS(tp)
{
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_with_LOCAL_CREDS_and_large_payload);
return (atf_no_error());
} }