1 /* Copyright (C) 2017-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 /* fprintf is a cancellation point, but getopt is not supposed to be a
19 cancellation point, even when it prints error messages. */
21 /* Note: getopt.h must be included first in this file, so we get the
22 GNU getopt rather than the POSIX one. */
33 #include <support/support.h>
34 #include <support/temp_file.h>
35 #include <support/xthread.h>
36 #include <support/xunistd.h>
39 check_stderr (bool expect_errmsg
, FILE *stderr_trapped
)
41 static char *lineptr
= 0;
42 static size_t linesz
= 0;
44 bool got_errmsg
= false;
45 rewind (stderr_trapped
);
46 while (getline (&lineptr
, &linesz
, stderr_trapped
) > 0)
49 fputs (lineptr
, stdout
);
51 rewind (stderr_trapped
);
52 xftruncate (fileno (stderr_trapped
), 0);
53 return got_errmsg
== expect_errmsg
;
60 const char *const argv
[8];
69 const struct option longopts
[4];
70 const char *const argv
[8];
75 #define DEFINE_TEST_DRIVER(test_type, getopt_call) \
76 struct test_type##_tdata \
78 pthread_mutex_t *sync; \
79 const struct test_type *tcase; \
84 test_type##_threadproc (void *data) \
86 struct test_type##_tdata *tdata = data; \
87 const struct test_type *tc = tdata->tcase; \
89 xpthread_mutex_lock (tdata->sync); \
90 xpthread_mutex_unlock (tdata->sync); \
92 /* At this point, this thread has a cancellation pending. \
93 We should still be able to get all the way through a getopt \
94 loop without being cancelled. \
95 Setting optind to 0 forces getopt to reinitialize itself. */ \
99 while (getopt_call != -1) \
103 pthread_testcancel(); \
108 do_##test_type (const struct test_type *tcase, FILE *stderr_trapped) \
110 pthread_mutex_t sync; \
111 struct test_type##_tdata tdata; \
113 printf("begin: %s\n", tcase->label); \
115 xpthread_mutex_init (&sync, 0); \
116 xpthread_mutex_lock (&sync); \
118 tdata.sync = &sync; \
119 tdata.tcase = tcase; \
122 pthread_t thr = xpthread_create (0, test_type##_threadproc, \
124 xpthread_cancel (thr); \
125 xpthread_mutex_unlock (&sync); \
126 void *rv = xpthread_join (thr); \
128 xpthread_mutex_destroy (&sync); \
131 if (!check_stderr (tcase->expect_errmsg, stderr_trapped)) \
134 printf("FAIL: %s: stderr not as expected\n", tcase->label); \
139 printf("FAIL: %s: did not complete loop\n", tcase->label); \
141 if (rv != PTHREAD_CANCELED) \
144 printf("FAIL: %s: thread was not cancelled\n", tcase->label); \
147 printf ("pass: %s\n", tcase->label); \
151 DEFINE_TEST_DRIVER (test_short
,
152 getopt (tc
->argc
, (char *const *)tc
->argv
, tc
->opts
))
153 DEFINE_TEST_DRIVER (test_long
,
154 getopt_long (tc
->argc
, (char *const *)tc
->argv
,
155 tc
->opts
, tc
->longopts
, 0))
157 /* Caution: all option strings must begin with a '+' or '-' so that
158 getopt does not attempt to permute the argument vector (which is in
159 read-only memory). */
160 const struct test_short tests_short
[] = {
162 "+ab:c", { "program", "-ac", "-b", "x", 0 }, 4, false },
164 "+ab:c", { "program", "-d", 0 }, 2, true },
165 { "missing argument",
166 "+ab:c", { "program", "-b", 0 }, 2, true },
170 const struct test_long tests_long
[] = {
171 { "no errors (long)",
172 "+ab:c", { { "alpha", no_argument
, 0, 'a' },
173 { "bravo", required_argument
, 0, 'b' },
174 { "charlie", no_argument
, 0, 'c' },
176 { "program", "-a", "--charlie", "--bravo=x", 0 }, 4, false },
178 { "invalid option (long)",
179 "+ab:c", { { "alpha", no_argument
, 0, 'a' },
180 { "bravo", required_argument
, 0, 'b' },
181 { "charlie", no_argument
, 0, 'c' },
183 { "program", "-a", "--charlie", "--dingo", 0 }, 4, true },
185 { "unwanted argument",
186 "+ab:c", { { "alpha", no_argument
, 0, 'a' },
187 { "bravo", required_argument
, 0, 'b' },
188 { "charlie", no_argument
, 0, 'c' },
190 { "program", "-a", "--charlie=dingo", "--bravo=x", 0 }, 4, true },
192 { "missing argument",
193 "+ab:c", { { "alpha", no_argument
, 0, 'a' },
194 { "bravo", required_argument
, 0, 'b' },
195 { "charlie", no_argument
, 0, 'c' },
197 { "program", "-a", "--charlie", "--bravo", 0 }, 4, true },
199 { "ambiguous options",
200 "+uvw", { { "veni", no_argument
, 0, 'u' },
201 { "vedi", no_argument
, 0, 'v' },
202 { "veci", no_argument
, 0, 'w' } },
203 { "program", "--ve", 0 }, 2, true },
205 { "no errors (long W)",
206 "+ab:cW;", { { "alpha", no_argument
, 0, 'a' },
207 { "bravo", required_argument
, 0, 'b' },
208 { "charlie", no_argument
, 0, 'c' },
210 { "program", "-a", "-W", "charlie", "-W", "bravo=x", 0 }, 6, false },
212 { "missing argument (W itself)",
213 "+ab:cW;", { { "alpha", no_argument
, 0, 'a' },
214 { "bravo", required_argument
, 0, 'b' },
215 { "charlie", no_argument
, 0, 'c' },
217 { "program", "-a", "-W", "charlie", "-W", 0 }, 5, true },
219 { "missing argument (W longopt)",
220 "+ab:cW;", { { "alpha", no_argument
, 0, 'a' },
221 { "bravo", required_argument
, 0, 'b' },
222 { "charlie", no_argument
, 0, 'c' },
224 { "program", "-a", "-W", "charlie", "-W", "bravo", 0 }, 6, true },
226 { "unwanted argument (W longopt)",
227 "+ab:cW;", { { "alpha", no_argument
, 0, 'a' },
228 { "bravo", required_argument
, 0, 'b' },
229 { "charlie", no_argument
, 0, 'c' },
231 { "program", "-a", "-W", "charlie=dingo", "-W", "bravo=x", 0 }, 6, true },
233 { "ambiguous options (W)",
234 "+uvwW;", { { "veni", no_argument
, 0, 'u' },
235 { "vedi", no_argument
, 0, 'v' },
236 { "veci", no_argument
, 0, 'w' } },
237 { "program", "-W", "ve", 0 }, 3, true },
245 int stderr_trap
= create_temp_file ("stderr", 0);
248 perror ("create_temp_file");
251 FILE *stderr_trapped
= fdopen(stderr_trap
, "r+");
257 int old_stderr
= dup (fileno (stderr
));
263 if (dup2 (stderr_trap
, 2) < 0)
272 for (const struct test_short
*tcase
= tests_short
; tcase
->label
; tcase
++)
273 success
= do_test_short (tcase
, stderr_trapped
) && success
;
275 for (const struct test_long
*tcase
= tests_long
; tcase
->label
; tcase
++)
276 success
= do_test_long (tcase
, stderr_trapped
) && success
;
278 dup2 (old_stderr
, 2);
280 fclose (stderr_trapped
);
282 return success
? 0 : 1;
285 #include <support/test-driver.c>