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 void finish_ok(struct io_conn *conn, struct data *d)
18 io_break(d, io_idle());
21 static void init_conn(int fd, struct data *d)
26 if (!io_new_conn(fd, io_read(d->buf, sizeof(d->buf), io_close, d),
31 static int make_listen_fd(const char *port, struct addrinfo **info)
34 struct addrinfo *addrinfo, hints;
36 memset(&hints, 0, sizeof(hints));
37 hints.ai_family = AF_UNSPEC;
38 hints.ai_socktype = SOCK_STREAM;
39 hints.ai_flags = AI_PASSIVE;
40 hints.ai_protocol = 0;
42 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
45 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
46 addrinfo->ai_protocol);
50 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
51 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
55 if (listen(fd, 1) != 0) {
65 struct data *d = malloc(sizeof(*d));
66 struct addrinfo *addrinfo;
67 struct io_listener *l;
70 /* This is how many tests you plan to run */
73 fd = make_listen_fd("65002", &addrinfo);
75 l = io_new_listener(fd, init_conn, d);
82 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
83 addrinfo->ai_protocol);
86 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
88 signal(SIGPIPE, SIG_IGN);
89 for (i = 0; i < strlen("hellothere"); i++) {
90 if (write(fd, "hellothere" + i, 1) != 1)
94 freeaddrinfo(addrinfo);
98 freeaddrinfo(addrinfo);
101 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
103 io_close_listener(l);
106 ok1(WIFEXITED(status));
107 ok1(WEXITSTATUS(status) == 0);
109 /* This exits depending on whether all tests passed */
110 return exit_status();