diff options
author | Florian Pritz <bluewind@xinu.at> | 2016-07-31 19:28:51 +0200 |
---|---|---|
committer | Florian Pritz <bluewind@xinu.at> | 2016-07-31 19:28:51 +0200 |
commit | cac7ae9fc539cb09db7e22c7578c4d6a06f29398 (patch) | |
tree | dd030910aa7a8230d63cee3fcb85afdb082845b0 | |
parent | 2ac3f246d713bc3db4c1c8e4b0c77bb70720fbda (diff) |
Test \libraries\ProcRunner
Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rw-r--r-- | application/test/tests/test_libraries_procrunner.php | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/application/test/tests/test_libraries_procrunner.php b/application/test/tests/test_libraries_procrunner.php new file mode 100644 index 000000000..4e6adf8dd --- /dev/null +++ b/application/test/tests/test_libraries_procrunner.php @@ -0,0 +1,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(['python', 'thisDoesNotExist']); + $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(), [ + "'python' 'thisDoesNotExist'", + null, + [ + 'return_code' => 2, + 'stdout' => '', + 'stderr' => "python: can't open file 'thisDoesNotExist': [Errno 2] No such file or directory\n", + ], + ], "correct exception data"); + } + } +} + |