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