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 void finish_ok(struct io_conn *conn, struct data *d)
27 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
30 io_set_debug(conn, true);
35 io_set_finish(conn, finish_ok, d);
36 return io_read(conn, d->buf, sizeof(d->buf), io_close_cb, 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;
75 struct io_listener *l;
78 /* This is how many tests you plan to run */
81 fd = make_listen_fd(PORT, &addrinfo);
83 l = io_new_listener(NULL, fd, init_conn, d);
90 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
91 addrinfo->ai_protocol);
94 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
96 signal(SIGPIPE, SIG_IGN);
97 for (i = 0; i < strlen("hellothere"); i++) {
98 if (write(fd, "hellothere" + i, 1) != 1)
102 freeaddrinfo(addrinfo);
106 freeaddrinfo(addrinfo);
107 ok1(io_loop(NULL, NULL) == d);
109 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
111 io_close_listener(l);
114 ok1(WIFEXITED(status));
115 ok1(WEXITSTATUS(status) == 0);
117 /* This exits depending on whether all tests passed */
118 return exit_status();