]> source.dussan.org Git - nextcloud-server.git/commitdiff
improved test
authorFlorin Peter <github@florin-peter.de>
Sun, 26 May 2013 01:22:16 +0000 (03:22 +0200)
committerFlorin Peter <github@florin-peter.de>
Mon, 27 May 2013 12:20:18 +0000 (14:20 +0200)
- fixed testPermanentDeleteFile sometimes failed
- speed optimization
- reformat code

apps/files_encryption/tests/crypt.php
apps/files_encryption/tests/keymanager.php
apps/files_encryption/tests/share.php
apps/files_encryption/tests/stream.php
apps/files_encryption/tests/trashbin.php
apps/files_encryption/tests/util.php
apps/files_encryption/tests/webdav.php

index 621941c52a10ec6e1dd8c78097ed0f3a100c32a2..b8fbdda400fe8750780db3d5ba413d4dbda2c3d2 100755 (executable)
@@ -7,15 +7,15 @@
  * See the COPYING-README file.
  */
 
-require_once realpath(dirname(__FILE__) . '/../3rdparty/Crypt_Blowfish/Blowfish.php');
-require_once realpath(dirname(__FILE__) . '/../../../lib/base.php');
-require_once realpath(dirname(__FILE__) . '/../lib/crypt.php');
-require_once realpath(dirname(__FILE__) . '/../lib/keymanager.php');
-require_once realpath(dirname(__FILE__) . '/../lib/proxy.php');
-require_once realpath(dirname(__FILE__) . '/../lib/stream.php');
-require_once realpath(dirname(__FILE__) . '/../lib/util.php');
-require_once realpath(dirname(__FILE__) . '/../lib/helper.php');
-require_once realpath(dirname(__FILE__) . '/../appinfo/app.php');
+require_once realpath( dirname( __FILE__ ) . '/../3rdparty/Crypt_Blowfish/Blowfish.php' );
+require_once realpath( dirname( __FILE__ ) . '/../../../lib/base.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/crypt.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/keymanager.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/proxy.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/stream.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/util.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/helper.php' );
+require_once realpath( dirname( __FILE__ ) . '/../appinfo/app.php' );
 
 use OCA\Encryption;
 
@@ -39,92 +39,95 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase
        public $genPrivateKey;
        public $genPublicKey;
 
