1 /* 	$OpenBSD: test_expand.c,v 1.3 2021/12/14 21:25:27 deraadt Exp $ */
2 /*
3  * Regress test for misc string expansion functions.
4  *
5  * Placed in the public domain.
6  */
7 
8 #include <sys/types.h>
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <stdlib.h>
12 #include <string.h>
13 
14 #include "test_helper.h"
15 
16 #include "log.h"
17 #include "misc.h"
18 
19 void test_expand(void);
20 
21 void
22 test_expand(void)
23 {
24 	int parseerr;
25 	char *ret;
26 
27 	TEST_START("dollar_expand");
28 	ASSERT_INT_EQ(setenv("FOO", "bar", 1), 0);
29 	ASSERT_INT_EQ(setenv("BAR", "baz", 1), 0);
30 	(void)unsetenv("BAZ");
31 #define ASSERT_DOLLAR_EQ(x, y) do { \
32 	char *str = dollar_expand(NULL, (x)); \
33 	ASSERT_STRING_EQ(str, (y)); \
34 	free(str); \
35 } while(0)
36 	ASSERT_DOLLAR_EQ("${FOO}", "bar");
37 	ASSERT_DOLLAR_EQ(" ${FOO}", " bar");
38 	ASSERT_DOLLAR_EQ("${FOO} ", "bar ");
39 	ASSERT_DOLLAR_EQ(" ${FOO} ", " bar ");
40 	ASSERT_DOLLAR_EQ("${FOO}${BAR}", "barbaz");
41 	ASSERT_DOLLAR_EQ(" ${FOO} ${BAR}", " bar baz");
42 	ASSERT_DOLLAR_EQ("${FOO}${BAR} ", "barbaz ");
43 	ASSERT_DOLLAR_EQ(" ${FOO} ${BAR} ", " bar baz ");
44 	ASSERT_DOLLAR_EQ("$", "$");
45 	ASSERT_DOLLAR_EQ(" $", " $");
46 	ASSERT_DOLLAR_EQ("$ ", "$ ");
47 
48 	/* suppress error messages for error handing tests */
49 	log_init("test_misc", SYSLOG_LEVEL_QUIET, SYSLOG_FACILITY_AUTH, 1);
50 	/* error checking, non existent variable */
51 	ret = dollar_expand(&parseerr, "a${BAZ}");
52 	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0);
53 	ret = dollar_expand(&parseerr, "${BAZ}b");
54 	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0);
55 	ret = dollar_expand(&parseerr, "a${BAZ}b");
56 	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0);
57 	/* invalid format */
58 	ret = dollar_expand(&parseerr, "${");
59 	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1);
60 	ret = dollar_expand(&parseerr, "${F");
61 	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1);
62 	ret = dollar_expand(&parseerr, "${FO");
63 	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1);
64 	/* empty variable name */
65 	ret = dollar_expand(&parseerr, "${}");
66 	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1);
67 	/* restore loglevel to default */
68 	log_init("test_misc", SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 1);
69 	TEST_DONE();
70 
71 	TEST_START("percent_expand");
72 	ASSERT_STRING_EQ(percent_expand("%%", "%h", "foo", NULL), "%");
73 	ASSERT_STRING_EQ(percent_expand("%h", "h", "foo", NULL), "foo");
74 	ASSERT_STRING_EQ(percent_expand("%h ", "h", "foo", NULL), "foo ");
75 	ASSERT_STRING_EQ(percent_expand(" %h", "h", "foo", NULL), " foo");
76 	ASSERT_STRING_EQ(percent_expand(" %h ", "h", "foo", NULL), " foo ");
77 	ASSERT_STRING_EQ(percent_expand(" %a%b ", "a", "foo", "b", "bar", NULL),
78 	    " foobar ");
79 	TEST_DONE();
80 
81 	TEST_START("percent_dollar_expand");
82 	ASSERT_STRING_EQ(percent_dollar_expand("%h${FOO}", "h", "foo", NULL),
83 	    "foobar");
84 	TEST_DONE();
85 }
86