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>
12 static int expected_fd;
14 static void finish_ok(struct io_conn *conn, int *state)
17 ok1(io_conn_fd(conn) == expected_fd);
22 static struct io_plan *init_conn(struct io_conn *conn, int *state)
26 expected_fd = io_conn_fd(conn);
27 io_set_finish(conn, finish_ok, state);
29 return io_close(conn);
32 static int make_listen_fd(const char *port, struct addrinfo **info)
35 struct addrinfo *addrinfo, hints;
37 memset(&hints, 0, sizeof(hints));
38 hints.ai_family = AF_UNSPEC;
39 hints.ai_socktype = SOCK_STREAM;
40 hints.ai_flags = AI_PASSIVE;
41 hints.ai_protocol = 0;
43 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
46 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
47 addrinfo->ai_protocol);
51 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
52 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
56 if (listen(fd, 1) != 0) {
67 struct addrinfo *addrinfo;
68 struct io_listener *l;
71 /* This is how many tests you plan to run */
73 fd = make_listen_fd(PORT, &addrinfo);
75 l = io_new_listener(NULL, fd, init_conn, &state);
80 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
81 addrinfo->ai_protocol);
84 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
87 freeaddrinfo(addrinfo);
90 freeaddrinfo(addrinfo);
91 ok1(io_loop(NULL, NULL) == &state + 1);
95 ok1(WIFEXITED(state));
96 ok1(WEXITSTATUS(state) == 0);
98 /* This exits depending on whether all tests passed */