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 struct io_plan no_timeout(struct io_conn *conn, struct data *d)
22 return io_close(conn, d);
25 static struct io_plan timeout(struct io_conn *conn, struct data *d)
30 return io_close(conn, d);
33 static void finish_ok(struct io_conn *conn, struct data *d)
37 io_break(d, io_idle());
40 static void init_conn(int fd, struct data *d)
47 conn = io_new_conn(fd, io_read(d->buf, sizeof(d->buf), no_timeout, d),
49 io_timeout(conn, time_from_usec(d->timeout_usec), timeout, d);
52 static int make_listen_fd(const char *port, struct addrinfo **info)
55 struct addrinfo *addrinfo, hints;
57 memset(&hints, 0, sizeof(hints));
58 hints.ai_family = AF_UNSPEC;
59 hints.ai_socktype = SOCK_STREAM;
60 hints.ai_flags = AI_PASSIVE;
61 hints.ai_protocol = 0;
63 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
66 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
67 addrinfo->ai_protocol);
71 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
72 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
76 if (listen(fd, 1) != 0) {
86 struct data *d = malloc(sizeof(*d));
87 struct addrinfo *addrinfo;
88 struct io_listener *l;
91 /* This is how many tests you plan to run */
95 d->timeout_usec = 100000;
96 fd = make_listen_fd("65002", &addrinfo);
98 l = io_new_listener(fd, init_conn, d);
105 io_close_listener(l);
106 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
107 addrinfo->ai_protocol);
110 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
112 signal(SIGPIPE, SIG_IGN);
114 for (i = 0; i < strlen("hellothere"); i++) {
115 if (write(fd, "hellothere" + i, 1) != 1)
119 freeaddrinfo(addrinfo);
125 ok1(d->timed_out == true);
127 ok1(WIFEXITED(status));
128 ok1(WEXITSTATUS(status) < sizeof(d->buf));
130 /* This one shouldn't time out. */
132 d->timed_out = false;
133 d->timeout_usec = 500000;
139 io_close_listener(l);
140 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
141 addrinfo->ai_protocol);
144 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
146 signal(SIGPIPE, SIG_IGN);
148 for (i = 0; i < strlen("hellothere"); i++) {
149 if (write(fd, "hellothere" + i, 1) != 1)
153 freeaddrinfo(addrinfo);
159 ok1(d->timed_out == false);
161 ok1(WIFEXITED(status));
162 ok1(WEXITSTATUS(status) >= sizeof(d->buf));
164 io_close_listener(l);
165 freeaddrinfo(addrinfo);
168 /* This exits depending on whether all tests passed */
169 return exit_status();