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>
12 static struct io_conn *idler;
19 static struct io_plan read_done(struct io_conn *conn, struct data *d)
21 ok1(d->state == 2 || d->state == 3);
23 return io_close(conn, NULL);
26 static void finish_waker(struct io_conn *conn, struct data *d)
28 io_wake(idler, io_read(d->buf, sizeof(d->buf), read_done, d));
33 static void finish_idle(struct io_conn *conn, struct data *d)
37 io_break(d, io_idle());
40 static struct io_plan never(struct io_conn *conn, void *arg)
45 static void init_conn(int fd, struct data *d)
51 idler = io_new_conn(fd, io_idle(), finish_idle, d);
53 /* This will wake us up, as read will fail. */
54 fd2 = open("/dev/null", O_RDONLY);
56 ok1(io_new_conn(fd2, io_read(idler, 1, never, NULL), finish_waker, d));
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;
95 struct io_listener *l;
98 /* This is how many tests you plan to run */
101 fd = make_listen_fd("65006", &addrinfo);
103 l = io_new_listener(fd, init_conn, d);
109 io_close_listener(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)
122 freeaddrinfo(addrinfo);
126 freeaddrinfo(addrinfo);
130 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
132 io_close_listener(l);
135 ok1(WIFEXITED(status));
136 ok1(WEXITSTATUS(status) == 0);
138 /* This exits depending on whether all tests passed */
139 return exit_status();