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>
14 struct io_listener *l;
20 static void finish_ok(struct io_conn *conn, struct data *d)
25 static struct io_plan write_done(struct io_conn *conn, struct data *d)
31 static void init_conn(int fd, struct data *d)
38 io_close_listener(d->l);
40 memset(d->wbuf, 7, sizeof(d->wbuf));
42 conn = io_new_conn(fd, io_read(d->buf, sizeof(d->buf), io_close_cb, d));
43 io_set_finish(conn, finish_ok, d);
44 conn = io_duplex(conn, io_write(d->wbuf, sizeof(d->wbuf), write_done, d));
46 io_set_finish(conn, finish_ok, d);
49 static int make_listen_fd(const char *port, struct addrinfo **info)
52 struct addrinfo *addrinfo, hints;
54 memset(&hints, 0, sizeof(hints));
55 hints.ai_family = AF_UNSPEC;
56 hints.ai_socktype = SOCK_STREAM;
57 hints.ai_flags = AI_PASSIVE;
58 hints.ai_protocol = 0;
60 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
63 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
64 addrinfo->ai_protocol);
68 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
69 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
73 if (listen(fd, 1) != 0) {
83 struct data *d = malloc(sizeof(*d));
84 struct addrinfo *addrinfo;
87 /* This is how many tests you plan to run */
90 fd = make_listen_fd(PORT, &addrinfo);
92 d->l = io_new_listener(fd, init_conn, d);
99 io_close_listener(d->l);
101 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
102 addrinfo->ai_protocol);
105 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
107 signal(SIGPIPE, SIG_IGN);
108 for (i = 0; i < 32; i++) {
109 if (read(fd, buf+i, 1) != 1)
112 for (i = 0; i < strlen("hellothere"); i++) {
113 if (write(fd, "hellothere" + i, 1) != 1)
117 freeaddrinfo(addrinfo);
120 freeaddrinfo(addrinfo);
121 ok1(io_loop() == NULL);
123 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
127 ok1(WIFEXITED(status));
128 ok1(WEXITSTATUS(status) == 0);
130 /* This exits depending on whether all tests passed */
131 return exit_status();