Sun May 3 23:19:59 2009 UTC ()
In addition to testing the fd passing doesn't crash the kernel,
also check that it actually works.


(pooka)
diff -r1.1 -r1.2 src/tests/syscall/t_cmsg.c

cvs diff -r1.1 -r1.2 src/tests/syscall/Attic/t_cmsg.c (expand / switch to unified diff)

--- src/tests/syscall/Attic/t_cmsg.c 2009/02/10 13:43:54 1.1
+++ src/tests/syscall/Attic/t_cmsg.c 2009/05/03 23:19:59 1.2
@@ -1,38 +1,40 @@ @@ -1,38 +1,40 @@
1/* $NetBSD: t_cmsg.c,v 1.1 2009/02/10 13:43:54 pooka Exp $ */ 1/* $NetBSD: t_cmsg.c,v 1.2 2009/05/03 23:19:59 pooka Exp $ */
2 2
3#include <sys/types.h> 3#include <sys/types.h>
4#include <sys/socket.h> 4#include <sys/socket.h>
5 5
6#include <rump/rump.h> 6#include <rump/rump.h>
7#include <rump/rump_syscalls.h> 7#include <rump/rump_syscalls.h>
8 8
9#include <atf-c.h> 9#include <atf-c.h>
10#include <err.h> 10#include <err.h>
11#include <errno.h> 11#include <errno.h>
12#include <stdio.h> 12#include <stdio.h>
13#include <stdlib.h> 13#include <stdlib.h>
14#include <string.h> 14#include <string.h>
15#include <unistd.h> 15#include <unistd.h>
16#include <util.h> 16#include <util.h>
17 17
18ATF_TC(cmsg_sendfd); 18#include "../h_macros.h"
19ATF_TC_HEAD(cmsg_sendfd, tc) 19
 20ATF_TC(cmsg_sendfd_bounds);
 21ATF_TC_HEAD(cmsg_sendfd_bounds, tc)
20{ 22{
21 atf_tc_set_md_var(tc, "descr", "Checks that attempting to pass an " 23 atf_tc_set_md_var(tc, "descr", "Checks that attempting to pass an "
22 "invalid fd returns an error"); 24 "invalid fd returns an error");
23} 25}
24 26
25ATF_TC_BODY(cmsg_sendfd, tc) 27ATF_TC_BODY(cmsg_sendfd_bounds, tc)
26{ 28{
27 struct cmsghdr *cmp; 29 struct cmsghdr *cmp;
28 struct msghdr msg; 30 struct msghdr msg;
29 struct iovec iov; 31 struct iovec iov;
30 ssize_t n; 32 ssize_t n;
31 int s[2]; 33 int s[2];
32 int rv, error = 0; 34 int rv, error = 0;
33 35
34 rump_init(); 36 rump_init();
35 37
36 if (rump_sys_socketpair(AF_LOCAL, SOCK_STREAM, 0, s) == -1) 38 if (rump_sys_socketpair(AF_LOCAL, SOCK_STREAM, 0, s) == -1)
37 atf_tc_fail("rump_sys_socket"); 39 atf_tc_fail("rump_sys_socket");
38 40
@@ -56,17 +58,74 @@ ATF_TC_BODY(cmsg_sendfd, tc) @@ -56,17 +58,74 @@ ATF_TC_BODY(cmsg_sendfd, tc)
56 /* 58 /*
57 * ERROR HERE: trying to pass invalid fd 59 * ERROR HERE: trying to pass invalid fd
58 * (This value was previously directly used to index the fd 60 * (This value was previously directly used to index the fd
59 * array and therefore we are passing a hyperspace index) 61 * array and therefore we are passing a hyperspace index)
60 */ 62 */
61 *(int *)CMSG_DATA(cmp) = 0x12345678; 63 *(int *)CMSG_DATA(cmp) = 0x12345678;
62 64
63 rump_sys_sendmsg(s[0], &msg, 0); 65 rump_sys_sendmsg(s[0], &msg, 0);
64 if (errno != EBADF) 66 if (errno != EBADF)
65 atf_tc_fail("descriptor passing failed: expected EBADF (9), " 67 atf_tc_fail("descriptor passing failed: expected EBADF (9), "
66 "got %d\n(%s)", errno, strerror(errno)); 68 "got %d\n(%s)", errno, strerror(errno));
67} 69}
68 70
 71
 72ATF_TC(cmsg_sendfd);
 73ATF_TC_HEAD(cmsg_sendfd, tc)
 74{
 75 atf_tc_set_md_var(tc, "descr", "Checks that fd passing works");
 76 atf_tc_set_md_var(tc, "timeout", "2");
 77}
 78
 79ATF_TC_BODY(cmsg_sendfd, tc)
 80{
 81 struct cmsghdr *cmp;
 82 struct msghdr msg;
 83 struct iovec iov;
 84 ssize_t n;
 85 int s[2], sgot;
 86 int rv, error = 0;
 87 int v1, v2;
 88
 89 rump_init();
 90
 91 if (rump_sys_socketpair(AF_LOCAL, SOCK_STREAM, 0, s) == -1)
 92 atf_tc_fail("rump_sys_socketpair");
 93
 94 rv = 0;
 95 cmp = malloc(CMSG_LEN(sizeof(int)));
 96
 97 iov.iov_base = &error;
 98 iov.iov_len = sizeof(int);
 99
 100 cmp->cmsg_level = SOL_SOCKET;
 101 cmp->cmsg_type = SCM_RIGHTS;
 102 cmp->cmsg_len = CMSG_LEN(sizeof(int));
 103
 104 msg.msg_iov = &iov;
 105 msg.msg_iovlen = 1;
 106 msg.msg_name = NULL;
 107 msg.msg_namelen = 0;
 108 msg.msg_control = cmp;
 109 msg.msg_controllen = CMSG_LEN(sizeof(int));
 110 *(int *)CMSG_DATA(cmp) = s[1];
 111
 112 if (rump_sys_sendmsg(s[0], &msg, 0) == -1)
 113 atf_tc_fail_errno("sendmsg failed");
 114 if (rump_sys_recvmsg(s[1], &msg, 0) == -1)
 115 atf_tc_fail_errno("recvmsg failed");
 116 sgot = *(int *)CMSG_DATA(cmp);
 117
 118 /* test that we really got the fd by writing something thought it */
 119 v1 = 37;
 120 v2 = -1;
 121 rump_sys_write(s[0], &v1, sizeof(v1));
 122 rump_sys_read(sgot, &v2, sizeof(v1));
 123 if (v1 != v2)
 124 atf_tc_fail("value mismatch: %d vs. %d", v1, v2);
 125}
 126
69ATF_TP_ADD_TCS(tp) 127ATF_TP_ADD_TCS(tp)
70{ 128{
71 ATF_TP_ADD_TC(tp, cmsg_sendfd); 129 ATF_TP_ADD_TC(tp, cmsg_sendfd);
 130 ATF_TP_ADD_TC(tp, cmsg_sendfd_bounds);
72} 131}