Code

frontend: Fix invalid command handling when receiving data in chunks.
[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         free(conn);
65 } /* mock_conn_destroy */
67 static sdb_conn_t *
68 mock_conn_create(void)
69 {
70         sdb_conn_t *conn;
72         char tmp_file[] = "connection_test_socket.XXXXXX";
74         conn = calloc(1, sizeof(*conn));
75         if (! conn) {
76                 fail("INTERNAL ERROR: failed to allocate connection object");
77                 return NULL;
78         }
80         SDB_OBJ(conn)->name = strdup("mock_connection");
81         SDB_OBJ(conn)->ref_cnt = 1;
83         conn->buf = sdb_strbuf_create(0);
84         conn->errbuf = sdb_strbuf_create(0);
85         if ((! conn->buf) || (! conn->errbuf)) {
86                 mock_conn_destroy(conn);
87                 fail("INTERNAL ERROR: failed to allocate connection object");
88                 return NULL;
89         }
91         conn->fd = mkstemp(tmp_file);
92         if (conn->fd < 0) {
93                 mock_conn_destroy(conn);
94                 fail("INTERNAL ERROR: failed to allocate connection object");
95                 return NULL;
96         }
98         unlink(tmp_file);
100         conn->cmd = CONNECTION_IDLE;
101         conn->cmd_len = 0;
102         return conn;
103 } /* mock_conn_create */
105 static void
106 mock_conn_rewind(sdb_conn_t *conn)
108         lseek(conn->fd, 0, SEEK_SET);
109 } /* mock_conn_rewind */
111 static void
112 mock_conn_truncate(sdb_conn_t *conn)
114         lseek(conn->fd, 0, SEEK_SET);
115         ftruncate(conn->fd, 0);
116 } /* mock_conn_truncate */
118 static int
119 mock_unixsock_listener(char *sock_path)
121         struct sockaddr_un sa;
122         char *filename;
123         int fd, status;
125         filename = tmpnam(sock_path);
126         fail_unless(filename != NULL,
127                         "INTERNAL ERROR: tmpnam() = NULL; expected: a string");
129         fd = socket(AF_UNIX, SOCK_STREAM, 0);
130         fail_unless(fd >= 0,
131                         "INTERNAL ERROR: socket() = %d; expected: >=0", fd);
133         memset(&sa, 0, sizeof(sa));
134         sa.sun_family = AF_UNIX;
135         strncpy(sa.sun_path, filename, sizeof(sa.sun_path));
137         status = bind(fd, (struct sockaddr *)&sa, sizeof(sa));
138         fail_unless(status == 0,
139                         "INTERNAL ERROR: bind() = %d; expected: 0", status);
140         status = listen(fd, 32);
141         fail_unless(status == 0,
142                         "INTERNAL ERROR: listen() = %d; expected: 0", status);
144         return fd;
145 } /* mock_unixsock */
147 static void *
148 mock_client(void *arg)
150         char *socket_path = arg;
152         struct sockaddr_un sa;
153         int fd, check;
155         fd = socket(AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
156         fail_unless(fd >= 0,
157                         "INTERNAL ERROR: socket() = %d; expected: >= 0", fd);
159         memset(&sa, 0, sizeof(sa));
160         sa.sun_family = AF_UNIX;
161         strncpy(sa.sun_path, socket_path, sizeof(sa.sun_path));
163         check = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
164         fail_unless(check == 0,
165                         "INTERNAL ERROR: connect() = %d; expected: 0", check);
167         close(fd);
168         return NULL;
169 } /* mock_client */
171 static void
172 connection_setup(sdb_conn_t *conn)
174         ssize_t check, expected;
176         expected = 2 * sizeof(uint32_t) + strlen("fakeuser");
177         check = sdb_connection_send(conn, CONNECTION_STARTUP,
178                         (uint32_t)strlen("fakeuser"), "fakeuser");
179         fail_unless(check == expected,
180                         "sdb_connection_send(STARTUP, fakeuser) = %zi; expected: %zi",
181                         check, expected);
183         mock_conn_rewind(conn);
184         check = sdb_connection_read(conn);
185         fail_unless(check == expected,
186                         "On startup: sdb_connection_read() = %zi; expected: %zi",
187                         check, expected);
189         fail_unless(sdb_strbuf_len(conn->errbuf) == 0,
190                         "sdb_connection_read() left %zu bytes in the error "
191                         "buffer; expected: 0", sdb_strbuf_len(conn->errbuf));
193         mock_conn_truncate(conn);
194 } /* connection_setup */
196 /*
197  * tests
198  */
200 START_TEST(test_conn_accept)
202         char socket_path[L_tmpnam];
203         int fd, check;
205         sdb_conn_t *conn;
207         pthread_t thr;
209         conn = sdb_connection_accept(-1);
210         fail_unless(conn == NULL,
211                         "sdb_connection_accept(-1) = %p; expected: NULL", conn);
213         memset(&socket_path, 0, sizeof(socket_path));
214         fd = mock_unixsock_listener(socket_path);
215         check = pthread_create(&thr, /* attr = */ NULL, mock_client, socket_path);
216         fail_unless(check == 0,
217                         "INTERNAL ERROR: pthread_create() = %i; expected: 0", check);
219         conn = sdb_connection_accept(fd);
220         fail_unless(conn != NULL,
221                         "sdb_connection_accept(%d) = %p; expected: <conn>", fd, conn);
223         unlink(socket_path);
224         sdb_connection_close(conn);
225         pthread_join(thr, NULL);
227 END_TEST
229 /* test connection setup and very basic commands */
230 START_TEST(test_conn_setup)
232         sdb_conn_t *conn = mock_conn_create();
234         struct {
235                 uint32_t code;
236                 const char *msg;
237                 const char *err;
238         } golden_data[] = {
239                 /* code == UINT32_MAX => no data will be sent */
240                 { UINT32_MAX,         NULL,       NULL },
241                 { CONNECTION_IDLE,    "fakedata", "Invalid command 0" },
242                 { CONNECTION_PING,    NULL,       "Authentication required" },
243                 { CONNECTION_STARTUP, "fakeuser", NULL },
244                 { CONNECTION_PING,    NULL,       NULL },
245                 { CONNECTION_IDLE,    NULL,       "Invalid command 0" },
246                 { CONNECTION_PING,    "fakedata", NULL },
247                 { CONNECTION_IDLE,    NULL,       "Invalid command 0" },
248         };
250         size_t i;
252         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
253                 ssize_t check, expected = 0;
255                 mock_conn_truncate(conn);
257                 if (golden_data[i].code != UINT32_MAX) {
258                         expected = 2 * sizeof(uint32_t)
259                                 + (golden_data[i].msg ? strlen(golden_data[i].msg) : 0);
261                         check = sdb_connection_send(conn, golden_data[i].code,
262                                         (uint32_t)(golden_data[i].msg
263                                                 ? strlen(golden_data[i].msg) : 0),
264                                         golden_data[i].msg);
265                         fail_unless(check == expected,
266                                         "sdb_connection_send(%d, %s) = %zi; expected: %zi",
267                                         golden_data[i].code,
268                                         golden_data[i].msg ? golden_data[i].msg : "<null>",
269                                         check, expected);
270                 }
272                 mock_conn_rewind(conn);
273                 check = sdb_connection_read(conn);
274                 fail_unless(check == expected,
275                                 "sdb_connection_read() = %zi; expected: %zi",
276                                 check, expected);
278                 fail_unless(sdb_strbuf_len(conn->buf) == 0,
279                                 "sdb_connection_read() left %zu bytes in the buffer; "
280                                 "expected: 0", sdb_strbuf_len(conn->buf));
282                 if (golden_data[i].err) {
283                         const char *err = sdb_strbuf_string(conn->errbuf);
284                         fail_unless(strcmp(err, golden_data[i].err) == 0,
285                                         "sdb_connection_read(): got error '%s'; "
286                                         "expected: '%s'", err, golden_data[i].err);
287                 }
288                 else
289                         fail_unless(sdb_strbuf_len(conn->errbuf) == 0,
290                                         "sdb_connection_read() left %zu bytes in the error "
291                                         "buffer; expected: 0", sdb_strbuf_len(conn->errbuf));
292         }
294         mock_conn_destroy(conn);
296 END_TEST
298 /* test simple I/O on open connections */
299 START_TEST(test_conn_io)
301         sdb_conn_t *conn = mock_conn_create();
303         struct {
304                 uint32_t code;
305                 uint32_t msg_len;
306                 const char *msg;
307                 size_t buf_len; /* number of bytes we expect in conn->buf */
308                 const char *err;
309         } golden_data[] = {
310                 /* code == UINT32_MAX => this is a follow-up package */
311                 { CONNECTION_PING, 20, "9876543210", 10, NULL },
312                 { UINT32_MAX,      -1, "9876543210",  0, NULL },
313                 { CONNECTION_IDLE, 20, "9876543210",  0, "Invalid command 0" },
314                 { UINT32_MAX,      -1, "9876543210",  0, "Invalid command 0" },
315                 { CONNECTION_IDLE, 20, "9876543210",  0, "Invalid command 0" },
316                 { UINT32_MAX,      -1, "9876543210",  0, "Invalid command 0" },
317                 { CONNECTION_PING, 10, "9876543210",  0, NULL },
318                 { CONNECTION_PING, 20, "9876543210", 10, NULL },
319                 { UINT32_MAX,      -1, "9876543210",  0, NULL },
320         };
322         size_t i;
324         connection_setup(conn);
326         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
327                 size_t msg_len = strlen(golden_data[i].msg);
328                 char buffer[2 * sizeof(uint32_t) + msg_len];
329                 size_t offset = 0;
331                 ssize_t check;
333                 mock_conn_truncate(conn);
335                 if (golden_data[i].code != UINT32_MAX) {
336                         uint32_t tmp;
338                         tmp = htonl(golden_data[i].code);
339                         memcpy(buffer, &tmp, sizeof(tmp));
340                         tmp = htonl(golden_data[i].msg_len);
341                         memcpy(buffer + sizeof(tmp), &tmp, sizeof(tmp));
343                         msg_len += 2 * sizeof(uint32_t);
344                         offset += 2 * sizeof(uint32_t);
345                 }
347                 memcpy(buffer + offset, golden_data[i].msg,
348                                 strlen(golden_data[i].msg));
350                 check = sdb_proto_send(conn->fd, msg_len, buffer);
351                 fail_unless(check == (ssize_t)msg_len,
352                                 "sdb_proto_send(%s) = %zi; expected: %zu",
353                                 check, msg_len);
355                 mock_conn_rewind(conn);
356                 check = sdb_connection_read(conn);
357                 fail_unless(check == (ssize_t)msg_len,
358                                 "sdb_connection_read() = %zi; expected: %zu",
359                                 check, msg_len);
361                 if (golden_data[i].buf_len) {
362                         /* partial commands need to be stored in the object */
363                         fail_unless(conn->cmd == golden_data[i].code,
364                                         "sdb_connection_read() set partial command "
365                                         "to %u; expected: %u", conn->cmd, golden_data[i].code);
366                         fail_unless(conn->cmd_len > golden_data[i].buf_len,
367                                         "sdb_connection_read() set partial command length "
368                                         "to %u; expected: > %u", conn->cmd_len,
369                                         golden_data[i].buf_len);
370                 }
371                 else {
372                         fail_unless(conn->cmd == CONNECTION_IDLE,
373                                         "sdb_connection_read() did not reset command; "
374                                         "got %u; expected: %u", conn->cmd, CONNECTION_IDLE);
375                         fail_unless(conn->cmd_len == 0,
376                                         "sdb_connection_read() did not reset command length; "
377                                         "got %u; expected: 0", conn->cmd_len);
378                 }
380                 fail_unless(sdb_strbuf_len(conn->buf) == golden_data[i].buf_len,
381                                 "sdb_connection_read() left %zu bytes in the buffer; "
382                                 "expected: %zu", sdb_strbuf_len(conn->buf),
383                                 golden_data[i].buf_len);
385                 if (golden_data[i].err) {
386                         const char *err = sdb_strbuf_string(conn->errbuf);
387                         fail_unless(strcmp(err, golden_data[i].err) == 0,
388                                         "sdb_connection_read(): got error '%s'; "
389                                         "expected: '%s'", err, golden_data[i].err);
390                 }
391                 else
392                         fail_unless(sdb_strbuf_len(conn->errbuf) == 0,
393                                         "sdb_connection_read() left %zu bytes in the error "
394                                         "buffer; expected: 0", sdb_strbuf_len(conn->errbuf));
395         }
397         mock_conn_destroy(conn);
399 END_TEST
401 Suite *
402 fe_conn_suite(void)
404         Suite *s = suite_create("frontend::connection");
405         TCase *tc;
407         tc = tcase_create("core");
408         tcase_add_test(tc, test_conn_accept);
409         tcase_add_test(tc, test_conn_setup);
410         tcase_add_test(tc, test_conn_io);
411         suite_add_tcase(s, tc);
413         return s;
414 } /* fe_conn_suite */
416 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */