diff options
Diffstat (limited to 'common/t-exechelp.c')
-rw-r--r-- | common/t-exechelp.c | 152 |
1 files changed, 150 insertions, 2 deletions
diff --git a/common/t-exechelp.c b/common/t-exechelp.c index 3bf082bbb..2179ef2a0 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,168 @@ test_close_all_fds (void) } } +#endif + +static char buff12k[1024*12]; +static char buff4k[1024*4]; + +static void +run_server (void) +{ + estream_t fp; + int i; + char *p; + unsigned int len; + int ret; + es_syshd_t syshd; + size_t n; + off_t o; + +#ifdef HAVE_W32_SYSTEM + syshd.type = ES_SYSHD_HANDLE; + syshd.u.handle = (HANDLE)_get_osfhandle (1); +#else + syshd.type = ES_SYSHD_FD; + syshd.u.fd = 1; +#endif + + fp = es_sysopen_nc (&syshd, "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); + } + + es_fflush (fp); + + o = 0; + n = len; + + while (1) + { + size_t n0, n1; + + n0 = n > 4096 ? 4096 : n; + memcpy (buff4k, buff12k + o, n0); + + ret = es_write (fp, buff4k, n0, &n1); + if (ret || n0 != n1) + { + fprintf (stderr, "es_write (2) failed\n"); + exit (1); + } + + o += n0; + n -= n0; + if (n == 0) + break; + } + + 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; + off_t o; + 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); + } + + o = 0; + while (1) + { + if (es_feof (outfp)) + break; + + ret = es_read (outfp, buff4k, sizeof (buff4k), &n); + if (ret) + { + fprintf (stderr, "es_read (2) failed\n"); + exit (1); + } + + memcpy (buff12k + o, buff4k, n); + o += n; + } + + if (o != sizeof (buff12k)) + { + fprintf (stderr, "received data with wrong length %d\n", (int)o); + 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; } |