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>
19 static void finish_ok(struct io_conn *conn, struct data *d)
23 io_break(d, io_idle());
26 static void init_conn(int fd, struct data *d)
30 io_set_finish(io_new_conn(fd, io_write(d->buf, d->bytes,
35 static int make_listen_fd(const char *port, struct addrinfo **info)
38 struct addrinfo *addrinfo, hints;
40 memset(&hints, 0, sizeof(hints));
41 hints.ai_family = AF_UNSPEC;
42 hints.ai_socktype = SOCK_STREAM;
43 hints.ai_flags = AI_PASSIVE;
44 hints.ai_protocol = 0;
46 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
49 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
50 addrinfo->ai_protocol);
54 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
55 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
59 if (listen(fd, 1) != 0) {
67 static void read_from_socket(size_t bytes, const struct addrinfo *addrinfo)
72 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
73 addrinfo->ai_protocol);
76 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
79 for (done = 0; done < bytes; done += r) {
80 r = read(fd, buf, sizeof(buf));
90 struct data *d = malloc(sizeof(*d));
91 struct addrinfo *addrinfo;
92 struct io_listener *l;
95 /* This is how many tests you plan to run */
99 d->buf = malloc(d->bytes);
100 memset(d->buf, 'a', d->bytes);
101 fd = make_listen_fd(PORT, &addrinfo);
103 l = io_new_listener(fd, init_conn, d);
107 io_close_listener(l);
108 read_from_socket(d->bytes, addrinfo);
109 freeaddrinfo(addrinfo);
118 ok1(WIFEXITED(status));
119 ok1(WEXITSTATUS(status) == 0);
121 freeaddrinfo(addrinfo);
124 io_close_listener(l);
126 /* This exits depending on whether all tests passed */
127 return exit_status();