1 #include <ccan/io/io.h>
2 /* Include the C files directly. */
3 #include <ccan/io/poll.c>
4 #include <ccan/io/io.c>
5 #include <ccan/tap/tap.h>
10 static int expected_fd;
12 static void finish_ok(struct io_conn *conn, int *state)
15 ok1(io_conn_fd(conn) == expected_fd);
20 static struct io_plan *init_conn(struct io_conn *conn, int *state)
24 expected_fd = io_conn_fd(conn);
25 io_set_finish(conn, finish_ok, state);
27 return io_close(conn);
30 static int make_listen_fd(const char *port, struct addrinfo **info)
33 struct addrinfo *addrinfo, hints;
35 memset(&hints, 0, sizeof(hints));
36 hints.ai_family = AF_UNSPEC;
37 hints.ai_socktype = SOCK_STREAM;
38 hints.ai_flags = AI_PASSIVE;
39 hints.ai_protocol = 0;
41 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
44 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
45 addrinfo->ai_protocol);
49 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
50 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
54 if (listen(fd, 1) != 0) {
65 struct addrinfo *addrinfo = NULL;
66 struct io_listener *l;
69 /* This is how many tests you plan to run */
71 fd = make_listen_fd(PORT, &addrinfo);
73 l = io_new_listener(NULL, fd, init_conn, &state);
78 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
79 addrinfo->ai_protocol);
82 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
85 freeaddrinfo(addrinfo);
88 freeaddrinfo(addrinfo);
89 ok1(io_loop(NULL, NULL) == &state + 1);
93 ok1(WIFEXITED(state));
94 ok1(WEXITSTATUS(state) == 0);
96 /* This exits depending on whether all tests passed */