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