Code

connection_test: Free username strings stored in mock connection objects.
[sysdb.git] / t / unit / frontend / connection_test.c
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)
110         lseek(conn->fd, 0, SEEK_SET);
111 } /* mock_conn_rewind */
113 static void
114 mock_conn_truncate(sdb_conn_t *conn)
116         lseek(conn->fd, 0, SEEK_SET);
117         ftruncate(conn->fd, 0);
118 } /* mock_conn_truncate */
120 static int
121 mock_unixsock_listener(char *sock_path)
123         struct sockaddr_un sa;
124         char *filename;
125         int fd, status;
127         filename = tmpnam(sock_path);
128         fail_unless(filename != NULL,
129                         "INTERNAL ERROR: tmpnam() = NULL; expected: a string");
131         fd = socket(AF_UNIX, SOCK_STREAM, 0);
132         fail_unless(fd >= 0,
133                         "INTERNAL ERROR: socket() = %d; expected: >=0", fd);
135         memset(&sa, 0, sizeof(sa));
136         sa.sun_family = AF_UNIX;
137         strncpy(sa.sun_path, filename, sizeof(sa.sun_path));
139         status = bind(fd, (struct sockaddr *)&sa, sizeof(sa));
140         fail_unless(status == 0,
141                         "INTERNAL ERROR: bind() = %d; expected: 0", status);
142         status = listen(fd, 32);
143         fail_unless(status == 0,
144                         "INTERNAL ERROR: listen() = %d; expected: 0", status);
146         return fd;
147 } /* mock_unixsock */
149 static void *
150 mock_client(void *arg)
152         char *socket_path = arg;
154         struct sockaddr_un sa;
155         int fd, check;
157         fd = socket(AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
158         fail_unless(fd >= 0,
159                         "INTERNAL ERROR: socket() = %d; expected: >= 0", fd);
161         memset(&sa, 0, sizeof(sa));
162         sa.sun_family = AF_UNIX;
163         strncpy(sa.sun_path, socket_path, sizeof(sa.sun_path));
165         check = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
166         fail_unless(check == 0,
167                         "INTERNAL ERROR: connect() = %d; expected: 0", check);
169         close(fd);
170         return NULL;
171 } /* mock_client */
173 static void
174 connection_startup(sdb_conn_t *conn)
176         ssize_t check, expected;
178         expected = 2 * sizeof(uint32_t) + strlen("fakeuser");
179         check = sdb_connection_send(conn, CONNECTION_STARTUP,
180                         (uint32_t)strlen("fakeuser"), "fakeuser");
181         fail_unless(check == expected,
182                         "sdb_connection_send(STARTUP, fakeuser) = %zi; expected: %zi",
183                         check, expected);
185         mock_conn_rewind(conn);
186         check = sdb_connection_read(conn);
187         fail_unless(check == expected,
188                         "On startup: sdb_connection_read() = %zi; expected: %zi",
189                         check, expected);
191         fail_unless(sdb_strbuf_len(conn->errbuf) == 0,
192                         "sdb_connection_read() left %zu bytes in the error "
193                         "buffer; expected: 0", sdb_strbuf_len(conn->errbuf));
195         mock_conn_truncate(conn);
196 } /* connection_startup */
198 /*
199  * tests
200  */
202 START_TEST(test_conn_accept)
204         char socket_path[L_tmpnam];
205         int fd, check;
207         sdb_conn_t *conn;
209         pthread_t thr;
211         conn = sdb_connection_accept(-1);
212         fail_unless(conn == NULL,
213                         "sdb_connection_accept(-1) = %p; expected: NULL", conn);
215         memset(&socket_path, 0, sizeof(socket_path));
216         fd = mock_unixsock_listener(socket_path);
217         check = pthread_create(&thr, /* attr = */ NULL, mock_client, socket_path);
218         fail_unless(check == 0,
219                         "INTERNAL ERROR: pthread_create() = %i; expected: 0", check);
221         conn = sdb_connection_accept(fd);
222         fail_unless(conn != NULL,
223                         "sdb_connection_accept(%d) = %p; expected: <conn>", fd, conn);
225         unlink(socket_path);
226         sdb_connection_close(conn);
227         pthread_join(thr, NULL);
229 END_TEST
231 /* test connection setup and very basic commands */
232 START_TEST(test_conn_setup)
234         sdb_conn_t *conn = mock_conn_create();
236         struct {
237                 uint32_t code;
238                 const char *msg;
239                 const char *err;
240         } golden_data[] = {
241                 /* code == UINT32_MAX => no data will be sent */
242                 { UINT32_MAX,         NULL,       NULL },
243                 { CONNECTION_IDLE,    "fakedata", "Authentication required" },
244                 { CONNECTION_PING,    NULL,       "Authentication required" },
245                 { CONNECTION_STARTUP, "fakeuser", NULL },
246                 { CONNECTION_PING,    NULL,       NULL },
247                 { CONNECTION_IDLE,    NULL,       "Invalid command 0" },
248                 { CONNECTION_PING,    "fakedata", NULL },
249                 { CONNECTION_IDLE,    NULL,       "Invalid command 0" },
250         };
252         size_t i;
254         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
255                 ssize_t check, expected = 0;
257                 mock_conn_truncate(conn);
259                 if (golden_data[i].code != UINT32_MAX) {
260                         expected = 2 * sizeof(uint32_t)
261                                 + (golden_data[i].msg ? strlen(golden_data[i].msg) : 0);
263                         check = sdb_connection_send(conn, golden_data[i].code,
264                                         (uint32_t)(golden_data[i].msg
265                                                 ? strlen(golden_data[i].msg) : 0),
266                                         golden_data[i].msg);
267                         fail_unless(check == expected,
268                                         "sdb_connection_send(%d, %s) = %zi; expected: %zi",
269                                         golden_data[i].code,
270                                         golden_data[i].msg ? golden_data[i].msg : "<null>",
271                                         check, expected);
272                 }
274                 mock_conn_rewind(conn);
275                 check = sdb_connection_read(conn);
276                 fail_unless(check == expected,
277                                 "sdb_connection_read() = %zi; expected: %zi",
278                                 check, expected);
280                 fail_unless(sdb_strbuf_len(conn->buf) == 0,
281                                 "sdb_connection_read() left %zu bytes in the buffer; "
282                                 "expected: 0", sdb_strbuf_len(conn->buf));
284                 if (golden_data[i].err) {
285                         const char *err = sdb_strbuf_string(conn->errbuf);
286                         fail_unless(strcmp(err, golden_data[i].err) == 0,
287                                         "sdb_connection_read(): got error '%s'; "
288                                         "expected: '%s'", err, golden_data[i].err);
289                 }
290                 else
291                         fail_unless(sdb_strbuf_len(conn->errbuf) == 0,
292                                         "sdb_connection_read() left %zu bytes in the error "
293                                         "buffer; expected: 0", sdb_strbuf_len(conn->errbuf));
294         }
296         mock_conn_destroy(conn);
298 END_TEST
300 /* test simple I/O on open connections */
301 START_TEST(test_conn_io)
303         sdb_conn_t *conn = mock_conn_create();
305         struct {
306                 uint32_t code;
307                 uint32_t msg_len;
308                 const char *msg;
309                 size_t buf_len; /* number of bytes we expect in conn->buf */
310                 const char *err;
311         } golden_data[] = {
312                 /* code == UINT32_MAX => this is a follow-up package */
313                 { CONNECTION_PING,    20, "9876543210",  0, "Authentication required" },
314                 { UINT32_MAX,         -1, "9876543210",  0, "Authentication required" },
315                 { CONNECTION_PING,    10, "9876543210",  0, "Authentication required" },
316                 { CONNECTION_IDLE,    10, "9876543210",  0, "Authentication required" },
317                 { CONNECTION_IDLE,    20, "9876543210",  0, "Authentication required" },
318                 { UINT32_MAX,         -1, "9876543210",  0, "Authentication required" },
319                 { CONNECTION_STARTUP, -1, NULL,          0, NULL },
320                 { CONNECTION_PING,    20, "9876543210", 10, NULL },
321                 { UINT32_MAX,         -1, "9876543210",  0, NULL },
322                 { CONNECTION_IDLE,    20, "9876543210",  0, "Invalid command 0" },
323                 { UINT32_MAX,         -1, "9876543210",  0, "Invalid command 0" },
324                 { CONNECTION_IDLE,    20, "9876543210",  0, "Invalid command 0" },
325                 { UINT32_MAX,         -1, "9876543210",  0, "Invalid command 0" },
326                 { CONNECTION_PING,    10, "9876543210",  0, NULL },
327                 { CONNECTION_PING,    20, "9876543210", 10, NULL },
328                 { UINT32_MAX,         -1, "9876543210",  0, NULL },
329         };
331         size_t i;
333         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
334                 size_t msg_len = golden_data[i].msg ? strlen(golden_data[i].msg) : 0;
335                 char buffer[2 * sizeof(uint32_t) + msg_len];
336                 size_t offset = 0;
338                 ssize_t check;
340                 mock_conn_truncate(conn);
342                 if (golden_data[i].code == CONNECTION_STARTUP) {
343                         connection_startup(conn);
344                         continue;
345                 }
347                 if (golden_data[i].code != UINT32_MAX) {
348                         uint32_t tmp;
350                         tmp = htonl(golden_data[i].code);
351                         memcpy(buffer, &tmp, sizeof(tmp));
352                         tmp = htonl(golden_data[i].msg_len);
353                         memcpy(buffer + sizeof(tmp), &tmp, sizeof(tmp));
355                         msg_len += 2 * sizeof(uint32_t);
356                         offset += 2 * sizeof(uint32_t);
357                 }
359                 memcpy(buffer + offset, golden_data[i].msg,
360                                 strlen(golden_data[i].msg));
362                 check = sdb_proto_send(conn->fd, msg_len, buffer);
363                 fail_unless(check == (ssize_t)msg_len,
364                                 "sdb_proto_send(%s) = %zi; expected: %zu",
365                                 check, msg_len);
367                 mock_conn_rewind(conn);
368                 check = sdb_connection_read(conn);
369                 fail_unless(check == (ssize_t)msg_len,
370                                 "sdb_connection_read() = %zi; expected: %zu",
371                                 check, msg_len);
373                 if (golden_data[i].buf_len) {
374                         /* partial commands need to be stored in the object */
375                         fail_unless(conn->cmd == golden_data[i].code,
376                                         "sdb_connection_read() set partial command "
377                                         "to %u; expected: %u", conn->cmd, golden_data[i].code);
378                         fail_unless(conn->cmd_len > golden_data[i].buf_len,
379                                         "sdb_connection_read() set partial command length "
380                                         "to %u; expected: > %u", conn->cmd_len,
381                                         golden_data[i].buf_len);
382                 }
383                 else {
384                         fail_unless(conn->cmd == CONNECTION_IDLE,
385                                         "sdb_connection_read() did not reset command; "
386                                         "got %u; expected: %u", conn->cmd, CONNECTION_IDLE);
387                         fail_unless(conn->cmd_len == 0,
388                                         "sdb_connection_read() did not reset command length; "
389                                         "got %u; expected: 0", conn->cmd_len);
390                 }
392                 fail_unless(sdb_strbuf_len(conn->buf) == golden_data[i].buf_len,
393                                 "sdb_connection_read() left %zu bytes in the buffer; "
394                                 "expected: %zu", sdb_strbuf_len(conn->buf),
395                                 golden_data[i].buf_len);
397                 if (golden_data[i].err) {
398                         const char *err = sdb_strbuf_string(conn->errbuf);
399                         fail_unless(strcmp(err, golden_data[i].err) == 0,
400                                         "sdb_connection_read(): got error '%s'; "
401                                         "expected: '%s'", err, golden_data[i].err);
402                 }
403                 else
404                         fail_unless(sdb_strbuf_len(conn->errbuf) == 0,
405                                         "sdb_connection_read() left %zu bytes in the error "
406                                         "buffer; expected: 0", sdb_strbuf_len(conn->errbuf));
407         }
409         mock_conn_destroy(conn);
411 END_TEST
413 Suite *
414 fe_conn_suite(void)
416         Suite *s = suite_create("frontend::connection");
417         TCase *tc;
419         tc = tcase_create("core");
420         tcase_add_test(tc, test_conn_accept);
421         tcase_add_test(tc, test_conn_setup);
422         tcase_add_test(tc, test_conn_io);
423         suite_add_tcase(s, tc);
425         return s;
426 } /* fe_conn_suite */
428 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */