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