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>
19 static void finish_ok(struct io_conn *conn, struct data *d)
23 io_break(d, io_idle());
26 static void init_conn(int fd, struct data *d)
30 io_set_finish(io_new_conn(fd,
31 io_write_partial(d->buf, &d->bytes, io_close_cb, d)),
35 static int make_listen_fd(const char *port, struct addrinfo **info)
38 struct addrinfo *addrinfo, hints;
40 memset(&hints, 0, sizeof(hints));
41 hints.ai_family = AF_UNSPEC;
42 hints.ai_socktype = SOCK_STREAM;
43 hints.ai_flags = AI_PASSIVE;
44 hints.ai_protocol = 0;
46 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
49 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
50 addrinfo->ai_protocol);
54 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
55 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
59 if (listen(fd, 1) != 0) {
67 static void read_from_socket(const char *str, const struct addrinfo *addrinfo)
72 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
73 addrinfo->ai_protocol);
76 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
78 if (read(fd, buf, strlen(str)) != strlen(str))
80 if (memcmp(buf, str, strlen(str)) != 0)
87 struct data *d = malloc(sizeof(*d));
88 struct addrinfo *addrinfo;
89 struct io_listener *l;
92 /* This is how many tests you plan to run */
96 d->buf = malloc(d->bytes);
97 memset(d->buf, 'a', d->bytes);
98 fd = make_listen_fd(PORT, &addrinfo);
100 l = io_new_listener(fd, init_conn, d);
104 io_close_listener(l);
105 read_from_socket("aaaaaa", addrinfo);
106 freeaddrinfo(addrinfo);
114 ok1(d->bytes <= 1024*1024);
117 ok1(WIFEXITED(status));
118 ok1(WEXITSTATUS(status) == 0);
120 freeaddrinfo(addrinfo);
123 io_close_listener(l);
125 /* This exits depending on whether all tests passed */
126 return exit_status();