conformance fixes
[libc-test.git] / src / functional / socket.c
blobb62cf988f6ce354eeb64d799c2055978b4ff3995
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <sys/socket.h>
6 #include <netinet/in.h>
7 #include <sys/time.h>
8 #include <fcntl.h>
9 #include "test.h"
11 #define TEST(c, ...) ((c) ? 1 : (t_error(#c" failed: " __VA_ARGS__),0))
12 #define TESTE(c) (errno=0, TEST(c, "errno = %s\n", strerror(errno)))
14 int main(void)
16 struct sockaddr_in sa = { .sin_family = AF_INET };
17 int s, c, t;
18 char buf[100];
20 TESTE((s=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP))>=0);
21 TESTE(bind(s, (void *)&sa, sizeof sa)==0);
22 TESTE(getsockname(s, (void *)&sa, (socklen_t[]){sizeof sa})==0);
24 TESTE(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO,
25 &(struct timeval){.tv_usec=1}, sizeof(struct timeval))==0);
27 TESTE((c=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP))>=0);
28 sa.sin_addr.s_addr = htonl(0x7f000001);
29 TESTE(sendto(c, "x", 1, 0, (void *)&sa, sizeof sa)==1);
30 TESTE(recvfrom(s, buf, sizeof buf, 0, (void *)&sa, (socklen_t[]){sizeof sa})==1);
31 TEST(buf[0]=='x', "'%c'\n", buf[0]);
33 close(c);
34 close(s);
36 memset(&sa, 0, sizeof sa);
37 sa.sin_family = AF_INET;
38 TESTE((s=socket(PF_INET, SOCK_STREAM|SOCK_CLOEXEC, IPPROTO_TCP))>=0);
39 TEST(fcntl(s, F_GETFD)&FD_CLOEXEC, "SOCK_CLOEXEC did not work\n");
40 TESTE(bind(s, (void *)&sa, sizeof sa)==0);
41 TESTE(getsockname(s, (void *)&sa, (socklen_t[]){sizeof sa})==0);
42 sa.sin_addr.s_addr = htonl(0x7f000001);
44 TESTE(listen(s, 1)==0);
46 TESTE((c=socket(PF_INET, SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP))>=0);
47 TEST(fcntl(c, F_GETFL)&O_NONBLOCK, "SOCK_NONBLOCK did not work\n");
49 TESTE(connect(c, (void *)&sa, sizeof sa)==0 || errno==EINPROGRESS);
51 TESTE((t=accept(s, (void *)&sa, &(socklen_t){sizeof sa}))>=0);
53 close(t);
54 close(c);
55 close(s);
57 return t_status;