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>
15 static struct io_plan *start_ok(struct io_conn *conn, struct data *d)
19 return io_write_partial(conn, d->buf, &d->bytes, io_close, d);
22 static void finish_ok(struct io_conn *conn, struct data *d)
26 io_break(conn, d, NULL, NULL);
29 static int make_listen_fd(const char *port, struct addrinfo **info)
32 struct addrinfo *addrinfo, hints;
34 memset(&hints, 0, sizeof(hints));
35 hints.ai_family = AF_UNSPEC;
36 hints.ai_socktype = SOCK_STREAM;
37 hints.ai_flags = AI_PASSIVE;
38 hints.ai_protocol = 0;
40 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
43 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
44 addrinfo->ai_protocol);
48 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
49 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
53 if (listen(fd, 1) != 0) {
61 static void read_from_socket(const char *str, const struct addrinfo *addrinfo)
66 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
67 addrinfo->ai_protocol);
70 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
72 if (read(fd, buf, strlen(str)) != strlen(str))
74 if (memcmp(buf, str, strlen(str)) != 0)
81 struct data *d = malloc(sizeof(*d));
82 struct addrinfo *addrinfo;
83 struct io_listener *l;
86 /* This is how many tests you plan to run */
90 d->buf = malloc(d->bytes);
91 memset(d->buf, 'a', d->bytes);
92 fd = make_listen_fd("65004", &addrinfo);
94 l = io_new_listener(fd, start_ok, finish_ok, d);
99 read_from_socket("aaaaaa", addrinfo);
100 freeaddrinfo(addrinfo);
108 ok1(d->bytes <= 1024*1024);
111 ok1(WIFEXITED(status));
112 ok1(WEXITSTATUS(status) == 0);
114 freeaddrinfo(addrinfo);
117 io_close_listener(l);
119 /* This exits depending on whether all tests passed */
120 return exit_status();