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>
17 static void finish_ok(struct io_conn *conn, struct data *d)
24 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
28 io_set_finish(conn, finish_ok, d);
30 return io_write_partial(conn, d->buf, d->bytes, &d->bytes,
34 static int make_listen_fd(const char *port, struct addrinfo **info)
37 struct addrinfo *addrinfo, hints;
39 memset(&hints, 0, sizeof(hints));
40 hints.ai_family = AF_UNSPEC;
41 hints.ai_socktype = SOCK_STREAM;
42 hints.ai_flags = AI_PASSIVE;
43 hints.ai_protocol = 0;
45 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
48 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
49 addrinfo->ai_protocol);
53 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
54 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
58 if (listen(fd, 1) != 0) {
66 static void read_from_socket(const char *str, const struct addrinfo *addrinfo)
71 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
72 addrinfo->ai_protocol);
75 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
77 if (read(fd, buf, strlen(str)) != strlen(str))
79 if (memcmp(buf, str, strlen(str)) != 0)
86 struct data *d = malloc(sizeof(*d));
87 struct addrinfo *addrinfo;
88 struct io_listener *l;
91 /* This is how many tests you plan to run */
95 d->buf = malloc(d->bytes);
96 memset(d->buf, 'a', d->bytes);
97 fd = make_listen_fd(PORT, &addrinfo);
99 l = io_new_listener(NULL, fd, init_conn, d);
103 io_close_listener(l);
104 read_from_socket("aaaaaa", addrinfo);
105 freeaddrinfo(addrinfo);
110 ok1(io_loop(NULL, NULL) == d);
113 ok1(d->bytes <= 1024*1024);
116 ok1(WIFEXITED(status));
117 ok1(WEXITSTATUS(status) == 0);
119 freeaddrinfo(addrinfo);
122 io_close_listener(l);
124 /* This exits depending on whether all tests passed */
125 return exit_status();