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