[openssh-commits] [openssh] 02/02: upstream: Add regression and unit tests for ${ENV} style

git+noreply at mindrot.org git+noreply at mindrot.org
Fri May 29 15:48:30 AEST 2020


This is an automated email from the git hooks/post-receive script.

djm pushed a commit to branch master
in repository openssh.

commit 058674a62ffe33f01d871d46e624bc2a2c22d91f
Author: dtucker at openbsd.org <dtucker at openbsd.org>
Date:   Fri May 29 04:32:26 2020 +0000

    upstream: Add regression and unit tests for ${ENV} style
    
    environment variable expansion in various keywords (bz#3140).  ok djm@
    
    OpenBSD-Regress-ID: 4d9ceb95d89365b7b674bc26cf064c15a5bbb197
---
 regress/percent.sh             | 15 +++++++--
 regress/unittests/misc/tests.c | 69 ++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 79 insertions(+), 5 deletions(-)

diff --git a/regress/percent.sh b/regress/percent.sh
index 2e891f69..cdcbe183 100644
--- a/regress/percent.sh
+++ b/regress/percent.sh
@@ -1,4 +1,4 @@
-#	$OpenBSD: percent.sh,v 1.6 2020/04/10 00:54:03 dtucker Exp $
+#	$OpenBSD: percent.sh,v 1.7 2020/05/29 04:32:26 dtucker Exp $
 #	Placed in the Public Domain.
 
 tid="percent expansions"
@@ -56,7 +56,7 @@ trial()
 
 for i in matchexec localcommand remotecommand controlpath identityagent \
     forwardagent localforward remoteforward; do
-	verbose $tid $i
+	verbose $tid $i percent
 	if [ "$i" = "localcommand" ]; then
 		REMUSER=$USER
 		trial $i '%T' NONE
@@ -81,8 +81,19 @@ for i in matchexec localcommand remotecommand controlpath identityagent \
 	    "%/$HASH/$USERID/127.0.0.1/$HOME/$HOST/$HOSTNAME/somehost/$PORT/$REMUSER/$USER"
 done
 
+# Subset of above since we don't expand shell-style variables on anything that
+# runs a command because the shell will expand those.
+for i in controlpath identityagent forwardagent localforward remoteforward; do
+	verbose $tid $i dollar
+	FOO=bar
+	export FOO
+	trial $i '${FOO}' $FOO
+done
+
+
 # A subset of options support tilde expansion
 for i in controlpath identityagent forwardagent; do
+	verbose $tid $i tilde
 	trial $i '~' $HOME/
 	trial $i '~/.ssh' $HOME/.ssh
 done
diff --git a/regress/unittests/misc/tests.c b/regress/unittests/misc/tests.c
index 8fe6aedb..0bd0c84f 100644
--- a/regress/unittests/misc/tests.c
+++ b/regress/unittests/misc/tests.c
@@ -1,4 +1,4 @@
-/* 	$OpenBSD: tests.c,v 1.2 2020/05/29 01:21:35 dtucker Exp $ */
+/* 	$OpenBSD: tests.c,v 1.3 2020/05/29 04:32:26 dtucker Exp $ */
 /*
  * Regress test for misc helper functions.
  *
@@ -14,13 +14,14 @@
 
 #include "test_helper.h"
 
+#include "log.h"
 #include "misc.h"
 
 void
 tests(void)
 {
-	int port;
-	char *user, *host, *path;
+	int port, parseerr;
+	char *user, *host, *path, *ret;
 
 	TEST_START("misc_parse_user_host_path");
 	ASSERT_INT_EQ(parse_user_host_path("someuser at some.host:some/path",
@@ -95,4 +96,66 @@ tests(void)
 	ASSERT_LONG_EQ(convtime("trout"),  -1);
 	ASSERT_LONG_EQ(convtime("-77"),  -1);
 	TEST_DONE();
+
+	TEST_START("dollar_expand");
+	if (setenv("FOO", "bar", 1) != 0)
+		abort();
+	if (setenv("BAR", "baz", 1) != 0)
+		abort();
+	if (unsetenv("BAZ") != 0)
+		abort();
+#define ASSERT_DOLLAR_EQ(x, y) do { \
+	char *str = dollar_expand(NULL, (x)); \
+	ASSERT_STRING_EQ(str, (y)); \
+	free(str); \
+} while(0)
+	ASSERT_DOLLAR_EQ("${FOO}", "bar");
+	ASSERT_DOLLAR_EQ(" ${FOO}", " bar");
+	ASSERT_DOLLAR_EQ("${FOO} ", "bar ");
+	ASSERT_DOLLAR_EQ(" ${FOO} ", " bar ");
+	ASSERT_DOLLAR_EQ("${FOO}${BAR}", "barbaz");
+	ASSERT_DOLLAR_EQ(" ${FOO} ${BAR}", " bar baz");
+	ASSERT_DOLLAR_EQ("${FOO}${BAR} ", "barbaz ");
+	ASSERT_DOLLAR_EQ(" ${FOO} ${BAR} ", " bar baz ");
+	ASSERT_DOLLAR_EQ("$", "$");
+	ASSERT_DOLLAR_EQ(" $", " $");
+	ASSERT_DOLLAR_EQ("$ ", "$ ");
+
+	/* suppress error messages for error handing tests */
+	log_init("test_misc", SYSLOG_LEVEL_QUIET, SYSLOG_FACILITY_AUTH, 1);
+	/* error checking, non existent variable */
+	ret = dollar_expand(&parseerr, "a${BAZ}");
+	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0);
+	ret = dollar_expand(&parseerr, "${BAZ}b");
+	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0);
+	ret = dollar_expand(&parseerr, "a${BAZ}b");
+	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0);
+	/* invalid format */
+	ret = dollar_expand(&parseerr, "${");
+	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1);
+	ret = dollar_expand(&parseerr, "${F");
+	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1);
+	ret = dollar_expand(&parseerr, "${FO");
+	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1);
+	/* empty variable name */
+	ret = dollar_expand(&parseerr, "${}");
+	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1);
+	/* restore loglevel to default */
+	log_init("test_misc", SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 1);
+	TEST_DONE();
+
+	TEST_START("percent_expand");
+	ASSERT_STRING_EQ(percent_expand("%%", "%h", "foo", NULL), "%");
+	ASSERT_STRING_EQ(percent_expand("%h", "h", "foo", NULL), "foo");
+	ASSERT_STRING_EQ(percent_expand("%h ", "h", "foo", NULL), "foo ");
+	ASSERT_STRING_EQ(percent_expand(" %h", "h", "foo", NULL), " foo");
+	ASSERT_STRING_EQ(percent_expand(" %h ", "h", "foo", NULL), " foo ");
+	ASSERT_STRING_EQ(percent_expand(" %a%b ", "a", "foo", "b", "bar", NULL),
+	    " foobar ");
+	TEST_DONE();
+
+	TEST_START("percent_dollar_expand");
+	ASSERT_STRING_EQ(percent_dollar_expand("%h${FOO}", "h", "foo", NULL),
+	    "foobar");
+	TEST_DONE();
 }

-- 
To stop receiving notification emails like this one, please contact
djm at mindrot.org.


More information about the openssh-commits mailing list