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 struct io_plan *read_done(struct io_conn *conn, struct data *d)
20 return io_close(conn);
23 static void finish_ok(struct io_conn *conn, struct data *d)
29 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
34 io_set_finish(conn, finish_ok, d);
37 return io_read(conn, d->buf, sizeof(d->buf), read_done, d);
40 static int make_listen_fd(const char *port, struct addrinfo **info)
43 struct addrinfo *addrinfo, hints;
45 memset(&hints, 0, sizeof(hints));
46 hints.ai_family = AF_UNSPEC;
47 hints.ai_socktype = SOCK_STREAM;
48 hints.ai_flags = AI_PASSIVE;
49 hints.ai_protocol = 0;
51 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
54 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
55 addrinfo->ai_protocol);
59 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
60 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
64 if (listen(fd, 1) != 0) {
74 struct data *d = malloc(sizeof(*d));
75 struct addrinfo *addrinfo;
76 struct io_listener *l;
79 /* This is how many tests you plan to run */
82 fd = make_listen_fd(PORT, &addrinfo);
84 l = io_new_listener(NULL, fd, init_conn, d);
91 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
92 addrinfo->ai_protocol);
95 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
97 signal(SIGPIPE, SIG_IGN);
98 for (i = 0; i < strlen("hellothere"); i++) {
99 if (write(fd, "hellothere" + i, 1) != 1)
103 freeaddrinfo(addrinfo);
107 freeaddrinfo(addrinfo);
108 ok1(io_loop(NULL, NULL) == d);
110 io_close_listener(l);
112 ok1(io_loop(NULL, NULL) == NULL);
114 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
118 ok1(WIFEXITED(status));
119 ok1(WEXITSTATUS(status) == 0);
121 /* This exits depending on whether all tests passed */
122 return exit_status();