]> git.ozlabs.org Git - ccan/blob - ccan/io/test/run-03-readpartial.c
ccan/io: generic init function for listening connections.
[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 struct data {
10         int state;
11         size_t bytes;
12         char buf[4];
13 };
14
15 static struct io_plan start_ok(struct io_conn *conn, struct data *d)
16 {
17         ok1(d->state == 0);
18         d->state++;
19         d->bytes = sizeof(d->buf);
20         return io_read_partial(d->buf, &d->bytes, io_close, d);
21 }
22
23 static void finish_ok(struct io_conn *conn, struct data *d)
24 {
25         ok1(d->state == 1);
26         d->state++;
27         io_break(d, NULL, NULL);
28 }
29
30 static void init_conn(int fd, struct data *d)
31 {
32         if (!io_new_conn(fd, start_ok, finish_ok, d))
33                 abort();
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 write_to_socket(const char *str, const struct addrinfo *addrinfo)
69 {
70         int fd, i;
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         signal(SIGPIPE, SIG_IGN);
79         for (i = 0; i < strlen(str); i++) {
80                 if (write(fd, str + i, 1) != 1)
81                         break;
82         }
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(22);
95         d->state = 0;
96         fd = make_listen_fd("65003", &addrinfo);
97         ok1(fd >= 0);
98         l = io_new_listener(fd, init_conn, d);
99         ok1(l);
100         fflush(stdout);
101         if (!fork()) {
102                 io_close_listener(l);
103                 write_to_socket("hellothere", addrinfo);
104                 freeaddrinfo(addrinfo);
105                 free(d);
106                 exit(0);
107         }
108         ok1(io_loop() == d);
109         ok1(d->state == 2);
110         ok1(d->bytes > 0);
111         ok1(d->bytes <= sizeof(d->buf));
112         ok1(memcmp(d->buf, "hellothere", d->bytes) == 0);
113
114         ok1(wait(&status));
115         ok1(WIFEXITED(status));
116         ok1(WEXITSTATUS(status) == 0);
117
118         fflush(stdout);
119         if (!fork()) {
120                 io_close_listener(l);
121                 write_to_socket("hi", addrinfo);
122                 freeaddrinfo(addrinfo);
123                 free(d);
124                 exit(0);
125         }
126         d->state = 0;
127         ok1(io_loop() == d);
128         ok1(d->state == 2);
129         ok1(d->bytes > 0);
130         ok1(d->bytes <= strlen("hi"));
131         ok1(memcmp(d->buf, "hi", d->bytes) == 0);
132
133         freeaddrinfo(addrinfo);
134         free(d);
135         io_close_listener(l);
136
137         ok1(wait(&status));
138         ok1(WIFEXITED(status));
139         ok1(WEXITSTATUS(status) == 0);
140
141         /* This exits depending on whether all tests passed */
142         return exit_status();
143 }