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>
17 struct io_listener *l;
18 struct io_conn *writer;
24 static void finish_ok(struct io_conn *conn, struct data *d)
29 static struct io_plan write_done(struct io_conn *conn, struct data *d)
32 return io_wait(d, io_close_cb, NULL);
35 static struct io_plan read_done(struct io_conn *conn, struct data *d)
38 io_close_other(d->writer);
42 static void init_conn(int fd, struct data *d)
49 memset(d->wbuf, 7, sizeof(d->wbuf));
51 conn = io_new_conn(fd, io_read(d->buf, sizeof(d->buf), read_done, d));
52 io_set_finish(conn, finish_ok, d);
53 d->writer = io_duplex(conn, io_write(d->wbuf, sizeof(d->wbuf), write_done, d));
55 io_set_finish(d->writer, finish_ok, d);
57 io_close_listener(d->l);
60 static int make_listen_fd(const char *port, struct addrinfo **info)
63 struct addrinfo *addrinfo, hints;
65 memset(&hints, 0, sizeof(hints));
66 hints.ai_family = AF_UNSPEC;
67 hints.ai_socktype = SOCK_STREAM;
68 hints.ai_flags = AI_PASSIVE;
69 hints.ai_protocol = 0;
71 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
74 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
75 addrinfo->ai_protocol);
79 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
80 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
84 if (listen(fd, 1) != 0) {
94 struct data *d = malloc(sizeof(*d));
95 struct addrinfo *addrinfo;
98 /* This is how many tests you plan to run */
101 fd = make_listen_fd(PORT, &addrinfo);
103 d->l = io_new_listener(fd, init_conn, d);
110 io_close_listener(d->l);
112 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
113 addrinfo->ai_protocol);
116 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
118 signal(SIGPIPE, SIG_IGN);
119 for (i = 0; i < 32; i++) {
120 if (read(fd, buf+i, 1) != 1)
123 for (i = 0; i < strlen("hellothere"); i++) {
124 if (write(fd, "hellothere" + i, 1) != 1)
128 freeaddrinfo(addrinfo);
131 freeaddrinfo(addrinfo);
132 ok1(io_loop() == NULL);
134 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
138 ok1(WIFEXITED(status));
139 ok1(WEXITSTATUS(status) == 0);
141 /* This exits depending on whether all tests passed */
142 return exit_status();