1 #include <ccan/io/io.h>
2 /* Include the C files directly. */
3 #include <ccan/io/poll.c>
4 #include <ccan/io/io.c>
5 #include <ccan/tap/tap.h>
16 static struct io_conn *idler;
23 static struct io_plan read_done(struct io_conn *conn, struct data *d)
25 ok1(d->state == 2 || d->state == 3);
30 static void finish_waker(struct io_conn *conn, struct data *d)
37 static void finish_idle(struct io_conn *conn, struct data *d)
41 io_break(d, io_never());
44 static struct io_plan never(struct io_conn *conn, void *arg)
49 static struct io_plan read_buf(struct io_conn *conn, struct data *d)
51 return io_read(d->buf, sizeof(d->buf), read_done, d);
54 static void init_conn(int fd, struct data *d)
60 idler = io_new_conn(fd, io_wait(d, read_buf, d));
61 io_set_finish(idler, finish_idle, d);
63 /* This will wake us up, as read will fail. */
64 fd2 = open("/dev/null", O_RDONLY);
66 io_set_finish(io_new_conn(fd2, io_read(idler, 1, never, NULL)),
70 static int make_listen_fd(const char *port, struct addrinfo **info)
73 struct addrinfo *addrinfo, hints;
75 memset(&hints, 0, sizeof(hints));
76 hints.ai_family = AF_UNSPEC;
77 hints.ai_socktype = SOCK_STREAM;
78 hints.ai_flags = AI_PASSIVE;
79 hints.ai_protocol = 0;
81 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
84 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
85 addrinfo->ai_protocol);
89 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
90 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
94 if (listen(fd, 1) != 0) {
104 struct data *d = malloc(sizeof(*d));
105 struct addrinfo *addrinfo;
106 struct io_listener *l;
109 /* This is how many tests you plan to run */
112 fd = make_listen_fd(PORT, &addrinfo);
114 l = io_new_listener(fd, init_conn, d);
120 io_close_listener(l);
121 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
122 addrinfo->ai_protocol);
125 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
127 signal(SIGPIPE, SIG_IGN);
128 for (i = 0; i < strlen("hellothere"); i++) {
129 if (write(fd, "hellothere" + i, 1) != 1)
133 freeaddrinfo(addrinfo);
137 freeaddrinfo(addrinfo);
141 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
143 io_close_listener(l);
146 ok1(WIFEXITED(status));
147 ok1(WEXITSTATUS(status) == 0);
149 /* This exits depending on whether all tests passed */
150 return exit_status();