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 struct io_listener *l;
20 static void finish_ok(struct io_conn *conn, struct data *d)
25 static struct io_plan write_done(struct io_conn *conn, struct data *d)
28 return io_close(conn, NULL);
31 static void init_conn(int fd, struct data *d)
38 io_close_listener(d->l);
40 memset(d->wbuf, 7, sizeof(d->wbuf));
42 conn = io_new_conn(fd, io_read(d->buf, sizeof(d->buf), io_close, d),
44 ok1(io_duplex(conn, io_write(d->wbuf, sizeof(d->wbuf), write_done, d),
48 static int make_listen_fd(const char *port, struct addrinfo **info)
51 struct addrinfo *addrinfo, hints;
53 memset(&hints, 0, sizeof(hints));
54 hints.ai_family = AF_UNSPEC;
55 hints.ai_socktype = SOCK_STREAM;
56 hints.ai_flags = AI_PASSIVE;
57 hints.ai_protocol = 0;
59 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
62 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
63 addrinfo->ai_protocol);
67 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
68 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
72 if (listen(fd, 1) != 0) {
82 struct data *d = malloc(sizeof(*d));
83 struct addrinfo *addrinfo;
86 /* This is how many tests you plan to run */
89 fd = make_listen_fd(PORT, &addrinfo);
91 d->l = io_new_listener(fd, init_conn, d);
98 io_close_listener(d->l);
100 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
101 addrinfo->ai_protocol);
104 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
106 signal(SIGPIPE, SIG_IGN);
107 for (i = 0; i < 32; i++) {
108 if (read(fd, buf+i, 1) != 1)
111 for (i = 0; i < strlen("hellothere"); i++) {
112 if (write(fd, "hellothere" + i, 1) != 1)
116 freeaddrinfo(addrinfo);
119 freeaddrinfo(addrinfo);
120 ok1(io_loop() == NULL);
122 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
126 ok1(WIFEXITED(status));
127 ok1(WEXITSTATUS(status) == 0);
129 /* This exits depending on whether all tests passed */
130 return exit_status();