Code

9936511c87200a2e528ec7584be52a36017391ea
[sysdb.git] / t / frontend / connection_test.c
1 /*
2  * SysDB - t/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 <sys/types.h>
46 /*
47  * private helper functions
48  */
50 static void
51 mock_conn_destroy(sdb_conn_t *conn)
52 {
53         if (SDB_OBJ(conn)->name)
54                 free(SDB_OBJ(conn)->name);
55         sdb_strbuf_destroy(conn->buf);
56         sdb_strbuf_destroy(conn->errbuf);
57         if (conn->fd >= 0)
58                 close(conn->fd);
59         free(conn);
60 } /* mock_conn_destroy */
62 static sdb_conn_t *
63 mock_conn_create(void)
64 {
65         sdb_conn_t *conn;
67         char tmp_file[] = "connection_test_socket.XXXXXX";
69         conn = calloc(1, sizeof(*conn));
70         if (! conn) {
71                 fail("INTERNAL ERROR: failed to allocate connection object");
72                 return NULL;
73         }
75         SDB_OBJ(conn)->name = strdup("mock_connection");
76         SDB_OBJ(conn)->ref_cnt = 1;
78         conn->buf = sdb_strbuf_create(0);
79         conn->errbuf = sdb_strbuf_create(0);
80         if ((! conn->buf) || (! conn->errbuf)) {
81                 mock_conn_destroy(conn);
82                 fail("INTERNAL ERROR: failed to allocate connection object");
83                 return NULL;
84         }
86         conn->fd = mkstemp(tmp_file);
87         if (conn->fd < 0) {
88                 mock_conn_destroy(conn);
89                 fail("INTERNAL ERROR: failed to allocate connection object");
90                 return NULL;
91         }
93         unlink(tmp_file);
95         conn->cmd = CONNECTION_IDLE;
96         conn->cmd_len = 0;
97         return conn;
98 } /* mock_conn_create */
100 static void
101 mock_conn_rewind(sdb_conn_t *conn)
103         lseek(conn->fd, 0, SEEK_SET);
104 } /* mock_conn_rewind */
106 static void
107 mock_conn_truncate(sdb_conn_t *conn)
109         lseek(conn->fd, 0, SEEK_SET);
110         ftruncate(conn->fd, 0);
111 } /* mock_conn_truncate */
113 /*
114  * tests
115  */
117 START_TEST(test_conn_setup)
119         sdb_conn_t *conn = mock_conn_create();
121         struct {
122                 uint32_t code;
123                 const char *msg;
124                 const char *err;
125         } golden_data[] = {
126                 { UINT32_MAX,         NULL,       NULL },
127                 { CONNECTION_IDLE,    "fakedata", NULL },
128                 { CONNECTION_PING,    NULL,       "Authentication required" },
129                 { CONNECTION_STARTUP, "fakeuser", NULL },
130                 { CONNECTION_PING,    NULL,       NULL },
131                 { CONNECTION_IDLE,    NULL,       NULL },
132                 { CONNECTION_PING,    "fakedata", NULL },
133                 { CONNECTION_IDLE,    NULL,       NULL },
134         };
136         size_t i;
138         for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
139                 ssize_t check, expected = 0;
141                 mock_conn_truncate(conn);
143                 if (golden_data[i].code != UINT32_MAX) {
144                         expected = 2 * sizeof(uint32_t)
145                                 + (golden_data[i].msg ? strlen(golden_data[i].msg) : 0);
147                         check = sdb_connection_send(conn, golden_data[i].code,
148                                         (uint32_t)(golden_data[i].msg
149                                                 ? strlen(golden_data[i].msg) : 0),
150                                         golden_data[i].msg);
151                         fail_unless(check == expected,
152                                         "sdb_connection_send(%d, %s) = %zi; expected: %zi",
153                                         golden_data[i].code,
154                                         golden_data[i].msg ? golden_data[i].msg : "<null>",
155                                         check, expected);
156                 }
158                 mock_conn_rewind(conn);
159                 check = sdb_connection_read(conn);
160                 fail_unless(check == expected,
161                                 "sdb_connection_read() = %zi; expected: %zi",
162                                 check, expected);
164                 fail_unless(sdb_strbuf_len(conn->buf) == 0,
165                                 "sdb_connection_read() left %zu bytes in the buffer; "
166                                 "expected: 0", sdb_strbuf_len(conn->buf));
168                 if (golden_data[i].err) {
169                         const char *err = sdb_strbuf_string(conn->errbuf);
170                         fail_unless(strcmp(err, golden_data[i].err) == 0,
171                                         "sdb_connection_read(): got error '%s'; "
172                                         "expected: '%s'", err, golden_data[i].err);
173                 }
174                 else
175                         fail_unless(sdb_strbuf_len(conn->errbuf) == 0,
176                                         "sdb_connection_read() left %zu bytes in the error "
177                                         "buffer; expected: 0", sdb_strbuf_len(conn->errbuf));
178         }
180         mock_conn_destroy(conn);
182 END_TEST
184 Suite *
185 fe_conn_suite(void)
187         Suite *s = suite_create("frontend::connection");
188         TCase *tc;
190         tc = tcase_create("core");
191         tcase_add_test(tc, test_conn_setup);
192         suite_add_tcase(s, tc);
194         return s;
195 } /* fe_conn_suite */
197 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */