]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-07-break.c
io: fix maybe-uninitialized warning in test (-O2)
[ccan] / ccan / io / test / run-07-break.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 "64007"
11 #else
12 #define PORT "65007"
13 #endif
14
15 struct data {
16         int state;
17         char buf[4];
18 };
19
20 static struct io_plan *read_done(struct io_conn *conn, struct data *d)
21 {
22         ok1(d->state == 1);
23         d->state++;
24         return io_close(conn);
25 }
26
27 static void finish_ok(struct io_conn *conn, struct data *d)
28 {
29         ok1(d->state == 2);
30         d->state++;
31 }
32
33 static struct io_plan *init_conn(struct io_conn *conn, struct data *d)
34 {
35 #ifdef DEBUG_CONN
36         io_set_debug(conn, true);
37 #endif
38         ok1(d->state == 0);
39         d->state++;
40
41         io_set_finish(conn, finish_ok, d);
42
43         io_break(d);
44         return io_read(conn, d->buf, sizeof(d->buf), read_done, d);
45 }
46
47 static int make_listen_fd(const char *port, struct addrinfo **info)
48 {
49         int fd, on = 1;
50         struct addrinfo *addrinfo, hints;
51
52         memset(&hints, 0, sizeof(hints));
53         hints.ai_family = AF_UNSPEC;
54         hints.ai_socktype = SOCK_STREAM;
55         hints.ai_flags = AI_PASSIVE;
56         hints.ai_protocol = 0;
57
58         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
59                 return -1;
60
61         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
62                     addrinfo->ai_protocol);
63         if (fd < 0)
64                 return -1;
65
66         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
67         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
68                 close(fd);
69                 return -1;
70         }
71         if (listen(fd, 1) != 0) {
72                 close(fd);
73                 return -1;
74         }
75         *info = addrinfo;
76         return fd;
77 }
78
79 int main(void)
80 {
81         struct data *d = malloc(sizeof(*d));
82         struct addrinfo *addrinfo;
83         struct io_listener *l;
84         int fd, status;
85
86         /* This is how many tests you plan to run */
87         plan_tests(13);
88         d->state = 0;
89         fd = make_listen_fd(PORT, &addrinfo);
90         ok1(fd >= 0);
91         l = io_new_listener(NULL, fd, init_conn, d);
92         ok1(l);
93         fflush(stdout);
94         if (!fork()) {
95                 int i;
96
97                 io_close_listener(l);
98                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
99                             addrinfo->ai_protocol);
100                 if (fd < 0)
101                         exit(1);
102                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
103                         exit(2);
104                 signal(SIGPIPE, SIG_IGN);
105                 for (i = 0; i < strlen("hellothere"); i++) {
106                         if (write(fd, "hellothere" + i, 1) != 1)
107                                 break;
108                 }
109                 close(fd);
110                 freeaddrinfo(addrinfo);
111                 free(d);
112                 exit(0);
113         }
114         freeaddrinfo(addrinfo);
115         ok1(io_loop(NULL, NULL) == d);
116         ok1(d->state == 1);
117         io_close_listener(l);
118
119         ok1(io_loop(NULL, NULL) == NULL);
120         ok1(d->state == 3);
121         ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
122         free(d);
123
124         ok1(wait(&status));
125         ok1(WIFEXITED(status));
126         ok1(WEXITSTATUS(status) == 0);
127
128         /* This exits depending on whether all tests passed */
129         return exit_status();
130 }