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 void finish_ok(struct io_conn *conn, struct data *d)
19 io_break(d, io_idle());
22 static void init_conn(int fd, struct data *d)
26 if (!io_new_conn(fd, io_write_partial(d->buf, &d->bytes, io_close, d),
31 static int make_listen_fd(const char *port, struct addrinfo **info)
34 struct addrinfo *addrinfo, hints;
36 memset(&hints, 0, sizeof(hints));
37 hints.ai_family = AF_UNSPEC;
38 hints.ai_socktype = SOCK_STREAM;
39 hints.ai_flags = AI_PASSIVE;
40 hints.ai_protocol = 0;
42 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
45 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
46 addrinfo->ai_protocol);
50 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
51 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
55 if (listen(fd, 1) != 0) {
63 static void read_from_socket(const char *str, const struct addrinfo *addrinfo)
68 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
69 addrinfo->ai_protocol);
72 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
74 if (read(fd, buf, strlen(str)) != strlen(str))
76 if (memcmp(buf, str, strlen(str)) != 0)
83 struct data *d = malloc(sizeof(*d));
84 struct addrinfo *addrinfo;
85 struct io_listener *l;
88 /* This is how many tests you plan to run */
92 d->buf = malloc(d->bytes);
93 memset(d->buf, 'a', d->bytes);
94 fd = make_listen_fd("65004", &addrinfo);
96 l = io_new_listener(fd, init_conn, d);
100 io_close_listener(l);
101 read_from_socket("aaaaaa", addrinfo);
102 freeaddrinfo(addrinfo);
110 ok1(d->bytes <= 1024*1024);
113 ok1(WIFEXITED(status));
114 ok1(WEXITSTATUS(status) == 0);
116 freeaddrinfo(addrinfo);
119 io_close_listener(l);
121 /* This exits depending on whether all tests passed */
122 return exit_status();