FirejailCommandTest.php 2.8 KB

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