json_out: make json_out_finished finish buffer.
[ccan] / ccan / io / test / run-17-homemade-io.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 #define PORT "65017"
10
11 struct packet {
12         int state;
13         size_t len;
14         void *contents;
15 };
16
17 static void finish_ok(struct io_conn *conn, struct packet *pkt)
18 {
19         ok1(pkt->state == 3);
20         pkt->state++;
21         io_break(pkt);
22 }
23
24 static int do_read_packet(int fd, struct io_plan_arg *arg)
25 {
26         struct packet *pkt = arg->u1.vp;
27         char *dest;
28         ssize_t ret;
29         size_t off, totlen;
30
31         /* Reading len? */
32         if (arg->u2.s < sizeof(size_t)) {
33                 ok1(pkt->state == 1);
34                 pkt->state++;
35                 dest = (char *)&pkt->len;
36                 off = arg->u2.s;
37                 totlen = sizeof(pkt->len);
38         } else {
39                 ok1(pkt->state == 2);
40                 pkt->state++;
41                 if (pkt->len == 0)
42                         return 1;
43                 if (!pkt->contents && !(pkt->contents = malloc(pkt->len)))
44                         goto fail;
45                 else {
46                         dest = pkt->contents;
47                         off = arg->u2.s - sizeof(pkt->len);
48                         totlen = pkt->len;
49                 }
50         }
51
52         ret = read(fd, dest + off, totlen - off);
53         if (ret <= 0)
54                 goto fail;
55
56         arg->u2.s += ret;
57
58         /* Finished? */
59         return arg->u2.s >= sizeof(pkt->len)
60                 && arg->u2.s == pkt->len + sizeof(pkt->len);
61
62 fail:
63         free(pkt->contents);
64         return -1;
65 }
66
67 static struct io_plan *io_read_packet(struct io_conn *conn,
68                                       struct packet *pkt,
69                                       struct io_plan *(*cb)(struct io_conn *,
70                                                             void *),
71                                       void *cb_arg)
72 {
73         struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
74
75         pkt->contents = NULL;
76         arg->u1.vp = pkt;
77         arg->u2.s = 0;
78
79         return io_set_plan(conn, IO_IN, do_read_packet, cb, cb_arg);
80 }
81
82 static struct io_plan *init_conn(struct io_conn *conn, struct packet *pkt)
83 {
84         ok1(pkt->state == 0);
85         pkt->state++;
86
87         io_set_finish(conn, finish_ok, pkt);
88         return io_read_packet(conn, pkt, io_close_cb, pkt);
89 }
90
91 static int make_listen_fd(const char *port, struct addrinfo **info)
92 {
93         int fd, on = 1;
94         struct addrinfo *addrinfo, hints;
95
96         memset(&hints, 0, sizeof(hints));
97         hints.ai_family = AF_UNSPEC;
98         hints.ai_socktype = SOCK_STREAM;
99         hints.ai_flags = AI_PASSIVE;
100         hints.ai_protocol = 0;
101
102         if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
103                 return -1;
104
105         fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
106                     addrinfo->ai_protocol);
107         if (fd < 0)
108                 return -1;
109
110         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
111         if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
112                 close(fd);
113                 return -1;
114         }
115         if (listen(fd, 1) != 0) {
116                 close(fd);
117                 return -1;
118         }
119         *info = addrinfo;
120         return fd;
121 }
122
123 int main(void)
124 {
125         struct packet *pkt = malloc(sizeof(*pkt));
126         struct addrinfo *addrinfo;
127         struct io_listener *l;
128         int fd, status;
129
130         /* This is how many tests you plan to run */
131         plan_tests(13);
132         pkt->state = 0;
133         fd = make_listen_fd(PORT, &addrinfo);
134         ok1(fd >= 0);
135         l = io_new_listener(NULL, fd, init_conn, pkt);
136         ok1(l);
137         fflush(stdout);
138         if (!fork()) {
139                 struct {
140                         size_t len;
141                         char data[8];
142                 } data;
143
144                 io_close_listener(l);
145                 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
146                             addrinfo->ai_protocol);
147                 if (fd < 0)
148                         exit(1);
149                 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
150                         exit(2);
151                 signal(SIGPIPE, SIG_IGN);
152
153                 data.len = sizeof(data.data);
154                 memcpy(data.data, "hithere!", sizeof(data.data));
155                 if (write(fd, &data, sizeof(data)) != sizeof(data))
156                         exit(3);
157
158                 close(fd);
159                 freeaddrinfo(addrinfo);
160                 free(pkt);
161                 exit(0);
162         }
163         freeaddrinfo(addrinfo);
164         ok1(io_loop(NULL, NULL) == pkt);
165         ok1(pkt->state == 4);
166         ok1(pkt->len == 8);
167         ok1(memcmp(pkt->contents, "hithere!", 8) == 0);
168         free(pkt->contents);
169         free(pkt);
170         io_close_listener(l);
171
172         ok1(wait(&status));
173         ok1(WIFEXITED(status));
174         ok1(WEXITSTATUS(status) == 0);
175
176         /* This exits depending on whether all tests passed */
177         return exit_status();
178 }