filetransfer.test 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. class FileTranferTest extends DrupalWebTestCase {
  3. protected $hostname = 'localhost';
  4. protected $username = 'drupal';
  5. protected $password = 'password';
  6. protected $port = '42';
  7. public static function getInfo() {
  8. return array(
  9. 'name' => 'FileTransfer unit tests',
  10. 'description' => 'Test that the jail is respected and that protocols using recursive file move operations work.',
  11. 'group' => 'System'
  12. );
  13. }
  14. function setUp() {
  15. parent::setUp();
  16. $this->testConnection = TestFileTransfer::factory(DRUPAL_ROOT, array('hostname' => $this->hostname, 'username' => $this->username, 'password' => $this->password, 'port' => $this->port));
  17. }
  18. function _getFakeModuleFiles() {
  19. $files = array(
  20. 'fake.module',
  21. 'fake.info',
  22. 'theme' => array(
  23. 'fake.tpl.php'
  24. ),
  25. 'inc' => array(
  26. 'fake.inc'
  27. )
  28. );
  29. return $files;
  30. }
  31. function _buildFakeModule() {
  32. $location = 'temporary://fake';
  33. if (is_dir($location)) {
  34. $ret = 0;
  35. $output = array();
  36. exec('rm -Rf ' . escapeshellarg($location), $output, $ret);
  37. if ($ret != 0) {
  38. throw new Exception('Error removing fake module directory.');
  39. }
  40. }
  41. $files = $this->_getFakeModuleFiles();
  42. $this->_writeDirectory($location, $files);
  43. return $location;
  44. }
  45. function _writeDirectory($base, $files = array()) {
  46. mkdir($base);
  47. foreach ($files as $key => $file) {
  48. if (is_array($file)) {
  49. $this->_writeDirectory($base . DIRECTORY_SEPARATOR . $key, $file);
  50. }
  51. else {
  52. //just write the filename into the file
  53. file_put_contents($base . DIRECTORY_SEPARATOR . $file, $file);
  54. }
  55. }
  56. }
  57. function testJail() {
  58. $source = $this->_buildFakeModule();
  59. // This convoluted piece of code is here because our testing framework does
  60. // not support expecting exceptions.
  61. $gotit = FALSE;
  62. try {
  63. $this->testConnection->copyDirectory($source, '/tmp');
  64. }
  65. catch (FileTransferException $e) {
  66. $gotit = TRUE;
  67. }
  68. $this->assertTrue($gotit, 'Was not able to copy a directory outside of the jailed area.');
  69. $gotit = TRUE;
  70. try {
  71. $this->testConnection->copyDirectory($source, DRUPAL_ROOT . '/'. variable_get('file_public_path', conf_path() . '/files'));
  72. }
  73. catch (FileTransferException $e) {
  74. $gotit = FALSE;
  75. }
  76. $this->assertTrue($gotit, 'Was able to copy a directory inside of the jailed area');
  77. }
  78. }
  79. /**
  80. * Mock FileTransfer object for test case.
  81. */
  82. class TestFileTransfer extends FileTransfer {
  83. protected $host = NULL;
  84. protected $username = NULL;
  85. protected $password = NULL;
  86. protected $port = NULL;
  87. /**
  88. * This is for testing the CopyRecursive logic.
  89. */
  90. public $shouldIsDirectoryReturnTrue = FALSE;
  91. function __construct($jail, $username, $password, $hostname = 'localhost', $port = 9999) {
  92. parent::__construct($jail, $username, $password, $hostname, $port);
  93. }
  94. static function factory($jail, $settings) {
  95. return new TestFileTransfer($jail, $settings['username'], $settings['password'], $settings['hostname'], $settings['port']);
  96. }
  97. function connect() {
  98. $parts = explode(':', $this->hostname);
  99. $port = (count($parts) == 2) ? $parts[1] : $this->port;
  100. $this->connection = new MockTestConnection();
  101. $this->connection->connectionString = 'test://' . urlencode($this->username) . ':' . urlencode($this->password) . "@$this->host:$this->port/";
  102. }
  103. function copyFileJailed($source, $destination) {
  104. $this->connection->run("copyFile $source $destination");
  105. }
  106. protected function removeDirectoryJailed($directory) {
  107. $this->connection->run("rmdir $directory");
  108. }
  109. function createDirectoryJailed($directory) {
  110. $this->connection->run("mkdir $directory");
  111. }
  112. function removeFileJailed($destination) {
  113. if (!ftp_delete($this->connection, $item)) {
  114. throw new FileTransferException('Unable to remove to file @file.', NULL, array('@file' => $item));
  115. }
  116. }
  117. function isDirectory($path) {
  118. return $this->shouldIsDirectoryReturnTrue;
  119. }
  120. function isFile($path) {
  121. return FALSE;
  122. }
  123. function chmodJailed($path, $mode, $recursive) {
  124. return;
  125. }
  126. }
  127. /**
  128. * Mock connection object for test case.
  129. */
  130. class MockTestConnection {
  131. var $commandsRun = array();
  132. var $connectionString;
  133. function run($cmd) {
  134. $this->commandsRun[] = $cmd;
  135. }
  136. function flushCommands() {
  137. $out = $this->commandsRun;
  138. $this->commandsRun = array();
  139. return $out;
  140. }
  141. }