]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-01-start-finish.c
ccan/io: io_set_finish()
[ccan] / ccan / io / test / run-01-start-finish.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 #ifndef PORT
10 #define PORT "65001"
11 #endif
12
13 static void finish_ok(struct io_conn *conn, int *state)
14 {
15         ok1(*state == 1);
16         (*state)++;
17         io_break(state + 1, io_idle());
18 }
19
20 static void init_conn(int fd, int *state)
21 {
22         ok1(*state == 0);
23         (*state)++;
24         io_set_finish(io_new_conn(fd, io_close(NULL, NULL)), finish_ok, state);
25
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         int state = 0;
63         struct addrinfo *addrinfo;
64         struct io_listener *l;
65         int fd;
66
67         /* This is how many tests you plan to run */
68         plan_tests(9);
69         fd = make_listen_fd(PORT, &addrinfo);
70         ok1(fd >= 0);
71         l = io_new_listener(fd, init_conn, &state);
72         ok1(l);
73         fflush(stdout);
74         if (!fork()) {
75                 io_close_listener(l);
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                 close(fd);
83                 freeaddrinfo(addrinfo);
84                 exit(0);
85         }
86         freeaddrinfo(addrinfo);
87         ok1(io_loop() == &state + 1);
88         ok1(state == 2);
89         io_close_listener(l);
90         ok1(wait(&state));
91         ok1(WIFEXITED(state));
92         ok1(WEXITSTATUS(state) == 0);
93
94         /* This exits depending on whether all tests passed */
95         return exit_status();
96 }