]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-02-read.c
9abcd9647b6fb7295e998ec9437f6ef4543ff29c
[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 struct data {
10         int state;
11         char buf[4];
12 };
13
14 static void finish_ok(struct io_conn *conn, struct data *d)
15 {
16         ok1(d->state == 1);
17         d->state++;
18         io_break(d, io_idle());
19 }
20
21 static void init_conn(int fd, struct data *d)
22 {
23         ok1(d->state == 0);
24         d->state++;
25
26         if (!io_new_conn(fd, io_read(d->buf, sizeof(d->buf), io_close, d),
27                          finish_ok, d))
28                 abort();
29 }
30
31 static int make_listen_fd(const char *port, struct addrinfo **info)
32 {
33         int fd, on = 1;
34         struct addrinfo *addrinfo, hints;
35
36         memset(&hints, 0, sizeof(hints));
37         hints.ai_family = AF_UNSPEC;
38         hints.ai_socktype = SOCK_STREAM;
39         hints.ai_flags = AI_PASSIVE;
40         hints.ai_protocol = 0;
41
42         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
43                 return -1;
44
45         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
46                     addrinfo->ai_protocol);
47         if (fd < 0)
48                 return -1;
49
50         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
51         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
52                 close(fd);
53                 return -1;
54         }
55         if (listen(fd, 1) != 0) {
56                 close(fd);
57                 return -1;
58         }
59         *info = addrinfo;
60         return fd;
61 }
62
63 int main(void)
64 {
65         struct data *d = malloc(sizeof(*d));
66         struct addrinfo *addrinfo;
67         struct io_listener *l;
68         int fd, status;
69
70         /* This is how many tests you plan to run */
71         plan_tests(10);
72         d->state = 0;
73         fd = make_listen_fd("65002", &addrinfo);
74         ok1(fd >= 0);
75         l = io_new_listener(fd, init_conn, d);
76         ok1(l);
77         fflush(stdout);
78         if (!fork()) {
79                 int i;
80
81                 io_close_listener(l);
82                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
83                             addrinfo->ai_protocol);
84                 if (fd < 0)
85                         exit(1);
86                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
87                         exit(2);
88                 signal(SIGPIPE, SIG_IGN);
89                 for (i = 0; i < strlen("hellothere"); i++) {
90                         if (write(fd, "hellothere" + i, 1) != 1)
91                                 break;
92                 }
93                 close(fd);
94                 freeaddrinfo(addrinfo);
95                 free(d);
96                 exit(0);
97         }
98         freeaddrinfo(addrinfo);
99         ok1(io_loop() == d);
100         ok1(d->state == 2);
101         ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
102         free(d);
103         io_close_listener(l);
104
105         ok1(wait(&status));
106         ok1(WIFEXITED(status));
107         ok1(WEXITSTATUS(status) == 0);
108
109         /* This exits depending on whether all tests passed */
110         return exit_status();
111 }