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_op *start_ok(struct io_conn *conn, struct data *d)
19 d->bytes = sizeof(d->buf);
20 return io_read_partial(d->buf, &d->bytes, io_next(conn, io_close, d));
23 static void finish_ok(struct io_conn *conn, struct data *d)
30 static int make_listen_fd(const char *port, struct addrinfo **info)
33 struct addrinfo *addrinfo, hints;
35 memset(&hints, 0, sizeof(hints));
36 hints.ai_family = AF_UNSPEC;
37 hints.ai_socktype = SOCK_STREAM;
38 hints.ai_flags = AI_PASSIVE;
39 hints.ai_protocol = 0;
41 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
44 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
45 addrinfo->ai_protocol);
49 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
50 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
54 if (listen(fd, 1) != 0) {
62 static void write_to_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 signal(SIGPIPE, SIG_IGN);
73 for (i = 0; i < strlen(str); i++) {
74 if (write(fd, str + i, 1) != 1)
82 struct data *d = malloc(sizeof(*d));
83 struct addrinfo *addrinfo;
84 struct io_listener *l;
87 /* This is how many tests you plan to run */
90 fd = make_listen_fd("65003", &addrinfo);
92 l = io_new_listener(fd, start_ok, finish_ok, d);
97 write_to_socket("hellothere", addrinfo);
98 freeaddrinfo(addrinfo);
105 ok1(d->bytes <= sizeof(d->buf));
106 ok1(memcmp(d->buf, "hellothere", d->bytes) == 0);
109 ok1(WIFEXITED(status));
110 ok1(WEXITSTATUS(status) == 0);
114 io_close_listener(l);
115 write_to_socket("hi", addrinfo);
116 freeaddrinfo(addrinfo);
124 ok1(d->bytes <= strlen("hi"));
125 ok1(memcmp(d->buf, "hi", d->bytes) == 0);
127 freeaddrinfo(addrinfo);
129 io_close_listener(l);
132 ok1(WIFEXITED(status));
133 ok1(WEXITSTATUS(status) == 0);
135 /* This exits depending on whether all tests passed */
136 return exit_status();