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 int 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 /* comments */
61 { "/* some comment */", -1, 0, 0 },
62 { "-- another comment", -1, 0, 0 },
64 /* syntax errors */
65 { "INVALID", -1, -1, 0 },
66 { "FETCH host", -1, -1, 0 },
67 { "LIST; INVALID", 8, -1, 0 },
68 { "/* some incomplete", -1, -1, 0 },
69 };
71 size_t i;
72 sdb_llist_t *check;
74 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
75 sdb_object_t *obj;
76 _Bool ok;
78 check = sdb_fe_parse(golden_data[i].query, golden_data[i].len);
79 if (golden_data[i].expected < 0)
80 ok = check == 0;
81 else
82 ok = sdb_llist_len(check) == (size_t)golden_data[i].expected;
84 fail_unless(ok, "sdb_fe_parse(%s) = %p (len: %zu); expected: %d",
85 golden_data[i].query, check, sdb_llist_len(check),
86 golden_data[i].expected);
88 if (! check)
89 continue;
91 if ((! golden_data[i].expected_cmd)
92 || (golden_data[i].expected <= 0)) {
93 sdb_llist_destroy(check);
94 continue;
95 }
97 obj = sdb_llist_get(check, 0);
98 fail_unless(SDB_CONN_NODE(obj)->cmd == golden_data[i].expected_cmd,
99 "sdb_fe_parse(%s)->cmd = %i; expected: %d",
100 golden_data[i].query, SDB_CONN_NODE(obj)->cmd,
101 golden_data[i].expected_cmd);
102 sdb_object_deref(obj);
103 sdb_llist_destroy(check);
104 }
105 }
106 END_TEST
108 START_TEST(test_parse_matcher)
109 {
110 struct {
111 const char *expr;
112 int len;
113 int expected;
114 } golden_data[] = {
115 /* empty expressions */
116 { NULL, -1, -1 },
117 { "", -1, -1 },
119 /* valid expressions */
120 { "'localhost'", -1, 0 },
121 { "'localhost' -- foo", -1, 0 },
122 { "'host' <garbage>", 6, 0 },
124 /* syntax errors */
125 { "LIST", -1, -1 },
126 { "foo &^ bar", -1, -1 },
127 };
129 size_t i;
130 sdb_store_matcher_t *m;
132 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
133 _Bool ok;
135 m = sdb_fe_parse_matcher(golden_data[i].expr, golden_data[i].len);
136 if (golden_data[i].expected < 0)
137 ok = m == NULL;
138 else
139 ok = m != NULL;
141 fail_unless(ok, "sdb_fe_parse_matcher(%s) = %p; expected: %s",
142 golden_data[i].expr, m, (golden_data[i].expected < 0)
143 ? "NULL" : "<matcher>");
145 sdb_object_deref(SDB_OBJ(m));
146 }
147 }
148 END_TEST
150 Suite *
151 fe_parser_suite(void)
152 {
153 Suite *s = suite_create("frontend::parser");
154 TCase *tc;
156 tc = tcase_create("core");
157 tcase_add_test(tc, test_parse);
158 tcase_add_test(tc, test_parse_matcher);
159 suite_add_tcase(s, tc);
161 return s;
162 } /* util_parser_suite */
164 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */