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