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 sdb_object_deref(obj);
95 }
97 sdb_llist_destroy(check);
98 }
99 }
100 END_TEST
102 START_TEST(test_parse_matcher)
103 {
104 struct {
105 const char *expr;
106 int len;
107 int expected;
108 } golden_data[] = {
109 /* empty expressions */
110 { NULL, -1, -1 },
111 { "", -1, -1 },
113 /* valid expressions */
114 { "localhost", -1, 0 },
115 { "localhost -- foo", -1, 0 },
116 { "localhost <garbage>", 9, 0 },
118 /* syntax errors */
119 { "LIST", -1, -1 },
120 { "foo &^ bar", -1, -1 },
121 };
123 size_t i;
124 sdb_store_matcher_t *m;
126 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
127 _Bool ok;
129 m = sdb_fe_parse_matcher(golden_data[i].expr, golden_data[i].len);
130 if (golden_data[i].expected < 0)
131 ok = m == NULL;
132 else
133 ok = m != NULL;
135 fail_unless(ok, "sdb_fe_parse_matcher(%s) = %p; expected: %s",
136 golden_data[i].expr, m, (golden_data[i].expected < 0)
137 ? "NULL" : "<matcher>");
139 sdb_object_deref(SDB_OBJ(m));
140 }
141 }
142 END_TEST
144 Suite *
145 fe_parser_suite(void)
146 {
147 Suite *s = suite_create("frontend::parser");
148 TCase *tc;
150 tc = tcase_create("core");
151 tcase_add_test(tc, test_parse);
152 tcase_add_test(tc, test_parse_matcher);
153 suite_add_tcase(s, tc);
155 return s;
156 } /* util_parser_suite */
158 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */