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;
17 struct io_conn *writer;
23 static void finish_ok(struct io_conn *conn, struct data *d)
28 static struct io_plan write_done(struct io_conn *conn, struct data *d)
34 static struct io_plan read_done(struct io_conn *conn, struct data *d)
37 io_close_other(d->writer);
41 static void init_conn(int fd, struct data *d)
48 memset(d->wbuf, 7, sizeof(d->wbuf));
50 conn = io_new_conn(fd, io_read(d->buf, sizeof(d->buf), read_done, d));
51 io_set_finish(conn, finish_ok, d);
52 d->writer = io_duplex(conn, io_write(d->wbuf, sizeof(d->wbuf), write_done, d));
54 io_set_finish(d->writer, finish_ok, d);
56 io_close_listener(d->l);
59 static int make_listen_fd(const char *port, struct addrinfo **info)
62 struct addrinfo *addrinfo, hints;
64 memset(&hints, 0, sizeof(hints));
65 hints.ai_family = AF_UNSPEC;
66 hints.ai_socktype = SOCK_STREAM;
67 hints.ai_flags = AI_PASSIVE;
68 hints.ai_protocol = 0;
70 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
73 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
74 addrinfo->ai_protocol);
78 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
79 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
83 if (listen(fd, 1) != 0) {
93 struct data *d = malloc(sizeof(*d));
94 struct addrinfo *addrinfo;
97 /* This is how many tests you plan to run */
100 fd = make_listen_fd(PORT, &addrinfo);
102 d->l = io_new_listener(fd, init_conn, d);
109 io_close_listener(d->l);
111 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
112 addrinfo->ai_protocol);
115 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
117 signal(SIGPIPE, SIG_IGN);
118 for (i = 0; i < 32; i++) {
119 if (read(fd, buf+i, 1) != 1)
122 for (i = 0; i < strlen("hellothere"); i++) {
123 if (write(fd, "hellothere" + i, 1) != 1)
127 freeaddrinfo(addrinfo);
130 freeaddrinfo(addrinfo);
131 ok1(io_loop() == NULL);
133 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
137 ok1(WIFEXITED(status));
138 ok1(WEXITSTATUS(status) == 0);
140 /* This exits depending on whether all tests passed */
141 return exit_status();