diff options
author | dtucker@openbsd.org <dtucker@openbsd.org> | 2020-05-29 04:32:26 +0000 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2020-05-29 15:48:15 +1000 |
commit | 058674a62ffe33f01d871d46e624bc2a2c22d91f (patch) | |
tree | fb368d4a258b5b98ed5449fa7517b7a6d97f5567 /regress/unittests/misc | |
parent | 0b15892fc47d6840eba1291a6be9be1a70bc8972 (diff) |
upstream: Add regression and unit tests for ${ENV} style
environment variable expansion in various keywords (bz#3140). ok djm@
OpenBSD-Regress-ID: 4d9ceb95d89365b7b674bc26cf064c15a5bbb197
Diffstat (limited to 'regress/unittests/misc')
-rw-r--r-- | regress/unittests/misc/tests.c | 69 |
1 files changed, 66 insertions, 3 deletions
diff --git a/regress/unittests/misc/tests.c b/regress/unittests/misc/tests.c index 8fe6aedbb..0bd0c84f9 100644 --- a/regress/unittests/misc/tests.c +++ b/regress/unittests/misc/tests.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: tests.c,v 1.2 2020/05/29 01:21:35 dtucker Exp $ */ | 1 | /* $OpenBSD: tests.c,v 1.3 2020/05/29 04:32:26 dtucker Exp $ */ |
2 | /* | 2 | /* |
3 | * Regress test for misc helper functions. | 3 | * Regress test for misc helper functions. |
4 | * | 4 | * |
@@ -14,13 +14,14 @@ | |||
14 | 14 | ||
15 | #include "test_helper.h" | 15 | #include "test_helper.h" |
16 | 16 | ||
17 | #include "log.h" | ||
17 | #include "misc.h" | 18 | #include "misc.h" |
18 | 19 | ||
19 | void | 20 | void |
20 | tests(void) | 21 | tests(void) |
21 | { | 22 | { |
22 | int port; | 23 | int port, parseerr; |
23 | char *user, *host, *path; | 24 | char *user, *host, *path, *ret; |
24 | 25 | ||
25 | TEST_START("misc_parse_user_host_path"); | 26 | TEST_START("misc_parse_user_host_path"); |
26 | ASSERT_INT_EQ(parse_user_host_path("someuser@some.host:some/path", | 27 | ASSERT_INT_EQ(parse_user_host_path("someuser@some.host:some/path", |
@@ -95,4 +96,66 @@ tests(void) | |||
95 | ASSERT_LONG_EQ(convtime("trout"), -1); | 96 | ASSERT_LONG_EQ(convtime("trout"), -1); |
96 | ASSERT_LONG_EQ(convtime("-77"), -1); | 97 | ASSERT_LONG_EQ(convtime("-77"), -1); |
97 | TEST_DONE(); | 98 | TEST_DONE(); |
99 | |||
100 | TEST_START("dollar_expand"); | ||
101 | if (setenv("FOO", "bar", 1) != 0) | ||
102 | abort(); | ||
103 | if (setenv("BAR", "baz", 1) != 0) | ||
104 | abort(); | ||
105 | if (unsetenv("BAZ") != 0) | ||
106 | abort(); | ||
107 | #define ASSERT_DOLLAR_EQ(x, y) do { \ | ||
108 | char *str = dollar_expand(NULL, (x)); \ | ||
109 | ASSERT_STRING_EQ(str, (y)); \ | ||
110 | free(str); \ | ||
111 | } while(0) | ||
112 | ASSERT_DOLLAR_EQ("${FOO}", "bar"); | ||
113 | ASSERT_DOLLAR_EQ(" ${FOO}", " bar"); | ||
114 | ASSERT_DOLLAR_EQ("${FOO} ", "bar "); | ||
115 | ASSERT_DOLLAR_EQ(" ${FOO} ", " bar "); | ||
116 | ASSERT_DOLLAR_EQ("${FOO}${BAR}", "barbaz"); | ||
117 | ASSERT_DOLLAR_EQ(" ${FOO} ${BAR}", " bar baz"); | ||
118 | ASSERT_DOLLAR_EQ("${FOO}${BAR} ", "barbaz "); | ||
119 | ASSERT_DOLLAR_EQ(" ${FOO} ${BAR} ", " bar baz "); | ||
120 | ASSERT_DOLLAR_EQ("$", "$"); | ||
121 | ASSERT_DOLLAR_EQ(" $", " $"); | ||
122 | ASSERT_DOLLAR_EQ("$ ", "$ "); | ||
123 | |||
124 | /* suppress error messages for error handing tests */ | ||
125 | log_init("test_misc", SYSLOG_LEVEL_QUIET, SYSLOG_FACILITY_AUTH, 1); | ||
126 | /* error checking, non existent variable */ | ||
127 | ret = dollar_expand(&parseerr, "a${BAZ}"); | ||
128 | ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0); | ||
129 | ret = dollar_expand(&parseerr, "${BAZ}b"); | ||
130 | ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0); | ||
131 | ret = dollar_expand(&parseerr, "a${BAZ}b"); | ||
132 | ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0); | ||
133 | /* invalid format */ | ||
134 | ret = dollar_expand(&parseerr, "${"); | ||
135 | ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1); | ||
136 | ret = dollar_expand(&parseerr, "${F"); | ||
137 | ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1); | ||
138 | ret = dollar_expand(&parseerr, "${FO"); | ||
139 | ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1); | ||
140 | /* empty variable name */ | ||
141 | ret = dollar_expand(&parseerr, "${}"); | ||
142 | ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1); | ||
143 | /* restore loglevel to default */ | ||
144 | log_init("test_misc", SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 1); | ||
145 | TEST_DONE(); | ||
146 | |||
147 | TEST_START("percent_expand"); | ||
148 | ASSERT_STRING_EQ(percent_expand("%%", "%h", "foo", NULL), "%"); | ||
149 | ASSERT_STRING_EQ(percent_expand("%h", "h", "foo", NULL), "foo"); | ||
150 | ASSERT_STRING_EQ(percent_expand("%h ", "h", "foo", NULL), "foo "); | ||
151 | ASSERT_STRING_EQ(percent_expand(" %h", "h", "foo", NULL), " foo"); | ||
152 | ASSERT_STRING_EQ(percent_expand(" %h ", "h", "foo", NULL), " foo "); | ||
153 | ASSERT_STRING_EQ(percent_expand(" %a%b ", "a", "foo", "b", "bar", NULL), | ||
154 | " foobar "); | ||
155 | TEST_DONE(); | ||
156 | |||
157 | TEST_START("percent_dollar_expand"); | ||
158 | ASSERT_STRING_EQ(percent_dollar_expand("%h${FOO}", "h", "foo", NULL), | ||
159 | "foobar"); | ||
160 | TEST_DONE(); | ||
98 | } | 161 | } |