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>
18 struct io_listener *l;
24 static void finish_ok(struct io_conn *conn, struct data *d)
29 static struct io_plan *io_done(struct io_conn *conn, struct data *d)
32 return io_halfclose(conn);
35 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
38 io_set_debug(conn, true);
43 memset(d->wbuf, 7, sizeof(d->wbuf));
45 io_set_finish(conn, finish_ok, d);
47 io_close_listener(d->l);
49 return io_duplex(conn,
50 io_read(conn, d->buf, sizeof(d->buf), io_done, d),
51 io_write(conn, d->wbuf, sizeof(d->wbuf), io_done, d));
54 static int make_listen_fd(const char *port, struct addrinfo **info)
57 struct addrinfo *addrinfo, hints;
59 memset(&hints, 0, sizeof(hints));
60 hints.ai_family = AF_UNSPEC;
61 hints.ai_socktype = SOCK_STREAM;
62 hints.ai_flags = AI_PASSIVE;
63 hints.ai_protocol = 0;
65 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
68 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
69 addrinfo->ai_protocol);
73 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
74 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
78 if (listen(fd, 1) != 0) {
88 struct data *d = malloc(sizeof(*d));
89 struct addrinfo *addrinfo;
92 /* This is how many tests you plan to run */
95 fd = make_listen_fd(PORT, &addrinfo);
97 d->l = io_new_listener(NULL, fd, init_conn, d);
104 io_close_listener(d->l);
106 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
107 addrinfo->ai_protocol);
110 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
112 signal(SIGPIPE, SIG_IGN);
113 for (i = 0; i < 32; i++) {
114 if (read(fd, buf+i, 1) != 1)
117 for (i = 0; i < strlen("hellothere"); i++) {
118 if (write(fd, "hellothere" + i, 1) != 1)
122 freeaddrinfo(addrinfo);
125 freeaddrinfo(addrinfo);
126 ok1(io_loop(NULL, NULL) == NULL);
128 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
132 ok1(WIFEXITED(status));
133 ok1(WEXITSTATUS(status) == 0);
135 /* This exits depending on whether all tests passed */
136 return exit_status();