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