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