1 /* Test of create_pipe_in/wait_subprocess.
2 Copyright (C) 2020-2021 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)
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/>. */
19 #include "spawn-pipe.h"
20 #include "wait-process.h"
33 /* Check an invocation of an executable script.
34 This should only be supported if the script has a '#!' marker; otherwise
35 it is unsecure: <https://sourceware.org/bugzilla/show_bug.cgi?id=13134>.
36 POSIX says that the execlp() and execvp() functions support executing
38 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html>,
39 but this is considered an antiquated feature. */
46 for (i = 0; i < 2; i++)
48 const char *progname =
49 (i == 0 ? "executable-script" : "executable-script.sh");
50 const char *prog_path =
51 (i == 0 ? SRCDIR "executable-script" : SRCDIR "executable-script.sh");
52 const char *prog_argv[2] = { prog_path, NULL };
54 pid = create_pipe_in (progname, prog_argv[0], prog_argv, NULL,
55 NULL, false, true, false, fd);
59 ASSERT (wait_subprocess (pid, progname, true, true, true, false,
66 ASSERT (errno == ENOEXEC);
71 #if defined _WIN32 && !defined __CYGWIN__
72 /* On native Windows, scripts - even with '#!' marker - are not executable.
73 Only .bat and .cmd files are. */
74 fprintf (stderr, "Skipping test: scripts are not executable on this platform.\n");
78 const char *progname = "executable-shell-script";
79 const char *prog_path = SRCDIR "executable-shell-script";
80 const char *prog_argv[2] = { prog_path, NULL };
82 pid = create_pipe_in (progname, prog_argv[0], prog_argv, NULL,
83 NULL, false, true, false, fd);
85 ASSERT (fd[0] > STDERR_FILENO);
87 /* Get child's output. */
89 FILE *fp = fdopen (fd[0], "r");
91 ASSERT (fread (buffer, 1, sizeof (buffer), fp) == 11);
93 /* Check the result. */
94 ASSERT (memcmp (buffer, "Halle Potta", 11) == 0);
97 ASSERT (wait_subprocess (pid, progname, true, false, true, true, NULL) == 0);
99 ASSERT (fclose (fp) == 0);