]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-45-eof.c
io: don't leave errno as a random value when we hit EOF.
[ccan] / ccan / io / test / run-45-eof.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 static size_t len;
10
11 static void finished_read(struct io_conn *conn, int *expect)
12 {
13         ok1(errno == *expect);
14 }
15
16 static struct io_plan *init_conn_read(struct io_conn *conn, int *expect)
17 {
18         io_set_finish(conn, finished_read, expect);
19         return io_read(conn, &expect, sizeof(expect), io_never, expect);
20 }
21
22 static struct io_plan *init_conn_read_partial(struct io_conn *conn, int *expect)
23 {
24         io_set_finish(conn, finished_read, expect);
25         return io_read_partial(conn, &expect, sizeof(expect), &len,
26                                io_never, expect);
27 }
28
29 int main(void)
30 {
31         int fd, expect_errno = 0;
32
33         /* This is how many tests you plan to run */
34         plan_tests(2);
35         fd = open("/dev/null", O_RDONLY);
36         io_new_conn(NULL, fd, init_conn_read, &expect_errno);
37
38         fd = open("/dev/null", O_RDONLY);
39         io_new_conn(NULL, fd, init_conn_read_partial, &expect_errno);
40
41         io_loop(NULL, NULL);
42
43         /* This exits depending on whether all tests passed */
44         return exit_status();
45 }