usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-spawn-pipe-script.c
blob07b06c1b29f45a6d6ba5d6d2403a1262c3e605a6
1 /* Test of create_pipe_in/wait_subprocess.
2 Copyright (C) 2020-2024 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3, or (at your option)
7 any later version.
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
17 #include <config.h>
19 #include "spawn-pipe.h"
20 #include "wait-process.h"
22 #include <errno.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
27 #include "macros.h"
29 int
30 main ()
32 /* Check an invocation of an executable script.
33 This should only be supported if the script has a '#!' marker; otherwise
34 it is unsecure: <https://sourceware.org/bugzilla/show_bug.cgi?id=13134>.
35 POSIX says that the execlp() and execvp() functions support executing
36 shell scripts
37 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html>,
38 but this is considered an antiquated feature. */
39 int fd[1];
40 pid_t pid;
43 size_t i;
45 for (i = 0; i < 2; i++)
47 const char *progname =
48 (i == 0 ? "executable-script" : "executable-script.sh");
49 const char *prog_path =
50 (i == 0 ? SRCDIR "executable-script" : SRCDIR "executable-script.sh");
51 const char *prog_argv[2] = { prog_path, NULL };
53 pid = create_pipe_in (progname, prog_argv[0], prog_argv, NULL,
54 NULL, false, true, false, fd);
55 if (pid >= 0)
57 /* Wait for child. */
58 ASSERT (wait_subprocess (pid, progname, true, true, true, false,
59 NULL)
60 == 127);
62 else
64 ASSERT (pid == -1);
65 ASSERT (errno == ENOEXEC);
70 #if defined _WIN32 && !defined __CYGWIN__
71 /* On native Windows, scripts - even with '#!' marker - are not executable.
72 Only .bat and .cmd files are. */
73 if (test_exit_status != EXIT_SUCCESS)
74 return test_exit_status;
75 fprintf (stderr, "Skipping test: scripts are not executable on this platform.\n");
76 return 77;
77 #else
79 const char *progname = "executable-shell-script";
80 const char *prog_path = SRCDIR "executable-shell-script";
81 const char *prog_argv[2] = { prog_path, NULL };
83 pid = create_pipe_in (progname, prog_argv[0], prog_argv, NULL,
84 NULL, false, true, false, fd);
85 ASSERT (pid >= 0);
86 ASSERT (fd[0] > STDERR_FILENO);
88 /* Get child's output. */
89 char buffer[1024];
90 FILE *fp = fdopen (fd[0], "r");
91 ASSERT (fp != NULL);
92 ASSERT (fread (buffer, 1, sizeof (buffer), fp) == 11);
94 /* Check the result. */
95 ASSERT (memcmp (buffer, "Halle Potta", 11) == 0);
97 /* Wait for child. */
98 ASSERT (wait_subprocess (pid, progname, true, false, true, true, NULL) == 0);
100 ASSERT (fclose (fp) == 0);
103 return test_exit_status;
104 #endif