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>
11 static struct io_listener *l;
13 static struct io_plan *steal_fd(struct io_conn *conn, int *fd)
16 *fd = io_conn_fd(conn);
17 return io_close_taken_fd(conn);
20 static int make_listen_fd(const char *port, struct addrinfo **info)
23 struct addrinfo *addrinfo, hints;
25 memset(&hints, 0, sizeof(hints));
26 hints.ai_family = AF_UNSPEC;
27 hints.ai_socktype = SOCK_STREAM;
28 hints.ai_flags = AI_PASSIVE;
29 hints.ai_protocol = 0;
31 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
34 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
35 addrinfo->ai_protocol);
39 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
40 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
44 if (listen(fd, 1) != 0) {
54 struct addrinfo *addrinfo = NULL;
55 int i, fd, in_fd, status;
56 char buf[strlen("hellothere")];
58 /* This is how many tests you plan to run */
60 fd = make_listen_fd(PORT, &addrinfo);
61 l = io_new_listener(NULL, fd, steal_fd, &in_fd);
65 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
66 addrinfo->ai_protocol);
69 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
71 signal(SIGPIPE, SIG_IGN);
72 for (i = 0; i < strlen("hellothere"); i++) {
73 if (write(fd, "hellothere" + i, 1) != 1)
77 freeaddrinfo(addrinfo);
80 freeaddrinfo(addrinfo);
81 ok1(io_loop(NULL, NULL) == NULL);
83 for (i = 0; i < strlen("hellothere"); i++)
84 ok1(read(in_fd, buf + i, 1) == 1);
86 ok1(memcmp(buf, "hellothere", sizeof(buf)) == 0);
88 ok1(WIFEXITED(status));
89 ok1(WEXITSTATUS(status) == 0);
91 /* This exits depending on whether all tests passed */