]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-43-io_plan_in_started.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / io / test / run-43-io_plan_in_started.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
7 static struct io_conn *in_conn;
8
9 static struct io_plan *in_conn_done(struct io_conn *conn, void *unused)
10 {
11         ok1(!io_plan_in_started(conn));
12         return io_close(conn);
13 }
14
15 static struct io_plan *init_in_conn(struct io_conn *conn, char *buf)
16 {
17         ok1(!io_plan_in_started(conn));
18         return io_read(conn, buf, 2, in_conn_done, NULL);
19 }
20
21 static int do_nothing(int fd, struct io_plan_arg *arg)
22 {
23         return 1;
24 }
25
26 static struct io_plan *dummy_write(struct io_conn *conn,
27                                    struct io_plan *(*next)
28                                    (struct io_conn *, void *),
29                                    void *next_arg)
30 {
31         io_plan_arg(conn, IO_OUT);
32         return io_set_plan(conn, IO_OUT, do_nothing, next, next_arg);
33 }
34
35 static struct io_plan *out_post_write(struct io_conn *conn, void *unused)
36 {
37         /* It might not have started yet: try again. */
38         if (!io_plan_in_started(in_conn))
39                 return dummy_write(conn, out_post_write, NULL);
40         ok1(io_plan_in_started(in_conn));
41
42         /* Final write, then close */
43         return io_write(conn, "2", 1, io_close_cb, NULL);
44 }
45
46 static struct io_plan *init_out_conn(struct io_conn *conn, void *unused)
47 {
48         ok1(!io_plan_in_started(in_conn));
49         return io_write(conn, "1", 1, out_post_write, NULL);
50 }
51
52 int main(void)
53 {
54         int fds[2];
55         const tal_t *ctx = tal(NULL, char);
56         char *buf = tal_arr(ctx, char, 3);
57
58         /* This is how many tests you plan to run */
59         plan_tests(5);
60
61         if (pipe(fds) != 0)
62                 abort();
63
64         buf[2] = '\0';
65
66         in_conn = io_new_conn(ctx, fds[0], init_in_conn, buf);
67         io_new_conn(ctx, fds[1], init_out_conn, NULL);
68
69         io_loop(NULL, NULL);
70         ok1(strcmp(buf, "12") == 0);
71         tal_free(ctx);
72
73         /* This exits depending on whether all tests passed */
74         return exit_status();
75 }