Blame support/tst-support_descriptors.c

Packit Service e05c66
/* Tests for monitoring file descriptor usage.
Packit Service e05c66
   Copyright (C) 2018 Free Software Foundation, Inc.
Packit Service e05c66
   This file is part of the GNU C Library.
Packit Service e05c66
Packit Service e05c66
   The GNU C Library is free software; you can redistribute it and/or
Packit Service e05c66
   modify it under the terms of the GNU Lesser General Public
Packit Service e05c66
   License as published by the Free Software Foundation; either
Packit Service e05c66
   version 2.1 of the License, or (at your option) any later version.
Packit Service e05c66
Packit Service e05c66
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service e05c66
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service e05c66
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service e05c66
   Lesser General Public License for more details.
Packit Service e05c66
Packit Service e05c66
   You should have received a copy of the GNU Lesser General Public
Packit Service e05c66
   License along with the GNU C Library; if not, see
Packit Service e05c66
   <http://www.gnu.org/licenses/>.  */
Packit Service e05c66
Packit Service e05c66
#include <fcntl.h>
Packit Service e05c66
#include <stdbool.h>
Packit Service e05c66
#include <stdlib.h>
Packit Service e05c66
#include <string.h>
Packit Service e05c66
#include <support/capture_subprocess.h>
Packit Service e05c66
#include <support/check.h>
Packit Service e05c66
#include <support/descriptors.h>
Packit Service e05c66
#include <support/support.h>
Packit Service e05c66
#include <support/xunistd.h>
Packit Service e05c66
Packit Service e05c66
/* This is the next free descriptor that the subprocess will pick.  */
Packit Service e05c66
static int free_descriptor;
Packit Service e05c66
Packit Service e05c66
static void
Packit Service e05c66
subprocess_no_change (void *closure)
Packit Service e05c66
{
Packit Service e05c66
  struct support_descriptors *descrs = support_descriptors_list ();
Packit Service e05c66
  int fd = xopen ("/dev/null", O_WRONLY, 0);
Packit Service e05c66
  TEST_COMPARE (fd, free_descriptor);
Packit Service e05c66
  xclose (fd);
Packit Service e05c66
  support_descriptors_free (descrs);
Packit Service e05c66
}
Packit Service e05c66
Packit Service e05c66
static void
Packit Service e05c66
subprocess_closed_descriptor (void *closure)
Packit Service e05c66
{
Packit Service e05c66
  int fd = xopen ("/dev/null", O_WRONLY, 0);
Packit Service e05c66
  TEST_COMPARE (fd, free_descriptor);
Packit Service e05c66
  struct support_descriptors *descrs = support_descriptors_list ();
Packit Service e05c66
  xclose (fd);
Packit Service e05c66
  support_descriptors_check (descrs); /* Will report failure.  */
Packit Service e05c66
  puts ("EOT");
Packit Service e05c66
  support_descriptors_free (descrs);
Packit Service e05c66
}
Packit Service e05c66
Packit Service e05c66
static void
Packit Service e05c66
subprocess_opened_descriptor (void *closure)
Packit Service e05c66
{
Packit Service e05c66
  struct support_descriptors *descrs = support_descriptors_list ();
Packit Service e05c66
  int fd = xopen ("/dev/null", O_WRONLY, 0);
Packit Service e05c66
  TEST_COMPARE (fd, free_descriptor);
Packit Service e05c66
  support_descriptors_check (descrs); /* Will report failure.  */
Packit Service e05c66
  puts ("EOT");
Packit Service e05c66
  support_descriptors_free (descrs);
Packit Service e05c66
}
Packit Service e05c66
Packit Service e05c66
static void
Packit Service e05c66
subprocess_changed_descriptor (void *closure)
Packit Service e05c66
{
Packit Service e05c66
  int fd = xopen ("/dev/null", O_WRONLY, 0);
Packit Service e05c66
  TEST_COMPARE (fd, free_descriptor);
Packit Service e05c66
  struct support_descriptors *descrs = support_descriptors_list ();
Packit Service e05c66
  xclose (fd);
Packit Service e05c66
  TEST_COMPARE (xopen ("/dev", O_DIRECTORY | O_RDONLY, 0), fd);
Packit Service e05c66
  support_descriptors_check (descrs); /* Will report failure.  */
Packit Service e05c66
  puts ("EOT");
Packit Service e05c66
  support_descriptors_free (descrs);
Packit Service e05c66
}
Packit Service e05c66
Packit Service e05c66
static void
Packit Service e05c66
report_subprocess_output (const char *name,
Packit Service e05c66
                          struct support_capture_subprocess *proc)
