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