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