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>
10 struct io_listener *l;
16 static void finish_ok(struct io_conn *conn, struct data *d)
21 static struct io_plan write_done(struct io_conn *conn, struct data *d)
24 return io_close(conn, NULL);
27 static void init_conn(int fd, struct data *d)
34 io_close_listener(d->l);
36 memset(d->wbuf, 7, sizeof(d->wbuf));
38 conn = io_new_conn(fd, io_read(d->buf, sizeof(d->buf), io_close, d),
40 ok1(io_duplex(conn, io_write(d->wbuf, sizeof(d->wbuf), write_done, d),
44 static int make_listen_fd(const char *port, struct addrinfo **info)
47 struct addrinfo *addrinfo, hints;
49 memset(&hints, 0, sizeof(hints));
50 hints.ai_family = AF_UNSPEC;
51 hints.ai_socktype = SOCK_STREAM;
52 hints.ai_flags = AI_PASSIVE;
53 hints.ai_protocol = 0;
55 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
58 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
59 addrinfo->ai_protocol);
63 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
64 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
68 if (listen(fd, 1) != 0) {
78 struct data *d = malloc(sizeof(*d));
79 struct addrinfo *addrinfo;
82 /* This is how many tests you plan to run */
85 fd = make_listen_fd("65012", &addrinfo);
87 d->l = io_new_listener(fd, init_conn, d);
94 io_close_listener(d->l);
96 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
97 addrinfo->ai_protocol);
100 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
102 signal(SIGPIPE, SIG_IGN);
103 for (i = 0; i < 32; i++) {
104 if (read(fd, buf+i, 1) != 1)
107 for (i = 0; i < strlen("hellothere"); i++) {
108 if (write(fd, "hellothere" + i, 1) != 1)
112 freeaddrinfo(addrinfo);
115 freeaddrinfo(addrinfo);
116 ok1(io_loop() == NULL);
118 ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
122 ok1(WIFEXITED(status));
123 ok1(WEXITSTATUS(status) == 0);
125 /* This exits depending on whether all tests passed */
126 return exit_status();