summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordtucker@openbsd.org <dtucker@openbsd.org>2020-05-29 04:32:26 +0000
committerDamien Miller <djm@mindrot.org>2020-05-29 15:48:15 +1000
commit058674a62ffe33f01d871d46e624bc2a2c22d91f (patch)
treefb368d4a258b5b98ed5449fa7517b7a6d97f5567
parent0b15892fc47d6840eba1291a6be9be1a70bc8972 (diff)
upstream: Add regression and unit tests for ${ENV} style
environment variable expansion in various keywords (bz#3140). ok djm@ OpenBSD-Regress-ID: 4d9ceb95d89365b7b674bc26cf064c15a5bbb197
-rw-r--r--regress/percent.sh15
-rw-r--r--regress/unittests/misc/tests.c69
2 files changed, 79 insertions, 5 deletions
diff --git a/regress/percent.sh b/regress/percent.sh
index 2e891f693..cdcbe1839 100644
--- a/regress/percent.sh
+++ b/regress/percent.sh
@@ -1,4 +1,4 @@
1# $OpenBSD: percent.sh,v 1.6 2020/04/10 00:54:03 dtucker Exp $ 1# $OpenBSD: percent.sh,v 1.7 2020/05/29 04:32:26 dtucker Exp $
2# Placed in the Public Domain. 2# Placed in the Public Domain.
3 3
4tid="percent expansions" 4tid="percent expansions"
@@ -56,7 +56,7 @@ trial()
56 56
57for i in matchexec localcommand remotecommand controlpath identityagent \ 57for i in matchexec localcommand remotecommand controlpath identityagent \
58 forwardagent localforward remoteforward; do 58 forwardagent localforward remoteforward; do
59 verbose $tid $i 59 verbose $tid $i percent
60 if [ "$i" = "localcommand" ]; then 60 if [ "$i" = "localcommand" ]; then
61 REMUSER=$USER 61 REMUSER=$USER
62 trial $i '%T' NONE 62 trial $i '%T' NONE
@@ -81,8 +81,19 @@ for i in matchexec localcommand remotecommand controlpath identityagent \
81 "%/$HASH/$USERID/127.0.0.1/$HOME/$HOST/$HOSTNAME/somehost/$PORT/$REMUSER/$USER" 81 "%/$HASH/$USERID/127.0.0.1/$HOME/$HOST/$HOSTNAME/somehost/$PORT/$REMUSER/$USER"
82done 82done
83 83
84# Subset of above since we don't expand shell-style variables on anything that
85# runs a command because the shell will expand those.
86for i in controlpath identityagent forwardagent localforward remoteforward; do
87 verbose $tid $i dollar
88 FOO=bar
89 export FOO
90 trial $i '${FOO}' $FOO
91done
92
93
84# A subset of options support tilde expansion 94# A subset of options support tilde expansion
85for i in controlpath identityagent forwardagent; do 95for i in controlpath identityagent forwardagent; do
96 verbose $tid $i tilde
86 trial $i '~' $HOME/ 97 trial $i '~' $HOME/
87 trial $i '~/.ssh' $HOME/.ssh 98 trial $i '~/.ssh' $HOME/.ssh
88done 99done
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
19void 20void
20tests(void) 21tests(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}