diff options
author | Bjoern Schiessle <schiessle@owncloud.com> | 2015-03-30 17:29:07 +0200 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2015-04-07 13:30:28 +0200 |
commit | e7a68d1c21c52a39ddec59579ab7701dfef82b2a (patch) | |
tree | f18553183eee730b754f89bf2b5a2a1ce5facade /apps/files_encryption/tests | |
parent | 0eee3a2618235bcb59ce1bcb98526a7592de4578 (diff) | |
download | nextcloud-server-e7a68d1c21c52a39ddec59579ab7701dfef82b2a.tar.gz nextcloud-server-e7a68d1c21c52a39ddec59579ab7701dfef82b2a.zip |
remove old encryption app
Diffstat (limited to 'apps/files_encryption/tests')
-rw-r--r-- | apps/files_encryption/tests/binary | bin | 9734 -> 0 bytes | |||
-rwxr-xr-x | apps/files_encryption/tests/crypt.php | 678 | ||||
-rw-r--r-- | apps/files_encryption/tests/encryption.key | bin | 24 -> 0 bytes | |||
-rw-r--r-- | apps/files_encryption/tests/encryption_table.xml | 39 | ||||
-rw-r--r-- | apps/files_encryption/tests/helper.php | 339 | ||||
-rw-r--r-- | apps/files_encryption/tests/hooks.php | 447 | ||||
-rw-r--r-- | apps/files_encryption/tests/keymanager.php | 411 | ||||
-rw-r--r-- | apps/files_encryption/tests/legacy-encrypted-text.txt | 1 | ||||
-rw-r--r-- | apps/files_encryption/tests/migration.php | 266 | ||||
-rw-r--r-- | apps/files_encryption/tests/proxy.php | 154 | ||||
-rwxr-xr-x | apps/files_encryption/tests/share.php | 1392 | ||||
-rw-r--r-- | apps/files_encryption/tests/stream.php | 232 | ||||
-rw-r--r-- | apps/files_encryption/tests/testcase.php | 111 | ||||
-rwxr-xr-x | apps/files_encryption/tests/trashbin.php | 346 | ||||
-rwxr-xr-x | apps/files_encryption/tests/util.php | 693 | ||||
-rw-r--r-- | apps/files_encryption/tests/zeros | bin | 10238 -> 0 bytes |
16 files changed, 0 insertions, 5109 deletions
diff --git a/apps/files_encryption/tests/binary b/apps/files_encryption/tests/binary Binary files differdeleted file mode 100644 index 79bc99479da..00000000000 --- a/apps/files_encryption/tests/binary +++ /dev/null diff --git a/apps/files_encryption/tests/crypt.php b/apps/files_encryption/tests/crypt.php deleted file mode 100755 index 4ce8f9a926d..00000000000 --- a/apps/files_encryption/tests/crypt.php +++ /dev/null @@ -1,678 +0,0 @@ -<?php -/** - * @author Andreas Fischer <bantu@owncloud.com> - * @author Björn Schießle <schiessle@owncloud.com> - * @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 Robin Appelman <icewind@owncloud.com> - * @author Sam Tuke <mail@samtuke.com> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * 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. - * - * This program 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ - -namespace OCA\Files_Encryption\Tests; - -/** - * Class Crypt - */ -class Crypt extends TestCase { - - const TEST_ENCRYPTION_CRYPT_USER1 = "test-crypt-user1"; - - public $userId; - public $pass; - public $stateFilesTrashbin; - public $dataLong; - public $dataUrl; - public $dataShort; - /** - * @var \OC\Files\View - */ - public $view; - public $legacyEncryptedData; - public $genPrivateKey; - public $genPublicKey; - - /** @var \OCP\IConfig */ - private $config; - - public static function setUpBeforeClass() { - parent::setUpBeforeClass(); - - // create test user - self::loginHelper(self::TEST_ENCRYPTION_CRYPT_USER1, true); - } - - protected function setUp() { - parent::setUp(); - - // set user id - self::loginHelper(self::TEST_ENCRYPTION_CRYPT_USER1); - $this->userId = self::TEST_ENCRYPTION_CRYPT_USER1; - $this->pass = self::TEST_ENCRYPTION_CRYPT_USER1; - - // set content for encrypting / decrypting in tests - $this->dataLong = file_get_contents(__DIR__ . '/../lib/crypt.php'); - $this->dataShort = 'hats'; - $this->dataUrl = __DIR__ . '/../lib/crypt.php'; - $this->legacyData = __DIR__ . '/legacy-text.txt'; - $this->legacyEncryptedData = __DIR__ . '/legacy-encrypted-text.txt'; - $this->legacyEncryptedDataKey = __DIR__ . '/encryption.key'; - $this->randomKey = \OCA\Files_Encryption\Crypt::generateKey(); - - $keypair = \OCA\Files_Encryption\Crypt::createKeypair(); - $this->genPublicKey = $keypair['publicKey']; - $this->genPrivateKey = $keypair['privateKey']; - - $this->view = new \OC\Files\View('/'); - - // remember files_trashbin state - $this->stateFilesTrashbin = \OC_App::isEnabled('files_trashbin'); - - // we don't want to tests with app files_trashbin enabled - \OC_App::disable('files_trashbin'); - - $this->config = \OC::$server->getConfig(); - } - - protected function tearDown() { - // reset app files_trashbin - if ($this->stateFilesTrashbin) { - \OC_App::enable('files_trashbin'); - } else { - \OC_App::disable('files_trashbin'); - } - - $this->assertTrue(\OC_FileProxy::$enabled); - $this->config->deleteSystemValue('cipher'); - - parent::tearDown(); - } - - public static function tearDownAfterClass() { - // cleanup test user - \OC_User::deleteUser(self::TEST_ENCRYPTION_CRYPT_USER1); - - parent::tearDownAfterClass(); - } - - /** - * @medium - */ - public function testGenerateKey() { - - # TODO: use more accurate (larger) string length for test confirmation - - $key = \OCA\Files_Encryption\Crypt::generateKey(); - - $this->assertTrue(strlen($key) > 16); - - } - - public function testDecryptPrivateKey() { - - // test successful decrypt - $crypted = \OCA\Files_Encryption\Crypt::symmetricEncryptFileContent($this->genPrivateKey, 'hat'); - - $header = \OCA\Files_Encryption\Crypt::generateHeader(); - - $decrypted = \OCA\Files_Encryption\Crypt::decryptPrivateKey($header . $crypted, 'hat'); - - $this->assertEquals($this->genPrivateKey, $decrypted); - - //test private key decrypt with wrong password - $wrongPasswd = \OCA\Files_Encryption\Crypt::decryptPrivateKey($crypted, 'hat2'); - - $this->assertEquals(false, $wrongPasswd); - - } - - - /** - * @medium - */ - public function testSymmetricEncryptFileContent() { - - # TODO: search in keyfile for actual content as IV will ensure this test always passes - - $crypted = \OCA\Files_Encryption\Crypt::symmetricEncryptFileContent($this->dataShort, 'hat'); - - $this->assertNotEquals($this->dataShort, $crypted); - - - $decrypt = \OCA\Files_Encryption\Crypt::symmetricDecryptFileContent($crypted, 'hat'); - - $this->assertEquals($this->dataShort, $decrypt); - - } - - /** - * @medium - */ - public function testSymmetricEncryptFileContentAes128() { - - # TODO: search in keyfile for actual content as IV will ensure this test always passes - - $crypted = \OCA\Files_Encryption\Crypt::symmetricEncryptFileContent($this->dataShort, 'hat', 'AES-128-CFB'); - - $this->assertNotEquals($this->dataShort, $crypted); - - - $decrypt = \OCA\Files_Encryption\Crypt::symmetricDecryptFileContent($crypted, 'hat', 'AES-128-CFB'); - - $this->assertEquals($this->dataShort, $decrypt); - - } - - /** - * @medium - */ - public function testSymmetricStreamEncryptShortFileContent() { - - $filename = 'tmp-' . $this->getUniqueID() . '.test'; - - $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/'. $filename, $this->dataShort); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - // Disable encryption proxy to prevent recursive calls - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = false; - - // Get file contents without using any wrapper to get it's actual contents on disk - $retreivedCryptedFile = $this->view->file_get_contents($this->userId . '/files/' . $filename); - - // Re-enable proxy - our work is done - \OC_FileProxy::$enabled = $proxyStatus; - - // Check that the file was encrypted before being written to disk - $this->assertNotEquals($this->dataShort, $retreivedCryptedFile); - - // Get file contents with the encryption wrapper - $decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename); - - // Check that decrypted data matches - $this->assertEquals($this->dataShort, $decrypted); - - // Teardown - $this->view->unlink($this->userId . '/files/' . $filename); - } - - /** - * @medium - */ - public function testSymmetricStreamEncryptShortFileContentAes128() { - - $filename = 'tmp-' . $this->getUniqueID() . '.test'; - - $this->config->setSystemValue('cipher', 'AES-128-CFB'); - - $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/'. $filename, $this->dataShort); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - $this->config->deleteSystemValue('cipher'); - - // Disable encryption proxy to prevent recursive calls - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = false; - - // Get file contents without using any wrapper to get it's actual contents on disk - $retreivedCryptedFile = $this->view->file_get_contents($this->userId . '/files/' . $filename); - - // Re-enable proxy - our work is done - \OC_FileProxy::$enabled = $proxyStatus; - - // Check that the file was encrypted before being written to disk - $this->assertNotEquals($this->dataShort, $retreivedCryptedFile); - - // Get file contents with the encryption wrapper - $decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename); - - // Check that decrypted data matches - $this->assertEquals($this->dataShort, $decrypted); - - // Teardown - $this->view->unlink($this->userId . '/files/' . $filename); - } - - /** - * @medium - * Test that data that is written by the crypto stream wrapper - * @note Encrypted data is manually prepared and decrypted here to avoid dependency on success of stream_read - * @note If this test fails with truncate content, check that enough array slices are being rejoined to form $e, as the crypt.php file may have gotten longer and broken the manual - * reassembly of its data - */ - public function testSymmetricStreamEncryptLongFileContent() { - - // Generate a a random filename - $filename = 'tmp-' . $this->getUniqueID() . '.test'; - - // Save long data as encrypted file using stream wrapper - $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong . $this->dataLong); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - // Disable encryption proxy to prevent recursive calls - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = false; - - // Get file contents without using any wrapper to get it's actual contents on disk - $retreivedCryptedFile = $this->view->file_get_contents($this->userId . '/files/' . $filename); - - // Re-enable proxy - our work is done - \OC_FileProxy::$enabled = $proxyStatus; - - - // Check that the file was encrypted before being written to disk - $this->assertNotEquals($this->dataLong . $this->dataLong, $retreivedCryptedFile); - - $decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename); - - $this->assertEquals($this->dataLong . $this->dataLong, $decrypted); - - // Teardown - $this->view->unlink($this->userId . '/files/' . $filename); - } - - /** - * @medium - * Test that data that is written by the crypto stream wrapper with AES 128 - * @note Encrypted data is manually prepared and decrypted here to avoid dependency on success of stream_read - * @note If this test fails with truncate content, check that enough array slices are being rejoined to form $e, as the crypt.php file may have gotten longer and broken the manual - * reassembly of its data - */ - public function testSymmetricStreamEncryptLongFileContentAes128() { - - // Generate a a random filename - $filename = 'tmp-' . $this->getUniqueID() . '.test'; - - $this->config->setSystemValue('cipher', 'AES-128-CFB'); - - // Save long data as encrypted file using stream wrapper - $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong . $this->dataLong); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - // Disable encryption proxy to prevent recursive calls - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = false; - - $this->config->deleteSystemValue('cipher'); - - // Get file contents without using any wrapper to get it's actual contents on disk - $retreivedCryptedFile = $this->view->file_get_contents($this->userId . '/files/' . $filename); - - // Re-enable proxy - our work is done - \OC_FileProxy::$enabled = $proxyStatus; - - - // Check that the file was encrypted before being written to disk - $this->assertNotEquals($this->dataLong . $this->dataLong, $retreivedCryptedFile); - - $decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename); - - $this->assertEquals($this->dataLong . $this->dataLong, $decrypted); - - // Teardown - $this->view->unlink($this->userId . '/files/' . $filename); - } - - /** - * @medium - * Test that data that is written by the crypto stream wrapper with AES 128 - * @note Encrypted data is manually prepared and decrypted here to avoid dependency on success of stream_read - * @note If this test fails with truncate content, check that enough array slices are being rejoined to form $e, as the crypt.php file may have gotten longer and broken the manual - * reassembly of its data - */ - public function testStreamDecryptLongFileContentWithoutHeader() { - - // Generate a a random filename - $filename = 'tmp-' . $this->getUniqueID() . '.test'; - - $this->config->setSystemValue('cipher', 'AES-128-CFB'); - - // Save long data as encrypted file using stream wrapper - $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong . $this->dataLong); - - $this->config->deleteSystemValue('cipher'); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - // Disable encryption proxy to prevent recursive calls - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = false; - - // Get file contents without using any wrapper to get it's actual contents on disk - $retreivedCryptedFile = $this->view->file_get_contents($this->userId . '/files/' . $filename); - - // Check that the file was encrypted before being written to disk - $this->assertNotEquals($this->dataLong . $this->dataLong, $retreivedCryptedFile); - - // remove the header to check if we can also decrypt old files without a header, - // this files should fall back to AES-128 - $cryptedWithoutHeader = substr($retreivedCryptedFile, \OCA\Files_Encryption\Crypt::BLOCKSIZE); - $this->view->file_put_contents($this->userId . '/files/' . $filename, $cryptedWithoutHeader); - - // Re-enable proxy - our work is done - \OC_FileProxy::$enabled = $proxyStatus; - - $decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename); - - $this->assertEquals($this->dataLong . $this->dataLong, $decrypted); - - // Teardown - $this->view->unlink($this->userId . '/files/' . $filename); - } - - /** - * @medium - */ - public function testIsEncryptedContent() { - - $this->assertFalse(\OCA\Files_Encryption\Crypt::isCatfileContent($this->dataUrl)); - - $this->assertFalse(\OCA\Files_Encryption\Crypt::isCatfileContent($this->legacyEncryptedData)); - - $keyfileContent = \OCA\Files_Encryption\Crypt::symmetricEncryptFileContent($this->dataUrl, 'hat', 'AES-128-CFB'); - - $this->assertTrue(\OCA\Files_Encryption\Crypt::isCatfileContent($keyfileContent)); - - } - - /** - * @large - */ - public function testMultiKeyEncrypt() { - - # TODO: search in keyfile for actual content as IV will ensure this test always passes - - $pair1 = \OCA\Files_Encryption\Crypt::createKeypair(); - - $this->assertEquals(2, count($pair1)); - - $this->assertTrue(strlen($pair1['publicKey']) > 1); - - $this->assertTrue(strlen($pair1['privateKey']) > 1); - - - $crypted = \OCA\Files_Encryption\Crypt::multiKeyEncrypt($this->dataShort, array($pair1['publicKey'])); - - $this->assertNotEquals($this->dataShort, $crypted['data']); - - - $decrypt = \OCA\Files_Encryption\Crypt::multiKeyDecrypt($crypted['data'], $crypted['keys'][0], $pair1['privateKey']); - - $this->assertEquals($this->dataShort, $decrypt); - - } - - /** - * @medium - */ - public function testRenameFile() { - - $filename = 'tmp-' . $this->getUniqueID(); - - // Save long data as encrypted file using stream wrapper - $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - // Get file decrypted contents - $decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename); - - $this->assertEquals($this->dataLong, $decrypt); - - $newFilename = 'tmp-new-' . $this->getUniqueID(); - $view = new \OC\Files\View('/' . $this->userId . '/files'); - $view->rename($filename, $newFilename); - - // Get file decrypted contents - $newDecrypt = file_get_contents('crypt:///'. $this->userId . '/files/' . $newFilename); - - $this->assertEquals($this->dataLong, $newDecrypt); - - // tear down - $view->unlink($newFilename); - } - - /** - * @medium - */ - public function testMoveFileIntoFolder() { - - $filename = 'tmp-' . $this->getUniqueID(); - - // Save long data as encrypted file using stream wrapper - $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - // Get file decrypted contents - $decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename); - - $this->assertEquals($this->dataLong, $decrypt); - - $newFolder = '/newfolder' . $this->getUniqueID(); - $newFilename = 'tmp-new-' . $this->getUniqueID(); - $view = new \OC\Files\View('/' . $this->userId . '/files'); - $view->mkdir($newFolder); - $view->rename($filename, $newFolder . '/' . $newFilename); - - // Get file decrypted contents - $newDecrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $newFolder . '/' . $newFilename); - - $this->assertEquals($this->dataLong, $newDecrypt); - - // tear down - $view->unlink($newFolder); - } - - /** - * @medium - */ - public function testMoveFolder() { - - $view = new \OC\Files\View('/' . $this->userId . '/files'); - - $filename = '/tmp-' . $this->getUniqueID(); - $folder = '/folder' . $this->getUniqueID(); - - $view->mkdir($folder); - - // Save long data as encrypted file using stream wrapper - $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $folder . $filename, $this->dataLong); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - // Get file decrypted contents - $decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $folder . $filename); - - $this->assertEquals($this->dataLong, $decrypt); - - $newFolder = '/newfolder/subfolder' . $this->getUniqueID(); - $view->mkdir('/newfolder'); - - $view->rename($folder, $newFolder); - - // Get file decrypted contents - $newDecrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $newFolder . $filename); - - $this->assertEquals($this->dataLong, $newDecrypt); - - // tear down - $view->unlink($newFolder); - $view->unlink('/newfolder'); - } - - /** - * @medium - */ - public function testChangePassphrase() { - $filename = 'tmp-' . $this->getUniqueID(); - - // Save long data as encrypted file using stream wrapper - $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - // Get file decrypted contents - $decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename); - - $this->assertEquals($this->dataLong, $decrypt); - - // change password - \OC_User::setPassword($this->userId, 'test', null); - - // relogin - $params['uid'] = $this->userId; - $params['password'] = 'test'; - \OCA\Files_Encryption\Hooks::login($params); - - // Get file decrypted contents - $newDecrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename); - - $this->assertEquals($this->dataLong, $newDecrypt); - - // tear down - // change password back - \OC_User::setPassword($this->userId, $this->pass); - $view = new \OC\Files\View('/' . $this->userId . '/files'); - $view->unlink($filename); - } - - /** - * @medium - */ - public function testViewFilePutAndGetContents() { - - $filename = '/tmp-' . $this->getUniqueID(); - $view = new \OC\Files\View('/' . $this->userId . '/files'); - - // Save short data as encrypted file using stream wrapper - $cryptedFile = $view->file_put_contents($filename, $this->dataShort); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - // Get file decrypted contents - $decrypt = $view->file_get_contents($filename); - - $this->assertEquals($this->dataShort, $decrypt); - - // Save long data as encrypted file using stream wrapper - $cryptedFileLong = $view->file_put_contents($filename, $this->dataLong); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFileLong)); - - // Get file decrypted contents - $decryptLong = $view->file_get_contents($filename); - - $this->assertEquals($this->dataLong, $decryptLong); - - // tear down - $view->unlink($filename); - } - - /** - * @large - */ - public function testTouchExistingFile() { - $filename = '/tmp-' . $this->getUniqueID(); - $view = new \OC\Files\View('/' . $this->userId . '/files'); - - // Save short data as encrypted file using stream wrapper - $cryptedFile = $view->file_put_contents($filename, $this->dataShort); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - $view->touch($filename); - - // Get file decrypted contents - $decrypt = $view->file_get_contents($filename); - - $this->assertEquals($this->dataShort, $decrypt); - - // tear down - $view->unlink($filename); - } - - /** - * @medium - */ - public function testTouchFile() { - $filename = '/tmp-' . $this->getUniqueID(); - $view = new \OC\Files\View('/' . $this->userId . '/files'); - - $view->touch($filename); - - // Save short data as encrypted file using stream wrapper - $cryptedFile = $view->file_put_contents($filename, $this->dataShort); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - // Get file decrypted contents - $decrypt = $view->file_get_contents($filename); - - $this->assertEquals($this->dataShort, $decrypt); - - // tear down - $view->unlink($filename); - } - - /** - * @medium - */ - public function testFopenFile() { - $filename = '/tmp-' . $this->getUniqueID(); - $view = new \OC\Files\View('/' . $this->userId . '/files'); - - // Save short data as encrypted file using stream wrapper - $cryptedFile = $view->file_put_contents($filename, $this->dataShort); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - $handle = $view->fopen($filename, 'r'); - - // Get file decrypted contents - $decrypt = fgets($handle); - - $this->assertEquals($this->dataShort, $decrypt); - - // tear down - fclose($handle); - $view->unlink($filename); - } - -} diff --git a/apps/files_encryption/tests/encryption.key b/apps/files_encryption/tests/encryption.key Binary files differdeleted file mode 100644 index 4ee962145c2..00000000000 --- a/apps/files_encryption/tests/encryption.key +++ /dev/null diff --git a/apps/files_encryption/tests/encryption_table.xml b/apps/files_encryption/tests/encryption_table.xml deleted file mode 100644 index c0f63dc0efa..00000000000 --- a/apps/files_encryption/tests/encryption_table.xml +++ /dev/null @@ -1,39 +0,0 @@ -<?xml version="1.0" encoding="ISO-8859-1" ?> -<database> - <name>*dbname*</name> - <create>true</create> - <overwrite>false</overwrite> - <charset>utf8</charset> - <table> - <name>*dbprefix*encryption_test</name> - <declaration> - <field> - <name>uid</name> - <type>text</type> - <notnull>true</notnull> - <length>64</length> - </field> - <field> - <name>mode</name> - <type>text</type> - <notnull>true</notnull> - <length>64</length> - <comments>What client-side / server-side configuration is used</comments> - </field> - <field> - <name>recovery_enabled</name> - <type>integer</type> - <notnull>true</notnull> - <default>0</default> - <comments>Whether encryption key recovery is enabled</comments> - </field> - <field> - <name>migration_status</name> - <type>integer</type> - <notnull>true</notnull> - <default>0</default> - <comments>Whether encryption migration has been performed</comments> - </field> - </declaration> - </table> -</database> diff --git a/apps/files_encryption/tests/helper.php b/apps/files_encryption/tests/helper.php deleted file mode 100644 index 8fbd4f419a9..00000000000 --- a/apps/files_encryption/tests/helper.php +++ /dev/null @@ -1,339 +0,0 @@ -<?php -/** - * @author Björn Schießle <schiessle@owncloud.com> - * @author Joas Schilling <nickvergessen@owncloud.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Morris Jobke <hey@morrisjobke.de> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * 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. - * - * This program 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ - -namespace OCA\Files_Encryption\Tests; - -use OCA\Files_Encryption; -use OCA\Files_Encryption\Helper; - -/** - * Class Helper - */ -class TestHelper extends TestCase { - - const TEST_ENCRYPTION_HELPER_USER1 = "test-helper-user1"; - const TEST_ENCRYPTION_HELPER_USER2 = "test-helper-user2"; - - protected function setUpUsers() { - // create test user - self::loginHelper(self::TEST_ENCRYPTION_HELPER_USER2, true); - self::loginHelper(self::TEST_ENCRYPTION_HELPER_USER1, true); - } - - protected function cleanUpUsers() { - // cleanup test user - \OC_User::deleteUser(self::TEST_ENCRYPTION_HELPER_USER1); - \OC_User::deleteUser(self::TEST_ENCRYPTION_HELPER_USER2); - } - - public static function setupHooks() { - // Filesystem related hooks - Helper::registerFilesystemHooks(); - - // clear and register hooks - \OC_FileProxy::clearProxies(); - \OC_FileProxy::register(new Files_Encryption\Proxy()); - } - - public static function tearDownAfterClass() { - parent::tearDownAfterClass(); - } - - /** - * @medium - */ - function testStripPartialFileExtension() { - - $partFilename = 'testfile.txt.part'; - $filename = 'testfile.txt'; - - $this->assertTrue(Helper::isPartialFilePath($partFilename)); - - $this->assertEquals('testfile.txt', Helper::stripPartialFileExtension($partFilename)); - - $this->assertFalse(Helper::isPartialFilePath($filename)); - - $this->assertEquals('testfile.txt', Helper::stripPartialFileExtension($filename)); - } - - - /** - * @medium - */ - function testStripPartialFileExtensionWithTransferIdPath() { - - $partFilename = 'testfile.txt.ocTransferId643653835.part'; - $filename = 'testfile.txt'; - - $this->assertTrue(Helper::isPartialFilePath($partFilename)); - - $this->assertEquals('testfile.txt', Helper::stripPartialFileExtension($partFilename)); - - $this->assertFalse(Helper::isPartialFilePath($filename)); - - $this->assertEquals('testfile.txt', Helper::stripPartialFileExtension($filename)); - } - - /** - * @dataProvider dataVersionsPathPositive - */ - function testGetPathFromVersionPositive($path, $expected) { - $result = Helper::getPathFromVersion($path); - $this->assertSame($expected, $result); - } - - function dataVersionsPathPositive() { - return array( - array('/user/files_versions/foo/bar/test.txt.v456756835', 'foo/bar/test.txt'), - array('user/files_versions/foo/bar/test.txt.v456756835', 'foo/bar/test.txt'), - array('user/files_versions//foo/bar/test.txt.v456756835', 'foo/bar/test.txt'), - array('user/files_versions/test.txt.v456756835', 'test.txt'), - ); - } - - /** - * @dataProvider dataVersionsPathNegative - * @expectedException \OCA\Files_Encryption\Exception\EncryptionException - */ - function testGetPathFromVersionNegative($path) { - Helper::getPathFromVersion($path); - } - - function dataVersionsPathNegative() { - return array( - array('/user/files_versions/'), - array('/user/files_versions'), - ); - } - - /** - * @dataProvider dataPathsCachedFilePositive - */ - function testGetPathFromCachedFilePositive($path, $expected) { - $result = Helper::getPathFromCachedFile($path); - $this->assertEquals($expected, $result); - } - - function dataPathsCachedFilePositive() { - return array( - array('/user/cache/transferid636483/foo/bar/test.txt', 'foo/bar/test.txt'), - array('/user/cache/transferid636483//test.txt', 'test.txt'), - array('user/cache/transferid636483//test.txt', 'test.txt'), - ); - } - - - /** - * @dataProvider dataPathsCachedFileNegative - * @expectedException \OCA\Files_Encryption\Exception\EncryptionException - */ - function testGetPathFromCachedFileNegative($path) { - Helper::getPathFromCachedFile($path); - } - - function dataPathsCachedFileNegative() { - return array( - array('/user/cache/transferid636483/'), - array('/user/cache/transferid636483'), - array('/user/cache/transferid636483//'), - array('/user/cache'), - ); - } - - function testGetUser() { - self::setUpUsers(); - - $path1 = "/" . self::TEST_ENCRYPTION_HELPER_USER1 . "/files/foo/bar.txt"; - $path2 = "/" . self::TEST_ENCRYPTION_HELPER_USER1 . "/cache/foo/bar.txt"; - $path3 = "/" . self::TEST_ENCRYPTION_HELPER_USER2 . "/thumbnails/foo"; - $path4 ="/" . "/" . self::TEST_ENCRYPTION_HELPER_USER1; - - self::loginHelper(self::TEST_ENCRYPTION_HELPER_USER1); - - // if we are logged-in every path should return the currently logged-in user - $this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Helper::getUser($path1)); - $this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Helper::getUser($path2)); - $this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Helper::getUser($path3)); - $this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Helper::getUser($path4)); - - // now log out - self::logoutHelper(); - - // now we should only get the user from /user/files and user/cache paths - $this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Helper::getUser($path1)); - $this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Helper::getUser($path2)); - - try { - $this->assertFalse(Helper::getUser($path3)); - $this->assertFalse(true, '"OCA\Files_Encryption\Exception\EncryptionException: Could not determine user expected"'); - } catch (Files_Encryption\Exception\EncryptionException $e) { - $this->assertSame('Could not determine user', $e->getMessage()); - } - try { - $this->assertFalse(Helper::getUser($path4)); - $this->assertFalse(true, '"OCA\Files_Encryption\Exception\EncryptionException: Could not determine user expected"'); - } catch (Files_Encryption\Exception\EncryptionException $e) { - $this->assertSame('Could not determine user', $e->getMessage()); - } - - // Log-in again - self::loginHelper(self::TEST_ENCRYPTION_HELPER_USER1); - self::cleanUpUsers(); - } - - /** - * @dataProvider dataStripUserFilesPath - */ - function testStripUserFilesPath($path, $expected) { - $result = Helper::stripUserFilesPath($path); - $this->assertSame($expected, $result); - } - - function dataStripUserFilesPath() { - return array( - array('/user/files/foo.txt', 'foo.txt'), - array('//user/files/foo.txt', 'foo.txt'), - array('user//files/foo/bar.txt', 'foo/bar.txt'), - array('user//files/', false), - array('/user', false), - array('', false), - ); - } - - /** - * @dataProvider dataStripUserFilesPathPositive - */ - function testGetUserFromPathPositive($path, $expected) { - self::setUpUsers(); - $result = Helper::getUserFromPath($path); - $this->assertSame($expected, $result); - self::cleanUpUsers(); - } - - function dataStripUserFilesPathPositive() { - return array( - array('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '/files/foo.txt', self::TEST_ENCRYPTION_HELPER_USER1), - array('//' . self::TEST_ENCRYPTION_HELPER_USER2 . '/files_versions/foo.txt', self::TEST_ENCRYPTION_HELPER_USER2), - array('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '/files_trashbin/', self::TEST_ENCRYPTION_HELPER_USER1), - array(self::TEST_ENCRYPTION_HELPER_USER1 . '//cache/foo/bar.txt', self::TEST_ENCRYPTION_HELPER_USER1), - ); - } - - /** - * @dataProvider dataStripUserFilesPathNegative - * @expectedException \OCA\Files_Encryption\Exception\EncryptionException - */ - function testGetUserFromPathNegative($path) { - Helper::getUserFromPath($path); - } - - function dataStripUserFilesPathNegative() { - return array( - array('/unknown_user/files/foo.txt'), - array('/' . self::TEST_ENCRYPTION_HELPER_USER2 . '/unknown_folder/foo.txt'), - array('/' . self::TEST_ENCRYPTION_HELPER_USER1), - array(''), - ); - } - - /** - * @dataProvider dataPaths - */ - function testMkdirr($path, $expected) { - self::setUpUsers(); - Helper::mkdirr($path, new \OC\Files\View('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '/files')); - // ignore the filename because we only check for the directories - $dirParts = array_slice($expected, 0, -1); - $expectedPath = implode('/', $dirParts); - $this->assertTrue(\OC\Files\Filesystem::is_dir($expectedPath)); - - // cleanup - \OC\Files\Filesystem::unlink('/' . $expected[0]); - self::cleanUpUsers(); - } - - /** - * @dataProvider dataDetectFileTypePositive - */ - function testDetectFileTypePositive($path, $expected) { - $result = Helper::detectFileType($path); - $this->assertSame($expected, $result); - } - - function dataDetectFileTypePositive() { - return array( - array(self::TEST_ENCRYPTION_HELPER_USER1 . '/files', Files_Encryption\Util::FILE_TYPE_FILE), - array(self::TEST_ENCRYPTION_HELPER_USER1 . '/files/foo/bar', Files_Encryption\Util::FILE_TYPE_FILE), - array('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '/files/foo/bar', Files_Encryption\Util::FILE_TYPE_FILE), - array(self::TEST_ENCRYPTION_HELPER_USER1 . '/files_versions', Files_Encryption\Util::FILE_TYPE_VERSION), - array('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '//files_versions/foo/bar', Files_Encryption\Util::FILE_TYPE_VERSION), - array('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '//cache/foo/bar', Files_Encryption\Util::FILE_TYPE_CACHE), - ); - } - - /** - * @dataProvider dataDetectFileTypeNegative - * @expectedException \OCA\Files_Encryption\Exception\EncryptionException - */ - function testDetectFileTypeNegative($path) { - Helper::detectFileType($path); - } - - function dataDetectFileTypeNegative() { - return array( - array('/files'), - array('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '/unsuported_dir/foo/bar'), - ); - } - - /** - * @dataProvider dataPaths - */ - function testSplitPath($path, $expected) { - $result = Helper::splitPath($path); - $this->compareArray($result, $expected); - } - - function dataPaths() { - return array( - array('foo/bar/test.txt', array('', 'foo', 'bar', 'test.txt')), - array('/foo/bar/test.txt', array('', 'foo', 'bar', 'test.txt')), - array('/foo/bar//test.txt', array('', 'foo', 'bar', 'test.txt')), - array('//foo/bar/test.txt', array('', 'foo', 'bar', 'test.txt')), - array('foo', array('', 'foo')), - array('/foo', array('', 'foo')), - array('//foo', array('', 'foo')), - ); - } - - function compareArray($result, $expected) { - $this->assertSame(count($expected), count($result)); - - foreach ($expected as $key => $value) { - $this->assertArrayHasKey($key, $result); - $this->assertSame($value, $result[$key]); - } - } - -} diff --git a/apps/files_encryption/tests/hooks.php b/apps/files_encryption/tests/hooks.php deleted file mode 100644 index b63e0431187..00000000000 --- a/apps/files_encryption/tests/hooks.php +++ /dev/null @@ -1,447 +0,0 @@ -<?php -/** - * @author Björn Schießle <schiessle@owncloud.com> - * @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> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * 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. - * - * This program 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ - -namespace OCA\Files_Encryption\Tests; - -/** - * Class Hooks - * this class provide basic hook app tests - */ -class Hooks extends TestCase { - - const TEST_ENCRYPTION_HOOKS_USER1 = "test-encryption-hooks-user1.dot"; - const TEST_ENCRYPTION_HOOKS_USER2 = "test-encryption-hooks-user2.dot"; - - /** @var \OC\Files\View */ - public $user1View; // view on /data/user1/files - /** @var \OC\Files\View */ - public $user2View; // view on /data/user2/files - /** @var \OC\Files\View */ - public $rootView; // view on /data/user - public $data; - public $filename; - public $folder; - - private static $testFiles; - - public static function setUpBeforeClass() { - parent::setUpBeforeClass(); - - // note: not using a data provider because these - // files all need to coexist to make sure the - // share keys are found properly (pattern matching) - self::$testFiles = array( - 't est.txt', - 't est_.txt', - 't est.doc.txt', - 't est(.*).txt', // make sure the regexp is escaped - 'multiple.dots.can.happen.too.txt', - 't est.' . self::TEST_ENCRYPTION_HOOKS_USER1 . '.txt', - 't est_.' . self::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey.txt', - 'who would upload their.shareKey', - 'user ones file.txt', - 'user ones file.txt.backup', - '.t est.txt' - ); - - // create test user - self::loginHelper(self::TEST_ENCRYPTION_HOOKS_USER1, true); - self::loginHelper(self::TEST_ENCRYPTION_HOOKS_USER2, true); - } - - protected function setUp() { - parent::setUp(); - - // set user id - self::loginHelper(self::TEST_ENCRYPTION_HOOKS_USER1); - \OC_User::setUserId(self::TEST_ENCRYPTION_HOOKS_USER1); - - // init filesystem view - $this->user1View = new \OC\Files\View('/'. self::TEST_ENCRYPTION_HOOKS_USER1 . '/files'); - $this->user2View = new \OC\Files\View('/'. self::TEST_ENCRYPTION_HOOKS_USER2 . '/files'); - $this->rootView = new \OC\Files\View('/'); - - // init short data - $this->data = 'hats'; - $this->filename = 'enc_hooks_tests-' . $this->getUniqueID() . '.txt'; - $this->folder = 'enc_hooks_tests_folder-' . $this->getUniqueID(); - - } - - public static function tearDownAfterClass() { - // cleanup test user - \OC_User::deleteUser(self::TEST_ENCRYPTION_HOOKS_USER1); - \OC_User::deleteUser(self::TEST_ENCRYPTION_HOOKS_USER2); - - parent::tearDownAfterClass(); - } - - function testDisableHook() { - // encryption is enabled and running so we should have some user specific - // settings in oc_preferences - $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*preferences` WHERE `appid` = ?'); - $result = $query->execute(array('files_encryption')); - $row = $result->fetchRow(); - $this->assertTrue(is_array($row)); - - // disabling the app should delete all user specific settings - \OCA\Files_Encryption\Hooks::preDisable(array('app' => 'files_encryption')); - - // check if user specific settings for the encryption app are really gone - $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*preferences` WHERE `appid` = ?'); - $result = $query->execute(array('files_encryption')); - $row = $result->fetchRow(); - $this->assertFalse($row); - - // relogin user to initialize the encryption again - $user = \OCP\User::getUser(); - self::loginHelper($user); - - } - - function testDeleteHooks() { - - // remember files_trashbin state - $stateFilesTrashbin = \OC_App::isEnabled('files_trashbin'); - - // we want to tests with app files_trashbin disabled - \OC_App::disable('files_trashbin'); - - // make sure that the trash bin is disabled - $this->assertFalse(\OC_APP::isEnabled('files_trashbin')); - - $this->user1View->file_put_contents($this->filename, $this->data); - - // check if all keys are generated - $this->assertTrue($this->rootView->file_exists( - self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey')); - $this->assertTrue($this->rootView->file_exists( - self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' . $this->filename . '/fileKey')); - - - self::logoutHelper(); - self::loginHelper(self::TEST_ENCRYPTION_HOOKS_USER2); - \OC_User::setUserId(self::TEST_ENCRYPTION_HOOKS_USER2); - - - $this->user2View->file_put_contents($this->filename, $this->data); - - // check if all keys are generated - $this->assertTrue($this->rootView->file_exists( - self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER2 . '.shareKey')); - $this->assertTrue($this->rootView->file_exists( - self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keys/' . $this->filename . '/fileKey')); - - - // create a dummy file that we can delete something outside of data/user/files - // in this case no share or file keys should be deleted - $this->rootView->file_put_contents(self::TEST_ENCRYPTION_HOOKS_USER2 . "/" . $this->filename, $this->data); - - // delete dummy file outside of data/user/files - $this->rootView->unlink(self::TEST_ENCRYPTION_HOOKS_USER2 . "/" . $this->filename); - - // all keys should still exist - $this->assertTrue($this->rootView->file_exists( - self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER2 . '.shareKey')); - $this->assertTrue($this->rootView->file_exists( - self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keys/' . $this->filename . '/fileKey')); - - - // delete the file in data/user/files - // now the correspondig share and file keys from user2 should be deleted - $this->user2View->unlink($this->filename); - - // check if keys from user2 are really deleted - $this->assertFalse($this->rootView->file_exists( - self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER2 . '.shareKey')); - $this->assertFalse($this->rootView->file_exists( - self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keys/' . $this->filename . '/fileKey')); - - // but user1 keys should still exist - $this->assertTrue($this->rootView->file_exists( - self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey')); - $this->assertTrue($this->rootView->file_exists( - self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' . $this->filename . '/fileKey')); - - if ($stateFilesTrashbin) { - \OC_App::enable('files_trashbin'); - } - else { - \OC_App::disable('files_trashbin'); - } - } - - function testDeleteHooksForSharedFiles() { - - self::logoutHelper(); - self::loginHelper(self::TEST_ENCRYPTION_HOOKS_USER1); - \OC_User::setUserId(self::TEST_ENCRYPTION_HOOKS_USER1); - - // remember files_trashbin state - $stateFilesTrashbin = \OC_App::isEnabled('files_trashbin'); - - // we want to tests with app files_trashbin disabled - \OC_App::disable('files_trashbin'); - - // make sure that the trash bin is disabled - $this->assertFalse(\OC_APP::isEnabled('files_trashbin')); - - $this->user1View->file_put_contents($this->filename, $this->data); - - // check if all keys are generated - $this->assertTrue($this->rootView->file_exists( - self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey')); - $this->assertTrue($this->rootView->file_exists( - self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' . $this->filename . '/fileKey')); - - // get the file info from previous created file - $fileInfo = $this->user1View->getFileInfo($this->filename); - - // check if we have a valid file info - $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo); - - // share the file with user2 - \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_HOOKS_USER2, \OCP\Constants::PERMISSION_ALL); - - // check if new share key exists - $this->assertTrue($this->rootView->file_exists( - self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER2 . '.shareKey')); - - self::logoutHelper(); - self::loginHelper(self::TEST_ENCRYPTION_HOOKS_USER2); - \OC_User::setUserId(self::TEST_ENCRYPTION_HOOKS_USER2); - - // user2 update the shared file - $this->user2View->file_put_contents($this->filename, $this->data); - - // keys should be stored at user1s dir, not in user2s - $this->assertFalse($this->rootView->file_exists( - self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER2 . '.shareKey')); - $this->assertFalse($this->rootView->file_exists( - self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keys/' . $this->filename . '/fileKey')); - - // delete the Shared file from user1 in data/user2/files/Shared - $result = $this->user2View->unlink($this->filename); - - $this->assertTrue($result); - - // share key for user2 from user1s home should be gone, all other keys should still exists - $this->assertTrue($this->rootView->file_exists( - self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey')); - $this->assertFalse($this->rootView->file_exists( - self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER2 . '.shareKey')); - $this->assertTrue($this->rootView->file_exists( - self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' . $this->filename . '/fileKey')); - - // cleanup - - self::logoutHelper(); - self::loginHelper(self::TEST_ENCRYPTION_HOOKS_USER1); - \OC_User::setUserId(self::TEST_ENCRYPTION_HOOKS_USER1); - - if ($stateFilesTrashbin) { - \OC_App::enable('files_trashbin'); - } - else { - \OC_App::disable('files_trashbin'); - } - } - - function testRenameHook() { - // create all files to make sure all keys can coexist properly - foreach (self::$testFiles as $file) { - // save file with content - $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $file, $this->data); - - // test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - } - - foreach (self::$testFiles as $file) { - $this->doTestRenameHook($file); - } - } - - /** - * test rename operation - */ - function doTestRenameHook($filename) { - // check if keys exists - $this->assertTrue($this->rootView->file_exists( - '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' - . $filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey')); - - $this->assertTrue($this->rootView->file_exists( - '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' - . $filename . '/fileKey')); - - // make subfolder and sub-subfolder - $this->rootView->mkdir('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder); - $this->rootView->mkdir('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder . '/' . $this->folder); - - $this->assertTrue($this->rootView->is_dir('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder . '/' . $this->folder)); - - // move the file to the sub-subfolder - $root = $this->rootView->getRoot(); - $this->rootView->chroot('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/'); - $this->rootView->rename($filename, '/' . $this->folder . '/' . $this->folder . '/' . $filename); - $this->rootView->chroot($root); - - $this->assertFalse($this->rootView->file_exists('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $filename)); - $this->assertTrue($this->rootView->file_exists('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder . '/' . $this->folder . '/' . $filename)); - - // keys should be renamed too - $this->assertFalse($this->rootView->file_exists( - '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' - . $filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey')); - $this->assertFalse($this->rootView->file_exists( - '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' - . $filename . '/fileKey')); - - $this->assertTrue($this->rootView->file_exists( - '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' . $this->folder . '/' . $this->folder . '/' - . $filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey')); - $this->assertTrue($this->rootView->file_exists( - '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' . $this->folder . '/' . $this->folder . '/' - . $filename . '/fileKey')); - - // cleanup - $this->rootView->unlink('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder); - } - - function testCopyHook() { - // create all files to make sure all keys can coexist properly - foreach (self::$testFiles as $file) { - // save file with content - $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $file, $this->data); - - // test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - } - - foreach (self::$testFiles as $file) { - $this->doTestCopyHook($file); - } - } - - /** - * test rename operation - */ - function doTestCopyHook($filename) { - // check if keys exists - $this->assertTrue($this->rootView->file_exists( - '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' - . $filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey')); - - $this->assertTrue($this->rootView->file_exists( - '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' - . $filename . '/fileKey')); - - // make subfolder and sub-subfolder - $this->rootView->mkdir('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder); - $this->rootView->mkdir('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder . '/' . $this->folder); - - $this->assertTrue($this->rootView->is_dir('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder . '/' . $this->folder)); - - // copy the file to the sub-subfolder - \OC\Files\Filesystem::copy($filename, '/' . $this->folder . '/' . $this->folder . '/' . $filename); - - $this->assertTrue($this->rootView->file_exists('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $filename)); - $this->assertTrue($this->rootView->file_exists('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder . '/' . $this->folder . '/' . $filename)); - - // keys should be copied too - $this->assertTrue($this->rootView->file_exists( - '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' - . $filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey')); - $this->assertTrue($this->rootView->file_exists( - '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' - . $filename . '/fileKey')); - - $this->assertTrue($this->rootView->file_exists( - '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' . $this->folder . '/' . $this->folder . '/' - . $filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey')); - $this->assertTrue($this->rootView->file_exists( - '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' . $this->folder . '/' . $this->folder . '/' - . $filename . '/fileKey')); - - // cleanup - $this->rootView->unlink('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder); - $this->rootView->unlink('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $filename); - } - - /** - * @brief replacing encryption keys during password change should be allowed - * until the user logged in for the first time - */ - public function testSetPassphrase() { - - $view = new \OC\Files\View(); - - // set user password for the first time - \OCA\Files_Encryption\Hooks::postCreateUser(array('uid' => 'newUser', 'password' => 'newUserPassword')); - - $this->assertTrue($view->file_exists(\OCA\Files_Encryption\Keymanager::getPublicKeyPath() . '/newUser.publicKey')); - $this->assertTrue($view->file_exists('newUser/files_encryption/newUser.privateKey')); - - // check if we are able to decrypt the private key - $encryptedKey = \OCA\Files_Encryption\Keymanager::getPrivateKey($view, 'newUser'); - $privateKey = \OCA\Files_Encryption\Crypt::decryptPrivateKey($encryptedKey, 'newUserPassword'); - $this->assertTrue(is_string($privateKey)); - - // change the password before the user logged-in for the first time, - // we can replace the encryption keys - \OCA\Files_Encryption\Hooks::setPassphrase(array('uid' => 'newUser', 'password' => 'passwordChanged')); - - $encryptedKey = \OCA\Files_Encryption\Keymanager::getPrivateKey($view, 'newUser'); - $privateKey = \OCA\Files_Encryption\Crypt::decryptPrivateKey($encryptedKey, 'passwordChanged'); - $this->assertTrue(is_string($privateKey)); - - // now create a files folder to simulate a already used account - $view->mkdir('/newUser/files'); - - // change the password after the user logged in, now the password should not change - \OCA\Files_Encryption\Hooks::setPassphrase(array('uid' => 'newUser', 'password' => 'passwordChanged2')); - - $encryptedKey = \OCA\Files_Encryption\Keymanager::getPrivateKey($view, 'newUser'); - $privateKey = \OCA\Files_Encryption\Crypt::decryptPrivateKey($encryptedKey, 'passwordChanged2'); - $this->assertFalse($privateKey); - - $privateKey = \OCA\Files_Encryption\Crypt::decryptPrivateKey($encryptedKey, 'passwordChanged'); - $this->assertTrue(is_string($privateKey)); - - } - -} diff --git a/apps/files_encryption/tests/keymanager.php b/apps/files_encryption/tests/keymanager.php deleted file mode 100644 index d1a3f1e140e..00000000000 --- a/apps/files_encryption/tests/keymanager.php +++ /dev/null @@ -1,411 +0,0 @@ -<?php -/** - * @author Andreas Fischer <bantu@owncloud.com> - * @author Björn Schießle <schiessle@owncloud.com> - * @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 Sam Tuke <mail@samtuke.com> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * 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. - * - * This program 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ - -namespace OCA\Files_Encryption\Tests; - -/** - * Class Keymanager - */ -class Keymanager extends TestCase { - - const TEST_USER = "test-keymanager-user.dot"; - - public $userId; - public $pass; - public static $stateFilesTrashbin; - /** - * @var \OC\Files\View - */ - public $view; - public $randomKey; - public $dataShort; - - public static function setUpBeforeClass() { - parent::setUpBeforeClass(); - - // disable file proxy by default - \OC_FileProxy::$enabled = false; - - // remember files_trashbin state - self::$stateFilesTrashbin = \OC_App::isEnabled('files_trashbin'); - - // we don't want to tests with app files_trashbin enabled - \OC_App::disable('files_trashbin'); - - // create test user - \OC_User::deleteUser(self::TEST_USER); - parent::loginHelper(self::TEST_USER, true); - } - - protected function setUp() { - parent::setUp(); - // set content for encrypting / decrypting in tests - $this->dataLong = file_get_contents(__DIR__ . '/../lib/crypt.php'); - $this->dataShort = 'hats'; - $this->dataUrl = __DIR__ . '/../lib/crypt.php'; - $this->legacyData = __DIR__ . '/legacy-text.txt'; - $this->legacyEncryptedData = __DIR__ . '/legacy-encrypted-text.txt'; - $this->randomKey = \OCA\Files_Encryption\Crypt::generateKey(); - - $keypair = \OCA\Files_Encryption\Crypt::createKeypair(); - $this->genPublicKey = $keypair['publicKey']; - $this->genPrivateKey = $keypair['privateKey']; - - $this->view = new \OC\Files\View('/'); - - self::loginHelper(self::TEST_USER); - $this->userId = self::TEST_USER; - $this->pass = self::TEST_USER; - - $userHome = \OC_User::getHome($this->userId); - $this->dataDir = str_replace('/' . $this->userId, '', $userHome); - } - - function tearDown() { - $this->view->deleteAll('/' . self::TEST_USER . '/files_encryption/keys'); - parent::tearDown(); - } - - public static function tearDownAfterClass() { - \OC_FileProxy::$enabled = true; - - // cleanup test user - \OC_User::deleteUser(self::TEST_USER); - // reset app files_trashbin - if (self::$stateFilesTrashbin) { - \OC_App::enable('files_trashbin'); - } - - parent::tearDownAfterClass(); - } - - function testKeyCacheUpdate() { - $testUser = 'testKeyCacheUpdate'; - \OCA\Files_Encryption\Keymanager::setPublicKey('oldKey', $testUser); - - $this->assertSame('oldKey', - \OCA\Files_Encryption\Keymanager::getPublicKey($this->view, $testUser)); - - // update key - \OCA\Files_Encryption\Keymanager::setPublicKey('newKey', $testUser); - - $this->assertSame('newKey', - \OCA\Files_Encryption\Keymanager::getPublicKey($this->view, $testUser)); - - // cleanup - \OCA\Files_Encryption\Keymanager::deletePublicKey($this->view, $testUser); - - } - - /** - * @medium - */ - function testGetPrivateKey() { - - $key = \OCA\Files_Encryption\Keymanager::getPrivateKey($this->view, $this->userId); - - $privateKey = \OCA\Files_Encryption\Crypt::decryptPrivateKey($key, $this->pass); - - $res = openssl_pkey_get_private($privateKey); - - $this->assertTrue(is_resource($res)); - - $sslInfo = openssl_pkey_get_details($res); - - $this->assertArrayHasKey('key', $sslInfo); - - } - - /** - * @medium - */ - function testGetPublicKey() { - - $publiceKey = \OCA\Files_Encryption\Keymanager::getPublicKey($this->view, $this->userId); - - $res = openssl_pkey_get_public($publiceKey); - - $this->assertTrue(is_resource($res)); - - $sslInfo = openssl_pkey_get_details($res); - - $this->assertArrayHasKey('key', $sslInfo); - } - - /** - * @medium - */ - function testSetFileKey() { - - $key = $this->randomKey; - - $file = 'unittest-' . $this->getUniqueID() . '.txt'; - - $util = new \OCA\Files_Encryption\Util($this->view, $this->userId); - - // Disable encryption proxy to prevent recursive calls - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = false; - - $this->view->file_put_contents($this->userId . '/files/' . $file, $this->dataShort); - - \OCA\Files_Encryption\Keymanager::setFileKey($this->view, $util, $file, $key); - - $this->assertTrue($this->view->file_exists('/' . $this->userId . '/files_encryption/keys/' . $file . '/fileKey')); - - // cleanup - $this->view->unlink('/' . $this->userId . '/files/' . $file); - - // change encryption proxy to previous state - \OC_FileProxy::$enabled = $proxyStatus; - } - - /** - * @medium - */ - function testSetPrivateKey() { - - $key = "dummy key"; - - \OCA\Files_Encryption\Keymanager::setPrivateKey($key, 'dummyUser'); - - $this->assertTrue($this->view->file_exists('/dummyUser/files_encryption/dummyUser.privateKey')); - - //clean up - $this->view->deleteAll('/dummyUser'); - } - - /** - * @medium - */ - function testSetPrivateSystemKey() { - - $key = "dummy key"; - $keyName = "myDummyKey"; - $encHeader = \OCA\Files_Encryption\Crypt::generateHeader(); - - \OCA\Files_Encryption\Keymanager::setPrivateSystemKey($key, $keyName); - - $this->assertTrue($this->view->file_exists('/files_encryption/' . $keyName . '.privateKey')); - - $result = \OCA\Files_Encryption\Keymanager::getPrivateSystemKey($keyName); - - $this->assertSame($encHeader . $key, $result); - - // clean up - $this->view->unlink('/files_encryption/' . $keyName.'.privateKey'); - } - - - /** - * @medium - */ - function testGetUserKeys() { - - $keys = \OCA\Files_Encryption\Keymanager::getUserKeys($this->view, $this->userId); - - $resPublic = openssl_pkey_get_public($keys['publicKey']); - - $this->assertTrue(is_resource($resPublic)); - - $sslInfoPublic = openssl_pkey_get_details($resPublic); - - $this->assertArrayHasKey('key', $sslInfoPublic); - - $privateKey = \OCA\Files_Encryption\Crypt::decryptPrivateKey($keys['privateKey'], $this->pass); - - $resPrivate = openssl_pkey_get_private($privateKey); - - $this->assertTrue(is_resource($resPrivate)); - - $sslInfoPrivate = openssl_pkey_get_details($resPrivate); - - $this->assertArrayHasKey('key', $sslInfoPrivate); - } - - /** - * @medium - */ - function testRecursiveDelShareKeysFolder() { - - $this->view->mkdir('/' . self::TEST_USER . '/files/folder1'); - $this->view->file_put_contents('/' . self::TEST_USER . '/files/folder1/existingFile.txt', 'data'); - - // create folder structure for some dummy share key files - $this->view->mkdir('/' . self::TEST_USER . '/files_encryption/keys/folder1'); - $this->view->mkdir('/' . self::TEST_USER . '/files_encryption/keys/folder1/existingFile.txt'); - $this->view->mkdir('/' . self::TEST_USER . '/files_encryption/keys/folder1/file1'); - $this->view->mkdir('/' . self::TEST_USER . '/files_encryption/keys/folder1/file2'); - $this->view->mkdir('/' . self::TEST_USER . '/files_encryption/keys/folder1/subfolder'); - $this->view->mkdir('/' . self::TEST_USER . '/files_encryption/keys/folder1/subfolder/file2'); - $this->view->mkdir('/' . self::TEST_USER . '/files_encryption/keys/folder1/subfolder/subsubfolder'); - $this->view->mkdir('/' . self::TEST_USER . '/files_encryption/keys/folder1/subfolder/subsubfolder/file1'); - $this->view->mkdir('/' . self::TEST_USER . '/files_encryption/keys/folder1/subfolder/subsubfolder/file2'); - - // create some dummy share keys - $this->view->file_put_contents('/' . self::TEST_USER . '/files_encryption/keys/folder1/existingFile.txt/user1.shareKey', 'data'); - $this->view->file_put_contents('/' . self::TEST_USER . '/files_encryption/keys/folder1/existingFile.txt/' . self::TEST_USER . '.shareKey', 'data'); - $this->view->file_put_contents('/' . self::TEST_USER . '/files_encryption/keys/folder1/file1/user1.shareKey', 'data'); - $this->view->file_put_contents('/' . self::TEST_USER . '/files_encryption/keys/folder1/file1/user1.test.shareKey', 'data'); - $this->view->file_put_contents('/' . self::TEST_USER . '/files_encryption/keys/folder1/file1/test-keymanager-userxdot.shareKey', 'data'); - $this->view->file_put_contents('/' . self::TEST_USER . '/files_encryption/keys/folder1/file1/userx.' . self::TEST_USER . '.shareKey', 'data'); - $this->view->file_put_contents('/' . self::TEST_USER . '/files_encryption/keys/folder1/file1/' . self::TEST_USER . '.userx.shareKey', 'data'); - $this->view->file_put_contents('/' . self::TEST_USER . '/files_encryption/keys/folder1/file1/user1.' . self::TEST_USER . '.shareKey', 'data'); - $this->view->file_put_contents('/' . self::TEST_USER . '/files_encryption/keys/folder1/file1/' . self::TEST_USER . '.user1.shareKey', 'data'); - $this->view->file_put_contents('/' . self::TEST_USER . '/files_encryption/keys/folder1/file2/user2.shareKey', 'data'); - $this->view->file_put_contents('/' . self::TEST_USER . '/files_encryption/keys/folder1/file2/user3.shareKey', 'data'); - $this->view->file_put_contents('/' . self::TEST_USER . '/files_encryption/keys/folder1/subfolder/file2/user3.shareKey', 'data'); - $this->view->file_put_contents('/' . self::TEST_USER . '/files_encryption/keys/folder1/subfolder/subsubfolder/file1/user1.shareKey', 'data'); - $this->view->file_put_contents('/' . self::TEST_USER . '/files_encryption/keys/folder1/subfolder/subsubfolder/file2/user2.shareKey', 'data'); - $this->view->file_put_contents('/' . self::TEST_USER . '/files_encryption/keys/folder1/subfolder/subsubfolder/file2/user3.shareKey', 'data'); - - // recursive delete share keys from user1 and user2 - \OCA\Files_Encryption\Keymanager::delShareKey($this->view, - array('user1', 'user2', self::TEST_USER), - \OCA\Files_Encryption\Keymanager::getKeyPath($this->view, new \OCA\Files_Encryption\Util($this->view, self::TEST_USER), '/folder1'), - self::TEST_USER, - '/folder1'); - - // check if share keys from user1 and user2 are deleted - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_USER . '/files_encryption/keys/folder1/existingFile.txt/user1.shareKey')); - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_USER . '/files_encryption/share-keys/folder1/file1/user1.shareKey')); - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_USER . '/files_encryption/share-keys/folder1/file2/user2.shareKey')); - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_USER . '/files_encryption/share-keys/folder1/subfolder/subsubfolder/file1/user1.shareKey')); - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_USER . '/files_encryption/share-keys/folder1/subfolder/subsubfolder/file2/user2.shareKey')); - - // check if share keys from user3 still exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_USER . '/files_encryption/keys/folder1/file2/user3.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_USER . '/files_encryption/keys/folder1/subfolder/subsubfolder/file2/user3.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_USER . '/files_encryption/keys/folder1/subfolder/file2/user3.shareKey')); - - // check if share keys for user or file with similar name - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_USER . '/files_encryption/keys/folder1/file1/user1.test.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_USER . '/files_encryption/keys/folder1/file1/test-keymanager-userxdot.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_USER . '/files_encryption/keys/folder1/file1/' . self::TEST_USER . '.userx.shareKey')); - // FIXME: this case currently cannot be distinguished, needs further fixing - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_USER . '/files_encryption/keys/folder1/file1/userx.' . self::TEST_USER . '.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_USER . '/files_encryption/keys/folder1/file1/user1.' . self::TEST_USER . '.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_USER . '/files_encryption/keys/folder1/file1/' . self::TEST_USER . '.user1.shareKey')); - - // owner key from existing file should still exists because the file is still there - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_USER . '/files_encryption/keys/folder1/existingFile.txt/' . self::TEST_USER . '.shareKey')); - - // cleanup - $this->view->deleteAll('/' . self::TEST_USER . '/files/folder1'); - - } - - /** - * @medium - */ - function testRecursiveDelShareKeysFile() { - - $this->view->mkdir('/' . self::TEST_USER . '/files/folder1'); - $this->view->file_put_contents('/' . self::TEST_USER . '/files/folder1/existingFile.txt', 'data'); - - // create folder structure for some dummy share key files - $this->view->mkdir('/' . self::TEST_USER . '/files_encryption/keys/folder1'); - $this->view->mkdir('/' . self::TEST_USER . '/files_encryption/keys/folder1/existingFile.txt'); - - // create some dummy share keys - $this->view->file_put_contents('/' . self::TEST_USER . '/files_encryption/keys/folder1/existingFile.txt/user1.shareKey', 'data'); - $this->view->file_put_contents('/' . self::TEST_USER . '/files_encryption/keys/folder1/existingFile.txt/user2.shareKey', 'data'); - $this->view->file_put_contents('/' . self::TEST_USER . '/files_encryption/keys/folder1/existingFile.txt/user3.shareKey', 'data'); - $this->view->file_put_contents('/' . self::TEST_USER . '/files_encryption/keys/folder1/existingFile.txt/' . self::TEST_USER . '.shareKey', 'data'); - - // recursive delete share keys from user1 and user2 - \OCA\Files_Encryption\Keymanager::delShareKey($this->view, - array('user1', 'user2', self::TEST_USER), - \OCA\Files_Encryption\Keymanager::getKeyPath($this->view, new \OCA\Files_Encryption\Util($this->view, self::TEST_USER), '/folder1/existingFile.txt'), - self::TEST_USER, - '/folder1/existingFile.txt'); - - - // check if share keys from user1 and user2 are deleted - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_USER . '/files_encryption/keys/folder1/existingFile/user1.shareKey')); - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_USER . '/files_encryption/keys/folder1/existingFile/user2.shareKey')); - - // check if share keys for user3 and owner - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_USER . '/files_encryption/keys/folder1/existingFile.txt/' . self::TEST_USER . '.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_USER . '/files_encryption/keys/folder1/existingFile.txt/user3.shareKey')); - // cleanup - $this->view->deleteAll('/' . self::TEST_USER . '/files/folder1'); - - } - - function testKeySetPreperation() { - $basePath = '/' . self::TEST_USER . '/files'; - $path = '/folder1/subfolder/subsubfolder/file.txt'; - - $this->assertFalse($this->view->is_dir($basePath . '/testKeySetPreperation')); - - TestProtectedKeymanagerMethods::testKeySetPreperation($this->view, $basePath . $path); - - // check if directory structure was created - $this->assertTrue($this->view->is_dir($basePath . $path)); - - // cleanup - $this->view->deleteAll($basePath . '/folder1'); - - } -} - -/** - * dummy class to access protected methods of \OCA\Files_Encryption\Keymanager for testing - */ -class TestProtectedKeymanagerMethods extends \OCA\Files_Encryption\Keymanager { - - /** - * @param \OC\Files\View $view relative to data/ - * @param string $path - * @param string $basePath - */ - public static function testKeySetPreperation($view, $path) { - self::keySetPreparation($view, $path); - } -} diff --git a/apps/files_encryption/tests/legacy-encrypted-text.txt b/apps/files_encryption/tests/legacy-encrypted-text.txt deleted file mode 100644 index 1f5087178cd..00000000000 --- a/apps/files_encryption/tests/legacy-encrypted-text.txt +++ /dev/null @@ -1 +0,0 @@ -5ǡiZgESlF=
\ No newline at end of file diff --git a/apps/files_encryption/tests/migration.php b/apps/files_encryption/tests/migration.php deleted file mode 100644 index 031c327d371..00000000000 --- a/apps/files_encryption/tests/migration.php +++ /dev/null @@ -1,266 +0,0 @@ -<?php -/** - * @author Björn Schießle <schiessle@owncloud.com> - * @author Joas Schilling <nickvergessen@owncloud.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * 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. - * - * This program 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ - -namespace OCA\Files_Encryption\Tests; - -class Migration extends TestCase { - - const TEST_ENCRYPTION_MIGRATION_USER1='test_encryption_user1'; - const TEST_ENCRYPTION_MIGRATION_USER2='test_encryption_user2'; - const TEST_ENCRYPTION_MIGRATION_USER3='test_encryption_user3'; - - /** @var \OC\Files\View */ - private $view; - private $public_share_key_id; - private $recovery_key_id; - - public static function setUpBeforeClass() { - parent::setUpBeforeClass(); - self::loginHelper(self::TEST_ENCRYPTION_MIGRATION_USER1, true); - self::loginHelper(self::TEST_ENCRYPTION_MIGRATION_USER2, true); - self::loginHelper(self::TEST_ENCRYPTION_MIGRATION_USER3, true); - } - - public static function tearDownAfterClass() { - \OC_User::deleteUser(self::TEST_ENCRYPTION_MIGRATION_USER1); - \OC_User::deleteUser(self::TEST_ENCRYPTION_MIGRATION_USER2); - \OC_User::deleteUser(self::TEST_ENCRYPTION_MIGRATION_USER3); - parent::tearDownAfterClass(); - } - - protected function tearDown() { - if (\OC_DB::tableExists('encryption_test')) { - \OC_DB::dropTable('encryption_test'); - } - $this->assertTableNotExist('encryption_test'); - - parent::tearDown(); - } - - public function setUp() { - $this->loginHelper(self::TEST_ENCRYPTION_MIGRATION_USER1); - $this->view = new \OC\Files\View(); - $this->public_share_key_id = \OCA\Files_Encryption\Helper::getPublicShareKeyId(); - $this->recovery_key_id = \OCA\Files_Encryption\Helper::getRecoveryKeyId(); - if (\OC_DB::tableExists('encryption_test')) { - \OC_DB::dropTable('encryption_test'); - } - $this->assertTableNotExist('encryption_test'); - } - - public function checkLastIndexId() { - $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (' - .' `item_type`, `item_source`, `item_target`, `share_type`,' - .' `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`,' - .' `file_target`, `token`, `parent`, `expiration`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)'); - $query->bindValue(1, 'file'); - $query->bindValue(2, 949); - $query->bindValue(3, '/949'); - $query->bindValue(4, 0); - $query->bindValue(5, 'migrate-test-user'); - $query->bindValue(6, 'migrate-test-owner'); - $query->bindValue(7, 23); - $query->bindValue(8, 1402493312); - $query->bindValue(9, 0); - $query->bindValue(10, '/migration.txt'); - $query->bindValue(11, null); - $query->bindValue(12, null); - $query->bindValue(13, null); - $this->assertEquals(1, $query->execute()); - - $this->assertNotEquals('0', \OC_DB::insertid('*PREFIX*share')); - - // cleanup - $query = \OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `file_target` = ?'); - $query->bindValue(1, '/migration.txt'); - $this->assertEquals(1, $query->execute()); - - } - - public function testBrokenLastIndexId() { - - // create test table - $this->checkLastIndexId(); - \OC_DB::createDbFromStructure(__DIR__ . '/encryption_table.xml'); - $this->checkLastIndexId(); - } - - /** - * @param string $table - */ - public function assertTableNotExist($table) { - $type = \OC_Config::getValue( "dbtype", "sqlite" ); - if( $type == 'sqlite' || $type == 'sqlite3' ) { - // sqlite removes the tables after closing the DB - $this->assertTrue(true); - } else { - $this->assertFalse(\OC_DB::tableExists($table), 'Table ' . $table . ' exists.'); - } - } - - protected function createDummyShareKeys($uid) { - $this->view->mkdir($uid . '/files_encryption/share-keys/folder1/folder2/folder3'); - $this->view->mkdir($uid . '/files_encryption/share-keys/folder2/'); - $this->view->file_put_contents($uid . '/files_encryption/share-keys/folder1/folder2/folder3/file3.' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey' , 'data'); - $this->view->file_put_contents($uid . '/files_encryption/share-keys/folder1/folder2/folder3/file3.' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey' , 'data'); - $this->view->file_put_contents($uid . '/files_encryption/share-keys/folder1/folder2/folder3/file3.' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey' , 'data'); - $this->view->file_put_contents($uid . '/files_encryption/share-keys/folder1/folder2/file2.' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey' , 'data'); - $this->view->file_put_contents($uid . '/files_encryption/share-keys/folder1/folder2/file2.' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey' , 'data'); - $this->view->file_put_contents($uid . '/files_encryption/share-keys/folder1/folder2/file2.' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey' , 'data'); - $this->view->file_put_contents($uid . '/files_encryption/share-keys/folder1/file.1.' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey' , 'data'); - $this->view->file_put_contents($uid . '/files_encryption/share-keys/folder1/file.1.' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey' , 'data'); - $this->view->file_put_contents($uid . '/files_encryption/share-keys/folder1/file.1.' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey' , 'data'); - $this->view->file_put_contents($uid . '/files_encryption/share-keys/folder2/file.2.1.' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey' , 'data'); - $this->view->file_put_contents($uid . '/files_encryption/share-keys/folder2/file.2.1.' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey' , 'data'); - $this->view->file_put_contents($uid . '/files_encryption/share-keys/folder2/file.2.1.' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey' , 'data'); - if ($this->public_share_key_id) { - $this->view->file_put_contents($uid . '/files_encryption/share-keys/folder2/file.2.1.' . $this->public_share_key_id . '.shareKey' , 'data'); - } - if ($this->recovery_key_id) { - $this->view->file_put_contents($uid . '/files_encryption/share-keys/folder2/file.2.1.' . $this->recovery_key_id . '.shareKey' , 'data'); - } - } - - protected function createDummyFileKeys($uid) { - $this->view->mkdir($uid . '/files_encryption/keyfiles/folder1/folder2/folder3'); - $this->view->mkdir($uid . '/files_encryption/keyfiles/folder2/'); - $this->view->file_put_contents($uid . '/files_encryption/keyfiles/folder1/folder2/folder3/file3.key' , 'data'); - $this->view->file_put_contents($uid . '/files_encryption/keyfiles/folder1/folder2/file2.key' , 'data'); - $this->view->file_put_contents($uid . '/files_encryption/keyfiles/folder1/file.1.key' , 'data'); - $this->view->file_put_contents($uid . '/files_encryption/keyfiles/folder2/file.2.1.key' , 'data'); - } - - protected function createDummyFilesInTrash($uid) { - $this->view->mkdir($uid . '/files_trashbin/share-keys'); - $this->view->mkdir($uid . '/files_trashbin/share-keys/folder1.d7437648723'); - $this->view->file_put_contents($uid . '/files_trashbin/share-keys/file1.' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey.d5457864' , 'data'); - $this->view->file_put_contents($uid . '/files_trashbin/share-keys/file1.' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey.d5457864' , 'data'); - $this->view->file_put_contents($uid . '/files_trashbin/share-keys/folder1.d7437648723/file2.' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey' , 'data'); - - $this->view->mkdir($uid . '/files_trashbin/keyfiles'); - $this->view->mkdir($uid . '/files_trashbin/keyfiles/folder1.d7437648723'); - $this->view->file_put_contents($uid . '/files_trashbin/keyfiles/file1.key.d5457864' , 'data'); - $this->view->file_put_contents($uid . '/files_trashbin/keyfiles/folder1.d7437648723/file2.key' , 'data'); - } - - protected function createDummySystemWideKeys() { - $this->view->mkdir('owncloud_private_key'); - $this->view->file_put_contents('owncloud_private_key/systemwide_1.private.key', 'data'); - $this->view->file_put_contents('owncloud_private_key/systemwide_2.private.key', 'data'); - } - - public function testMigrateToNewFolderStructure() { - - // go back to the state before migration - $this->view->rename('/files_encryption/public_keys', '/public-keys'); - $this->view->rename('/public-keys/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.publicKey', '/public-keys/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.public.key'); - $this->view->rename('/public-keys/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.publicKey', '/public-keys/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.public.key'); - $this->view->rename('/public-keys/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.publicKey', '/public-keys/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.public.key'); - $this->view->deleteAll(self::TEST_ENCRYPTION_MIGRATION_USER1 . '/files_encryption/keys'); - $this->view->deleteAll(self::TEST_ENCRYPTION_MIGRATION_USER2 . '/files_encryption/keys'); - $this->view->deleteAll(self::TEST_ENCRYPTION_MIGRATION_USER3 . '/files_encryption/keys'); - $this->view->rename(self::TEST_ENCRYPTION_MIGRATION_USER1 . '/files_encryption/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.privateKey', - self::TEST_ENCRYPTION_MIGRATION_USER1 . '/files_encryption/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.private.key'); - $this->view->rename(self::TEST_ENCRYPTION_MIGRATION_USER2 . '/files_encryption/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.privateKey', - self::TEST_ENCRYPTION_MIGRATION_USER2 . '/files_encryption/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.private.key'); - $this->view->rename(self::TEST_ENCRYPTION_MIGRATION_USER3 . '/files_encryption/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.privateKey', - self::TEST_ENCRYPTION_MIGRATION_USER3 . '/files_encryption/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.private.key'); - - $this->createDummyShareKeys(self::TEST_ENCRYPTION_MIGRATION_USER1); - $this->createDummyShareKeys(self::TEST_ENCRYPTION_MIGRATION_USER2); - $this->createDummyShareKeys(self::TEST_ENCRYPTION_MIGRATION_USER3); - - $this->createDummyFileKeys(self::TEST_ENCRYPTION_MIGRATION_USER1); - $this->createDummyFileKeys(self::TEST_ENCRYPTION_MIGRATION_USER2); - $this->createDummyFileKeys(self::TEST_ENCRYPTION_MIGRATION_USER3); - - $this->createDummyFilesInTrash(self::TEST_ENCRYPTION_MIGRATION_USER2); - - // no user for system wide mount points - $this->createDummyFileKeys(''); - $this->createDummyShareKeys(''); - - $this->createDummySystemWideKeys(); - - $m = new \OCA\Files_Encryption\Migration(); - $m->reorganizeFolderStructure(); - - // TODO Verify that all files at the right place - $this->assertTrue($this->view->file_exists('/files_encryption/public_keys/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.publicKey')); - $this->assertTrue($this->view->file_exists('/files_encryption/public_keys/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.publicKey')); - $this->assertTrue($this->view->file_exists('/files_encryption/public_keys/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.publicKey')); - $this->verifyNewKeyPath(self::TEST_ENCRYPTION_MIGRATION_USER1); - $this->verifyNewKeyPath(self::TEST_ENCRYPTION_MIGRATION_USER2); - $this->verifyNewKeyPath(self::TEST_ENCRYPTION_MIGRATION_USER3); - // system wide keys - $this->verifyNewKeyPath(''); - // trash - $this->verifyFilesInTrash(self::TEST_ENCRYPTION_MIGRATION_USER2); - - } - - protected function verifyFilesInTrash($uid) { - // share keys - $this->view->file_exists($uid . '/files_trashbin/keys/file1.d5457864/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey.d5457864' , 'data'); - $this->view->file_exists($uid . '/files_trashbin/keys/file1.d5457864/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey.d5457864' , 'data'); - $this->view->file_exists($uid . '/files_trashbin/keys/folder1.d7437648723/file2/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey' , 'data'); - - // file keys - $this->view->file_exists($uid . '/files_trashbin/keys/file1.d5457864/fileKey.d5457864' , 'data'); - $this->view->file_exists($uid . '/files_trashbin/keyfiles/file1.d5457864/fileKey.d5457864' , 'data'); - $this->view->file_exists($uid . '/files_trashbin/keyfiles/folder1.d7437648723/file2/fileKey' , 'data'); - } - - protected function verifyNewKeyPath($uid) { - // private key - if ($uid !== '') { - $this->assertTrue($this->view->file_exists($uid . '/files_encryption/' . $uid . '.privateKey')); - } - // file keys - $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/folder1/folder2/folder3/file3/fileKey')); - $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/folder1/folder2/file2/fileKey')); - $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/folder1/file.1/fileKey')); - $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/folder2/file.2.1/fileKey')); - // share keys - $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/folder1/folder2/folder3/file3/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey')); - $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/folder1/folder2/folder3/file3/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey')); - $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/folder1/folder2/folder3/file3/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey')); - $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/folder1/folder2/file2/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey')); - $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/folder1/folder2/file2/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey')); - $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/folder1/folder2/file2/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey')); - $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/folder1/file.1/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey')); - $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/folder1/file.1/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey')); - $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/folder1/file.1/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey')); - $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/folder2/file.2.1/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey')); - $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/folder2/file.2.1/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey')); - $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/folder2/file.2.1/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey')); - if ($this->public_share_key_id) { - $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/folder2/file.2.1/' . $this->public_share_key_id . '.shareKey')); - } - if ($this->recovery_key_id) { - $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/folder2/file.2.1/' . $this->recovery_key_id . '.shareKey')); - } - } -} diff --git a/apps/files_encryption/tests/proxy.php b/apps/files_encryption/tests/proxy.php deleted file mode 100644 index 59fcb896a2b..00000000000 --- a/apps/files_encryption/tests/proxy.php +++ /dev/null @@ -1,154 +0,0 @@ -<?php -/** - * @author Björn Schießle <schiessle@owncloud.com> - * @author Joas Schilling <nickvergessen@owncloud.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Sam Tuke <mail@samtuke.com> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * 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. - * - * This program 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ - -namespace OCA\Files_Encryption\Tests; - -/** - * Class Proxy - * this class provide basic proxy app tests - */ -class Proxy extends TestCase { - - const TEST_ENCRYPTION_PROXY_USER1 = "test-proxy-user1"; - - public $userId; - public $pass; - /** - * @var \OC\Files\View - */ - public $view; // view in /data/user/files - public $rootView; // view on /data/user - public $data; - public $dataLong; - public $filename; - - public static function setUpBeforeClass() { - parent::setUpBeforeClass(); - - // create test user - self::loginHelper(self::TEST_ENCRYPTION_PROXY_USER1, true); - } - - protected function setUp() { - parent::setUp(); - - // set user id - \OC_User::setUserId(self::TEST_ENCRYPTION_PROXY_USER1); - $this->userId = self::TEST_ENCRYPTION_PROXY_USER1; - $this->pass = self::TEST_ENCRYPTION_PROXY_USER1; - - // init filesystem view - $this->view = new \OC\Files\View('/'. self::TEST_ENCRYPTION_PROXY_USER1 . '/files'); - $this->rootView = new \OC\Files\View('/'. self::TEST_ENCRYPTION_PROXY_USER1 ); - - // init short data - $this->data = 'hats'; - $this->dataLong = file_get_contents(__DIR__ . '/../lib/crypt.php'); - $this->filename = 'enc_proxy_tests-' . $this->getUniqueID() . '.txt'; - - } - - public static function tearDownAfterClass() { - // cleanup test user - \OC_User::deleteUser(self::TEST_ENCRYPTION_PROXY_USER1); - - parent::tearDownAfterClass(); - } - - /** - * @medium - * test if postFileSize returns the unencrypted file size - */ - function testPostFileSize() { - - $this->view->file_put_contents($this->filename, $this->dataLong); - $size = strlen($this->dataLong); - - \OC_FileProxy::$enabled = false; - - $encryptedSize = $this->view->filesize($this->filename); - - \OC_FileProxy::$enabled = true; - - $unencryptedSize = $this->view->filesize($this->filename); - - $this->assertTrue($encryptedSize > $unencryptedSize); - $this->assertSame($size, $unencryptedSize); - - // cleanup - $this->view->unlink($this->filename); - - } - - function testPostFileSizeWithDirectory() { - - $this->view->file_put_contents($this->filename, $this->data); - - \OC_FileProxy::$enabled = false; - - // get root size, must match the file's unencrypted size - $unencryptedSize = $this->view->filesize(''); - - \OC_FileProxy::$enabled = true; - - $encryptedSize = $this->view->filesize(''); - - $this->assertTrue($encryptedSize !== $unencryptedSize); - - // cleanup - $this->view->unlink($this->filename); - - } - - /** - * @dataProvider isExcludedPathProvider - */ - function testIsExcludedPath($path, $expected) { - $this->view->mkdir(dirname($path)); - $this->view->file_put_contents($path, "test"); - - $result = \Test_Helper::invokePrivate(new \OCA\Files_Encryption\Proxy(), 'isExcludedPath', array($path)); - $this->assertSame($expected, $result); - - $this->view->deleteAll(dirname($path)); - - } - - public function isExcludedPathProvider() { - return array( - array ('/' . self::TEST_ENCRYPTION_PROXY_USER1 . '/files/test.txt', false), - array (self::TEST_ENCRYPTION_PROXY_USER1 . '/files/test.txt', false), - array ('/files/test.txt', true), - array ('/' . self::TEST_ENCRYPTION_PROXY_USER1 . '/files/versions/test.txt', false), - array ('/' . self::TEST_ENCRYPTION_PROXY_USER1 . '/files_versions/test.txt', false), - array ('/' . self::TEST_ENCRYPTION_PROXY_USER1 . '/files_trashbin/test.txt', true), - array ('/' . self::TEST_ENCRYPTION_PROXY_USER1 . '/file/test.txt', true), - ); - } - -} - diff --git a/apps/files_encryption/tests/share.php b/apps/files_encryption/tests/share.php deleted file mode 100755 index 2a9f0359c91..00000000000 --- a/apps/files_encryption/tests/share.php +++ /dev/null @@ -1,1392 +0,0 @@ -<?php -/** - * @author Bart Visscher <bartv@thisnet.nl> - * @author Björn Schießle <schiessle@owncloud.com> - * @author Florin Peter <github@florin-peter.de> - * @author Joas Schilling <nickvergessen@owncloud.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Lukas Reschke <lukas@owncloud.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <icewind@owncloud.com> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * 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. - * - * This program 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ - -namespace OCA\Files_Encryption\Tests; - -/** - * Class Share - */ -class Share extends TestCase { - - const TEST_ENCRYPTION_SHARE_USER1 = "test-share-user1"; - const TEST_ENCRYPTION_SHARE_USER2 = "test-share-user2"; - const TEST_ENCRYPTION_SHARE_USER3 = "test-share-user3"; - const TEST_ENCRYPTION_SHARE_USER4 = "test-share-user4"; - const TEST_ENCRYPTION_SHARE_GROUP1 = "test-share-group1"; - - public $stateFilesTrashbin; - public $filename; - public $dataShort; - /** - * @var \OC\Files\View - */ - public $view; - public $folder1; - public $subfolder; - public $subsubfolder; - - public static function setUpBeforeClass() { - parent::setUpBeforeClass(); - - // enable resharing - \OC::$server->getAppConfig()->setValue('core', 'shareapi_allow_resharing', 'yes'); - - // register share hooks - \OC::registerShareHooks(); - \OCA\Files_Sharing\Helper::registerHooks(); - - // create users - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1, true); - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER2, true); - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER3, true); - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER4, true); - - // create group and assign users - \OC_Group::createGroup(self::TEST_ENCRYPTION_SHARE_GROUP1); - \OC_Group::addToGroup(self::TEST_ENCRYPTION_SHARE_USER3, self::TEST_ENCRYPTION_SHARE_GROUP1); - \OC_Group::addToGroup(self::TEST_ENCRYPTION_SHARE_USER4, self::TEST_ENCRYPTION_SHARE_GROUP1); - } - - protected function setUp() { - parent::setUp(); - - $this->dataShort = 'hats'; - $this->view = new \OC\Files\View('/'); - - $this->folder1 = '/folder1'; - $this->subfolder = '/subfolder1'; - $this->subsubfolder = '/subsubfolder1'; - - $this->filename = 'share-tmp.test'; - - // remember files_trashbin state - $this->stateFilesTrashbin = \OC_App::isEnabled('files_trashbin'); - - // we don't want to tests with app files_trashbin enabled - \OC_App::disable('files_trashbin'); - - // login as first user - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - $this->createMocks(); - } - - protected function tearDown() { - // reset app files_trashbin - if ($this->stateFilesTrashbin) { - \OC_App::enable('files_trashbin'); - } else { - \OC_App::disable('files_trashbin'); - } - - $this->restoreHttpHelper(); - - parent::tearDown(); - } - - public static function tearDownAfterClass() { - // clean group - \OC_Group::deleteGroup(self::TEST_ENCRYPTION_SHARE_GROUP1); - - // cleanup users - \OC_User::deleteUser(self::TEST_ENCRYPTION_SHARE_USER1); - \OC_User::deleteUser(self::TEST_ENCRYPTION_SHARE_USER2); - \OC_User::deleteUser(self::TEST_ENCRYPTION_SHARE_USER3); - \OC_User::deleteUser(self::TEST_ENCRYPTION_SHARE_USER4); - - parent::tearDownAfterClass(); - } - - private function createMocks() { - $config = $this->getMockBuilder('\OCP\IConfig') - ->disableOriginalConstructor()->getMock(); - $clientService = $this->getMock('\OCP\Http\Client\IClientService'); - $httpHelperMock = $this->getMockBuilder('\OC\HTTPHelper') - ->setConstructorArgs([$config, $clientService]) - ->getMock(); - $httpHelperMock->expects($this->any())->method('post')->with($this->anything())->will($this->returnValue(array('success' => true, 'result' => "{'ocs' : { 'meta' : { 'statuscode' : 100 }}}"))); - - $this->registerHttpHelper($httpHelperMock); - } - - /** - * Register an http helper mock for testing purposes. - * @param $httpHelper http helper mock - */ - private function registerHttpHelper($httpHelper) { - $this->oldHttpHelper = \OC::$server->query('HTTPHelper'); - \OC::$server->registerService('HTTPHelper', function ($c) use ($httpHelper) { - return $httpHelper; - }); - } - - /** - * Restore the original http helper - */ - private function restoreHttpHelper() { - $oldHttpHelper = $this->oldHttpHelper; - \OC::$server->registerService('HTTPHelper', function ($c) use ($oldHttpHelper) { - return $oldHttpHelper; - }); - } - - /** - * @medium - */ - function testDeclineServer2ServerShare() { - - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // save file with content - $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort); - - // test that data was successfully written - $this->assertInternalType('int', $cryptedFile); - - // get the file info from previous created file - $fileInfo = $this->view->getFileInfo( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); - - - // share the file - $token = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, '', \OCP\Constants::PERMISSION_ALL); - $this->assertTrue(is_string($token)); - - $publicShareKeyId = \OC::$server->getConfig()->getAppValue('files_encryption', 'publicShareKeyId'); - - // check if share key for public exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . $publicShareKeyId . '.shareKey')); - - // manipulate share - $query = \OC::$server->getDatabaseConnection()->prepare('UPDATE `*PREFIX*share` SET `share_type` = ?, `share_with` = ? WHERE `token`=?'); - $this->assertTrue($query->execute(array(\OCP\Share::SHARE_TYPE_REMOTE, 'foo@bar', $token))); - - // check if share key not exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . $publicShareKeyId . '.shareKey')); - - - $query = \OC::$server->getDatabaseConnection()->prepare('SELECT * FROM `*PREFIX*share` WHERE `token`=?'); - $query->execute(array($token)); - - $share = $query->fetch(); - - $_POST['token'] = $token; - $s2s = new \OCA\Files_Sharing\API\Server2Server(); - $s2s->declineShare(array('id' => $share['id'])); - - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . $publicShareKeyId . '.shareKey')); - - } - - /** - * @medium - * @param bool $withTeardown - */ - function testShareFile($withTeardown = true) { - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // save file with content - $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort); - - // test that data was successfully written - $this->assertInternalType('int', $cryptedFile); - - // disable encryption proxy to prevent recursive calls - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = false; - - // get the file info from previous created file - $fileInfo = $this->view->getFileInfo( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); - - // check if we have a valid file info - $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); - - // check if the unencrypted file size is stored - $this->assertGreaterThan(0, $fileInfo['unencrypted_size']); - - // re-enable the file proxy - \OC_FileProxy::$enabled = $proxyStatus; - - // share the file - \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER2, \OCP\Constants::PERMISSION_ALL); - - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // check if share key for user1 exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); - - // login as user1 - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER2); - - // get file contents - $retrievedCryptedFile = $this->view->file_get_contents( - '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files/' . $this->filename); - - // check if data is the same as we previously written - $this->assertEquals($this->dataShort, $retrievedCryptedFile); - - // cleanup - if ($withTeardown) { - - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // unshare the file - \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER2); - - // check if share key not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); - - // cleanup - $this->view->chroot('/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/'); - $this->view->unlink($this->filename); - $this->view->chroot('/'); - - // check if share key not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); - } - } - - function testDownloadVersions() { - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - $rootView = new \OC\Files\View(); - - // save file twice to create a new version - \OC\Files\Filesystem::file_put_contents($this->filename, "revision1"); - \OCA\Files_Versions\Storage::store($this->filename); - \OC\Files\Filesystem::file_put_contents($this->filename, "revision2"); - - // check if the owner can retrieve the correct version - $versions = \OCA\Files_Versions\Storage::getVersions(self::TEST_ENCRYPTION_SHARE_USER1, $this->filename); - $this->assertSame(1, count($versions)); - $version = reset($versions); - $versionUser1 = $rootView->file_get_contents('/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_versions/' . $this->filename . '.v' . $version['version']); - $this->assertSame('revision1', $versionUser1); - - // share the file - $fileInfo = \OC\Files\Filesystem::getFileInfo($this->filename); - $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); - $this->assertTrue(\OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER2, \OCP\Constants::PERMISSION_ALL)); - - // try to download the version as user2 - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER2); - $versionUser2 = $rootView->file_get_contents('/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_versions/' . $this->filename . '.v' . $version['version']); - $this->assertSame('revision1', $versionUser2); - - //cleanup - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER2); - \OC\Files\Filesystem::unlink($this->filename); - } - - /** - * @medium - * @param bool $withTeardown - */ - function testReShareFile($withTeardown = true) { - $this->testShareFile(false); - - // login as user2 - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER2); - - // get the file info - $fileInfo = $this->view->getFileInfo( - '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files/' . $this->filename); - - // share the file with user3 - \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER3, \OCP\Constants::PERMISSION_ALL); - - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // check if share key for user2 exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER3 . '.shareKey')); - - // login as user2 - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER3); - - // get file contents - $retrievedCryptedFile = $this->view->file_get_contents( - '/' . self::TEST_ENCRYPTION_SHARE_USER3 . '/files/' . $this->filename); - - // check if data is the same as previously written - $this->assertEquals($this->dataShort, $retrievedCryptedFile); - - // cleanup - if ($withTeardown) { - - // login as user1 - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER2); - - // unshare the file with user2 - \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER3); - - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // check if share key not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER3 . '.shareKey')); - - // unshare the file with user1 - \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER2); - - // check if share key not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); - - // cleanup - $this->view->chroot('/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/'); - $this->view->unlink($this->filename); - $this->view->chroot('/'); - - // check if share key not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); - } - } - - /** - * @medium - * @param bool $withTeardown - * @return array - */ - function testShareFolder($withTeardown = true) { - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // create folder structure - $this->view->mkdir('/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files' . $this->folder1); - $this->view->mkdir( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files' . $this->folder1 . $this->subfolder); - $this->view->mkdir( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files' . $this->folder1 . $this->subfolder - . $this->subsubfolder); - - // save file with content - $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' - . $this->filename, $this->dataShort); - - // test that data was successfully written - $this->assertInternalType('int', $cryptedFile); - - // disable encryption proxy to prevent recursive calls - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = false; - - // get the file info from previous created folder - $fileInfo = $this->view->getFileInfo( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files' . $this->folder1); - - // check if we have a valid file info - $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); - - // re-enable the file proxy - \OC_FileProxy::$enabled = $proxyStatus; - - // share the folder with user1 - \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER2, \OCP\Constants::PERMISSION_ALL); - - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // check if share key for user1 exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys' . $this->folder1 - . $this->subfolder . $this->subsubfolder . '/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); - - // login as user1 - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER2); - - // get file contents - $retrievedCryptedFile = $this->view->file_get_contents( - '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files/' . $this->folder1 - . $this->subfolder . $this->subsubfolder . '/' . $this->filename); - - // check if data is the same - $this->assertEquals($this->dataShort, $retrievedCryptedFile); - - // cleanup - if ($withTeardown) { - - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // unshare the folder with user1 - \OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER2); - - // check if share key not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys' - . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); - - // cleanup - $this->view->chroot('/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files'); - $this->view->unlink($this->folder1); - $this->view->chroot('/'); - - // check if share key not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys' - . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); - } - - return $fileInfo; - } - - /** - * @medium - * @param bool $withTeardown - */ - function testReShareFolder($withTeardown = true) { - $fileInfoFolder1 = $this->testShareFolder(false); - - // login as user2 - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER2); - - // disable encryption proxy to prevent recursive calls - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = false; - - // get the file info from previous created folder - $fileInfoSubFolder = $this->view->getFileInfo( - '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files/' . $this->folder1 - . $this->subfolder); - - // check if we have a valid file info - $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfoSubFolder); - - // re-enable the file proxy - \OC_FileProxy::$enabled = $proxyStatus; - - // share the file with user3 - \OCP\Share::shareItem('folder', $fileInfoSubFolder['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER3, \OCP\Constants::PERMISSION_ALL); - - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // check if share key for user3 exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys' . $this->folder1 - . $this->subfolder . $this->subsubfolder . '/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER3 . '.shareKey')); - - // login as user3 - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER3); - - // get file contents - $retrievedCryptedFile = $this->view->file_get_contents( - '/' . self::TEST_ENCRYPTION_SHARE_USER3 . '/files/' . $this->subfolder - . $this->subsubfolder . '/' . $this->filename); - - // check if data is the same - $this->assertEquals($this->dataShort, $retrievedCryptedFile); - - // get the file info - $fileInfo = $this->view->getFileInfo( - '/' . self::TEST_ENCRYPTION_SHARE_USER3 . '/files/' . $this->subfolder - . $this->subsubfolder . '/' . $this->filename); - - // check if we have fileInfos - $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); - - // share the file with user3 - \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER4, \OCP\Constants::PERMISSION_ALL); - - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // check if share key for user3 exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys' . $this->folder1 - . $this->subfolder . $this->subsubfolder . '/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER4 . '.shareKey')); - - // login as user3 - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER4); - - // get file contents - $retrievedCryptedFile = $this->view->file_get_contents( - '/' . self::TEST_ENCRYPTION_SHARE_USER4 . '/files/' . $this->filename); - - // check if data is the same - $this->assertEquals($this->dataShort, $retrievedCryptedFile); - - // cleanup - if ($withTeardown) { - - // login as user2 - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER3); - - // unshare the file with user3 - \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER4); - - // check if share key not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys' - . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER4 . '.shareKey')); - - // login as user1 - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER2); - - // unshare the folder with user2 - \OCP\Share::unshare('folder', $fileInfoSubFolder['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER3); - - // check if share key not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys' - . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER3 . '.shareKey')); - - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // unshare the folder1 with user1 - \OCP\Share::unshare('folder', $fileInfoFolder1['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER2); - - // check if share key not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys' - . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); - - // cleanup - $this->view->chroot('/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files'); - $this->view->unlink($this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename); - $this->view->chroot('/'); - - // check if share key not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys' - . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); - } - } - - - function testRemoteShareFile() { - // login as admin - //self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // save file with content - $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort); - - // test that data was successfully written - $this->assertInternalType('int', $cryptedFile); - - // disable encryption proxy to prevent recursive calls - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = false; - - // get the file info from previous created file - $fileInfo = $this->view->getFileInfo( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); - - // check if we have a valid file info - $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); - - // check if the unencrypted file size is stored - $this->assertGreaterThan(0, $fileInfo['unencrypted_size']); - - // re-enable the file proxy - \OC_FileProxy::$enabled = $proxyStatus; - - // share the file - \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_REMOTE, 'user1@server1', \OCP\Constants::PERMISSION_ALL); - - $publicShareKeyId = \OC::$server->getAppConfig()->getValue('files_encryption', 'publicShareKeyId'); - - // check if share key for public exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . $publicShareKeyId . '.shareKey')); - - // unshare the file - \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_REMOTE, 'user1@server1'); - - // check if share key not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . $publicShareKeyId . '.shareKey')); - - // cleanup - $this->view->chroot('/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/'); - $this->view->unlink($this->filename); - $this->view->chroot('/'); - - // check if share key not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); - } - - function testPublicShareFile() { - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // save file with content - $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort); - - // test that data was successfully written - $this->assertInternalType('int', $cryptedFile); - - // disable encryption proxy to prevent recursive calls - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = false; - - // get the file info from previous created file - $fileInfo = $this->view->getFileInfo( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); - - // check if we have a valid file info - $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); - - // check if the unencrypted file size is stored - $this->assertGreaterThan(0, $fileInfo['unencrypted_size']); - - // re-enable the file proxy - \OC_FileProxy::$enabled = $proxyStatus; - - // share the file - \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, false, \OCP\Constants::PERMISSION_ALL); - - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - $publicShareKeyId = \OC::$server->getAppConfig()->getValue('files_encryption', 'publicShareKeyId'); - - // check if share key for public exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . $publicShareKeyId . '.shareKey')); - - // some hacking to simulate public link - //$GLOBALS['app'] = 'files_sharing'; - //$GLOBALS['fileOwner'] = self::TEST_ENCRYPTION_SHARE_USER1; - self::logoutHelper(); - - // get file contents - $retrievedCryptedFile = file_get_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); - - // check if data is the same as we previously written - $this->assertEquals($this->dataShort, $retrievedCryptedFile); - - // tear down - - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // unshare the file - \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null); - - // check if share key not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . $publicShareKeyId . '.shareKey')); - - // cleanup - $this->view->chroot('/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/'); - $this->view->unlink($this->filename); - $this->view->chroot('/'); - - // check if share key not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); - } - - /** - * @medium - */ - function testShareFileWithGroup() { - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // save file with content - $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort); - - // test that data was successfully written - $this->assertInternalType('int', $cryptedFile); - - // disable encryption proxy to prevent recursive calls - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = false; - - // get the file info from previous created file - $fileInfo = $this->view->getFileInfo( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); - - // check if we have a valid file info - $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); - - // check if the unencrypted file size is stored - $this->assertGreaterThan(0, $fileInfo['unencrypted_size']); - - // re-enable the file proxy - \OC_FileProxy::$enabled = $proxyStatus; - - // share the file - \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, self::TEST_ENCRYPTION_SHARE_GROUP1, \OCP\Constants::PERMISSION_ALL); - - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // check if share key for user2 and user3 exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER3 . '.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER4 . '.shareKey')); - - // login as user1 - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER3); - - // get file contents - $retrievedCryptedFile = $this->view->file_get_contents( - '/' . self::TEST_ENCRYPTION_SHARE_USER3 . '/files/' . $this->filename); - - // check if data is the same as we previously written - $this->assertEquals($this->dataShort, $retrievedCryptedFile); - - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // unshare the file - \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, self::TEST_ENCRYPTION_SHARE_GROUP1); - - // check if share key not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER3 . '.shareKey')); - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER4 . '.shareKey')); - - // cleanup - $this->view->chroot('/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/'); - $this->view->unlink($this->filename); - $this->view->chroot('/'); - - // check if share key not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); - - } - - /** - * @large - */ - function testRecoveryFile() { - - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - \OCA\Files_Encryption\Helper::adminEnableRecovery(null, 'test123'); - $recoveryKeyId = \OC::$server->getAppConfig()->getValue('files_encryption', 'recoveryKeyId'); - - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - $util = new \OCA\Files_Encryption\Util(new \OC\Files\View('/'), self::TEST_ENCRYPTION_SHARE_USER1); - - // check if recovery password match - $this->assertTrue($util->checkRecoveryPassword('test123')); - - // enable recovery for admin - $this->assertTrue($util->setRecoveryForUser(1)); - $util->addRecoveryKeys(); - - // create folder structure - $this->view->mkdir('/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files' . $this->folder1); - $this->view->mkdir( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files' . $this->folder1 . $this->subfolder); - $this->view->mkdir( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files' . $this->folder1 . $this->subfolder - . $this->subsubfolder); - - // save file with content - $cryptedFile1 = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort); - $cryptedFile2 = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' - . $this->filename, $this->dataShort); - - // test that data was successfully written - $this->assertInternalType('int', $cryptedFile1); - $this->assertInternalType('int', $cryptedFile2); - - // check if share key for admin and recovery exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . $recoveryKeyId . '.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' . $this->folder1 - . $this->subfolder . $this->subsubfolder . '/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' . $this->folder1 - . $this->subfolder . $this->subsubfolder . '/' - . $this->filename . '/' . $recoveryKeyId . '.shareKey')); - - // disable recovery for admin - $this->assertTrue($util->setRecoveryForUser(0)); - - // remove all recovery keys - $util->removeRecoveryKeys('/'); - - // check if share key for recovery not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . $recoveryKeyId . '.shareKey')); - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' . $this->folder1 - . $this->subfolder . $this->subsubfolder . '/' - . $this->filename . '/' . $recoveryKeyId . '.shareKey')); - - // enable recovery for admin - $this->assertTrue($util->setRecoveryForUser(1)); - - // add recovery keys again - $util->addRecoveryKeys('/'); - - // check if share key for admin and recovery exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . $recoveryKeyId . '.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' . $this->folder1 - . $this->subfolder . $this->subsubfolder . '/' - . $this->filename . '/' . $recoveryKeyId . '.shareKey')); - - // cleanup - $this->view->chroot('/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/'); - $this->view->unlink($this->filename); - $this->view->unlink($this->folder1); - $this->view->chroot('/'); - - // check if share key for recovery not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . $recoveryKeyId . '.shareKey')); - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' . $this->folder1 - . $this->subfolder . $this->subsubfolder . '/' - . $this->filename . '/' . $recoveryKeyId . '.shareKey')); - - $this->assertTrue(\OCA\Files_Encryption\Helper::adminEnableRecovery(null, 'test123')); - $this->assertTrue(\OCA\Files_Encryption\Helper::adminDisableRecovery('test123')); - $this->assertEquals(0, \OC::$server->getAppConfig()->getValue('files_encryption', 'recoveryAdminEnabled')); - } - - /** - * @large - */ - function testRecoveryForUser() { - - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - $result = \OCA\Files_Encryption\Helper::adminEnableRecovery(null, 'test123'); - $this->assertTrue($result); - - $recoveryKeyId = \OC::$server->getAppConfig()->getValue('files_encryption', 'recoveryKeyId'); - - // login as user2 - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER2); - - $util = new \OCA\Files_Encryption\Util(new \OC\Files\View('/'), self::TEST_ENCRYPTION_SHARE_USER2); - - // enable recovery for admin - $this->assertTrue($util->setRecoveryForUser(1)); - - // add recovery keys for existing files (e.g. the auto-generated welcome.txt) - $util->addRecoveryKeys(); - - // create folder structure - $this->view->mkdir('/' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files' . $this->folder1); - $this->view->mkdir( - '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files' . $this->folder1 . $this->subfolder); - $this->view->mkdir( - '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files' . $this->folder1 . $this->subfolder - . $this->subsubfolder); - - // save file with content - $cryptedFile1 = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER2. '/files/' . $this->filename, $this->dataShort); - $cryptedFile2 = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' - . $this->filename, $this->dataShort); - - // test that data was successfully written - $this->assertInternalType('int', $cryptedFile1); - $this->assertInternalType('int', $cryptedFile2); - - // check if share key for user and recovery exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files_encryption/keys/' - . $this->filename . '/' . $recoveryKeyId . '.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files_encryption/keys/' . $this->folder1 - . $this->subfolder . $this->subsubfolder . '/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files_encryption/keys/' . $this->folder1 - . $this->subfolder . $this->subsubfolder . '/' - . $this->filename . '/' . $recoveryKeyId . '.shareKey')); - - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // change password - \OC_User::setPassword(self::TEST_ENCRYPTION_SHARE_USER2, 'test', 'test123'); - $params = array('uid' => self::TEST_ENCRYPTION_SHARE_USER2, - 'password' => 'test', - 'recoveryPassword' => 'test123'); - \OCA\Files_Encryption\Hooks::setPassphrase($params); - - // login as user2 - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER2, false, 'test'); - - // get file contents - $retrievedCryptedFile1 = file_get_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files/' . $this->filename); - $retrievedCryptedFile2 = file_get_contents( - 'crypt:///' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename); - - // check if data is the same as we previously written - $this->assertEquals($this->dataShort, $retrievedCryptedFile1); - $this->assertEquals($this->dataShort, $retrievedCryptedFile2); - - // cleanup - $this->view->chroot('/' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files/'); - $this->view->unlink($this->folder1); - $this->view->unlink($this->filename); - $this->view->chroot('/'); - - // check if share key for user and recovery exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files_encryption/keys/' - . $this->filename . '/' . $recoveryKeyId . '.shareKey')); - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files_encryption/keys/' . $this->folder1 - . $this->subfolder . $this->subsubfolder . '/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files_encryption/keys/' . $this->folder1 - . $this->subfolder . $this->subsubfolder . '/' - . $this->filename . '/' . $recoveryKeyId . '.shareKey')); - - // enable recovery for admin - $this->assertTrue($util->setRecoveryForUser(0)); - - \OCA\Files_Encryption\Helper::adminDisableRecovery('test123'); - $this->assertEquals(0, \OC::$server->getAppConfig()->getValue('files_encryption', 'recoveryAdminEnabled')); - - //clean up, reset passwords - \OC_User::setPassword(self::TEST_ENCRYPTION_SHARE_USER2, self::TEST_ENCRYPTION_SHARE_USER2, 'test123'); - $params = array('uid' => self::TEST_ENCRYPTION_SHARE_USER2, - 'password' => self::TEST_ENCRYPTION_SHARE_USER2, - 'recoveryPassword' => 'test123'); - \OCA\Files_Encryption\Hooks::setPassphrase($params); - } - - /** - * @medium - */ - function testFailShareFile() { - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // save file with content - $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort); - - // test that data was successfully written - $this->assertInternalType('int', $cryptedFile); - - // disable encryption proxy to prevent recursive calls - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = false; - - // get the file info from previous created file - $fileInfo = $this->view->getFileInfo( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); - - // check if we have a valid file info - $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); - - // check if the unencrypted file size is stored - $this->assertGreaterThan(0, $fileInfo['unencrypted_size']); - - // break users public key - $this->view->rename(\OCA\Files_Encryption\Keymanager::getPublicKeyPath() . '/' . self::TEST_ENCRYPTION_SHARE_USER3 . '.publicKey', - \OCA\Files_Encryption\Keymanager::getPublicKeyPath() . '/' . self::TEST_ENCRYPTION_SHARE_USER3 . '.publicKey_backup'); - - // re-enable the file proxy - \OC_FileProxy::$enabled = $proxyStatus; - - // share the file - try { - \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, self::TEST_ENCRYPTION_SHARE_GROUP1, \OCP\Constants::PERMISSION_ALL); - } catch (\Exception $e) { - $this->assertEquals(0, strpos($e->getMessage(), "Following users are not set up for encryption")); - } - - - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // check if share key for user1 not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER3 . '.shareKey')); - - // disable encryption proxy to prevent recursive calls - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = false; - - // break user1 public key - $this->view->rename( - \OCA\Files_Encryption\Keymanager::getPublicKeyPath() . '/' . self::TEST_ENCRYPTION_SHARE_USER3 . '.publicKey_backup', - \OCA\Files_Encryption\Keymanager::getPublicKeyPath() . '/' . self::TEST_ENCRYPTION_SHARE_USER3 . '.publicKey'); - - // remove share file - $this->view->unlink('/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER3 - . '.shareKey'); - - // re-enable the file proxy - \OC_FileProxy::$enabled = $proxyStatus; - - // unshare the file with user1 - \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, self::TEST_ENCRYPTION_SHARE_GROUP1); - - // check if share key not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER3 . '.shareKey')); - - // cleanup - $this->view->chroot('/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/'); - $this->view->unlink($this->filename); - $this->view->chroot('/'); - } - - - /** - * test rename a shared file mount point - */ - function testRename() { - - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // save file with content - $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort); - - // test that data was successfully written - $this->assertInternalType('int', $cryptedFile); - - // get the file info from previous created file - $fileInfo = $this->view->getFileInfo( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); - - // check if we have a valid file info - $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); - - // share the file - \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER2, \OCP\Constants::PERMISSION_ALL); - - // check if share key for user1 and user2 exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); - - - // login as user2 - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER2); - - $this->assertTrue($this->view->file_exists('/' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files/' . $this->filename)); - - // get file contents - $retrievedCryptedFile = $this->view->file_get_contents( - '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files/' . $this->filename); - - // check if data is the same as we previously written - $this->assertEquals($this->dataShort, $retrievedCryptedFile); - - \OC\Files\Filesystem::mkdir($this->folder1); - - // move the file to a subfolder - \OC\Files\Filesystem::rename($this->filename, $this->folder1 . $this->filename); - - // check if we can read the moved file - $retrievedRenamedFile = $this->view->file_get_contents( - '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '/files/' . $this->folder1 . $this->filename); - - // check if data is the same as we previously written - $this->assertEquals($this->dataShort, $retrievedRenamedFile); - - // check if share key for user2 and user1 still exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); - - // cleanup - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - $this->view->unlink('/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); - } - - function testRenameGroupShare() { - // login as admin - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // save file with content - $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort); - - // test that data was successfully written - $this->assertInternalType('int', $cryptedFile); - - // get the file info from previous created file - $fileInfo = $this->view->getFileInfo( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); - - // check if we have a valid file info - $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); - - // share the file - \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, self::TEST_ENCRYPTION_SHARE_GROUP1, \OCP\Constants::PERMISSION_ALL); - - // check if share key for user1, user3 and user4 exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER3 . '.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER4 . '.shareKey')); - - - // login as user2 - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER3); - - $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename)); - - // get file contents - $retrievedCryptedFile = \OC\Files\Filesystem::file_get_contents($this->filename); - - // check if data is the same as we previously written - $this->assertEquals($this->dataShort, $retrievedCryptedFile); - - \OC\Files\Filesystem::mkdir($this->folder1); - - // move the file to a subfolder - \OC\Files\Filesystem::rename($this->filename, $this->folder1 . $this->filename); - - // check if we can read the moved file - $retrievedRenamedFile = \OC\Files\Filesystem::file_get_contents($this->folder1 . $this->filename); - - // check if data is the same as we previously written - $this->assertEquals($this->dataShort, $retrievedRenamedFile); - - // check if share key for user1, user3 and user4 still exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER3 . '.shareKey')); - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' - . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER4 . '.shareKey')); - - // cleanup - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - \OC\Files\Filesystem::unlink($this->filename); - } - - /** - * test if additional share keys are added if we move a folder to a shared parent - * @medium - */ - function testMoveFolder() { - - $view = new \OC\Files\View('/' . self::TEST_ENCRYPTION_SHARE_USER1); - - $filename = '/tmp-' . $this->getUniqueID(); - $folder = '/folder' . $this->getUniqueID(); - - \OC\Files\Filesystem::mkdir($folder); - - // Save long data as encrypted file using stream wrapper - $cryptedFile = \OC\Files\Filesystem::file_put_contents($folder . $filename, $this->dataShort); - - // Test that data was successfully written - $this->assertInternalType('int', $cryptedFile); - - // Get file decrypted contents - $decrypt = \OC\Files\Filesystem::file_get_contents($folder . $filename); - - $this->assertEquals($this->dataShort, $decrypt); - - $newFolder = '/newfolder/subfolder' . $this->getUniqueID(); - \OC\Files\Filesystem::mkdir('/newfolder'); - - // get the file info from previous created file - $fileInfo = \OC\Files\Filesystem::getFileInfo('/newfolder'); - $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); - - // share the folder - \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER2, \OCP\Constants::PERMISSION_ALL); - - \OC\Files\Filesystem::rename($folder, $newFolder); - - // Get file decrypted contents - $newDecrypt = \OC\Files\Filesystem::file_get_contents($newFolder . $filename); - $this->assertEquals($this->dataShort, $newDecrypt); - - // check if additional share key for user2 exists - $this->assertTrue($view->file_exists('files_encryption/keys' . $newFolder . '/' . $filename . '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); - - // check that old keys were removed/moved properly - $this->assertFalse($view->file_exists('files_encryption/keys' . $folder . '/' . $filename . '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); - - // tear down - \OC\Files\Filesystem::unlink($newFolder); - \OC\Files\Filesystem::unlink('/newfolder'); - } - - function usersProvider() { - return array( - // test as owner - array(self::TEST_ENCRYPTION_SHARE_USER1), - // test as share receiver - array(self::TEST_ENCRYPTION_SHARE_USER2), - ); - } - - /** - * @dataProvider usersProvider - */ - function testMoveFileToFolder($userId) { - $view = new \OC\Files\View('/' . self::TEST_ENCRYPTION_SHARE_USER1); - - $filename = '/tmp-' . $this->getUniqueID(); - $folder = '/folder' . $this->getUniqueID(); - - \OC\Files\Filesystem::mkdir($folder); - - // Save long data as encrypted file using stream wrapper - $cryptedFile = \OC\Files\Filesystem::file_put_contents($folder . $filename, $this->dataShort); - - // Test that data was successfully written - $this->assertInternalType('int', $cryptedFile); - - // Get file decrypted contents - $decrypt = \OC\Files\Filesystem::file_get_contents($folder . $filename); - - $this->assertEquals($this->dataShort, $decrypt); - - $subFolder = $folder . '/subfolder' . $this->getUniqueID(); - \OC\Files\Filesystem::mkdir($subFolder); - - // get the file info from previous created file - $fileInfo = \OC\Files\Filesystem::getFileInfo($folder); - $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); - - // share the folder - \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER2, \OCP\Constants::PERMISSION_ALL); - - // check that the share keys exist - $this->assertTrue($view->file_exists('files_encryption/keys' . $folder . '/' . $filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); - $this->assertTrue($view->file_exists('files_encryption/keys' . $folder . '/' . $filename . '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); - - // move the file into the subfolder as the test user - self::loginHelper($userId); - \OC\Files\Filesystem::rename($folder . $filename, $subFolder . $filename); - self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); - - // Get file decrypted contents - $newDecrypt = \OC\Files\Filesystem::file_get_contents($subFolder . $filename); - $this->assertEquals($this->dataShort, $newDecrypt); - - // check if additional share key for user2 exists - $this->assertTrue($view->file_exists('files_encryption/keys' . $subFolder . '/' . $filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); - $this->assertTrue($view->file_exists('files_encryption/keys' . $subFolder . '/' . $filename . '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); - - // check that old keys were removed/moved properly - $this->assertFalse($view->file_exists('files_encryption/keys' . $folder . '/' . $filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); - $this->assertFalse($view->file_exists('files_encryption/keys' . $folder . '/' . $filename . '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); - - // tear down - \OC\Files\Filesystem::unlink($subFolder); - \OC\Files\Filesystem::unlink($folder); - } - -} diff --git a/apps/files_encryption/tests/stream.php b/apps/files_encryption/tests/stream.php deleted file mode 100644 index 34e4ee6a32a..00000000000 --- a/apps/files_encryption/tests/stream.php +++ /dev/null @@ -1,232 +0,0 @@ -<?php -/** - * @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> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * 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. - * - * This program 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ - -namespace OCA\Files_Encryption\Tests; - -/** - * Class Stream - * this class provide basic stream tests - */ -class Stream extends TestCase { - - const TEST_ENCRYPTION_STREAM_USER1 = "test-stream-user1"; - - public $userId; - public $pass; - /** - * @var \OC\Files\View - */ - public $view; - public $dataShort; - public $stateFilesTrashbin; - - public static function setUpBeforeClass() { - parent::setUpBeforeClass(); - - // create test user - self::loginHelper(self::TEST_ENCRYPTION_STREAM_USER1, true); - } - - protected function setUp() { - parent::setUp(); - - // set user id - \OC_User::setUserId(self::TEST_ENCRYPTION_STREAM_USER1); - $this->userId = self::TEST_ENCRYPTION_STREAM_USER1; - $this->pass = self::TEST_ENCRYPTION_STREAM_USER1; - - // init filesystem view - $this->view = new \OC\Files\View('/'); - - // init short data - $this->dataShort = 'hats'; - - // remember files_trashbin state - $this->stateFilesTrashbin = \OC_App::isEnabled('files_trashbin'); - - // we don't want to tests with app files_trashbin enabled - \OC_App::disable('files_trashbin'); - } - - protected function tearDown() { - // reset app files_trashbin - if ($this->stateFilesTrashbin) { - \OC_App::enable('files_trashbin'); - } - else { - \OC_App::disable('files_trashbin'); - } - - parent::tearDown(); - } - - public static function tearDownAfterClass() { - // cleanup test user - \OC_User::deleteUser(self::TEST_ENCRYPTION_STREAM_USER1); - - parent::tearDownAfterClass(); - } - - function testStreamOptions() { - $filename = '/tmp-' . $this->getUniqueID(); - $view = new \OC\Files\View('/' . $this->userId . '/files'); - - // Save short data as encrypted file using stream wrapper - $cryptedFile = $view->file_put_contents($filename, $this->dataShort); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - $handle = $view->fopen($filename, 'r'); - - // check if stream is at position zero - $this->assertEquals(0, ftell($handle)); - - // set stream options - $this->assertTrue(flock($handle, LOCK_SH)); - $this->assertTrue(flock($handle, LOCK_UN)); - - fclose($handle); - - // tear down - $view->unlink($filename); - } - - function testStreamSetBlocking() { - $filename = '/tmp-' . $this->getUniqueID(); - $view = new \OC\Files\View('/' . $this->userId . '/files'); - - // Save short data as encrypted file using stream wrapper - $cryptedFile = $view->file_put_contents($filename, $this->dataShort); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - $handle = $view->fopen($filename, 'r'); - - - if (\OC_Util::runningOnWindows()) { - fclose($handle); - $view->unlink($filename); - $this->markTestSkipped('[Windows] stream_set_blocking() does not work as expected on Windows.'); - } - - // set stream options - $this->assertTrue(stream_set_blocking($handle, 1)); - - fclose($handle); - - // tear down - $view->unlink($filename); - } - - /** - * @medium - */ - function testStreamSetTimeout() { - $filename = '/tmp-' . $this->getUniqueID(); - $view = new \OC\Files\View('/' . $this->userId . '/files'); - - // Save short data as encrypted file using stream wrapper - $cryptedFile = $view->file_put_contents($filename, $this->dataShort); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - $handle = $view->fopen($filename, 'r'); - - // set stream options - $this->assertFalse(stream_set_timeout($handle, 1)); - - fclose($handle); - - // tear down - $view->unlink($filename); - } - - function testStreamSetWriteBuffer() { - $filename = '/tmp-' . $this->getUniqueID(); - $view = new \OC\Files\View('/' . $this->userId . '/files'); - - // Save short data as encrypted file using stream wrapper - $cryptedFile = $view->file_put_contents($filename, $this->dataShort); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - $handle = $view->fopen($filename, 'r'); - - // set stream options - $this->assertEquals(0, stream_set_write_buffer($handle, 1024)); - - fclose($handle); - - // tear down - $view->unlink($filename); - } - - /** - * @medium - * test if stream wrapper can read files outside from the data folder - */ - function testStreamFromLocalFile() { - - $filename = '/' . $this->userId . '/files/' . 'tmp-' . $this->getUniqueID().'.txt'; - - $tmpFilename = "/tmp/" . $this->getUniqueID() . ".txt"; - - // write an encrypted file - $cryptedFile = $this->view->file_put_contents($filename, $this->dataShort); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - // create a copy outside of the data folder in /tmp - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = false; - $encryptedContent = $this->view->file_get_contents($filename); - \OC_FileProxy::$enabled = $proxyStatus; - - file_put_contents($tmpFilename, $encryptedContent); - - \OCA\Files_Encryption\Helper::addTmpFileToMapper($tmpFilename, $filename); - - // try to read the file from /tmp - $handle = fopen("crypt://".$tmpFilename, "r"); - $contentFromTmpFile = stream_get_contents($handle); - - // check if it was successful - $this->assertEquals($this->dataShort, $contentFromTmpFile); - - fclose($handle); - - // clean up - unlink($tmpFilename); - $this->view->unlink($filename); - - } -} diff --git a/apps/files_encryption/tests/testcase.php b/apps/files_encryption/tests/testcase.php deleted file mode 100644 index c03147fabcd..00000000000 --- a/apps/files_encryption/tests/testcase.php +++ /dev/null @@ -1,111 +0,0 @@ -<?php -/** - * @author Björn Schießle <schiessle@owncloud.com> - * @author Joas Schilling <nickvergessen@owncloud.com> - * @author Morris Jobke <hey@morrisjobke.de> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * 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. - * - * This program 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ - -namespace OCA\Files_Encryption\Tests; - -/** - * Class TestCase - */ -abstract class TestCase extends \Test\TestCase { - - /** - * @param string $user - * @param bool $create - * @param bool $password - */ - public static function loginHelper($user, $create = false, $password = false, $loadEncryption = true) { - if ($create) { - try { - \OC_User::createUser($user, $user); - } catch (\Exception $e) { - // catch username is already being used from previous aborted runs - } - } - - if ($password === false) { - $password = $user; - } - - \OC_Util::tearDownFS(); - \OC_User::setUserId(''); - \OC\Files\Filesystem::tearDown(); - \OC::$server->getUserSession()->setUser(new \OC\User\User($user, new \OC_User_Database())); - \OC_Util::setupFS($user); - - if ($loadEncryption) { - $params['uid'] = $user; - $params['password'] = $password; - \OCA\Files_Encryption\Hooks::login($params); - } - } - - public static function logoutHelper() { - \OC_Util::tearDownFS(); - \OC_User::setUserId(false); - \OC\Files\Filesystem::tearDown(); - } - - public static function setUpBeforeClass() { - parent::setUpBeforeClass(); - - // reset backend - \OC_User::clearBackends(); - \OC_User::useBackend('database'); - - \OCA\Files_Encryption\Helper::registerFilesystemHooks(); - \OCA\Files_Encryption\Helper::registerUserHooks(); - \OCA\Files_Encryption\Helper::registerShareHooks(); - - \OC::registerShareHooks(); - \OCP\Util::connectHook('OC_Filesystem', 'setup', '\OC\Files\Storage\Shared', 'setup'); - - // clear and register hooks - \OC_FileProxy::clearProxies(); - \OC_FileProxy::register(new \OCA\Files_Encryption\Proxy()); - } - - public static function tearDownAfterClass() { - \OC_Hook::clear(); - \OC_FileProxy::clearProxies(); - - // Delete keys in /data/ - $view = new \OC\Files\View('/'); - $view->deleteAll('files_encryption'); - - parent::tearDownAfterClass(); - } - - protected function tearDown() { - parent::tearDown(); - $this->resetKeyCache(); - } - - protected function resetKeyCache() { - // reset key cache for every testrun - $keyCache = new \ReflectionProperty('\OCA\Files_Encryption\Keymanager', 'key_cache'); - $keyCache->setAccessible(true); - $keyCache->setValue(array()); - $keyCache->setAccessible(false); - } - -} diff --git a/apps/files_encryption/tests/trashbin.php b/apps/files_encryption/tests/trashbin.php deleted file mode 100755 index 84785738bfc..00000000000 --- a/apps/files_encryption/tests/trashbin.php +++ /dev/null @@ -1,346 +0,0 @@ -<?php -/** - * @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> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * 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. - * - * This program 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ - -namespace OCA\Files_Encryption\Tests; - -/** - * Class Trashbin - * this class provide basic trashbin app tests - */ -class Trashbin extends TestCase { - - const TEST_ENCRYPTION_TRASHBIN_USER1 = "test-trashbin-user1"; - - public $userId; - public $pass; - /** - * @var \OC\Files\View - */ - 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(); - - // 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; - - // init filesystem view - $this->view = new \OC\Files\View('/'); - - // init short data - $this->dataShort = 'hats'; - - $this->folder1 = '/folder1'; - $this->subfolder = '/subfolder1'; - $this->subsubfolder = '/subsubfolder1'; - - // remember files_trashbin state - $this->stateFilesTrashbin = \OC_App::isEnabled('files_trashbin'); - - // we want to tests with app files_trashbin enabled - \OC_App::enable('files_trashbin'); - } - - protected function tearDown() { - // reset app files_trashbin - if ($this->stateFilesTrashbin) { - \OC_App::enable('files_trashbin'); - } - else { - \OC_App::disable('files_trashbin'); - } - - 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(); - } - - /** - * @medium - * test delete file - */ - function testDeleteFile() { - - // generate filename - $filename = 'tmp-' . $this->getUniqueID() . '.txt'; - $filename2 = $filename . '.backup'; // a second file with similar name - - // 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); - - // test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - $this->assertTrue(is_int($cryptedFile2)); - - // 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')); - - // 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')); - - // delete first file - \OC\Files\Filesystem::unlink($filename); - - // check if file not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files/' . $filename)); - - // check if key for admin not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/' . $filename . '/fileKey')); - - // 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')); - - // 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')); - - // get files - $trashFiles = \OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_ENCRYPTION_TRASHBIN_USER1); - - // find created file with timestamp - $timestamp = null; - foreach ($trashFiles as $file) { - if ($file['name'] === $filename) { - $timestamp = $file['mtime']; - break; - } - } - - // check if we found the file we created - $this->assertNotNull($timestamp); - - $this->assertTrue($this->view->is_dir('/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keys/' . $filename . '.d' . $timestamp)); - - // check if key for admin not exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keys/' . $filename . '.d' . $timestamp . '/fileKey')); - - // check if share key for admin not exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keys/' . $filename - . '.d' . $timestamp . '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey')); - } - - /** - * @medium - * test restore file - */ - function testRestoreFile() { - // generate filename - $filename = 'tmp-' . $this->getUniqueID() . '.txt'; - $filename2 = $filename . '.backup'; // a second file with similar name - - // save file with content - 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); - - $trashFiles = \OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_ENCRYPTION_TRASHBIN_USER1); - - // find created file with timestamp - $timestamp = null; - foreach ($trashFiles as $file) { - if ($file['name'] === $filename) { - $timestamp = $file['mtime']; - break; - } - } - - // make sure that we have a timestamp - $this->assertNotNull($timestamp); - - // 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 - $this->assertTrue(\OCA\Files_Trashbin\Trashbin::restore($filename . '.d' . $timestamp, $filename, $timestamp)); - - // check if file exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files/' . $filename)); - - // check if key for admin exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/' . $filename . '/fileKey')); - - // 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')); - } - - /** - * @medium - * test delete file forever - */ - function testPermanentDeleteFile() { - - // generate filename - $filename = 'tmp-' . $this->getUniqueID() . '.txt'; - - // save file with content - $cryptedFile = file_put_contents('crypt:///' .$this->userId. '/files/' . $filename, $this->dataShort); - - // test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - // check if key for admin exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/' . $filename . '/fileKey')); - - // 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')); - - // delete file - \OC\Files\Filesystem::unlink($filename); - - // check if file not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files/' . $filename)); - - // check if key for admin not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keys/' . $filename . '/' - . $filename . '.key')); - - // 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')); - - // find created file with timestamp - $query = \OC_DB::prepare('SELECT `timestamp`,`type` FROM `*PREFIX*files_trash`' - . ' WHERE `id`=?'); - $result = $query->execute(array($filename))->fetchRow(); - - $this->assertTrue(is_array($result)); - - // build suffix - $trashFileSuffix = 'd' . $result['timestamp']; - - // check if key for admin exists - $this->assertTrue($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keys/' . $filename - . '.' . $trashFileSuffix . '/fileKey')); - - // 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')); - - // get timestamp from file - $timestamp = str_replace('d', '', $trashFileSuffix); - - // delete file forever - $this->assertGreaterThan(0, \OCA\Files_Trashbin\Trashbin::delete($filename, $this->userId, $timestamp)); - - // check if key for admin not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/files/' . $filename . '.' - . $trashFileSuffix)); - - // check if key for admin not exists - $this->assertFalse($this->view->file_exists( - '/' . self::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keys/' . $filename - . '.' . $trashFileSuffix . '/fileKey')); - - // 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')); - } - -} diff --git a/apps/files_encryption/tests/util.php b/apps/files_encryption/tests/util.php deleted file mode 100755 index 4174a0da0da..00000000000 --- a/apps/files_encryption/tests/util.php +++ /dev/null @@ -1,693 +0,0 @@ -<?php -/** - * @author Andreas Fischer <bantu@owncloud.com> - * @author Björn Schießle <schiessle@owncloud.com> - * @author Florin Peter <github@florin-peter.de> - * @author Joas Schilling <nickvergessen@owncloud.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Markus Goetz <markus@woboq.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <icewind@owncloud.com> - * @author Robin McCorkell <rmccorkell@karoshi.org.uk> - * @author Sam Tuke <mail@samtuke.com> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * 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. - * - * This program 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ - -namespace OCA\Files_Encryption\Tests; - -/** - * Class Util - */ -class Util extends TestCase { - - const TEST_ENCRYPTION_UTIL_USER1 = "test-util-user1"; - const TEST_ENCRYPTION_UTIL_USER2 = "test-util-user2"; - const TEST_ENCRYPTION_UTIL_GROUP1 = "test-util-group1"; - const TEST_ENCRYPTION_UTIL_GROUP2 = "test-util-group2"; - const TEST_ENCRYPTION_UTIL_LEGACY_USER = "test-legacy-user"; - - public $userId; - public $encryptionDir; - public $publicKeyDir; - public $pass; - /** - * @var \OC\Files\View - */ - public $view; - public $keysPath; - public $publicKeyPath; - public $privateKeyPath; - /** - * @var \OCA\Files_Encryption\Util - */ - public $util; - public $dataShort; - public $legacyEncryptedData; - public $legacyEncryptedDataKey; - public $legacyKey; - public $stateFilesTrashbin; - - public static function setUpBeforeClass() { - parent::setUpBeforeClass(); - - // create test user - self::loginHelper(self::TEST_ENCRYPTION_UTIL_USER1, true); - self::loginHelper(self::TEST_ENCRYPTION_UTIL_USER2, true); - self::loginHelper(self::TEST_ENCRYPTION_UTIL_LEGACY_USER, true); - - // create groups - \OC_Group::createGroup(self::TEST_ENCRYPTION_UTIL_GROUP1); - \OC_Group::createGroup(self::TEST_ENCRYPTION_UTIL_GROUP2); - - // add user 1 to group1 - \OC_Group::addToGroup(self::TEST_ENCRYPTION_UTIL_USER1, self::TEST_ENCRYPTION_UTIL_GROUP1); - } - - protected function setUp() { - parent::setUp(); - - // login user - self::loginHelper(self::TEST_ENCRYPTION_UTIL_USER1); - \OC_User::setUserId(self::TEST_ENCRYPTION_UTIL_USER1); - $this->userId = self::TEST_ENCRYPTION_UTIL_USER1; - $this->pass = self::TEST_ENCRYPTION_UTIL_USER1; - - // set content for encrypting / decrypting in tests - $this->dataUrl = __DIR__ . '/../lib/crypt.php'; - $this->dataShort = 'hats'; - $this->dataLong = file_get_contents(__DIR__ . '/../lib/crypt.php'); - $this->legacyData = __DIR__ . '/legacy-text.txt'; - $this->legacyEncryptedData = __DIR__ . '/legacy-encrypted-text.txt'; - $this->legacyEncryptedDataKey = __DIR__ . '/encryption.key'; - $this->legacyKey = "30943623843030686906\0\0\0\0"; - - $keypair = \OCA\Files_Encryption\Crypt::createKeypair(); - - $this->genPublicKey = $keypair['publicKey']; - $this->genPrivateKey = $keypair['privateKey']; - - $this->publicKeyDir = \OCA\Files_Encryption\Keymanager::getPublicKeyPath(); - $this->encryptionDir = '/' . $this->userId . '/' . 'files_encryption'; - $this->keysPath = $this->encryptionDir . '/' . 'keys'; - $this->publicKeyPath = - $this->publicKeyDir . '/' . $this->userId . '.publicKey'; // e.g. data/public-keys/admin.publicKey - $this->privateKeyPath = - $this->encryptionDir . '/' . $this->userId . '.privateKey'; // e.g. data/admin/admin.privateKey - - $this->view = new \OC\Files\View('/'); - - $this->util = new \OCA\Files_Encryption\Util($this->view, $this->userId); - - // remember files_trashbin state - $this->stateFilesTrashbin = \OC_App::isEnabled('files_trashbin'); - - // we don't want to tests with app files_trashbin enabled - \OC_App::disable('files_trashbin'); - } - - protected function tearDown() { - // reset app files_trashbin - if ($this->stateFilesTrashbin) { - \OC_App::enable('files_trashbin'); - } - else { - \OC_App::disable('files_trashbin'); - } - - parent::tearDown(); - } - - public static function tearDownAfterClass() { - // cleanup test user - \OC_User::deleteUser(self::TEST_ENCRYPTION_UTIL_USER1); - \OC_User::deleteUser(self::TEST_ENCRYPTION_UTIL_USER2); - \OC_User::deleteUser(self::TEST_ENCRYPTION_UTIL_LEGACY_USER); - - //cleanup groups - \OC_Group::deleteGroup(self::TEST_ENCRYPTION_UTIL_GROUP1); - \OC_Group::deleteGroup(self::TEST_ENCRYPTION_UTIL_GROUP2); - - parent::tearDownAfterClass(); - } - - /** - * @medium - * test that paths set during User construction are correct - */ - function testKeyPaths() { - $util = new \OCA\Files_Encryption\Util($this->view, $this->userId); - - $this->assertEquals($this->publicKeyDir, $util->getPath('publicKeyDir')); - $this->assertEquals($this->encryptionDir, $util->getPath('encryptionDir')); - $this->assertEquals($this->keysPath, $util->getPath('keysPath')); - $this->assertEquals($this->publicKeyPath, $util->getPath('publicKeyPath')); - $this->assertEquals($this->privateKeyPath, $util->getPath('privateKeyPath')); - - } - - /** - * @medium - * test detection of encrypted files - */ - function testIsEncryptedPath() { - - $util = new \OCA\Files_Encryption\Util($this->view, $this->userId); - - self::loginHelper($this->userId); - - $unencryptedFile = '/tmpUnencrypted-' . $this->getUniqueID() . '.txt'; - $encryptedFile = '/tmpEncrypted-' . $this->getUniqueID() . '.txt'; - - // Disable encryption proxy to write a unencrypted file - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = false; - - $this->view->file_put_contents($this->userId . '/files/' . $unencryptedFile, $this->dataShort); - - // Re-enable proxy - our work is done - \OC_FileProxy::$enabled = $proxyStatus; - - // write a encrypted file - $this->view->file_put_contents($this->userId . '/files/' . $encryptedFile, $this->dataShort); - - // test if both files are detected correctly - $this->assertFalse($util->isEncryptedPath($this->userId . '/files/' . $unencryptedFile)); - $this->assertTrue($util->isEncryptedPath($this->userId . '/files/' . $encryptedFile)); - - // cleanup - $this->view->unlink($this->userId . '/files/' . $unencryptedFile); - $this->view->unlink($this->userId . '/files/' . $encryptedFile); - - } - - /** - * @medium - * test setup of encryption directories - */ - function testSetupServerSide() { - $this->assertEquals(true, $this->util->setupServerSide($this->pass)); - } - - /** - * @medium - * test checking whether account is ready for encryption, - */ - function testUserIsReady() { - $this->assertEquals(true, $this->util->ready()); - } - - /** - * test checking whether account is not ready for encryption, - */ -// function testUserIsNotReady() { -// $this->view->unlink($this->publicKeyDir); -// -// $params['uid'] = $this->userId; -// $params['password'] = $this->pass; -// $this->assertFalse(OCA\Files_Encryption\Hooks::login($params)); -// -// $this->view->unlink($this->privateKeyPath); -// } - - /** - * @medium - */ - function testRecoveryEnabledForUser() { - - $util = new \OCA\Files_Encryption\Util($this->view, $this->userId); - - // Record the value so we can return it to it's original state later - $enabled = $util->recoveryEnabledForUser(); - - $this->assertTrue($util->setRecoveryForUser(!$enabled)); - - $this->assertEquals(!$enabled, $util->recoveryEnabledForUser()); - - $this->assertTrue($util->setRecoveryForUser($enabled)); - - $this->assertEquals($enabled, $util->recoveryEnabledForUser()); - - - } - - /** - * @medium - */ - function testGetUidAndFilename() { - - \OC_User::setUserId(self::TEST_ENCRYPTION_UTIL_USER1); - - $filename = '/tmp-' . $this->getUniqueID() . '.test'; - - // Disable encryption proxy to prevent recursive calls - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = false; - - $this->view->file_put_contents($this->userId . '/files/' . $filename, $this->dataShort); - - // Re-enable proxy - our work is done - \OC_FileProxy::$enabled = $proxyStatus; - - $util = new \OCA\Files_Encryption\Util($this->view, $this->userId); - - list($fileOwnerUid, $file) = $util->getUidAndFilename($filename); - - $this->assertEquals(self::TEST_ENCRYPTION_UTIL_USER1, $fileOwnerUid); - - $this->assertEquals($file, $filename); - - $this->view->unlink($this->userId . '/files/' . $filename); - } - - /** - * Test that data that is read by the crypto stream wrapper - */ - function testGetFileSize() { - self::loginHelper(self::TEST_ENCRYPTION_UTIL_USER1); - - $filename = 'tmp-' . $this->getUniqueID(); - $externalFilename = '/' . $this->userId . '/files/' . $filename; - - // Test for 0 byte files - $problematicFileSizeData = ""; - $cryptedFile = $this->view->file_put_contents($externalFilename, $problematicFileSizeData); - $this->assertTrue(is_int($cryptedFile)); - $this->assertEquals($this->util->getFileSize($externalFilename), 0); - $decrypt = $this->view->file_get_contents($externalFilename); - $this->assertEquals($problematicFileSizeData, $decrypt); - $this->view->unlink($this->userId . '/files/' . $filename); - - // Test a file with 18377 bytes as in https://github.com/owncloud/mirall/issues/1009 - $problematicFileSizeData = str_pad("", 18377, "abc"); - $cryptedFile = $this->view->file_put_contents($externalFilename, $problematicFileSizeData); - $this->assertTrue(is_int($cryptedFile)); - $this->assertEquals($this->util->getFileSize($externalFilename), 18377); - $decrypt = $this->view->file_get_contents($externalFilename); - $this->assertEquals($problematicFileSizeData, $decrypt); - $this->view->unlink($this->userId . '/files/' . $filename); - } - - function testEncryptAll() { - - $filename = "/encryptAll" . $this->getUniqueID() . ".txt"; - $util = new \OCA\Files_Encryption\Util($this->view, $this->userId); - - // disable encryption to upload a unencrypted file - \OC_App::disable('files_encryption'); - - $this->view->file_put_contents($this->userId . '/files/' . $filename, $this->dataShort); - - $fileInfoUnencrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename); - - $this->assertTrue($fileInfoUnencrypted instanceof \OC\Files\FileInfo); - - // enable file encryption again - \OC_App::enable('files_encryption'); - - // encrypt all unencrypted files - $util->encryptAll('/' . $this->userId . '/' . 'files'); - - $fileInfoEncrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename); - - $this->assertTrue($fileInfoEncrypted instanceof \OC\Files\FileInfo); - - // check if mtime and etags unchanged - $this->assertEquals($fileInfoEncrypted['mtime'], $fileInfoUnencrypted['mtime']); - $this->assertSame($fileInfoEncrypted['etag'], $fileInfoUnencrypted['etag']); - - $this->view->unlink($this->userId . '/files/' . $filename); - } - - function testDecryptAll() { - - $filename = "/decryptAll" . $this->getUniqueID() . ".txt"; - $datadir = \OC_Config::getValue('datadirectory', \OC::$SERVERROOT . '/data/'); - $userdir = $datadir . '/' . $this->userId . '/files/'; - - $this->view->file_put_contents($this->userId . '/files/' . $filename, $this->dataShort); - - $fileInfoEncrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename); - - $this->assertTrue($fileInfoEncrypted instanceof \OC\Files\FileInfo); - $this->assertEquals($fileInfoEncrypted['encrypted'], 1); - - $encContent = file_get_contents($userdir . $filename); - - \OC_App::disable('files_encryption'); - - $user = \OCP\User::getUser(); - $this->logoutHelper(); - $this->loginHelper($user, false, false, false); - - $content = file_get_contents($userdir . $filename); - - //content should be encrypted - $this->assertSame($encContent, $content); - - // now we load the encryption app again - \OC_App::loadApp('files_encryption'); - - // init encryption app - $params = array('uid' => \OCP\User::getUser(), - 'password' => \OCP\User::getUser()); - - $view = new \OC\Files\View('/'); - $util = new \OCA\Files_Encryption\Util($view, \OCP\User::getUser()); - - $result = $util->initEncryption($params); - - $this->assertTrue($result instanceof \OCA\Files_Encryption\Session); - - $successful = $util->decryptAll(); - - $this->assertTrue($successful); - - $this->logoutHelper(); - $this->loginHelper($user, false, false, false); - - // file should be unencrypted and fileInfo should contain the correct values - $content = file_get_contents($userdir . $filename); - - // now we should get the plain data - $this->assertSame($this->dataShort, $content); - - $fileInfoUnencrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename); - $this->assertTrue($fileInfoUnencrypted instanceof \OC\Files\FileInfo); - - // check if mtime and etags unchanged - $this->assertEquals($fileInfoEncrypted['mtime'], $fileInfoUnencrypted['mtime']); - $this->assertSame($fileInfoEncrypted['etag'], $fileInfoUnencrypted['etag']); - // file should no longer be encrypted - $this->assertEquals(0, $fileInfoUnencrypted['encrypted']); - - $backupPath = $this->getBackupPath('decryptAll'); - - // check if the keys where moved to the backup location - $this->assertTrue($this->view->is_dir($backupPath . '/keys')); - $this->assertTrue($this->view->file_exists($backupPath . '/keys/' . $filename . '/fileKey')); - $this->assertTrue($this->view->file_exists($backupPath . '/keys/' . $filename . '/' . $user . '.shareKey')); - - // cleanup - $this->view->unlink($this->userId . '/files/' . $filename); - $this->view->deleteAll($backupPath); - \OC_App::enable('files_encryption'); - - } - - private function createDummyKeysForBackupTest() { - // create some dummy key files - $encPath = '/' . self::TEST_ENCRYPTION_UTIL_USER1 . '/files_encryption'; - $this->view->mkdir($encPath . '/keys/foo'); - $this->view->file_put_contents($encPath . '/keys/foo/fileKey', 'key'); - $this->view->file_put_contents($encPath . '/keys/foo/user1.shareKey', 'share key'); - } - - /** - * test if all keys get moved to the backup folder correctly - * - * @dataProvider dataBackupAllKeys - */ - function testBackupAllKeys($addTimestamp, $includeUserKeys) { - self::loginHelper(self::TEST_ENCRYPTION_UTIL_USER1); - - $this->createDummyKeysForBackupTest(); - - $util = new \OCA\Files_Encryption\Util($this->view, self::TEST_ENCRYPTION_UTIL_USER1); - - $util->backupAllKeys('testBackupAllKeys', $addTimestamp, $includeUserKeys); - - $backupPath = $this->getBackupPath('testBackupAllKeys'); - - // check backupDir Content - $this->assertTrue($this->view->is_dir($backupPath . '/keys')); - $this->assertTrue($this->view->is_dir($backupPath . '/keys/foo')); - $this->assertTrue($this->view->file_exists($backupPath . '/keys/foo/fileKey')); - $this->assertTrue($this->view->file_exists($backupPath . '/keys/foo/user1.shareKey')); - - if ($includeUserKeys) { - $this->assertTrue($this->view->file_exists($backupPath . '/' . self::TEST_ENCRYPTION_UTIL_USER1 . '.privateKey')); - $this->assertTrue($this->view->file_exists($backupPath . '/' . self::TEST_ENCRYPTION_UTIL_USER1 . '.publicKey')); - } else { - $this->assertFalse($this->view->file_exists($backupPath . '/' . self::TEST_ENCRYPTION_UTIL_USER1 . '.privateKey')); - $this->assertFalse($this->view->file_exists($backupPath . '/' . self::TEST_ENCRYPTION_UTIL_USER1 . '.publicKey')); - } - - //cleanup - $this->view->deleteAll($backupPath); - $this->view->unlink($this->encryptionDir . '/keys/foo/fileKey'); - $this->view->unlink($this->encryptionDir . '/keys/foo/user1.shareKey'); - } - - function dataBackupAllKeys() { - return array( - array(true, true), - array(false, true), - array(true, false), - array(false, false), - ); - } - - - /** - * @dataProvider dataBackupAllKeys - */ - function testRestoreBackup($addTimestamp, $includeUserKeys) { - - $util = new \OCA\Files_Encryption\Util($this->view, self::TEST_ENCRYPTION_UTIL_USER1); - $this->createDummyKeysForBackupTest(); - - $util->backupAllKeys('restoreKeysBackupTest', $addTimestamp, $includeUserKeys); - $this->view->deleteAll($this->keysPath); - if ($includeUserKeys) { - $this->view->unlink($this->privateKeyPath); - $this->view->unlink($this->publicKeyPath); - } - - // key should be removed after backup was created - $this->assertFalse($this->view->is_dir($this->keysPath)); - if ($includeUserKeys) { - $this->assertFalse($this->view->file_exists($this->privateKeyPath)); - $this->assertFalse($this->view->file_exists($this->publicKeyPath)); - } - - $backupPath = $this->getBackupPath('restoreKeysBackupTest'); - $backupName = substr(basename($backupPath), strlen('backup.')); - - $this->assertTrue($util->restoreBackup($backupName)); - - // check if all keys are restored - $this->assertFalse($this->view->is_dir($backupPath)); - $this->assertTrue($this->view->is_dir($this->keysPath)); - $this->assertTrue($this->view->is_dir($this->keysPath . '/foo')); - $this->assertTrue($this->view->file_exists($this->keysPath . '/foo/fileKey')); - $this->assertTrue($this->view->file_exists($this->keysPath . '/foo/user1.shareKey')); - $this->assertTrue($this->view->file_exists($this->privateKeyPath)); - $this->assertTrue($this->view->file_exists($this->publicKeyPath)); - } - - function testDeleteBackup() { - $util = new \OCA\Files_Encryption\Util($this->view, self::TEST_ENCRYPTION_UTIL_USER1); - $this->createDummyKeysForBackupTest(); - - $util->backupAllKeys('testDeleteBackup', false, false); - - $this->assertTrue($this->view->is_dir($this->encryptionDir . '/backup.testDeleteBackup')); - - $util->deleteBackup('testDeleteBackup'); - - $this->assertFalse($this->view->is_dir($this->encryptionDir . '/backup.testDeleteBackup')); - } - - function testDescryptAllWithBrokenFiles() { - - $file1 = "/decryptAll1" . $this->getUniqueID() . ".txt"; - $file2 = "/decryptAll2" . $this->getUniqueID() . ".txt"; - - $util = new \OCA\Files_Encryption\Util($this->view, $this->userId); - - $this->view->file_put_contents($this->userId . '/files/' . $file1, $this->dataShort); - $this->view->file_put_contents($this->userId . '/files/' . $file2, $this->dataShort); - - $fileInfoEncrypted1 = $this->view->getFileInfo($this->userId . '/files/' . $file1); - $fileInfoEncrypted2 = $this->view->getFileInfo($this->userId . '/files/' . $file2); - - $this->assertTrue($fileInfoEncrypted1 instanceof \OC\Files\FileInfo); - $this->assertTrue($fileInfoEncrypted2 instanceof \OC\Files\FileInfo); - $this->assertEquals($fileInfoEncrypted1['encrypted'], 1); - $this->assertEquals($fileInfoEncrypted2['encrypted'], 1); - - // rename keyfile for file1 so that the decryption for file1 fails - // Expected behaviour: decryptAll() returns false, file2 gets decrypted anyway - $this->view->rename($this->userId . '/files_encryption/keys/' . $file1 . '/fileKey', - $this->userId . '/files_encryption/keys/' . $file1 . '/fileKey.moved'); - - // need to reset key cache that we don't use the cached key - $this->resetKeyCache(); - - // decrypt all encrypted files - $result = $util->decryptAll(); - - $this->assertFalse($result); - - $fileInfoUnencrypted1 = $this->view->getFileInfo($this->userId . '/files/' . $file1); - $fileInfoUnencrypted2 = $this->view->getFileInfo($this->userId . '/files/' . $file2); - - $this->assertTrue($fileInfoUnencrypted1 instanceof \OC\Files\FileInfo); - $this->assertTrue($fileInfoUnencrypted2 instanceof \OC\Files\FileInfo); - - // file1 should be still encrypted; file2 should be decrypted - $this->assertEquals(1, $fileInfoUnencrypted1['encrypted']); - $this->assertEquals(0, $fileInfoUnencrypted2['encrypted']); - - // keyfiles and share keys should still exist - $this->assertTrue($this->view->is_dir($this->userId . '/files_encryption/keys/')); - $this->assertTrue($this->view->file_exists($this->userId . '/files_encryption/keys/' . $file1 . '/fileKey.moved')); - $this->assertTrue($this->view->file_exists($this->userId . '/files_encryption/keys/' . $file1 . '/' . $this->userId . '.shareKey')); - - // rename the keyfile for file1 back - $this->view->rename($this->userId . '/files_encryption/keys/' . $file1 . '/fileKey.moved', - $this->userId . '/files_encryption/keys/' . $file1 . '/fileKey'); - - // try again to decrypt all encrypted files - $result = $util->decryptAll(); - - $this->assertTrue($result); - - $fileInfoUnencrypted1 = $this->view->getFileInfo($this->userId . '/files/' . $file1); - $fileInfoUnencrypted2 = $this->view->getFileInfo($this->userId . '/files/' . $file2); - - $this->assertTrue($fileInfoUnencrypted1 instanceof \OC\Files\FileInfo); - $this->assertTrue($fileInfoUnencrypted2 instanceof \OC\Files\FileInfo); - - // now both files should be decrypted - $this->assertEquals(0, $fileInfoUnencrypted1['encrypted']); - $this->assertEquals(0, $fileInfoUnencrypted2['encrypted']); - - // keyfiles and share keys should be deleted - $this->assertFalse($this->view->is_dir($this->userId . '/files_encryption/keys/')); - - //cleanup - $backupPath = $this->getBackupPath('decryptAll'); - $this->view->unlink($this->userId . '/files/' . $file1); - $this->view->unlink($this->userId . '/files/' . $file2); - $this->view->deleteAll($backupPath); - - } - - function getBackupPath($extension) { - $encPath = '/' . self::TEST_ENCRYPTION_UTIL_USER1 . '/files_encryption'; - $encFolderContent = $this->view->getDirectoryContent($encPath); - - $backupPath = ''; - foreach ($encFolderContent as $c) { - $name = $c['name']; - if (substr($name, 0, strlen('backup.' . $extension)) === 'backup.' . $extension) { - $backupPath = $encPath . '/'. $c['name']; - break; - } - } - - return $backupPath; - } - - /** - * @dataProvider dataProviderFortestIsMountPointApplicableToUser - */ - function testIsMountPointApplicableToUser($mount, $expectedResult) { - self::loginHelper(self::TEST_ENCRYPTION_UTIL_USER1); - $dummyClass = new DummyUtilClass($this->view, self::TEST_ENCRYPTION_UTIL_USER1); - $result = $dummyClass->testIsMountPointApplicableToUser($mount); - - $this->assertSame($expectedResult, $result); - } - - function dataProviderFortestIsMountPointApplicableToUser() { - return array( - array(array('applicable' => array('groups' => array(), 'users' => array(self::TEST_ENCRYPTION_UTIL_USER1))), true), - array(array('applicable' => array('groups' => array(), 'users' => array(self::TEST_ENCRYPTION_UTIL_USER2))), false), - array(array('applicable' => array('groups' => array(self::TEST_ENCRYPTION_UTIL_GROUP1), 'users' => array())), true), - array(array('applicable' => array('groups' => array(self::TEST_ENCRYPTION_UTIL_GROUP1), 'users' => array(self::TEST_ENCRYPTION_UTIL_USER2))), true), - array(array('applicable' => array('groups' => array(self::TEST_ENCRYPTION_UTIL_GROUP2), 'users' => array(self::TEST_ENCRYPTION_UTIL_USER2))), false), - array(array('applicable' => array('groups' => array(self::TEST_ENCRYPTION_UTIL_GROUP2), 'users' => array(self::TEST_ENCRYPTION_UTIL_USER2, 'all'))), true), - array(array('applicable' => array('groups' => array(self::TEST_ENCRYPTION_UTIL_GROUP2), 'users' => array('all'))), true), - ); - } - - /** - * Tests that filterShareReadyUsers() returns the correct list of - * users that are ready or not ready for encryption - */ - public function testFilterShareReadyUsers() { - $appConfig = \OC::$server->getAppConfig(); - - $publicShareKeyId = $appConfig->getValue('files_encryption', 'publicShareKeyId'); - $recoveryKeyId = $appConfig->getValue('files_encryption', 'recoveryKeyId'); - - $usersToTest = array( - 'readyUser', - 'notReadyUser', - 'nonExistingUser', - $publicShareKeyId, - $recoveryKeyId, - ); - self::loginHelper('readyUser', true); - self::loginHelper('notReadyUser', true); - // delete encryption dir to make it not ready - $this->view->unlink('notReadyUser/files_encryption/'); - - // login as user1 - self::loginHelper(self::TEST_ENCRYPTION_UTIL_USER1); - - $result = $this->util->filterShareReadyUsers($usersToTest); - $this->assertEquals( - array('readyUser', $publicShareKeyId, $recoveryKeyId), - $result['ready'] - ); - $this->assertEquals( - array('notReadyUser', 'nonExistingUser'), - $result['unready'] - ); - \OC_User::deleteUser('readyUser'); - } - - /** - * helper function to set migration status to the right value - * to be able to test the migration path - * - * @param integer $status needed migration status for test - * @param string $user for which user the status should be set - * @return boolean - */ - private function setMigrationStatus($status, $user) { - \OC::$server->getConfig()->setUserValue($user, 'files_encryption', 'migration_status', (string)$status); - // the update will definitely be executed -> return value is always true - return true; - } - -} - -/** - * dummy class extends \OCA\Files_Encryption\Util to access protected methods for testing - */ -class DummyUtilClass extends \OCA\Files_Encryption\Util { - public function testIsMountPointApplicableToUser($mount) { - return $this->isMountPointApplicableToUser($mount); - } -} diff --git a/apps/files_encryption/tests/zeros b/apps/files_encryption/tests/zeros Binary files differdeleted file mode 100644 index ff982acf423..00000000000 --- a/apps/files_encryption/tests/zeros +++ /dev/null |