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