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 d->bytes = sizeof(d->buf);
32 io_set_finish(io_new_conn(fd,
33 io_read_partial(d->buf, &d->bytes, io_close_cb, d)),
37 static int make_listen_fd(const char *port, struct addrinfo **info)
40 struct addrinfo *addrinfo, hints;
42 memset(&hints, 0, sizeof(hints));
43 hints.ai_family = AF_UNSPEC;
44 hints.ai_socktype = SOCK_STREAM;
45 hints.ai_flags = AI_PASSIVE;
46 hints.ai_protocol = 0;
48 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
51 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
52 addrinfo->ai_protocol);
56 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
57 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
61 if (listen(fd, 1) != 0) {
69 static void write_to_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 signal(SIGPIPE, SIG_IGN);
80 for (i = 0; i < strlen(str); i++) {
81 if (write(fd, str + i, 1) != 1)
89 struct data *d = malloc(sizeof(*d));
90 struct addrinfo *addrinfo;
91 struct io_listener *l;
94 /* This is how many tests you plan to run */
97 fd = make_listen_fd(PORT, &addrinfo);
99 l = io_new_listener(fd, init_conn, d);
103 io_close_listener(l);
104 write_to_socket("hellothere", addrinfo);
105 freeaddrinfo(addrinfo);
112 ok1(d->bytes <= sizeof(d->buf));
113 ok1(memcmp(d->buf, "hellothere", d->bytes) == 0);
116 ok1(WIFEXITED(status));
117 ok1(WEXITSTATUS(status) == 0);
121 io_close_listener(l);
122 write_to_socket("hi", addrinfo);
123 freeaddrinfo(addrinfo);
131 ok1(d->bytes <= strlen("hi"));
132 ok1(memcmp(d->buf, "hi", d->bytes) == 0);
134 freeaddrinfo(addrinfo);
136 io_close_listener(l);
139 ok1(WIFEXITED(status));
140 ok1(WEXITSTATUS(status) == 0);
142 /* This exits depending on whether all tests passed */
143 return exit_status();