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(d->buf, sizeof(d->buf), no_timeout, d);
41 static void finish_ok(struct io_conn *conn, struct data *d)
45 io_break(d, NULL, NULL);
48 static void init_conn(int fd, struct data *d)
50 if (!io_new_conn(fd, start_ok, finish_ok, d))
54 static int make_listen_fd(const char *port, struct addrinfo **info)
57 struct addrinfo *addrinfo, hints;
59 memset(&hints, 0, sizeof(hints));
60 hints.ai_family = AF_UNSPEC;
61 hints.ai_socktype = SOCK_STREAM;
62 hints.ai_flags = AI_PASSIVE;
63 hints.ai_protocol = 0;
65 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
68 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
69 addrinfo->ai_protocol);
73 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
74 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
78 if (listen(fd, 1) != 0) {
88 struct data *d = malloc(sizeof(*d));
89 struct addrinfo *addrinfo;
90 struct io_listener *l;
93 /* This is how many tests you plan to run */
97 d->timeout_usec = 100000;
98 fd = make_listen_fd("65002", &addrinfo);
100 l = io_new_listener(fd, init_conn, d);
107 io_close_listener(l);
108 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
109 addrinfo->ai_protocol);
112 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
114 signal(SIGPIPE, SIG_IGN);
116 for (i = 0; i < strlen("hellothere"); i++) {
117 if (write(fd, "hellothere" + i, 1) != 1)
121 freeaddrinfo(addrinfo);
127 ok1(d->timed_out == true);
129 ok1(WIFEXITED(status));
130 ok1(WEXITSTATUS(status) < sizeof(d->buf));
132 /* This one shouldn't time out. */
134 d->timed_out = false;
135 d->timeout_usec = 500000;
141 io_close_listener(l);
142 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
143 addrinfo->ai_protocol);
146 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
148 signal(SIGPIPE, SIG_IGN);
150 for (i = 0; i < strlen("hellothere"); i++) {
151 if (write(fd, "hellothere" + i, 1) != 1)
155 freeaddrinfo(addrinfo);
161 ok1(d->timed_out == false);
163 ok1(WIFEXITED(status));
164 ok1(WEXITSTATUS(status) >= sizeof(d->buf));
166 io_close_listener(l);
167 freeaddrinfo(addrinfo);
170 /* This exits depending on whether all tests passed */
171 return exit_status();