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 struct io_listener *l;
18 static void finish_ok(struct io_conn *conn, struct data *d)
23 static struct io_plan *r_done(struct io_conn *conn, struct data *d)
27 return io_close(conn);
28 return io_wait(conn, NULL, io_never, NULL);
31 static struct io_plan *w_done(struct io_conn *conn, struct data *d)
35 return io_close(conn);
36 return io_out_wait(conn, NULL, io_never, NULL);
39 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
44 io_close_listener(d->l);
46 memset(d->wbuf, 7, sizeof(d->wbuf));
47 io_set_finish(conn, finish_ok, d);
49 return io_duplex(conn,
50 io_read(conn, d->buf, sizeof(d->buf), r_done, d),
51 io_write(conn, d->wbuf, sizeof(d->wbuf), w_done, d));
54 static int make_listen_fd(const char *port, struct addrinfo **info)
57 struct addrinfo *addrinfo, hints;
59 memset(&hints, 0, sizeof(hints));
60 hints.ai_family = AF_UNSPEC;
61 hints.ai_socktype = SOCK_STREAM;
62 hints.ai_flags = AI_PASSIVE;
63 hints.ai_protocol = 0;
65 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
68 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
69 addrinfo->ai_protocol);
73 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
74 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
78 if (listen(fd, 1) != 0) {
88 struct data *d = malloc(sizeof(*d));
89 struct addrinfo *addrinfo;
92 /* This is how many tests you plan to run */
95 fd = make_listen_fd(PORT, &addrinfo);
97 d->l = io_new_listener(NULL, fd, init_conn, d);
104 io_close_listener(d->l);
106 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
107 addrinfo->ai_protocol);
110 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
112 signal(SIGPIPE, SIG_IGN);
113 for (i = 0; i < 32; i++) {
114 if (read(fd, buf+i, 1) != 1)
117 for (i = 0; i < strlen("hellothere"); i++) {
118 if (write(fd, "hellothere" + i, 1) != 1)
122 freeaddrinfo(addrinfo);
125 freeaddrinfo(addrinfo);
126 ok1(io_loop(NULL, NULL) == NULL);
128 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
132 ok1(WIFEXITED(status));
133 ok1(WEXITSTATUS(status) == 0);
135 /* This exits depending on whether all tests passed */
136 return exit_status();