Use MediaWikiCoversValidator for tests that don't use MediaWikiTestCase
[lhc/web/wiklou.git] / tests / phpunit / includes / shell / FirejailCommandTest.php
1 <?php
2
3 /**
4 * Copyright (C) 2017 Kunal Mehta <legoktm@member.fsf.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22 use MediaWiki\Shell\FirejailCommand;
23 use MediaWiki\Shell\Shell;
24 use Wikimedia\TestingAccessWrapper;
25
26 class FirejailCommandTest extends PHPUnit_Framework_TestCase {
27
28 use MediaWikiCoversValidator;
29
30 public function provideBuildFinalCommand() {
31 global $IP;
32 // @codingStandardsIgnoreStart
33 $env = "'MW_INCLUDE_STDERR=;MW_CPU_LIMIT=180; MW_CGROUP='\'''\''; MW_MEM_LIMIT=307200; MW_FILE_SIZE_LIMIT=102400; MW_WALL_CLOCK_LIMIT=180; MW_USE_LOG_PIPE=yes'";
34 // @codingStandardsIgnoreEnd
35 $limit = "/bin/bash '$IP/includes/shell/limit.sh'";
36 $profile = "--profile=$IP/includes/shell/firejail.profile";
37 $blacklist = '--blacklist=' . realpath( MW_CONFIG_FILE );
38 $default = "$blacklist --noroot --seccomp=@default --private-dev";
39 return [
40 [
41 'No restrictions',
42 'ls', 0, "$limit ''\''ls'\''' $env"
43 ],
44 [
45 'default restriction',
46 'ls', Shell::RESTRICT_DEFAULT,
47 "$limit 'firejail --quiet $profile $default -- '\''ls'\''' $env"
48 ],
49 [
50 'no network',
51 'ls', Shell::NO_NETWORK,
52 "$limit 'firejail --quiet $profile --net=none -- '\''ls'\''' $env"
53 ],
54 [
55 'default restriction & no network',
56 'ls', Shell::RESTRICT_DEFAULT | Shell::NO_NETWORK,
57 "$limit 'firejail --quiet $profile $default --net=none -- '\''ls'\''' $env"
58 ],
59 [
60 'seccomp',
61 'ls', Shell::SECCOMP,
62 "$limit 'firejail --quiet $profile --seccomp=@default -- '\''ls'\''' $env"
63 ],
64 [
65 'seccomp & no execve',
66 'ls', Shell::SECCOMP | Shell::NO_EXECVE,
67 "$limit 'firejail --quiet $profile --shell=none --seccomp=@default,execve -- '\''ls'\''' $env"
68 ],
69 ];
70 }
71
72 /**
73 * @covers \MediaWiki\Shell\FirejailCommand::buildFinalCommand()
74 * @dataProvider provideBuildFinalCommand
75 */
76 public function testBuildFinalCommand( $desc, $params, $flags, $expected ) {
77 $command = new FirejailCommand( 'firejail' );
78 $command
79 ->params( $params )
80 ->restrict( $flags );
81 $wrapper = TestingAccessWrapper::newFromObject( $command );
82 $output = $wrapper->buildFinalCommand( $wrapper->command );
83 $this->assertEquals( $expected, $output[0], $desc );
84 }
85
86 }