Debian Codegraph @

tests/api/expr.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2008-2016 The ProFTPD Project team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, The ProFTPD Project team and other respective
20 * copyright holders give permission to link this program with OpenSSL, and
21 * distribute the resulting executable, without including the source code for
22 * OpenSSL in the source distribution.
23 */
24
25 /* Expression API tests */
26
27 #include "tests.h"
28
29 static pool *p = NULL;
30
31 static void set_up(void) {
32 if (p == NULL) {
33 p = permanent_pool = make_sub_pool(NULL);
34 }
35
36 main_server = pcalloc(p, sizeof(server_rec));
37 main_server->pool = p;
38 }
39
40 static void tear_down(void) {
41 if (p) {
42 destroy_pool(p);
43 p = NULL;
44 permanent_pool = NULL;
45 }
46 }
47
48 START_TEST (expr_create_test) {
49 array_header *res;
50 unsigned int expr_argc = 2;
51 char *expr_argv[4] = { NULL, NULL, NULL, NULL };
52 char **elts;
53
54 res = pr_expr_create(NULL, NULL, NULL);
55 ck_assert_msg(res == NULL, "Failed to handle null arguments");
56 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
57
58 res = pr_expr_create(p, NULL, NULL);
59 ck_assert_msg(res == NULL, "Failed to handle null count, argv arguments");
60 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
61
62 res = pr_expr_create(p, &expr_argc, NULL);
63 ck_assert_msg(res == NULL, "Failed to handle null argv argument");
64 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
65
66 res = pr_expr_create(p, NULL, expr_argv);
67 ck_assert_msg(res == NULL, "Failed to handle null argc argument");
68 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
69
70 res = pr_expr_create(NULL, &expr_argc, expr_argv);
71 ck_assert_msg(res == NULL, "Failed to handle null pool argument");
72 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
73
74 res = pr_expr_create(p, &expr_argc, expr_argv);
75 ck_assert_msg(res == NULL, "Failed to handle empty argv argument");
76 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
77
78 expr_argc = 0;
79 expr_argv[0] = "foo";
80 expr_argv[1] = "bar";
81
82 res = pr_expr_create(p, &expr_argc, expr_argv);
83 ck_assert_msg(res != NULL, "Failed to create expr: %s", strerror(errno));
84 ck_assert_msg(expr_argc == 0, "Failed to set negative argc to zero");
85 ck_assert_msg(res->nelts == expr_argc, "Expected %d, got %d", expr_argc,
86 res->nelts);
87
88 expr_argc = 1;
89 expr_argv[0] = pstrdup(p, "foo");
90 expr_argv[1] = NULL;
91
92 res = pr_expr_create(p, &expr_argc, expr_argv);
93 ck_assert_msg(res != NULL, "Failed to create expr: %s", strerror(errno));
94 ck_assert_msg(res->nelts == expr_argc, "Expected %d, got %d", expr_argc,
95 res->nelts);
96
97 elts = res->elts;
98 ck_assert_msg(elts[0] == NULL, "Expected null, got '%s'", elts[0]);
99
100 expr_argc = 2;
101 expr_argv[0] = pstrdup(p, "foo");
102 expr_argv[1] = pstrdup(p, "bar,baz,quxx");
103
104 res = pr_expr_create(p, &expr_argc, expr_argv);
105 ck_assert_msg(res != NULL, "Failed to create expr: %s", strerror(errno));
106 ck_assert_msg(res->nelts == 3, "Expected %d, got %d", 3, res->nelts);
107
108 elts = res->elts;
109 ck_assert_msg(strcmp(elts[0], "bar") == 0, "Expected '%s', got '%s'",
110 "bar", elts[0]);
111
112 elts = res->elts;
113 ck_assert_msg(strcmp(elts[1], "baz") == 0, "Expected '%s', got '%s'",
114 "baz", elts[1]);
115
116 elts = res->elts;
117 ck_assert_msg(strcmp(elts[2], "quxx") == 0, "Expected '%s', got '%s'",
118 "quxx", elts[2]);
119
120 expr_argc = 3;
121 expr_argv[0] = pstrdup(p, "foo");
122 expr_argv[1] = pstrdup(p, "bar,baz,quxx");
123 expr_argv[2] = pstrdup(p, "alef");
124
125 res = pr_expr_create(p, &expr_argc, expr_argv);
126 ck_assert_msg(res != NULL, "Failed to create expr: %s", strerror(errno));
127 ck_assert_msg(res->nelts == 4, "Expected %d, got %d", 4, res->nelts);
128
129 elts = res->elts;
130 ck_assert_msg(strcmp(elts[0], "bar") == 0, "Expected '%s', got '%s'",
131 "bar", elts[0]);
132
133 elts = res->elts;
134 ck_assert_msg(strcmp(elts[1], "baz") == 0, "Expected '%s', got '%s'",
135 "baz", elts[1]);
136
137 elts = res->elts;
138 ck_assert_msg(strcmp(elts[2], "quxx") == 0, "Expected '%s', got '%s'",
139 "quxx", elts[2]);
140
141 elts = res->elts;
142 ck_assert_msg(strcmp(elts[3], "alef") == 0, "Expected '%s', got '%s'",
143 "alef", elts[3]);
144 }
145 END_TEST
146
147 START_TEST (expr_eval_class_and_test) {
148 pr_netacl_t *acl;
149 char *names1[3] = { "foo", "bar", NULL }, *names2[2] = { "test", NULL },
150 *names3[2] = { "!baz", NULL };
151 int res;
152
153 res = pr_expr_eval_class_and(NULL);
154 ck_assert_msg(res == -1, "Failed to handle null argument");
155 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
156
157 session.conn_class = NULL;
158
159 res = pr_expr_eval_class_and(names1);
160 ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
161
162 init_netaddr();
163 init_class();
164
165 res = pr_class_open(p, "test");
166 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
167
168 acl = pr_netacl_create(p, "all");
169 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
170
171 res = pr_class_add_acl(acl);
172 ck_assert_msg(res == 0, "Failed to add ACL to class: %s", strerror(errno));
173
174 res = pr_class_close();
175 ck_assert_msg(res == 0, "Failed to close class: %s", strerror(errno));
176
177 session.conn_class = pr_class_find("test");
178 ck_assert_msg(session.conn_class != NULL, "Failed to find 'test' class: %s",
179 strerror(errno));
180
181 res = pr_expr_eval_class_and(names1);
182 ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
183
184 res = pr_expr_eval_class_and(names2);
185 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
186
187 res = pr_expr_eval_class_and(names3);
188 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
189 }
190 END_TEST
191
192 START_TEST (expr_eval_class_or_test) {
193 pr_netacl_t *acl;
194 char *names1[3] = { "foo", "test", NULL }, *names2[2] = { "test", NULL },
195 *names3[2] = { "!baz", NULL }, *names4[2] = { "foo", NULL };
196 int res;
197
198 res = pr_expr_eval_class_or(NULL);
199 ck_assert_msg(res == -1, "Failed to handle null argument");
200 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
201
202 session.conn_class = NULL;
203
204 res = pr_expr_eval_class_or(names1);
205 ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
206
207 init_netaddr();
208 init_class();
209
210 res = pr_class_open(p, "test");
211 ck_assert_msg(res == 0, "Failed to open class: %s", strerror(errno));
212
213 acl = pr_netacl_create(p, "all");
214 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
215
216 res = pr_class_add_acl(acl);
217 ck_assert_msg(res == 0, "Failed to add ACL to class: %s", strerror(errno));
218
219 res = pr_class_close();
220 ck_assert_msg(res == 0, "Failed to close class: %s", strerror(errno));
221
222 session.conn_class = pr_class_find("test");
223 ck_assert_msg(session.conn_class != NULL, "Failed to find 'test' class: %s",
224 strerror(errno));
225
226 res = pr_expr_eval_class_or(names1);
227 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
228
229 res = pr_expr_eval_class_or(names2);
230 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
231
232 res = pr_expr_eval_class_or(names3);
233 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
234
235 res = pr_expr_eval_class_or(names4);
236 ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
237 }
238 END_TEST
239
240 START_TEST (expr_eval_group_and_test) {
241 char *names1[3] = { "foo", "bar", NULL }, *names2[2] = { "test", NULL },
242 *names3[2] = { "!baz", NULL };
243 int res;
244
245 res = pr_expr_eval_group_and(NULL);
246 ck_assert_msg(res == -1, "Failed to handle null argument");
247 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
248
249 session.group = NULL;
250 session.groups = NULL;
251
252 res = pr_expr_eval_group_and(names1);
253 ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
254
255 session.group = "test";
256
257 res = pr_expr_eval_group_and(names1);
258 ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
259
260 res = pr_expr_eval_group_and(names2);
261 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
262
263 res = pr_expr_eval_group_and(names3);
264 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
265
266 session.group = NULL;
267 session.groups = make_array(p, 2, sizeof(char *));
268 *((char **) push_array(session.groups)) = "test";
269 *((char **) push_array(session.groups)) = NULL;
270 *((char **) push_array(session.groups)) = "spank";
271
272 res = pr_expr_eval_group_and(names1);
273 ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
274
275 res = pr_expr_eval_group_and(names2);
276 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
277
278 res = pr_expr_eval_group_and(names3);
279 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
280 }
281 END_TEST
282
283 START_TEST (expr_eval_group_or_test) {
284 char *names1[3] = { "foo", "test", NULL }, *names2[2] = { "test", NULL },
285 *names3[2] = { "!baz", NULL }, *names4[2] = { "foo", NULL };
286 int res;
287
288 res = pr_expr_eval_group_or(NULL);
289 ck_assert_msg(res == -1, "Failed to handle null argument");
290 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
291
292 session.group = NULL;
293 session.groups = NULL;
294
295 res = pr_expr_eval_group_or(names1);
296 ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
297
298 session.group = "test";
299
300 res = pr_expr_eval_group_or(names1);
301 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
302
303 res = pr_expr_eval_group_or(names2);
304 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
305
306 res = pr_expr_eval_group_or(names3);
307 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
308
309 res = pr_expr_eval_group_or(names4);
310 ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
311
312 session.group = NULL;
313 session.groups = make_array(p, 1, sizeof(char *));
314 *((char **) push_array(session.groups)) = "test";
315
316 res = pr_expr_eval_group_or(names1);
317 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
318
319 res = pr_expr_eval_group_or(names2);
320 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
321
322 res = pr_expr_eval_group_or(names3);
323 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
324
325 res = pr_expr_eval_group_or(names4);
326 ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
327 }
328 END_TEST
329
330 START_TEST (expr_eval_user_and_test) {
331 char *names1[3] = { "foo", "bar", NULL }, *names2[2] = { "test", NULL },
332 *names3[2] = { "!baz", NULL };
333 int res;
334
335 res = pr_expr_eval_user_and(NULL);
336 ck_assert_msg(res == -1, "Failed to handle null argument");
337 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
338
339 session.user = NULL;
340
341 res = pr_expr_eval_user_and(names1);
342 ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
343
344 session.user = "test";
345
346 res = pr_expr_eval_user_and(names1);
347 ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
348
349 res = pr_expr_eval_user_and(names2);
350 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
351
352 res = pr_expr_eval_user_and(names3);
353 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
354 }
355 END_TEST
356
357 START_TEST (expr_eval_user_or_test) {
358 char *names1[3] = { "foo", "test", NULL }, *names2[2] = { "test", NULL },
359 *names3[2] = { "!baz", NULL }, *names4[2] = { "foo", NULL };
360 int res;
361
362 res = pr_expr_eval_user_or(NULL);
363 ck_assert_msg(res == -1, "Failed to handle null argument");
364 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
365
366 session.user = NULL;
367
368 res = pr_expr_eval_user_or(names1);
369 ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
370
371 session.user = "test";
372
373 res = pr_expr_eval_user_or(names1);
374 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
375
376 res = pr_expr_eval_user_or(names2);
377 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
378
379 res = pr_expr_eval_user_or(names3);
380 ck_assert_msg(res == TRUE, "Expected TRUE, got FALSE");
381
382 res = pr_expr_eval_user_or(names4);
383 ck_assert_msg(res == FALSE, "Expected FALSE, got TRUE");
384 }
385 END_TEST
386
387 Suite *tests_get_expr_suite(void) {
388 Suite *suite;
389 TCase *testcase;
390
391 suite = suite_create("expr");
392
393 testcase = tcase_create("base");
394 tcase_add_checked_fixture(testcase, set_up, tear_down);
395
396 tcase_add_test(testcase, expr_create_test);
397 tcase_add_test(testcase, expr_eval_class_and_test);
398 tcase_add_test(testcase, expr_eval_class_or_test);
399 tcase_add_test(testcase, expr_eval_group_and_test);
400 tcase_add_test(testcase, expr_eval_group_or_test);
401 tcase_add_test(testcase, expr_eval_user_and_test);
402 tcase_add_test(testcase, expr_eval_user_or_test);
403
404 suite_add_tcase(suite, testcase);
405 return suite;
406 }