summaryrefslogtreecommitdiffstats
path: root/application/test/tests/test_libraries_procrunner.php
blob: b077f991c32358273a2b79d034af03b2f44f88af (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/*
 * Copyright 2015 Florian "Bluewind" Pritz <bluewind@server-speed.net>
 *
 * Licensed under AGPLv3
 * (see COPYING for full license text)
 *
 */

namespace test\tests;

class test_libraries_procrunner extends \test\Test {

	public function __construct()
	{
		parent::__construct();
	}

	public function init()
	{
	}

	public function cleanup()
	{
	}

	public function test_exec_true()
	{
		$p = new \libraries\ProcRunner(['true']);
		$ret = $p->exec();

		$this->t->is($ret['stderr'], '', 'stderr should be empty');
		$this->t->is($ret['stdout'], '', 'stdout should be empty');
		$this->t->is($ret['return_code'], 0, 'return code should be 0');
	}

	public function test_exec_false()
	{
		$p = new \libraries\ProcRunner(['false']);
		$ret = $p->exec();

		$this->t->is($ret['stderr'], '', 'stderr should be empty');
		$this->t->is($ret['stdout'], '', 'stdout should be empty');
		$this->t->is($ret['return_code'], 1, 'return code should be 1');
	}

	public function test_exec_nonexistent()
	{
		$p = new \libraries\ProcRunner(['thisCommandDoesNotExist']);
		$ret = $p->exec();

		$this->t->is($ret['stderr'], "sh: thisCommandDoesNotExist: command not found\n", 'stderr should be empty');
		$this->t->is($ret['stdout'], '', 'stdout should be empty');
		$this->t->is($ret['return_code'], 127, 'return code should be 127');
	}

	public function test_exec_tac()
	{

		$line1 = "this is the first line";
		$line2 = "and this is the second one";
		$input = "$line1\n$line2\n";
		$output = "$line2\n$line1\n";

		$p = new \libraries\ProcRunner(['tac']);
		$p->input($input);
		$ret = $p->exec();

		$this->t->is($ret['stderr'], '', 'stderr should be empty');
		$this->t->is($ret['stdout'], $output, 'stdout should be reversed');
		$this->t->is($ret['return_code'], 0, 'return code should be 0');
	}

	public function test_forbid_nonzero()
	{
		$p = new \libraries\ProcRunner(['false']);
		$p->forbid_nonzero();

		try {
			$p->exec();
			$this->t->ok(false, "this should have caused an an exception");
		} catch (\exceptions\ApiException $e) {
			$this->t->is($e->get_error_id(), 'procrunner/non-zero-exit', "correct exception triggered");
			$this->t->is_deeply($e->get_data(), [
				"'false'",
				null,
				[
					'return_code' => 1,
					'stdout' => '',
					'stderr' => '',
				],
			], "correct exception data");
		}
	}

	public function test_forbid_stderr()
	{
		$p = new \libraries\ProcRunner(['bash', '-c', 'echo "This is a test error message" >&2; exit 2;']);
		$p->forbid_stderr();

		try {
			$p->exec();
			$this->t->ok(false, "this should have caused an an exception");
		} catch (\exceptions\ApiException $e) {
			$this->t->is($e->get_error_id(), 'procrunner/stderr', "correct exception triggered");
			$this->t->is_deeply($e->get_data(), [
				"'bash' '-c' 'echo \"This is a test error message\" >&2; exit 2;'",
				null,
				[
					'return_code' => 2,
					'stdout' => '',
					'stderr' => "This is a test error message\n",
				],
			], "correct exception data");
		}
	}
}