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 d->bytes = sizeof(d->buf);
20 return io_read_partial(d->buf, &d->bytes, io_close, d);
23 static void finish_ok(struct io_conn *conn, struct data *d)
27 io_break(d, NULL, NULL);
30 static void init_conn(int fd, struct data *d)
32 if (!io_new_conn(fd, start_ok, finish_ok, d))
36 static int make_listen_fd(const char *port, struct addrinfo **info)
39 struct addrinfo *addrinfo, hints;
41 memset(&hints, 0, sizeof(hints));
42 hints.ai_family = AF_UNSPEC;
43 hints.ai_socktype = SOCK_STREAM;
44 hints.ai_flags = AI_PASSIVE;
45 hints.ai_protocol = 0;
47 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
50 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
51 addrinfo->ai_protocol);
55 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
56 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
60 if (listen(fd, 1) != 0) {
68 static void write_to_socket(const char *str, const struct addrinfo *addrinfo)
72 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
73 addrinfo->ai_protocol);
76 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
78 signal(SIGPIPE, SIG_IGN);
79 for (i = 0; i < strlen(str); i++) {
80 if (write(fd, str + i, 1) != 1)
88 struct data *d = malloc(sizeof(*d));
89 struct addrinfo *addrinfo;
90 struct io_listener *l;
93 /* This is how many tests you plan to run */
96 fd = make_listen_fd("65003", &addrinfo);
98 l = io_new_listener(fd, init_conn, d);
102 io_close_listener(l);
103 write_to_socket("hellothere", addrinfo);
104 freeaddrinfo(addrinfo);
111 ok1(d->bytes <= sizeof(d->buf));
112 ok1(memcmp(d->buf, "hellothere", d->bytes) == 0);
115 ok1(WIFEXITED(status));
116 ok1(WEXITSTATUS(status) == 0);
120 io_close_listener(l);
121 write_to_socket("hi", addrinfo);
122 freeaddrinfo(addrinfo);
130 ok1(d->bytes <= strlen("hi"));
131 ok1(memcmp(d->buf, "hi", d->bytes) == 0);
133 freeaddrinfo(addrinfo);
135 io_close_listener(l);
138 ok1(WIFEXITED(status));
139 ok1(WEXITSTATUS(status) == 0);
141 /* This exits depending on whether all tests passed */
142 return exit_status();