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