8000 minix/minix/tests/socklib.c at master · arruma2160/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
3600 lines (2801 loc) · 96.7 KB

socklib.c

File metadata and controls

3600 lines (2801 loc) · 96.7 KB
free(nresults);
#endif
}
/*
* Test for setting and retrieving UDP/RAW multicast transmission options.
* This is an interface-level test only: we do not (yet) test whether the
* options have any effect. The given 'type' must be SOCK_DGRAM or SOCK_RAW.
*/
void
socklib_multicast_tx_options(int type)
{
struct in_addr in_addr;
socklen_t len;
unsigned int ifindex;
uint8_t byte;
int fd, val;
subtest = 10;
if ((fd = socket(AF_INET, type, 0)) < 0) e(0);
/*
* Initially, the multicast TTL is expected be 1, looping should be
* enabled, and the multicast source address should be <any>.
*/
byte = 0;
len = sizeof(byte);
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &byte, &len) != 0)
e(0);
if (len != sizeof(byte)) e(0);
if (type != SOCK_STREAM && byte != 1) e(0);
byte = 0;
len = sizeof(byte);
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &byte, &len) != 0)
e(0);
if (len != sizeof(byte)) e(0);
if (byte != 1) e(0);
len = sizeof(in_addr);
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &in_addr, &len) != 0)
e(0);
if (len != sizeof(in_addr)) e(0);
if (in_addr.s_addr != htonl(INADDR_ANY)) e(0);
/* It must not be possible to get/set IPv6 options on IPv4 sockets. */
val = 0;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val,
sizeof(val)) != -1) e(0);
if (errno != ENOPROTOOPT) e(0);
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val,
sizeof(val)) != -1) e(0);
if (errno != ENOPROTOOPT) e(0);
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &val,
sizeof(val) /*wrong but it doesn't matter*/) != -1) e(0);
if (errno != ENOPROTOOPT) e(0);
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val,
&len) != -1) e(0);
if (errno != ENOPROTOOPT) e(0);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val,
&len) != -1) e(0);
if (errno != ENOPROTOOPT) e(0);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &val, &len) != -1)
e(0);
if (errno != ENOPROTOOPT) e(0);
if (close(fd) != 0) e(0);
if ((fd = socket(AF_INET6, type, 0)) < 0) e(0);
/*
* Expect the same defaults as for IPv4. IPV6_MULTICAST_IF uses an
* interface index rather than an IP address, though.
*/
val = 0;
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (type != SOCK_STREAM && val != 1) e(0);
val = 0;
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (val != 1) e(0);
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (val != 0) e(0);
/* It must not be possible to get/set IPv4 options on IPv6 sockets. */
byte = 0;
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &byte,
sizeof(byte)) != -1) e(0);
if (errno != ENOPROTOOPT) e(0);
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &byte,
sizeof(byte)) != -1) e(0);
if (errno != ENOPROTOOPT) e(0);
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &byte,
sizeof(byte) /* wrong but it doesn't matter */) != -1) e(0);
if (errno != ENOPROTOOPT) e(0);
len = sizeof(byte);
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &val, &len) != -1)
e(0);
if (errno != ENOPROTOOPT) e(0);
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &val, &len) != -1)
e(0);
if (errno != ENOPROTOOPT) e(0);
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &val, &len) != -1)
e(0);
if (errno != ENOPROTOOPT) e(0);
if (close(fd) != 0) e(0);
/* Test changing options. */
if ((fd = socket(AF_INET, type, 0)) < 0) e(0);
byte = 129;
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &byte,
sizeof(byte)) != 0) e(0);
byte = 0;
len = sizeof(byte);
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &byte, &len) != 0)
e(0);
if (len != sizeof(byte)) e(0);
if (byte != 129) e(0);
byte = 0;
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &byte,
sizeof(byte)) != 0)
e(0);
byte = 1;
len = sizeof(byte);
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &byte, &len) != 0)
e(0);
if (len != sizeof(byte)) e(0);
if (byte != 0) e(0);
in_addr.s_addr = htonl(INADDR_LOOPBACK);
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &in_addr,
sizeof(in_addr)) != 0)
e(0);
in_addr.s_addr = htonl(INADDR_ANY);
len = sizeof(in_addr);
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &in_addr, &len) != 0)
e(0);
if (len != sizeof(in_addr)) e(0);
if (in_addr.s_addr != htonl(INADDR_LOOPBACK)) e(0);
if (close(fd) != 0) e(0);
if ((fd = socket(AF_INET6, type, 0)) < 0) e(0);
val = 137;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val,
sizeof(val)) != 0) e(0);
val = 0;
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (val != 137) e(0);
val = -2;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val,
sizeof(val)) != -1) e(0);
if (errno != EINVAL) e(0);
val = 256;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val,
sizeof(val)) != -1) e(0);
if (errno != EINVAL) e(0);
val = 0;
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (val != 137) e(0);
val = -1; /* use default */
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val,
sizeof(val)) != 0) e(0);
val = 0;
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (val != 1) e(0);
val = 0;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val,
sizeof(val)) != 0) e(0);
val = 1;
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (val != 0) e(0);
val = 1;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val,
sizeof(val)) != 0) e(0);
val = -1;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val,
sizeof(val)) != -1) e(0);
if (errno != EINVAL) e(0);
val = 2;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val,
sizeof(val)) != -1) e(0);
if (errno != EINVAL) e(0);
val = 0;
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (val != 1) e(0);
val = -1;
ifindex = if_nametoindex(LOOPBACK_IFNAME);
val = ifindex;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &val,
sizeof(val)) != 0) e(0);
val = 0;
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (val != ifindex) e(0);
val = BAD_IFINDEX;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &val,
sizeof(val)) != -1) e(0);
val = -1;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &val,
sizeof(val)) != -1) e(0);
val = 0;
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e(0);
if (val != ifindex) e(0);
val = 0;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &val,
sizeof(val)) != 0) e(0);
val = ifindex;
len = sizeof(val);
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &val, &len) != 0)
e(0);
if (len != sizeof(val)) e 10BC8 (0);
if (val != 0) e(0);
if (close(fd) != 0) e(0);
}
/*
* Test for large sends and receives on stream sockets with MSG_WAITALL.
*/
void
socklib_large_transfers(int fd[2])
{
char *buf;
pid_t pid;
int i, status;
#define LARGE_BUF (4096*1024)
if ((buf = malloc(LARGE_BUF)) == NULL) e(0);
memset(buf, 0, LARGE_BUF);
pid = fork();
switch (pid) {
case 0:
errct = 0;
if (close(fd[0]) != 0) e(0);
/* Part 1. */
if (recv(fd[1], buf, LARGE_BUF, MSG_WAITALL) != LARGE_BUF)
e(0);
for (i = 0; i < LARGE_BUF; i++)
if (buf[i] != (char)(i + (i >> 16))) e(0);
if (recv(fd[1], buf, LARGE_BUF,
MSG_DONTWAIT | MSG_WAITALL) != -1) e(0);
if (errno != EWOULDBLOCK) e(0);
/* Part 2. */
if (send(fd[1], buf, LARGE_BUF / 2, 0) != LARGE_BUF / 2) e(0);
if (shutdown(fd[1], SHUT_WR) != 0) e(0);
/* Part 3. */
memset(buf, 'y', LARGE_BUF);
if (recv(fd[1], buf, LARGE_BUF, MSG_WAITALL) != LARGE_BUF - 1)
e(0);
for (i = 0; i < LARGE_BUF - 1; i++)
if (buf[i] != (char)(i + (i >> 16))) e(0);
if (buf[LARGE_BUF - 1] != 'y') e(0);
if (recv(fd[1], buf, LARGE_BUF, MSG_WAITALL) != 0) e(0);
exit(errct);
case -1:
e(0);
}
if (close(fd[1]) != 0) e(0);
/* Part 1: check that a large send fully arrives. */
for (i = 0; i < LARGE_BUF; i++)
buf[i] = (char)(i + (i >> 16));
if (send(fd[0], buf, LARGE_BUF, 0) != LARGE_BUF) e(0);
/* Part 2: check that remote shutdown terminates a partial receive. */
memset(buf, 'x', LARGE_BUF);
if (recv(fd[0], buf, LARGE_BUF, MSG_WAITALL) != LARGE_BUF / 2) e(0);
for (i = 0; i < LARGE_BUF / 2; i++)
if (buf[i] != (char)(i + (i >> 16))) e(0);
for (; i < LARGE_BUF; i++)
if (buf[i] != 'x') e(0);
if (recv(fd[0], buf, LARGE_BUF, MSG_WAITALL) != 0) e(0);
/* Part 3: check that remote close terminates a partial receive. */
for (i = 0; i < LARGE_BUF; i++)
buf[i] = (char)(i + (i >> 16));
if (send(fd[0], buf, LARGE_BUF - 1, 0) != LARGE_BUF - 1) e(0);
if (close(fd[0]) != 0) e(0);
if (waitpid(pid, &status, 0) != pid) e(0);
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) e(0);
free(buf);
}
#define PRINT_STATS 0
/*
* A randomized producer-consumer test for stream sockets. As part of this,
* we also perform very basic bulk functionality tests of FIONREAD, MSG_PEEK,
* MSG_DONTWAIT, and MSG_WAITALL.
*/
void
socklib_producer_consumer(int fd[2])
{
char *buf;
time_t t;
socklen_t len, size, off;
ssize_t r;
pid_t pid;
int i, rcvlen, status, exp, flags, num, stat[3] = { 0, 0, 0 };
len = sizeof(rcvlen);
if (getsockopt(fd[0], SOL_SOCKET, SO_RCVBUF, &rcvlen, &len) != 0) e(0);
if (len != sizeof(rcvlen)) e(0);
size = rcvlen * 3;
if ((buf = malloc(size)) == NULL) e(0);
t = time(NULL);
/*
* We vary small versus large (random) send and receive sizes,
* splitting the entire transfer in four phases along those lines.
*
* In theory, the use of an extra system call, the use of MSG_PEEK, and
* the fact that without MSG_WAITALL a receive call may return any
* partial result, all contribute to the expectation that the consumer
* side will fall behind the producer. In order to test both filling
* and draining the receive queue, we use a somewhat larger small
* receive size for the consumer size (up to 256 bytes rather than 64)
* during each half of the four phases. The effectiveness of these
* numbers can be verified with statistics (disabled by default).
*/
#define TRANSFER_SIZE (16 * 1024 * 1024)
pid = fork();
switch (pid) {
case 0:
errct = 0;
if (close(fd[0]) != 0) e(0);
srand48(t + 1);
for (off = 0; off < TRANSFER_SIZE; ) {
if (off < TRANSFER_SIZE / 2)
len = lrand48() %
((off / (TRANSFER_SIZE / 8) % 2) ? 64 :
256);
else
len = lrand48() % size;
num = lrand48() % 16;
flags = 0;
if (num & 1) flags |= MSG_PEEK;
if (num & 2) flags |= MSG_WAITALL;
if (num & 4) flags |= MSG_DONTWAIT;
if (num & 8) {
/*
* Obviously there are race conditions here but
* the returned number should be a lower bound.
*/
if (ioctl(fd[1], FIONREAD, &exp) != 0) e(0);
if (exp < 0 || exp > rcvlen) e(0);
} else
exp = -1;
stat[0]++;
if ((r = recv(fd[1], buf, len, flags)) == -1) {
if (errno != EWOULDBLOCK) e(0);
if (exp > 0) e(0);
stat[2]++;
continue;
}
if (r < len) {
stat[1]++;
if (exp > r) e(0);
}
for (i = 0; i < r; i++)
if (buf[i] != (char)((off + i) +
((off + i) >> 16))) e(0);
if (!(flags & MSG_PEEK)) {
off += r;
if ((flags & (MSG_DONTWAIT | MSG_WAITALL)) ==
MSG_WAITALL && r != len &&
off < TRANSFER_SIZE) e(0);
}
}
#if PRINT_STATS
/*
* The second and third numbers should ideally be a large but
* non-dominating fraction of the first one.
*/
printf("RECV: total %d short %d again %d\n",
stat[0], stat[1], stat[2]);
#endif
if (close(fd[1]) != 0) e(0);
exit(errct);
case -1:
e(0);
}
if (close(fd[1]) != 0) e(0);
srand48(t);
for (off = 0; off < TRANSFER_SIZE; ) {
if (off < TRANSFER_SIZE / 4 ||
(off >= TRANSFER_SIZE / 2 && off < TRANSFER_SIZE * 3 / 4))
len = lrand48() % 64;
else
len = lrand48() % size;
if (len > TRANSFER_SIZE - off)
len = TRANSFER_SIZE - off;
for (i = 0; i < len; i++)
buf[i] = (off + i) + ((off + i) >> 16);
flags = (lrand48() % 2) ? MSG_DONTWAIT : 0;
stat[0]++;
r = send(fd[0], buf, len, flags);
if (r != len) {
if (r > (ssize_t)len) e(0);
if (!(flags & MSG_DONTWAIT)) e(0);
if (r == -1) {
if (errno != EWOULDBLOCK) e(0);
r = 0;
stat[2]++;
} else
stat[1]++;
}
if (off / (TRANSFER_SIZE / 4) !=
(off + r) / (TRANSFER_SIZE / 4))
sleep(1);
off += r;
0