tests: Simplify fake-pinentry to use the option only.

* tests/openpgp/fake-pinentry.c (parse_pinentry_user_data): New.
(main): Don't use PINENTRY_USER_DATA env var.

--

Since environment variable is unreliable, use the option only.

Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
This commit is contained in:
NIIBE Yutaka 2022-12-02 16:55:49 +09:00
parent 7c6b014d3b
commit 0a93b5b96a
No known key found for this signature in database
GPG Key ID: 640114AF89DE6054
1 changed files with 41 additions and 31 deletions

View File

@ -196,26 +196,20 @@ option_value (const char *line, const char *name)
return NULL; return NULL;
} }
int static int
main (int argc, char **argv) parse_pinentry_user_data (const char *args,
char **r_passphrase)
{ {
char *args;
char *option_user_data = NULL;
char *logfile; char *logfile;
char *passphrasefile; char *passphrasefile;
char *passphrase; char *passphrase;
/* We get our options via PINENTRY_USER_DATA. */ *r_passphrase = NULL;
(void) argc, (void) argv;
setvbuf (stdin, NULL, _IOLBF, BUFSIZ); if (log_stream)
setvbuf (stdout, NULL, _IOLBF, BUFSIZ); fclose (log_stream);
log_stream = NULL;
args = getenv ("PINENTRY_USER_DATA");
if (! args)
args = "";
restart:
logfile = option_value (args, "--logfile"); logfile = option_value (args, "--logfile");
if (logfile) if (logfile)
{ {
@ -230,7 +224,7 @@ main (int argc, char **argv)
if (! log_stream) if (! log_stream)
{ {
perror (logfile); perror (logfile);
return 1; return -1;
} }
} }
@ -249,20 +243,31 @@ main (int argc, char **argv)
{ {
reply ("# Passphrasefile '%s' is empty. Terminating.\n", reply ("# Passphrasefile '%s' is empty. Terminating.\n",
passphrasefile); passphrasefile);
return 1; return -1;
} }
rstrip (passphrase); rstrip (passphrase);
} }
else else
{ passphrase = strdup (skip_options (args));
passphrase = skip_options (args);
if (*passphrase == 0)
passphrase = "no PINENTRY_USER_DATA -- using default passphrase";
}
reply ("# fake-pinentry(%u) started. Passphrase='%s'.\n", *r_passphrase = passphrase;
(unsigned int)getpid (), passphrase); return 0;
}
int
main (int argc, char **argv)
{
char *passphrase = NULL;
/* We get our options via PINENTRY_USER_DATA. */
(void) argc, (void) argv;
setvbuf (stdin, NULL, _IOLBF, BUFSIZ);
setvbuf (stdout, NULL, _IOLBF, BUFSIZ);
reply ("# fake-pinentry(%u) started.\n", (unsigned int)getpid ());
reply ("OK - what's up?\n"); reply ("OK - what's up?\n");
while (! feof (stdin)) while (! feof (stdin))
@ -280,7 +285,12 @@ main (int argc, char **argv)
#define OPT_USER_DATA "OPTION pinentry-user-data=" #define OPT_USER_DATA "OPTION pinentry-user-data="
if (strncmp (buffer, "GETPIN", 6) == 0) if (strncmp (buffer, "GETPIN", 6) == 0)
reply ("D %s\n", passphrase); {
if (passphrase)
reply ("D %s\n", passphrase);
else
reply ("D deafult\n");
}
else if (strncmp (buffer, "BYE", 3) == 0) else if (strncmp (buffer, "BYE", 3) == 0)
{ {
reply ("OK\n"); reply ("OK\n");
@ -288,13 +298,12 @@ main (int argc, char **argv)
} }
else if (strncmp (buffer, OPT_USER_DATA, strlen (OPT_USER_DATA)) == 0) else if (strncmp (buffer, OPT_USER_DATA, strlen (OPT_USER_DATA)) == 0)
{ {
/* Prefer interactive data to the one from environment variable. */ if (parse_pinentry_user_data (buffer + strlen (OPT_USER_DATA),
if (log_stream) &passphrase) < 0)
fclose (log_stream); {
log_stream = NULL; /* Failure. */
free (option_user_data); return 1;
option_user_data = args = strdup (buffer + strlen (OPT_USER_DATA)); }
goto restart;
} }
reply ("OK\n"); reply ("OK\n");
@ -306,6 +315,7 @@ main (int argc, char **argv)
if (log_stream) if (log_stream)
fclose (log_stream); fclose (log_stream);
free (option_user_data); if (passphrase)
free (passphrase);
return 0; return 0;
} }