]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-19-always.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / io / test / run-19-always.c
1 #include <ccan/io/io.h>
2 /* Include the C files directly. */
3 #include <ccan/io/poll.c>
4 #include <ccan/io/io.c>
5 #include <ccan/tap/tap.h>
6 #include <sys/wait.h>
7 #include <stdio.h>
8
9 #define PORT "65019"
10
11 struct data {
12         int state;
13         size_t bytes;
14         char *buf;
15 };
16
17 static void finish_ok(struct io_conn *conn, struct data *d)
18 {
19         ok1(d->state == 1);
20         d->state++;
21         io_break(d);
22 }
23
24 static struct io_plan *write_buf(struct io_conn *conn, struct data *d)
25 {
26         return io_write(conn, d->buf, d->bytes, io_close_cb, d);
27 }
28
29 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
30 {
31         ok1(d->state == 0);
32         d->state++;
33         io_set_finish(conn, finish_ok, d);
34
35         /* Empty read should run immediately... */
36         return io_read(conn, NULL, 0, write_buf, d);
37 }
38
39 static int make_listen_fd(const char *port, struct addrinfo **info)
40 {
41         int fd, on = 1;
42         struct addrinfo *addrinfo, hints;
43
44         memset(&hints, 0, sizeof(hints));
45         hints.ai_family = AF_UNSPEC;
46         hints.ai_socktype = SOCK_STREAM;
47         hints.ai_flags = AI_PASSIVE;
48         hints.ai_protocol = 0;
49
50         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
51                 return -1;
52
53         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
54                     addrinfo->ai_protocol);
55         if (fd < 0)
56                 return -1;
57
58         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
59         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
60                 close(fd);
61                 return -1;
62         }
63         if (listen(fd, 1) != 0) {
64                 close(fd);
65                 return -1;
66         }
67         *info = addrinfo;
68         return fd;
69 }
70
71 static void read_from_socket(size_t bytes, const struct addrinfo *addrinfo)
72 {
73         int fd, done, r;
74         char buf[100];
75
76         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
77                     addrinfo->ai_protocol);
78         if (fd < 0)
79                 exit(1);
80         if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
81                 exit(2);
82
83         for (done = 0; done < bytes; done += r) {
84                 r = read(fd, buf, sizeof(buf));
85                 if (r < 0)
86                         exit(3);
87                 done += r;
88         }
89         close(fd);
90 }
91
92 int main(void)
93 {
94         struct data *d = malloc(sizeof(*d));
95         struct addrinfo *addrinfo;
96         struct io_listener *l;
97         int fd, status;
98
99         /* This is how many tests you plan to run */
100         plan_tests(9);
101         d->state = 0;
102         d->bytes = 1024*1024;
103         d->buf = malloc(d->bytes);
104         memset(d->buf, 'a', d->bytes);
105         fd = make_listen_fd(PORT, &addrinfo);
106         ok1(fd >= 0);
107         l = io_new_listener(NULL, fd, init_conn, d);
108         ok1(l);
109         fflush(stdout);
110         if (!fork()) {
111                 io_close_listener(l);
112                 read_from_socket(d->bytes, addrinfo);
113                 freeaddrinfo(addrinfo);
114                 free(d->buf);
115                 free(d);
116                 exit(0);
117         }
118         ok1(io_loop(NULL, NULL) == d);
119         ok1(d->state == 2);
120
121         ok1(wait(&status));
122         ok1(WIFEXITED(status));
123         ok1(WEXITSTATUS(status) == 0);
124
125         freeaddrinfo(addrinfo);
126         free(d->buf);
127         free(d);
128         io_close_listener(l);
129
130         /* This exits depending on whether all tests passed */
131         return exit_status();
132 }