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