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 plan_read(struct io_conn *conn, struct data *d)
21 ok1(d->state == 2 || d->state == 3);
23 return io_read(conn, d->buf, sizeof(d->buf), io_close, d);
26 static struct io_plan start_waker(struct io_conn *conn, struct data *d)
31 io_wake(idler, plan_read, d);
32 return io_close(conn, NULL);
35 static void finish_waker(struct io_conn *conn, struct data *d)
37 ok1(d->state == 2 || d->state == 3);
41 static struct io_plan start_idle(struct io_conn *conn, struct data *d)
49 /* This will wake us up. */
50 fd = open("/dev/null", O_RDONLY);
52 ok1(io_new_conn(fd, start_waker, finish_waker, d));
57 static void finish_idle(struct io_conn *conn, struct data *d)
61 io_break(conn, d, NULL, NULL);
64 static int make_listen_fd(const char *port, struct addrinfo **info)
67 struct addrinfo *addrinfo, hints;
69 memset(&hints, 0, sizeof(hints));
70 hints.ai_family = AF_UNSPEC;
71 hints.ai_socktype = SOCK_STREAM;
72 hints.ai_flags = AI_PASSIVE;
73 hints.ai_protocol = 0;
75 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
78 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
79 addrinfo->ai_protocol);
83 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
84 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
88 if (listen(fd, 1) != 0) {
98 struct data *d = malloc(sizeof(*d));
99 struct addrinfo *addrinfo;
100 struct io_listener *l;
103 /* This is how many tests you plan to run */
106 fd = make_listen_fd("65006", &addrinfo);
108 l = io_new_listener(fd, start_idle, finish_idle, d);
114 io_close_listener(l);
115 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
116 addrinfo->ai_protocol);
119 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
121 signal(SIGPIPE, SIG_IGN);
122 for (i = 0; i < strlen("hellothere"); i++) {
123 if (write(fd, "hellothere" + i, 1) != 1)
127 freeaddrinfo(addrinfo);
131 freeaddrinfo(addrinfo);
135 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
137 io_close_listener(l);
140 ok1(WIFEXITED(status));
141 ok1(WEXITSTATUS(status) == 0);
143 /* This exits depending on whether all tests passed */
144 return exit_status();