]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-01-start-finish.c
ccan/io: initialize connection with an explicit I/O plan.
[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 static void finish_ok(struct io_conn *conn, int *state)
10 {
11         ok1(*state == 1);
12         (*state)++;
13         io_break(state + 1, io_idle());
14 }
15
16 static void init_conn(int fd, int *state)
17 {
18         ok1(*state == 0);
19         (*state)++;
20         if (!io_new_conn(fd, io_close(NULL, NULL), finish_ok, state))
21                 abort();
22 }
23
24 static int make_listen_fd(const char *port, struct addrinfo **info)
25 {
26         int fd, on = 1;
27         struct addrinfo *addrinfo, hints;
28
29         memset(&hints, 0, sizeof(hints));
30         hints.ai_family = AF_UNSPEC;
31         hints.ai_socktype = SOCK_STREAM;
32         hints.ai_flags = AI_PASSIVE;
33         hints.ai_protocol = 0;
34
35         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
36                 return -1;
37
38         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
39                     addrinfo->ai_protocol);
40         if (fd < 0)
41                 return -1;
42
43         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
44         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
45                 close(fd);
46                 return -1;
47         }
48         if (listen(fd, 1) != 0) {
49                 close(fd);
50                 return -1;
51         }
52         *info = addrinfo;
53         return fd;
54 }
55
56 int main(void)
57 {
58         int state = 0;
59         struct addrinfo *addrinfo;
60         struct io_listener *l;
61         int fd;
62
63         /* This is how many tests you plan to run */
64         plan_tests(9);
65         fd = make_listen_fd("65001", &addrinfo);
66         ok1(fd >= 0);
67         l = io_new_listener(fd, init_conn, &state);
68         ok1(l);
69         fflush(stdout);
70         if (!fork()) {
71                 io_close_listener(l);
72                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
73                             addrinfo->ai_protocol);
74                 if (fd < 0)
75                         exit(1);
76                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
77                         exit(2);
78                 close(fd);
79                 freeaddrinfo(addrinfo);
80                 exit(0);
81         }
82         freeaddrinfo(addrinfo);
83         ok1(io_loop() == &state + 1);
84         ok1(state == 2);
85         io_close_listener(l);
86         ok1(wait(&state));
87         ok1(WIFEXITED(state));
88         ok1(WEXITSTATUS(state) == 0);
89
90         /* This exits depending on whether all tests passed */
91         return exit_status();
92 }