2013-05-21 23:09:39 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud
|
|
|
|
*
|
|
|
|
* @author Florin Peter
|
|
|
|
* @copyright 2013 Florin Peter <owncloud@florin-peter.de>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-12-03 18:52:44 +03:00
|
|
|
use OCA\Files_Encryption\Crypt;
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Test_Encryption_Webdav
|
2014-03-05 16:19:08 +04:00
|
|
|
*
|
2014-05-19 19:50:53 +04:00
|
|
|
* this class provide basic webdav tests for PUT,GET and DELETE
|
2013-05-21 23:09:39 +04:00
|
|
|
*/
|
2014-11-06 18:53:35 +03:00
|
|
|
class Test_Encryption_Webdav extends \OCA\Files_Encryption\Tests\TestCase {
|
2013-05-26 22:44:15 +04:00
|
|
|
|
|
|
|
const TEST_ENCRYPTION_WEBDAV_USER1 = "test-webdav-user1";
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
public $userId;
|
|
|
|
public $pass;
|
|
|
|
/**
|
2014-05-12 18:30:39 +04:00
|
|
|
* @var \OC\Files\View
|
2013-05-21 23:09:39 +04:00
|
|
|
*/
|
|
|
|
public $view;
|
|
|
|
public $dataShort;
|
|
|
|
public $stateFilesTrashbin;
|
|
|
|
|
2014-06-03 12:23:51 +04:00
|
|
|
private $storage;
|
2014-03-05 16:19:08 +04:00
|
|
|
|
2013-05-26 22:44:15 +04:00
|
|
|
public static function setUpBeforeClass() {
|
2014-11-06 18:53:35 +03:00
|
|
|
parent::setUpBeforeClass();
|
|
|
|
|
2013-05-26 22:44:15 +04:00
|
|
|
// create test user
|
2014-12-03 19:03:11 +03:00
|
|
|
self::loginHelper(self::TEST_ENCRYPTION_WEBDAV_USER1, true);
|
2014-03-05 16:19:08 +04:00
|
|
|
|
2013-05-26 22:44:15 +04:00
|
|
|
}
|
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2013-05-21 23:09:39 +04:00
|
|
|
// reset backend
|
2013-05-26 22:44:15 +04:00
|
|
|
\OC_User::useBackend('database');
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
// set user id
|
2014-12-03 19:03:11 +03:00
|
|
|
\OC_User::setUserId(self::TEST_ENCRYPTION_WEBDAV_USER1);
|
|
|
|
$this->userId = self::TEST_ENCRYPTION_WEBDAV_USER1;
|
|
|
|
$this->pass = self::TEST_ENCRYPTION_WEBDAV_USER1;
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
// init filesystem view
|
2014-05-12 18:30:39 +04:00
|
|
|
$this->view = new \OC\Files\View('/');
|
2014-06-18 15:26:42 +04:00
|
|
|
list($this->storage, ) = $this->view->resolvePath('/');
|
2013-05-21 23:09:39 +04:00
|
|
|
// init short data
|
|
|
|
$this->dataShort = 'hats';
|
|
|
|
|
|
|
|
// remember files_trashbin state
|
2013-05-26 22:44:15 +04:00
|
|
|
$this->stateFilesTrashbin = OC_App::isEnabled('files_trashbin');
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
// we don't want to tests with app files_trashbin enabled
|
2013-05-26 22:44:15 +04:00
|
|
|
\OC_App::disable('files_trashbin');
|
|
|
|
|
|
|
|
// create test user
|
2014-12-03 19:03:11 +03:00
|
|
|
self::loginHelper(self::TEST_ENCRYPTION_WEBDAV_USER1);
|
2013-05-21 23:09:39 +04:00
|
|
|
}
|
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
protected function tearDown() {
|
2013-05-21 23:09:39 +04:00
|
|
|
// reset app files_trashbin
|
2013-05-26 22:44:15 +04:00
|
|
|
if ($this->stateFilesTrashbin) {
|
|
|
|
OC_App::enable('files_trashbin');
|
2014-03-05 16:19:08 +04:00
|
|
|
} else {
|
2013-05-26 22:44:15 +04:00
|
|
|
OC_App::disable('files_trashbin');
|
2013-05-21 23:09:39 +04:00
|
|
|
}
|
2014-11-06 18:53:35 +03:00
|
|
|
|
|
|
|
parent::tearDown();
|
2013-05-26 22:44:15 +04:00
|
|
|
}
|
2013-05-21 23:09:39 +04:00
|
|
|
|
2013-05-26 22:44:15 +04:00
|
|
|
public static function tearDownAfterClass() {
|
|
|
|
// cleanup test user
|
2014-12-03 19:03:11 +03:00
|
|
|
\OC_User::deleteUser(self::TEST_ENCRYPTION_WEBDAV_USER1);
|
2014-10-10 17:34:19 +04:00
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
parent::tearDownAfterClass();
|
2013-05-21 23:09:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* test webdav put random file
|
2013-05-21 23:09:39 +04:00
|
|
|
*/
|
|
|
|
function testWebdavPUT() {
|
|
|
|
|
|
|
|
// generate filename
|
2014-11-06 18:53:35 +03:00
|
|
|
$filename = '/tmp-' . $this->getUniqueID() . '.txt';
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
// set server vars
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'OPTIONS';
|
|
|
|
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'PUT';
|
|
|
|
$_SERVER['REQUEST_URI'] = '/remote.php/webdav' . $filename;
|
2013-05-27 22:32:59 +04:00
|
|
|
$_SERVER['HTTP_AUTHORIZATION'] = 'Basic dGVzdC13ZWJkYXYtdXNlcjE6dGVzdC13ZWJkYXYtdXNlcjE=';
|
2013-05-21 23:09:39 +04:00
|
|
|
$_SERVER['CONTENT_TYPE'] = 'application/octet-stream';
|
|
|
|
$_SERVER['PATH_INFO'] = '/webdav' . $filename;
|
2013-05-26 22:44:15 +04:00
|
|
|
$_SERVER['CONTENT_LENGTH'] = strlen($this->dataShort);
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
// handle webdav request
|
2013-05-26 22:44:15 +04:00
|
|
|
$this->handleWebdavRequest($this->dataShort);
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
// check if file was created
|
2013-05-26 22:44:15 +04:00
|
|
|
$this->assertTrue($this->view->file_exists('/' . $this->userId . '/files' . $filename));
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
// check if key-file was created
|
2013-05-26 22:44:15 +04:00
|
|
|
$this->assertTrue($this->view->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
'/' . $this->userId . '/files_encryption/keys/' . $filename . '/fileKey'));
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
// check if shareKey-file was created
|
2013-05-26 22:44:15 +04:00
|
|
|
$this->assertTrue($this->view->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
'/' . $this->userId . '/files_encryption/keys/' . $filename . '/' . $this->userId . '.shareKey'));
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
// disable encryption proxy to prevent recursive calls
|
|
|
|
$proxyStatus = \OC_FileProxy::$enabled;
|
|
|
|
\OC_FileProxy::$enabled = false;
|
|
|
|
|
|
|
|
// get encrypted file content
|
2013-05-26 22:44:15 +04:00
|
|
|
$encryptedContent = $this->view->file_get_contents('/' . $this->userId . '/files' . $filename);
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
// restore proxy state
|
|
|
|
\OC_FileProxy::$enabled = $proxyStatus;
|
|
|
|
|
|
|
|
// check if encrypted content is valid
|
2014-12-03 18:52:44 +03:00
|
|
|
$this->assertTrue(Crypt::isCatfileContent($encryptedContent));
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
// get decrypted file contents
|
2014-03-05 16:19:08 +04:00
|
|
|
$decrypt = file_get_contents('crypt:///' . $this->userId . '/files' . $filename);
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
// check if file content match with the written content
|
2013-05-26 22:44:15 +04:00
|
|
|
$this->assertEquals($this->dataShort, $decrypt);
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
// return filename for next test
|
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* test webdav get random file
|
2013-05-21 23:09:39 +04:00
|
|
|
*
|
|
|
|
* @depends testWebdavPUT
|
|
|
|
*/
|
2013-05-26 22:44:15 +04:00
|
|
|
function testWebdavGET($filename) {
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
// set server vars
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
|
$_SERVER['REQUEST_URI'] = '/remote.php/webdav' . $filename;
|
2013-05-27 22:32:59 +04:00
|
|
|
$_SERVER['HTTP_AUTHORIZATION'] = 'Basic dGVzdC13ZWJkYXYtdXNlcjE6dGVzdC13ZWJkYXYtdXNlcjE=';
|
2013-05-21 23:09:39 +04:00
|
|
|
$_SERVER['PATH_INFO'] = '/webdav' . $filename;
|
|
|
|
|
|
|
|
// handle webdav request
|
|
|
|
$content = $this->handleWebdavRequest();
|
|
|
|
|
|
|
|
// check if file content match with the written content
|
2013-05-26 22:44:15 +04:00
|
|
|
$this->assertEquals($this->dataShort, $content);
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
// return filename for next test
|
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* test webdav delete random file
|
2013-05-21 23:09:39 +04:00
|
|
|
* @depends testWebdavGET
|
|
|
|
*/
|
2013-05-26 22:44:15 +04:00
|
|
|
function testWebdavDELETE($filename) {
|
2013-05-21 23:09:39 +04:00
|
|
|
// set server vars
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'DELETE';
|
|
|
|
$_SERVER['REQUEST_URI'] = '/remote.php/webdav' . $filename;
|
2013-05-27 22:32:59 +04:00
|
|
|
$_SERVER['HTTP_AUTHORIZATION'] = 'Basic dGVzdC13ZWJkYXYtdXNlcjE6dGVzdC13ZWJkYXYtdXNlcjE=';
|
2013-05-21 23:09:39 +04:00
|
|
|
$_SERVER['PATH_INFO'] = '/webdav' . $filename;
|
|
|
|
|
2014-06-03 12:23:51 +04:00
|
|
|
// at the beginning the file should exist
|
|
|
|
$this->assertTrue($this->view->file_exists('/' . $this->userId . '/files' . $filename));
|
|
|
|
|
2013-05-21 23:09:39 +04:00
|
|
|
// handle webdav request
|
|
|
|
$content = $this->handleWebdavRequest();
|
|
|
|
|
|
|
|
// check if file was removed
|
2013-05-26 22:44:15 +04:00
|
|
|
$this->assertFalse($this->view->file_exists('/' . $this->userId . '/files' . $filename));
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
// check if key-file was removed
|
2013-05-26 22:44:15 +04:00
|
|
|
$this->assertFalse($this->view->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
'/' . $this->userId . '/files_encryption/keys/' . $filename . '/fileKey'));
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
// check if shareKey-file was removed
|
2013-05-26 22:44:15 +04:00
|
|
|
$this->assertFalse($this->view->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
'/' . $this->userId . '/files_encryption/keys/' . $filename . '/' . $this->userId . '.shareKey'));
|
2013-05-21 23:09:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* handle webdav request
|
2013-05-21 23:09:39 +04:00
|
|
|
*
|
|
|
|
* @param bool $body
|
|
|
|
*
|
2013-05-29 22:11:53 +04:00
|
|
|
* @note this init procedure is copied from /apps/files/appinfo/remote.php
|
2013-05-21 23:09:39 +04:00
|
|
|
*/
|
2013-05-26 22:44:15 +04:00
|
|
|
function handleWebdavRequest($body = false) {
|
2013-05-21 23:09:39 +04:00
|
|
|
// Backends
|
|
|
|
$authBackend = new OC_Connector_Sabre_Auth();
|
|
|
|
$lockBackend = new OC_Connector_Sabre_Locks();
|
|
|
|
$requestBackend = new OC_Connector_Sabre_Request();
|
|
|
|
|
|
|
|
// Create ownCloud Dir
|
2014-03-05 16:19:08 +04:00
|
|
|
$root = '/' . $this->userId . '/files';
|
|
|
|
$view = new \OC\Files\View($root);
|
|
|
|
$publicDir = new OC_Connector_Sabre_Directory($view, $view->getFileInfo(''));
|
|
|
|
$objectTree = new \OC\Connector\Sabre\ObjectTree();
|
2014-06-17 16:10:11 +04:00
|
|
|
$mountManager = \OC\Files\Filesystem::getMountManager();
|
|
|
|
$objectTree->init($publicDir, $view, $mountManager);
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
// Fire up server
|
2014-01-09 17:25:48 +04:00
|
|
|
$server = new \Sabre\DAV\Server($publicDir);
|
2013-05-21 23:09:39 +04:00
|
|
|
$server->httpRequest = $requestBackend;
|
2013-05-26 22:44:15 +04:00
|
|
|
$server->setBaseUri('/remote.php/webdav/');
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
// Load plugins
|
2014-01-09 17:25:48 +04:00
|
|
|
$server->addPlugin(new \Sabre\DAV\Auth\Plugin($authBackend, 'ownCloud'));
|
|
|
|
$server->addPlugin(new \Sabre\DAV\Locks\Plugin($lockBackend));
|
|
|
|
$server->addPlugin(new \Sabre\DAV\Browser\Plugin(false)); // Show something in the Browser, but no upload
|
2014-03-05 16:19:08 +04:00
|
|
|
$server->addPlugin(new OC_Connector_Sabre_QuotaPlugin($view));
|
2013-05-26 22:44:15 +04:00
|
|
|
$server->addPlugin(new OC_Connector_Sabre_MaintenancePlugin());
|
2014-04-22 17:24:38 +04:00
|
|
|
$server->debugExceptions = true;
|
2013-05-21 23:09:39 +04:00
|
|
|
|
|
|
|
// And off we go!
|
2013-05-26 22:44:15 +04:00
|
|
|
if ($body) {
|
|
|
|
$server->httpRequest->setBody($body);
|
2013-05-21 23:09:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// turn on output buffering
|
|
|
|
ob_start();
|
|
|
|
|
|
|
|
// handle request
|
|
|
|
$server->exec();
|
|
|
|
|
|
|
|
// file content is written in the output buffer
|
|
|
|
$content = ob_get_contents();
|
|
|
|
|
|
|
|
// flush the output buffer and turn off output buffering
|
|
|
|
ob_end_clean();
|
|
|
|
|
|
|
|
// return captured content
|
|
|
|
return $content;
|
|
|
|
}
|
2013-08-18 13:02:08 +04:00
|
|
|
}
|