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>
14 struct io_listener *l;
20 static void finish_ok(struct io_conn *conn, struct data *d)
25 static struct io_plan *io_done(struct io_conn *conn, struct data *d)
28 return io_halfclose(conn);
31 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
36 memset(d->wbuf, 7, sizeof(d->wbuf));
38 io_set_finish(conn, finish_ok, d);
40 io_close_listener(d->l);
42 return io_duplex(conn,
43 io_read(conn, d->buf, sizeof(d->buf), io_done, d),
44 io_write(conn, d->wbuf, sizeof(d->wbuf), io_done, d));
47 static int make_listen_fd(const char *port, struct addrinfo **info)
50 struct addrinfo *addrinfo, hints;
52 memset(&hints, 0, sizeof(hints));
53 hints.ai_family = AF_UNSPEC;
54 hints.ai_socktype = SOCK_STREAM;
55 hints.ai_flags = AI_PASSIVE;
56 hints.ai_protocol = 0;
58 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
61 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
62 addrinfo->ai_protocol);
66 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
67 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
71 if (listen(fd, 1) != 0) {
81 struct data *d = malloc(sizeof(*d));
82 struct addrinfo *addrinfo;
85 /* This is how many tests you plan to run */
88 fd = make_listen_fd(PORT, &addrinfo);
90 d->l = io_new_listener(NULL, fd, init_conn, d);
97 io_close_listener(d->l);
99 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
100 addrinfo->ai_protocol);
103 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
105 signal(SIGPIPE, SIG_IGN);
106 for (i = 0; i < 32; i++) {
107 if (read(fd, buf+i, 1) != 1)
110 for (i = 0; i < strlen("hellothere"); i++) {
111 if (write(fd, "hellothere" + i, 1) != 1)
115 freeaddrinfo(addrinfo);
118 freeaddrinfo(addrinfo);
119 ok1(io_loop(NULL, NULL) == NULL);
121 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
125 ok1(WIFEXITED(status));
126 ok1(WEXITSTATUS(status) == 0);
128 /* This exits depending on whether all tests passed */
129 return exit_status();