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