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>
15 static void finish_100(struct io_conn *conn, int *state)
22 static void finish_EBADF(struct io_conn *conn, int *state)
30 static struct io_plan *init_conn(struct io_conn *conn, int *state)
33 io_set_debug(conn, true);
38 io_set_finish(conn, finish_100, state);
39 return io_close(conn);
43 close(io_conn_fd(conn));
45 io_set_finish(conn, finish_EBADF, state);
47 return io_read(conn, state, 1, io_close_cb, NULL);
51 static int make_listen_fd(const char *port, struct addrinfo **info)
54 struct addrinfo *addrinfo, hints;
56 memset(&hints, 0, sizeof(hints));
57 hints.ai_family = AF_UNSPEC;
58 hints.ai_socktype = SOCK_STREAM;
59 hints.ai_flags = AI_PASSIVE;
60 hints.ai_protocol = 0;
62 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
65 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
66 addrinfo->ai_protocol);
70 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
71 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
75 if (listen(fd, 1) != 0) {
86 struct addrinfo *addrinfo;
87 struct io_listener *l;
90 /* This is how many tests you plan to run */
92 fd = make_listen_fd(PORT, &addrinfo);
94 l = io_new_listener(NULL, fd, init_conn, &state);
99 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
100 addrinfo->ai_protocol);
103 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
106 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
107 addrinfo->ai_protocol);
110 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
113 freeaddrinfo(addrinfo);
116 freeaddrinfo(addrinfo);
117 ok1(io_loop(NULL, NULL) == &state + 1);
119 io_close_listener(l);
121 ok1(WIFEXITED(state));
122 ok1(WEXITSTATUS(state) == 0);
124 /* This exits depending on whether all tests passed */
125 return exit_status();