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 } golden_data[] = {
46 /* empty commands */
47 { NULL, -1, -1 },
48 { "", -1, 0 },
49 { ";", -1, 0 },
50 { ";;", -1, 0 },
52 /* valid commands */
53 { "LIST", -1, 1 },
54 { "LIST -- comment", -1, 1 },
55 { "LIST;", -1, 1 },
56 { "LIST; INVALID", 5, 1 },
58 /* comments */
59 { "/* some comment */", -1, 0 },
60 { "-- another comment", -1, 0 },
62 /* syntax errors */
63 { "INVALID", -1, -1 },
64 { "LIST; INVALID", 8, -1 },
65 { "/* some incomplete", -1, -1 },
66 };
68 size_t i;
69 sdb_llist_t *check;
71 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
72 _Bool ok;
74 check = sdb_fe_parse(golden_data[i].query, golden_data[i].len);
75 if (golden_data[i].expected < 0)
76 ok = check == 0;
77 else
78 ok = sdb_llist_len(check) == (size_t)golden_data[i].expected;
80 fail_unless(ok, "sdb_fe_parse(%s) = %p (len: %zu); expected: %d",
81 golden_data[i].query, check, sdb_llist_len(check),
82 golden_data[i].expected);
84 if (! check)
85 continue;
87 if ((! strcmp(golden_data[i].query, "LIST"))
88 || (! strcmp(golden_data[i].query, "LIST;"))) {
89 sdb_object_t *obj = sdb_llist_get(check, 0);
90 fail_unless(SDB_CONN_NODE(obj)->cmd == CONNECTION_LIST,
91 "sdb_fe_parse(LIST)->cmd = %i; expected: %d "
92 "(CONNECTION_LIST)", SDB_CONN_NODE(obj)->cmd,
93 CONNECTION_LIST);
94 }
96 sdb_llist_destroy(check);
97 }
98 }
99 END_TEST
101 START_TEST(test_parse_matcher)
102 {
103 struct {
104 const char *expr;
105 int len;
106 int expected;
107 } golden_data[] = {
108 /* empty expressions */
109 { NULL, -1, -1 },
110 { "", -1, -1 },
112 /* valid expressions */
113 { "localhost", -1, 0 },
114 { "localhost -- foo", -1, 0 },
115 { "localhost <garbage>", 9, 0 },
117 /* syntax errors */
118 { "LIST", -1, -1 },
119 { "foo &^ bar", -1, -1 },
120 };
122 size_t i;
123 sdb_store_matcher_t *m;
125 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
126 _Bool ok;
128 m = sdb_fe_parse_matcher(golden_data[i].expr, golden_data[i].len);
129 if (golden_data[i].expected < 0)
130 ok = m == NULL;
131 else
132 ok = m != NULL;
134 fail_unless(ok, "sdb_fe_parse_matcher(%s) = %p; expected: %s",
135 golden_data[i].expr, m, (golden_data[i].expected < 0)
136 ? "NULL" : "<matcher>");
138 sdb_object_deref(SDB_OBJ(m));
139 }
140 }
141 END_TEST
143 Suite *
144 fe_parser_suite(void)
145 {
146 Suite *s = suite_create("frontend::parser");
147 TCase *tc;
149 tc = tcase_create("core");
150 tcase_add_test(tc, test_parse);
151 tcase_add_test(tc, test_parse_matcher);
152 suite_add_tcase(s, tc);
154 return s;
155 } /* util_parser_suite */
157 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */