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>
16 static void finish_ok(struct io_conn *conn, struct data *d)
23 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
28 io_set_finish(conn, finish_ok, d);
29 return io_read(conn, d->buf, sizeof(d->buf), io_close_cb, d);
32 static int make_listen_fd(const char *port, struct addrinfo **info)
35 struct addrinfo *addrinfo, hints;
37 memset(&hints, 0, sizeof(hints));
38 hints.ai_family = AF_UNSPEC;
39 hints.ai_socktype = SOCK_STREAM;
40 hints.ai_flags = AI_PASSIVE;
41 hints.ai_protocol = 0;
43 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
46 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
47 addrinfo->ai_protocol);
51 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
52 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
56 if (listen(fd, 1) != 0) {
66 struct data *d = malloc(sizeof(*d));
67 struct addrinfo *addrinfo = NULL;
68 struct io_listener *l;
71 /* This is how many tests you plan to run */
74 fd = make_listen_fd(PORT, &addrinfo);
76 l = io_new_listener(NULL, fd, init_conn, d);
83 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
84 addrinfo->ai_protocol);
87 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
89 signal(SIGPIPE, SIG_IGN);
90 for (i = 0; i < strlen("hellothere"); i++) {
91 if (write(fd, "hellothere" + i, 1) != 1)
95 freeaddrinfo(addrinfo);
99 freeaddrinfo(addrinfo);
100 ok1(io_loop(NULL, NULL) == d);
102 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
104 io_close_listener(l);
107 ok1(WIFEXITED(status));
108 ok1(WEXITSTATUS(status) == 0);
110 /* This exits depending on whether all tests passed */
111 return exit_status();