webdav.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Florin Peter
  6. * @copyright 2013 Florin Peter <owncloud@florin-peter.de>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. require_once __DIR__ . '/../../../lib/base.php';
  23. require_once __DIR__ . '/../lib/crypt.php';
  24. require_once __DIR__ . '/../lib/keymanager.php';
  25. require_once __DIR__ . '/../lib/proxy.php';
  26. require_once __DIR__ . '/../lib/stream.php';
  27. require_once __DIR__ . '/../lib/util.php';
  28. require_once __DIR__ . '/../appinfo/app.php';
  29. require_once __DIR__ . '/util.php';
  30. use OCA\Encryption;
  31. /**
  32. * Class Test_Encryption_Webdav
  33. * @brief this class provide basic webdav tests for PUT,GET and DELETE
  34. */
  35. class Test_Encryption_Webdav extends \PHPUnit_Framework_TestCase {
  36. const TEST_ENCRYPTION_WEBDAV_USER1 = "test-webdav-user1";
  37. public $userId;
  38. public $pass;
  39. /**
  40. * @var \OC_FilesystemView
  41. */
  42. public $view;
  43. public $dataShort;
  44. public $stateFilesTrashbin;
  45. public static function setUpBeforeClass() {
  46. // reset backend
  47. \OC_User::clearBackends();
  48. \OC_User::useBackend('database');
  49. // Filesystem related hooks
  50. \OCA\Encryption\Helper::registerFilesystemHooks();
  51. // Filesystem related hooks
  52. \OCA\Encryption\Helper::registerUserHooks();
  53. // clear and register hooks
  54. \OC_FileProxy::clearProxies();
  55. \OC_FileProxy::register(new OCA\Encryption\Proxy());
  56. // create test user
  57. \Test_Encryption_Util::loginHelper(\Test_Encryption_Webdav::TEST_ENCRYPTION_WEBDAV_USER1, true);
  58. }
  59. function setUp() {
  60. // reset backend
  61. \OC_User::useBackend('database');
  62. // set user id
  63. \OC_User::setUserId(\Test_Encryption_Webdav::TEST_ENCRYPTION_WEBDAV_USER1);
  64. $this->userId = \Test_Encryption_Webdav::TEST_ENCRYPTION_WEBDAV_USER1;
  65. $this->pass = \Test_Encryption_Webdav::TEST_ENCRYPTION_WEBDAV_USER1;
  66. // init filesystem view
  67. $this->view = new \OC_FilesystemView('/');
  68. // init short data
  69. $this->dataShort = 'hats';
  70. // remember files_trashbin state
  71. $this->stateFilesTrashbin = OC_App::isEnabled('files_trashbin');
  72. // we don't want to tests with app files_trashbin enabled
  73. \OC_App::disable('files_trashbin');
  74. // create test user
  75. \Test_Encryption_Util::loginHelper(\Test_Encryption_Webdav::TEST_ENCRYPTION_WEBDAV_USER1);
  76. }
  77. function tearDown() {
  78. // reset app files_trashbin
  79. if ($this->stateFilesTrashbin) {
  80. OC_App::enable('files_trashbin');
  81. }
  82. else {
  83. OC_App::disable('files_trashbin');
  84. }
  85. }
  86. public static function tearDownAfterClass() {
  87. // cleanup test user
  88. \OC_User::deleteUser(\Test_Encryption_Webdav::TEST_ENCRYPTION_WEBDAV_USER1);
  89. }
  90. /**
  91. * @brief test webdav put random file
  92. */
  93. function testWebdavPUT() {
  94. // generate filename
  95. $filename = '/tmp-' . time() . '.txt';
  96. // set server vars
  97. $_SERVER['REQUEST_METHOD'] = 'OPTIONS';
  98. $_SERVER['REQUEST_METHOD'] = 'PUT';
  99. $_SERVER['REQUEST_URI'] = '/remote.php/webdav' . $filename;
  100. $_SERVER['HTTP_AUTHORIZATION'] = 'Basic dGVzdC13ZWJkYXYtdXNlcjE6dGVzdC13ZWJkYXYtdXNlcjE=';
  101. $_SERVER['CONTENT_TYPE'] = 'application/octet-stream';
  102. $_SERVER['PATH_INFO'] = '/webdav' . $filename;
  103. $_SERVER['CONTENT_LENGTH'] = strlen($this->dataShort);
  104. // handle webdav request
  105. $this->handleWebdavRequest($this->dataShort);
  106. // check if file was created
  107. $this->assertTrue($this->view->file_exists('/' . $this->userId . '/files' . $filename));
  108. // check if key-file was created
  109. $this->assertTrue($this->view->file_exists(
  110. '/' . $this->userId . '/files_encryption/keyfiles/' . $filename . '.key'));
  111. // check if shareKey-file was created
  112. $this->assertTrue($this->view->file_exists(
  113. '/' . $this->userId . '/files_encryption/share-keys/' . $filename . '.' . $this->userId . '.shareKey'));
  114. // disable encryption proxy to prevent recursive calls
  115. $proxyStatus = \OC_FileProxy::$enabled;
  116. \OC_FileProxy::$enabled = false;
  117. // get encrypted file content
  118. $encryptedContent = $this->view->file_get_contents('/' . $this->userId . '/files' . $filename);
  119. // restore proxy state
  120. \OC_FileProxy::$enabled = $proxyStatus;
  121. // check if encrypted content is valid
  122. $this->assertTrue(Encryption\Crypt::isCatfileContent($encryptedContent));
  123. // get decrypted file contents
  124. $decrypt = file_get_contents('crypt:///' . $this->userId . '/files'. $filename);
  125. // check if file content match with the written content
  126. $this->assertEquals($this->dataShort, $decrypt);
  127. // return filename for next test
  128. return $filename;
  129. }
  130. /**
  131. * @brief test webdav get random file
  132. *
  133. * @depends testWebdavPUT
  134. */
  135. function testWebdavGET($filename) {
  136. // set server vars
  137. $_SERVER['REQUEST_METHOD'] = 'GET';
  138. $_SERVER['REQUEST_URI'] = '/remote.php/webdav' . $filename;
  139. $_SERVER['HTTP_AUTHORIZATION'] = 'Basic dGVzdC13ZWJkYXYtdXNlcjE6dGVzdC13ZWJkYXYtdXNlcjE=';
  140. $_SERVER['PATH_INFO'] = '/webdav' . $filename;
  141. // handle webdav request
  142. $content = $this->handleWebdavRequest();
  143. // check if file content match with the written content
  144. $this->assertEquals($this->dataShort, $content);
  145. // return filename for next test
  146. return $filename;
  147. }
  148. /**
  149. * @brief test webdav delete random file
  150. * @depends testWebdavGET
  151. */
  152. function testWebdavDELETE($filename) {
  153. // set server vars
  154. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  155. $_SERVER['REQUEST_URI'] = '/remote.php/webdav' . $filename;
  156. $_SERVER['HTTP_AUTHORIZATION'] = 'Basic dGVzdC13ZWJkYXYtdXNlcjE6dGVzdC13ZWJkYXYtdXNlcjE=';
  157. $_SERVER['PATH_INFO'] = '/webdav' . $filename;
  158. // handle webdav request
  159. $content = $this->handleWebdavRequest();
  160. // check if file was removed
  161. $this->assertFalse($this->view->file_exists('/' . $this->userId . '/files' . $filename));
  162. // check if key-file was removed
  163. $this->assertFalse($this->view->file_exists(
  164. '/' . $this->userId . '/files_encryption/keyfiles' . $filename . '.key'));
  165. // check if shareKey-file was removed
  166. $this->assertFalse($this->view->file_exists(
  167. '/' . $this->userId . '/files_encryption/share-keys' . $filename . '.' . $this->userId . '.shareKey'));
  168. }
  169. /**
  170. * @brief handle webdav request
  171. *
  172. * @param bool $body
  173. *
  174. * @note this init procedure is copied from /apps/files/appinfo/remote.php
  175. */
  176. function handleWebdavRequest($body = false) {
  177. // Backends
  178. $authBackend = new OC_Connector_Sabre_Auth();
  179. $lockBackend = new OC_Connector_Sabre_Locks();
  180. $requestBackend = new OC_Connector_Sabre_Request();
  181. // Create ownCloud Dir
  182. $publicDir = new OC_Connector_Sabre_Directory('');
  183. // Fire up server
  184. $server = new Sabre_DAV_Server($publicDir);
  185. $server->httpRequest = $requestBackend;
  186. $server->setBaseUri('/remote.php/webdav/');
  187. // Load plugins
  188. $server->addPlugin(new Sabre_DAV_Auth_Plugin($authBackend, 'ownCloud'));
  189. $server->addPlugin(new Sabre_DAV_Locks_Plugin($lockBackend));
  190. $server->addPlugin(new Sabre_DAV_Browser_Plugin(false)); // Show something in the Browser, but no upload
  191. $server->addPlugin(new OC_Connector_Sabre_QuotaPlugin());
  192. $server->addPlugin(new OC_Connector_Sabre_MaintenancePlugin());
  193. // And off we go!
  194. if ($body) {
  195. $server->httpRequest->setBody($body);
  196. }
  197. // turn on output buffering
  198. ob_start();
  199. // handle request
  200. $server->exec();
  201. // file content is written in the output buffer
  202. $content = ob_get_contents();
  203. // flush the output buffer and turn off output buffering
  204. ob_end_clean();
  205. // return captured content
  206. return $content;
  207. }
  208. }