Debian Codegraph @

tests/api/parser.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2014-2022 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 /* Parser API tests. */
26
27 #include "tests.h"
28
29 static pool *p = NULL;
30
31 static const char *config_path = "/tmp/prt-parser.conf";
32 static const char *config_path2 = "/tmp/prt-parser2.conf";
33 static const char *config_path3 = "/tmp/prt-parser3.d";
34 static const char *config_tmp_path = "/tmp/prt-parser.conf~";
35
36 static void set_up(void) {
37 (void) unlink(config_path);
38 (void) unlink(config_path2);
39 (void) rmdir(config_path3);
40 (void) unlink(config_tmp_path);
41
42 if (p == NULL) {
43 p = permanent_pool = make_sub_pool(NULL);
44 }
45
46 init_fs();
47 pr_fs_statcache_set_policy(PR_TUNABLE_FS_STATCACHE_SIZE,
48 PR_TUNABLE_FS_STATCACHE_MAX_AGE, 0);
49 init_stash();
50 modules_init();
51
52 if (getenv("TEST_VERBOSE") != NULL) {
53 pr_trace_set_levels("config", 1, 20);
54 }
55 }
56
57 static void tear_down(void) {
58 (void) pr_parser_cleanup();
59
60 (void) unlink(config_path);
61 (void) unlink(config_path2);
62 (void) rmdir(config_path3);
63 (void) unlink(config_tmp_path);
64
65 if (getenv("TEST_VERBOSE") != NULL) {
66 pr_trace_set_levels("config", 0, 0);
67 }
68
69 if (p) {
70 destroy_pool(p);
71 p = permanent_pool = NULL;
72 }
73 }
74
75 /* Tests */
76
77 START_TEST (parser_prepare_test) {
78 int res;
79 xaset_t *parsed_servers = NULL;
80
81 res = pr_parser_prepare(NULL, NULL);
82 ck_assert_msg(res == 0, "Failed to handle null arguments: %s", strerror(errno));
83
84 res = pr_parser_prepare(p, NULL);
85 ck_assert_msg(res == 0, "Failed to handle null parsed_servers: %s",
86 strerror(errno));
87
88 res = pr_parser_prepare(NULL, &parsed_servers);
89 ck_assert_msg(res == 0, "Failed to handle null pool: %s", strerror(errno));
90
91 (void) pr_parser_cleanup();
92 }
93 END_TEST
94
95 START_TEST (parser_cleanup_test) {
96 int res;
97 server_rec *ctx;
98
99 mark_point();
100 res = pr_parser_cleanup();
101 ck_assert_msg(res == 0, "Failed to handle unprepared parser: %s",
102 strerror(errno));
103
104 pr_parser_prepare(NULL, NULL);
105
106 mark_point();
107 ctx = pr_parser_server_ctxt_open("127.0.0.1");
108 ck_assert_msg(ctx != NULL, "Failed to open server context: %s",
109 strerror(errno));
110
111 mark_point();
112 res = pr_parser_cleanup();
113 ck_assert_msg(res < 0, "Failed to handle existing contexts");
114 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
115 strerror(errno), errno);
116
117 mark_point();
118 (void) pr_parser_server_ctxt_close();
119 res = pr_parser_cleanup();
120 ck_assert_msg(res == 0, "Failed to cleanup parser: %s", strerror(errno));
121 }
122 END_TEST
123
124 START_TEST (parser_server_ctxt_test) {
125 server_rec *ctx, *res;
126
127 ctx = pr_parser_server_ctxt_get();
128 ck_assert_msg(ctx == NULL, "Found server context unexpectedly");
129 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
130 strerror(errno), errno);
131
132 pr_parser_prepare(p, NULL);
133
134 mark_point();
135 res = pr_parser_server_ctxt_close();
136 ck_assert_msg(res == NULL, "Closed server context unexpectedly");
137 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
138 strerror(errno), errno);
139
140 mark_point();
141 res = pr_parser_server_ctxt_open("127.0.0.1");
142 ck_assert_msg(res != NULL, "Failed to open server context: %s",
143 strerror(errno));
144
145 mark_point();
146 ctx = pr_parser_server_ctxt_get();
147 ck_assert_msg(ctx != NULL, "Failed to get current server context: %s",
148 strerror(errno));
149 ck_assert_msg(ctx == res, "Expected server context %p, got %p", res, ctx);
150
151 mark_point();
152 (void) pr_parser_server_ctxt_close();
153 (void) pr_parser_cleanup();
154 }
155 END_TEST
156
157 START_TEST (parser_server_ctxt_push_test) {
158 int res;
159 server_rec *ctx, *ctx2;
160
161 res = pr_parser_server_ctxt_push(NULL);
162 ck_assert_msg(res < 0, "Failed to handle null argument");
163 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
164 strerror(errno), errno);
165
166 ctx = pcalloc(p, sizeof(server_rec));
167
168 mark_point();
169 res = pr_parser_server_ctxt_push(ctx);
170 ck_assert_msg(res < 0, "Failed to handle unprepared parser");
171 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
172 strerror(errno), errno);
173
174 pr_parser_prepare(p, NULL);
175
176 mark_point();
177 res = pr_parser_server_ctxt_push(ctx);
178 ck_assert_msg(res == 0, "Failed to push server rec: %s", strerror(errno));
179
180 mark_point();
181 ctx2 = pr_parser_server_ctxt_get();
182 ck_assert_msg(ctx2 != NULL, "Failed to get current server context: %s",
183 strerror(errno));
184 ck_assert_msg(ctx2 == ctx, "Expected server context %p, got %p", ctx, ctx2);
185
186 (void) pr_parser_server_ctxt_close();
187 (void) pr_parser_cleanup();
188 }
189 END_TEST
190
191 START_TEST (parser_config_ctxt_test) {
192 int is_empty = FALSE;
193 config_rec *ctx, *res;
194
195 ctx = pr_parser_config_ctxt_get();
196 ck_assert_msg(ctx == NULL, "Found config context unexpectedly");
197 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
198 strerror(errno), errno);
199
200 pr_parser_prepare(p, NULL);
201 pr_parser_server_ctxt_open("127.0.0.1");
202
203 res = pr_parser_config_ctxt_open(NULL);
204 ck_assert_msg(res == NULL, "Failed to handle null arguments");
205 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
206 strerror(errno), errno);
207
208 mark_point();
209 res = pr_parser_config_ctxt_open("<TestSuite>");
210 ck_assert_msg(res != NULL, "Failed to open config context: %s",
211 strerror(errno));
212
213 mark_point();
214 ctx = pr_parser_config_ctxt_get();
215 ck_assert_msg(ctx != NULL, "Failed to get current config context: %s",
216 strerror(errno));
217 ck_assert_msg(ctx == res, "Expected config context %p, got %p", res, ctx);
218
219 mark_point();
220 (void) pr_parser_config_ctxt_close(&is_empty);
221 ck_assert_msg(is_empty == TRUE, "Expected config context to be empty");
222
223 mark_point();
224 res = pr_parser_config_ctxt_open("<Global>");
225 ck_assert_msg(res != NULL, "Failed to open config context: %s",
226 strerror(errno));
227 (void) pr_parser_config_ctxt_close(&is_empty);
228 ck_assert_msg(is_empty == TRUE, "Expected config context to be empty");
229
230 (void) pr_parser_server_ctxt_close();
231 (void) pr_parser_cleanup();
232 }
233 END_TEST
234
235 START_TEST (parser_config_ctxt_push_test) {
236 int res;
237 config_rec *ctx, *ctx2;
238
239 res = pr_parser_config_ctxt_push(NULL);
240 ck_assert_msg(res < 0, "Failed to handle null argument");
241 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
242 strerror(errno), errno);
243
244 ctx = pcalloc(p, sizeof(config_rec));
245
246 mark_point();
247 res = pr_parser_config_ctxt_push(ctx);
248 ck_assert_msg(res < 0, "Failed to handle unprepared parser");
249 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
250 strerror(errno), errno);
251
252 pr_parser_prepare(p, NULL);
253
254 mark_point();
255 res = pr_parser_config_ctxt_push(ctx);
256 ck_assert_msg(res == 0, "Failed to push config rec: %s", strerror(errno));
257
258 mark_point();
259 ctx2 = pr_parser_config_ctxt_get();
260 ck_assert_msg(ctx2 != NULL, "Failed to get current config context: %s",
261 strerror(errno));
262 ck_assert_msg(ctx2 == ctx, "Expected config context %p, got %p", ctx, ctx2);
263
264 (void) pr_parser_config_ctxt_close(NULL);
265 (void) pr_parser_cleanup();
266 }
267 END_TEST
268
269 START_TEST (parser_get_lineno_test) {
270 unsigned int res;
271
272 res = pr_parser_get_lineno();
273 ck_assert_msg(res == 0, "Expected 0, got %u", res);
274
275 res = pr_parser_get_lineno();
276 ck_assert_msg(res == 0, "Expected 0, got %u", res);
277 }
278 END_TEST
279
280 START_TEST (parser_read_line_test) {
281 char *res;
282
283 res = pr_parser_read_line(NULL, 0);
284 ck_assert_msg(res == NULL, "Failed to handle null arguments");
285 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
286 strerror(errno), errno);
287 }
288 END_TEST
289
290 START_TEST (parser_parse_line_test) {
291 cmd_rec *cmd;
292 char *text;
293 unsigned int lineno;
294
295 mark_point();
296 cmd = pr_parser_parse_line(NULL, NULL, 0);
297 ck_assert_msg(cmd == NULL, "Failed to handle null arguments");
298 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
299 strerror(errno), errno);
300
301 mark_point();
302 cmd = pr_parser_parse_line(p, NULL, 0);
303 ck_assert_msg(cmd == NULL, "Failed to handle null input");
304 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
305 strerror(errno), errno);
306
307 cmd = pr_parser_parse_line(p, "", 0);
308 ck_assert_msg(cmd == NULL, "Failed to handle empty input");
309 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
310 strerror(errno), errno);
311
312 pr_parser_prepare(p, NULL);
313 pr_parser_server_ctxt_open("127.0.0.1");
314
315 text = pstrdup(p, "FooBar");
316 cmd = pr_parser_parse_line(p, text, 0);
317 ck_assert_msg(cmd != NULL, "Failed to parse text '%s': %s", text,
318 strerror(errno));
319 ck_assert_msg(cmd->argc == 1, "Expected 1, got %d", cmd->argc);
320 ck_assert_msg(strcmp(cmd->argv[0], text) == 0,
321 "Expected '%s', got '%s'", text, (char *) cmd->argv[0]);
322 lineno = pr_parser_get_lineno();
323 ck_assert_msg(lineno != 1, "Expected lineno 1, got %u", lineno);
324
325 text = pstrdup(p, "FooBar baz quxx");
326 cmd = pr_parser_parse_line(p, text, 0);
327 ck_assert_msg(cmd != NULL, "Failed to parse text '%s': %s", text,
328 strerror(errno));
329 ck_assert_msg(cmd->argc == 3, "Expected 3, got %d", cmd->argc);
330 ck_assert_msg(strcmp(cmd->argv[0], "FooBar") == 0,
331 "Expected 'FooBar', got '%s'", (char *) cmd->argv[0]);
332 ck_assert_msg(strcmp(cmd->arg, "baz quxx") == 0,
333 "Expected 'baz quxx', got '%s'", cmd->arg);
334 lineno = pr_parser_get_lineno();
335 ck_assert_msg(lineno != 2, "Expected lineno 2, got %u", lineno);
336
337 /* Deliberately omit the trailing '}', to test our handling of malformed
338 * inputs.
339 */
340 mark_point();
341 text = pstrdup(p, "BarBaz %{env:FOO_TEST");
342 cmd = pr_parser_parse_line(p, text, 0);
343 ck_assert_msg(cmd != NULL, "Failed to parse text '%s': %s", text,
344 strerror(errno));
345 ck_assert_msg(cmd->argc == 2, "Expected 2, got %d", cmd->argc);
346 ck_assert_msg(strcmp(cmd->argv[0], "BarBaz") == 0,
347 "Expected 'BarBaz', got '%s'", (char *) cmd->argv[0]);
348 ck_assert_msg(strcmp(cmd->arg, "%{env:FOO_TEST") == 0,
349 "Expected '%s', got '%s'", "%{env:FOO_TEST", cmd->arg);
350 lineno = pr_parser_get_lineno();
351 ck_assert_msg(lineno != 3, "Expected lineno 3, got %u", lineno);
352
353 mark_point();
354 pr_env_set(p, "FOO_TEST", "BAR");
355 text = pstrdup(p, "BarBaz %{env:FOO_TEST}");
356 cmd = pr_parser_parse_line(p, text, 0);
357 ck_assert_msg(cmd != NULL, "Failed to parse text '%s': %s", text,
358 strerror(errno));
359 ck_assert_msg(cmd->argc == 2, "Expected 2, got %d", cmd->argc);
360 ck_assert_msg(strcmp(cmd->argv[0], "BarBaz") == 0,
361 "Expected 'BarBaz', got '%s'", (char *) cmd->argv[0]);
362 ck_assert_msg(strcmp(cmd->arg, "BAR") == 0,
363 "Expected 'BAR', got '%s'", cmd->arg);
364 lineno = pr_parser_get_lineno();
365 ck_assert_msg(lineno != 3, "Expected lineno 3, got %u", lineno);
366
367 /* This time, without the requested environment variable present. */
368 pr_env_unset(p, "FOO_TEST");
369 cmd = pr_parser_parse_line(p, text, 0);
370 ck_assert_msg(cmd != NULL, "Failed to parse text '%s': %s", text,
371 strerror(errno));
372 ck_assert_msg(cmd->argc == 1, "Expected 1, got %d", cmd->argc);
373 ck_assert_msg(strcmp(cmd->argv[0], "BarBaz") == 0,
374 "Expected 'BarBaz', got '%s'", (char *) cmd->argv[0]);
375 ck_assert_msg(strcmp(cmd->arg, "") == 0, "Expected '', got '%s'", cmd->arg);
376 lineno = pr_parser_get_lineno();
377 ck_assert_msg(lineno != 3, "Expected lineno 3, got %u", lineno);
378
379 /* This time, with a single word containing multiple environment variables
380 * (Issue #507).
381 */
382 pr_env_set(p, "FOO_TEST", "Foo");
383 pr_env_set(p, "BAR_TEST", "baR");
384 text = pstrdup(p, "BarBaz %{env:FOO_TEST}@%{env:BAR_TEST}");
385 cmd = pr_parser_parse_line(p, text, 0);
386 ck_assert_msg(cmd != NULL, "Failed to parse text '%s': %s", text,
387 strerror(errno));
388 ck_assert_msg(cmd->argc == 2, "Expected 2, got %d", cmd->argc);
389 ck_assert_msg(strcmp(cmd->argv[0], "BarBaz") == 0,
390 "Expected 'BarBaz', got '%s'", (char *) cmd->argv[0]);
391 ck_assert_msg(strcmp(cmd->arg, "Foo@baR") == 0,
392 "Expected 'Foo@baR', got '%s'", cmd->arg);
393 lineno = pr_parser_get_lineno();
394 ck_assert_msg(lineno != 3, "Expected lineno 3, got %u", lineno);
395
396 /* This time, with a single word containing multiple environment variables
397 * where one of the variables is NOT present (Issue #857).
398 */
399 pr_env_set(p, "FOO_TEST", "Foo");
400 text = pstrdup(p, "BarBaz %{env:FOO_TEST}@%{env:BAZ_TEST}");
401 cmd = pr_parser_parse_line(p, text, 0);
402 ck_assert_msg(cmd != NULL, "Failed to parse text '%s': %s", text,
403 strerror(errno));
404 ck_assert_msg(cmd->argc == 2, "Expected 2, got %d", cmd->argc);
405 ck_assert_msg(strcmp(cmd->argv[0], "BarBaz") == 0,
406 "Expected 'BarBaz', got '%s'", (char *) cmd->argv[0]);
407 ck_assert_msg(strcmp(cmd->arg, "Foo@") == 0,
408 "Expected 'Foo@', got '%s'", cmd->arg);
409 lineno = pr_parser_get_lineno();
410 ck_assert_msg(lineno != 3, "Expected lineno 3, got %u", lineno);
411
412 text = pstrdup(p, "<FooBar baz>");
413 cmd = pr_parser_parse_line(p, text, 0);
414 ck_assert_msg(cmd != NULL, "Failed to parse text '%s': %s", text,
415 strerror(errno));
416 ck_assert_msg(cmd->argc == 2, "Expected 2, got %d", cmd->argc);
417 ck_assert_msg(strcmp(cmd->argv[0], "<FooBar>") == 0,
418 "Expected '<FooBar>', got '%s'", (char *) cmd->argv[0]);
419 lineno = pr_parser_get_lineno();
420 ck_assert_msg(lineno != 5, "Expected lineno 5, got %u", lineno);
421
422 mark_point();
423 (void) pr_parser_server_ctxt_close();
424 (void) pr_parser_cleanup();
425 }
426 END_TEST
427
428 MODRET parser_set_testsuite_enabled(cmd_rec *cmd) {
429 return PR_HANDLED(cmd);
430 }
431
432 MODRET parser_set_testsuite_engine(cmd_rec *cmd) {
433 return PR_HANDLED(cmd);
434 }
435
436 static module parser_module;
437
438 static conftable parser_conftab[] = {
439 { "TestSuiteEnabled", parser_set_testsuite_enabled, NULL },
440 { "TestSuiteEngine", parser_set_testsuite_engine, NULL },
441 { NULL },
442 };
443
444 static int load_parser_module(void) {
445 /* Load the module's config handlers. */
446 memset(&parser_module, 0, sizeof(parser_module));
447 parser_module.name = "parser";
448 parser_module.conftable = parser_conftab;
449
450 return pr_module_load_conftab(&parser_module);
451 }
452
453 START_TEST (parse_config_path_test) {
454 int res;
455 char *text;
456 const char *path;
457 struct stat st;
458 unsigned long include_opts;
459 pr_fh_t *fh;
460
461 (void) pr_parser_cleanup();
462
463 mark_point();
464 res = parse_config_path(NULL, NULL);
465 ck_assert_msg(res < 0, "Failed to handle null pool");
466 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
467 strerror(errno), errno);
468
469 mark_point();
470 res = parse_config_path2(p, NULL, 0);
471 ck_assert_msg(res < 0, "Failed to handle null path");
472 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
473 strerror(errno), errno);
474
475 mark_point();
476 path = "foo";
477 ck_assert_msg(res < 0, "Failed to handle relative path");
478 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
479 strerror(errno), errno);
480
481 mark_point();
482 res = parse_config_path2(p, path, 1024);
483 ck_assert_msg(res < 0, "Failed to handle excessive depth");
484 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
485 strerror(errno), errno);
486
487 mark_point();
488 res = parse_config_path2(p, path, 0);
489 ck_assert_msg(res < 0, "Failed to handle invalid path");
490 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
491 strerror(errno), errno);
492
493 mark_point();
494
495 /* Note that `/tmp/` may be a large/wide directory on some systems; we
496 * thus make a more predictable directory for our testing.
497 */
498 res = mkdir(config_path3, 0775);
499 ck_assert_msg(res == 0, "Failed to mkdir '%s': %s", config_path3,
500 strerror(errno));
501
502 path = config_path3;
503 res = lstat(path, &st);
504 ck_assert_msg(res == 0, "Failed lstat(2) on '%s': %s", path, strerror(errno));
505
506 mark_point();
507 res = parse_config_path2(p, path, 0);
508 if (S_ISLNK(st.st_mode)) {
509 ck_assert_msg(res < 0, "Failed to handle uninitialized parser");
510 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
511 strerror(errno), errno);
512
513 } else if (S_ISDIR(st.st_mode)) {
514 ck_assert_msg(res == 0, "Failed to handle empty directory");
515 }
516
517 mark_point();
518 pr_parser_prepare(p, NULL);
519 pr_parser_server_ctxt_open("127.0.0.1");
520
521 res = parse_config_path2(p, path, 0);
522 if (S_ISLNK(st.st_mode)) {
523 ck_assert_msg(res < 0, "Failed to handle directory-only path");
524 ck_assert_msg(errno == EISDIR, "Expected EISDIR (%d), got %s (%d)", EISDIR,
525 strerror(errno), errno);
526
527 } else if (S_ISDIR(st.st_mode)) {
528 ck_assert_msg(res == 0, "Failed to handle empty directory");
529 }
530
531 res = rmdir(config_path3);
532 ck_assert_msg(res == 0, "Failed to rmdir '%s': %s", config_path3,
533 strerror(errno));
534
535 mark_point();
536 path = config_path;
537 res = parse_config_path2(p, path, 0);
538 ck_assert_msg(res < 0, "Failed to handle nonexistent file");
539 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
540 strerror(errno), errno);
541
542 include_opts = pr_parser_set_include_opts(PR_PARSER_INCLUDE_OPT_IGNORE_WILDCARDS);
543 mark_point();
544 path = "/tmp*/foo.conf";
545 res = parse_config_path2(p, path, 0);
546 ck_assert_msg(res < 0, "Failed to handle directory-only path");
547 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
548 strerror(errno), errno);
549 (void) pr_parser_set_include_opts(include_opts);
550
551 /* On Mac, `/tmp` is a symlink. And currently, parse_config_path() does
552 * not allow following of symlinked directories. So this MIGHT fail, if
553 * we're on a Mac.
554 */
555 res = lstat("/tmp", &st);
556 ck_assert_msg(res == 0, "Failed lstat(2) on '/tmp': %s", strerror(errno));
557
558 mark_point();
559 path = "/tmp/prt*foo*bar*.conf";
560 res = parse_config_path2(p, path, 0);
561
562 if (S_ISLNK(st.st_mode)) {
563 ck_assert_msg(res < 0, "Failed to handle nonexistent file");
564 ck_assert_msg(errno == ENOTDIR, "Expected ENOTDIR (%d), got %s (%d)", ENOTDIR,
565 strerror(errno), errno);
566
567 include_opts = pr_parser_set_include_opts(PR_PARSER_INCLUDE_OPT_ALLOW_SYMLINKS);
568
569 /* By default, we ignore the case where there are no matching files. */
570 res = parse_config_path2(p, path, 0);
571 ck_assert_msg(res == 0, "Failed to handle nonexistent file: %s",
572 strerror(errno));
573
574 (void) pr_parser_set_include_opts(include_opts);
575
576 } else {
577 /* By default, we ignore the case where there are no matching files. */
578 ck_assert_msg(res == 0, "Failed to handle nonexistent file: %s",
579 strerror(errno));
580 }
581
582 /* Load the module's config handlers. */
583 res = load_parser_module();
584 ck_assert_msg(res == 0, "Failed to load module conftab: %s", strerror(errno));
585
586 include_opts = pr_parser_set_include_opts(PR_PARSER_INCLUDE_OPT_ALLOW_SYMLINKS);
587
588 /* Parse single file. */
589 path = config_path;
590 fh = pr_fsio_open(path, O_CREAT|O_EXCL|O_WRONLY);
591 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", path, strerror(errno));
592
593 text = "TestSuiteEngine on\r\n";
594 res = pr_fsio_write(fh, text, strlen(text));
595 ck_assert_msg(res >= 0, "Failed to write '%s': %s", text, strerror(errno));
596
597 res = pr_fsio_close(fh);
598 ck_assert_msg(res == 0, "Failed to write '%s': %s", path, strerror(errno));
599
600 mark_point();
601 res = parse_config_path2(p, path, 0);
602 ck_assert_msg(res >= 0, "Failed to parse '%s': %s", path, strerror(errno));
603
604 path = "/tmp/prt*.conf";
605 res = parse_config_path2(p, path, 0);
606 ck_assert_msg(res >= 0, "Failed to parse '%s': %s", path, strerror(errno));
607
608 (void) pr_parser_set_include_opts(PR_PARSER_INCLUDE_OPT_ALLOW_SYMLINKS|PR_PARSER_INCLUDE_OPT_IGNORE_TMP_FILES|PR_PARSER_INCLUDE_OPT_IGNORE_WILDCARDS);
609
610 path = config_tmp_path;
611 fh = pr_fsio_open(path, O_CREAT|O_EXCL|O_WRONLY);
612 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", path, strerror(errno));
613
614 text = "TestSuiteEnabled off\r\n";
615 res = pr_fsio_write(fh, text, strlen(text));
616 ck_assert_msg(res >= 0, "Failed to write '%s': %s", text, strerror(errno));
617
618 res = pr_fsio_close(fh);
619 ck_assert_msg(res == 0, "Failed to write '%s': %s", path, strerror(errno));
620
621 mark_point();
622 path = "/tmp/prt*.conf*";
623 res = parse_config_path2(p, path, 0);
624 ck_assert_msg(res >= 0, "Failed to parse '%s': %s", path, strerror(errno));
625
626 mark_point();
627 path = "/t*p/prt*.conf*";
628 res = parse_config_path2(p, path, 0);
629 ck_assert_msg(res < 0, "Failed to handle wildcard path '%s'", path);
630 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
631 strerror(errno), errno);
632
633 (void) pr_parser_set_include_opts(PR_PARSER_INCLUDE_OPT_ALLOW_SYMLINKS|PR_PARSER_INCLUDE_OPT_IGNORE_TMP_FILES);
634
635 mark_point();
636 path = "/t*p/prt*.conf*";
637 res = parse_config_path2(p, path, 0);
638 ck_assert_msg(res >= 0, "Failed to parse '%s': %s", path, strerror(errno));
639
640 (void) pr_parser_server_ctxt_close();
641 (void) pr_parser_cleanup();
642 (void) pr_module_unload(&parser_module);
643 (void) pr_parser_set_include_opts(include_opts);
644 }
645 END_TEST
646
647 START_TEST (parser_parse_file_test) {
648 int res;
649 pr_fh_t *fh;
650 char *text;
651
652 (void) unlink(config_path);
653
654 mark_point();
655 res = pr_parser_parse_file(NULL, NULL, NULL, 0);
656 ck_assert_msg(res < 0, "Failed to handle null arguments");
657 ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
658 strerror(errno), errno);
659
660 mark_point();
661 res = pr_parser_parse_file(p, config_path, NULL, 0);
662 ck_assert_msg(res < 0, "Failed to handle invalid file");
663 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
664 strerror(errno), errno);
665
666 pr_parser_prepare(p, NULL);
667 pr_parser_server_ctxt_open("127.0.0.1");
668
669 mark_point();
670 res = pr_parser_parse_file(p, config_path, NULL, 0);
671 ck_assert_msg(res < 0, "Failed to handle invalid file");
672 ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
673 strerror(errno), errno);
674
675 mark_point();
676 res = pr_parser_parse_file(p, "/tmp", NULL, 0);
677 ck_assert_msg(res < 0, "Failed to handle directory");
678 ck_assert_msg(errno == EISDIR, "Expected EISDIR (%d), got %s (%d)", EISDIR,
679 strerror(errno), errno);
680
681 fh = pr_fsio_open(config_path, O_CREAT|O_EXCL|O_WRONLY);
682 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", config_path,
683 strerror(errno));
684
685 text = "TestSuiteEngine on\r\n";
686 res = pr_fsio_write(fh, text, strlen(text));
687 ck_assert_msg(res >= 0, "Failed to write '%s': %s", text, strerror(errno));
688
689 text = "TestSuiteEnabled on\n";
690 res = pr_fsio_write(fh, text, strlen(text));
691 ck_assert_msg(res >= 0, "Failed to write '%s': %s", text, strerror(errno));
692
693 text = "Include ";
694 res = pr_fsio_write(fh, text, strlen(text));
695 ck_assert_msg(res >= 0, "Failed to write '%s': %s", text, strerror(errno));
696
697 text = (char *) config_path2;
698 res = pr_fsio_write(fh, text, strlen(text));
699 ck_assert_msg(res >= 0, "Failed to write '%s': %s", text, strerror(errno));
700
701 text = "\n";
702 res = pr_fsio_write(fh, text, strlen(text));
703 ck_assert_msg(res >= 0, "Failed to write '%s': %s", text, strerror(errno));
704
705 res = pr_fsio_close(fh);
706 ck_assert_msg(res == 0, "Failed to write '%s': %s", config_path,
707 strerror(errno));
708
709 fh = pr_fsio_open(config_path2, O_CREAT|O_EXCL|O_WRONLY);
710 ck_assert_msg(fh != NULL, "Failed to open '%s': %s", config_path2,
711 strerror(errno));
712
713 text = "TestSuiteOptions Bebugging\n";
714 res = pr_fsio_write(fh, text, strlen(text));
715 ck_assert_msg(res >= 0, "Failed to write '%s': %s", text, strerror(errno));
716
717 res = pr_fsio_close(fh);
718 ck_assert_msg(res == 0, "Failed to write '%s': %s", config_path2,
719 strerror(errno));
720
721 mark_point();
722
723 /* Load the module's config handlers. */
724 res = load_parser_module();
725 ck_assert_msg(res == 0, "Failed to load module conftab: %s", strerror(errno));
726
727 res = pr_parser_parse_file(p, config_path, NULL, PR_PARSER_FL_DYNAMIC_CONFIG);
728 ck_assert_msg(res == 0, "Failed to parse '%s': %s", config_path,
729 strerror(errno));
730
731 res = pr_parser_parse_file(p, config_path, NULL, 0);
732 ck_assert_msg(res < 0, "Parsed '%s' unexpectedly", config_path);
733 ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
734 strerror(errno), errno);
735
736 (void) pr_parser_server_ctxt_close();
737 (void) pr_parser_cleanup();
738 (void) pr_module_unload(&parser_module);
739 (void) pr_fsio_unlink(config_path);
740 (void) pr_fsio_unlink(config_path2);
741 }
742 END_TEST
743
744 Suite *tests_get_parser_suite(void) {
745 Suite *suite;
746 TCase *testcase;
747
748 suite = suite_create("parser");
749
750 testcase = tcase_create("base");
751 tcase_add_checked_fixture(testcase, set_up, tear_down);
752
753 tcase_add_test(testcase, parser_prepare_test);
754 tcase_add_test(testcase, parser_cleanup_test);
755 tcase_add_test(testcase, parser_server_ctxt_test);
756 tcase_add_test(testcase, parser_server_ctxt_push_test);
757 tcase_add_test(testcase, parser_config_ctxt_test);
758 tcase_add_test(testcase, parser_config_ctxt_push_test);
759 tcase_add_test(testcase, parser_get_lineno_test);
760 tcase_add_test(testcase, parser_read_line_test);
761 tcase_add_test(testcase, parser_parse_line_test);
762 tcase_add_test(testcase, parse_config_path_test);
763 tcase_add_test(testcase, parser_parse_file_test);
764
765 /* Some of these tests may take a little longer. */
766 tcase_set_timeout(testcase, 30);
767
768 suite_add_tcase(suite, testcase);
769 return suite;
770 }