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