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