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>
12 struct io_listener *l;
19 static struct io_plan *read_more(struct io_conn *conn, struct data *d);
21 static struct io_plan *read_done(struct io_conn *conn, struct data *d)
23 tal_resize(&d->pattern, tal_count(d->pattern) + strlen(d->buf));
24 strcat(d->pattern, d->buf);
25 return read_more(conn, d);
28 static struct io_plan *read_more(struct io_conn *conn, struct data *d)
30 memset(d->buf, 0, sizeof(d->buf));
31 return io_read_partial(conn, d->buf, sizeof(d->buf), &d->buflen,
36 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
39 if (d->num_clients == 2) {
40 /* Free listener so when conns close we exit io_loop */
41 io_close_listener(d->l);
42 /* Set priority to second connection. */
43 ok1(io_conn_exclusive(conn, true) == true);
45 return read_more(conn, d);
49 static int make_listen_fd(const char *port, struct addrinfo **info)
52 struct addrinfo *addrinfo, hints;
54 memset(&hints, 0, sizeof(hints));
55 hints.ai_family = AF_UNSPEC;
56 hints.ai_socktype = SOCK_STREAM;
57 hints.ai_flags = AI_PASSIVE;
58 hints.ai_protocol = 0;
60 if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
63 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
64 addrinfo->ai_protocol);
68 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
69 if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
73 if (listen(fd, 1) != 0) {
83 struct addrinfo *addrinfo = NULL;
89 /* This is how many tests you plan to run */
91 fd = make_listen_fd(PORT, &addrinfo);
93 d.l = io_new_listener(NULL, fd, init_conn, &d);
100 io_close_listener(d.l);
101 fd1 = socket(addrinfo->ai_family, addrinfo->ai_socktype,
102 addrinfo->ai_protocol);
105 if (connect(fd1, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
107 if (write(fd1, "1hellothere", strlen("1hellothere")) != strlen("1hellothere"))
109 fd2 = socket(addrinfo->ai_family, addrinfo->ai_socktype,
110 addrinfo->ai_protocol);
113 if (connect(fd2, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
115 signal(SIGPIPE, SIG_IGN);
118 if (write(fd1, "1helloagain", strlen("1helloagain")) != strlen("1helloagain"))
121 if (write(fd2, "2hellonew", strlen("2hellonew")) != strlen("2hellonew"))
125 freeaddrinfo(addrinfo);
128 freeaddrinfo(addrinfo);
130 d.pattern = tal_arrz(NULL, char, 1);
131 ok1(io_loop(NULL, NULL) == NULL);
132 if (!ok1(strcmp(d.pattern, "1hellothere2hellonew1helloagain") == 0))
133 printf("d.patterns = %s\n", d.pattern);
137 ok1(WIFEXITED(status));
138 ok1(WEXITSTATUS(status) == 0);
140 /* This exits depending on whether all tests passed */
141 return exit_status();