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 void init_conn(int fd, struct data *d)
46 idler = io_new_conn(fd, io_idle(), finish_idle, d);
48 /* This will wake us up, as read will fail. */
49 fd2 = open("/dev/null", O_RDONLY);
51 ok1(io_new_conn(fd2, io_read(idler, 1, NULL, NULL), finish_waker, d));
54 static int make_listen_fd(const char *port, struct addrinfo **info)
57 struct addrinfo *addrinfo, hints;
59 memset(&hints, 0, sizeof(hints));
60 hints.ai_family = AF_UNSPEC;
61 hints.ai_socktype = SOCK_STREAM;
62 hints.ai_flags = AI_PASSIVE;
63 hints.ai_protocol = 0;
65 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
68 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
69 addrinfo->ai_protocol);
73 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
74 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
78 if (listen(fd, 1) != 0) {
88 struct data *d = malloc(sizeof(*d));
89 struct addrinfo *addrinfo;
90 struct io_listener *l;
93 /* This is how many tests you plan to run */
96 fd = make_listen_fd("65006", &addrinfo);
98 l = io_new_listener(fd, init_conn, d);
104 io_close_listener(l);
105 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
106 addrinfo->ai_protocol);
109 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
111 signal(SIGPIPE, SIG_IGN);
112 for (i = 0; i < strlen("hellothere"); i++) {
113 if (write(fd, "hellothere" + i, 1) != 1)
117 freeaddrinfo(addrinfo);
121 freeaddrinfo(addrinfo);
125 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
127 io_close_listener(l);
130 ok1(WIFEXITED(status));
131 ok1(WEXITSTATUS(status) == 0);
133 /* This exits depending on whether all tests passed */
134 return exit_status();