diff options
author | Werner Koch <wk@gnupg.org> | 2021-09-17 17:33:21 +0200 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 2021-09-17 17:33:21 +0200 |
commit | 9c272dc245456d5403e3bd50553b4fdccb370e27 (patch) | |
tree | 73ea80e19e91dddddce2c5c98720cfb87d9cc38b /common/t-stringhelp.c | |
parent | doc: Clarify some gpg keyring options (diff) | |
download | gnupg2-9c272dc245456d5403e3bd50553b4fdccb370e27.tar.xz gnupg2-9c272dc245456d5403e3bd50553b4fdccb370e27.zip |
common: New function substitute_envvars.
* common/stringhelp.c (substitute_envvars): New. Based on code in
gpg-connect-agent.
* common/t-stringhelp.c: Include sysutils.h.
(test_substitute_envvars): New.
--
GnuPG-bug-id: 5599
Diffstat (limited to 'common/t-stringhelp.c')
-rw-r--r-- | common/t-stringhelp.c | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/common/t-stringhelp.c b/common/t-stringhelp.c index 5eca5a092..8d0c1c576 100644 --- a/common/t-stringhelp.c +++ b/common/t-stringhelp.c @@ -43,6 +43,7 @@ #include <limits.h> #include "t-support.h" +#include "sysutils.h" #include "stringhelp.h" @@ -1194,6 +1195,103 @@ test_compare_version_strings (void) } +static void +test_substitute_envvars (void) +{ + struct { + const char *name; + const char *value; + } envvars[] = { + { "HOME", "/home/joe" }, + { "AVAR", "avar" }, + { "AVAR1", "avarx" }, + { "AVAR2", "avarxy" }, + { "AVAR3", "avarxyz" }, + { "AVAR0", "ava" }, + { "MY_VAR", "my_vars_value" }, + { "STRANGE{X}VAR", "strange{x}vars-value" }, + { "ZERO", "" } + }; + struct { + const char *string; + const char *result; + } tests[] = { + { "foo bar", + "foo bar" + }, + { "foo $HOME", + "foo /home/joe" + }, + { "foo $HOME ", + "foo /home/joe " + }, + { "foo $HOME$$", + "foo /home/joe$" + }, + { "foo ${HOME}/.ssh", + "foo /home/joe/.ssh" + }, + { "foo $HOME/.ssh", + "foo /home/joe/.ssh" + }, + { "foo $HOME_/.ssh", + "foo /.ssh" + }, + { "foo $HOME/.ssh/$MY_VAR:1", + "foo /home/joe/.ssh/my_vars_value:1" + }, + { "foo $HOME${MY_VAR}:1", + "foo /home/joemy_vars_value:1" + }, + { "${STRANGE{X}VAR}-bla", + "strange{x}vars-value-bla" + }, + { "${STRANGE{X}{VAR}-bla", /* missing "}" */ + "${STRANGE{X}{VAR}-bla" + }, + { "zero->$ZERO<-", + "zero-><-" + }, + { "->$AVAR.$AVAR1.$AVAR2.$AVAR3.$AVAR0<-", + "->avar.avarx.avarxy.avarxyz.ava<-" + }, + { "", + "" + } + }; + int idx; + char *res; + + for (idx=0; idx < DIM(envvars); idx++) + if (gnupg_setenv (envvars[idx].name, envvars[idx].value, 1)) + { + fprintf (stderr,"error setting envvar '%s' to '%s': %s\n", + envvars[idx].name, envvars[idx].value, + strerror (errno)); + exit (2); + } + + for (idx=0; idx < DIM(tests); idx++) + { + res = substitute_envvars (tests[idx].string); + if (!res) + { + fprintf (stderr,"error substituting '%s' (test %d): %s\n", + tests[idx].string, idx, strerror (errno)); + exit (2); + } + if (strcmp (res, tests[idx].result)) + { + fprintf (stderr, "substituted '%s'\n", tests[idx].string); + fprintf (stderr, " wanted '%s'\n", tests[idx].result); + fprintf (stderr, " got '%s'\n", res); + fail (idx); + } + xfree (res); + } +} + + int main (int argc, char **argv) { @@ -1213,6 +1311,7 @@ main (int argc, char **argv) test_split_fields_colon (); test_compare_version_strings (); test_format_text (); + test_substitute_envvars (); xfree (home_buffer); return !!errcount; |