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>
14 static struct io_plan *start_ok(struct io_conn *conn, struct data *d)
18 return io_read(conn, d->buf, sizeof(d->buf), io_close, d);
21 static void finish_ok(struct io_conn *conn, struct data *d)
25 io_break(conn, d, NULL, NULL);
28 static int make_listen_fd(const char *port, struct addrinfo **info)
31 struct addrinfo *addrinfo, hints;
33 memset(&hints, 0, sizeof(hints));
34 hints.ai_family = AF_UNSPEC;
35 hints.ai_socktype = SOCK_STREAM;
36 hints.ai_flags = AI_PASSIVE;
37 hints.ai_protocol = 0;
39 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
42 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
43 addrinfo->ai_protocol);
47 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
48 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
52 if (listen(fd, 1) != 0) {
62 struct data *d = malloc(sizeof(*d));
63 struct addrinfo *addrinfo;
64 struct io_listener *l;
67 /* This is how many tests you plan to run */
70 fd = make_listen_fd("65002", &addrinfo);
72 l = io_new_listener(fd, start_ok, finish_ok, d);
79 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
80 addrinfo->ai_protocol);
83 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
85 signal(SIGPIPE, SIG_IGN);
86 for (i = 0; i < strlen("hellothere"); i++) {
87 if (write(fd, "hellothere" + i, 1) != 1)
91 freeaddrinfo(addrinfo);
95 freeaddrinfo(addrinfo);
98 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
100 io_close_listener(l);
103 ok1(WIFEXITED(status));
104 ok1(WEXITSTATUS(status) == 0);
106 /* This exits depending on whether all tests passed */
107 return exit_status();