1 /*
2 * SysDB - t/unit/frontend/connection_test.c
3 * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "frontend/connection.h"
33 #include "frontend/connection-private.h"
34 #include "utils/proto.h"
35 #include "libsysdb_test.h"
37 #include "utils/strbuf.h"
39 #include <check.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
45 #include <pthread.h>
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <sys/un.h>
51 /*
52 * private helper functions
53 */
55 static void
56 mock_conn_destroy(sdb_conn_t *conn)
57 {
58 if (SDB_OBJ(conn)->name)
59 free(SDB_OBJ(conn)->name);
60 sdb_strbuf_destroy(conn->buf);
61 sdb_strbuf_destroy(conn->errbuf);
62 if (conn->fd >= 0)
63 close(conn->fd);
64 if (conn->username)
65 free(conn->username);
66 free(conn);
67 } /* mock_conn_destroy */
69 static sdb_conn_t *
70 mock_conn_create(void)
71 {
72 sdb_conn_t *conn;
74 char tmp_file[] = "connection_test_socket.XXXXXX";
76 conn = calloc(1, sizeof(*conn));
77 if (! conn) {
78 fail("INTERNAL ERROR: failed to allocate connection object");
79 return NULL;
80 }
82 SDB_OBJ(conn)->name = strdup("mock_connection");
83 SDB_OBJ(conn)->ref_cnt = 1;
85 conn->buf = sdb_strbuf_create(0);
86 conn->errbuf = sdb_strbuf_create(0);
87 if ((! conn->buf) || (! conn->errbuf)) {
88 mock_conn_destroy(conn);
89 fail("INTERNAL ERROR: failed to allocate connection object");
90 return NULL;
91 }
93 conn->fd = mkstemp(tmp_file);
94 if (conn->fd < 0) {
95 mock_conn_destroy(conn);
96 fail("INTERNAL ERROR: failed to allocate connection object");
97 return NULL;
98 }
100 unlink(tmp_file);
102 conn->cmd = CONNECTION_IDLE;
103 conn->cmd_len = 0;
104 return conn;
105 } /* mock_conn_create */
107 static void
108 mock_conn_rewind(sdb_conn_t *conn)
109 {
110 lseek(conn->fd, 0, SEEK_SET);
111 } /* mock_conn_rewind */
113 static void
114 mock_conn_truncate(sdb_conn_t *conn)
115 {
116 int status;
117 lseek(conn->fd, 0, SEEK_SET);
118 status = ftruncate(conn->fd, 0);
119 fail_unless(status == 0,
120 "INTERNAL ERROR: ftruncate(%d, 0) = %d; expected: 0",
121 conn->fd, status);
122 } /* mock_conn_truncate */
124 static int
125 mock_unixsock_listener(char *socket_path)
126 {
127 struct sockaddr_un sa;
128 int fd, status;
130 fd = socket(AF_UNIX, SOCK_STREAM, 0);
131 fail_unless(fd >= 0,
132 "INTERNAL ERROR: socket() = %d; expected: >=0", fd);
134 memset(&sa, 0, sizeof(sa));
135 sa.sun_family = AF_UNIX;
136 strncpy(sa.sun_path, socket_path, sizeof(sa.sun_path));
138 status = bind(fd, (struct sockaddr *)&sa, sizeof(sa));
139 fail_unless(status == 0,
140 "INTERNAL ERROR: bind() = %d; expected: 0", status);
141 status = listen(fd, 32);
142 fail_unless(status == 0,
143 "INTERNAL ERROR: listen() = %d; expected: 0", status);
145 return fd;
146 } /* mock_unixsock */
148 static void *
149 mock_client(void *arg)
150 {
151 char *socket_path = arg;
153 struct sockaddr_un sa;
154 int fd, check;
156 fd = socket(AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
157 fail_unless(fd >= 0,
158 "INTERNAL ERROR: socket() = %d; expected: >= 0", fd);
160 memset(&sa, 0, sizeof(sa));
161 sa.sun_family = AF_UNIX;
162 strncpy(sa.sun_path, socket_path, sizeof(sa.sun_path));
164 check = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
165 fail_unless(check == 0,
166 "INTERNAL ERROR: connect() = %d; expected: 0", check);
168 close(fd);
169 return NULL;
170 } /* mock_client */
172 static void
173 connection_startup(sdb_conn_t *conn)
174 {
175 ssize_t check, expected;
177 expected = 2 * sizeof(uint32_t) + strlen("fakeuser");
178 check = sdb_connection_send(conn, CONNECTION_STARTUP,
179 (uint32_t)strlen("fakeuser"), "fakeuser");
180 fail_unless(check == expected,
181 "sdb_connection_send(STARTUP, fakeuser) = %zi; expected: %zi",
182 check, expected);
184 mock_conn_rewind(conn);
185 check = sdb_connection_read(conn);
186 fail_unless(check == expected,
187 "On startup: sdb_connection_read() = %zi; expected: %zi",
188 check, expected);
190 fail_unless(sdb_strbuf_len(conn->errbuf) == 0,
191 "sdb_connection_read() left %zu bytes in the error "
192 "buffer; expected: 0", sdb_strbuf_len(conn->errbuf));
194 mock_conn_truncate(conn);
195 } /* connection_startup */
197 /*
198 * tests
199 */
201 START_TEST(test_conn_accept)
202 {
203 char socket_path[] = "connection_test_socket.XXXXXX";
204 int fd, check;
206 sdb_conn_t *conn;
208 pthread_t thr;
210 conn = sdb_connection_accept(-1);
211 fail_unless(conn == NULL,
212 "sdb_connection_accept(-1) = %p; expected: NULL", conn);
214 fd = mkstemp(socket_path);
215 unlink(socket_path);
216 close(fd);
218 fd = mock_unixsock_listener(socket_path);
219 check = pthread_create(&thr, /* attr = */ NULL, mock_client, socket_path);
220 fail_unless(check == 0,
221 "INTERNAL ERROR: pthread_create() = %i; expected: 0", check);
223 conn = sdb_connection_accept(fd);
224 fail_unless(conn != NULL,
225 "sdb_connection_accept(%d) = %p; expected: <conn>", fd, conn);
227 unlink(socket_path);
228 sdb_connection_close(conn);
229 pthread_join(thr, NULL);
230 }
231 END_TEST
233 /* test connection setup and very basic commands */
234 START_TEST(test_conn_setup)
235 {
236 sdb_conn_t *conn = mock_conn_create();
238 struct {
239 uint32_t code;
240 const char *msg;
241 const char *err;
242 } golden_data[] = {
243 /* code == UINT32_MAX => no data will be sent */
244 { UINT32_MAX, NULL, NULL },
245 { CONNECTION_IDLE, "fakedata", "Authentication required" },
246 { CONNECTION_PING, NULL, "Authentication required" },
247 { CONNECTION_STARTUP, "fakeuser", NULL },
248 { CONNECTION_PING, NULL, NULL },
249 { CONNECTION_IDLE, NULL, "Invalid command 0" },
250 { CONNECTION_PING, "fakedata", NULL },
251 { CONNECTION_IDLE, NULL, "Invalid command 0" },
252 };
254 size_t i;
256 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
257 ssize_t check, expected = 0;
259 mock_conn_truncate(conn);
261 if (golden_data[i].code != UINT32_MAX) {
262 expected = 2 * sizeof(uint32_t)
263 + (golden_data[i].msg ? strlen(golden_data[i].msg) : 0);
265 check = sdb_connection_send(conn, golden_data[i].code,
266 (uint32_t)(golden_data[i].msg
267 ? strlen(golden_data[i].msg) : 0),
268 golden_data[i].msg);
269 fail_unless(check == expected,
270 "sdb_connection_send(%d, %s) = %zi; expected: %zi",
271 golden_data[i].code,
272 golden_data[i].msg ? golden_data[i].msg : "<null>",
273 check, expected);
274 }
276 mock_conn_rewind(conn);
277 check = sdb_connection_read(conn);
278 fail_unless(check == expected,
279 "sdb_connection_read() = %zi; expected: %zi",
280 check, expected);
282 fail_unless(sdb_strbuf_len(conn->buf) == 0,
283 "sdb_connection_read() left %zu bytes in the buffer; "
284 "expected: 0", sdb_strbuf_len(conn->buf));
286 if (golden_data[i].err) {
287 const char *err = sdb_strbuf_string(conn->errbuf);
288 fail_unless(strcmp(err, golden_data[i].err) == 0,
289 "sdb_connection_read(): got error '%s'; "
290 "expected: '%s'", err, golden_data[i].err);
291 }
292 else
293 fail_unless(sdb_strbuf_len(conn->errbuf) == 0,
294 "sdb_connection_read() left %zu bytes in the error "
295 "buffer; expected: 0", sdb_strbuf_len(conn->errbuf));
296 }
298 mock_conn_destroy(conn);
299 }
300 END_TEST
302 /* test simple I/O on open connections */
303 START_TEST(test_conn_io)
304 {
305 sdb_conn_t *conn = mock_conn_create();
307 struct {
308 uint32_t code;
309 uint32_t msg_len;
310 const char *msg;
311 size_t buf_len; /* number of bytes we expect in conn->buf */
312 const char *err;
313 } golden_data[] = {
314 /* code == UINT32_MAX => this is a follow-up package */
315 { CONNECTION_PING, 20, "9876543210", 0, "Authentication required" },
316 { UINT32_MAX, -1, "9876543210", 0, "Authentication required" },
317 { CONNECTION_PING, 10, "9876543210", 0, "Authentication required" },
318 { CONNECTION_IDLE, 10, "9876543210", 0, "Authentication required" },
319 { CONNECTION_IDLE, 20, "9876543210", 0, "Authentication required" },
320 { UINT32_MAX, -1, "9876543210", 0, "Authentication required" },
321 { CONNECTION_STARTUP, -1, NULL, 0, NULL },
322 { CONNECTION_PING, 20, "9876543210", 10, NULL },
323 { UINT32_MAX, -1, "9876543210", 0, NULL },
324 { CONNECTION_IDLE, 20, "9876543210", 0, "Invalid command 0" },
325 { UINT32_MAX, -1, "9876543210", 0, "Invalid command 0" },
326 { CONNECTION_IDLE, 20, "9876543210", 0, "Invalid command 0" },
327 { UINT32_MAX, -1, "9876543210", 0, "Invalid command 0" },
328 { CONNECTION_PING, 10, "9876543210", 0, NULL },
329 { CONNECTION_PING, 20, "9876543210", 10, NULL },
330 { UINT32_MAX, -1, "9876543210", 0, NULL },
331 };
333 size_t i;
335 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
336 size_t msg_len = golden_data[i].msg ? strlen(golden_data[i].msg) : 0;
337 char buffer[2 * sizeof(uint32_t) + msg_len];
338 size_t offset = 0;
340 ssize_t check;
342 mock_conn_truncate(conn);
344 if (golden_data[i].code == CONNECTION_STARTUP) {
345 connection_startup(conn);
346 continue;
347 }
349 if (golden_data[i].code != UINT32_MAX) {
350 uint32_t tmp;
352 tmp = htonl(golden_data[i].code);
353 memcpy(buffer, &tmp, sizeof(tmp));
354 tmp = htonl(golden_data[i].msg_len);
355 memcpy(buffer + sizeof(tmp), &tmp, sizeof(tmp));
357 msg_len += 2 * sizeof(uint32_t);
358 offset += 2 * sizeof(uint32_t);
359 }
361 memcpy(buffer + offset, golden_data[i].msg,
362 strlen(golden_data[i].msg));
364 check = sdb_proto_send(conn->fd, msg_len, buffer);
365 fail_unless(check == (ssize_t)msg_len,
366 "sdb_proto_send(%s) = %zi; expected: %zu",
367 check, msg_len);
369 mock_conn_rewind(conn);
370 check = sdb_connection_read(conn);
371 fail_unless(check == (ssize_t)msg_len,
372 "sdb_connection_read() = %zi; expected: %zu",
373 check, msg_len);
375 if (golden_data[i].buf_len) {
376 /* partial commands need to be stored in the object */
377 fail_unless(conn->cmd == golden_data[i].code,
378 "sdb_connection_read() set partial command "
379 "to %u; expected: %u", conn->cmd, golden_data[i].code);
380 fail_unless(conn->cmd_len > golden_data[i].buf_len,
381 "sdb_connection_read() set partial command length "
382 "to %u; expected: > %u", conn->cmd_len,
383 golden_data[i].buf_len);
384 }
385 else {
386 fail_unless(conn->cmd == CONNECTION_IDLE,
387 "sdb_connection_read() did not reset command; "
388 "got %u; expected: %u", conn->cmd, CONNECTION_IDLE);
389 fail_unless(conn->cmd_len == 0,
390 "sdb_connection_read() did not reset command length; "
391 "got %u; expected: 0", conn->cmd_len);
392 }
394 fail_unless(sdb_strbuf_len(conn->buf) == golden_data[i].buf_len,
395 "sdb_connection_read() left %zu bytes in the buffer; "
396 "expected: %zu", sdb_strbuf_len(conn->buf),
397 golden_data[i].buf_len);
399 if (golden_data[i].err) {
400 const char *err = sdb_strbuf_string(conn->errbuf);
401 fail_unless(strcmp(err, golden_data[i].err) == 0,
402 "sdb_connection_read(): got error '%s'; "
403 "expected: '%s'", err, golden_data[i].err);
404 }
405 else
406 fail_unless(sdb_strbuf_len(conn->errbuf) == 0,
407 "sdb_connection_read() left %zu bytes in the error "
408 "buffer; expected: 0", sdb_strbuf_len(conn->errbuf));
409 }
411 mock_conn_destroy(conn);
412 }
413 END_TEST
415 Suite *
416 fe_conn_suite(void)
417 {
418 Suite *s = suite_create("frontend::connection");
419 TCase *tc;
421 tc = tcase_create("core");
422 tcase_add_test(tc, test_conn_accept);
423 tcase_add_test(tc, test_conn_setup);
424 tcase_add_test(tc, test_conn_io);
425 suite_add_tcase(s, tc);
427 return s;
428 } /* fe_conn_suite */
430 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */