1 /* Tests when the last connection is a duplex, and poll.c moves it over
3 #include <ccan/io/io.h>
4 /* Include the C files directly. */
5 #include <ccan/io/poll.c>
6 #include <ccan/io/io.c>
7 #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 *io_done(struct io_conn *conn, struct data *d)
31 return io_close(conn);
32 return io_wait(conn, d, io_close_cb, NULL);
35 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
40 memset(d->wbuf, 7, sizeof(d->wbuf));
42 io_set_finish(conn, finish_ok, d);
44 io_close_listener(d->l);
46 return io_duplex(io_read(conn, d->buf, sizeof(d->buf), io_done, d),
47 io_write(conn, d->wbuf, sizeof(d->wbuf), io_done, 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(NULL, 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, NULL) == 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();