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>
10 struct io_listener *l;
16 static void finish_ok(struct io_conn *conn, struct data *d)
21 static struct io_plan *write_out(struct io_conn *conn, struct data *d)
24 return io_write(d->wbuf, sizeof(d->wbuf), io_next(conn, io_close, d));
27 static struct io_plan *start_ok(struct io_conn *conn, struct data *d)
32 io_close_listener(d->l);
34 memset(d->wbuf, 7, sizeof(d->wbuf));
35 ok1(io_duplex(conn, write_out, finish_ok, d));
36 return io_read(d->buf, sizeof(d->buf), io_next(conn, io_close, d));
39 static int make_listen_fd(const char *port, struct addrinfo **info)
42 struct addrinfo *addrinfo, hints;
44 memset(&hints, 0, sizeof(hints));
45 hints.ai_family = AF_UNSPEC;
46 hints.ai_socktype = SOCK_STREAM;
47 hints.ai_flags = AI_PASSIVE;
48 hints.ai_protocol = 0;
50 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
53 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
54 addrinfo->ai_protocol);
58 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
59 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
63 if (listen(fd, 1) != 0) {
73 struct data *d = malloc(sizeof(*d));
74 struct addrinfo *addrinfo;
77 /* This is how many tests you plan to run */
80 fd = make_listen_fd("65012", &addrinfo);
82 d->l = io_new_listener(fd, start_ok, finish_ok, d);
89 io_close_listener(d->l);
91 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
92 addrinfo->ai_protocol);
95 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
97 signal(SIGPIPE, SIG_IGN);
98 for (i = 0; i < 32; i++) {
99 if (read(fd, buf+i, 1) != 1)
102 for (i = 0; i < strlen("hellothere"); i++) {
103 if (write(fd, "hellothere" + i, 1) != 1)
107 freeaddrinfo(addrinfo);
110 freeaddrinfo(addrinfo);
111 ok1(io_loop() == NULL);
113 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
117 ok1(WIFEXITED(status));
118 ok1(WEXITSTATUS(status) == 0);
120 /* This exits depending on whether all tests passed */
121 return exit_status();