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 struct io_plan start_ok(struct io_conn *conn, struct data *d)
37 io_timeout(conn, time_from_usec(d->timeout_usec), timeout, d);
38 return io_read(conn, d->buf, sizeof(d->buf), no_timeout, d);
41 static void finish_ok(struct io_conn *conn, struct data *d)
45 io_break(conn, d, NULL, NULL);
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;
84 struct io_listener *l;
87 /* This is how many tests you plan to run */
91 d->timeout_usec = 100000;
92 fd = make_listen_fd("65002", &addrinfo);
94 l = io_new_listener(fd, start_ok, finish_ok, d);
101 io_close_listener(l);
102 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
103 addrinfo->ai_protocol);
106 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
108 signal(SIGPIPE, SIG_IGN);
110 for (i = 0; i < strlen("hellothere"); i++) {
111 if (write(fd, "hellothere" + i, 1) != 1)
115 freeaddrinfo(addrinfo);
121 ok1(d->timed_out == true);
123 ok1(WIFEXITED(status));
124 ok1(WEXITSTATUS(status) < sizeof(d->buf));
126 /* This one shouldn't time out. */
128 d->timed_out = false;
129 d->timeout_usec = 500000;
135 io_close_listener(l);
136 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
137 addrinfo->ai_protocol);
140 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
142 signal(SIGPIPE, SIG_IGN);
144 for (i = 0; i < strlen("hellothere"); i++) {
145 if (write(fd, "hellothere" + i, 1) != 1)
149 freeaddrinfo(addrinfo);
155 ok1(d->timed_out == false);
157 ok1(WIFEXITED(status));
158 ok1(WEXITSTATUS(status) >= sizeof(d->buf));
160 io_close_listener(l);
161 freeaddrinfo(addrinfo);
164 /* This exits depending on whether all tests passed */
165 return exit_status();