1 /*
2 * SysDB - t/frontend/parser_test.c
3 * Copyright (C) 2013 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 #include "frontend/connection.h"
29 #include "frontend/parser.h"
30 #include "core/object.h"
31 #include "libsysdb_test.h"
33 #include <check.h>
35 /*
36 * tests
37 */
39 START_TEST(test_parse)
40 {
41 struct {
42 const char *query;
43 int len;
44 int expected;
45 sdb_conn_state_t expected_cmd;
46 } golden_data[] = {
47 /* empty commands */
48 { NULL, -1, -1, 0 },
49 { "", -1, 0, 0 },
50 { ";", -1, 0, 0 },
51 { ";;", -1, 0, 0 },
53 /* valid commands */
54 { "FETCH 'host'", -1, 1, CONNECTION_FETCH },
55 { "LIST", -1, 1, CONNECTION_LIST },
56 { "LIST -- comment", -1, 1, CONNECTION_LIST },
57 { "LIST;", -1, 1, CONNECTION_LIST },
58 { "LIST; INVALID", 5, 1, CONNECTION_LIST },
60 { "LOOKUP hosts "
61 "WHERE 'host'", -1, 1, CONNECTION_LOOKUP },
63 /* comments */
64 { "/* some comment */", -1, 0, 0 },
65 { "-- another comment", -1, 0, 0 },
67 /* syntax errors */
68 { "INVALID", -1, -1, 0 },
69 { "FETCH host", -1, -1, 0 },
70 { "LIST; INVALID", 8, -1, 0 },
71 { "/* some incomplete", -1, -1, 0 },
73 { "LOOKUP hosts", -1, -1, 0 },
74 { "LOOKUP invalid "
75 "WHERE 'host'", -1, -1, 0 },
76 };
78 size_t i;
79 sdb_llist_t *check;
81 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
82 sdb_object_t *obj;
83 _Bool ok;
85 check = sdb_fe_parse(golden_data[i].query, golden_data[i].len);
86 if (golden_data[i].expected < 0)
87 ok = check == 0;
88 else
89 ok = sdb_llist_len(check) == (size_t)golden_data[i].expected;
91 fail_unless(ok, "sdb_fe_parse(%s) = %p (len: %zu); expected: %d",
92 golden_data[i].query, check, sdb_llist_len(check),
93 golden_data[i].expected);
95 if (! check)
96 continue;
98 if ((! golden_data[i].expected_cmd)
99 || (golden_data[i].expected <= 0)) {
100 sdb_llist_destroy(check);
101 continue;
102 }
104 obj = sdb_llist_get(check, 0);
105 fail_unless(SDB_CONN_NODE(obj)->cmd == golden_data[i].expected_cmd,
106 "sdb_fe_parse(%s)->cmd = %i; expected: %d",
107 golden_data[i].query, SDB_CONN_NODE(obj)->cmd,
108 golden_data[i].expected_cmd);
109 sdb_object_deref(obj);
110 sdb_llist_destroy(check);
111 }
112 }
113 END_TEST
115 START_TEST(test_parse_matcher)
116 {
117 struct {
118 const char *expr;
119 int len;
120 int expected;
121 } golden_data[] = {
122 /* empty expressions */
123 { NULL, -1, -1 },
124 { "", -1, -1 },
126 /* valid expressions */
127 { "'localhost'", -1, 0 },
128 { "'localhost' -- foo", -1, 0 },
129 { "'host' <garbage>", 6, 0 },
131 /* syntax errors */
132 { "LIST", -1, -1 },
133 { "foo &^ bar", -1, -1 },
134 };
136 size_t i;
137 sdb_store_matcher_t *m;
139 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
140 _Bool ok;
142 m = sdb_fe_parse_matcher(golden_data[i].expr, golden_data[i].len);
143 if (golden_data[i].expected < 0)
144 ok = m == NULL;
145 else
146 ok = m != NULL;
148 fail_unless(ok, "sdb_fe_parse_matcher(%s) = %p; expected: %s",
149 golden_data[i].expr, m, (golden_data[i].expected < 0)
150 ? "NULL" : "<matcher>");
152 sdb_object_deref(SDB_OBJ(m));
153 }
154 }
155 END_TEST
157 Suite *
158 fe_parser_suite(void)
159 {
160 Suite *s = suite_create("frontend::parser");
161 TCase *tc;
163 tc = tcase_create("core");
164 tcase_add_test(tc, test_parse);
165 tcase_add_test(tc, test_parse_matcher);
166 suite_add_tcase(s, tc);
168 return s;
169 } /* util_parser_suite */
171 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */