Code

connection_test: Use mkstemp() instead of tmpnam().
[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 *socket_path)
123         struct sockaddr_un sa;
124         int fd, status;
126         fd = socket(AF_UNIX, SOCK_STREAM, 0);
127         fail_unless(fd >= 0,
128                         "INTERNAL ERROR: socket() = %d; expected: >=0", fd);
130         memset(&sa, 0, sizeof(sa));
131         sa.sun_family = AF_UNIX;
132         strncpy(sa.sun_path, socket_path, sizeof(sa.sun_path));
134         status = bind(fd, (struct sockaddr *)&sa, sizeof(sa));
135         fail_unless(status == 0,
136                         "INTERNAL ERROR: bind() = %d; expected: 0", status);
137         status = listen(fd, 32);
138         fail_unless(status == 0,
139                         "INTERNAL ERROR: listen() = %d; expected: 0", status);
141         return fd;
142 } /* mock_unixsock */
144 static void *
145 mock_client(void *arg)
147         char *socket_path = arg;
149         struct sockaddr_un sa;
150         int fd, check;
152         fd = socket(AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
153         fail_unless(fd >= 0,
154                         "INTERNAL ERROR: socket() = %d; expected: >= 0", fd);
156         memset(&sa, 0, sizeof(sa));
157         sa.sun_family = AF_UNIX;
158         strncpy(sa.sun_path, socket_path, sizeof(sa.sun_path));
160         check = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
161         fail_unless(check == 0,
162                         "INTERNAL ERROR: connect() = %d; expected: 0", check);
164         close(fd);
165         return NULL;
166 } /* mock_client */
168 static void
169 connection_startup(sdb_conn_t *conn)
171         ssize_t check, expected;
173         expected = 2 * sizeof(uint32_t) + strlen("fakeuser");
174         check = sdb_connection_send(conn, CONNECTION_STARTUP,
175                         (uint32_t)strlen("fakeuser"), "fakeuser");
176         fail_unless(check == expected,
177                         "sdb_connection_send(STARTUP, fakeuser) = %zi; expected: %zi",
178                         check, expected);
180         mock_conn_rewind(conn);
181         check = sdb_connection_read(conn);
182         fail_unless(check == expected,
183                         "On startup: sdb_connection_read() = %zi; expected: %zi",
184                         check, expected);
186         fail_unless(sdb_strbuf_len(conn->errbuf) == 0,
187                         "sdb_connection_read() left %zu bytes in the error "
188                         "buffer; expected: 0", sdb_strbuf_len(conn->errbuf));
190         mock_conn_truncate(conn);
191 } /* connection_startup */
193 /*
194  * tests
195  */
197 START_TEST(test_conn_accept)
199         char socket_path[] = "connection_test_socket.XXXXXX";
200         int fd, check;
202         sdb_conn_t *conn;
204         pthread_t thr;
206         conn = sdb_connection_accept(-1);
207         fail_unless(conn == NULL,
208                         "sdb_connection_accept(-1) = %p; expected: NULL", conn);
210         mkstemp(socket_path);
211         unlink(socket_path);
213         fd = mock_unixsock_listener(socket_path);
214         check = pthread_create(&thr, /* attr = */ NULL, mock_client, socket_path);
215         fail_unless(check == 0,
216                         "INTERNAL ERROR: pthread_create() = %i; expected: 0", check);
218         conn = sdb_connection_accept(fd);
219         fail_unless(conn != NULL,
220                         "sdb_connection_accept(%d) = %p; expected: <conn>", fd, conn);
222         unlink(socket_path);
223         sdb_connection_close(conn);
224         pthread_join(thr, NULL);
226 END_TEST
228 /* test connection setup and very basic commands */
229 START_TEST(test_conn_setup)
231         sdb_conn_t *conn = mock_conn_create();
233         struct {
234                 uint32_t code;
235                 const char *msg;
236                 const char *err;
237         } golden_data[] = {
238                 /* code == UINT32_MAX => no data will be sent */
239                 { UINT32_MAX,         NULL,       NULL },
240                 { CONNECTION_IDLE,    "fakedata", "Authentication required" },
241                 { CONNECTION_PING,    NULL,       "Authentication required" },
242                 { CONNECTION_STARTUP, "fakeuser", NULL },
243                 { CONNECTION_PING,    NULL,       NULL },
244                 { CONNECTION_IDLE,    NULL,       "Invalid command 0" },
245                 { CONNECTION_PING,    "fakedata", NULL },
246                 { CONNECTION_IDLE,    NULL,       "Invalid command 0" },
247         };
249         size_t i;
251         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
252                 ssize_t check, expected = 0;
254                 mock_conn_truncate(conn);
256                 if (golden_data[i].code != UINT32_MAX) {
257                         expected = 2 * sizeof(uint32_t)
258                                 + (golden_data[i].msg ? strlen(golden_data[i].msg) : 0);
260                         check = sdb_connection_send(conn, golden_data[i].code,
261                                         (uint32_t)(golden_data[i].msg
262                                                 ? strlen(golden_data[i].msg) : 0),
263                                         golden_data[i].msg);
264                         fail_unless(check == expected,
265                                         "sdb_connection_send(%d, %s) = %zi; expected: %zi",
266                                         golden_data[i].code,
267                                         golden_data[i].msg ? golden_data[i].msg : "<null>",
268                                         check, expected);
269                 }
271                 mock_conn_rewind(conn);
272                 check = sdb_connection_read(conn);
273                 fail_unless(check == expected,
274                                 "sdb_connection_read() = %zi; expected: %zi",
275                                 check, expected);
277                 fail_unless(sdb_strbuf_len(conn->buf) == 0,
278                                 "sdb_connection_read() left %zu bytes in the buffer; "
279                                 "expected: 0", sdb_strbuf_len(conn->buf));
281                 if (golden_data[i].err) {
282                         const char *err = sdb_strbuf_string(conn->errbuf);
283                         fail_unless(strcmp(err, golden_data[i].err) == 0,
284                                         "sdb_connection_read(): got error '%s'; "
285                                         "expected: '%s'", err, golden_data[i].err);
286                 }
287                 else
288                         fail_unless(sdb_strbuf_len(conn->errbuf) == 0,
289                                         "sdb_connection_read() left %zu bytes in the error "
290                                         "buffer; expected: 0", sdb_strbuf_len(conn->errbuf));
291         }
293         mock_conn_destroy(conn);
295 END_TEST
297 /* test simple I/O on open connections */
298 START_TEST(test_conn_io)
300         sdb_conn_t *conn = mock_conn_create();
302         struct {
303                 uint32_t code;
304                 uint32_t msg_len;
305                 const char *msg;
306                 size_t buf_len; /* number of bytes we expect in conn->buf */
307                 const char *err;
308         } golden_data[] = {
309                 /* code == UINT32_MAX => this is a follow-up package */
310                 { CONNECTION_PING,    20, "9876543210",  0, "Authentication required" },
311                 { UINT32_MAX,         -1, "9876543210",  0, "Authentication required" },
312                 { CONNECTION_PING,    10, "9876543210",  0, "Authentication required" },
313                 { CONNECTION_IDLE,    10, "9876543210",  0, "Authentication required" },
314                 { CONNECTION_IDLE,    20, "9876543210",  0, "Authentication required" },
315                 { UINT32_MAX,         -1, "9876543210",  0, "Authentication required" },
316                 { CONNECTION_STARTUP, -1, NULL,          0, NULL },
317                 { CONNECTION_PING,    20, "9876543210", 10, NULL },
318                 { UINT32_MAX,         -1, "9876543210",  0, NULL },
319                 { CONNECTION_IDLE,    20, "9876543210",  0, "Invalid command 0" },
320                 { UINT32_MAX,         -1, "9876543210",  0, "Invalid command 0" },
321                 { CONNECTION_IDLE,    20, "9876543210",  0, "Invalid command 0" },
322                 { UINT32_MAX,         -1, "9876543210",  0, "Invalid command 0" },
323                 { CONNECTION_PING,    10, "9876543210",  0, NULL },
324                 { CONNECTION_PING,    20, "9876543210", 10, NULL },
325                 { UINT32_MAX,         -1, "9876543210",  0, NULL },
326         };
328         size_t i;
330         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
331                 size_t msg_len = golden_data[i].msg ? strlen(golden_data[i].msg) : 0;
332                 char buffer[2 * sizeof(uint32_t) + msg_len];
333                 size_t offset = 0;
335                 ssize_t check;
337                 mock_conn_truncate(conn);
339                 if (golden_data[i].code == CONNECTION_STARTUP) {
340                         connection_startup(conn);
341                         continue;
342                 }
344                 if (golden_data[i].code != UINT32_MAX) {
345                         uint32_t tmp;
347                         tmp = htonl(golden_data[i].code);
348                         memcpy(buffer, &tmp, sizeof(tmp));
349                         tmp = htonl(golden_data[i].msg_len);
350                         memcpy(buffer + sizeof(tmp), &tmp, sizeof(tmp));
352                         msg_len += 2 * sizeof(uint32_t);
353                         offset += 2 * sizeof(uint32_t);
354                 }
356                 memcpy(buffer + offset, golden_data[i].msg,
357                                 strlen(golden_data[i].msg));
359                 check = sdb_proto_send(conn->fd, msg_len, buffer);
360                 fail_unless(check == (ssize_t)msg_len,
361                                 "sdb_proto_send(%s) = %zi; expected: %zu",
362                                 check, msg_len);
364                 mock_conn_rewind(conn);
365                 check = sdb_connection_read(conn);
366                 fail_unless(check == (ssize_t)msg_len,
367                                 "sdb_connection_read() = %zi; expected: %zu",
368                                 check, msg_len);
370                 if (golden_data[i].buf_len) {
371                         /* partial commands need to be stored in the object */
372                         fail_unless(conn->cmd == golden_data[i].code,
373                                         "sdb_connection_read() set partial command "
374                                         "to %u; expected: %u", conn->cmd, golden_data[i].code);
375                         fail_unless(conn->cmd_len > golden_data[i].buf_len,
376                                         "sdb_connection_read() set partial command length "
377                                         "to %u; expected: > %u", conn->cmd_len,
378                                         golden_data[i].buf_len);
379                 }
380                 else {
381                         fail_unless(conn->cmd == CONNECTION_IDLE,
382                                         "sdb_connection_read() did not reset command; "
383                                         "got %u; expected: %u", conn->cmd, CONNECTION_IDLE);
384                         fail_unless(conn->cmd_len == 0,
385                                         "sdb_connection_read() did not reset command length; "
386                                         "got %u; expected: 0", conn->cmd_len);
387                 }
389                 fail_unless(sdb_strbuf_len(conn->buf) == golden_data[i].buf_len,
390                                 "sdb_connection_read() left %zu bytes in the buffer; "
391                                 "expected: %zu", sdb_strbuf_len(conn->buf),
392                                 golden_data[i].buf_len);
394                 if (golden_data[i].err) {
395                         const char *err = sdb_strbuf_string(conn->errbuf);
396                         fail_unless(strcmp(err, golden_data[i].err) == 0,
397                                         "sdb_connection_read(): got error '%s'; "
398                                         "expected: '%s'", err, golden_data[i].err);
399                 }
400                 else
401                         fail_unless(sdb_strbuf_len(conn->errbuf) == 0,
402                                         "sdb_connection_read() left %zu bytes in the error "
403                                         "buffer; expected: 0", sdb_strbuf_len(conn->errbuf));
404         }
406         mock_conn_destroy(conn);
408 END_TEST
410 Suite *
411 fe_conn_suite(void)
413         Suite *s = suite_create("frontend::connection");
414         TCase *tc;
416         tc = tcase_create("core");
417         tcase_add_test(tc, test_conn_accept);
418         tcase_add_test(tc, test_conn_setup);
419         tcase_add_test(tc, test_conn_io);
420         suite_add_tcase(s, tc);
422         return s;
423 } /* fe_conn_suite */
425 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */