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>
18 static void finish_ok(struct io_conn *conn, struct data *d)
22 io_break(d, io_idle());
25 static void init_conn(int fd, struct data *d)
30 io_set_finish(io_new_conn(fd,
31 io_read(d->buf, sizeof(d->buf), io_close_cb, d)),
35 static int make_listen_fd(const char *port, struct addrinfo **info)
38 struct addrinfo *addrinfo, hints;
40 memset(&hints, 0, sizeof(hints));
41 hints.ai_family = AF_UNSPEC;
42 hints.ai_socktype = SOCK_STREAM;
43 hints.ai_flags = AI_PASSIVE;
44 hints.ai_protocol = 0;
46 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
49 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
50 addrinfo->ai_protocol);
54 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
55 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
59 if (listen(fd, 1) != 0) {
69 struct data *d = malloc(sizeof(*d));
70 struct addrinfo *addrinfo;
71 struct io_listener *l;
74 /* This is how many tests you plan to run */
77 fd = make_listen_fd(PORT, &addrinfo);
79 l = io_new_listener(fd, init_conn, d);
86 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
87 addrinfo->ai_protocol);
90 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
92 signal(SIGPIPE, SIG_IGN);
93 for (i = 0; i < strlen("hellothere"); i++) {
94 if (write(fd, "hellothere" + i, 1) != 1)
98 freeaddrinfo(addrinfo);
102 freeaddrinfo(addrinfo);
105 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
107 io_close_listener(l);
110 ok1(WIFEXITED(status));
111 ok1(WEXITSTATUS(status) == 0);
113 /* This exits depending on whether all tests passed */
114 return exit_status();