]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-14-duplex-both-read.c
366f1d44bbaafdf65c09baa1942a490df6c60053
[ccan] / ccan / io / test / run-14-duplex-both-read.c
1 /* Check a bug where we have just completed a read, then set up a duplex
2  * which tries to do a read. */
3 #include <ccan/io/io.h>
4 /* Include the C files directly. */
5 #include <ccan/io/poll.c>
6 #include <ccan/io/io.c>
7 #include <ccan/tap/tap.h>
8 #include <sys/wait.h>
9 #include <stdio.h>
10
11 #ifndef PORT
12 #define PORT "65014"
13 #endif
14
15 #define is_idle(conn) ((conn)->plan.io == NULL)
16
17 struct data {
18         struct io_listener *l;
19         int state;
20         struct io_conn *c1, *c2;
21         char buf[4];
22         char wbuf[32];
23 };
24
25 static void finish_ok(struct io_conn *conn, struct data *d)
26 {
27         d->state++;
28 }
29
30 static struct io_plan end(struct io_conn *conn, struct data *d)
31 {
32         d->state++;
33
34         /* last one out closes. */
35         if (conn == d->c1 && is_idle(d->c2))
36                 return io_close();
37
38         /* last one out closes. */
39         if (conn == d->c2 && is_idle(d->c1))
40                 return io_close();
41
42         return io_idle();
43 }
44
45 static struct io_plan make_duplex(struct io_conn *conn, struct data *d)
46 {
47         /* Have duplex read the rest of the buffer. */
48         d->c2 = io_duplex(conn, io_read(d->buf+1, sizeof(d->buf)-1, end, d));
49         ok1(d->c2);
50         io_set_finish(d->c2, finish_ok, d);
51
52         return io_write(d->wbuf, sizeof(d->wbuf), end, d);
53 }
54
55 static void init_conn(int fd, struct data *d)
56 {
57         ok1(d->state == 0);
58         d->state++;
59
60         io_close_listener(d->l);
61
62         memset(d->wbuf, 7, sizeof(d->wbuf));
63
64         d->c1 = io_new_conn(fd, io_read(d->buf, 1, make_duplex, d));
65         io_set_finish(d->c1, finish_ok, d);
66 }
67
68 static int make_listen_fd(const char *port, struct addrinfo **info)
69 {
70         int fd, on = 1;
71         struct addrinfo *addrinfo, hints;
72
73         memset(&hints, 0, sizeof(hints));
74         hints.ai_family = AF_UNSPEC;
75         hints.ai_socktype = SOCK_STREAM;
76         hints.ai_flags = AI_PASSIVE;
77         hints.ai_protocol = 0;
78
79         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
80                 return -1;
81
82         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
83                     addrinfo->ai_protocol);
84         if (fd < 0)
85                 return -1;
86
87         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
88         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
89                 close(fd);
90                 return -1;
91         }
92         if (listen(fd, 1) != 0) {
93                 close(fd);
94                 return -1;
95         }
96         *info = addrinfo;
97         return fd;
98 }
99
100 int main(void)
101 {
102         struct data *d = malloc(sizeof(*d));
103         struct addrinfo *addrinfo;
104         int fd, status;
105
106         /* This is how many tests you plan to run */
107         plan_tests(10);
108         d->state = 0;
109         fd = make_listen_fd(PORT, &addrinfo);
110         ok1(fd >= 0);
111         d->l = io_new_listener(fd, init_conn, d);
112         ok1(d->l);
113         fflush(stdout);
114         if (!fork()) {
115                 int i;
116                 char buf[32];
117
118                 io_close_listener(d->l);
119                 free(d);
120                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
121                             addrinfo->ai_protocol);
122                 if (fd < 0)
123                         exit(1);
124                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
125                         exit(2);
126                 signal(SIGPIPE, SIG_IGN);
127                 for (i = 0; i < strlen("hellothere"); i++) {
128                         if (write(fd, "hellothere" + i, 1) != 1)
129                                 break;
130                 }
131                 for (i = 0; i < 32; i++) {
132                         if (read(fd, buf+i, 1) != 1)
133                                 break;
134                 }
135                 close(fd);
136                 freeaddrinfo(addrinfo);
137                 exit(0);
138         }
139         freeaddrinfo(addrinfo);
140         ok1(io_loop() == NULL);
141         ok1(d->state == 5);
142         ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
143         free(d);
144
145         ok1(wait(&status));
146         ok1(WIFEXITED(status));
147         ok1(WEXITSTATUS(status) == 0);
148
149         /* This exits depending on whether all tests passed */
150         return exit_status();
151 }