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