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