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>
16 struct io_listener *l;
22 static void finish_ok(struct io_conn *conn, struct data *d)
27 static struct io_plan *r_done(struct io_conn *conn, struct data *d)
31 return io_close(conn);
32 return io_wait(conn, NULL, io_never, NULL);
35 static struct io_plan *w_done(struct io_conn *conn, struct data *d)
39 return io_close(conn);
40 return io_out_wait(conn, NULL, io_never, NULL);
43 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
46 io_set_debug(conn, true);
51 io_close_listener(d->l);
53 memset(d->wbuf, 7, sizeof(d->wbuf));
54 io_set_finish(conn, finish_ok, d);
56 return io_duplex(conn,
57 io_read(conn, d->buf, sizeof(d->buf), r_done, d),
58 io_write(conn, d->wbuf, sizeof(d->wbuf), w_done, d));
61 static int make_listen_fd(const char *port, struct addrinfo **info)
64 struct addrinfo *addrinfo, hints;
66 memset(&hints, 0, sizeof(hints));
67 hints.ai_family = AF_UNSPEC;
68 hints.ai_socktype = SOCK_STREAM;
69 hints.ai_flags = AI_PASSIVE;
70 hints.ai_protocol = 0;
72 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
75 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
76 addrinfo->ai_protocol);
80 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
81 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
85 if (listen(fd, 1) != 0) {
95 struct data *d = malloc(sizeof(*d));
96 struct addrinfo *addrinfo;
99 /* This is how many tests you plan to run */
102 fd = make_listen_fd(PORT, &addrinfo);
104 d->l = io_new_listener(NULL, fd, init_conn, d);
111 io_close_listener(d->l);
113 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
114 addrinfo->ai_protocol);
117 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
119 signal(SIGPIPE, SIG_IGN);
120 for (i = 0; i < 32; i++) {
121 if (read(fd, buf+i, 1) != 1)
124 for (i = 0; i < strlen("hellothere"); i++) {
125 if (write(fd, "hellothere" + i, 1) != 1)
129 freeaddrinfo(addrinfo);
132 freeaddrinfo(addrinfo);
133 ok1(io_loop(NULL, NULL) == NULL);
135 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
139 ok1(WIFEXITED(status));
140 ok1(WEXITSTATUS(status) == 0);
142 /* This exits depending on whether all tests passed */
143 return exit_status();