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