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 *do_read(struct io_conn *conn, struct data *d)
18 return io_read(conn, d->buf, sizeof(d->buf), io_close, d);
21 static struct io_plan *start_break(struct io_conn *conn, struct data *d)
25 return io_break(conn, d, do_read, d);
28 static void finish_ok(struct io_conn *conn, struct data *d)
34 static int make_listen_fd(const char *port, struct addrinfo **info)
37 struct addrinfo *addrinfo, hints;
39 memset(&hints, 0, sizeof(hints));
40 hints.ai_family = AF_UNSPEC;
41 hints.ai_socktype = SOCK_STREAM;
42 hints.ai_flags = AI_PASSIVE;
43 hints.ai_protocol = 0;
45 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
48 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
49 addrinfo->ai_protocol);
53 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
54 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
58 if (listen(fd, 1) != 0) {
68 struct data *d = malloc(sizeof(*d));
69 struct addrinfo *addrinfo;
70 struct io_listener *l;
73 /* This is how many tests you plan to run */
76 fd = make_listen_fd("65007", &addrinfo);
78 l = io_new_listener(fd, start_break, finish_ok, d);
85 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
86 addrinfo->ai_protocol);
89 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
91 signal(SIGPIPE, SIG_IGN);
92 for (i = 0; i < strlen("hellothere"); i++) {
93 if (write(fd, "hellothere" + i, 1) != 1)
97 freeaddrinfo(addrinfo);
101 freeaddrinfo(addrinfo);
104 io_close_listener(l);
106 ok1(io_loop() == NULL);
108 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
112 ok1(WIFEXITED(status));
113 ok1(WEXITSTATUS(status) == 0);
115 /* This exits depending on whether all tests passed */
116 return exit_status();