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 "utils/os.h"
36 #include "libsysdb_test.h"
38 #include "utils/strbuf.h"
40 #include <assert.h>
41 #include <check.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
47 #include <pthread.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <sys/un.h>
53 static char username[1024];
55 /*
56 * private helper functions
57 */
59 static void
60 mock_conn_destroy(sdb_conn_t *conn)
61 {
62 if (SDB_OBJ(conn)->name)
63 free(SDB_OBJ(conn)->name);
64 sdb_strbuf_destroy(conn->buf);
65 sdb_strbuf_destroy(conn->errbuf);
66 if (conn->fd >= 0)
67 close(conn->fd);
68 if (conn->username)
69 free(conn->username);
70 free(conn);
71 } /* mock_conn_destroy */
73 static sdb_conn_t *
74 mock_conn_create(void)
75 {
76 sdb_conn_t *conn;
78 char tmp_file[] = "connection_test_socket.XXXXXX";
80 conn = calloc(1, sizeof(*conn));
81 if (! conn) {
82 fail("INTERNAL ERROR: failed to allocate connection object");
83 return NULL;
84 }
86 SDB_OBJ(conn)->name = strdup("mock_connection");
87 SDB_OBJ(conn)->ref_cnt = 1;
89 conn->buf = sdb_strbuf_create(0);
90 conn->errbuf = sdb_strbuf_create(0);
91 if ((! conn->buf) || (! conn->errbuf)) {
92 mock_conn_destroy(conn);
93 fail("INTERNAL ERROR: failed to allocate connection object");
94 return NULL;
95 }
97 conn->fd = mkstemp(tmp_file);
98 if (conn->fd < 0) {
99 mock_conn_destroy(conn);
100 fail("INTERNAL ERROR: failed to allocate connection object");
101 return NULL;
102 }
104 unlink(tmp_file);
106 conn->username = strdup(username);
107 assert(conn->username);
109 conn->cmd = SDB_CONNECTION_IDLE;
110 conn->cmd_len = 0;
111 return conn;
112 } /* mock_conn_create */
114 static void
115 mock_conn_rewind(sdb_conn_t *conn)
116 {
117 lseek(conn->fd, 0, SEEK_SET);
118 } /* mock_conn_rewind */
120 static void
121 mock_conn_truncate(sdb_conn_t *conn)
122 {
123 int status;
124 lseek(conn->fd, 0, SEEK_SET);
125 status = ftruncate(conn->fd, 0);
126 fail_unless(status == 0,
127 "INTERNAL ERROR: ftruncate(%d, 0) = %d; expected: 0",
128 conn->fd, status);
129 } /* mock_conn_truncate */
131 static int
132 mock_unixsock_listener(char *socket_path)
133 {
134 struct sockaddr_un sa;
135 int fd, status;
137 fd = socket(AF_UNIX, SOCK_STREAM, 0);
138 fail_unless(fd >= 0,
139 "INTERNAL ERROR: socket() = %d; expected: >=0", fd);
141 memset(&sa, 0, sizeof(sa));
142 sa.sun_family = AF_UNIX;
143 strncpy(sa.sun_path, socket_path, sizeof(sa.sun_path));
145 status = bind(fd, (struct sockaddr *)&sa, sizeof(sa));
146 fail_unless(status == 0,
147 "INTERNAL ERROR: bind() = %d; expected: 0", status);
148 status = listen(fd, 32);
149 fail_unless(status == 0,
150 "INTERNAL ERROR: listen() = %d; expected: 0", status);
152 return fd;
153 } /* mock_unixsock */
155 static void *
156 mock_client(void *arg)
157 {
158 char *socket_path = arg;
160 struct sockaddr_un sa;
161 int fd, check;
163 fd = socket(AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
164 fail_unless(fd >= 0,
165 "INTERNAL ERROR: socket() = %d; expected: >= 0", fd);
167 memset(&sa, 0, sizeof(sa));
168 sa.sun_family = AF_UNIX;
169 strncpy(sa.sun_path, socket_path, sizeof(sa.sun_path));
171 check = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
172 fail_unless(check == 0,
173 "INTERNAL ERROR: connect() = %d; expected: 0", check);
175 close(fd);
176 return NULL;
177 } /* mock_client */
179 static void
180 connection_startup(sdb_conn_t *conn)
181 {
182 ssize_t check, expected;
184 expected = 2 * sizeof(uint32_t) + strlen(username);
185 check = sdb_connection_send(conn, SDB_CONNECTION_STARTUP,
186 (uint32_t)strlen(username), username);
187 fail_unless(check == expected,
188 "sdb_connection_send(STARTUP, %s) = %zi; expected: %zi",
189 username, check, expected);
191 mock_conn_rewind(conn);
192 check = sdb_connection_handle(conn);
193 fail_unless(check == expected,
194 "On startup: sdb_connection_handle() = %zi; expected: %zi",
195 check, expected);
197 fail_unless(sdb_strbuf_len(conn->errbuf) == 0,
198 "sdb_connection_handle() left %zu bytes in the error "
199 "buffer (%s); expected: 0", sdb_strbuf_len(conn->errbuf),
200 sdb_strbuf_string(conn->errbuf));
202 mock_conn_truncate(conn);
203 } /* connection_startup */
205 /*
206 * tests
207 */
209 START_TEST(test_conn_accept)
210 {
211 char socket_path[] = "connection_test_socket.XXXXXX";
212 int fd, check;
214 sdb_conn_t *conn;
216 pthread_t thr;
218 conn = sdb_connection_accept(-1);
219 fail_unless(conn == NULL,
220 "sdb_connection_accept(-1) = %p; expected: NULL", conn);
222 fd = mkstemp(socket_path);
223 unlink(socket_path);
224 close(fd);
226 fd = mock_unixsock_listener(socket_path);
227 check = pthread_create(&thr, /* attr = */ NULL, mock_client, socket_path);
228 fail_unless(check == 0,
229 "INTERNAL ERROR: pthread_create() = %i; expected: 0", check);
231 conn = sdb_connection_accept(fd);
232 fail_unless(conn != NULL,
233 "sdb_connection_accept(%d) = %p; expected: <conn>", fd, conn);
235 unlink(socket_path);
236 sdb_connection_close(conn);
237 sdb_object_deref(SDB_OBJ(conn));
238 pthread_join(thr, NULL);
239 }
240 END_TEST
242 /* test connection setup and very basic commands */
243 START_TEST(test_conn_setup)
244 {
245 sdb_conn_t *conn = mock_conn_create();
247 struct {
248 uint32_t code;
249 const char *msg;
250 const char *err;
251 } golden_data[] = {
252 /* code == UINT32_MAX => no data will be sent */
253 { UINT32_MAX, NULL, NULL },
254 { SDB_CONNECTION_IDLE, "fakedata", "Authentication required" },
255 { SDB_CONNECTION_PING, NULL, "Authentication required" },
256 { SDB_CONNECTION_STARTUP, username, NULL },
257 { SDB_CONNECTION_PING, NULL, NULL },
258 { SDB_CONNECTION_IDLE, NULL, "Invalid command 0" },
259 { SDB_CONNECTION_PING, "fakedata", NULL },
260 { SDB_CONNECTION_IDLE, NULL, "Invalid command 0" },
261 };
263 size_t i;
265 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
266 ssize_t check, expected = 0;
268 mock_conn_truncate(conn);
270 if (golden_data[i].code != UINT32_MAX) {
271 expected = 2 * sizeof(uint32_t)
272 + (golden_data[i].msg ? strlen(golden_data[i].msg) : 0);
274 check = sdb_connection_send(conn, golden_data[i].code,
275 (uint32_t)(golden_data[i].msg
276 ? strlen(golden_data[i].msg) : 0),
277 golden_data[i].msg);
278 fail_unless(check == expected,
279 "sdb_connection_send(%d, %s) = %zi; expected: %zi",
280 golden_data[i].code,
281 golden_data[i].msg ? golden_data[i].msg : "<null>",
282 check, expected);
283 }
285 mock_conn_rewind(conn);
286 check = sdb_connection_handle(conn);
287 fail_unless(check == expected,
288 "sdb_connection_handle() = %zi; expected: %zi",
289 check, expected);
291 fail_unless(sdb_strbuf_len(conn->buf) == 0,
292 "sdb_connection_handle() left %zu bytes in the buffer; "
293 "expected: 0", sdb_strbuf_len(conn->buf));
295 if (golden_data[i].err) {
296 const char *err = sdb_strbuf_string(conn->errbuf);
297 fail_unless(strcmp(err, golden_data[i].err) == 0,
298 "sdb_connection_handle(): got error '%s'; "
299 "expected: '%s'", err, golden_data[i].err);
300 }
301 else
302 fail_unless(sdb_strbuf_len(conn->errbuf) == 0,
303 "sdb_connection_handle() left %zu bytes in the error "
304 "buffer (%s); expected: 0", sdb_strbuf_len(conn->errbuf),
305 sdb_strbuf_string(conn->errbuf));
306 }
308 mock_conn_destroy(conn);
309 }
310 END_TEST
312 /* test simple I/O on open connections */
313 START_TEST(test_conn_io)
314 {
315 sdb_conn_t *conn = mock_conn_create();
317 struct {
318 uint32_t code;
319 uint32_t msg_len;
320 const char *msg;
321 size_t buf_len; /* number of bytes we expect in conn->buf */
322 const char *err;
323 } golden_data[] = {
324 /* code == UINT32_MAX => this is a follow-up package */
325 { SDB_CONNECTION_PING, 20, "9876543210", 0, "Authentication required" },
326 { UINT32_MAX, -1, "9876543210", 0, "Authentication required" },
327 { SDB_CONNECTION_PING, 10, "9876543210", 0, "Authentication required" },
328 { SDB_CONNECTION_IDLE, 10, "9876543210", 0, "Authentication required" },
329 { SDB_CONNECTION_IDLE, 20, "9876543210", 0, "Authentication required" },
330 { UINT32_MAX, -1, "9876543210", 0, "Authentication required" },
331 { SDB_CONNECTION_STARTUP, -1, NULL, 0, NULL },
332 { SDB_CONNECTION_PING, 20, "9876543210", 10, NULL },
333 { UINT32_MAX, -1, "9876543210", 0, NULL },
334 { SDB_CONNECTION_IDLE, 20, "9876543210", 0, "Invalid command 0" },
335 { UINT32_MAX, -1, "9876543210", 0, "Invalid command 0" },
336 { SDB_CONNECTION_IDLE, 20, "9876543210", 0, "Invalid command 0" },
337 { UINT32_MAX, -1, "9876543210", 0, "Invalid command 0" },
338 { SDB_CONNECTION_PING, 10, "9876543210", 0, NULL },
339 { SDB_CONNECTION_PING, 20, "9876543210", 10, NULL },
340 { UINT32_MAX, -1, "9876543210", 0, NULL },
341 };
343 size_t i;
345 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
346 size_t msg_len = golden_data[i].msg ? strlen(golden_data[i].msg) : 0;
347 char buffer[2 * sizeof(uint32_t) + msg_len];
348 size_t offset = 0;
350 ssize_t check;
352 mock_conn_truncate(conn);
354 if (golden_data[i].code == SDB_CONNECTION_STARTUP) {
355 connection_startup(conn);
356 continue;
357 }
359 if (golden_data[i].code != UINT32_MAX) {
360 uint32_t tmp;
362 tmp = htonl(golden_data[i].code);
363 memcpy(buffer, &tmp, sizeof(tmp));
364 tmp = htonl(golden_data[i].msg_len);
365 memcpy(buffer + sizeof(tmp), &tmp, sizeof(tmp));
367 msg_len += 2 * sizeof(uint32_t);
368 offset += 2 * sizeof(uint32_t);
369 }
371 memcpy(buffer + offset, golden_data[i].msg,
372 strlen(golden_data[i].msg));
374 check = sdb_proto_send(conn->fd, msg_len, buffer);
375 fail_unless(check == (ssize_t)msg_len,
376 "sdb_proto_send(%s) = %zi; expected: %zu",
377 check, msg_len);
379 mock_conn_rewind(conn);
380 check = sdb_connection_handle(conn);
381 fail_unless(check == (ssize_t)msg_len,
382 "sdb_connection_handle() = %zi; expected: %zu",
383 check, msg_len);
385 if (golden_data[i].buf_len) {
386 /* partial commands need to be stored in the object */
387 fail_unless(conn->cmd == golden_data[i].code,
388 "sdb_connection_handle() set partial command "
389 "to %u; expected: %u", conn->cmd, golden_data[i].code);
390 fail_unless(conn->cmd_len > golden_data[i].buf_len,
391 "sdb_connection_handle() set partial command length "
392 "to %u; expected: > %u", conn->cmd_len,
393 golden_data[i].buf_len);
394 }
395 else {
396 fail_unless(conn->cmd == SDB_CONNECTION_IDLE,
397 "sdb_connection_handle() did not reset command; "
398 "got %u; expected: %u", conn->cmd, SDB_CONNECTION_IDLE);
399 fail_unless(conn->cmd_len == 0,
400 "sdb_connection_handle() did not reset command length; "
401 "got %u; expected: 0", conn->cmd_len);
402 }
404 fail_unless(sdb_strbuf_len(conn->buf) == golden_data[i].buf_len,
405 "sdb_connection_handle() left %zu bytes in the buffer; "
406 "expected: %zu", sdb_strbuf_len(conn->buf),
407 golden_data[i].buf_len);
409 if (golden_data[i].err) {
410 const char *err = sdb_strbuf_string(conn->errbuf);
411 fail_unless(strcmp(err, golden_data[i].err) == 0,
412 "sdb_connection_handle(): got error '%s'; "
413 "expected: '%s'", err, golden_data[i].err);
414 }
415 else
416 fail_unless(sdb_strbuf_len(conn->errbuf) == 0,
417 "sdb_connection_handle() left %zu bytes in the error "
418 "buffer; expected: 0", sdb_strbuf_len(conn->errbuf));
419 }
421 mock_conn_destroy(conn);
422 }
423 END_TEST
425 Suite *
426 fe_conn_suite(void)
427 {
428 Suite *s = suite_create("frontend::connection");
429 TCase *tc;
431 char *tmp = sdb_get_current_user();
432 assert(tmp);
433 strcpy(username, tmp);
434 free(tmp);
436 tc = tcase_create("core");
437 tcase_add_test(tc, test_conn_accept);
438 tcase_add_test(tc, test_conn_setup);
439 tcase_add_test(tc, test_conn_io);
440 suite_add_tcase(s, tc);
442 return s;
443 } /* fe_conn_suite */
445 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */