Blame support/tst-support_record_failure.c

Packit 6c4009
/* Test support_record_failure state sharing.
Packit 6c4009
   Copyright (C) 2016-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 <support/check.h>
Packit 6c4009
#include <support/support.h>
Packit 6c4009
#include <support/test-driver.h>
Packit 6c4009
#include <support/xunistd.h>
Packit 6c4009
Packit 6c4009
#include <getopt.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
static int exit_status_with_failure = -1;
Packit 6c4009
static bool test_verify;
Packit 6c4009
static bool test_verify_exit;
Packit 6c4009
enum
Packit 6c4009
  {
Packit 6c4009
    OPT_STATUS = 10001,
Packit 6c4009
    OPT_TEST_VERIFY,
Packit 6c4009
    OPT_TEST_VERIFY_EXIT,
Packit 6c4009
  };
Packit 6c4009
#define CMDLINE_OPTIONS                                                 \
Packit 6c4009
  { "status", required_argument, NULL, OPT_STATUS },                    \
Packit 6c4009
  { "test-verify", no_argument, NULL, OPT_TEST_VERIFY },                \
Packit 6c4009
  { "test-verify-exit", no_argument, NULL, OPT_TEST_VERIFY_EXIT },
Packit 6c4009
static void
Packit 6c4009
cmdline_process (int c)
Packit 6c4009
{
Packit 6c4009
  switch (c)
Packit 6c4009
    {
Packit 6c4009
    case OPT_STATUS:
Packit 6c4009
      exit_status_with_failure = atoi (optarg);
Packit 6c4009
      break;
Packit 6c4009
    case OPT_TEST_VERIFY:
Packit 6c4009
      test_verify = true;
Packit 6c4009
      break;
Packit 6c4009
    case OPT_TEST_VERIFY_EXIT:
Packit 6c4009
      test_verify_exit = true;
Packit 6c4009
      break;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
#define CMDLINE_PROCESS cmdline_process
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
check_failure_reporting (int phase, int zero, int unsupported)
Packit 6c4009
{
Packit 6c4009
  int status = support_report_failure (0);
Packit 6c4009
  if (status != zero)
Packit 6c4009
    {
Packit 6c4009
      printf ("real-error (phase %d): support_report_failure (0) == %d\n",
Packit 6c4009
              phase, status);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  status = support_report_failure (1);
Packit 6c4009
  if (status != 1)
Packit 6c4009
    {
Packit 6c4009
      printf ("real-error (phase %d): support_report_failure (1) == %d\n",
Packit 6c4009
              phase, status);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  status = support_report_failure (2);
Packit 6c4009
  if (status != 2)
Packit 6c4009
    {
Packit 6c4009
      printf ("real-error (phase %d): support_report_failure (2) == %d\n",
Packit 6c4009
              phase, status);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  status = support_report_failure (EXIT_UNSUPPORTED);
Packit 6c4009
  if (status != unsupported)
Packit 6c4009
    {
Packit 6c4009
      printf ("real-error (phase %d): "
Packit 6c4009
              "support_report_failure (EXIT_UNSUPPORTED) == %d\n",
Packit 6c4009
              phase, status);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  if (exit_status_with_failure >= 0)
Packit 6c4009
    {
Packit 6c4009
      /* External invocation with requested error status.  Used by
Packit 6c4009
         tst-support_report_failure-2.sh.  */
Packit 6c4009
      support_record_failure ();
Packit 6c4009
      return exit_status_with_failure;
Packit 6c4009
    }
Packit 6c4009
  TEST_VERIFY (true);
Packit 6c4009
  TEST_VERIFY_EXIT (true);
Packit 6c4009
  if (test_verify)
Packit 6c4009
    {
Packit 6c4009
      TEST_VERIFY (false);
Packit 6c4009
      if (test_verbose)
Packit 6c4009
        printf ("info: execution passed failed TEST_VERIFY\n");
Packit 6c4009
      return 2; /* Expected exit status.  */
Packit 6c4009
    }
Packit 6c4009
  if (test_verify_exit)
Packit 6c4009
    {
Packit 6c4009
      TEST_VERIFY_EXIT (false);
Packit 6c4009
      return 3; /* Not reached.  Expected exit status is 1.  */
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  printf ("info: This test tests the test framework.\n"
Packit 6c4009
          "info: It reports some expected errors on stdout.\n");
Packit 6c4009
Packit 6c4009
  /* Check that the status is passed through unchanged.  */
Packit 6c4009
  check_failure_reporting (1, 0, EXIT_UNSUPPORTED);
Packit 6c4009
Packit 6c4009
  /* Check state propagation from a subprocess.  */
Packit 6c4009
  pid_t pid = xfork ();
Packit 6c4009
  if (pid == 0)
Packit 6c4009
    {
Packit 6c4009
      support_record_failure ();
Packit 6c4009
      _exit (0);
Packit 6c4009
    }
Packit 6c4009
  int status;
Packit 6c4009
  xwaitpid (pid, &status, 0);
Packit 6c4009
  if (status != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("real-error: incorrect status from subprocess: %d\n", status);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  check_failure_reporting (2, 1, 1);
Packit 6c4009
Packit 6c4009
  /* Also test directly in the parent process.  */
Packit 6c4009
  support_record_failure_reset ();
Packit 6c4009
  check_failure_reporting (3, 0, EXIT_UNSUPPORTED);
Packit 6c4009
  support_record_failure ();
Packit 6c4009
  check_failure_reporting (4, 1, 1);
Packit 6c4009
Packit 6c4009
  /* We need to mask the failure above.  */
Packit 6c4009
  support_record_failure_reset ();
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>