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