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