1 #include <ccan/tap/tap.h>
8 /* Make sure we override these! */
9 static void *no_malloc(size_t size)
13 static void *no_realloc(void *p, size_t size)
17 static void no_free(void *p)
21 #define malloc no_malloc
22 #define realloc no_realloc
25 #include <ccan/io/poll.c>
26 #include <ccan/io/io.c>
32 static unsigned int alloc_count, realloc_count, free_count;
33 static void *ptrs[100];
35 static void **find_ptr(void *p)
39 for (i = 0; i < 100; i++)
45 static void *allocfn(size_t size)
48 return *find_ptr(NULL) = malloc(size);
51 static void *reallocfn(void *ptr, size_t size)
57 return *find_ptr(ptr) = realloc(ptr, size);
60 static void freefn(void *ptr)
64 *find_ptr(ptr) = NULL;
79 static struct io_plan no_timeout(struct io_conn *conn, struct data *d)
86 static struct io_plan timeout(struct io_conn *conn, struct data *d)
94 static void finish_ok(struct io_conn *conn, struct data *d)
98 io_break(d, io_never());
101 static void init_conn(int fd, struct data *d)
103 struct io_conn *conn;
108 conn = io_new_conn(fd, io_read(d->buf, sizeof(d->buf), no_timeout, d));
109 io_set_finish(conn, finish_ok, d);
110 io_timeout(conn, time_from_usec(d->timeout_usec), timeout, d);
113 static int make_listen_fd(const char *port, struct addrinfo **info)
116 struct addrinfo *addrinfo, hints;
118 memset(&hints, 0, sizeof(hints));
119 hints.ai_family = AF_UNSPEC;
120 hints.ai_socktype = SOCK_STREAM;
121 hints.ai_flags = AI_PASSIVE;
122 hints.ai_protocol = 0;
124 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
127 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
128 addrinfo->ai_protocol);
132 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
133 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
137 if (listen(fd, 1) != 0) {
147 struct data *d = allocfn(sizeof(*d));
148 struct addrinfo *addrinfo;
149 struct io_listener *l;
152 io_set_alloc(allocfn, reallocfn, freefn);
154 /* This is how many tests you plan to run */
157 d->timed_out = false;
158 d->timeout_usec = 100000;
159 fd = make_listen_fd(PORT, &addrinfo);
161 l = io_new_listener(fd, init_conn, d);
168 io_close_listener(l);
169 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
170 addrinfo->ai_protocol);
173 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
175 signal(SIGPIPE, SIG_IGN);
177 for (i = 0; i < strlen("hellothere"); i++) {
178 if (write(fd, "hellothere" + i, 1) != 1)
182 freeaddrinfo(addrinfo);
188 ok1(d->timed_out == true);
190 ok1(WIFEXITED(status));
191 ok1(WEXITSTATUS(status) < sizeof(d->buf));
193 /* This one shouldn't time out. */
195 d->timed_out = false;
196 d->timeout_usec = 500000;
202 io_close_listener(l);
203 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
204 addrinfo->ai_protocol);
207 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
209 signal(SIGPIPE, SIG_IGN);
211 for (i = 0; i < strlen("hellothere"); i++) {
212 if (write(fd, "hellothere" + i, 1) != 1)
216 freeaddrinfo(addrinfo);
222 ok1(d->timed_out == false);
224 ok1(WIFEXITED(status));
225 ok1(WEXITSTATUS(status) >= sizeof(d->buf));
227 io_close_listener(l);
228 freeaddrinfo(addrinfo);
230 /* We should have tested each one at least once! */
235 ok1(free_count < alloc_count);
237 ok1(free_count == alloc_count);
239 return exit_status();