]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-02-read.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / io / test / run-02-read.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 "65002"
10
11 struct data {
12         int state;
13         char buf[4];
14 };
15
16 static void finish_ok(struct io_conn *conn, struct data *d)
17 {
18         ok1(d->state == 1);
19         d->state++;
20         io_break(d);
21 }
22
23 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
24 {
25         ok1(d->state == 0);
26         d->state++;
27
28         io_set_finish(conn, finish_ok, d);
29         return io_read(conn, d->buf, sizeof(d->buf), 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 int main(void)
65 {
66         struct data *d = malloc(sizeof(*d));
67         struct addrinfo *addrinfo = NULL;
68         struct io_listener *l;
69         int fd, status;
70
71         /* This is how many tests you plan to run */
72         plan_tests(10);
73         d->state = 0;
74         fd = make_listen_fd(PORT, &addrinfo);
75         ok1(fd >= 0);
76         l = io_new_listener(NULL, fd, init_conn, d);
77         ok1(l);
78         fflush(stdout);
79         if (!fork()) {
80                 int i;
81
82                 io_close_listener(l);
83                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
84                             addrinfo->ai_protocol);
85                 if (fd < 0)
86                         exit(1);
87                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
88                         exit(2);
89                 signal(SIGPIPE, SIG_IGN);
90                 for (i = 0; i < strlen("hellothere"); i++) {
91                         if (write(fd, "hellothere" + i, 1) != 1)
92                                 break;
93                 }
94                 close(fd);
95                 freeaddrinfo(addrinfo);
96                 free(d);
97                 exit(0);
98         }
99         freeaddrinfo(addrinfo);
100         ok1(io_loop(NULL, NULL) == d);
101         ok1(d->state == 2);
102         ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
103         free(d);
104         io_close_listener(l);
105
106         ok1(wait(&status));
107         ok1(WIFEXITED(status));
108         ok1(WEXITSTATUS(status) == 0);
109
110         /* This exits depending on whether all tests passed */
111         return exit_status();
112 }