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