]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-07-break.c
ccan/io: initialize connection with an explicit I/O plan.
[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 struct data {
10         int state;
11         char buf[4];
12 };
13
14 static struct io_plan read_done(struct io_conn *conn, struct data *d)
15 {
16         ok1(d->state == 1);
17         d->state++;
18         return io_close(conn, NULL);
19 }
20
21 static void finish_ok(struct io_conn *conn, struct data *d)
22 {
23         ok1(d->state == 2);
24         d->state++;
25 }
26
27 static void init_conn(int fd, struct data *d)
28 {
29         ok1(d->state == 0);
30         d->state++;
31
32         if (!io_new_conn(fd,
33                          io_break(d,
34                                   io_read(d->buf, sizeof(d->buf), read_done, d)),
35                          finish_ok, d))
36                 abort();
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(13);
80         d->state = 0;
81         fd = make_listen_fd("65007", &addrinfo);
82         ok1(fd >= 0);
83         l = io_new_listener(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() == d);
108         ok1(d->state == 1);
109         io_close_listener(l);
110
111         ok1(io_loop() == NULL);
112         ok1(d->state == 3);
113         ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
114         free(d);
115
116         ok1(wait(&status));
117         ok1(WIFEXITED(status));
118         ok1(WEXITSTATUS(status) == 0);
119
120         /* This exits depending on whether all tests passed */
121         return exit_status();
122 }