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>
20 static struct io_plan *read_done(struct io_conn *conn, struct data *d)
24 return io_close(conn);
27 static void finish_ok(struct io_conn *conn, struct data *d)
33 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
36 io_set_debug(conn, true);
41 io_set_finish(conn, finish_ok, d);
44 return io_read(conn, d->buf, sizeof(d->buf), read_done, d);
47 static int make_listen_fd(const char *port, struct addrinfo **info)
50 struct addrinfo *addrinfo, hints;
52 memset(&hints, 0, sizeof(hints));
53 hints.ai_family = AF_UNSPEC;
54 hints.ai_socktype = SOCK_STREAM;
55 hints.ai_flags = AI_PASSIVE;
56 hints.ai_protocol = 0;
58 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
61 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
62 addrinfo->ai_protocol);
66 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
67 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
71 if (listen(fd, 1) != 0) {
81 struct data *d = malloc(sizeof(*d));
82 struct addrinfo *addrinfo;
83 struct io_listener *l;
86 /* This is how many tests you plan to run */
89 fd = make_listen_fd(PORT, &addrinfo);
91 l = io_new_listener(NULL, fd, init_conn, d);
98 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
99 addrinfo->ai_protocol);
102 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
104 signal(SIGPIPE, SIG_IGN);
105 for (i = 0; i < strlen("hellothere"); i++) {
106 if (write(fd, "hellothere" + i, 1) != 1)
110 freeaddrinfo(addrinfo);
114 freeaddrinfo(addrinfo);
115 ok1(io_loop(NULL, NULL) == d);
117 io_close_listener(l);
119 ok1(io_loop(NULL, NULL) == NULL);
121 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
125 ok1(WIFEXITED(status));
126 ok1(WEXITSTATUS(status) == 0);
128 /* This exits depending on whether all tests passed */
129 return exit_status();