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 *rw_done(struct io_conn *conn, struct data *d)
30 return io_halfclose(conn);
33 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
36 io_set_debug(conn, true);
41 io_close_listener(d->l);
43 memset(d->wbuf, 7, sizeof(d->wbuf));
44 io_set_finish(conn, finish_ok, d);
46 return io_duplex(conn,
47 io_read(conn, d->buf, sizeof(d->buf), rw_done, d),
48 io_write(conn, d->wbuf, sizeof(d->wbuf), rw_done, d));
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) {
85 struct data *d = malloc(sizeof(*d));
86 struct addrinfo *addrinfo;
89 /* This is how many tests you plan to run */
92 fd = make_listen_fd(PORT, &addrinfo);
94 d->l = io_new_listener(NULL, fd, init_conn, d);
101 io_close_listener(d->l);
103 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
104 addrinfo->ai_protocol);
107 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
109 signal(SIGPIPE, SIG_IGN);
110 for (i = 0; i < 32; i++) {
111 if (read(fd, buf+i, 1) != 1)
114 for (i = 0; i < strlen("hellothere"); i++) {
115 if (write(fd, "hellothere" + i, 1) != 1)
119 freeaddrinfo(addrinfo);
122 freeaddrinfo(addrinfo);
123 ok1(io_loop(NULL, NULL) == NULL);
125 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
129 ok1(WIFEXITED(status));
130 ok1(WEXITSTATUS(status) == 0);
132 /* This exits depending on whether all tests passed */
133 return exit_status();