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