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 struct io_conn *idler;
21 static struct io_plan *read_done(struct io_conn *conn, struct data *d)
23 ok1(d->state == 2 || d->state == 3);
25 return io_close(conn);
28 static void finish_waker(struct io_conn *conn, struct data *d)
35 static void finish_idle(struct io_conn *conn, struct data *d)
42 static struct io_plan *never(struct io_conn *conn, void *arg)
47 static struct io_plan *read_buf(struct io_conn *conn, struct data *d)
49 return io_read(conn, d->buf, sizeof(d->buf), read_done, d);
52 static struct io_plan *init_waker(struct io_conn *conn, void *unused)
54 /* This is /dev/null, so will never succeed. */
55 return io_read(conn, unused, 1, never, NULL);
58 static struct io_plan *init_idle(struct io_conn *conn, struct data *d)
65 io_set_finish(conn, finish_idle, d);
67 /* This will wake us up, as read will fail. */
68 fd2 = open("/dev/null", O_RDONLY);
70 io_set_finish(io_new_conn(NULL, fd2, init_waker, d), finish_waker, d);
72 return io_wait(conn, d, read_buf, d);
75 static int make_listen_fd(const char *port, struct addrinfo **info)
78 struct addrinfo *addrinfo, hints;
80 memset(&hints, 0, sizeof(hints));
81 hints.ai_family = AF_UNSPEC;
82 hints.ai_socktype = SOCK_STREAM;
83 hints.ai_flags = AI_PASSIVE;
84 hints.ai_protocol = 0;
86 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
89 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
90 addrinfo->ai_protocol);
94 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
95 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
99 if (listen(fd, 1) != 0) {
109 struct data *d = malloc(sizeof(*d));
110 struct addrinfo *addrinfo;
111 struct io_listener *l;
114 /* This is how many tests you plan to run */
117 fd = make_listen_fd(PORT, &addrinfo);
119 l = io_new_listener(NULL, fd, init_idle, d);
125 io_close_listener(l);
126 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
127 addrinfo->ai_protocol);
130 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
132 signal(SIGPIPE, SIG_IGN);
133 for (i = 0; i < strlen("hellothere"); i++) {
134 if (write(fd, "hellothere" + i, 1) != 1)
138 freeaddrinfo(addrinfo);
142 freeaddrinfo(addrinfo);
144 ok1(io_loop(NULL, NULL) == d);
146 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
148 io_close_listener(l);
151 ok1(WIFEXITED(status));
152 ok1(WEXITSTATUS(status) == 0);
154 /* This exits depending on whether all tests passed */
155 return exit_status();