xref: /freebsd-src/crypto/openssh/regress/unittests/misc/test_expand.c (revision 1323ec571215a77ddd21294f0871979d5ad6b992)
1*1323ec57SEd Maste /* 	$OpenBSD: test_expand.c,v 1.3 2021/12/14 21:25:27 deraadt Exp $ */
219261079SEd Maste /*
319261079SEd Maste  * Regress test for misc string expansion functions.
419261079SEd Maste  *
519261079SEd Maste  * Placed in the public domain.
619261079SEd Maste  */
719261079SEd Maste 
819261079SEd Maste #include "includes.h"
919261079SEd Maste 
1019261079SEd Maste #include <sys/types.h>
1119261079SEd Maste #include <stdio.h>
1219261079SEd Maste #ifdef HAVE_STDINT_H
1319261079SEd Maste #include <stdint.h>
1419261079SEd Maste #endif
1519261079SEd Maste #include <stdlib.h>
1619261079SEd Maste #include <string.h>
1719261079SEd Maste 
1819261079SEd Maste #include "../test_helper/test_helper.h"
1919261079SEd Maste 
2019261079SEd Maste #include "log.h"
2119261079SEd Maste #include "misc.h"
2219261079SEd Maste 
2319261079SEd Maste void test_expand(void);
2419261079SEd Maste 
2519261079SEd Maste void
test_expand(void)2619261079SEd Maste test_expand(void)
2719261079SEd Maste {
2819261079SEd Maste 	int parseerr;
2919261079SEd Maste 	char *ret;
3019261079SEd Maste 
3119261079SEd Maste 	TEST_START("dollar_expand");
3219261079SEd Maste 	ASSERT_INT_EQ(setenv("FOO", "bar", 1), 0);
3319261079SEd Maste 	ASSERT_INT_EQ(setenv("BAR", "baz", 1), 0);
3419261079SEd Maste 	(void)unsetenv("BAZ");
3519261079SEd Maste #define ASSERT_DOLLAR_EQ(x, y) do { \
3619261079SEd Maste 	char *str = dollar_expand(NULL, (x)); \
3719261079SEd Maste 	ASSERT_STRING_EQ(str, (y)); \
3819261079SEd Maste 	free(str); \
3919261079SEd Maste } while(0)
4019261079SEd Maste 	ASSERT_DOLLAR_EQ("${FOO}", "bar");
4119261079SEd Maste 	ASSERT_DOLLAR_EQ(" ${FOO}", " bar");
4219261079SEd Maste 	ASSERT_DOLLAR_EQ("${FOO} ", "bar ");
4319261079SEd Maste 	ASSERT_DOLLAR_EQ(" ${FOO} ", " bar ");
4419261079SEd Maste 	ASSERT_DOLLAR_EQ("${FOO}${BAR}", "barbaz");
4519261079SEd Maste 	ASSERT_DOLLAR_EQ(" ${FOO} ${BAR}", " bar baz");
4619261079SEd Maste 	ASSERT_DOLLAR_EQ("${FOO}${BAR} ", "barbaz ");
4719261079SEd Maste 	ASSERT_DOLLAR_EQ(" ${FOO} ${BAR} ", " bar baz ");
4819261079SEd Maste 	ASSERT_DOLLAR_EQ("$", "$");
4919261079SEd Maste 	ASSERT_DOLLAR_EQ(" $", " $");
5019261079SEd Maste 	ASSERT_DOLLAR_EQ("$ ", "$ ");
5119261079SEd Maste 
5219261079SEd Maste 	/* suppress error messages for error handing tests */
5319261079SEd Maste 	log_init("test_misc", SYSLOG_LEVEL_QUIET, SYSLOG_FACILITY_AUTH, 1);
5419261079SEd Maste 	/* error checking, non existent variable */
5519261079SEd Maste 	ret = dollar_expand(&parseerr, "a${BAZ}");
5619261079SEd Maste 	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0);
5719261079SEd Maste 	ret = dollar_expand(&parseerr, "${BAZ}b");
5819261079SEd Maste 	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0);
5919261079SEd Maste 	ret = dollar_expand(&parseerr, "a${BAZ}b");
6019261079SEd Maste 	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 0);
6119261079SEd Maste 	/* invalid format */
6219261079SEd Maste 	ret = dollar_expand(&parseerr, "${");
6319261079SEd Maste 	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1);
6419261079SEd Maste 	ret = dollar_expand(&parseerr, "${F");
6519261079SEd Maste 	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1);
6619261079SEd Maste 	ret = dollar_expand(&parseerr, "${FO");
6719261079SEd Maste 	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1);
6819261079SEd Maste 	/* empty variable name */
6919261079SEd Maste 	ret = dollar_expand(&parseerr, "${}");
7019261079SEd Maste 	ASSERT_PTR_EQ(ret, NULL); ASSERT_INT_EQ(parseerr, 1);
7119261079SEd Maste 	/* restore loglevel to default */
7219261079SEd Maste 	log_init("test_misc", SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 1);
7319261079SEd Maste 	TEST_DONE();
7419261079SEd Maste 
7519261079SEd Maste 	TEST_START("percent_expand");
7619261079SEd Maste 	ASSERT_STRING_EQ(percent_expand("%%", "%h", "foo", NULL), "%");
7719261079SEd Maste 	ASSERT_STRING_EQ(percent_expand("%h", "h", "foo", NULL), "foo");
7819261079SEd Maste 	ASSERT_STRING_EQ(percent_expand("%h ", "h", "foo", NULL), "foo ");
7919261079SEd Maste 	ASSERT_STRING_EQ(percent_expand(" %h", "h", "foo", NULL), " foo");
8019261079SEd Maste 	ASSERT_STRING_EQ(percent_expand(" %h ", "h", "foo", NULL), " foo ");
8119261079SEd Maste 	ASSERT_STRING_EQ(percent_expand(" %a%b ", "a", "foo", "b", "bar", NULL),
8219261079SEd Maste 	    " foobar ");
8319261079SEd Maste 	TEST_DONE();
8419261079SEd Maste 
8519261079SEd Maste 	TEST_START("percent_dollar_expand");
8619261079SEd Maste 	ASSERT_STRING_EQ(percent_dollar_expand("%h${FOO}", "h", "foo", NULL),
8719261079SEd Maste 	    "foobar");
8819261079SEd Maste 	TEST_DONE();
8919261079SEd Maste }
90