1 /* Copyright (C) 2017-2019 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>
38 check_stderr (bool expect_errmsg
, FILE *stderr_trapped
)
40 static char *lineptr
= 0;
41 static size_t linesz
= 0;
43 bool got_errmsg
= false;
44 rewind (stderr_trapped
);
45 while (getline (&lineptr
, &linesz
, stderr_trapped
) > 0)
48 fputs (lineptr
, stdout
);
50 rewind (stderr_trapped
);
51 ftruncate (fileno (stderr_trapped
), 0);
52 return got_errmsg
== expect_errmsg
;
59 const char *const argv
[8];
68 const struct option longopts
[4];
69 const char *const argv
[8];
74 #define DEFINE_TEST_DRIVER(test_type, getopt_call) \
75 struct test_type##_tdata \
77 pthread_mutex_t *sync; \
78 const struct test_type *tcase; \
83 test_type##_threadproc (void *data) \
85 struct test_type##_tdata *tdata = data; \
86 const struct test_type *tc = tdata->tcase; \
88 xpthread_mutex_lock (tdata->sync); \
89 xpthread_mutex_unlock (tdata->sync); \
91 /* At this point, this thread has a cancellation pending. \
92 We should still be able to get all the way through a getopt \
93 loop without being cancelled. \
94 Setting optind to 0 forces getopt to reinitialize itself. */ \
98 while (getopt_call != -1) \
102 pthread_testcancel(); \
107 do_##test_type (const struct test_type *tcase, FILE *stderr_trapped) \
109 pthread_mutex_t sync; \
110 struct test_type##_tdata tdata; \
112 printf("begin: %s\n", tcase->label); \
114 xpthread_mutex_init (&sync, 0); \
115 xpthread_mutex_lock (&sync); \
117 tdata.sync = &sync; \
118 tdata.tcase = tcase; \
121 pthread_t thr = xpthread_create (0, test_type##_threadproc, \
123 xpthread_cancel (thr); \
124 xpthread_mutex_unlock (&sync); \
125 void *rv = xpthread_join (thr); \
127 xpthread_mutex_destroy (&sync); \
130 if (!check_stderr (tcase->expect_errmsg, stderr_trapped)) \
133 printf("FAIL: %s: stderr not as expected\n", tcase->label); \
138 printf("FAIL: %s: did not complete loop\n", tcase->label); \
140 if (rv != PTHREAD_CANCELED) \
143 printf("FAIL: %s: thread was not cancelled\n", tcase->label); \
146 printf ("pass: %s\n", tcase->label); \
150 DEFINE_TEST_DRIVER (test_short
,
151 getopt (tc
->argc
, (char *const *)tc
->argv
, tc
->opts
))
152 DEFINE_TEST_DRIVER (test_long
,
153 getopt_long (tc
->argc
, (char *const *)tc
->argv
,
154 tc
->opts
, tc
->longopts
, 0))
156 /* Caution: all option strings must begin with a '+' or '-' so that
157 getopt does not attempt to permute the argument vector (which is in
158 read-only memory). */
159 const struct test_short tests_short
[] = {
161 "+ab:c", { "program", "-ac", "-b", "x", 0 }, 4, false },
163 "+ab:c", { "program", "-d", 0 }, 2, true },
164 { "missing argument",
165 "+ab:c", { "program", "-b", 0 }, 2, true },
169 const struct test_long tests_long
[] = {
170 { "no errors (long)",
171 "+ab:c", { { "alpha", no_argument
, 0, 'a' },
172 { "bravo", required_argument
, 0, 'b' },
173 { "charlie", no_argument
, 0, 'c' },
175 { "program", "-a", "--charlie", "--bravo=x", 0 }, 4, false },
177 { "invalid option (long)",
178 "+ab:c", { { "alpha", no_argument
, 0, 'a' },
179 { "bravo", required_argument
, 0, 'b' },
180 { "charlie", no_argument
, 0, 'c' },
182 { "program", "-a", "--charlie", "--dingo", 0 }, 4, true },
184 { "unwanted argument",
185 "+ab:c", { { "alpha", no_argument
, 0, 'a' },
186 { "bravo", required_argument
, 0, 'b' },
187 { "charlie", no_argument
, 0, 'c' },
189 { "program", "-a", "--charlie=dingo", "--bravo=x", 0 }, 4, true },
191 { "missing argument",
192 "+ab:c", { { "alpha", no_argument
, 0, 'a' },
193 { "bravo", required_argument
, 0, 'b' },
194 { "charlie", no_argument
, 0, 'c' },
196 { "program", "-a", "--charlie", "--bravo", 0 }, 4, true },
198 { "ambiguous options",
199 "+uvw", { { "veni", no_argument
, 0, 'u' },
200 { "vedi", no_argument
, 0, 'v' },
201 { "veci", no_argument
, 0, 'w' } },
202 { "program", "--ve", 0 }, 2, true },
204 { "no errors (long W)",
205 "+ab:cW;", { { "alpha", no_argument
, 0, 'a' },
206 { "bravo", required_argument
, 0, 'b' },
207 { "charlie", no_argument
, 0, 'c' },
209 { "program", "-a", "-W", "charlie", "-W", "bravo=x", 0 }, 6, false },
211 { "missing argument (W itself)",
212 "+ab:cW;", { { "alpha", no_argument
, 0, 'a' },
213 { "bravo", required_argument
, 0, 'b' },
214 { "charlie", no_argument
, 0, 'c' },
216 { "program", "-a", "-W", "charlie", "-W", 0 }, 5, true },
218 { "missing argument (W longopt)",
219 "+ab:cW;", { { "alpha", no_argument
, 0, 'a' },
220 { "bravo", required_argument
, 0, 'b' },
221 { "charlie", no_argument
, 0, 'c' },
223 { "program", "-a", "-W", "charlie", "-W", "bravo", 0 }, 6, true },
225 { "unwanted argument (W longopt)",
226 "+ab:cW;", { { "alpha", no_argument
, 0, 'a' },
227 { "bravo", required_argument
, 0, 'b' },
228 { "charlie", no_argument
, 0, 'c' },
230 { "program", "-a", "-W", "charlie=dingo", "-W", "bravo=x", 0 }, 6, true },
232 { "ambiguous options (W)",
233 "+uvwW;", { { "veni", no_argument
, 0, 'u' },
234 { "vedi", no_argument
, 0, 'v' },
235 { "veci", no_argument
, 0, 'w' } },
236 { "program", "-W", "ve", 0 }, 3, true },
244 int stderr_trap
= create_temp_file ("stderr", 0);
247 perror ("create_temp_file");
250 FILE *stderr_trapped
= fdopen(stderr_trap
, "r+");
256 int old_stderr
= dup (fileno (stderr
));
262 if (dup2 (stderr_trap
, 2) < 0)
271 for (const struct test_short
*tcase
= tests_short
; tcase
->label
; tcase
++)
272 success
= do_test_short (tcase
, stderr_trapped
) && success
;
274 for (const struct test_long
*tcase
= tests_long
; tcase
->label
; tcase
++)
275 success
= do_test_long (tcase
, stderr_trapped
) && success
;
277 dup2 (old_stderr
, 2);
279 fclose (stderr_trapped
);
281 return success
? 0 : 1;
284 #include <support/test-driver.c>