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>
14 static int expected_fd;
16 static void finish_ok(struct io_conn *conn, int *state)
19 ok1(io_conn_fd(conn) == expected_fd);
24 static struct io_plan *init_conn(struct io_conn *conn, int *state)
27 io_set_debug(conn, true);
31 expected_fd = io_conn_fd(conn);
32 io_set_finish(conn, finish_ok, state);
34 return io_close(conn);
37 static int make_listen_fd(const char *port, struct addrinfo **info)
40 struct addrinfo *addrinfo, hints;
42 memset(&hints, 0, sizeof(hints));
43 hints.ai_family = AF_UNSPEC;
44 hints.ai_socktype = SOCK_STREAM;
45 hints.ai_flags = AI_PASSIVE;
46 hints.ai_protocol = 0;
48 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
51 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
52 addrinfo->ai_protocol);
56 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
57 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
61 if (listen(fd, 1) != 0) {
72 struct addrinfo *addrinfo;
73 struct io_listener *l;
76 /* This is how many tests you plan to run */
78 fd = make_listen_fd(PORT, &addrinfo);
80 l = io_new_listener(NULL, fd, init_conn, &state);
85 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
86 addrinfo->ai_protocol);
89 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
92 freeaddrinfo(addrinfo);
95 freeaddrinfo(addrinfo);
96 ok1(io_loop(NULL, NULL) == &state + 1);
100 ok1(WIFEXITED(state));
101 ok1(WEXITSTATUS(state) == 0);
103 /* This exits depending on whether all tests passed */
104 return exit_status();