1 /* Tests for monitoring file descriptor usage.
2 Copyright (C) 2018-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
23 #include <support/capture_subprocess.h>
24 #include <support/check.h>
25 #include <support/descriptors.h>
26 #include <support/support.h>
27 #include <support/xunistd.h>
29 /* This is the next free descriptor that the subprocess will pick. */
30 static int free_descriptor
;
33 subprocess_no_change (void *closure
)
35 struct support_descriptors
*descrs
= support_descriptors_list ();
36 int fd
= xopen ("/dev/null", O_WRONLY
, 0);
37 TEST_COMPARE (fd
, free_descriptor
);
39 support_descriptors_free (descrs
);
43 subprocess_closed_descriptor (void *closure
)
45 int fd
= xopen ("/dev/null", O_WRONLY
, 0);
46 TEST_COMPARE (fd
, free_descriptor
);
47 struct support_descriptors
*descrs
= support_descriptors_list ();
49 support_descriptors_check (descrs
); /* Will report failure. */
51 support_descriptors_free (descrs
);
55 subprocess_opened_descriptor (void *closure
)
57 struct support_descriptors
*descrs
= support_descriptors_list ();
58 int fd
= xopen ("/dev/null", O_WRONLY
, 0);
59 TEST_COMPARE (fd
, free_descriptor
);
60 support_descriptors_check (descrs
); /* Will report failure. */
62 support_descriptors_free (descrs
);
66 subprocess_changed_descriptor (void *closure
)
68 int fd
= xopen ("/dev/null", O_WRONLY
, 0);
69 TEST_COMPARE (fd
, free_descriptor
);
70 struct support_descriptors
*descrs
= support_descriptors_list ();
72 TEST_COMPARE (xopen ("/dev", O_DIRECTORY
| O_RDONLY
, 0), fd
);
73 support_descriptors_check (descrs
); /* Will report failure. */
75 support_descriptors_free (descrs
);
79 report_subprocess_output (const char *name
,
80 struct support_capture_subprocess
*proc
)
82 printf ("info: BEGIN %s output\n"
84 "info: END %s output\n",
85 name
, proc
->out
.buffer
, name
);
88 /* Use an explicit flag to preserve failure status across
89 support_record_failure_reset calls. */
90 static bool good
= true;
95 struct support_capture_subprocess proc
= support_capture_subprocess
96 (&subprocess_no_change
, NULL
);
97 support_capture_subprocess_check (&proc
, "subprocess_no_change",
99 support_capture_subprocess_free (&proc
);
101 char *expected
= xasprintf ("\nDifferences:\n"
102 "error: descriptor %d was closed\n"
105 good
= good
&& !support_record_failure_is_failed ();
106 proc
= support_capture_subprocess (&subprocess_closed_descriptor
, NULL
);
107 good
= good
&& support_record_failure_is_failed ();
108 support_record_failure_reset (); /* Discard the reported error. */
109 report_subprocess_output ("subprocess_closed_descriptor", &proc
);
110 TEST_VERIFY (strstr (proc
.out
.buffer
, expected
) != NULL
);
111 support_capture_subprocess_check (&proc
, "subprocess_closed_descriptor",
113 support_capture_subprocess_free (&proc
);
116 expected
= xasprintf ("\nDifferences:\n"
117 "error: descriptor %d was opened (\"/dev/null\")\n"
120 good
= good
&& !support_record_failure_is_failed ();
121 proc
= support_capture_subprocess (&subprocess_opened_descriptor
, NULL
);
122 good
= good
&& support_record_failure_is_failed ();
123 support_record_failure_reset (); /* Discard the reported error. */
124 report_subprocess_output ("subprocess_opened_descriptor", &proc
);
125 TEST_VERIFY (strstr (proc
.out
.buffer
, expected
) != NULL
);
126 support_capture_subprocess_check (&proc
, "subprocess_opened_descriptor",
128 support_capture_subprocess_free (&proc
);
131 expected
= xasprintf ("\nDifferences:\n"
132 "error: descriptor %d changed from \"/dev/null\""
134 "error: descriptor %d changed ino ",
135 free_descriptor
, free_descriptor
);
136 good
= good
&& !support_record_failure_is_failed ();
137 proc
= support_capture_subprocess (&subprocess_changed_descriptor
, NULL
);
138 good
= good
&& support_record_failure_is_failed ();
139 support_record_failure_reset (); /* Discard the reported error. */
140 report_subprocess_output ("subprocess_changed_descriptor", &proc
);
141 TEST_VERIFY (strstr (proc
.out
.buffer
, expected
) != NULL
);
142 support_capture_subprocess_check (&proc
, "subprocess_changed_descriptor",
144 support_capture_subprocess_free (&proc
);
151 puts ("info: initial descriptor set");
153 struct support_descriptors
*descrs
= support_descriptors_list ();
154 support_descriptors_dump (descrs
, "info: ", stdout
);
155 support_descriptors_free (descrs
);
158 free_descriptor
= xopen ("/dev/null", O_WRONLY
, 0);
159 puts ("info: descriptor set with additional free descriptor");
161 struct support_descriptors
*descrs
= support_descriptors_list ();
162 support_descriptors_dump (descrs
, "info: ", stdout
);
163 support_descriptors_free (descrs
);
165 TEST_VERIFY (free_descriptor
>= 3);
166 xclose (free_descriptor
);
168 /* Initial test run without a sentinel descriptor. The presence of
169 such a descriptor exercises different conditions in the list
170 comparison in support_descriptors_check. */
173 /* Allocate a sentinel descriptor at the end of the descriptor list,
174 after free_descriptor. */
177 int fd
= xopen ("/dev/full", O_WRONLY
, 0);
178 TEST_COMPARE (fd
, free_descriptor
);
179 sentinel_fd
= dup (fd
);
180 TEST_VERIFY_EXIT (sentinel_fd
> fd
);
183 puts ("info: descriptor set with sentinel descriptor");
185 struct support_descriptors
*descrs
= support_descriptors_list ();
186 support_descriptors_dump (descrs
, "info: ", stdout
);
187 support_descriptors_free (descrs
);
190 /* Second test run with sentinel descriptor. */
193 xclose (sentinel_fd
);
198 #include <support/test-driver.c>