Packit Service e05c66
{
Packit Service e05c66
  printf ("info: BEGIN %s output\n"
Packit Service e05c66
          "%s"
Packit Service e05c66
          "info: END %s output\n",
Packit Service e05c66
          name, proc->out.buffer, name);
Packit Service e05c66
}
Packit Service e05c66
Packit Service e05c66
/* Use an explicit flag to preserve failure status across
Packit Service e05c66
   support_record_failure_reset calls.  */
Packit Service e05c66
static bool good = true;
Packit Service e05c66
Packit Service e05c66
static void
Packit Service e05c66
test_run (void)
Packit Service e05c66
{
Packit Service e05c66
  struct support_capture_subprocess proc = support_capture_subprocess
Packit Service e05c66
    (&subprocess_no_change, NULL);
Packit Service e05c66
  support_capture_subprocess_check (&proc, "subprocess_no_change",
Packit Service e05c66
                                    0, sc_allow_none);
Packit Service e05c66
  support_capture_subprocess_free (&proc;;
Packit Service e05c66
Packit Service e05c66
  char *expected = xasprintf ("\nDifferences:\n"
Packit Service e05c66
                              "error: descriptor %d was closed\n"
Packit Service e05c66
                              "EOT\n",
Packit Service e05c66
                              free_descriptor);
Packit Service e05c66
  good = good && !support_record_failure_is_failed ();
Packit Service e05c66
  proc = support_capture_subprocess (&subprocess_closed_descriptor, NULL);
Packit Service e05c66
  good = good && support_record_failure_is_failed ();
Packit Service e05c66
  support_record_failure_reset (); /* Discard the reported error.  */
Packit Service e05c66
  report_subprocess_output ("subprocess_closed_descriptor", &proc;;
Packit Service e05c66
  TEST_VERIFY (strstr (proc.out.buffer, expected) != NULL);
Packit Service e05c66
  support_capture_subprocess_check (&proc, "subprocess_closed_descriptor",
Packit Service e05c66
                                    0, sc_allow_stdout);
Packit Service e05c66
  support_capture_subprocess_free (&proc;;
Packit Service e05c66
  free (expected);
Packit Service e05c66
Packit Service e05c66
  expected = xasprintf ("\nDifferences:\n"
Packit Service e05c66
                        "error: descriptor %d was opened (\"/dev/null\")\n"
Packit Service e05c66
                        "EOT\n",
Packit Service e05c66
                        free_descriptor);
Packit Service e05c66
  good = good && !support_record_failure_is_failed ();
Packit Service e05c66
  proc = support_capture_subprocess (&subprocess_opened_descriptor, NULL);
Packit Service e05c66
  good = good && support_record_failure_is_failed ();
Packit Service e05c66
  support_record_failure_reset (); /* Discard the reported error.  */
Packit Service e05c66
  report_subprocess_output ("subprocess_opened_descriptor", &proc;;
Packit Service e05c66
  TEST_VERIFY (strstr (proc.out.buffer, expected) != NULL);
Packit Service e05c66
  support_capture_subprocess_check (&proc, "subprocess_opened_descriptor",
Packit Service e05c66
                                    0, sc_allow_stdout);
Packit Service e05c66
  support_capture_subprocess_free (&proc;;
Packit Service e05c66
  free (expected);
Packit Service e05c66
Packit Service e05c66
  expected = xasprintf ("\nDifferences:\n"
Packit Service e05c66
                        "error: descriptor %d changed from \"/dev/null\""
Packit Service e05c66
                        " to \"/dev\"\n"
Packit Service e05c66
                        "error: descriptor %d changed ino ",
Packit Service e05c66
                        free_descriptor, free_descriptor);
Packit Service e05c66
  good = good && !support_record_failure_is_failed ();
Packit Service e05c66
  proc = support_capture_subprocess (&subprocess_changed_descriptor, NULL);
Packit Service e05c66
  good = good && support_record_failure_is_failed ();
Packit Service e05c66
  support_record_failure_reset (); /* Discard the reported error.  */
Packit Service e05c66
  report_subprocess_output ("subprocess_changed_descriptor", &proc;;
Packit Service e05c66
  TEST_VERIFY (strstr (proc.out.buffer, expected) != NULL);
Packit Service e05c66
  support_capture_subprocess_check (&proc, "subprocess_changed_descriptor",
Packit Service e05c66
                                    0, sc_allow_stdout);
Packit Service e05c66
  support_capture_subprocess_free (&proc;;
Packit Service e05c66
  free (expected);
Packit Service e05c66
}
Packit Service e05c66
Packit Service e05c66
static int
Packit Service e05c66
do_test (void)
Packit Service e05c66
{
Packit Service e05c66
  puts ("info: initial descriptor set");
Packit Service e05c66
  {
Packit Service e05c66
    struct support_descriptors *descrs = support_descriptors_list ();
Packit Service e05c66
    support_descriptors_dump (descrs, "info:  ", stdout);
Packit Service e05c66
    support_descriptors_free (descrs);
Packit Service e05c66
  }
Packit Service e05c66
Packit Service e05c66
  free_descriptor = xopen ("/dev/null", O_WRONLY, 0);
Packit Service e05c66
  puts ("info: descriptor set with additional free descriptor");
Packit Service e05c66
  {
Packit Service e05c66
    struct support_descriptors *descrs = support_descriptors_list ();
Packit Service e05c66
    support_descriptors_dump (descrs, "info:  ", stdout);
Packit Service e05c66
    support_descriptors_free (descrs);
Packit Service e05c66
  }
Packit Service e05c66
  TEST_VERIFY (free_descriptor >= 3);
Packit Service e05c66
  xclose (free_descriptor);
Packit Service e05c66
Packit Service e05c66
  /* Initial test run without a sentinel descriptor.  The presence of
Packit Service e05c66
     such a descriptor exercises different conditions in the list
Packit Service e05c66
     comparison in support_descriptors_check.  */
Packit Service e05c66
  test_run ();
Packit Service e05c66
Packit Service e05c66
  /* Allocate a sentinel descriptor at the end of the descriptor list,
Packit Service e05c66
     after free_descriptor.  */
Packit Service e05c66
  int sentinel_fd;
Packit Service e05c66
  {
Packit Service e05c66
    int fd = xopen ("/dev/full", O_WRONLY, 0);
Packit Service e05c66
    TEST_COMPARE (fd, free_descriptor);
Packit Service e05c66
    sentinel_fd = dup (fd);
Packit Service e05c66
    TEST_VERIFY_EXIT (sentinel_fd > fd);
Packit Service e05c66
    xclose (fd);
Packit Service e05c66
  }
Packit Service e05c66
  puts ("info: descriptor set with sentinel descriptor");
Packit Service e05c66
  {
Packit Service e05c66
    struct support_descriptors *descrs = support_descriptors_list ();
Packit Service e05c66
    support_descriptors_dump (descrs, "info:  ", stdout);
Packit Service e05c66
    support_descriptors_free (descrs);
Packit Service e05c66
  }
Packit Service e05c66
Packit Service e05c66
  /* Second test run with sentinel descriptor.  */
Packit Service e05c66
  test_run ();
Packit Service e05c66
Packit Service e05c66
  xclose (sentinel_fd);
Packit Service e05c66
Packit Service e05c66
  return !good;
Packit Service e05c66
}
Packit Service e05c66
Packit Service e05c66
#include <support/test-driver.c>