mirror of
git://git.gnupg.org/gnupg.git
synced 2025-07-03 22:56:33 +02:00
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
This commit is contained in:
parent
7f8ccb67e3
commit
9c272dc245
3 changed files with 206 additions and 0 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue