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