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)
26 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
30 io_set_finish(conn, finish_ok, d);
32 return io_write_partial(conn, d->buf, d->bytes, &d->bytes,
36 static int make_listen_fd(const char *port, struct addrinfo **info)
39 struct addrinfo *addrinfo, hints;
41 memset(&hints, 0, sizeof(hints));
42 hints.ai_family = AF_UNSPEC;
43 hints.ai_socktype = SOCK_STREAM;
44 hints.ai_flags = AI_PASSIVE;
45 hints.ai_protocol = 0;
47 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
50 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
51 addrinfo->ai_protocol);
55 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
56 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
60 if (listen(fd, 1) != 0) {
68 static void read_from_socket(const char *str, const struct addrinfo *addrinfo)
73 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
74 addrinfo->ai_protocol);
77 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
79 if (read(fd, buf, strlen(str)) != strlen(str))
81 if (memcmp(buf, str, strlen(str)) != 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->buf = malloc(d->bytes);
98 memset(d->buf, 'a', d->bytes);
99 fd = make_listen_fd(PORT, &addrinfo);
101 l = io_new_listener(NULL, fd, init_conn, d);
105 io_close_listener(l);
106 read_from_socket("aaaaaa", addrinfo);
107 freeaddrinfo(addrinfo);
112 ok1(io_loop(NULL, NULL) == d);
115 ok1(d->bytes <= 1024*1024);
118 ok1(WIFEXITED(status));
119 ok1(WEXITSTATUS(status) == 0);
121 freeaddrinfo(addrinfo);
124 io_close_listener(l);
126 /* This exits depending on whether all tests passed */
127 return exit_status();