1--TEST--
2Using proc_open() with a command array (no shell)
3--FILE--
4<?php
5
6$php = getenv('TEST_PHP_EXECUTABLE');
7$ds = [
8    0 => ['pipe', 'r'],
9    1 => ['pipe', 'w'],
10    2 => ['pipe', 'w'],
11];
12
13echo "Empty command array:\n";
14try {
15    proc_open([], $ds, $pipes);
16} catch (ValueError $exception) {
17    echo $exception->getMessage() . "\n";
18}
19
20echo "\nNul byte in program name:\n";
21try {
22    proc_open(["php\0oops"], $ds, $pipes);
23} catch (ValueError $exception) {
24    echo $exception->getMessage() . "\n";
25}
26
27echo "\nNul byte in argument:\n";
28try {
29    proc_open(["php", "array\0oops"], $ds, $pipes);
30} catch (ValueError $exception) {
31    echo $exception->getMessage() . "\n";
32}
33
34echo "\nEmpty program name:\n";
35try {
36     proc_open([""], $ds, $pipes);
37} catch (ValueError $exception) {
38    echo $exception->getMessage() . "\n";
39}
40
41echo "\nBasic usage:\n";
42$proc = proc_open([$php, '-r', 'echo "Hello World!\n";'], $ds, $pipes);
43fpassthru($pipes[1]);
44proc_close($proc);
45
46putenv('ENV_1=ENV_1');
47$env = ['ENV_2' => 'ENV_2'];
48$cmd = [$php, '-n', '-r', 'var_dump(getenv("ENV_1"), getenv("ENV_2"));'];
49
50echo "\nEnvironment inheritance:\n";
51$proc = proc_open($cmd, $ds, $pipes);
52fpassthru($pipes[1]);
53proc_close($proc);
54
55echo "\nExplicit environment:\n";
56$proc = proc_open($cmd, $ds, $pipes, null, $env);
57fpassthru($pipes[1]);
58proc_close($proc);
59
60echo "\nCheck that arguments are correctly passed through:\n";
61$args = [
62    "Simple",
63    "White space\ttab\nnewline",
64    '"Quoted"',
65    'Qu"ot"ed',
66    '\\Back\\slash\\',
67    '\\\\Back\\\\slash\\\\',
68    '\\"Qu\\"ot\\"ed\\"',
69];
70$cmd = [$php, '-r', 'var_export(array_slice($argv, 1));', '--', ...$args];
71$proc = proc_open($cmd, $ds, $pipes);
72fpassthru($pipes[1]);
73proc_close($proc);
74
75?>
76--EXPECT--
77Empty command array:
78proc_open(): Argument #1 ($command) must have at least one element
79
80Nul byte in program name:
81Command array element 1 contains a null byte
82
83Nul byte in argument:
84Command array element 2 contains a null byte
85
86Empty program name:
87First element must contain a non-empty program name
88
89Basic usage:
90Hello World!
91
92Environment inheritance:
93string(5) "ENV_1"
94bool(false)
95
96Explicit environment:
97bool(false)
98string(5) "ENV_2"
99
100Check that arguments are correctly passed through:
101array (
102  0 => 'Simple',
103  1 => 'White space	tab
104newline',
105  2 => '"Quoted"',
106  3 => 'Qu"ot"ed',
107  4 => '\\Back\\slash\\',
108  5 => '\\\\Back\\\\slash\\\\',
109  6 => '\\"Qu\\"ot\\"ed\\"',
110)
111