nextcloud/apps/files_encryption/tests/trashbin.php

347 lines
12 KiB
PHP
Raw Normal View History

2013-05-23 01:51:35 +04:00
<?php
/**
2015-03-26 13:44:34 +03:00
* @author Björn Schießle <schiessle@owncloud.com>
* @author Christopher Schäpers <kondou@ts.unde.re>
* @author Florin Peter <github@florin-peter.de>
* @author Joas Schilling <nickvergessen@owncloud.com>
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Vincent Petry <pvince81@owncloud.com>
2013-05-23 01:51:35 +04:00
*
2015-03-26 13:44:34 +03:00
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
2013-05-23 01:51:35 +04:00
*
2015-03-26 13:44:34 +03:00
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
2013-05-23 01:51:35 +04:00
*
2015-03-26 13:44:34 +03:00
* This program is distributed in the hope that it will be useful,
2013-05-23 01:51:35 +04:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2015-03-26 13:44:34 +03:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2013-05-23 01:51:35 +04:00
*
2015-03-26 13:44:34 +03:00
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
2013-05-23 01:51:35 +04:00
*
*/
2014-12-03 19:20:04 +03:00
namespace OCA\Files_Encryption\Tests;
2013-05-23 01:51:35 +04:00
/**
2014-12-03 19:20:04 +03:00
* Class Trashbin
* this class provide basic trashbin app tests
2013-05-23 01:51:35 +04:00
*/
2014-12-03 19:20:04 +03:00
class Trashbin extends TestCase {
const TEST_ENCRYPTION_TRASHBIN_USER1 = "test-trashbin-user1";
2013-05-23 01:51:35 +04:00
public $userId;
public $pass;
/**
* @var \OC\Files\View
2013-05-23 01:51:35 +04:00
*/
public $view;
public $dataShort;
public $stateFilesTrashbin;
public $folder1;
public $subfolder;
public $subsubfolder;
public static function setUpBeforeClass() {
parent::setUpBeforeClass();
// trashbin hooks
\OCA\Files_Trashbin\Trashbin::registerHooks();
// create test user
self::loginHelper(self::TEST_ENCRYPTION_TRASHBIN_USER1, true);
}
protected function setUp() {
parent::setUp();
2013-05-23 01:51:35 +04:00
// set user id
\OC_User::setUserId(self::TEST_ENCRYPTION_TRASHBIN_USER1);
$this->userId = self::TEST_ENCRYPTION_TRASHBIN_USER1;
$this->pass = self::TEST_ENCRYPTION_TRASHBIN_USER1;
2013-05-23 01:51:35 +04:00
// init filesystem view
$this->view = new \OC\Files\View('/');
2013-05-23 01:51:35 +04:00
// init short data
$this->dataShort = 'hats';
$this->folder1 = '/folder1';
$this->subfolder = '/subfolder1';
$this->subsubfolder = '/subsubfolder1';
// remember files_trashbin state
2014-12-03 19:20:04 +03:00
$this->stateFilesTrashbin = \OC_App::isEnabled('files_trashbin');
2013-05-23 01:51:35 +04:00
// we want to tests with app files_trashbin enabled
\OC_App::enable('files_trashbin');
2013-05-23 01:51:35 +04:00
}
protected function tearDown() {
2013-05-23 01:51:35 +04:00
// reset app files_trashbin
if ($this->stateFilesTrashbin) {
2014-12-03 19:20:04 +03:00
\OC_App::enable('files_trashbin');
}
else {
2014-12-03 19:20:04 +03:00
\OC_App::disable('files_trashbin');
2013-05-23 01:51:35 +04:00
}
parent::tearDown();
}
public static function tearDownAfterClass() {
// cleanup test user
\OC_User::deleteUser(self::TEST_ENCRYPTION_TRASHBIN_USER1);
\OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
parent::tearDownAfterClass();
2013-05-23 01:51:35 +04:00
}
/**
2013-06-10 16:45:39 +04:00
* @medium
* test delete file
2013-05-23 01:51:35 +04:00
*/
function testDeleteFile() {
// generate filename
$filename = 'tmp-' . $this->getUniqueID() . '.txt';
$filename2 = $filename . '.backup'; // a second file with similar name
2013-05-23 01:51:35 +04:00
// save file with content
$cryptedFile = file_put_contents('crypt:///' .self::TEST_ENCRYPTION_TRASHBIN_USER1. '/files/'. $filename, $this->dataShort);
$cryptedFile2 = file_put_contents('crypt:///' .self::TEST_ENCRYPTION_TRASHBIN_USER1. '/files/'. $filename2, $this->dataShort);
2013-05-23 01:51:35 +04:00
// test that data was successfully written
$this->assertTrue(is_int($cryptedFile));
$this->assertTrue(is_int($cryptedFile2));
2013-05-23 01:51:35 +04:00
// check if key for admin exists
$this->assertTrue($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/' . $filename . '/fileKey'));
$this->assertTrue($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/' . $filename2 . '/fileKey'));
2013-05-23 01:51:35 +04:00
// check if share key for admin exists
$this->assertTrue($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/'
. $filename . '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
$this->assertTrue($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/'
. $filename2 . '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
2013-05-23 01:51:35 +04:00
// delete first file
\OC\Files\Filesystem::unlink($filename);
2013-05-23 01:51:35 +04:00
// check if file not exists
$this->assertFalse($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files/' . $filename));
2013-05-23 01:51:35 +04:00
// check if key for admin not exists
$this->assertFalse($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/' . $filename . '/fileKey'));
2013-05-23 01:51:35 +04:00
// check if share key for admin not exists
$this->assertFalse($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/'
. $filename . '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
2013-05-23 01:51:35 +04:00
// check that second file still exists
$this->assertTrue($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files/' . $filename2));
// check that key for second file still exists
$this->assertTrue($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/' . $filename2 . '/fileKey'));
// check that share key for second file still exists
$this->assertTrue($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/'
. $filename2 . '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
2013-05-23 01:51:35 +04:00
// get files
2015-01-14 23:06:26 +03:00
$trashFiles = \OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_ENCRYPTION_TRASHBIN_USER1);
2013-05-23 01:51:35 +04:00
// find created file with timestamp
2015-01-14 23:06:26 +03:00
$timestamp = null;
foreach ($trashFiles as $file) {
2015-01-14 23:06:26 +03:00
if ($file['name'] === $filename) {
$timestamp = $file['mtime'];
break;
2013-05-23 01:51:35 +04:00
}
}
// check if we found the file we created
2015-01-14 23:06:26 +03:00
$this->assertNotNull($timestamp);
2013-05-23 01:51:35 +04:00
2015-01-14 23:06:26 +03:00
$this->assertTrue($this->view->is_dir('/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keys/' . $filename . '.d' . $timestamp));
2013-05-23 01:51:35 +04:00
// check if key for admin not exists
$this->assertTrue($this->view->file_exists(
2015-01-14 23:06:26 +03:00
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keys/' . $filename . '.d' . $timestamp . '/fileKey'));
2013-05-23 01:51:35 +04:00
// check if share key for admin not exists
$this->assertTrue($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keys/' . $filename
2015-01-14 23:06:26 +03:00
. '.d' . $timestamp . '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
2013-05-23 01:51:35 +04:00
}
/**
2013-06-10 11:31:22 +04:00
* @medium
* test restore file
2013-05-23 01:51:35 +04:00
*/
function testRestoreFile() {
// generate filename
$filename = 'tmp-' . $this->getUniqueID() . '.txt';
$filename2 = $filename . '.backup'; // a second file with similar name
// save file with content
2015-01-14 23:06:26 +03:00
file_put_contents('crypt:///' . self::TEST_ENCRYPTION_TRASHBIN_USER1. '/files/'. $filename, $this->dataShort);
file_put_contents('crypt:///' . self::TEST_ENCRYPTION_TRASHBIN_USER1. '/files/'. $filename2, $this->dataShort);
// delete both files
\OC\Files\Filesystem::unlink($filename);
\OC\Files\Filesystem::unlink($filename2);
2015-01-14 23:06:26 +03:00
$trashFiles = \OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_ENCRYPTION_TRASHBIN_USER1);
// find created file with timestamp
2015-01-14 23:06:26 +03:00
$timestamp = null;
foreach ($trashFiles as $file) {
2015-01-14 23:06:26 +03:00
if ($file['name'] === $filename) {
$timestamp = $file['mtime'];
break;
}
}
2013-05-23 01:51:35 +04:00
2015-01-14 23:06:26 +03:00
// make sure that we have a timestamp
$this->assertNotNull($timestamp);
2013-05-23 01:51:35 +04:00
// before calling the restore operation the keys shouldn't be there
$this->assertFalse($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/' . $filename . '/fileKey'));
$this->assertFalse($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/'
. $filename . '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
// restore first file
2015-01-14 23:06:26 +03:00
$this->assertTrue(\OCA\Files_Trashbin\Trashbin::restore($filename . '.d' . $timestamp, $filename, $timestamp));
2013-05-23 01:51:35 +04:00
// check if file exists
$this->assertTrue($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files/' . $filename));
2013-05-23 01:51:35 +04:00
// check if key for admin exists
$this->assertTrue($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/' . $filename . '/fileKey'));
2013-05-23 01:51:35 +04:00
// check if share key for admin exists
$this->assertTrue($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/'
. $filename . '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
// check that second file was NOT restored
$this->assertFalse($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files/' . $filename2));
// check if key for admin exists
$this->assertFalse($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/' . $filename2 . '/fileKey'));
// check if share key for admin exists
$this->assertFalse($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/'
. $filename2 . '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
2013-05-23 01:51:35 +04:00
}
/**
2013-06-10 11:31:22 +04:00
* @medium
* test delete file forever
2013-05-23 01:51:35 +04:00
*/
function testPermanentDeleteFile() {
// generate filename
$filename = 'tmp-' . $this->getUniqueID() . '.txt';
2013-05-23 01:51:35 +04:00
// save file with content
$cryptedFile = file_put_contents('crypt:///' .$this->userId. '/files/' . $filename, $this->dataShort);
2013-05-23 01:51:35 +04:00
// test that data was successfully written
$this->assertTrue(is_int($cryptedFile));
2013-05-23 01:51:35 +04:00
// check if key for admin exists
$this->assertTrue($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/' . $filename . '/fileKey'));
2013-05-23 01:51:35 +04:00
// check if share key for admin exists
$this->assertTrue($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/'
. $filename . '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
2013-05-23 01:51:35 +04:00
// delete file
\OC\Files\Filesystem::unlink($filename);
2013-05-23 01:51:35 +04:00
// check if file not exists
$this->assertFalse($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files/' . $filename));
2013-05-23 01:51:35 +04:00
// check if key for admin not exists
$this->assertFalse($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/' . $filename . '/'
. $filename . '.key'));
2013-05-23 01:51:35 +04:00
// check if share key for admin not exists
$this->assertFalse($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/'
. $filename . '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
2013-05-23 01:51:35 +04:00
// find created file with timestamp
$query = \OC_DB::prepare('SELECT `timestamp`,`type` FROM `*PREFIX*files_trash`'
. ' WHERE `id`=?');
$result = $query->execute(array($filename))->fetchRow();
2013-05-23 01:51:35 +04:00
$this->assertTrue(is_array($result));
// build suffix
$trashFileSuffix = 'd' . $result['timestamp'];
2013-05-23 01:51:35 +04:00
// check if key for admin exists
$this->assertTrue($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keys/' . $filename
. '.' . $trashFileSuffix . '/fileKey'));
2013-05-23 01:51:35 +04:00
// check if share key for admin exists
$this->assertTrue($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keys/'
. $filename . '.' . $trashFileSuffix . '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
2013-05-23 01:51:35 +04:00
// get timestamp from file
$timestamp = str_replace('d', '', $trashFileSuffix);
2013-05-23 01:51:35 +04:00
// delete file forever
$this->assertGreaterThan(0, \OCA\Files_Trashbin\Trashbin::delete($filename, $this->userId, $timestamp));
2013-05-23 01:51:35 +04:00
// check if key for admin not exists
$this->assertFalse($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/files/' . $filename . '.'
. $trashFileSuffix));
2013-05-23 01:51:35 +04:00
// check if key for admin not exists
$this->assertFalse($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keys/' . $filename
. '.' . $trashFileSuffix . '/fileKey'));
2013-05-23 01:51:35 +04:00
// check if share key for admin not exists
$this->assertFalse($this->view->file_exists(
'/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keys/' . $filename
. '.' . $trashFileSuffix . '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
2013-05-23 01:51:35 +04:00
}
}