summaryrefslogtreecommitdiff
path: root/test/exec.t
blob: 9e55726d0ccf7c0a3d157fd9aaaf04db8a82dfee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/www/server/bin/php -qC
<?php

# Tests for getopt in it.class

is(it::exec("echo gna"), "gna\n", "basic exec");
is(it::exec("echo {arg}", ['arg' => 'gnu'], ['arg' => "gna"]), "gna\n", "exec with argument");
is(it::exec("echo {0}", 'gna'), "gna\n", "exec with positional argument");
is(it::shell_command("echo {arg}", ['arg' => 'gna07,-:blah']),  "echo gna07,-:blah", "don't quote arguments with only whitelistes characters");
is(it::shell_command("echo {arg}", ['arg' => '2>&1']),          "echo '2>&1'", "quote arguments with dangerous characters");
is(it::shell_command("echo {arg}", ['arg' => '']),              "echo ''", "quote empty arguments");
is(it::shell_command("echo {arg}", ['arg' => null]),            "echo ", "remove null values");
is(it::shell_command("echo {arg}", ['arg' => false]),           "echo ", "remove false values");
is(it::shell_command("echo {arg}", ['arg' => []]),              "echo ", "remove empty arrays");
is(it::shell_command("echo {arg}", (object)['arg' => "foo"])  , "echo foo", "allow objects");
is(it::shell_command("echo {arg}", ['arg' => 1], ['arg' => 2]), "echo 2", "support varargs");


is(
	it::shell_command("echo {-opts}", ['-opts' => ['--longopt' => true]]),
	"echo --longopt",
	"options argument with long option"
);
is(
	it::shell_command("echo {-opts}", ['-opts' => ['-onedash' => true]]),
	"echo -onedash",
	"... with long option but only one dash"
);
is(
	it::shell_command("echo {-opts}", ['-opts' => ['-s' => true]]),
	"echo -s",
	"... with short option"
);
is(
	it::shell_command("echo {-opts}", ['-opts' => ['--longopt' => 'val']]),
	"echo --longopt val",
	"... with long option with value"
);
is(
	it::shell_command("echo {-opts}", ['-opts' => ['-onedash' => 'val']]),
	"echo -onedash val",
	"... with long option but only one dash and with value"
);
is(
	it::shell_command("echo {-opts}", ['-opts' => ['-s' => 'val']]),
	"echo -s val",
	"... with short option with value"
);
is(
	it::shell_command("echo {-opts}", ['-opts' => ['--longopt' => false]]),
	"echo ",
	"... with disabled long option"
);
is(
	it::shell_command("echo {-opts}", ['-opts' => ['-s' => false]]),
	"echo ",
	"... with disabled short option"
);
is(
	it::shell_command("echo {-opts}", ['-opts' => ['longopt' => true]]),
	"echo --longopt",
	"... long option without dashes"
);
is(
	it::shell_command("echo {-opts}", ['-opts' => ['s' => true]]),
	"echo -s",
	"... short option without dashes"
);

foreach (["", "C", "de_CH", "de_CH.utf8"] as $locale)
{
	setlocale(LC_ALL, $locale);
	$arg = "preüpost";
	if (it::match('utf8', $locale))
		$arg = it::any2utf8($arg);
	is(it::exec("echo {arg}", ['arg' => $arg]), $arg . "\n", "exec with argument and umlaut (locale '$locale')");
}

is(it::_exec_quotevalue(""), "''", "empty arg needs quotes");
is(it::_exec_quotevalue("*"), "'*'", "special chars need quotes");
is(it::_exec_quotevalue("Aabcdef0123456789"), "Aabcdef0123456789", "simple case. tel:debug_getdata needs unquoted vals");

is(it::system('exit 0'),    0, 'return exit code 0');
is(it::system('exit 1'),    1, 'return exit code 1');
is(it::system('exit -1'), 255, 'return exit code unsigned');

@unlink('/tmp/it_system_test');
it::system('touch /tmp/it_system_test');
ok(file_exists('/tmp/it_system_test'), 'shell command gets executed');

@unlink('/tmp/it_system_test');
it::system('touch {path}', ['path' => '/tmp/it_system_test']);
ok(file_exists('/tmp/it_system_test'), 'shell command with argument');

@unlink('/tmp/it_system_test');
it::system('touch {0}', '/tmp/it_system_test');
ok(file_exists('/tmp/it_system_test'), 'shell command with positional argument');
@unlink('/tmp/it_system_test');