Blame support/support_capture_subprocess_check.c

Packit 6c4009
/* Verify capture output from a subprocess.
Packit 6c4009
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <support/capture_subprocess.h>
Packit 6c4009
#include <support/check.h>
Packit Service 0ec72e
#include <sys/wait.h>
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
print_context (const char *context, bool *failed)
Packit 6c4009
{
Packit 6c4009
  if (*failed)
Packit 6c4009
    /* Do not duplicate message.  */
Packit 6c4009
    return;
Packit 6c4009
  support_record_failure ();
Packit 6c4009
  printf ("error: subprocess failed: %s\n", context);
Packit 6c4009
}
Packit 6c4009
Packit Service 0ec72e
static void
Packit Service 0ec72e
print_actual_status (struct support_capture_subprocess *proc)
Packit Service 0ec72e
{
Packit Service 0ec72e
  if (WIFEXITED (proc->status))
Packit Service 0ec72e
    printf ("error:   actual exit status: %d [0x%x]\n",
Packit Service 0ec72e
            WEXITSTATUS (proc->status), proc->status);
Packit Service 0ec72e
  else if (WIFSIGNALED (proc->status))
Packit Service 0ec72e
    printf ("error:   actual termination signal: %d [0x%x]\n",
Packit Service 0ec72e
            WTERMSIG (proc->status), proc->status);
Packit Service 0ec72e
  else
Packit Service 0ec72e
    printf ("error:   actual undecoded exit status: [0x%x]\n", proc->status);
Packit Service 0ec72e
}
Packit Service 0ec72e
Packit 6c4009
void
Packit 6c4009
support_capture_subprocess_check (struct support_capture_subprocess *proc,
Packit Service 0ec72e
                                  const char *context, int status_or_signal,
Packit 6c4009
                                  int allowed)
Packit 6c4009
{
Packit 6c4009
  TEST_VERIFY ((allowed & sc_allow_none)
Packit 6c4009
               || (allowed & sc_allow_stdout)
Packit 6c4009
               || (allowed & sc_allow_stderr));
Packit 6c4009
  TEST_VERIFY (!((allowed & sc_allow_none)
Packit 6c4009
                 && ((allowed & sc_allow_stdout)
Packit 6c4009
                     || (allowed & sc_allow_stderr))));
Packit 6c4009
Packit 6c4009
  bool failed = false;
Packit Service 0ec72e
  if (status_or_signal >= 0)
Packit 6c4009
    {
Packit Service 0ec72e
      /* Expect regular termination.  */
Packit Service 0ec72e
      if (!(WIFEXITED (proc->status)
Packit Service 0ec72e
            && WEXITSTATUS (proc->status) == status_or_signal))
Packit Service 0ec72e
        {
Packit Service 0ec72e
          print_context (context, &failed);
Packit Service 0ec72e
          printf ("error:   expected exit status: %d\n", status_or_signal);
Packit Service 0ec72e
          print_actual_status (proc);
Packit Service 0ec72e
        }
Packit Service 0ec72e
    }
Packit Service 0ec72e
  else
Packit Service 0ec72e
    {
Packit Service 0ec72e
      /* status_or_signal < 0.  Expect termination by signal.  */
Packit Service 0ec72e
      if (!(WIFSIGNALED (proc->status)
Packit Service 0ec72e
            && WTERMSIG (proc->status) == -status_or_signal))
Packit Service 0ec72e
        {
Packit Service 0ec72e
          print_context (context, &failed);
Packit Service 0ec72e
          printf ("error:   expected termination signal: %d\n",
Packit Service 0ec72e
                  -status_or_signal);
Packit Service 0ec72e
          print_actual_status (proc);
Packit Service 0ec72e
        }
Packit 6c4009
    }
Packit 6c4009
  if (!(allowed & sc_allow_stdout) && proc->out.length != 0)
Packit 6c4009
    {
Packit 6c4009
      print_context (context, &failed);
Packit 6c4009
      printf ("error:   unexpected output from subprocess\n");
Packit 6c4009
      fwrite (proc->out.buffer, proc->out.length, 1, stdout);
Packit 6c4009
      puts ("\n");
Packit 6c4009
    }
Packit 6c4009
  if (!(allowed & sc_allow_stderr) && proc->err.length != 0)
Packit 6c4009
    {
Packit 6c4009
      print_context (context, &failed);
Packit 6c4009
      printf ("error:   unexpected error output from subprocess\n");
Packit 6c4009
      fwrite (proc->err.buffer, proc->err.length, 1, stdout);
Packit 6c4009
      puts ("\n");
Packit 6c4009
    }
Packit 6c4009
}