summaryrefslogtreecommitdiff
path: root/support/tst-support_descriptors.c
blob: ada760c1e233bdb37a7accd4f5c252f681f85b2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* Tests for monitoring file descriptor usage.
   Copyright (C) 2018-2019 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <fcntl.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <support/capture_subprocess.h>
#include <support/check.h>
#include <support/descriptors.h>
#include <support/support.h>
#include <support/xunistd.h>

/* This is the next free descriptor that the subprocess will pick.  */
static int free_descriptor;

static void
subprocess_no_change (void *closure)
{
  struct support_descriptors *descrs = support_descriptors_list ();
  int fd = xopen ("/dev/null", O_WRONLY, 0);
  TEST_COMPARE (fd, free_descriptor);
  xclose (fd);
  support_descriptors_free (descrs);
}

static void
subprocess_closed_descriptor (void *closure)
{
  int fd = xopen ("/dev/null", O_WRONLY, 0);
  TEST_COMPARE (fd, free_descriptor);
  struct support_descriptors *descrs = support_descriptors_list ();
  xclose (fd);
  support_descriptors_check (descrs); /* Will report failure.  */
  puts ("EOT");
  support_descriptors_free (descrs);
}

static void
subprocess_opened_descriptor (void *closure)
{
  struct support_descriptors *descrs = support_descriptors_list ();
  int fd = xopen ("/dev/null", O_WRONLY, 0);
  TEST_COMPARE (fd, free_descriptor);
  support_descriptors_check (descrs); /* Will report failure.  */
  puts ("EOT");
  support_descriptors_free (descrs);
}

static void
subprocess_changed_descriptor (void *closure)
{
  int fd = xopen ("/dev/null", O_WRONLY, 0);
  TEST_COMPARE (fd, free_descriptor);
  struct support_descriptors *descrs = support_descriptors_list ();
  xclose (fd);
  TEST_COMPARE (xopen ("/dev", O_DIRECTORY | O_RDONLY, 0), fd);
  support_descriptors_check (descrs); /* Will report failure.  */
  puts ("EOT");
  support_descriptors_free (descrs);
}

static void
report_subprocess_output (const char *name,
                          struct support_capture_subprocess *proc)
{
  printf ("info: BEGIN %s output\n"
          "%s"
          "info: END %s output\n",
          name, proc->out.buffer, name);
}

/* Use an explicit flag to preserve failure status across
   support_record_failure_reset calls.  */
static bool good = true;

static void
test_run (void)
{
  struct support_capture_subprocess proc = support_capture_subprocess
    (&subprocess_no_change, NULL);
  support_capture_subprocess_check (&proc, "subprocess_no_change",
                                    0, sc_allow_none);
  support_capture_subprocess_free (&proc);

  char *expected = xasprintf ("\nDifferences:\n"
                              "error: descriptor %d was closed\n"
                              "EOT\n",
                              free_descriptor);
  good = good && !support_record_failure_is_failed ();
  proc = support_capture_subprocess (&subprocess_closed_descriptor, NULL);
  good = good && support_record_failure_is_failed ();
  support_record_failure_reset (); /* Discard the reported error.  */
  report_subprocess_output ("subprocess_closed_descriptor", &proc);
  TEST_VERIFY (strstr (proc.out.buffer, expected) != NULL);
  support_capture_subprocess_check (&proc, "subprocess_closed_descriptor",
                                    0, sc_allow_stdout);
  support_capture_subprocess_free (&proc);
  free (expected);

  expected = xasprintf ("\nDifferences:\n"
                        "error: descriptor %d was opened (\"/dev/null\")\n"
                        "EOT\n",
                        free_descriptor);
  good = good && !support_record_failure_is_failed ();
  proc = support_capture_subprocess (&subprocess_opened_descriptor, NULL);
  good = good && support_record_failure_is_failed ();
  support_record_failure_reset (); /* Discard the reported error.  */
  report_subprocess_output ("subprocess_opened_descriptor", &proc);
  TEST_VERIFY (strstr (proc.out.buffer, expected) != NULL);
  support_capture_subprocess_check (&proc, "subprocess_opened_descriptor",
                                    0, sc_allow_stdout);
  support_capture_subprocess_free (&proc);
  free (expected);

  expected = xasprintf ("\nDifferences:\n"
                        "error: descriptor %d changed from \"/dev/null\""
                        " to \"/dev\"\n"
                        "error: descriptor %d changed ino ",
                        free_descriptor, free_descriptor);
  good = good && !support_record_failure_is_failed ();
  proc = support_capture_subprocess (&subprocess_changed_descriptor, NULL);
  good = good && support_record_failure_is_failed ();
  support_record_failure_reset (); /* Discard the reported error.  */
  report_subprocess_output ("subprocess_changed_descriptor", &proc);
  TEST_VERIFY (strstr (proc.out.buffer, expected) != NULL);
  support_capture_subprocess_check (&proc, "subprocess_changed_descriptor",
                                    0, sc_allow_stdout);
  support_capture_subprocess_free (&proc);
  free (expected);
}

static int
do_test (void)
{
  puts ("info: initial descriptor set");
  {
    struct support_descriptors *descrs = support_descriptors_list ();
    support_descriptors_dump (descrs, "info:  ", stdout);
    support_descriptors_free (descrs);
  }

  free_descriptor = xopen ("/dev/null", O_WRONLY, 0);
  puts ("info: descriptor set with additional free descriptor");
  {
    struct support_descriptors *descrs = support_descriptors_list ();
    support_descriptors_dump (descrs, "info:  ", stdout);
    support_descriptors_free (descrs);
  }
  TEST_VERIFY (free_descriptor >= 3);
  xclose (free_descriptor);

  /* Initial test run without a sentinel descriptor.  The presence of
     such a descriptor exercises different conditions in the list
     comparison in support_descriptors_check.  */
  test_run ();

  /* Allocate a sentinel descriptor at the end of the descriptor list,
     after free_descriptor.  */
  int sentinel_fd;
  {
    int fd = xopen ("/dev/full", O_WRONLY, 0);
    TEST_COMPARE (fd, free_descriptor);
    sentinel_fd = dup (fd);
    TEST_VERIFY_EXIT (sentinel_fd > fd);
    xclose (fd);
  }
  puts ("info: descriptor set with sentinel descriptor");
  {
    struct support_descriptors *descrs = support_descriptors_list ();
    support_descriptors_dump (descrs, "info:  ", stdout);
    support_descriptors_free (descrs);
  }

  /* Second test run with sentinel descriptor.  */
  test_run ();

  xclose (sentinel_fd);

  return !good;
}

#include <support/test-driver.c>