Debian Codegraph @

test/unit/ext/test_uv.c

raw
1 #include <raft.h>
2 #include <uv.h>
3
4 #include <unistd.h>
5
6 #include "../../../src/lib/transport.h"
7 #include "../../lib/endpoint.h"
8 #include "../../lib/runner.h"
9 #include "../../lib/uv.h"
10
11 TEST_MODULE(ext_uv);
12
13 /******************************************************************************
14 *
15 * Helpers
16 *
17 ******************************************************************************/
18
19 struct fixture
20 {
21 struct uv_loop_s loop;
22 struct uv_stream_s *listener;
23 struct test_endpoint endpoint;
24 int client;
25 union {
26 uv_tcp_t tcp;
27 uv_pipe_t pipe;
28 uv_stream_t stream;
29 };
30 };
31
32 /* Return a buffer of size TEST_SOCKET_MIN_BUF_SIZE */
33 static uv_buf_t *buf_malloc(void)
34 {
35 uv_buf_t *buf = munit_malloc(sizeof *buf);
36 buf->base = munit_malloc(TEST_SOCKET_MIN_BUF_SIZE);
37 buf->len = TEST_SOCKET_MIN_BUF_SIZE;
38 return buf;
39 }
40
41 /* Free the buffer returned by buf_malloc() */
42 static void buf_free(uv_buf_t *buf)
43 {
44 free(buf->base);
45 free(buf);
46 }
47
48 /******************************************************************************
49 *
50 * Parameters
51 *
52 ******************************************************************************/
53
54 /* Run the tests using both TCP and Unix sockets. */
55 static MunitParameterEnum endpointParams[] = {
56 {TEST_ENDPOINT_FAMILY, test_endpoint_family_values},
57 {NULL, NULL},
58 };
59
60 /******************************************************************************
61 *
62 * Setup and tear down
63 *
64 ******************************************************************************/
65
66 static void listenCb(uv_stream_t *listener, int status)
67 {
68 struct fixture *f = listener->data;
69 int rv;
70 munit_assert_int(status, ==, 0);
71
72 switch (listener->type) {
73 case UV_TCP:
74 rv = uv_tcp_init(&f->loop, &f->tcp);
75 munit_assert_int(rv, ==, 0);
76 break;
77 case UV_NAMED_PIPE:
78 rv = uv_pipe_init(&f->loop, &f->pipe, 0);
79 munit_assert_int(rv, ==, 0);
80 break;
81 default:
82 munit_assert(0);
83 }
84
85 rv = uv_accept(listener, &f->stream);
86 munit_assert_int(rv, ==, 0);
87 }
88
89 static void *setup(const MunitParameter params[], void *user_data)
90 {
91 struct fixture *f = munit_malloc(sizeof *f);
92 int rv;
93 (void)user_data;
94
95 test_uv_setup(params, &f->loop);
96 test_endpoint_setup(&f->endpoint, params);
97
98 rv = transport__stream(&f->loop, f->endpoint.fd, &f->listener);
99 munit_assert_int(rv, ==, 0);
100
101 f->listener->data = f;
102
103 rv = uv_listen(f->listener, 128, listenCb);
104 munit_assert_int(rv, ==, 0);
105
106 f->client = test_endpoint_connect(&f->endpoint);
107
108 test_uv_run(&f->loop, 1);
109
110 return f;
111 }
112
113 static void tear_down(void *data)
114 {
115 struct fixture *f = data;
116 int rv;
117 rv = close(f->client);
118 munit_assert_int(rv, ==, 0);
119 uv_close((struct uv_handle_s *)f->listener, (uv_close_cb)raft_free);
120 test_endpoint_tear_down(&f->endpoint);
121 uv_close((uv_handle_t *)(&f->stream), NULL);
122 test_uv_stop(&f->loop);
123 test_uv_tear_down(&f->loop);
124 free(f);
125 }
126
127 /******************************************************************************
128 *
129 * uv_write
130 *
131 ******************************************************************************/
132
133 TEST_SUITE(write);
134 TEST_SETUP(write, setup);
135 TEST_TEAR_DOWN(write, tear_down);
136
137 /* Writing an amount of data below the buffer size makes that data immediately
138 * available for reading. */
139 TEST_CASE(write, sync, endpointParams)
140 {
141 struct fixture *f = data;
142 uv_write_t req;
143 uv_buf_t *buf1 = buf_malloc();
144 uv_buf_t *buf2 = buf_malloc();
145 int rv;
146
147 (void)params;
148
149 rv = uv_write(&req, &f->stream, buf1, 1, NULL);
150 munit_assert_int(rv, ==, 0);
151
152 rv = read(f->client, buf2->base, buf2->len);
153 munit_assert_int(rv, ==, buf2->len);
154
155 test_uv_run(&f->loop, 1);
156
157 buf_free(buf1);
158 buf_free(buf2);
159
160 return MUNIT_OK;
161 }
162
163 /******************************************************************************
164 *
165 * uv_read
166 *
167 ******************************************************************************/
168
169 TEST_SUITE(read);
170 TEST_SETUP(read, setup);
171 TEST_TEAR_DOWN(read, tear_down);
172
173 static void test_read_sync__alloc_cb(uv_handle_t *stream,
174 size_t _,
175 uv_buf_t *buf)
176 {
177 (void)stream;
178 (void)_;
179
180 buf->len = TEST_SOCKET_MIN_BUF_SIZE;
181 buf->base = munit_malloc(TEST_SOCKET_MIN_BUF_SIZE);
182 }
183
184 static void test_read_sync__read_cb(uv_stream_t *stream,
185 ssize_t nread,
186 const uv_buf_t *buf)
187 {
188 bool *read_cb_called;
189
190 /* Apprently there's an empty read before the actual one. */
191 if (nread == 0) {
192 free(buf->base);
193 return;
194 }
195
196 munit_assert_int(nread, ==, TEST_SOCKET_MIN_BUF_SIZE);
197 munit_assert_int(buf->len, ==, TEST_SOCKET_MIN_BUF_SIZE);
198
199 read_cb_called = stream->data;
200
201 *read_cb_called = true;
202
203 free(buf->base);
204 }
205
206 /* Reading an amount of data below the buffer happens synchronously. */
207 TEST_CASE(read, sync, endpointParams)
208 {
209 struct fixture *f = data;
210 uv_buf_t *buf = buf_malloc();
211 int rv;
212 bool read_cb_called;
213
214 (void)params;
215
216 f->stream.data = &read_cb_called;
217
218 rv = uv_read_start(&f->stream, test_read_sync__alloc_cb,
219 test_read_sync__read_cb);
220
221 rv = write(f->client, buf->base, buf->len);
222 munit_assert_int(rv, ==, buf->len);
223
224 test_uv_run(&f->loop, 1);
225
226 munit_assert_true(read_cb_called);
227
228 buf_free(buf);
229
230 return MUNIT_OK;
231 }