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