From 9433dfa5dd4ba1f90a9c09c41a695f55b6c1f9a8 Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Wed, 7 Jun 2023 09:08:58 +0900 Subject: [PATCH] common: Add test case for IPC with spawned process. * common/Makefile.am (module_tests): Add t-exechelp. * common/t-exechelp.c [HAVE_W32_SYSTEM] (print_open_fds) (test_close_all_fds, main): Exclude the test_close_all_fds test. (run_server, test_pipe_stream): New. Signed-off-by: NIIBE Yutaka --- common/Makefile.am | 4 +- common/t-exechelp.c | 103 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 103 insertions(+), 4 deletions(-) diff --git a/common/Makefile.am b/common/Makefile.am index d5ab038bf..0d5af3e5c 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -161,11 +161,11 @@ module_tests = t-stringhelp t-timestuff \ t-convert t-percent t-gettime t-sysutils t-sexputil \ t-session-env t-openpgp-oid t-ssh-utils \ t-mapstrings t-zb32 t-mbox-util t-iobuf t-strlist \ - t-name-value t-ccparray t-recsel t-w32-cmdline + t-name-value t-ccparray t-recsel t-w32-cmdline t-exechelp if HAVE_W32_SYSTEM module_tests += t-w32-reg else -module_tests += t-exechelp t-exectool +module_tests += t-exectool endif if MAINTAINER_MODE diff --git a/common/t-exechelp.c b/common/t-exechelp.c index 3bf082bbb..613beb82b 100644 --- a/common/t-exechelp.c +++ b/common/t-exechelp.c @@ -29,7 +29,7 @@ static int verbose; - +#ifndef HAVE_W32_SYSTEM static void print_open_fds (int *array) { @@ -169,20 +169,119 @@ test_close_all_fds (void) } } +#endif + +static char buff12k[1024*12]; + +static void +run_server (void) +{ + estream_t fp; + int i; + char *p; + unsigned int len; + int ret; + + fp = es_fdopen_nc (1, "w"); + if (fp == NULL) + { + fprintf (stderr, "es_fdopen failed\n"); + exit (1); + } + + /* Fill the buffer by ASCII chars. */ + p = buff12k; + for (i = 0; i < sizeof (buff12k); i++) + if ((i % 64) == 63) + *p++ = '\n'; + else + *p++ = (i % 64) + '@'; + + len = sizeof (buff12k); + + ret = es_write (fp, (void *)&len, sizeof (len), NULL); + if (ret) + { + fprintf (stderr, "es_write (1) failed\n"); + exit (1); + } + + ret = es_write (fp, buff12k, sizeof (buff12k), NULL); + if (ret) + { + fprintf (stderr, "es_write (2) failed\n"); + exit (1); + } + es_fclose (fp); + exit (0); +} + + +static void +test_pipe_stream (const char *pgmname) +{ + gpg_error_t err; + gnupg_process_t proc; + estream_t outfp; + const char *argv[2]; + unsigned int len; + size_t n; + int ret; + + argv[0] = "--server"; + argv[1] = NULL; + + err = gnupg_process_spawn (pgmname, argv, + (GNUPG_PROCESS_STDOUT_PIPE + |GNUPG_PROCESS_STDERR_KEEP), + NULL, NULL, &proc); + if (err) + { + fprintf (stderr, "gnupg_process_spawn failed\n"); + exit (1); + } + + gnupg_process_get_streams (proc, 0, NULL, &outfp, NULL); + + ret = es_read (outfp, (void *)&len, sizeof (len), NULL); + if (ret) + { + fprintf (stderr, "es_read (1) failed\n"); + exit (1); + } + ret = es_read (outfp, buff12k, sizeof (buff12k), &n); + if (ret || n != sizeof (buff12k)) + { + fprintf (stderr, "es_read (2) failed\n"); + exit (1); + } + es_fclose (outfp); + gnupg_process_release (proc); +} int main (int argc, char **argv) { + const char *myname = "no-pgm"; + if (argc) - { argc--; argv++; } + { + myname = argv[0]; + argc--; argv++; + } if (argc && !strcmp (argv[0], "--verbose")) { verbose = 1; argc--; argv++; } + if (argc && !strcmp (argv[0], "--server")) + run_server (); +#ifndef HAVE_W32_SYSTEM test_close_all_fds (); +#endif + test_pipe_stream (myname); return 0; }