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"
10
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)))
13
main(void)14 int main(void)
15 {
16 struct sockaddr_in sa = { .sin_family = AF_INET };
17 int s, c, t;
18 char buf[100];
19
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);
23
24 TESTE(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO,
25 &(struct timeval){.tv_usec=1}, sizeof(struct timeval))==0);
26
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]);
32
33 close(c);
34 close(s);
35
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);
43
44 TESTE(listen(s, 1)==0);
45
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");
48
49 TESTE(connect(c, (void *)&sa, sizeof sa)==0 || errno==EINPROGRESS);
50
51 TESTE((t=accept(s, (void *)&sa, &(socklen_t){sizeof sa}))>=0);
52
53 close(t);
54 close(c);
55 close(s);
56
57 return t_status;
58 }
59