-       function setUp()
-       {
+       public static function setUpBeforeClass() {
                // reset backend
                \OC_User::clearBackends();
-               \OC_User::useBackend('database');
+               \OC_User::useBackend( 'database' );
 
+               // Filesystem related hooks
+               \OCA\Encryption\Helper::registerFilesystemHooks();
+
+               // Filesystem related hooks
+               \OCA\Encryption\Helper::registerUserHooks();
+
+               // clear and register hooks
+               \OC_FileProxy::clearProxies();
+               \OC_FileProxy::register( new OCA\Encryption\Proxy() );
+
+               // setup filesystem
+               \OC_Util::tearDownFS();
+               \OC_User::setUserId( '' );
+               \OC\Files\Filesystem::tearDown();
+               \OC_Util::setupFS( 'admin' );
+               \OC_User::setUserId( 'admin' );
+
+               // login admin
+               $params['uid'] = 'admin';
+               $params['password'] = 'admin';
+               OCA\Encryption\Hooks::login( $params );
+       }
+
+       function setUp() {
                // set content for encrypting / decrypting in tests
-               $this->dataLong = file_get_contents(realpath(dirname(__FILE__) . '/../lib/crypt.php'));
+               $this->dataLong = file_get_contents( realpath( dirname( __FILE__ ) . '/../lib/crypt.php' ) );
                $this->dataShort = 'hats';
-               $this->dataUrl = realpath(dirname(__FILE__) . '/../lib/crypt.php');
-               $this->legacyData = realpath(dirname(__FILE__) . '/legacy-text.txt');
-               $this->legacyEncryptedData = realpath(dirname(__FILE__) . '/legacy-encrypted-text.txt');
-               $this->legacyEncryptedDataKey = realpath(dirname(__FILE__) . '/encryption.key');
+               $this->dataUrl = realpath( dirname( __FILE__ ) . '/../lib/crypt.php' );
+               $this->legacyData = realpath( dirname( __FILE__ ) . '/legacy-text.txt' );
+               $this->legacyEncryptedData = realpath( dirname( __FILE__ ) . '/legacy-encrypted-text.txt' );
+               $this->legacyEncryptedDataKey = realpath( dirname( __FILE__ ) . '/encryption.key' );
                $this->randomKey = Encryption\Crypt::generateKey();
 
                $keypair = Encryption\Crypt::createKeypair();
                $this->genPublicKey = $keypair['publicKey'];
                $this->genPrivateKey = $keypair['privateKey'];
 
-               $this->view = new \OC_FilesystemView('/');
+               $this->view = new \OC_FilesystemView( '/' );
 
-               \OC_User::setUserId('admin');
+               \OC_User::setUserId( 'admin' );
                $this->userId = 'admin';
                $this->pass = 'admin';
 
-               $userHome = \OC_User::getHome($this->userId);
-               $this->dataDir = str_replace('/' . $this->userId, '', $userHome);
-
-               // Filesystem related hooks
-               \OCA\Encryption\Helper::registerFilesystemHooks();
-
-               // Filesystem related hooks
-               \OCA\Encryption\Helper::registerUserHooks();
-
-               \OC_FileProxy::register(new OCA\Encryption\Proxy());
+               $userHome = \OC_User::getHome( $this->userId );
+               $this->dataDir = str_replace( '/' . $this->userId, '', $userHome );
 
                // remember files_trashbin state
-               $this->stateFilesTrashbin = OC_App::isEnabled('files_trashbin');
+               $this->stateFilesTrashbin = OC_App::isEnabled( 'files_trashbin' );
 
                // we don't want to tests with app files_trashbin enabled
-               \OC_App::disable('files_trashbin');
-
-               \OC_Util::tearDownFS();
-               \OC_User::setUserId('');
-               \OC\Files\Filesystem::tearDown();
-               \OC_Util::setupFS($this->userId);
-               \OC_User::setUserId($this->userId);
-
-               $params['uid'] = $this->userId;
-               $params['password'] = $this->pass;
-               OCA\Encryption\Hooks::login($params);
-
+               \OC_App::disable( 'files_trashbin' );
        }
 
-       function tearDown()
-       {
-               \OC_FileProxy::clearProxies();
-
+       function tearDown() {
                // reset app files_trashbin
-               if ($this->stateFilesTrashbin) {
-                       OC_App::enable('files_trashbin');
+               if ( $this->stateFilesTrashbin ) {
+                       OC_App::enable( 'files_trashbin' );
                } else {
-                       OC_App::disable('files_trashbin');
+                       OC_App::disable( 'files_trashbin' );
                }
        }
 
-       function testGenerateKey()
-       {
+       public static function tearDownAfterClass() {
+
+       }
+
+       function testGenerateKey() {
 
                # TODO: use more accurate (larger) string length for test confirmation
 
                $key = Encryption\Crypt::generateKey();
 
-               $this->assertTrue(strlen($key) > 16);
+               $this->assertTrue( strlen( $key ) > 16 );
 
        }
 
        /**
         * @return String
         */
-       function testGenerateIv()
-       {
+       function testGenerateIv() {
 
                $iv = Encryption\Crypt::generateIv();
 
-               $this->assertEquals(16, strlen($iv));
+               $this->assertEquals( 16, strlen( $iv ) );
 
                return $iv;
 
@@ -133,27 +136,26 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase
        /**
         * @depends testGenerateIv
         */
-       function testConcatIv($iv)
-       {
+       function testConcatIv( $iv ) {
 
-               $catFile = Encryption\Crypt::concatIv($this->dataLong, $iv);
+               $catFile = Encryption\Crypt::concatIv( $this->dataLong, $iv );
 
                // Fetch encryption metadata from end of file
-               $meta = substr($catFile, -22);
+               $meta = substr( $catFile, -22 );
 
-               $identifier = substr($meta, 0, 6);
+               $identifier = substr( $meta, 0, 6 );
 
                // Fetch IV from end of file
-               $foundIv = substr($meta, 6);
+               $foundIv = substr( $meta, 6 );
 
-               $this->assertEquals('00iv00', $identifier);
+               $this->assertEquals( '00iv00', $identifier );
 
-               $this->assertEquals($iv, $foundIv);
+               $this->assertEquals( $iv, $foundIv );
 
                // Remove IV and IV identifier text to expose encrypted content
-               $data = substr($catFile, 0, -22);
+               $data = substr( $catFile, 0, -22 );
 
-               $this->assertEquals($this->dataLong, $data);
+               $this->assertEquals( $this->dataLong, $data );
 
                return array(
                        'iv' => $iv
@@ -165,31 +167,29 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase
        /**
         * @depends testConcatIv
         */
-       function testSplitIv($testConcatIv)
-       {
+       function testSplitIv( $testConcatIv ) {
 
                // Split catfile into components
-               $splitCatfile = Encryption\Crypt::splitIv($testConcatIv['catfile']);
+               $splitCatfile = Encryption\Crypt::splitIv( $testConcatIv['catfile'] );
 
                // Check that original IV and split IV match
-               $this->assertEquals($testConcatIv['iv'], $splitCatfile['iv']);
+               $this->assertEquals( $testConcatIv['iv'], $splitCatfile['iv'] );
 
                // Check that original data and split data match
-               $this->assertEquals($this->dataLong, $splitCatfile['encrypted']);
+               $this->assertEquals( $this->dataLong, $splitCatfile['encrypted'] );
 
        }
 
        /**
         * @return string padded
         */
-       function testAddPadding()
-       {
+       function testAddPadding() {
 
-               $padded = Encryption\Crypt::addPadding($this->dataLong);
+               $padded = Encryption\Crypt::addPadding( $this->dataLong );
 
-               $padding = substr($padded, -2);
+               $padding = substr( $padded, -2 );
 
-               $this->assertEquals('xx', $padding);
+               $this->assertEquals( 'xx', $padding );
 
                return $padded;
 
@@ -198,107 +198,102 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase
        /**
         * @depends testAddPadding
         */
-       function testRemovePadding($padded)
-       {
+       function testRemovePadding( $padded ) {
 
-               $noPadding = Encryption\Crypt::RemovePadding($padded);
+               $noPadding = Encryption\Crypt::RemovePadding( $padded );
 
-               $this->assertEquals($this->dataLong, $noPadding);
+               $this->assertEquals( $this->dataLong, $noPadding );
 
        }
 
-       function testEncrypt()
-       {
+       function testEncrypt() {
 
-               $random = openssl_random_pseudo_bytes(13);
+               $random = openssl_random_pseudo_bytes( 13 );
 
-               $iv = substr(base64_encode($random), 0, -4); // i.e. E5IG033j+mRNKrht
+               $iv = substr( base64_encode( $random ), 0, -4 ); // i.e. E5IG033j+mRNKrht
 
-               $crypted = Encryption\Crypt::encrypt($this->dataUrl, $iv, 'hat');
+               $crypted = Encryption\Crypt::encrypt( $this->dataUrl, $iv, 'hat' );
 
-               $this->assertNotEquals($this->dataUrl, $crypted);
+               $this->assertNotEquals( $this->dataUrl, $crypted );
 
        }
 
-       function testDecrypt()
-       {
+       function testDecrypt() {
 
-               $random = openssl_random_pseudo_bytes(13);
+               $random = openssl_random_pseudo_bytes( 13 );
 
-               $iv = substr(base64_encode($random), 0, -4); // i.e. E5IG033j+mRNKrht
+               $iv = substr( base64_encode( $random ), 0, -4 ); // i.e. E5IG033j+mRNKrht
 
-               $crypted = Encryption\Crypt::encrypt($this->dataUrl, $iv, 'hat');
+               $crypted = Encryption\Crypt::encrypt( $this->dataUrl, $iv, 'hat' );
 
-               $decrypt = Encryption\Crypt::decrypt($crypted, $iv, 'hat');
+               $decrypt = Encryption\Crypt::decrypt( $crypted, $iv, 'hat' );
 
-               $this->assertEquals($this->dataUrl, $decrypt);
+               $this->assertEquals( $this->dataUrl, $decrypt );
 
        }
 
-       function testSymmetricEncryptFileContent()
-       {
+       function testSymmetricEncryptFileContent() {
 
                # TODO: search in keyfile for actual content as IV will ensure this test always passes
 
-               $crypted = Encryption\Crypt::symmetricEncryptFileContent($this->dataShort, 'hat');
+               $crypted = Encryption\Crypt::symmetricEncryptFileContent( $this->dataShort, 'hat' );
 
-               $this->assertNotEquals($this->dataShort, $crypted);
+               $this->assertNotEquals( $this->dataShort, $crypted );
 
 
-               $decrypt = Encryption\Crypt::symmetricDecryptFileContent($crypted, 'hat');
+               $decrypt = Encryption\Crypt::symmetricDecryptFileContent( $crypted, 'hat' );
 
-               $this->assertEquals($this->dataShort, $decrypt);
+               $this->assertEquals( $this->dataShort, $decrypt );
 
        }
 
-       function testSymmetricStreamEncryptShortFileContent()
-       {
+       function testSymmetricStreamEncryptShortFileContent() {
 
                $filename = 'tmp-' . time() . '.test';
 
-               $cryptedFile = file_put_contents('crypt://' . $filename, $this->dataShort);
+               $cryptedFile = file_put_contents( 'crypt://' . $filename, $this->dataShort );
 
                // Test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $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);
+               $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);
+               $this->assertNotEquals( $this->dataShort, $retreivedCryptedFile );
 
                // Get the encrypted keyfile
-               $encKeyfile = Encryption\Keymanager::getFileKey($this->view, $this->userId, $filename);
+               $encKeyfile = Encryption\Keymanager::getFileKey( $this->view, $this->userId, $filename );
 
                // Attempt to fetch the user's shareKey
-               $shareKey = Encryption\Keymanager::getShareKey($this->view, $this->userId, $filename);
+               $shareKey = Encryption\Keymanager::getShareKey( $this->view, $this->userId, $filename );
 
                // get session
-               $session = new Encryption\Session($this->view);
+               $session = new Encryption\Session( $this->view );
 
                // get private key
-               $privateKey = $session->getPrivateKey($this->userId);
+               $privateKey = $session->getPrivateKey( $this->userId );
 
                // Decrypt keyfile with shareKey
-               $plainKeyfile = Encryption\Crypt::multiKeyDecrypt($encKeyfile, $shareKey, $privateKey);
+               $plainKeyfile = Encryption\Crypt::multiKeyDecrypt( $encKeyfile, $shareKey, $privateKey );
 
                // Manually decrypt
-               $manualDecrypt = Encryption\Crypt::symmetricDecryptFileContent($retreivedCryptedFile, $plainKeyfile);
+               $manualDecrypt = Encryption\Crypt::symmetricDecryptFileContent( $retreivedCryptedFile, $plainKeyfile );
 
                // Check that decrypted data matches
-               $this->assertEquals($this->dataShort, $manualDecrypt);
+               $this->assertEquals( $this->dataShort, $manualDecrypt );
 
                // Teardown
-               $this->view->unlink($this->userId . '/files/' . $filename);
+               $this->view->unlink( $this->userId . '/files/' . $filename );
 
-               Encryption\Keymanager::deleteFileKey($this->view, $this->userId, $filename);
+               Encryption\Keymanager::deleteFileKey( $this->view, $this->userId, $filename );
        }
 
        /**
@@ -307,212 +302,204 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase
         * @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
         */
-       function testSymmetricStreamEncryptLongFileContent()
-       {
+       function testSymmetricStreamEncryptLongFileContent() {
 
                // Generate a a random filename
                $filename = 'tmp-' . time() . '.test';
 
                // Save long data as encrypted file using stream wrapper
-               $cryptedFile = file_put_contents('crypt://' . $filename, $this->dataLong . $this->dataLong);
+               $cryptedFile = file_put_contents( 'crypt://' . $filename, $this->dataLong . $this->dataLong );
 
                // Test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $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);
+               $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);
+               $this->assertNotEquals( $this->dataLong . $this->dataLong, $retreivedCryptedFile );
 
                // Manuallly split saved file into separate IVs and encrypted chunks
-               $r = preg_split('/(00iv00.{16,18})/', $retreivedCryptedFile, NULL, PREG_SPLIT_DELIM_CAPTURE);
+               $r = preg_split( '/(00iv00.{16,18})/', $retreivedCryptedFile, NULL, PREG_SPLIT_DELIM_CAPTURE );
 
                //print_r($r);
 
                // Join IVs and their respective data chunks
-               $e = array($r[0] . $r[1], $r[2] . $r[3], $r[4] . $r[5], $r[6] . $r[7], $r[8] . $r[9], $r[10] . $r[11]); //.$r[11], $r[12].$r[13], $r[14] );
+               $e = array( $r[0] . $r[1], $r[2] . $r[3], $r[4] . $r[5], $r[6] . $r[7], $r[8] . $r[9], $r[10] . $r[11] ); //.$r[11], $r[12].$r[13], $r[14] );
 
                //print_r($e);
 
                // Get the encrypted keyfile
-               $encKeyfile = Encryption\Keymanager::getFileKey($this->view, $this->userId, $filename);
+               $encKeyfile = Encryption\Keymanager::getFileKey( $this->view, $this->userId, $filename );
 
                // Attempt to fetch the user's shareKey
-               $shareKey = Encryption\Keymanager::getShareKey($this->view, $this->userId, $filename);
+               $shareKey = Encryption\Keymanager::getShareKey( $this->view, $this->userId, $filename );
 
                // get session
-               $session = new Encryption\Session($this->view);
+               $session = new Encryption\Session( $this->view );
 
                // get private key
-               $privateKey = $session->getPrivateKey($this->userId);
+               $privateKey = $session->getPrivateKey( $this->userId );
 
                // Decrypt keyfile with shareKey
-               $plainKeyfile = Encryption\Crypt::multiKeyDecrypt($encKeyfile, $shareKey, $privateKey);
+               $plainKeyfile = Encryption\Crypt::multiKeyDecrypt( $encKeyfile, $shareKey, $privateKey );
 
                // Set var for reassembling decrypted content
                $decrypt = '';
 
                // Manually decrypt chunk
-               foreach ($e as $chunk) {
+               foreach ( $e as $chunk ) {
 
-                       $chunkDecrypt = Encryption\Crypt::symmetricDecryptFileContent($chunk, $plainKeyfile);
+                       $chunkDecrypt = Encryption\Crypt::symmetricDecryptFileContent( $chunk, $plainKeyfile );
 
                        // Assemble decrypted chunks
                        $decrypt .= $chunkDecrypt;
 
                }
 
-               $this->assertEquals($this->dataLong . $this->dataLong, $decrypt);
+               $this->assertEquals( $this->dataLong . $this->dataLong, $decrypt );
 
                // Teardown
 
-               $this->view->unlink($this->userId . '/files/' . $filename);
+               $this->view->unlink( $this->userId . '/files/' . $filename );
 
-               Encryption\Keymanager::deleteFileKey($this->view, $this->userId, $filename);
+               Encryption\Keymanager::deleteFileKey( $this->view, $this->userId, $filename );
 
        }
 
        /**
         * @brief Test that data that is read by the crypto stream wrapper
         */
-       function testSymmetricStreamDecryptShortFileContent()
-       {
+       function testSymmetricStreamDecryptShortFileContent() {
 
                $filename = 'tmp-' . time();
 
                // Save long data as encrypted file using stream wrapper
-               $cryptedFile = file_put_contents('crypt://' . $filename, $this->dataShort);
+               $cryptedFile = file_put_contents( 'crypt://' . $filename, $this->dataShort );
 
                // Test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_int( $cryptedFile ) );
 
                // Disable encryption proxy to prevent recursive calls
                $proxyStatus = \OC_FileProxy::$enabled;
                \OC_FileProxy::$enabled = false;
 
-               $this->assertTrue(Encryption\Crypt::isEncryptedMeta($filename));
+               $this->assertTrue( Encryption\Crypt::isEncryptedMeta( $filename ) );
 
                \OC_FileProxy::$enabled = $proxyStatus;
 
                // Get file decrypted contents
-               $decrypt = file_get_contents('crypt://' . $filename);
+               $decrypt = file_get_contents( 'crypt://' . $filename );
 
-               $this->assertEquals($this->dataShort, $decrypt);
+               $this->assertEquals( $this->dataShort, $decrypt );
 
                // tear down
-               $this->view->unlink($this->userId . '/files/' . $filename);
+               $this->view->unlink( $this->userId . '/files/' . $filename );
        }
 
-       function testSymmetricStreamDecryptLongFileContent()
-       {
+       function testSymmetricStreamDecryptLongFileContent() {
 
                $filename = 'tmp-' . time();
 
                // Save long data as encrypted file using stream wrapper
-               $cryptedFile = file_put_contents('crypt://' . $filename, $this->dataLong);
+               $cryptedFile = file_put_contents( 'crypt://' . $filename, $this->dataLong );
 
                // Test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_int( $cryptedFile ) );
 
                // Get file decrypted contents
-               $decrypt = file_get_contents('crypt://' . $filename);
+               $decrypt = file_get_contents( 'crypt://' . $filename );
 
-               $this->assertEquals($this->dataLong, $decrypt);
+               $this->assertEquals( $this->dataLong, $decrypt );
 
                // tear down
-               $this->view->unlink($this->userId . '/files/' . $filename);
+               $this->view->unlink( $this->userId . '/files/' . $filename );
        }
 
-       function testSymmetricEncryptFileContentKeyfile()
-       {
+       function testSymmetricEncryptFileContentKeyfile() {
 
                # TODO: search in keyfile for actual content as IV will ensure this test always passes
 
-               $crypted = Encryption\Crypt::symmetricEncryptFileContentKeyfile($this->dataUrl);
+               $crypted = Encryption\Crypt::symmetricEncryptFileContentKeyfile( $this->dataUrl );
 
-               $this->assertNotEquals($this->dataUrl, $crypted['encrypted']);
+               $this->assertNotEquals( $this->dataUrl, $crypted['encrypted'] );
 
 
-               $decrypt = Encryption\Crypt::symmetricDecryptFileContent($crypted['encrypted'], $crypted['key']);
+               $decrypt = Encryption\Crypt::symmetricDecryptFileContent( $crypted['encrypted'], $crypted['key'] );
 
-               $this->assertEquals($this->dataUrl, $decrypt);
+               $this->assertEquals( $this->dataUrl, $decrypt );
 
        }
 
-       function testIsEncryptedContent()
-       {
+       function testIsEncryptedContent() {
 
-               $this->assertFalse(Encryption\Crypt::isCatfileContent($this->dataUrl));
+               $this->assertFalse( Encryption\Crypt::isCatfileContent( $this->dataUrl ) );
 
-               $this->assertFalse(Encryption\Crypt::isCatfileContent($this->legacyEncryptedData));
+               $this->assertFalse( Encryption\Crypt::isCatfileContent( $this->legacyEncryptedData ) );
 
-               $keyfileContent = Encryption\Crypt::symmetricEncryptFileContent($this->dataUrl, 'hat');
+               $keyfileContent = Encryption\Crypt::symmetricEncryptFileContent( $this->dataUrl, 'hat' );
 
-               $this->assertTrue(Encryption\Crypt::isCatfileContent($keyfileContent));
+               $this->assertTrue( Encryption\Crypt::isCatfileContent( $keyfileContent ) );
 
        }
 
-       function testMultiKeyEncrypt()
-       {
+       function testMultiKeyEncrypt() {
 
                # TODO: search in keyfile for actual content as IV will ensure this test always passes
 
                $pair1 = Encryption\Crypt::createKeypair();
 
-               $this->assertEquals(2, count($pair1));
+               $this->assertEquals( 2, count( $pair1 ) );
 
-               $this->assertTrue(strlen($pair1['publicKey']) > 1);
+               $this->assertTrue( strlen( $pair1['publicKey'] ) > 1 );
 
-               $this->assertTrue(strlen($pair1['privateKey']) > 1);
+               $this->assertTrue( strlen( $pair1['privateKey'] ) > 1 );
 
 
-               $crypted = Encryption\Crypt::multiKeyEncrypt($this->dataShort, array($pair1['publicKey']));
+               $crypted = Encryption\Crypt::multiKeyEncrypt( $this->dataShort, array( $pair1['publicKey'] ) );
 
-               $this->assertNotEquals($this->dataShort, $crypted['data']);
+               $this->assertNotEquals( $this->dataShort, $crypted['data'] );
 
 
-               $decrypt = Encryption\Crypt::multiKeyDecrypt($crypted['data'], $crypted['keys'][0], $pair1['privateKey']);
+               $decrypt = Encryption\Crypt::multiKeyDecrypt( $crypted['data'], $crypted['keys'][0], $pair1['privateKey'] );
 
-               $this->assertEquals($this->dataShort, $decrypt);
+               $this->assertEquals( $this->dataShort, $decrypt );
 
        }
 
-       function testKeyEncrypt()
-       {
+       function testKeyEncrypt() {
 
                // Generate keypair
                $pair1 = Encryption\Crypt::createKeypair();
 
                // Encrypt data
-               $crypted = Encryption\Crypt::keyEncrypt($this->dataUrl, $pair1['publicKey']);
+               $crypted = Encryption\Crypt::keyEncrypt( $this->dataUrl, $pair1['publicKey'] );
 
-               $this->assertNotEquals($this->dataUrl, $crypted);
+               $this->assertNotEquals( $this->dataUrl, $crypted );
 
                // Decrypt data
-               $decrypt = Encryption\Crypt::keyDecrypt($crypted, $pair1['privateKey']);
+               $decrypt = Encryption\Crypt::keyDecrypt( $crypted, $pair1['privateKey'] );
 
-               $this->assertEquals($this->dataUrl, $decrypt);
+               $this->assertEquals( $this->dataUrl, $decrypt );
 
        }
 
        /**
         * @brief test encryption using legacy blowfish method
         */
-       function testLegacyEncryptShort()
-       {
+       function testLegacyEncryptShort() {
 
-               $crypted = Encryption\Crypt::legacyEncrypt($this->dataShort, $this->pass);
+               $crypted = Encryption\Crypt::legacyEncrypt( $this->dataShort, $this->pass );
 
-               $this->assertNotEquals($this->dataShort, $crypted);
+               $this->assertNotEquals( $this->dataShort, $crypted );
 
                # TODO: search inencrypted text for actual content to ensure it
                # genuine transformation
@@ -525,24 +512,22 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase
         * @brief test decryption using legacy blowfish method
         * @depends testLegacyEncryptShort
         */
-       function testLegacyDecryptShort($crypted)
-       {
+       function testLegacyDecryptShort( $crypted ) {
 
-               $decrypted = Encryption\Crypt::legacyDecrypt($crypted, $this->pass);
+               $decrypted = Encryption\Crypt::legacyDecrypt( $crypted, $this->pass );
 
-               $this->assertEquals($this->dataShort, $decrypted);
+               $this->assertEquals( $this->dataShort, $decrypted );
 
        }
 
        /**
         * @brief test encryption using legacy blowfish method
         */
-       function testLegacyEncryptLong()
-       {
+       function testLegacyEncryptLong() {
 
-               $crypted = Encryption\Crypt::legacyEncrypt($this->dataLong, $this->pass);
+               $crypted = Encryption\Crypt::legacyEncrypt( $this->dataLong, $this->pass );
 
-               $this->assertNotEquals($this->dataLong, $crypted);
+               $this->assertNotEquals( $this->dataLong, $crypted );
 
                # TODO: search inencrypted text for actual content to ensure it
                # genuine transformation
@@ -555,33 +540,31 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase
         * @brief test decryption using legacy blowfish method
         * @depends testLegacyEncryptLong
         */
-       function testLegacyDecryptLong($crypted)
-       {
+       function testLegacyDecryptLong( $crypted ) {
 
-               $decrypted = Encryption\Crypt::legacyDecrypt($crypted, $this->pass);
+               $decrypted = Encryption\Crypt::legacyDecrypt( $crypted, $this->pass );
 
-               $this->assertEquals($this->dataLong, $decrypted);
+               $this->assertEquals( $this->dataLong, $decrypted );
 
-               $this->assertFalse(Encryption\Crypt::getBlowfish(''));
+               $this->assertFalse( Encryption\Crypt::getBlowfish( '' ) );
        }
 
        /**
         * @brief test generation of legacy encryption key
         * @depends testLegacyDecryptShort
         */
-       function testLegacyCreateKey()
-       {
+       function testLegacyCreateKey() {
 
                // Create encrypted key
-               $encKey = Encryption\Crypt::legacyCreateKey($this->pass);
+               $encKey = Encryption\Crypt::legacyCreateKey( $this->pass );
 
                // Decrypt key
-               $key = Encryption\Crypt::legacyDecrypt($encKey, $this->pass);
+               $key = Encryption\Crypt::legacyDecrypt( $encKey, $this->pass );
 
-               $this->assertTrue(is_numeric($key));
+               $this->assertTrue( is_numeric( $key ) );
 
                // Check that key is correct length
-               $this->assertEquals(20, strlen($key));
+               $this->assertEquals( 20, strlen( $key ) );
 
        }
 
@@ -589,12 +572,11 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase
         * @brief test decryption using legacy blowfish method
         * @depends testLegacyEncryptLong
         */
-       function testLegacyKeyRecryptKeyfileEncrypt($crypted)
-       {
+       function testLegacyKeyRecryptKeyfileEncrypt( $crypted ) {
 
-               $recrypted = Encryption\Crypt::LegacyKeyRecryptKeyfile($crypted, $this->pass, array($this->genPublicKey), $this->pass, '');
+               $recrypted = Encryption\Crypt::LegacyKeyRecryptKeyfile( $crypted, $this->pass, array( $this->genPublicKey ), $this->pass, '' );
 
-               $this->assertNotEquals($this->dataLong, $recrypted['data']);
+               $this->assertNotEquals( $this->dataLong, $recrypted['data'] );
 
                return $recrypted;
 
@@ -603,231 +585,224 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase
 
        }
 
-       function testRenameFile()
-       {
+       function testRenameFile() {
 
                $filename = 'tmp-' . time();
 
                // Save long data as encrypted file using stream wrapper
-               $cryptedFile = file_put_contents('crypt://' . $filename, $this->dataLong);
+               $cryptedFile = file_put_contents( 'crypt://' . $filename, $this->dataLong );
 
                // Test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_int( $cryptedFile ) );
 
                // Get file decrypted contents
-               $decrypt = file_get_contents('crypt://' . $filename);
+               $decrypt = file_get_contents( 'crypt://' . $filename );
 
-               $this->assertEquals($this->dataLong, $decrypt);
+               $this->assertEquals( $this->dataLong, $decrypt );
 
                $newFilename = 'tmp-new-' . time();
-               $view = new \OC\Files\View('/' . $this->userId . '/files');
-               $view->rename($filename, $newFilename);
+               $view = new \OC\Files\View( '/' . $this->userId . '/files' );
+               $view->rename( $filename, $newFilename );
 
                // Get file decrypted contents
-               $newDecrypt = file_get_contents('crypt://' . $newFilename);
+               $newDecrypt = file_get_contents( 'crypt://' . $newFilename );
 
-               $this->assertEquals($this->dataLong, $newDecrypt);
+               $this->assertEquals( $this->dataLong, $newDecrypt );
 
                // tear down
-               $view->unlink($newFilename);
+               $view->unlink( $newFilename );
        }
 
-       function testMoveFileIntoFolder()
-       {
+       function testMoveFileIntoFolder() {
 
                $filename = 'tmp-' . time();
 
                // Save long data as encrypted file using stream wrapper
-               $cryptedFile = file_put_contents('crypt://' . $filename, $this->dataLong);
+               $cryptedFile = file_put_contents( 'crypt://' . $filename, $this->dataLong );
 
                // Test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_int( $cryptedFile ) );
 
                // Get file decrypted contents
-               $decrypt = file_get_contents('crypt://' . $filename);
+               $decrypt = file_get_contents( 'crypt://' . $filename );
 
-               $this->assertEquals($this->dataLong, $decrypt);
+               $this->assertEquals( $this->dataLong, $decrypt );
 
                $newFolder = '/newfolder' . time();
                $newFilename = 'tmp-new-' . time();
-               $view = new \OC\Files\View('/' . $this->userId . '/files');
-               $view->mkdir($newFolder);
-               $view->rename($filename, $newFolder . '/' . $newFilename);
+               $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://' . $newFolder . '/' . $newFilename);
+               $newDecrypt = file_get_contents( 'crypt://' . $newFolder . '/' . $newFilename );
 
-               $this->assertEquals($this->dataLong, $newDecrypt);
+               $this->assertEquals( $this->dataLong, $newDecrypt );
 
                // tear down
-               $view->unlink($newFolder);
+               $view->unlink( $newFolder );
        }
 
-       function testMoveFolder()
-       {
+       function testMoveFolder() {
 
-               $view = new \OC\Files\View('/' . $this->userId . '/files');
+               $view = new \OC\Files\View( '/' . $this->userId . '/files' );
 
                $filename = '/tmp-' . time();
                $folder = '/folder' . time();
 
-               $view->mkdir($folder);
+               $view->mkdir( $folder );
 
                // Save long data as encrypted file using stream wrapper
-               $cryptedFile = file_put_contents('crypt://' . $folder . $filename, $this->dataLong);
+               $cryptedFile = file_put_contents( 'crypt://' . $folder . $filename, $this->dataLong );
 
                // Test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_int( $cryptedFile ) );
 
                // Get file decrypted contents
-               $decrypt = file_get_contents('crypt://' . $folder . $filename);
+               $decrypt = file_get_contents( 'crypt://' . $folder . $filename );
 
-               $this->assertEquals($this->dataLong, $decrypt);
+               $this->assertEquals( $this->dataLong, $decrypt );
 
                $newFolder = '/newfolder/subfolder' . time();
-               $view->mkdir('/newfolder');
+               $view->mkdir( '/newfolder' );
 
-               $view->rename($folder, $newFolder);
+               $view->rename( $folder, $newFolder );
 
                // Get file decrypted contents
-               $newDecrypt = file_get_contents('crypt://' . $newFolder . $filename);
+               $newDecrypt = file_get_contents( 'crypt://' . $newFolder . $filename );
 
-               $this->assertEquals($this->dataLong, $newDecrypt);
+               $this->assertEquals( $this->dataLong, $newDecrypt );
 
                // tear down
-               $view->unlink($newFolder);
+               $view->unlink( $newFolder );
+               $view->unlink( '/newfolder' );
        }
 
-       function testChangePassphrase()
-       {
-       $filename = 'tmp-' . time();
+       function testChangePassphrase() {
+               $filename = 'tmp-' . time();
 
                // Save long data as encrypted file using stream wrapper
-               $cryptedFile = file_put_contents('crypt://' . $filename, $this->dataLong);
+               $cryptedFile = file_put_contents( 'crypt://' . $filename, $this->dataLong );
 
                // Test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_int( $cryptedFile ) );
 
                // Get file decrypted contents
-               $decrypt = file_get_contents('crypt://' . $filename);
+               $decrypt = file_get_contents( 'crypt://' . $filename );
 
-               $this->assertEquals($this->dataLong, $decrypt);
+               $this->assertEquals( $this->dataLong, $decrypt );
 
                // change password
-               \OC_User::setPassword($this->userId, 'test', null);
+               \OC_User::setPassword( $this->userId, 'test', null );
 
                // relogin
                $params['uid'] = $this->userId;
                $params['password'] = 'test';
-               OCA\Encryption\Hooks::login($params);
+               OCA\Encryption\Hooks::login( $params );
 
                // Get file decrypted contents
-               $newDecrypt = file_get_contents('crypt://' . $filename);
+               $newDecrypt = file_get_contents( 'crypt://' . $filename );
 
-               $this->assertEquals($this->dataLong, $newDecrypt);
+               $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);
+               \OC_User::setPassword( $this->userId, $this->pass );
+               $view = new \OC\Files\View( '/' . $this->userId . '/files' );
+               $view->unlink( $filename );
        }
 
-       function testViewFilePutAndGetContents()
-       {
+       function testViewFilePutAndGetContents() {
 
                $filename = '/tmp-' . time();
-               $view = new \OC\Files\View('/' . $this->userId . '/files');
+               $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);
+               $cryptedFile = $view->file_put_contents( $filename, $this->dataShort );
 
                // Test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_int( $cryptedFile ) );
 
                // Get file decrypted contents
-               $decrypt = $view->file_get_contents($filename);
+               $decrypt = $view->file_get_contents( $filename );
 
-               $this->assertEquals($this->dataShort, $decrypt);
+               $this->assertEquals( $this->dataShort, $decrypt );
 
                // Save long data as encrypted file using stream wrapper
-               $cryptedFileLong = $view->file_put_contents($filename, $this->dataLong);
+               $cryptedFileLong = $view->file_put_contents( $filename, $this->dataLong );
 
                // Test that data was successfully written
-               $this->assertTrue(is_int($cryptedFileLong));
+               $this->assertTrue( is_int( $cryptedFileLong ) );
 
                // Get file decrypted contents
-               $decryptLong = $view->file_get_contents($filename);
+               $decryptLong = $view->file_get_contents( $filename );
 
-               $this->assertEquals($this->dataLong, $decryptLong);
+               $this->assertEquals( $this->dataLong, $decryptLong );
 
                // tear down
-               $view->unlink($filename);
+               $view->unlink( $filename );
        }
 
-       function testTouchExistingFile()
-       {
+       function testTouchExistingFile() {
                $filename = '/tmp-' . time();
-               $view = new \OC\Files\View('/' . $this->userId . '/files');
+               $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);
+               $cryptedFile = $view->file_put_contents( $filename, $this->dataShort );
 
                // Test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_int( $cryptedFile ) );
 
-               $view->touch($filename);
+               $view->touch( $filename );
 
                // Get file decrypted contents
-               $decrypt = $view->file_get_contents($filename);
+               $decrypt = $view->file_get_contents( $filename );
 
-               $this->assertEquals($this->dataShort, $decrypt);
+               $this->assertEquals( $this->dataShort, $decrypt );
 
                // tear down
-               $view->unlink($filename);
+               $view->unlink( $filename );
        }
 
-       function testTouchFile()
-       {
+       function testTouchFile() {
                $filename = '/tmp-' . time();
-               $view = new \OC\Files\View('/' . $this->userId . '/files');
+               $view = new \OC\Files\View( '/' . $this->userId . '/files' );
 
-               $view->touch($filename);
+               $view->touch( $filename );
 
                // Save short data as encrypted file using stream wrapper
-               $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
+               $cryptedFile = $view->file_put_contents( $filename, $this->dataShort );
 
                // Test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_int( $cryptedFile ) );
 
                // Get file decrypted contents
-               $decrypt = $view->file_get_contents($filename);
+               $decrypt = $view->file_get_contents( $filename );
 
-               $this->assertEquals($this->dataShort, $decrypt);
+               $this->assertEquals( $this->dataShort, $decrypt );
 
                // tear down
-               $view->unlink($filename);
+               $view->unlink( $filename );
        }
 
-       function testFopenFile()
-       {
+       function testFopenFile() {
                $filename = '/tmp-' . time();
-               $view = new \OC\Files\View('/' . $this->userId . '/files');
+               $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);
+               $cryptedFile = $view->file_put_contents( $filename, $this->dataShort );
 
                // Test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_int( $cryptedFile ) );
 
-               $handle = $view->fopen($filename, 'r');
+               $handle = $view->fopen( $filename, 'r' );
 
                // Get file decrypted contents
-               $decrypt = fgets($handle);
+               $decrypt = fgets( $handle );
 
-               $this->assertEquals($this->dataShort, $decrypt);
+               $this->assertEquals( $this->dataShort, $decrypt );
 
                // tear down
-               $view->unlink($filename);
+               $view->unlink( $filename );
        }
 }
index b1bae673e8264d26bc4fe9bba79831b0e04e1f43..1e7c2bdb40bf91df1e0259405c77737adb559fbb 100644 (file)
@@ -6,14 +6,14 @@
  * See the COPYING-README file.
  */
 
-require_once realpath(dirname(__FILE__) . '/../../../lib/base.php');
-require_once realpath(dirname(__FILE__) . '/../lib/crypt.php');
-require_once realpath(dirname(__FILE__) . '/../lib/keymanager.php');
-require_once realpath(dirname(__FILE__) . '/../lib/proxy.php');
-require_once realpath(dirname(__FILE__) . '/../lib/stream.php');
-require_once realpath(dirname(__FILE__) . '/../lib/util.php');
-require_once realpath(dirname(__FILE__) . '/../lib/helper.php');
-require_once realpath(dirname(__FILE__) . '/../appinfo/app.php');
+require_once realpath( dirname( __FILE__ ) . '/../../../lib/base.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/crypt.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/keymanager.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/proxy.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/stream.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/util.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/helper.php' );
+require_once realpath( dirname( __FILE__ ) . '/../appinfo/app.php' );
 
 use OCA\Encryption;
 
@@ -33,109 +33,111 @@ class Test_Encryption_Keymanager extends \PHPUnit_Framework_TestCase
        public $randomKey;
        public $dataShort;
 
-       function setUp()
-       {
+       public static function setUpBeforeClass() {
                // reset backend
                \OC_User::clearBackends();
-               \OC_User::useBackend('database');
+               \OC_User::useBackend( 'database' );
 
+               // Filesystem related hooks
+               \OCA\Encryption\Helper::registerFilesystemHooks();
+
+               // clear and register hooks
+               \OC_FileProxy::clearProxies();
+               \OC_FileProxy::register( new OCA\Encryption\Proxy() );
+
+               // disable file proxy by default
                \OC_FileProxy::$enabled = false;
 
+               // setup filesystem
+               \OC_Util::tearDownFS();
+               \OC_User::setUserId( '' );
+               \OC\Files\Filesystem::tearDown();
+               \OC_Util::setupFS( 'admin' );
+               \OC_User::setUserId( 'admin' );
+
+               // login admin
+               $params['uid'] = 'admin';
+               $params['password'] = 'admin';
+               OCA\Encryption\Hooks::login( $params );
+       }
+
+       function setUp() {
                // set content for encrypting / decrypting in tests
-               $this->dataLong = file_get_contents(realpath(dirname(__FILE__) . '/../lib/crypt.php'));
+               $this->dataLong = file_get_contents( realpath( dirname( __FILE__ ) . '/../lib/crypt.php' ) );
                $this->dataShort = 'hats';
-               $this->dataUrl = realpath(dirname(__FILE__) . '/../lib/crypt.php');
-               $this->legacyData = realpath(dirname(__FILE__) . '/legacy-text.txt');
-               $this->legacyEncryptedData = realpath(dirname(__FILE__) . '/legacy-encrypted-text.txt');
+               $this->dataUrl = realpath( dirname( __FILE__ ) . '/../lib/crypt.php' );
+               $this->legacyData = realpath( dirname( __FILE__ ) . '/legacy-text.txt' );
+               $this->legacyEncryptedData = realpath( dirname( __FILE__ ) . '/legacy-encrypted-text.txt' );
                $this->randomKey = Encryption\Crypt::generateKey();
 
                $keypair = Encryption\Crypt::createKeypair();
                $this->genPublicKey = $keypair['publicKey'];
                $this->genPrivateKey = $keypair['privateKey'];
 
-               $this->view = new \OC_FilesystemView('/');
+               $this->view = new \OC_FilesystemView( '/' );
 
-               \OC_User::setUserId('admin');
+               \OC_User::setUserId( 'admin' );
                $this->userId = 'admin';
                $this->pass = 'admin';
 
-               $userHome = \OC_User::getHome($this->userId);
-               $this->dataDir = str_replace('/' . $this->userId, '', $userHome);
-
-               // Filesystem related hooks
-               \OCA\Encryption\Helper::registerFilesystemHooks();
-
-               \OC_FileProxy::register(new OCA\Encryption\Proxy());
+               $userHome = \OC_User::getHome( $this->userId );
+               $this->dataDir = str_replace( '/' . $this->userId, '', $userHome );
 
                // remember files_trashbin state
-               $this->stateFilesTrashbin = OC_App::isEnabled('files_trashbin');
+               $this->stateFilesTrashbin = OC_App::isEnabled( 'files_trashbin' );
 
                // we don't want to tests with app files_trashbin enabled
-               \OC_App::disable('files_trashbin');
-
-               \OC_Util::tearDownFS();
-               \OC_User::setUserId('');
-               \OC\Files\Filesystem::tearDown();
-               \OC_Util::setupFS($this->userId);
-               \OC_User::setUserId($this->userId);
-
-               $params['uid'] = $this->userId;
-               $params['password'] = $this->pass;
-               OCA\Encryption\Hooks::login($params);
+               \OC_App::disable( 'files_trashbin' );
        }
 
-       function tearDown()
-       {
-
-               \OC_FileProxy::$enabled = true;
-               \OC_FileProxy::clearProxies();
-
+       function tearDown() {
                // reset app files_trashbin
-               if ($this->stateFilesTrashbin) {
-                       OC_App::enable('files_trashbin');
+               if ( $this->stateFilesTrashbin ) {
+                       OC_App::enable( 'files_trashbin' );
                } else {
-                       OC_App::disable('files_trashbin');
+                       OC_App::disable( 'files_trashbin' );
                }
        }
 
-       function testGetPrivateKey()
-       {
+       public static function tearDownAfterClass() {
+               \OC_FileProxy::$enabled = true;
+       }
+
+       function testGetPrivateKey() {
 
-               $key = Encryption\Keymanager::getPrivateKey($this->view, $this->userId);
+               $key = Encryption\Keymanager::getPrivateKey( $this->view, $this->userId );
 
-               $privateKey = Encryption\Crypt::symmetricDecryptFileContent($key, $this->pass);
+               $privateKey = Encryption\Crypt::symmetricDecryptFileContent( $key, $this->pass );
 
-               $res = openssl_pkey_get_private($privateKey);
+               $res = openssl_pkey_get_private( $privateKey );
 
-               $this->assertTrue(is_resource($res));
+               $this->assertTrue( is_resource( $res ) );
 
-               $sslInfo = openssl_pkey_get_details($res);
+               $sslInfo = openssl_pkey_get_details( $res );
 
-               $this->assertArrayHasKey('key', $sslInfo);
+               $this->assertArrayHasKey( 'key', $sslInfo );
 
        }
 
-       function testGetPublicKey()
-       {
+       function testGetPublicKey() {
 
-               $publiceKey = Encryption\Keymanager::getPublicKey($this->view, $this->userId);
+               $publiceKey = Encryption\Keymanager::getPublicKey( $this->view, $this->userId );
 
-               $res = openssl_pkey_get_public($publiceKey);
+               $res = openssl_pkey_get_public( $publiceKey );
 
-               $this->assertTrue(is_resource($res));
+               $this->assertTrue( is_resource( $res ) );
 
-               $sslInfo = openssl_pkey_get_details($res);
+               $sslInfo = openssl_pkey_get_details( $res );
 
-               $this->assertArrayHasKey('key', $sslInfo);
+               $this->assertArrayHasKey( 'key', $sslInfo );
        }
 
-       function testSetFileKey()
-       {
+       function testSetFileKey() {
 
                # NOTE: This cannot be tested until we are able to break out 
                # of the FileSystemView data directory root
 
-               $key = Encryption\Crypt::symmetricEncryptFileContentKeyfile($this->randomKey, 'hat');
+               $key = Encryption\Crypt::symmetricEncryptFileContentKeyfile( $this->randomKey, 'hat' );
 
                $file = 'unittest-' . time() . '.txt';
 
@@ -143,101 +145,98 @@ class Test_Encryption_Keymanager extends \PHPUnit_Framework_TestCase
                $proxyStatus = \OC_FileProxy::$enabled;
                \OC_FileProxy::$enabled = false;
 
-               $this->view->file_put_contents($this->userId . '/files/' . $file, $key['encrypted']);
+               $this->view->file_put_contents( $this->userId . '/files/' . $file, $key['encrypted'] );
 
                // Re-enable proxy - our work is done
                \OC_FileProxy::$enabled = $proxyStatus;
 
                //$view = new \OC_FilesystemView( '/' . $this->userId . '/files_encryption/keyfiles' );
-               Encryption\Keymanager::setFileKey($this->view, $file, $this->userId, $key['key']);
+               Encryption\Keymanager::setFileKey( $this->view, $file, $this->userId, $key['key'] );
 
                // enable encryption proxy
                $proxyStatus = \OC_FileProxy::$enabled;
                \OC_FileProxy::$enabled = true;
 
                // cleanup
-               $this->view->unlink('/' . $this->userId . '/files/' . $file);
+               $this->view->unlink( '/' . $this->userId . '/files/' . $file );
 
                // change encryption proxy to previous state
                \OC_FileProxy::$enabled = $proxyStatus;
 
        }
 
-       function testGetUserKeys()
-       {
+       function testGetUserKeys() {
 
-               $keys = Encryption\Keymanager::getUserKeys($this->view, $this->userId);
+               $keys = Encryption\Keymanager::getUserKeys( $this->view, $this->userId );
 
-               $resPublic = openssl_pkey_get_public($keys['publicKey']);
+               $resPublic = openssl_pkey_get_public( $keys['publicKey'] );
 
-               $this->assertTrue(is_resource($resPublic));
+               $this->assertTrue( is_resource( $resPublic ) );
 
-               $sslInfoPublic = openssl_pkey_get_details($resPublic);
+               $sslInfoPublic = openssl_pkey_get_details( $resPublic );
 
-               $this->assertArrayHasKey('key', $sslInfoPublic);
+               $this->assertArrayHasKey( 'key', $sslInfoPublic );
 
-               $privateKey = Encryption\Crypt::symmetricDecryptFileContent($keys['privateKey'], $this->pass);
+               $privateKey = Encryption\Crypt::symmetricDecryptFileContent( $keys['privateKey'], $this->pass );
 
-               $resPrivate = openssl_pkey_get_private($privateKey);
+               $resPrivate = openssl_pkey_get_private( $privateKey );
 
-               $this->assertTrue(is_resource($resPrivate));
+               $this->assertTrue( is_resource( $resPrivate ) );
 
-               $sslInfoPrivate = openssl_pkey_get_details($resPrivate);
+               $sslInfoPrivate = openssl_pkey_get_details( $resPrivate );
 
-               $this->assertArrayHasKey('key', $sslInfoPrivate);
+               $this->assertArrayHasKey( 'key', $sslInfoPrivate );
        }
 
-       function testFixPartialFilePath()
-       {
+       function testFixPartialFilePath() {
 
                $partFilename = 'testfile.txt.part';
                $filename = 'testfile.txt';
 
-               $this->assertTrue(Encryption\Keymanager::isPartialFilePath($partFilename));
+               $this->assertTrue( Encryption\Keymanager::isPartialFilePath( $partFilename ) );
 
-               $this->assertEquals('testfile.txt', Encryption\Keymanager::fixPartialFilePath($partFilename));
+               $this->assertEquals( 'testfile.txt', Encryption\Keymanager::fixPartialFilePath( $partFilename ) );
 
-               $this->assertFalse(Encryption\Keymanager::isPartialFilePath($filename));
+               $this->assertFalse( Encryption\Keymanager::isPartialFilePath( $filename ) );
 
-               $this->assertEquals('testfile.txt', Encryption\Keymanager::fixPartialFilePath($filename));
+               $this->assertEquals( 'testfile.txt', Encryption\Keymanager::fixPartialFilePath( $filename ) );
        }
 
-       function testRecursiveDelShareKeys()
-       {
+       function testRecursiveDelShareKeys() {
 
                // generate filename
                $filename = '/tmp-' . time() . '.txt';
 
                // create folder structure
-               $this->view->mkdir('/admin/files/folder1');
-               $this->view->mkdir('/admin/files/folder1/subfolder');
-               $this->view->mkdir('/admin/files/folder1/subfolder/subsubfolder');
+               $this->view->mkdir( '/admin/files/folder1' );
+               $this->view->mkdir( '/admin/files/folder1/subfolder' );
+               $this->view->mkdir( '/admin/files/folder1/subfolder/subsubfolder' );
 
                // enable encryption proxy
                $proxyStatus = \OC_FileProxy::$enabled;
                \OC_FileProxy::$enabled = true;
 
                // save file with content
-               $cryptedFile = file_put_contents('crypt:///folder1/subfolder/subsubfolder/' . $filename, $this->dataShort);
+               $cryptedFile = file_put_contents( 'crypt:///folder1/subfolder/subsubfolder/' . $filename, $this->dataShort );
 
                // test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_int( $cryptedFile ) );
 
                // change encryption proxy to previous state
                \OC_FileProxy::$enabled = $proxyStatus;
 
                // recursive delete keys
-               Encryption\Keymanager::delShareKey($this->view, array('admin'), '/folder1/');
+               Encryption\Keymanager::delShareKey( $this->view, array( 'admin' ), '/folder1/' );
 
                // check if share key not exists
-               $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/folder1/subfolder/subsubfolder/' . $filename . '.admin.shareKey'));
+               $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/folder1/subfolder/subsubfolder/' . $filename . '.admin.shareKey' ) );
 
                // enable encryption proxy
                $proxyStatus = \OC_FileProxy::$enabled;
                \OC_FileProxy::$enabled = true;
 
                // cleanup
-               $this->view->unlink('/admin/files/folder1');
+               $this->view->unlink( '/admin/files/folder1' );
 
                // change encryption proxy to previous state
                \OC_FileProxy::$enabled = $proxyStatus;
index 1d0cbfbc1def814a512a2f05a8481eac9b251898..0131ac88e72846c13908c661e81c8bb338a92b37 100755 (executable)
  *
  */
 
-require_once realpath(dirname(__FILE__) . '/../3rdparty/Crypt_Blowfish/Blowfish.php');
-require_once realpath(dirname(__FILE__) . '/../../../lib/base.php');
-require_once realpath(dirname(__FILE__) . '/../lib/crypt.php');
-require_once realpath(dirname(__FILE__) . '/../lib/keymanager.php');
-require_once realpath(dirname(__FILE__) . '/../lib/proxy.php');
-require_once realpath(dirname(__FILE__) . '/../lib/stream.php');
-require_once realpath(dirname(__FILE__) . '/../lib/util.php');
-require_once realpath(dirname(__FILE__) . '/../lib/helper.php');
-require_once realpath(dirname(__FILE__) . '/../appinfo/app.php');
+require_once realpath( dirname( __FILE__ ) . '/../3rdparty/Crypt_Blowfish/Blowfish.php' );
+require_once realpath( dirname( __FILE__ ) . '/../../../lib/base.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/crypt.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/keymanager.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/proxy.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/stream.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/util.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/helper.php' );
+require_once realpath( dirname( __FILE__ ) . '/../appinfo/app.php' );
 
 use OCA\Encryption;
 
@@ -49,31 +49,18 @@ class Test_Encryption_Share extends \PHPUnit_Framework_TestCase
        public $subfolder;
        public $subsubfolder;
 
-       function setUp()
-       {
+       public static function setUpBeforeClass() {
                // reset backend
                \OC_User::clearBackends();
-               \OC_User::useBackend('database');
-
-               $this->dataShort = 'hats';
-               $this->view = new \OC_FilesystemView('/');
-
-               $userHome = \OC_User::getHome('admin');
-               $this->dataDir = str_replace('/admin', '', $userHome);
-
-               $this->folder1 = '/folder1';
-               $this->subfolder = '/subfolder1';
-               $this->subsubfolder = '/subsubfolder1';
-
-               $this->filename = 'share-tmp.test';
+               \OC_User::useBackend( 'database' );
 
                // enable resharing
-               \OC_Appconfig::setValue('core', 'shareapi_allow_resharing', 'yes');
+               \OC_Appconfig::setValue( 'core', 'shareapi_allow_resharing', 'yes' );
 
                // clear share hooks
-               \OC_Hook::clear('OCP\\Share');
+               \OC_Hook::clear( 'OCP\\Share' );
                \OC::registerShareHooks();
-               \OCP\Util::connectHook('OC_Filesystem', 'setup', '\OC\Files\Storage\Shared', 'setup');
+               \OCP\Util::connectHook( 'OC_Filesystem', 'setup', '\OC\Files\Storage\Shared', 'setup' );
 
                // Sharing related hooks
                \OCA\Encryption\Helper::registerShareHooks();
@@ -81,170 +68,183 @@ class Test_Encryption_Share extends \PHPUnit_Framework_TestCase
                // Filesystem related hooks
                \OCA\Encryption\Helper::registerFilesystemHooks();
 
-               \OC_FileProxy::register(new OCA\Encryption\Proxy());
-
-               // 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');
+               // clear and register hooks
+               \OC_FileProxy::clearProxies();
+               \OC_FileProxy::register( new OCA\Encryption\Proxy() );
 
                // create users
-               $this->loginHelper('user1', true);
-               $this->loginHelper('user2', true);
-               $this->loginHelper('user3', true);
+               \Test_Encryption_Share::loginHelper( 'user1', true );
+               \Test_Encryption_Share::loginHelper( 'user2', true );
+               \Test_Encryption_Share::loginHelper( 'user3', true );
 
                // create group and assign users
-               \OC_Group::createGroup('group1');
-               \OC_Group::addToGroup('user2', 'group1');
-               \OC_Group::addToGroup('user3', 'group1');
+               \OC_Group::createGroup( 'group1' );
+               \OC_Group::addToGroup( 'user2', 'group1' );
+               \OC_Group::addToGroup( 'user3', 'group1' );
+       }
+
+       function setUp() {
+               $this->dataShort = 'hats';
+               $this->view = new \OC_FilesystemView( '/' );
+
+               $userHome = \OC_User::getHome( 'admin' );
+               $this->dataDir = str_replace( '/admin', '', $userHome );
+
+               $this->folder1 = '/folder1';
+               $this->subfolder = '/subfolder1';
+               $this->subsubfolder = '/subsubfolder1';
+
+               $this->filename = 'share-tmp.test';
+
+               // we don't want to tests with app files_trashbin enabled
+               \OC_App::disable( 'files_trashbin' );
+
+               // remember files_trashbin state
+               $this->stateFilesTrashbin = OC_App::isEnabled( 'files_trashbin' );
        }
 
-       function tearDown()
-       {
+       function tearDown() {
                // reset app files_trashbin
-               if ($this->stateFilesTrashbin) {
-                       OC_App::enable('files_trashbin');
+               if ( $this->stateFilesTrashbin ) {
+                       OC_App::enable( 'files_trashbin' );
                } else {
-                       OC_App::disable('files_trashbin');
+                       OC_App::disable( 'files_trashbin' );
                }
+       }
 
+       public static function tearDownAfterClass() {
                // clean group
-               \OC_Group::deleteGroup('group1');
+               \OC_Group::deleteGroup( 'group1' );
 
                // cleanup users
-               \OC_User::deleteUser('user1');
-               \OC_User::deleteUser('user2');
-               \OC_User::deleteUser('user3');
-
-               \OC_FileProxy::clearProxies();
+               \OC_User::deleteUser( 'user1' );
+               \OC_User::deleteUser( 'user2' );
+               \OC_User::deleteUser( 'user3' );
        }
 
        /**
         * @param bool $withTeardown
         */
-       function testShareFile($withTeardown = true)
-       {
+       function testShareFile( $withTeardown = true ) {
                // login as admin
-               $this->loginHelper('admin');
+               $this->loginHelper( 'admin' );
 
                // save file with content
-               $cryptedFile = file_put_contents('crypt://' . $this->filename, $this->dataShort);
+               $cryptedFile = file_put_contents( 'crypt://' . $this->filename, $this->dataShort );
 
                // test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_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('/admin/files/' . $this->filename);
+               $fileInfo = $this->view->getFileInfo( '/admin/files/' . $this->filename );
 
                // check if we have a valid file info
-               $this->assertTrue(is_array($fileInfo));
+               $this->assertTrue( is_array( $fileInfo ) );
 
                // check if the unencrypted file size is stored
-               $this->assertGreaterThan(0, $fileInfo['unencrypted_size']);
+               $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, 'user1', OCP\PERMISSION_ALL);
+               \OCP\Share::shareItem( 'file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user1', OCP\PERMISSION_ALL );
 
                // login as admin
-               $this->loginHelper('admin');
+               $this->loginHelper( 'admin' );
 
                // check if share key for user1 exists
-               $this->assertTrue($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.user1.shareKey'));
+               $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.user1.shareKey' ) );
 
                // login as user1
-               $this->loginHelper('user1');
+               $this->loginHelper( 'user1' );
 
                // get file contents
-               $retrievedCryptedFile = $this->view->file_get_contents('/user1/files/Shared/' . $this->filename);
+               $retrievedCryptedFile = $this->view->file_get_contents( '/user1/files/Shared/' . $this->filename );
 
                // check if data is the same as we previously written
-               $this->assertEquals($this->dataShort, $retrievedCryptedFile);
+               $this->assertEquals( $this->dataShort, $retrievedCryptedFile );
 
                // cleanup
-               if ($withTeardown) {
+               if ( $withTeardown ) {
 
                        // login as admin
-                       $this->loginHelper('admin');
+                       $this->loginHelper( 'admin' );
 
                        // unshare the file
-                       \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user1');
+                       \OCP\Share::unshare( 'file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user1' );
 
                        // check if share key not exists
-                       $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.user1.shareKey'));
+                       $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.user1.shareKey' ) );
 
                        // cleanup
-                       $this->view->unlink('/admin/files/' . $this->filename);
+                       $this->view->unlink( '/admin/files/' . $this->filename );
 
                        // check if share key not exists
-                       $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.admin.shareKey'));
+                       $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.admin.shareKey' ) );
                }
        }
 
        /**
         * @param bool $withTeardown
         */
-       function testReShareFile($withTeardown = true)
-       {
-               $this->testShareFile(false);
+       function testReShareFile( $withTeardown = true ) {
+               $this->testShareFile( false );
 
                // login as user1
-               $this->loginHelper('user1');
+               $this->loginHelper( 'user1' );
 
                // get the file info
-               $fileInfo = $this->view->getFileInfo('/user1/files/Shared/' . $this->filename);
+               $fileInfo = $this->view->getFileInfo( '/user1/files/Shared/' . $this->filename );
 
                // share the file with user2
-               \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user2', OCP\PERMISSION_ALL);
+               \OCP\Share::shareItem( 'file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user2', OCP\PERMISSION_ALL );
 
                // login as admin
-               $this->loginHelper('admin');
+               $this->loginHelper( 'admin' );
 
                // check if share key for user2 exists
-               $this->assertTrue($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.user2.shareKey'));
+               $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.user2.shareKey' ) );
 
                // login as user2
-               $this->loginHelper('user2');
+               $this->loginHelper( 'user2' );
 
                // get file contents
-               $retrievedCryptedFile = $this->view->file_get_contents('/user2/files/Shared/' . $this->filename);
+               $retrievedCryptedFile = $this->view->file_get_contents( '/user2/files/Shared/' . $this->filename );
 
                // check if data is the same as previously written
-               $this->assertEquals($this->dataShort, $retrievedCryptedFile);
+               $this->assertEquals( $this->dataShort, $retrievedCryptedFile );
 
                // cleanup
-               if ($withTeardown) {
+               if ( $withTeardown ) {
 
                        // login as user1
-                       $this->loginHelper('user1');
+                       $this->loginHelper( 'user1' );
 
                        // unshare the file with user2
-                       \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user2');
+                       \OCP\Share::unshare( 'file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user2' );
 
                        // login as admin
-                       $this->loginHelper('admin');
+                       $this->loginHelper( 'admin' );
 
                        // check if share key not exists
-                       $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.user2.shareKey'));
+                       $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.user2.shareKey' ) );
 
                        // unshare the file with user1
-                       \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user1');
+                       \OCP\Share::unshare( 'file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user1' );
 
                        // check if share key not exists
-                       $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.user1.shareKey'));
+                       $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.user1.shareKey' ) );
 
                        // cleanup
-                       $this->view->unlink('/admin/files/' . $this->filename);
+                       $this->view->unlink( '/admin/files/' . $this->filename );
 
                        // check if share key not exists
-                       $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.admin.shareKey'));
+                       $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.admin.shareKey' ) );
                }
        }
 
@@ -252,70 +252,69 @@ class Test_Encryption_Share extends \PHPUnit_Framework_TestCase
         * @param bool $withTeardown
         * @return array
         */
-       function testShareFolder($withTeardown = true)
-       {
+       function testShareFolder( $withTeardown = true ) {
                // login as admin
-               $this->loginHelper('admin');
+               $this->loginHelper( 'admin' );
 
                // create folder structure
-               $this->view->mkdir('/admin/files' . $this->folder1);
-               $this->view->mkdir('/admin/files' . $this->folder1 . $this->subfolder);
-               $this->view->mkdir('/admin/files' . $this->folder1 . $this->subfolder . $this->subsubfolder);
+               $this->view->mkdir( '/admin/files' . $this->folder1 );
+               $this->view->mkdir( '/admin/files' . $this->folder1 . $this->subfolder );
+               $this->view->mkdir( '/admin/files' . $this->folder1 . $this->subfolder . $this->subsubfolder );
 
                // save file with content
-               $cryptedFile = file_put_contents('crypt://' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename, $this->dataShort);
+               $cryptedFile = file_put_contents( 'crypt://' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename, $this->dataShort );
 
                // test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_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('/admin/files' . $this->folder1);
+               $fileInfo = $this->view->getFileInfo( '/admin/files' . $this->folder1 );
 
                // check if we have a valid file info
-               $this->assertTrue(is_array($fileInfo));
+               $this->assertTrue( is_array( $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, 'user1', OCP\PERMISSION_ALL);
+               \OCP\Share::shareItem( 'folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user1', OCP\PERMISSION_ALL );
 
                // login as admin
-               $this->loginHelper('admin');
+               $this->loginHelper( 'admin' );
 
                // check if share key for user1 exists
-               $this->assertTrue($this->view->file_exists('/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.user1.shareKey'));
+               $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.user1.shareKey' ) );
 
                // login as user1
-               $this->loginHelper('user1');
+               $this->loginHelper( 'user1' );
 
                // get file contents
-               $retrievedCryptedFile = $this->view->file_get_contents('/user1/files/Shared' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename);
+               $retrievedCryptedFile = $this->view->file_get_contents( '/user1/files/Shared' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename );
 
                // check if data is the same
-               $this->assertEquals($this->dataShort, $retrievedCryptedFile);
+               $this->assertEquals( $this->dataShort, $retrievedCryptedFile );
 
                // cleanup
-               if ($withTeardown) {
+               if ( $withTeardown ) {
 
                        // login as admin
-                       $this->loginHelper('admin');
+                       $this->loginHelper( 'admin' );
 
                        // unshare the folder with user1
-                       \OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user1');
+                       \OCP\Share::unshare( 'folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user1' );
 
                        // check if share key not exists
-                       $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.user1.shareKey'));
+                       $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.user1.shareKey' ) );
 
                        // cleanup
-                       $this->view->unlink('/admin/files' . $this->folder1);
+                       $this->view->unlink( '/admin/files' . $this->folder1 );
 
                        // check if share key not exists
-                       $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.admin.shareKey'));
+                       $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.admin.shareKey' ) );
                }
 
                return $fileInfo;
@@ -324,467 +323,462 @@ class Test_Encryption_Share extends \PHPUnit_Framework_TestCase
        /**
         * @param bool $withTeardown
         */
-       function testReShareFolder($withTeardown = true)
-       {
-               $fileInfoFolder1 = $this->testShareFolder(false);
+       function testReShareFolder( $withTeardown = true ) {
+               $fileInfoFolder1 = $this->testShareFolder( false );
 
                // login as user1
-               $this->loginHelper('user1');
+               $this->loginHelper( 'user1' );
 
                // 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('/user1/files/Shared' . $this->folder1 . $this->subfolder);
+               $fileInfoSubFolder = $this->view->getFileInfo( '/user1/files/Shared' . $this->folder1 . $this->subfolder );
 
                // check if we have a valid file info
-               $this->assertTrue(is_array($fileInfoSubFolder));
+               $this->assertTrue( is_array( $fileInfoSubFolder ) );
 
                // re-enable the file proxy
                \OC_FileProxy::$enabled = $proxyStatus;
 
                // share the file with user2
-               \OCP\Share::shareItem('folder', $fileInfoSubFolder['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user2', OCP\PERMISSION_ALL);
+               \OCP\Share::shareItem( 'folder', $fileInfoSubFolder['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user2', OCP\PERMISSION_ALL );
 
                // login as admin
-               $this->loginHelper('admin');
+               $this->loginHelper( 'admin' );
 
                // check if share key for user2 exists
-               $this->assertTrue($this->view->file_exists('/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.user2.shareKey'));
+               $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.user2.shareKey' ) );
 
                // login as user2
-               $this->loginHelper('user2');
+               $this->loginHelper( 'user2' );
 
                // get file contents
-               $retrievedCryptedFile = $this->view->file_get_contents('/user2/files/Shared' . $this->subfolder . $this->subsubfolder . '/' . $this->filename);
+               $retrievedCryptedFile = $this->view->file_get_contents( '/user2/files/Shared' . $this->subfolder . $this->subsubfolder . '/' . $this->filename );
 
                // check if data is the same
-               $this->assertEquals($this->dataShort, $retrievedCryptedFile);
+               $this->assertEquals( $this->dataShort, $retrievedCryptedFile );
 
                // get the file info
-               $fileInfo = $this->view->getFileInfo('/user2/files/Shared' . $this->subfolder . $this->subsubfolder . '/' . $this->filename);
+               $fileInfo = $this->view->getFileInfo( '/user2/files/Shared' . $this->subfolder . $this->subsubfolder . '/' . $this->filename );
 
                // check if we have fileInfos
-               $this->assertTrue(is_array($fileInfo));
+               $this->assertTrue( is_array( $fileInfo ) );
 
                // share the file with user3
-               \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user3', OCP\PERMISSION_ALL);
+               \OCP\Share::shareItem( 'file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user3', OCP\PERMISSION_ALL );
 
                // login as admin
-               $this->loginHelper('admin');
+               $this->loginHelper( 'admin' );
 
                // check if share key for user3 exists
-               $this->assertTrue($this->view->file_exists('/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.user3.shareKey'));
+               $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.user3.shareKey' ) );
 
                // login as user3
-               $this->loginHelper('user3');
+               $this->loginHelper( 'user3' );
 
                // get file contents
-               $retrievedCryptedFile = $this->view->file_get_contents('/user3/files/Shared/' . $this->filename);
+               $retrievedCryptedFile = $this->view->file_get_contents( '/user3/files/Shared/' . $this->filename );
 
                // check if data is the same
-               $this->assertEquals($this->dataShort, $retrievedCryptedFile);
+               $this->assertEquals( $this->dataShort, $retrievedCryptedFile );
 
                // cleanup
-               if ($withTeardown) {
+               if ( $withTeardown ) {
 
                        // login as user2
-                       $this->loginHelper('user2');
+                       $this->loginHelper( 'user2' );
 
                        // unshare the file with user3
-                       \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user3');
+                       \OCP\Share::unshare( 'file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user3' );
 
                        // check if share key not exists
-                       $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.user3.shareKey'));
+                       $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.user3.shareKey' ) );
 
                        // login as user1
-                       $this->loginHelper('user1');
+                       $this->loginHelper( 'user1' );
 
                        // unshare the folder with user2
-                       \OCP\Share::unshare('folder', $fileInfoSubFolder['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user2');
+                       \OCP\Share::unshare( 'folder', $fileInfoSubFolder['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user2' );
 
                        // check if share key not exists
-                       $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.user2.shareKey'));
+                       $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.user2.shareKey' ) );
 
                        // login as admin
-                       $this->loginHelper('admin');
+                       $this->loginHelper( 'admin' );
 
                        // unshare the folder1 with user1
-                       \OCP\Share::unshare('folder', $fileInfoFolder1['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user1');
+                       \OCP\Share::unshare( 'folder', $fileInfoFolder1['fileid'], \OCP\Share::SHARE_TYPE_USER, 'user1' );
 
                        // check if share key not exists
-                       $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.user1.shareKey'));
+                       $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.user1.shareKey' ) );
 
                        // cleanup
-                       $this->view->unlink('/admin/files' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename);
+                       $this->view->unlink( '/admin/files' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename );
 
                        // check if share key not exists
-                       $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.admin.shareKey'));
+                       $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.admin.shareKey' ) );
                }
        }
 
-       function testPublicShareFile()
-       {
+       function testPublicShareFile() {
                // login as admin
-               $this->loginHelper('admin');
+               $this->loginHelper( 'admin' );
 
                // save file with content
-               $cryptedFile = file_put_contents('crypt://' . $this->filename, $this->dataShort);
+               $cryptedFile = file_put_contents( 'crypt://' . $this->filename, $this->dataShort );
 
                // test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_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('/admin/files/' . $this->filename);
+               $fileInfo = $this->view->getFileInfo( '/admin/files/' . $this->filename );
 
                // check if we have a valid file info
-               $this->assertTrue(is_array($fileInfo));
+               $this->assertTrue( is_array( $fileInfo ) );
 
                // check if the unencrypted file size is stored
-               $this->assertGreaterThan(0, $fileInfo['unencrypted_size']);
+               $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\PERMISSION_ALL);
+               \OCP\Share::shareItem( 'file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, false, OCP\PERMISSION_ALL );
 
                // login as admin
-               $this->loginHelper('admin');
+               $this->loginHelper( 'admin' );
 
-               $publicShareKeyId = \OC_Appconfig::getValue('files_encryption', 'publicShareKeyId');
+               $publicShareKeyId = \OC_Appconfig::getValue( 'files_encryption', 'publicShareKeyId' );
 
                // check if share key for public exists
-               $this->assertTrue($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.' . $publicShareKeyId . '.shareKey'));
+               $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.' . $publicShareKeyId . '.shareKey' ) );
 
                // some hacking to simulate public link
                $GLOBALS['app'] = 'files_sharing';
                $GLOBALS['fileOwner'] = 'admin';
-               \OC_User::setUserId('');
+               \OC_User::setUserId( '' );
 
                // get file contents
-               $retrievedCryptedFile = file_get_contents('crypt://' . $this->filename);
+               $retrievedCryptedFile = file_get_contents( 'crypt://' . $this->filename );
 
                // check if data is the same as we previously written
-               $this->assertEquals($this->dataShort, $retrievedCryptedFile);
+               $this->assertEquals( $this->dataShort, $retrievedCryptedFile );
 
                // tear down
 
                // login as admin
-               $this->loginHelper('admin');
+               $this->loginHelper( 'admin' );
 
                // unshare the file
-               \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
+               \OCP\Share::unshare( 'file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null );
 
                // check if share key not exists
-               $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.' . $publicShareKeyId . '.shareKey'));
+               $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.' . $publicShareKeyId . '.shareKey' ) );
 
                // cleanup
-               $this->view->unlink('/admin/files/' . $this->filename);
+               $this->view->unlink( '/admin/files/' . $this->filename );
 
                // check if share key not exists
-               $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.admin.shareKey'));
+               $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.admin.shareKey' ) );
        }
 
-       function testShareFileWithGroup()
-       {
+       function testShareFileWithGroup() {
                // login as admin
-               $this->loginHelper('admin');
+               $this->loginHelper( 'admin' );
 
                // save file with content
-               $cryptedFile = file_put_contents('crypt://' . $this->filename, $this->dataShort);
+               $cryptedFile = file_put_contents( 'crypt://' . $this->filename, $this->dataShort );
 
                // test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_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('/admin/files/' . $this->filename);
+               $fileInfo = $this->view->getFileInfo( '/admin/files/' . $this->filename );
 
                // check if we have a valid file info
-               $this->assertTrue(is_array($fileInfo));
+               $this->assertTrue( is_array( $fileInfo ) );
 
                // check if the unencrypted file size is stored
-               $this->assertGreaterThan(0, $fileInfo['unencrypted_size']);
+               $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, 'group1', OCP\PERMISSION_ALL);
+               \OCP\Share::shareItem( 'file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, 'group1', OCP\PERMISSION_ALL );
 
                // login as admin
-               $this->loginHelper('admin');
+               $this->loginHelper( 'admin' );
 
                // check if share key for user2 and user3 exists
-               $this->assertTrue($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.user2.shareKey'));
-               $this->assertTrue($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.user3.shareKey'));
+               $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.user2.shareKey' ) );
+               $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.user3.shareKey' ) );
 
                // login as user1
-               $this->loginHelper('user2');
+               $this->loginHelper( 'user2' );
 
                // get file contents
-               $retrievedCryptedFile = $this->view->file_get_contents('/user2/files/Shared/' . $this->filename);
+               $retrievedCryptedFile = $this->view->file_get_contents( '/user2/files/Shared/' . $this->filename );
 
                // check if data is the same as we previously written
-               $this->assertEquals($this->dataShort, $retrievedCryptedFile);
+               $this->assertEquals( $this->dataShort, $retrievedCryptedFile );
 
                // login as admin
-               $this->loginHelper('admin');
+               $this->loginHelper( 'admin' );
 
                // unshare the file
-               \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, 'group1');
+               \OCP\Share::unshare( 'file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, 'group1' );
 
                // check if share key not exists
-               $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.user2.shareKey'));
-               $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.user3.shareKey'));
+               $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.user2.shareKey' ) );
+               $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.user3.shareKey' ) );
 
                // cleanup
-               $this->view->unlink('/admin/files/' . $this->filename);
+               $this->view->unlink( '/admin/files/' . $this->filename );
 
                // check if share key not exists
-               $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.admin.shareKey'));
+               $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.admin.shareKey' ) );
 
        }
 
-       function testRecoveryFile()
-       {
-               \OCA\Encryption\Helper::adminEnableRecovery(null, 'test123');
-               $recoveryKeyId = OC_Appconfig::getValue('files_encryption', 'recoveryKeyId');
+       function testRecoveryFile() {
+               // login as admin
+               $this->loginHelper( 'admin' );
+
+               \OCA\Encryption\Helper::adminEnableRecovery( null, 'test123' );
+               $recoveryKeyId = OC_Appconfig::getValue( 'files_encryption', 'recoveryKeyId' );
 
                // check if control file created
-               $this->assertTrue($this->view->file_exists('/control-file/controlfile.enc'));
+               $this->assertTrue( $this->view->file_exists( '/control-file/controlfile.enc' ) );
 
                // login as admin
-               $this->loginHelper('admin');
+               $this->loginHelper( 'admin' );
 
-               $util = new \OCA\Encryption\Util(new \OC_FilesystemView('/'), 'admin');
+               $util = new \OCA\Encryption\Util( new \OC_FilesystemView( '/' ), 'admin' );
 
                // check if recovery password match
-               $this->assertTrue($util->checkRecoveryPassword('test123'));
+               $this->assertTrue( $util->checkRecoveryPassword( 'test123' ) );
 
                // enable recovery for admin
-               $this->assertTrue($util->setRecoveryForUser(1));
+               $this->assertTrue( $util->setRecoveryForUser( 1 ) );
 
                // create folder structure
-               $this->view->mkdir('/admin/files' . $this->folder1);
-               $this->view->mkdir('/admin/files' . $this->folder1 . $this->subfolder);
-               $this->view->mkdir('/admin/files' . $this->folder1 . $this->subfolder . $this->subsubfolder);
+               $this->view->mkdir( '/admin/files' . $this->folder1 );
+               $this->view->mkdir( '/admin/files' . $this->folder1 . $this->subfolder );
+               $this->view->mkdir( '/admin/files' . $this->folder1 . $this->subfolder . $this->subsubfolder );
 
                // save file with content
-               $cryptedFile1 = file_put_contents('crypt://' . $this->filename, $this->dataShort);
-               $cryptedFile2 = file_put_contents('crypt://' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename, $this->dataShort);
+               $cryptedFile1 = file_put_contents( 'crypt://' . $this->filename, $this->dataShort );
+               $cryptedFile2 = file_put_contents( 'crypt://' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename, $this->dataShort );
 
                // test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile1));
-               $this->assertTrue(is_int($cryptedFile2));
+               $this->assertTrue( is_int( $cryptedFile1 ) );
+               $this->assertTrue( is_int( $cryptedFile2 ) );
 
                // check if share key for admin and recovery exists
-               $this->assertTrue($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.admin.shareKey'));
-               $this->assertTrue($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
-               $this->assertTrue($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.admin.shareKey'));
-               $this->assertTrue($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
+               $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.admin.shareKey' ) );
+               $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.' . $recoveryKeyId . '.shareKey' ) );
+               $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.admin.shareKey' ) );
+               $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.' . $recoveryKeyId . '.shareKey' ) );
 
                // disable recovery for admin
-               $this->assertTrue($util->setRecoveryForUser(0));
+               $this->assertTrue( $util->setRecoveryForUser( 0 ) );
 
                // remove all recovery keys
-               $util->removeRecoveryKeys('/');
+               $util->removeRecoveryKeys( '/' );
 
                // check if share key for recovery not exists
-               $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
-               $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
+               $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.' . $recoveryKeyId . '.shareKey' ) );
+               $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.' . $recoveryKeyId . '.shareKey' ) );
 
                // enable recovery for admin
-               $this->assertTrue($util->setRecoveryForUser(1));
+               $this->assertTrue( $util->setRecoveryForUser( 1 ) );
 
                // remove all recovery keys
-               $util->addRecoveryKeys('/');
+               $util->addRecoveryKeys( '/' );
 
                // check if share key for admin and recovery exists
-               $this->assertTrue($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
-               $this->assertTrue($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
+               $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.' . $recoveryKeyId . '.shareKey' ) );
+               $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.' . $recoveryKeyId . '.shareKey' ) );
 
                // cleanup
-               $this->view->unlink('/admin/files/' . $this->filename);
-               $this->view->unlink('/admin/files/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename);
+               $this->view->unlink( '/admin/files/' . $this->filename );
+               $this->view->unlink( '/admin/files/' . $this->folder1 );
 
                // check if share key for recovery not exists
-               $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
-               $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
+               $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.' . $recoveryKeyId . '.shareKey' ) );
+               $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.' . $recoveryKeyId . '.shareKey' ) );
 
-               $this->assertTrue(\OCA\Encryption\Helper::adminEnableRecovery(null, 'test123'));
-               $this->assertTrue(\OCA\Encryption\Helper::adminDisableRecovery('test123'));
-               $this->assertEquals(0, \OC_Appconfig::getValue('files_encryption', 'recoveryAdminEnabled'));
+               $this->assertTrue( \OCA\Encryption\Helper::adminEnableRecovery( null, 'test123' ) );
+               $this->assertTrue( \OCA\Encryption\Helper::adminDisableRecovery( 'test123' ) );
+               $this->assertEquals( 0, \OC_Appconfig::getValue( 'files_encryption', 'recoveryAdminEnabled' ) );
        }
 
-       function testRecoveryForUser()
-       {
+       function testRecoveryForUser() {
                // login as admin
-               $this->loginHelper('admin');
+               $this->loginHelper( 'admin' );
 
-               \OCA\Encryption\Helper::adminEnableRecovery(null, 'test123');
-               $recoveryKeyId = OC_Appconfig::getValue('files_encryption', 'recoveryKeyId');
+               \OCA\Encryption\Helper::adminEnableRecovery( null, 'test123' );
+               $recoveryKeyId = OC_Appconfig::getValue( 'files_encryption', 'recoveryKeyId' );
 
                // check if control file created
-               $this->assertTrue($this->view->file_exists('/control-file/controlfile.enc'));
+               $this->assertTrue( $this->view->file_exists( '/control-file/controlfile.enc' ) );
 
                // login as user1
-               $this->loginHelper('user1');
+               $this->loginHelper( 'user1' );
 
-               $util = new \OCA\Encryption\Util(new \OC_FilesystemView('/'), 'user1');
+               $util = new \OCA\Encryption\Util( new \OC_FilesystemView( '/' ), 'user1' );
 
                // enable recovery for admin
-               $this->assertTrue($util->setRecoveryForUser(1));
+               $this->assertTrue( $util->setRecoveryForUser( 1 ) );
 
                // create folder structure
-               $this->view->mkdir('/user1/files' . $this->folder1);
-               $this->view->mkdir('/user1/files' . $this->folder1 . $this->subfolder);
-               $this->view->mkdir('/user1/files' . $this->folder1 . $this->subfolder . $this->subsubfolder);
+               $this->view->mkdir( '/user1/files' . $this->folder1 );
+               $this->view->mkdir( '/user1/files' . $this->folder1 . $this->subfolder );
+               $this->view->mkdir( '/user1/files' . $this->folder1 . $this->subfolder . $this->subsubfolder );
 
                // save file with content
-               $cryptedFile1 = file_put_contents('crypt://' . $this->filename, $this->dataShort);
-               $cryptedFile2 = file_put_contents('crypt://' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename, $this->dataShort);
+               $cryptedFile1 = file_put_contents( 'crypt://' . $this->filename, $this->dataShort );
+               $cryptedFile2 = file_put_contents( 'crypt://' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename, $this->dataShort );
 
                // test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile1));
-               $this->assertTrue(is_int($cryptedFile2));
+               $this->assertTrue( is_int( $cryptedFile1 ) );
+               $this->assertTrue( is_int( $cryptedFile2 ) );
 
                // check if share key for user and recovery exists
-               $this->assertTrue($this->view->file_exists('/user1/files_encryption/share-keys/' . $this->filename . '.user1.shareKey'));
-               $this->assertTrue($this->view->file_exists('/user1/files_encryption/share-keys/' . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
-               $this->assertTrue($this->view->file_exists('/user1/files_encryption/share-keys/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.user1.shareKey'));
-               $this->assertTrue($this->view->file_exists('/user1/files_encryption/share-keys/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
+               $this->assertTrue( $this->view->file_exists( '/user1/files_encryption/share-keys/' . $this->filename . '.user1.shareKey' ) );
+               $this->assertTrue( $this->view->file_exists( '/user1/files_encryption/share-keys/' . $this->filename . '.' . $recoveryKeyId . '.shareKey' ) );
+               $this->assertTrue( $this->view->file_exists( '/user1/files_encryption/share-keys/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.user1.shareKey' ) );
+               $this->assertTrue( $this->view->file_exists( '/user1/files_encryption/share-keys/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.' . $recoveryKeyId . '.shareKey' ) );
 
                // login as admin
-               $this->loginHelper('admin');
+               $this->loginHelper( 'admin' );
 
                // change password
-               \OC_User::setPassword('user1', 'test', 'test123');
+               \OC_User::setPassword( 'user1', 'test', 'test123' );
 
                // login as user1
-               $this->loginHelper('user1', false, 'test');
+               $this->loginHelper( 'user1', false, 'test' );
 
                // get file contents
-               $retrievedCryptedFile1 = file_get_contents('crypt://' . $this->filename);
-               $retrievedCryptedFile2 = file_get_contents('crypt://' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename);
+               $retrievedCryptedFile1 = file_get_contents( 'crypt://' . $this->filename );
+               $retrievedCryptedFile2 = file_get_contents( 'crypt://' . $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);
+               $this->assertEquals( $this->dataShort, $retrievedCryptedFile1 );
+               $this->assertEquals( $this->dataShort, $retrievedCryptedFile2 );
 
                // cleanup
-               $this->view->unlink('/user1/files' . $this->folder1);
-               $this->view->unlink('/user1/files' . $this->filename);
+               $this->view->unlink( '/user1/files' . $this->folder1 );
+               $this->view->unlink( '/user1/files' . $this->filename );
 
                // check if share key for user and recovery exists
-               $this->assertFalse($this->view->file_exists('/user1/files_encryption/share-keys/' . $this->filename . '.user1.shareKey'));
-               $this->assertFalse($this->view->file_exists('/user1/files_encryption/share-keys/' . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
-               $this->assertFalse($this->view->file_exists('/user1/files_encryption/share-keys/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.user1.shareKey'));
-               $this->assertFalse($this->view->file_exists('/user1/files_encryption/share-keys/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
+               $this->assertFalse( $this->view->file_exists( '/user1/files_encryption/share-keys/' . $this->filename . '.user1.shareKey' ) );
+               $this->assertFalse( $this->view->file_exists( '/user1/files_encryption/share-keys/' . $this->filename . '.' . $recoveryKeyId . '.shareKey' ) );
+               $this->assertFalse( $this->view->file_exists( '/user1/files_encryption/share-keys/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.user1.shareKey' ) );
+               $this->assertFalse( $this->view->file_exists( '/user1/files_encryption/share-keys/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.' . $recoveryKeyId . '.shareKey' ) );
 
                // enable recovery for admin
-               $this->assertTrue($util->setRecoveryForUser(0));
+               $this->assertTrue( $util->setRecoveryForUser( 0 ) );
 
-               \OCA\Encryption\Helper::adminDisableRecovery('test123');
-               $this->assertEquals(0, \OC_Appconfig::getValue('files_encryption', 'recoveryAdminEnabled'));
+               \OCA\Encryption\Helper::adminDisableRecovery( 'test123' );
+               $this->assertEquals( 0, \OC_Appconfig::getValue( 'files_encryption', 'recoveryAdminEnabled' ) );
        }
 
-       function testFailShareFile()
-       {
+       function testFailShareFile() {
                // login as admin
-               $this->loginHelper('admin');
+               $this->loginHelper( 'admin' );
 
                // save file with content
-               $cryptedFile = file_put_contents('crypt://' . $this->filename, $this->dataShort);
+               $cryptedFile = file_put_contents( 'crypt://' . $this->filename, $this->dataShort );
 
                // test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_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('/admin/files/' . $this->filename);
+               $fileInfo = $this->view->getFileInfo( '/admin/files/' . $this->filename );
 
                // check if we have a valid file info
-               $this->assertTrue(is_array($fileInfo));
+               $this->assertTrue( is_array( $fileInfo ) );
 
                // check if the unencrypted file size is stored
-               $this->assertGreaterThan(0, $fileInfo['unencrypted_size']);
+               $this->assertGreaterThan( 0, $fileInfo['unencrypted_size'] );
 
                // break users public key
-               $this->view->rename('/public-keys/user2.public.key', '/public-keys/user2.public.key_backup');
+               $this->view->rename( '/public-keys/user2.public.key', '/public-keys/user2.public.key_backup' );
 
                // re-enable the file proxy
                \OC_FileProxy::$enabled = $proxyStatus;
 
                // share the file
-               \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, 'group1', OCP\PERMISSION_ALL);
+               \OCP\Share::shareItem( 'file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, 'group1', OCP\PERMISSION_ALL );
 
                // login as admin
-               $this->loginHelper('admin');
+               $this->loginHelper( 'admin' );
 
                // check if share key for user1 not exists
-               $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.user2.shareKey'));
+               $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.user2.shareKey' ) );
 
                // disable encryption proxy to prevent recursive calls
                $proxyStatus = \OC_FileProxy::$enabled;
                \OC_FileProxy::$enabled = false;
 
                // break user1 public key
-               $this->view->rename('/public-keys/user2.public.key_backup', '/public-keys/user2.public.key');
+               $this->view->rename( '/public-keys/user2.public.key_backup', '/public-keys/user2.public.key' );
 
                // remove share file
-               $this->view->unlink('/admin/files_encryption/share-keys/' . $this->filename . '.user2.shareKey');
+               $this->view->unlink( '/admin/files_encryption/share-keys/' . $this->filename . '.user2.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, 'group1');
+               \OCP\Share::unshare( 'file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, 'group1' );
 
                // check if share key not exists
-               $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.user2.shareKey'));
+               $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $this->filename . '.user2.shareKey' ) );
 
                // cleanup
-               $this->view->unlink('/admin/files/' . $this->filename);
+               $this->view->unlink( '/admin/files/' . $this->filename );
        }
 
 
-
        /**
         * @param $user
         * @param bool $create
         * @param bool $password
         */
-       function loginHelper($user, $create = false, $password = false)
-       {
-               if ($create) {
-                       \OC_User::createUser($user, $user);
+       public static function loginHelper( $user, $create = false, $password = false ) {
+               if ( $create ) {
+                       \OC_User::createUser( $user, $user );
                }
 
-               if ($password === false) {
+               if ( $password === false ) {
                        $password = $user;
                }
 
                \OC_Util::tearDownFS();
-               \OC_User::setUserId('');
+               \OC_User::setUserId( '' );
                \OC\Files\Filesystem::tearDown();
-               \OC_Util::setupFS($user);
-               \OC_User::setUserId($user);
+               \OC_Util::setupFS( $user );
+               \OC_User::setUserId( $user );
 
                $params['uid'] = $user;
                $params['password'] = $password;
-               OCA\Encryption\Hooks::login($params);
+               OCA\Encryption\Hooks::login( $params );
        }
 }
index 3765d986e12c22d537ed416666617737352f725a..59b310c7ac88518406baefd4aac517fe8798e4ea 100644 (file)
  *
  */
 
-require_once realpath(dirname(__FILE__) . '/../../../lib/base.php');
-require_once realpath(dirname(__FILE__) . '/../lib/crypt.php');
-require_once realpath(dirname(__FILE__) . '/../lib/keymanager.php');
-require_once realpath(dirname(__FILE__) . '/../lib/proxy.php');
-require_once realpath(dirname(__FILE__) . '/../lib/stream.php');
-require_once realpath(dirname(__FILE__) . '/../lib/util.php');
-require_once realpath(dirname(__FILE__) . '/../appinfo/app.php');
+require_once realpath( dirname( __FILE__ ) . '/../../../lib/base.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/crypt.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/keymanager.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/proxy.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/stream.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/util.php' );
+require_once realpath( dirname( __FILE__ ) . '/../appinfo/app.php' );
 
 use OCA\Encryption;
 
@@ -46,137 +46,141 @@ class Test_Encryption_Stream extends \PHPUnit_Framework_TestCase
        public $dataShort;
        public $stateFilesTrashbin;
 
-       function setUp()
-       {
+       public static function setUpBeforeClass() {
                // reset backend
-               \OC_User::useBackend('database');
+               \OC_User::clearBackends();
+               \OC_User::useBackend( 'database' );
 
+               // Filesystem related hooks
+               \OCA\Encryption\Helper::registerFilesystemHooks();
+
+               // clear and register hooks
+               \OC_FileProxy::clearProxies();
+               \OC_FileProxy::register( new OCA\Encryption\Proxy() );
+
+               // setup filesystem
+               \OC_Util::tearDownFS();
+               \OC_User::setUserId( '' );
+               \OC\Files\Filesystem::tearDown();
+               \OC_Util::setupFS( 'admin' );
+               \OC_User::setUserId( 'admin' );
+
+               // login admin
+               $params['uid'] = 'admin';
+               $params['password'] = 'admin';
+               OCA\Encryption\Hooks::login( $params );
+
+       }
+
+       function setUp() {
                // set user id
-               \OC_User::setUserId('admin');
+               \OC_User::setUserId( 'admin' );
                $this->userId = 'admin';
                $this->pass = 'admin';
 
                // init filesystem view
-               $this->view = new \OC_FilesystemView('/');
+               $this->view = new \OC_FilesystemView( '/' );
 
                // init short data
                $this->dataShort = 'hats';
 
-               // init filesystem related hooks
-               \OCA\Encryption\Helper::registerFilesystemHooks();
-
-               // register encryption file proxy
-               \OC_FileProxy::register(new OCA\Encryption\Proxy());
-
                // remember files_trashbin state
-               $this->stateFilesTrashbin = OC_App::isEnabled('files_trashbin');
+               $this->stateFilesTrashbin = OC_App::isEnabled( 'files_trashbin' );
 
                // we don't want to tests with app files_trashbin enabled
-               \OC_App::disable('files_trashbin');
-
-               // init filesystem for user
-               \OC_Util::tearDownFS();
-               \OC_User::setUserId('');
-               \OC\Files\Filesystem::tearDown();
-               \OC_Util::setupFS($this->userId);
-               \OC_User::setUserId($this->userId);
-
-               // login user
-               $params['uid'] = $this->userId;
-               $params['password'] = $this->pass;
-               OCA\Encryption\Hooks::login($params);
+               \OC_App::disable( 'files_trashbin' );
        }
 
-       function tearDown()
-       {
+       function tearDown() {
                // reset app files_trashbin
-               if ($this->stateFilesTrashbin) {
-                       OC_App::enable('files_trashbin');
+               if ( $this->stateFilesTrashbin ) {
+                       OC_App::enable( 'files_trashbin' );
                } else {
-                       OC_App::disable('files_trashbin');
+                       OC_App::disable( 'files_trashbin' );
                }
+       }
+
+       public static function tearDownAfterClass() {
 
-               // clear all proxies
-               \OC_FileProxy::clearProxies();
        }
 
        function testStreamOptions() {
                $filename = '/tmp-' . time();
-               $view = new \OC\Files\View('/' . $this->userId . '/files');
+               $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);
+               $cryptedFile = $view->file_put_contents( $filename, $this->dataShort );
 
                // Test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_int( $cryptedFile ) );
 
-               $handle = $view->fopen($filename, 'r');
+               $handle = $view->fopen( $filename, 'r' );
 
                // check if stream is at position zero
-               $this->assertEquals(0,ftell($handle));
+               $this->assertEquals( 0, ftell( $handle ) );
 
                // set stream options
-               $this->assertTrue(flock($handle, LOCK_SH));
-               $this->assertTrue(flock($handle, LOCK_UN));
+               $this->assertTrue( flock( $handle, LOCK_SH ) );
+               $this->assertTrue( flock( $handle, LOCK_UN ) );
 
                // tear down
-               $view->unlink($filename);
+               $view->unlink( $filename );
        }
 
        function testStreamSetBlocking() {
                $filename = '/tmp-' . time();
-               $view = new \OC\Files\View('/' . $this->userId . '/files');
+               $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);
+               $cryptedFile = $view->file_put_contents( $filename, $this->dataShort );
 
                // Test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_int( $cryptedFile ) );
 
-               $handle = $view->fopen($filename, 'r');
+               $handle = $view->fopen( $filename, 'r' );
 
                // set stream options
-               $this->assertTrue(stream_set_blocking($handle,1));
+               $this->assertTrue( stream_set_blocking( $handle, 1 ) );
 
                // tear down
-               $view->unlink($filename);
+               $view->unlink( $filename );
        }
 
        function testStreamSetTimeout() {
                $filename = '/tmp-' . time();
-               $view = new \OC\Files\View('/' . $this->userId . '/files');
+               $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);
+               $cryptedFile = $view->file_put_contents( $filename, $this->dataShort );
 
                // Test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_int( $cryptedFile ) );
 
-               $handle = $view->fopen($filename, 'r');
+               $handle = $view->fopen( $filename, 'r' );
 
                // set stream options
-               $this->assertFalse(stream_set_timeout($handle,1));
+               $this->assertFalse( stream_set_timeout( $handle, 1 ) );
 
                // tear down
-               $view->unlink($filename);
+               $view->unlink( $filename );
        }
 
        function testStreamSetWriteBuffer() {
                $filename = '/tmp-' . time();
-               $view = new \OC\Files\View('/' . $this->userId . '/files');
+               $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);
+               $cryptedFile = $view->file_put_contents( $filename, $this->dataShort );
 
                // Test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_int( $cryptedFile ) );
 
-               $handle = $view->fopen($filename, 'r');
+               $handle = $view->fopen( $filename, 'r' );
 
                // set stream options
-               $this->assertEquals(0, stream_set_write_buffer($handle,1024));
+               $this->assertEquals( 0, stream_set_write_buffer( $handle, 1024 ) );
 
                // tear down
-               $view->unlink($filename);
+               $view->unlink( $filename );
        }
 }
\ No newline at end of file
index cc8709b6f246868967155de6db58f5f8d9d24d93..1d7cdc60b212fe375c571bab9c1e96d1cfbb1a83 100755 (executable)
  *
  */
 
-require_once realpath(dirname(__FILE__) . '/../../../lib/base.php');
-require_once realpath(dirname(__FILE__) . '/../lib/crypt.php');
-require_once realpath(dirname(__FILE__) . '/../lib/keymanager.php');
-require_once realpath(dirname(__FILE__) . '/../lib/proxy.php');
-require_once realpath(dirname(__FILE__) . '/../lib/stream.php');
-require_once realpath(dirname(__FILE__) . '/../lib/util.php');
-require_once realpath(dirname(__FILE__) . '/../appinfo/app.php');
-require_once realpath(dirname(__FILE__) . '/../../files_trashbin/appinfo/app.php');
+require_once realpath( dirname( __FILE__ ) . '/../../../lib/base.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/crypt.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/keymanager.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/proxy.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/stream.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/util.php' );
+require_once realpath( dirname( __FILE__ ) . '/../appinfo/app.php' );
+require_once realpath( dirname( __FILE__ ) . '/../../files_trashbin/appinfo/app.php' );
 
 use OCA\Encryption;
 
@@ -50,18 +50,45 @@ class Test_Encryption_Trashbin extends \PHPUnit_Framework_TestCase
        public $subfolder;
        public $subsubfolder;
 
-       function setUp()
-       {
+       public static function setUpBeforeClass() {
                // reset backend
-               \OC_User::useBackend('database');
+               \OC_User::clearBackends();
+               \OC_User::useBackend( 'database' );
 
+               \OC_Hook::clear( 'OC_Filesystem' );
+               \OC_Hook::clear( 'OC_User' );
+
+               // trashbin hooks
+               \OCA\Files_Trashbin\Trashbin::registerHooks();
+
+               // Filesystem related hooks
+               \OCA\Encryption\Helper::registerFilesystemHooks();
+
+               // clear and register hooks
+               \OC_FileProxy::clearProxies();
+               \OC_FileProxy::register( new OCA\Encryption\Proxy() );
+
+               // setup filesystem
+               \OC_Util::tearDownFS();
+               \OC_User::setUserId( '' );
+               \OC\Files\Filesystem::tearDown();
+               \OC_Util::setupFS( 'admin' );
+               \OC_User::setUserId( 'admin' );
+
+               // login admin
+               $params['uid'] = 'admin';
+               $params['password'] = 'admin';
+               OCA\Encryption\Hooks::login( $params );
+       }
+
+       function setUp() {
                // set user id
-               \OC_User::setUserId('admin');
+               \OC_User::setUserId( 'admin' );
                $this->userId = 'admin';
                $this->pass = 'admin';
 
                // init filesystem view
-               $this->view = new \OC_FilesystemView('/');
+               $this->view = new \OC_FilesystemView( '/' );
 
                // init short data
                $this->dataShort = 'hats';
@@ -70,48 +97,24 @@ class Test_Encryption_Trashbin extends \PHPUnit_Framework_TestCase
                $this->subfolder = '/subfolder1';
                $this->subsubfolder = '/subsubfolder1';
 
-               \OC_Hook::clear('OC_Filesystem');
-               \OC_Hook::clear('OC_User');
-
-               // init filesystem related hooks
-               \OCA\Encryption\Helper::registerFilesystemHooks();
-
-               // register encryption file proxy
-               \OC_FileProxy::register(new OCA\Encryption\Proxy());
-
-               // trashbin hooks
-               \OCA\Files_Trashbin\Trashbin::registerHooks();
-
                // remember files_trashbin state
-               $this->stateFilesTrashbin = OC_App::isEnabled('files_trashbin');
-
-               // we don't want to tests with app files_trashbin enabled
-               \OC_App::enable('files_trashbin');
+               $this->stateFilesTrashbin = OC_App::isEnabled( 'files_trashbin' );
 
-               // init filesystem for user
-               \OC_Util::tearDownFS();
-               \OC_User::setUserId('');
-               \OC\Files\Filesystem::tearDown();
-               \OC_Util::setupFS($this->userId);
-               \OC_User::setUserId($this->userId);
-
-               // login user
-               $params['uid'] = $this->userId;
-               $params['password'] = $this->pass;
-               OCA\Encryption\Hooks::login($params);
+               // we want to tests with app files_trashbin enabled
+               \OC_App::enable( 'files_trashbin' );
        }
 
-       function tearDown()
-       {
+       function tearDown() {
                // reset app files_trashbin
-               if ($this->stateFilesTrashbin) {
-                       OC_App::enable('files_trashbin');
+               if ( $this->stateFilesTrashbin ) {
+                       OC_App::enable( 'files_trashbin' );
                } else {
-                       OC_App::disable('files_trashbin');
+                       OC_App::disable( 'files_trashbin' );
                }
+       }
+
+       public static function tearDownAfterClass() {
 
-               // clear all proxies
-               \OC_FileProxy::clearProxies();
        }
 
        /**
@@ -123,49 +126,49 @@ class Test_Encryption_Trashbin extends \PHPUnit_Framework_TestCase
                $filename = 'tmp-' . time() . '.txt';
 
                // save file with content
-               $cryptedFile = file_put_contents('crypt:///' . $filename, $this->dataShort);
+               $cryptedFile = file_put_contents( 'crypt:///' . $filename, $this->dataShort );
 
                // test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_int( $cryptedFile ) );
 
                // check if key for admin exists
-               $this->assertTrue($this->view->file_exists('/admin/files_encryption/keyfiles/' . $filename . '.key'));
+               $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/keyfiles/' . $filename . '.key' ) );
 
                // check if share key for admin exists
-               $this->assertTrue($this->view->file_exists('/admin/files_encryption/share-keys/' . $filename . '.admin.shareKey'));
+               $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $filename . '.admin.shareKey' ) );
 
                // delete file
-               \OC\FIles\Filesystem::unlink($filename);
+               \OC\FIles\Filesystem::unlink( $filename );
 
                // check if file not exists
-               $this->assertFalse($this->view->file_exists('/admin/files/' . $filename));
+               $this->assertFalse( $this->view->file_exists( '/admin/files/' . $filename ) );
 
                // check if key for admin not exists
-               $this->assertFalse($this->view->file_exists('/admin/files_encryption/keyfiles/' . $filename . '.key'));
+               $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/keyfiles/' . $filename . '.key' ) );
 
                // check if share key for admin not exists
-               $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $filename . '.admin.shareKey'));
+               $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $filename . '.admin.shareKey' ) );
 
                // get files
-               $trashFiles = $this->view->getDirectoryContent('/admin/files_trashbin/files/');
+               $trashFiles = $this->view->getDirectoryContent( '/admin/files_trashbin/files/' );
 
                $trashFileSuffix = null;
                // find created file with timestamp
-               foreach($trashFiles as $file) {
-                       if(strncmp($file['path'], $filename, strlen($filename))) {
-                               $path_parts = pathinfo($file['name']);
+               foreach ( $trashFiles as $file ) {
+                       if ( strncmp( $file['path'], $filename, strlen( $filename ) ) ) {
+                               $path_parts = pathinfo( $file['name'] );
                                $trashFileSuffix = $path_parts['extension'];
                        }
                }
 
                // check if we found the file we created
-               $this->assertNotNull($trashFileSuffix);
+               $this->assertNotNull( $trashFileSuffix );
 
                // check if key for admin not exists
-               $this->assertTrue($this->view->file_exists('/admin/files_trashbin/keyfiles/' . $filename . '.key.' . $trashFileSuffix));
+               $this->assertTrue( $this->view->file_exists( '/admin/files_trashbin/keyfiles/' . $filename . '.key.' . $trashFileSuffix ) );
 
                // check if share key for admin not exists
-               $this->assertTrue($this->view->file_exists('/admin/files_trashbin/share-keys/' . $filename . '.admin.shareKey.' . $trashFileSuffix));
+               $this->assertTrue( $this->view->file_exists( '/admin/files_trashbin/share-keys/' . $filename . '.admin.shareKey.' . $trashFileSuffix ) );
 
                // return filename for next test
                return $filename . '.' . $trashFileSuffix;
@@ -176,25 +179,25 @@ class Test_Encryption_Trashbin extends \PHPUnit_Framework_TestCase
         *
         * @depends testDeleteFile
         */
-       function testRestoreFile($filename) {
+       function testRestoreFile( $filename ) {
 
                // prepare file information
-               $path_parts = pathinfo($filename);
+               $path_parts = pathinfo( $filename );
                $trashFileSuffix = $path_parts['extension'];
-               $timestamp = str_replace('d', '', $trashFileSuffix);
-               $fileNameWithoutSuffix = str_replace('.'.$trashFileSuffix, '', $filename);
+               $timestamp = str_replace( 'd', '', $trashFileSuffix );
+               $fileNameWithoutSuffix = str_replace( '.' . $trashFileSuffix, '', $filename );
 
                // restore file
-               $this->assertTrue(\OCA\Files_Trashbin\Trashbin::restore($filename, $fileNameWithoutSuffix, $timestamp));
+               $this->assertTrue( \OCA\Files_Trashbin\Trashbin::restore( $filename, $fileNameWithoutSuffix, $timestamp ) );
 
                // check if file exists
-               $this->assertTrue($this->view->file_exists('/admin/files/' . $fileNameWithoutSuffix));
+               $this->assertTrue( $this->view->file_exists( '/admin/files/' . $fileNameWithoutSuffix ) );
 
                // check if key for admin exists
-               $this->assertTrue($this->view->file_exists('/admin/files_encryption/keyfiles/' . $fileNameWithoutSuffix . '.key'));
+               $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/keyfiles/' . $fileNameWithoutSuffix . '.key' ) );
 
                // check if share key for admin exists
-               $this->assertTrue($this->view->file_exists('/admin/files_encryption/share-keys/' . $fileNameWithoutSuffix . '.admin.shareKey'));
+               $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $fileNameWithoutSuffix . '.admin.shareKey' ) );
        }
 
        /**
@@ -206,65 +209,59 @@ class Test_Encryption_Trashbin extends \PHPUnit_Framework_TestCase
                $filename = 'tmp-' . time() . '.txt';
 
                // save file with content
-               $cryptedFile = file_put_contents('crypt:///' . $filename, $this->dataShort);
+               $cryptedFile = file_put_contents( 'crypt:///' . $filename, $this->dataShort );
 
                // test that data was successfully written
-               $this->assertTrue(is_int($cryptedFile));
+               $this->assertTrue( is_int( $cryptedFile ) );
 
                // check if key for admin exists
-               $this->assertTrue($this->view->file_exists('/admin/files_encryption/keyfiles/' . $filename . '.key'));
+               $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/keyfiles/' . $filename . '.key' ) );
 
                // check if share key for admin exists
-               $this->assertTrue($this->view->file_exists('/admin/files_encryption/share-keys/' . $filename . '.admin.shareKey'));
+               $this->assertTrue( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $filename . '.admin.shareKey' ) );
 
                // delete file
-               \OC\FIles\Filesystem::unlink($filename);
+               \OC\FIles\Filesystem::unlink( $filename );
 
                // check if file not exists
-               $this->assertFalse($this->view->file_exists('/admin/files/' . $filename));
+               $this->assertFalse( $this->view->file_exists( '/admin/files/' . $filename ) );
 
                // check if key for admin not exists
-               $this->assertFalse($this->view->file_exists('/admin/files_encryption/keyfiles/' . $filename . '.key'));
+               $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/keyfiles/' . $filename . '.key' ) );
 
                // check if share key for admin not exists
-               $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $filename . '.admin.shareKey'));
+               $this->assertFalse( $this->view->file_exists( '/admin/files_encryption/share-keys/' . $filename . '.admin.shareKey' ) );
 
-               // get files
-               $trashFiles = $this->view->getDirectoryContent('/admin/files_trashbin/files/');
-
-               $trashFileSuffix = null;
                // find created file with timestamp
-               foreach($trashFiles as $file) {
-                       if(strncmp($file['name'], $filename, strlen($filename)) == 0) {
-                               $path_parts = pathinfo($file['name']);
-                               $trashFileSuffix = $path_parts['extension'];
-                               break;
-                       }
-               }
+               $query = \OC_DB::prepare( 'SELECT `timestamp`,`type` FROM `*PREFIX*files_trash`'
+                       . ' WHERE `id`=?' );
+               $result = $query->execute( array( $filename ) )->fetchRow();
 
-               // check if we found the file we created
-               $this->assertNotNull($trashFileSuffix);
+               $this->assertTrue( is_array( $result ) );
+
+               // build suffix
+               $trashFileSuffix = 'd' . $result['timestamp'];
 
                // check if key for admin exists
-               $this->assertTrue($this->view->file_exists('/admin/files_trashbin/keyfiles/' . $filename . '.key.' . $trashFileSuffix));
+               $this->assertTrue( $this->view->file_exists( '/admin/files_trashbin/keyfiles/' . $filename . '.key.' . $trashFileSuffix ) );
 
                // check if share key for admin exists
-               $this->assertTrue($this->view->file_exists('/admin/files_trashbin/share-keys/' . $filename . '.admin.shareKey.' . $trashFileSuffix));
+               $this->assertTrue( $this->view->file_exists( '/admin/files_trashbin/share-keys/' . $filename . '.admin.shareKey.' . $trashFileSuffix ) );
 
                // get timestamp from file
-               $timestamp = str_replace('d', '', $trashFileSuffix);
+               $timestamp = str_replace( 'd', '', $trashFileSuffix );
 
                // delete file forever
-               $this->assertGreaterThan(0, \OCA\Files_Trashbin\Trashbin::delete($filename, $timestamp));
+               $this->assertGreaterThan( 0, \OCA\Files_Trashbin\Trashbin::delete( $filename, $timestamp ) );
 
                // check if key for admin not exists
-               $this->assertFalse($this->view->file_exists('/admin/files_trashbin/files/' . $filename . '.' . $trashFileSuffix));
+               $this->assertFalse( $this->view->file_exists( '/admin/files_trashbin/files/' . $filename . '.' . $trashFileSuffix ) );
 
                // check if key for admin not exists
-               $this->assertFalse($this->view->file_exists('/admin/files_trashbin/keyfiles/' . $filename . '.key.' . $trashFileSuffix));
+               $this->assertFalse( $this->view->file_exists( '/admin/files_trashbin/keyfiles/' . $filename . '.key.' . $trashFileSuffix ) );
 
                // check if share key for admin not exists
-               $this->assertFalse($this->view->file_exists('/admin/files_trashbin/share-keys/' . $filename . '.admin.shareKey.' . $trashFileSuffix));
+               $this->assertFalse( $this->view->file_exists( '/admin/files_trashbin/share-keys/' . $filename . '.admin.shareKey.' . $trashFileSuffix ) );
        }
 
 }
\ No newline at end of file
index a2be8a40417f4ed754ae477a6002dcb7cd21d0f5..af78894da2bb88006b1ab37414e5b140ac715137 100755 (executable)
@@ -6,13 +6,13 @@
  * See the COPYING-README file.
  */
 
-require_once realpath(dirname(__FILE__) . '/../../../lib/base.php');
-require_once realpath(dirname(__FILE__) . '/../lib/crypt.php');
-require_once realpath(dirname(__FILE__) . '/../lib/keymanager.php');
-require_once realpath(dirname(__FILE__) . '/../lib/proxy.php');
-require_once realpath(dirname(__FILE__) . '/../lib/stream.php');
-require_once realpath(dirname(__FILE__) . '/../lib/util.php');
-require_once realpath(dirname(__FILE__) . '/../appinfo/app.php');
+require_once realpath( dirname( __FILE__ ) . '/../../../lib/base.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/crypt.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/keymanager.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/proxy.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/stream.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/util.php' );
+require_once realpath( dirname( __FILE__ ) . '/../appinfo/app.php' );
 
 use OCA\Encryption;
 
@@ -42,22 +42,21 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase
        public $legacyEncryptedDataKey;
        public $lagacyKey;
 
-       function setUp()
-       {
+       function setUp() {
                // reset backend
-               \OC_User::useBackend('database');
+               \OC_User::useBackend( 'database' );
 
-               \OC_User::setUserId('admin');
+               \OC_User::setUserId( 'admin' );
                $this->userId = 'admin';
                $this->pass = 'admin';
 
                // set content for encrypting / decrypting in tests
-               $this->dataUrl = realpath(dirname(__FILE__) . '/../lib/crypt.php');
+               $this->dataUrl = realpath( dirname( __FILE__ ) . '/../lib/crypt.php' );
                $this->dataShort = 'hats';
-               $this->dataLong = file_get_contents(realpath(dirname(__FILE__) . '/../lib/crypt.php'));
-               $this->legacyData = realpath(dirname(__FILE__) . '/legacy-text.txt');
-               $this->legacyEncryptedData = realpath(dirname(__FILE__) . '/legacy-encrypted-text.txt');
-               $this->legacyEncryptedDataKey = realpath(dirname(__FILE__) . '/encryption.key');
+               $this->dataLong = file_get_contents( realpath( dirname( __FILE__ ) . '/../lib/crypt.php' ) );
+               $this->legacyData = realpath( dirname( __FILE__ ) . '/legacy-text.txt' );
+               $this->legacyEncryptedData = realpath( dirname( __FILE__ ) . '/legacy-encrypted-text.txt' );
+               $this->legacyEncryptedDataKey = realpath( dirname( __FILE__ ) . '/encryption.key' );
                $this->lagacyKey = '62829813025828180801';
 
                $keypair = Encryption\Crypt::createKeypair();
@@ -71,132 +70,128 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase
                $this->publicKeyPath = $this->publicKeyDir . '/' . $this->userId . '.public.key'; // e.g. data/public-keys/admin.public.key
                $this->privateKeyPath = $this->encryptionDir . '/' . $this->userId . '.private.key'; // e.g. data/admin/admin.private.key
 
-               $this->view = new \OC_FilesystemView('/');
+               $this->view = new \OC_FilesystemView( '/' );
 
-               $userHome = \OC_User::getHome($this->userId);
-               $this->dataDir = str_replace('/' . $this->userId, '', $userHome);
+               $userHome = \OC_User::getHome( $this->userId );
+               $this->dataDir = str_replace( '/' . $this->userId, '', $userHome );
 
                // Filesystem related hooks
                \OCA\Encryption\Helper::registerFilesystemHooks();
 
-               \OC_FileProxy::register(new OCA\Encryption\Proxy());
+               // clear and register hooks
+               \OC_FileProxy::clearProxies();
+               \OC_FileProxy::register( new OCA\Encryption\Proxy() );
 
+               // setup filesystem
                \OC_Util::tearDownFS();
-               \OC_User::setUserId('');
+               \OC_User::setUserId( '' );
                \OC\Files\Filesystem::tearDown();
-               \OC_Util::setupFS($this->userId);
-               \OC_User::setUserId($this->userId);
+               \OC_Util::setupFS( $this->userId );
+               \OC_User::setUserId( $this->userId );
 
+               // login admin
                $params['uid'] = $this->userId;
                $params['password'] = $this->pass;
-               OCA\Encryption\Hooks::login($params);
+               OCA\Encryption\Hooks::login( $params );
 
-               $this->util = new Encryption\Util($this->view, $this->userId);
+               $this->util = new Encryption\Util( $this->view, $this->userId );
        }
 
-       function tearDown()
-       {
-
+       function tearDown() {
+               // clear and register hooks
                \OC_FileProxy::clearProxies();
        }
 
        /**
         * @brief test that paths set during User construction are correct
         */
-       function testKeyPaths()
-       {
-               $util = new Encryption\Util($this->view, $this->userId);
+       function testKeyPaths() {
+               $util = new Encryption\Util( $this->view, $this->userId );
 
-               $this->assertEquals($this->publicKeyDir, $util->getPath('publicKeyDir'));
-               $this->assertEquals($this->encryptionDir, $util->getPath('encryptionDir'));
-               $this->assertEquals($this->keyfilesPath, $util->getPath('keyfilesPath'));
-               $this->assertEquals($this->publicKeyPath, $util->getPath('publicKeyPath'));
-               $this->assertEquals($this->privateKeyPath, $util->getPath('privateKeyPath'));
+               $this->assertEquals( $this->publicKeyDir, $util->getPath( 'publicKeyDir' ) );
+               $this->assertEquals( $this->encryptionDir, $util->getPath( 'encryptionDir' ) );
+               $this->assertEquals( $this->keyfilesPath, $util->getPath( 'keyfilesPath' ) );
+               $this->assertEquals( $this->publicKeyPath, $util->getPath( 'publicKeyPath' ) );
+               $this->assertEquals( $this->privateKeyPath, $util->getPath( 'privateKeyPath' ) );
 
        }
 
        /**
         * @brief test setup of encryption directories
         */
-       function testSetupServerSide()
-       {
-               $this->assertEquals(true, $this->util->setupServerSide($this->pass));
+       function testSetupServerSide() {
+               $this->assertEquals( true, $this->util->setupServerSide( $this->pass ) );
        }
 
        /**
         * @brief test checking whether account is ready for encryption,
         */
-       function testUserIsReady()
-       {
-               $this->assertEquals(true, $this->util->ready());
+       function testUserIsReady() {
+               $this->assertEquals( true, $this->util->ready() );
        }
 
        /**
         * @brief test checking whether account is not ready for encryption,
         */
-       function testUserIsNotReady()
-       {
-               $this->view->unlink($this->publicKeyDir);
+       function testUserIsNotReady() {
+               $this->view->unlink( $this->publicKeyDir );
 
                $params['uid'] = $this->userId;
                $params['password'] = $this->pass;
-               $this->assertFalse(OCA\Encryption\Hooks::login($params));
+               $this->assertFalse( OCA\Encryption\Hooks::login( $params ) );
 
-               $this->view->unlink($this->privateKeyPath);
+               $this->view->unlink( $this->privateKeyPath );
        }
 
        /**
         * @brief test checking whether account is not ready for encryption,
         */
-       function testIsLagacyUser()
-       {
+       function testIsLagacyUser() {
                $userView = new \OC_FilesystemView( '/' . $this->userId );
 
                // Disable encryption proxy to prevent recursive calls
                $proxyStatus = \OC_FileProxy::$enabled;
                \OC_FileProxy::$enabled = false;
 
-               $encryptionKeyContent = file_get_contents($this->legacyEncryptedDataKey);
-               $userView->file_put_contents('/encryption.key', $encryptionKeyContent);
+               $encryptionKeyContent = file_get_contents( $this->legacyEncryptedDataKey );
+               $userView->file_put_contents( '/encryption.key', $encryptionKeyContent );
 
                \OC_FileProxy::$enabled = $proxyStatus;
 
                $params['uid'] = $this->userId;
                $params['password'] = $this->pass;
 
-               $util = new Encryption\Util($this->view, $this->userId);
-               $util->setMigrationStatus(0);
+               $util = new Encryption\Util( $this->view, $this->userId );
+               $util->setMigrationStatus( 0 );
 
-               $this->assertTrue(OCA\Encryption\Hooks::login($params));
+               $this->assertTrue( OCA\Encryption\Hooks::login( $params ) );
 
-               $this->assertEquals($this->lagacyKey, $_SESSION['legacyKey']);
+               $this->assertEquals( $this->lagacyKey, $_SESSION['legacyKey'] );
        }
 
-       function testRecoveryEnabledForUser()
-       {
+       function testRecoveryEnabledForUser() {
 
-               $util = new Encryption\Util($this->view, $this->userId);
+               $util = new 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(1));
+               $this->assertTrue( $util->setRecoveryForUser( 1 ) );
 
-               $this->assertEquals(1, $util->recoveryEnabledForUser());
+               $this->assertEquals( 1, $util->recoveryEnabledForUser() );
 
-               $this->assertTrue($util->setRecoveryForUser(0));
+               $this->assertTrue( $util->setRecoveryForUser( 0 ) );
 
-               $this->assertEquals(0, $util->recoveryEnabledForUser());
+               $this->assertEquals( 0, $util->recoveryEnabledForUser() );
 
                // Return the setting to it's previous state
-               $this->assertTrue($util->setRecoveryForUser($enabled));
+               $this->assertTrue( $util->setRecoveryForUser( $enabled ) );
 
        }
 
-       function testGetUidAndFilename()
-       {
+       function testGetUidAndFilename() {
 
-               \OC_User::setUserId('admin');
+               \OC_User::setUserId( 'admin' );
 
                $filename = 'tmp-' . time() . '.test';
 
@@ -204,74 +199,80 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase
                $proxyStatus = \OC_FileProxy::$enabled;
                \OC_FileProxy::$enabled = false;
 
-               $this->view->file_put_contents($this->userId . '/files/' . $filename, $this->dataShort);
+               $this->view->file_put_contents( $this->userId . '/files/' . $filename, $this->dataShort );
 
                // Re-enable proxy - our work is done
                \OC_FileProxy::$enabled = $proxyStatus;
 
-               $util = new Encryption\Util($this->view, $this->userId);
+               $util = new Encryption\Util( $this->view, $this->userId );
 
-               list($fileOwnerUid, $file) = $util->getUidAndFilename($filename);
+               list( $fileOwnerUid, $file ) = $util->getUidAndFilename( $filename );
 
-               $this->assertEquals('admin', $fileOwnerUid);
+               $this->assertEquals( 'admin', $fileOwnerUid );
 
-               $this->assertEquals($file, $filename);
+               $this->assertEquals( $file, $filename );
+
+               $this->view->unlink( $this->userId . '/files/' . $filename );
        }
 
        function testIsSharedPath() {
                $sharedPath = '/user1/files/Shared/test';
                $path = '/user1/files/test';
 
-               $this->assertTrue($this->util->isSharedPath($sharedPath));
+               $this->assertTrue( $this->util->isSharedPath( $sharedPath ) );
 
-               $this->assertFalse($this->util->isSharedPath($path));
+               $this->assertFalse( $this->util->isSharedPath( $path ) );
        }
 
-       function testEncryptLagacyFiles()
-       {
-               $userView = new \OC_FilesystemView( '/' . $this->userId);
+       function testEncryptLagacyFiles() {
+               // login admin
+               $params['uid'] = $this->userId;
+               $params['password'] = $this->pass;
+               OCA\Encryption\Hooks::login( $params );
+
+               $userView = new \OC_FilesystemView( '/' . $this->userId );
                $view = new \OC_FilesystemView( '/' . $this->userId . '/files' );
 
                // Disable encryption proxy to prevent recursive calls
                $proxyStatus = \OC_FileProxy::$enabled;
                \OC_FileProxy::$enabled = false;
 
-               $encryptionKeyContent = file_get_contents($this->legacyEncryptedDataKey);
-               $userView->file_put_contents('/encryption.key', $encryptionKeyContent);
+               $encryptionKeyContent = file_get_contents( $this->legacyEncryptedDataKey );
+               $userView->file_put_contents( '/encryption.key', $encryptionKeyContent );
 
-               $legacyEncryptedData = file_get_contents($this->legacyEncryptedData);
-               $view->mkdir('/test/');
-               $view->mkdir('/test/subtest/');
-               $view->file_put_contents('/test/subtest/legacy-encrypted-text.txt', $legacyEncryptedData);
+               $legacyEncryptedData = file_get_contents( $this->legacyEncryptedData );
+               $view->mkdir( '/test/' );
+               $view->mkdir( '/test/subtest/' );
+               $view->file_put_contents( '/test/subtest/legacy-encrypted-text.txt', $legacyEncryptedData );
 
-               $fileInfo = $view->getFileInfo('/test/subtest/legacy-encrypted-text.txt');
+               $fileInfo = $view->getFileInfo( '/test/subtest/legacy-encrypted-text.txt' );
                $fileInfo['encrypted'] = true;
-               $view->putFileInfo('/test/subtest/legacy-encrypted-text.txt', $fileInfo);
+               $view->putFileInfo( '/test/subtest/legacy-encrypted-text.txt', $fileInfo );
 
                \OC_FileProxy::$enabled = $proxyStatus;
 
                $params['uid'] = $this->userId;
                $params['password'] = $this->pass;
 
-               $util = new Encryption\Util($this->view, $this->userId);
-               $util->setMigrationStatus(0);
+               $util = new Encryption\Util( $this->view, $this->userId );
+               $util->setMigrationStatus( 0 );
 
-               $this->assertTrue(OCA\Encryption\Hooks::login($params));
+               $this->assertTrue( OCA\Encryption\Hooks::login( $params ) );
 
-               $this->assertEquals($this->lagacyKey, $_SESSION['legacyKey']);
+               $this->assertEquals( $this->lagacyKey, $_SESSION['legacyKey'] );
 
-               $files = $util->findEncFiles('/' . $this->userId . '/files/');
+               $files = $util->findEncFiles( '/' . $this->userId . '/files/' );
 
-               $this->assertTrue(is_array($files));
+               $this->assertTrue( is_array( $files ) );
 
                $found = false;
-               foreach($files['encrypted'] as $encryptedFile) {
-                       if($encryptedFile['name'] === 'legacy-encrypted-text.txt') {
+               foreach ( $files['encrypted'] as $encryptedFile ) {
+                       if ( $encryptedFile['name'] === 'legacy-encrypted-text.txt' ) {
                                $found = true;
                                break;
                        }
                }
 
-               $this->assertTrue($found);
+               $this->assertTrue( $found );
        }
 }
\ No newline at end of file
index 4b453d0c9d16221c24caa7343237af716a490409..57848cd273c5788dfcac4c605db7a80945350e2a 100755 (executable)
  *
  */
 
-require_once realpath(dirname(__FILE__) . '/../../../lib/base.php');
-require_once realpath(dirname(__FILE__) . '/../lib/crypt.php');
-require_once realpath(dirname(__FILE__) . '/../lib/keymanager.php');
-require_once realpath(dirname(__FILE__) . '/../lib/proxy.php');
-require_once realpath(dirname(__FILE__) . '/../lib/stream.php');
-require_once realpath(dirname(__FILE__) . '/../lib/util.php');
-require_once realpath(dirname(__FILE__) . '/../appinfo/app.php');
+require_once realpath( dirname( __FILE__ ) . '/../../../lib/base.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/crypt.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/keymanager.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/proxy.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/stream.php' );
+require_once realpath( dirname( __FILE__ ) . '/../lib/util.php' );
+require_once realpath( dirname( __FILE__ ) . '/../appinfo/app.php' );
 
 use OCA\Encryption;
 
@@ -46,18 +46,17 @@ class Test_Encryption_Webdav extends \PHPUnit_Framework_TestCase
        public $dataShort;
        public $stateFilesTrashbin;
 
-       function setUp()
-       {
+       function setUp() {
                // reset backend
-               \OC_User::useBackend('database');
+               \OC_User::useBackend( 'database' );
 
                // set user id
-               \OC_User::setUserId('admin');
+               \OC_User::setUserId( 'admin' );
                $this->userId = 'admin';
                $this->pass = 'admin';
 
                // init filesystem view
-               $this->view = new \OC_FilesystemView('/');
+               $this->view = new \OC_FilesystemView( '/' );
 
                // init short data
                $this->dataShort = 'hats';
@@ -65,38 +64,38 @@ class Test_Encryption_Webdav extends \PHPUnit_Framework_TestCase
                // init filesystem related hooks
                \OCA\Encryption\Helper::registerFilesystemHooks();
 
-               // register encryption file proxy
-               \OC_FileProxy::register(new OCA\Encryption\Proxy());
+               // clear and register hooks
+               \OC_FileProxy::clearProxies();
+               \OC_FileProxy::register( new OCA\Encryption\Proxy() );
 
                // remember files_trashbin state
-               $this->stateFilesTrashbin = OC_App::isEnabled('files_trashbin');
+               $this->stateFilesTrashbin = OC_App::isEnabled( 'files_trashbin' );
 
                // we don't want to tests with app files_trashbin enabled
-               \OC_App::disable('files_trashbin');
+               \OC_App::disable( 'files_trashbin' );
 
                // init filesystem for user
                \OC_Util::tearDownFS();
-               \OC_User::setUserId('');
+               \OC_User::setUserId( '' );
                \OC\Files\Filesystem::tearDown();
-               \OC_Util::setupFS($this->userId);
-               \OC_User::setUserId($this->userId);
+               \OC_Util::setupFS( $this->userId );
+               \OC_User::setUserId( $this->userId );
 
                // login user
                $params['uid'] = $this->userId;
                $params['password'] = $this->pass;
-               OCA\Encryption\Hooks::login($params);
+               OCA\Encryption\Hooks::login( $params );
        }
 
-       function tearDown()
-       {
+       function tearDown() {
                // reset app files_trashbin
-               if ($this->stateFilesTrashbin) {
-                       OC_App::enable('files_trashbin');
+               if ( $this->stateFilesTrashbin ) {
+                       OC_App::enable( 'files_trashbin' );
                } else {
-                       OC_App::disable('files_trashbin');
+                       OC_App::disable( 'files_trashbin' );
                }
 
-               // clear all proxies
+               // clear and register hooks
                \OC_FileProxy::clearProxies();
        }
 
@@ -116,38 +115,38 @@ class Test_Encryption_Webdav extends \PHPUnit_Framework_TestCase
                $_SERVER['HTTP_AUTHORIZATION'] = 'Basic YWRtaW46YWRtaW4=';
                $_SERVER['CONTENT_TYPE'] = 'application/octet-stream';
                $_SERVER['PATH_INFO'] = '/webdav' . $filename;
-               $_SERVER['CONTENT_LENGTH'] = strlen($this->dataShort);
+               $_SERVER['CONTENT_LENGTH'] = strlen( $this->dataShort );
 
                // handle webdav request
-               $this->handleWebdavRequest($this->dataShort);
+               $this->handleWebdavRequest( $this->dataShort );
 
                // check if file was created
-               $this->assertTrue($this->view->file_exists('/' . $this->userId . '/files' . $filename));
+               $this->assertTrue( $this->view->file_exists( '/' . $this->userId . '/files' . $filename ) );
 
                // check if key-file was created
-               $this->assertTrue($this->view->file_exists('/' . $this->userId . '/files_encryption/keyfiles/' . $filename . '.key'));
+               $this->assertTrue( $this->view->file_exists( '/' . $this->userId . '/files_encryption/keyfiles/' . $filename . '.key' ) );
 
                // check if shareKey-file was created
-               $this->assertTrue($this->view->file_exists('/' . $this->userId . '/files_encryption/share-keys/' . $filename . '.' . $this->userId . '.shareKey'));
+               $this->assertTrue( $this->view->file_exists( '/' . $this->userId . '/files_encryption/share-keys/' . $filename . '.' . $this->userId . '.shareKey' ) );
 
                // disable encryption proxy to prevent recursive calls
                $proxyStatus = \OC_FileProxy::$enabled;
                \OC_FileProxy::$enabled = false;
 
                // get encrypted file content
-               $encryptedContent = $this->view->file_get_contents('/' . $this->userId . '/files' . $filename);
+               $encryptedContent = $this->view->file_get_contents( '/' . $this->userId . '/files' . $filename );
 
                // restore proxy state
                \OC_FileProxy::$enabled = $proxyStatus;
 
                // check if encrypted content is valid
-               $this->assertTrue(Encryption\Crypt::isCatfileContent($encryptedContent));
+               $this->assertTrue( Encryption\Crypt::isCatfileContent( $encryptedContent ) );
 
                // get decrypted file contents
-               $decrypt = file_get_contents('crypt://' . $filename);
+               $decrypt = file_get_contents( 'crypt://' . $filename );
 
                // check if file content match with the written content
-               $this->assertEquals($this->dataShort, $decrypt);
+               $this->assertEquals( $this->dataShort, $decrypt );
 
                // return filename for next test
                return $filename;
@@ -158,7 +157,7 @@ class Test_Encryption_Webdav extends \PHPUnit_Framework_TestCase
         *
         * @depends testWebdavPUT
         */
-       function testWebdavGET($filename) {
+       function testWebdavGET( $filename ) {
 
                // set server vars
                $_SERVER['REQUEST_METHOD'] = 'GET';
@@ -170,7 +169,7 @@ class Test_Encryption_Webdav extends \PHPUnit_Framework_TestCase
                $content = $this->handleWebdavRequest();
 
                // check if file content match with the written content
-               $this->assertEquals($this->dataShort, $content);
+               $this->assertEquals( $this->dataShort, $content );
 
                // return filename for next test
                return $filename;
@@ -180,7 +179,7 @@ class Test_Encryption_Webdav extends \PHPUnit_Framework_TestCase
         * @brief test webdav delete random file
         * @depends testWebdavGET
         */
-       function testWebdavDELETE($filename) {
+       function testWebdavDELETE( $filename ) {
                // set server vars
                $_SERVER['REQUEST_METHOD'] = 'DELETE';
                $_SERVER['REQUEST_URI'] = '/remote.php/webdav' . $filename;
@@ -191,13 +190,13 @@ class Test_Encryption_Webdav extends \PHPUnit_Framework_TestCase
                $content = $this->handleWebdavRequest();
 
                // check if file was removed
-               $this->assertFalse($this->view->file_exists('/' . $this->userId . '/files' . $filename));
+               $this->assertFalse( $this->view->file_exists( '/' . $this->userId . '/files' . $filename ) );
 
                // check if key-file was removed
-               $this->assertFalse($this->view->file_exists('/' . $this->userId . '/files_encryption/keyfiles' . $filename . '.key'));
+               $this->assertFalse( $this->view->file_exists( '/' . $this->userId . '/files_encryption/keyfiles' . $filename . '.key' ) );
 
                // check if shareKey-file was removed
-               $this->assertFalse($this->view->file_exists('/' . $this->userId . '/files_encryption/share-keys' . $filename . '.' . $this->userId . '.shareKey'));
+               $this->assertFalse( $this->view->file_exists( '/' . $this->userId . '/files_encryption/share-keys' . $filename . '.' . $this->userId . '.shareKey' ) );
        }
 
        /**
@@ -207,30 +206,30 @@ class Test_Encryption_Webdav extends \PHPUnit_Framework_TestCase
         *
         * @note this init procedure is copied from /apps/files/remote.php
         */
-       function handleWebdavRequest($body = false) {
+       function handleWebdavRequest( $body = false ) {
                // Backends
                $authBackend = new OC_Connector_Sabre_Auth();
                $lockBackend = new OC_Connector_Sabre_Locks();
                $requestBackend = new OC_Connector_Sabre_Request();
 
                // Create ownCloud Dir
-               $publicDir = new OC_Connector_Sabre_Directory('');
+               $publicDir = new OC_Connector_Sabre_Directory( '' );
 
                // Fire up server
-               $server = new Sabre_DAV_Server($publicDir);
+               $server = new Sabre_DAV_Server( $publicDir );
                $server->httpRequest = $requestBackend;
-               $server->setBaseUri('/remote.php/webdav/');
+               $server->setBaseUri( '/remote.php/webdav/' );
 
                // Load plugins
-               $server->addPlugin(new Sabre_DAV_Auth_Plugin($authBackend, 'ownCloud'));
-               $server->addPlugin(new Sabre_DAV_Locks_Plugin($lockBackend));
-               $server->addPlugin(new Sabre_DAV_Browser_Plugin(false)); // Show something in the Browser, but no upload
-               $server->addPlugin(new OC_Connector_Sabre_QuotaPlugin());
-               $server->addPlugin(new OC_Connector_Sabre_MaintenancePlugin());
+               $server->addPlugin( new Sabre_DAV_Auth_Plugin( $authBackend, 'ownCloud' ) );
+               $server->addPlugin( new Sabre_DAV_Locks_Plugin( $lockBackend ) );
+               $server->addPlugin( new Sabre_DAV_Browser_Plugin( false ) ); // Show something in the Browser, but no upload
+               $server->addPlugin( new OC_Connector_Sabre_QuotaPlugin() );
+               $server->addPlugin( new OC_Connector_Sabre_MaintenancePlugin() );
 
                // And off we go!
-               if($body) {
-                       $server->httpRequest->setBody($body);
+               if ( $body ) {
+                       $server->httpRequest->setBody( $body );
                }
 
                // turn on output buffering