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;
23 static void finish_ok(struct io_conn *conn, struct data *d)
28 static struct io_plan *rw_done(struct io_conn *conn, struct data *d)
33 return io_close(conn);
34 return io_wait(conn, NULL, io_never, NULL);
37 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
40 io_set_debug(conn, true);
45 io_close_listener(d->l);
47 memset(d->wbuf, 7, sizeof(d->wbuf));
48 io_set_finish(conn, finish_ok, d);
50 return io_duplex(conn,
51 io_read(conn, d->buf, sizeof(d->buf), rw_done, d),
52 io_write(conn, d->wbuf, sizeof(d->wbuf), rw_done, d));
55 static int make_listen_fd(const char *port, struct addrinfo **info)
58 struct addrinfo *addrinfo, hints;
60 memset(&hints, 0, sizeof(hints));
61 hints.ai_family = AF_UNSPEC;
62 hints.ai_socktype = SOCK_STREAM;
63 hints.ai_flags = AI_PASSIVE;
64 hints.ai_protocol = 0;
66 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
69 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
70 addrinfo->ai_protocol);
74 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
75 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
79 if (listen(fd, 1) != 0) {
89 struct data *d = malloc(sizeof(*d));
90 struct addrinfo *addrinfo;
93 /* This is how many tests you plan to run */
97 fd = make_listen_fd(PORT, &addrinfo);
99 d->l = io_new_listener(NULL, fd, init_conn, d);
106 io_close_listener(d->l);
108 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
109 addrinfo->ai_protocol);
112 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
114 signal(SIGPIPE, SIG_IGN);
115 for (i = 0; i < 32; i++) {
116 if (read(fd, buf+i, 1) != 1)
119 for (i = 0; i < strlen("hellothere"); i++) {
120 if (write(fd, "hellothere" + i, 1) != 1)
124 freeaddrinfo(addrinfo);
127 freeaddrinfo(addrinfo);
128 ok1(io_loop(NULL, NULL) == NULL);
131 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
135 ok1(WIFEXITED(status));
136 ok1(WEXITSTATUS(status) == 0);
138 /* This exits depending on whether all tests passed */
139 return exit_status();