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