1570af302Sopenharmony_ci#include <stdio.h>
2570af302Sopenharmony_ci#include <unistd.h>
3570af302Sopenharmony_ci#include <errno.h>
4570af302Sopenharmony_ci#include <string.h>
5570af302Sopenharmony_ci#include <sys/socket.h>
6570af302Sopenharmony_ci#include <netinet/in.h>
7570af302Sopenharmony_ci#include <sys/time.h>
8570af302Sopenharmony_ci#include <fcntl.h>
9570af302Sopenharmony_ci#include "test.h"
10570af302Sopenharmony_ci
11570af302Sopenharmony_ci#define TEST(c, ...) ((c) ? 1 : (t_error(#c" failed: " __VA_ARGS__),0))
12570af302Sopenharmony_ci#define TESTE(c) (errno=0, TEST(c, "errno = %s\n", strerror(errno)))
13570af302Sopenharmony_ci
14570af302Sopenharmony_ciint main(void)
15570af302Sopenharmony_ci{
16570af302Sopenharmony_ci	struct sockaddr_in sa = { .sin_family = AF_INET };
17570af302Sopenharmony_ci	int s, c, t;
18570af302Sopenharmony_ci	char buf[100];
19570af302Sopenharmony_ci
20570af302Sopenharmony_ci	TESTE((s=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP))>=0);
21570af302Sopenharmony_ci	TESTE(bind(s, (void *)&sa, sizeof sa)==0);
22570af302Sopenharmony_ci	TESTE(getsockname(s, (void *)&sa, (socklen_t[]){sizeof sa})==0);
23570af302Sopenharmony_ci
24570af302Sopenharmony_ci	TESTE(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO,
25570af302Sopenharmony_ci		&(struct timeval){.tv_usec=1}, sizeof(struct timeval))==0);
26570af302Sopenharmony_ci
27570af302Sopenharmony_ci	TESTE((c=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP))>=0);
28570af302Sopenharmony_ci	sa.sin_addr.s_addr = htonl(0x7f000001);
29570af302Sopenharmony_ci	TESTE(sendto(c, "x", 1, 0, (void *)&sa, sizeof sa)==1);
30570af302Sopenharmony_ci	TESTE(recvfrom(s, buf, sizeof buf, 0, (void *)&sa, (socklen_t[]){sizeof sa})==1);
31570af302Sopenharmony_ci	TEST(buf[0]=='x', "'%c'\n", buf[0]);
32570af302Sopenharmony_ci
33570af302Sopenharmony_ci	close(c);
34570af302Sopenharmony_ci	close(s);
35570af302Sopenharmony_ci
36570af302Sopenharmony_ci	memset(&sa, 0, sizeof sa);
37570af302Sopenharmony_ci	sa.sin_family = AF_INET;
38570af302Sopenharmony_ci	TESTE((s=socket(PF_INET, SOCK_STREAM|SOCK_CLOEXEC, IPPROTO_TCP))>=0);
39570af302Sopenharmony_ci	TEST(fcntl(s, F_GETFD)&FD_CLOEXEC, "SOCK_CLOEXEC did not work\n");
40570af302Sopenharmony_ci	TESTE(bind(s, (void *)&sa, sizeof sa)==0);
41570af302Sopenharmony_ci	TESTE(getsockname(s, (void *)&sa, (socklen_t[]){sizeof sa})==0);
42570af302Sopenharmony_ci	sa.sin_addr.s_addr = htonl(0x7f000001);
43570af302Sopenharmony_ci
44570af302Sopenharmony_ci	TESTE(listen(s, 1)==0);
45570af302Sopenharmony_ci
46570af302Sopenharmony_ci	TESTE((c=socket(PF_INET, SOCK_STREAM|SOCK_NONBLOCK, IPPROTO_TCP))>=0);
47570af302Sopenharmony_ci	TEST(fcntl(c, F_GETFL)&O_NONBLOCK, "SOCK_NONBLOCK did not work\n");
48570af302Sopenharmony_ci
49570af302Sopenharmony_ci	TESTE(connect(c, (void *)&sa, sizeof sa)==0 || errno==EINPROGRESS);
50570af302Sopenharmony_ci
51570af302Sopenharmony_ci	TESTE((t=accept(s, (void *)&sa, &(socklen_t){sizeof sa}))>=0);
52570af302Sopenharmony_ci
53570af302Sopenharmony_ci	close(t);
54570af302Sopenharmony_ci	close(c);
55570af302Sopenharmony_ci	close(s);
56570af302Sopenharmony_ci
57570af302Sopenharmony_ci	return t_status;
58570af302Sopenharmony_ci}
59