1 /* Check a bug where we have just completed a read, then set up a duplex
2 * which tries to do a read. */
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>
17 struct io_listener *l;
19 struct io_conn *c1, *c2;
24 static void finish_ok(struct io_conn *conn, struct data *d)
29 static struct io_plan end(struct io_conn *conn, struct data *d)
35 static struct io_plan make_duplex(struct io_conn *conn, struct data *d)
37 /* Have duplex read the rest of the buffer. */
38 d->c2 = io_duplex(conn, io_read(d->buf+1, sizeof(d->buf)-1, end, d));
40 io_set_finish(d->c2, finish_ok, d);
42 return io_write(d->wbuf, sizeof(d->wbuf), end, d);
45 static void init_conn(int fd, struct data *d)
50 io_close_listener(d->l);
52 memset(d->wbuf, 7, sizeof(d->wbuf));
54 d->c1 = io_new_conn(fd, io_read(d->buf, 1, make_duplex, d));
55 io_set_finish(d->c1, finish_ok, d);
58 static int make_listen_fd(const char *port, struct addrinfo **info)
61 struct addrinfo *addrinfo, hints;
63 memset(&hints, 0, sizeof(hints));
64 hints.ai_family = AF_UNSPEC;
65 hints.ai_socktype = SOCK_STREAM;
66 hints.ai_flags = AI_PASSIVE;
67 hints.ai_protocol = 0;
69 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
72 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
73 addrinfo->ai_protocol);
77 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
78 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
82 if (listen(fd, 1) != 0) {
92 struct data *d = malloc(sizeof(*d));
93 struct addrinfo *addrinfo;
96 /* This is how many tests you plan to run */
99 fd = make_listen_fd(PORT, &addrinfo);
101 d->l = io_new_listener(fd, init_conn, d);
108 io_close_listener(d->l);
110 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
111 addrinfo->ai_protocol);
114 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
116 signal(SIGPIPE, SIG_IGN);
117 for (i = 0; i < strlen("hellothere"); i++) {
118 if (write(fd, "hellothere" + i, 1) != 1)
121 for (i = 0; i < 32; i++) {
122 if (read(fd, buf+i, 1) != 1)
126 freeaddrinfo(addrinfo);
129 freeaddrinfo(addrinfo);
130 ok1(io_loop() == NULL);
132 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
136 ok1(WIFEXITED(status));
137 ok1(WEXITSTATUS(status) == 0);
139 /* This exits depending on whether all tests passed */
140 return exit_status();