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>
15 struct io_listener *l;
21 static void finish_ok(struct io_conn *conn, struct data *d)
26 static struct io_plan *write_done(struct io_conn *conn, struct data *d)
29 return io_close(conn);
32 static void init_conn(int fd, struct data *d)
39 io_close_listener(d->l);
41 memset(d->wbuf, 7, sizeof(d->wbuf));
43 conn = io_new_conn(fd, io_read(d->buf, sizeof(d->buf), io_close_cb, d));
44 io_set_finish(conn, finish_ok, d);
45 conn = io_duplex(conn, io_write(d->wbuf, sizeof(d->wbuf), write_done, d));
47 io_set_finish(conn, finish_ok, d);
50 static int make_listen_fd(const char *port, struct addrinfo **info)
53 struct addrinfo *addrinfo, hints;
55 memset(&hints, 0, sizeof(hints));
56 hints.ai_family = AF_UNSPEC;
57 hints.ai_socktype = SOCK_STREAM;
58 hints.ai_flags = AI_PASSIVE;
59 hints.ai_protocol = 0;
61 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
64 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
65 addrinfo->ai_protocol);
69 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
70 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
74 if (listen(fd, 1) != 0) {
84 struct data *d = malloc(sizeof(*d));
85 struct addrinfo *addrinfo;
88 /* This is how many tests you plan to run */
91 fd = make_listen_fd(PORT, &addrinfo);
93 d->l = io_new_listener(fd, init_conn, d);
100 io_close_listener(d->l);
102 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
103 addrinfo->ai_protocol);
106 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
108 signal(SIGPIPE, SIG_IGN);
109 for (i = 0; i < 32; i++) {
110 if (read(fd, buf+i, 1) != 1)
113 for (i = 0; i < strlen("hellothere"); i++) {
114 if (write(fd, "hellothere" + i, 1) != 1)
118 freeaddrinfo(addrinfo);
121 freeaddrinfo(addrinfo);
122 ok1(io_loop() == NULL);
124 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
128 ok1(WIFEXITED(status));
129 ok1(WEXITSTATUS(status) == 0);
131 /* This exits depending on whether all tests passed */
132 return exit_status();