Code

Moved unit tests into t/unit/ subdirectory.
[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 "libsysdb_test.h"
36 #include "utils/strbuf.h"
38 #include <check.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
44 #include <pthread.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <sys/un.h>
50 /*
51  * private helper functions
52  */
54 static void
55 mock_conn_destroy(sdb_conn_t *conn)
56 {
57         if (SDB_OBJ(conn)->name)
58                 free(SDB_OBJ(conn)->name);
59         sdb_strbuf_destroy(conn->buf);
60         sdb_strbuf_destroy(conn->errbuf);
61         if (conn->fd >= 0)
62                 close(conn->fd);
63         free(conn);
64 } /* mock_conn_destroy */
66 static sdb_conn_t *
67 mock_conn_create(void)
68 {
69         sdb_conn_t *conn;
71         char tmp_file[] = "connection_test_socket.XXXXXX";
73         conn = calloc(1, sizeof(*conn));
74         if (! conn) {
75                 fail("INTERNAL ERROR: failed to allocate connection object");
76                 return NULL;
77         }
79         SDB_OBJ(conn)->name = strdup("mock_connection");
80         SDB_OBJ(conn)->ref_cnt = 1;
82         conn->buf = sdb_strbuf_create(0);
83         conn->errbuf = sdb_strbuf_create(0);
84         if ((! conn->buf) || (! conn->errbuf)) {
85                 mock_conn_destroy(conn);
86                 fail("INTERNAL ERROR: failed to allocate connection object");
87                 return NULL;
88         }
90         conn->fd = mkstemp(tmp_file);
91         if (conn->fd < 0) {
92                 mock_conn_destroy(conn);
93                 fail("INTERNAL ERROR: failed to allocate connection object");
94                 return NULL;
95         }
97         unlink(tmp_file);
99         conn->cmd = CONNECTION_IDLE;
100         conn->cmd_len = 0;
101         return conn;
102 } /* mock_conn_create */
104 static void
105 mock_conn_rewind(sdb_conn_t *conn)
107         lseek(conn->fd, 0, SEEK_SET);
108 } /* mock_conn_rewind */
110 static void
111 mock_conn_truncate(sdb_conn_t *conn)
113         lseek(conn->fd, 0, SEEK_SET);
114         ftruncate(conn->fd, 0);
115 } /* mock_conn_truncate */
117 static int
118 mock_unixsock_listener(char *sock_path)
120         struct sockaddr_un sa;
121         char *filename;
122         int fd, status;
124         filename = tmpnam(sock_path);
125         fail_unless(filename != NULL,
126                         "INTERNAL ERROR: tmpnam() = NULL; expected: a string");
128         fd = socket(AF_UNIX, SOCK_STREAM, 0);
129         fail_unless(fd >= 0,
130                         "INTERNAL ERROR: socket() = %d; expected: >=0", fd);
132         memset(&sa, 0, sizeof(sa));
133         sa.sun_family = AF_UNIX;
134         strncpy(sa.sun_path, filename, sizeof(sa.sun_path));
136         status = bind(fd, (struct sockaddr *)&sa, sizeof(sa));
137         fail_unless(status == 0,
138                         "INTERNAL ERROR: bind() = %d; expected: 0", status);
139         status = listen(fd, 32);
140         fail_unless(status == 0,
141                         "INTERNAL ERROR: listen() = %d; expected: 0", status);
143         return fd;
144 } /* mock_unixsock */
146 static void *
147 mock_client(void *arg)
149         char *socket_path = arg;
151         struct sockaddr_un sa;
152         int fd, check;
154         fd = socket(AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
155         fail_unless(fd >= 0,
156                         "INTERNAL ERROR: socket() = %d; expected: >= 0", fd);
158         memset(&sa, 0, sizeof(sa));
159         sa.sun_family = AF_UNIX;
160         strncpy(sa.sun_path, socket_path, sizeof(sa.sun_path));
162         check = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
163         fail_unless(check == 0,
164                         "INTERNAL ERROR: connect() = %d; expected: 0", check);
166         close(fd);
167         return NULL;
168 } /* mock_client */
170 /*
171  * tests
172  */
174 START_TEST(test_conn_accept)
176         char socket_path[L_tmpnam];
177         int fd, check;
179         sdb_conn_t *conn;
181         pthread_t thr;
183         conn = sdb_connection_accept(-1);
184         fail_unless(conn == NULL,
185                         "sdb_connection_accept(-1) = %p; expected: NULL", conn);
187         memset(&socket_path, 0, sizeof(socket_path));
188         fd = mock_unixsock_listener(socket_path);
189         check = pthread_create(&thr, /* attr = */ NULL, mock_client, socket_path);
190         fail_unless(check == 0,
191                         "INTERNAL ERROR: pthread_create() = %i; expected: 0", check);
193         conn = sdb_connection_accept(fd);
194         fail_unless(conn != NULL,
195                         "sdb_connection_accept(%d) = %p; expected: <conn>", fd, conn);
197         unlink(socket_path);
198         sdb_connection_close(conn);
199         pthread_join(thr, NULL);
201 END_TEST
203 START_TEST(test_conn_setup)
205         sdb_conn_t *conn = mock_conn_create();
207         struct {
208                 uint32_t code;
209                 const char *msg;
210                 const char *err;
211         } golden_data[] = {
212                 { UINT32_MAX,         NULL,       NULL },
213                 { CONNECTION_IDLE,    "fakedata", NULL },
214                 { CONNECTION_PING,    NULL,       "Authentication required" },
215                 { CONNECTION_STARTUP, "fakeuser", NULL },
216                 { CONNECTION_PING,    NULL,       NULL },
217                 { CONNECTION_IDLE,    NULL,       NULL },
218                 { CONNECTION_PING,    "fakedata", NULL },
219                 { CONNECTION_IDLE,    NULL,       NULL },
220         };
222         size_t i;
224         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
225                 ssize_t check, expected = 0;
227                 mock_conn_truncate(conn);
229                 if (golden_data[i].code != UINT32_MAX) {
230                         expected = 2 * sizeof(uint32_t)
231                                 + (golden_data[i].msg ? strlen(golden_data[i].msg) : 0);
233                         check = sdb_connection_send(conn, golden_data[i].code,
234                                         (uint32_t)(golden_data[i].msg
235                                                 ? strlen(golden_data[i].msg) : 0),
236                                         golden_data[i].msg);
237                         fail_unless(check == expected,
238                                         "sdb_connection_send(%d, %s) = %zi; expected: %zi",
239                                         golden_data[i].code,
240                                         golden_data[i].msg ? golden_data[i].msg : "<null>",
241                                         check, expected);
242                 }
244                 mock_conn_rewind(conn);
245                 check = sdb_connection_read(conn);
246                 fail_unless(check == expected,
247                                 "sdb_connection_read() = %zi; expected: %zi",
248                                 check, expected);
250                 fail_unless(sdb_strbuf_len(conn->buf) == 0,
251                                 "sdb_connection_read() left %zu bytes in the buffer; "
252                                 "expected: 0", sdb_strbuf_len(conn->buf));
254                 if (golden_data[i].err) {
255                         const char *err = sdb_strbuf_string(conn->errbuf);
256                         fail_unless(strcmp(err, golden_data[i].err) == 0,
257                                         "sdb_connection_read(): got error '%s'; "
258                                         "expected: '%s'", err, golden_data[i].err);
259                 }
260                 else
261                         fail_unless(sdb_strbuf_len(conn->errbuf) == 0,
262                                         "sdb_connection_read() left %zu bytes in the error "
263                                         "buffer; expected: 0", sdb_strbuf_len(conn->errbuf));
264         }
266         mock_conn_destroy(conn);
268 END_TEST
270 Suite *
271 fe_conn_suite(void)
273         Suite *s = suite_create("frontend::connection");
274         TCase *tc;
276         tc = tcase_create("core");
277         tcase_add_test(tc, test_conn_accept);
278         tcase_add_test(tc, test_conn_setup);
279         suite_add_tcase(s, tc);
281         return s;
282 } /* fe_conn_suite */
284 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */