]> source.dussan.org Git - nextcloud-server.git/commitdiff
improved tests
authorFlorin Peter <github@florin-peter.de>
Tue, 21 May 2013 22:55:16 +0000 (00:55 +0200)
committerFlorin Peter <github@florin-peter.de>
Tue, 21 May 2013 23:11:55 +0000 (01:11 +0200)
apps/files_encryption/lib/proxy.php
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/util.php

index f29e893f12a0a741d974359fd3ddbd4565c701e4..cc9d239b256e1ddef8ea733b7a2e0b5e98bc3558 100644 (file)
@@ -359,24 +359,6 @@ class Proxy extends \OC_FileProxy
 
        }
 
-       /**
-        * @param $path
-        * @param $mime
-        * @return string
-        */
-       public function postGetMimeType($path, $mime)
-       {
-
-               if (Crypt::isCatfileContent($path)) {
-
-                       $mime = \OCP\Files::getMimeType('crypt://' . $path, 'w');
-
-               }
-
-               return $mime;
-
-       }
-
        /**
         * @param $path
         * @param $data
@@ -402,43 +384,6 @@ class Proxy extends \OC_FileProxy
                return $data;
        }
 
-       /**
-        * @param $path
-        * @param $data
-        * @return mixed
-        */
-       public function postStat($path, $data)
-       {
-               $content = '';
-               $view = new \OC_FilesystemView('/');
-               if($view->file_exists($path)) {
-                       // disable encryption proxy
-                       $proxyStatus = \OC_FileProxy::$enabled;
-                       \OC_FileProxy::$enabled = false;
-
-                       // we only need 24 byte from the last chunk
-                       $handle = $view->fopen($path, 'r');
-                       if (!fseek($handle, -24, SEEK_END)) {
-                               $content = fgets($handle);
-                       }
-
-                       // re-enable proxy
-                       \OC_FileProxy::$enabled = $proxyStatus;
-               }
-
-               // check if file is encrypted
-               if (Crypt::isCatfileContent($content)) {
-
-                       // get file info from cache
-                       $cached = $view->getFileInfo($path);
-
-                       // set the real file size
-                       $data['size'] = $cached['unencrypted_size'];
-               }
-
-               return $data;
-       }
-
        /**
         * @param $path
         * @param $size
index eaedc48541398bfb3eecf9486ee773430a31f732..621941c52a10ec6e1dd8c78097ed0f3a100c32a2 100755 (executable)
@@ -432,29 +432,6 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase
                $this->view->unlink($this->userId . '/files/' . $filename);
        }
 
-       // Is this test still necessary?
-//     function testSymmetricBlockStreamDecryptFileContent() {
-//     
-//             \OC_User::setUserId( 'admin' );
-//             
-//             // Disable encryption proxy to prevent unwanted en/decryption
-//             \OC_FileProxy::$enabled = false;
-//             
-//             $cryptedFile = file_put_contents( 'crypt://' . '/blockEncrypt', $this->dataUrl );
-//             
-//             // Disable encryption proxy to prevent unwanted en/decryption
-//             \OC_FileProxy::$enabled = false;
-//             
-//             echo "\n\n\$cryptedFile = " . $this->view->file_get_contents( '/blockEncrypt' );
-//             
-//             $retreivedCryptedFile = file_get_contents( 'crypt://' . '/blockEncrypt' );
-//             
-//             $this->assertEquals( $this->dataUrl, $retreivedCryptedFile );
-//             
-//             \OC_FileProxy::$enabled = false;
-//             
-//     }
-
        function testSymmetricEncryptFileContentKeyfile()
        {
 
@@ -585,6 +562,7 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase
 
                $this->assertEquals($this->dataLong, $decrypted);
 
+               $this->assertFalse(Encryption\Crypt::getBlowfish(''));
        }
 
        /**
@@ -852,69 +830,4 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase
                // tear down
                $view->unlink($filename);
        }
-
-//     function testEncryption(){
-//     
-//             $key=uniqid();
-//             $file=OC::$SERVERROOT.'/3rdparty/MDB2.php';
-//             $source=file_get_contents($file); //nice large text file
-//             $encrypted=OC_Encryption\Crypt::encrypt($source,$key);
-//             $decrypted=OC_Encryption\Crypt::decrypt($encrypted,$key);
-//             $decrypted=rtrim($decrypted, "\0");
-//             $this->assertNotEquals($encrypted,$source);
-//             $this->assertEquals($decrypted,$source);
-// 
-//             $chunk=substr($source,0,8192);
-//             $encrypted=OC_Encryption\Crypt::encrypt($chunk,$key);
-//             $this->assertEquals(strlen($chunk),strlen($encrypted));
-//             $decrypted=OC_Encryption\Crypt::decrypt($encrypted,$key);
-//             $decrypted=rtrim($decrypted, "\0");
-//             $this->assertEquals($decrypted,$chunk);
-//             
-//             $encrypted=OC_Encryption\Crypt::blockEncrypt($source,$key);
-//             $decrypted=OC_Encryption\Crypt::blockDecrypt($encrypted,$key);
-//             $this->assertNotEquals($encrypted,$source);
-//             $this->assertEquals($decrypted,$source);
-// 
-//             $tmpFileEncrypted=OCP\Files::tmpFile();
-//             OC_Encryption\Crypt::encryptfile($file,$tmpFileEncrypted,$key);
-//             $encrypted=file_get_contents($tmpFileEncrypted);
-//             $decrypted=OC_Encryption\Crypt::blockDecrypt($encrypted,$key);
-//             $this->assertNotEquals($encrypted,$source);
-//             $this->assertEquals($decrypted,$source);
-// 
-//             $tmpFileDecrypted=OCP\Files::tmpFile();
-//             OC_Encryption\Crypt::decryptfile($tmpFileEncrypted,$tmpFileDecrypted,$key);
-//             $decrypted=file_get_contents($tmpFileDecrypted);
-//             $this->assertEquals($decrypted,$source);
-// 
-//             $file=OC::$SERVERROOT.'/core/img/weather-clear.png';
-//             $source=file_get_contents($file); //binary file
-//             $encrypted=OC_Encryption\Crypt::encrypt($source,$key);
-//             $decrypted=OC_Encryption\Crypt::decrypt($encrypted,$key);
-//             $decrypted=rtrim($decrypted, "\0");
-//             $this->assertEquals($decrypted,$source);
-// 
-//             $encrypted=OC_Encryption\Crypt::blockEncrypt($source,$key);
-//             $decrypted=OC_Encryption\Crypt::blockDecrypt($encrypted,$key);
-//             $this->assertEquals($decrypted,$source);
-// 
-//     }
-// 
-//     function testBinary(){
-//             $key=uniqid();
-//     
-//             $file=__DIR__.'/binary';
-//             $source=file_get_contents($file); //binary file
-//             $encrypted=OC_Encryption\Crypt::encrypt($source,$key);
-//             $decrypted=OC_Encryption\Crypt::decrypt($encrypted,$key);
-// 
-//             $decrypted=rtrim($decrypted, "\0");
-//             $this->assertEquals($decrypted,$source);
-// 
-//             $encrypted=OC_Encryption\Crypt::blockEncrypt($source,$key);
-//             $decrypted=OC_Encryption\Crypt::blockDecrypt($encrypted,$key,strlen($source));
-//             $this->assertEquals($decrypted,$source);
-//     }
-
 }
index 334cc743f2c76b1ebd3701cdec08a461ee9180d7..b1bae673e8264d26bc4fe9bba79831b0e04e1f43 100644 (file)
@@ -31,6 +31,7 @@ class Test_Encryption_Keymanager extends \PHPUnit_Framework_TestCase
         */
        public $view;
        public $randomKey;
+       public $dataShort;
 
        function setUp()
        {
@@ -150,34 +151,18 @@ class Test_Encryption_Keymanager extends \PHPUnit_Framework_TestCase
                //$view = new \OC_FilesystemView( '/' . $this->userId . '/files_encryption/keyfiles' );
                Encryption\Keymanager::setFileKey($this->view, $file, $this->userId, $key['key']);
 
-               // Disable encryption proxy to prevent recursive calls
+               // enable encryption proxy
                $proxyStatus = \OC_FileProxy::$enabled;
                \OC_FileProxy::$enabled = true;
 
                // cleanup
                $this->view->unlink('/' . $this->userId . '/files/' . $file);
 
-               // Re-enable proxy - our work is done
+               // change encryption proxy to previous state
                \OC_FileProxy::$enabled = $proxyStatus;
 
        }
 
-//     /**
-//      * @depends testGetPrivateKey
-//      */
-//     function testGetPrivateKey_decrypt() {
-//     
-//             $key = Encryption\Keymanager::getPrivateKey( $this->view, $this->userId );
-//             
-//             # TODO: replace call to Crypt with a mock object?
-//             $decrypted = Encryption\Crypt::symmetricDecryptFileContent( $key, $this->passphrase );
-//             
-//             $this->assertEquals( 1704, strlen( $decrypted ) );
-//             
-//             $this->assertEquals( '-----BEGIN PRIVATE KEY-----', substr( $decrypted, 0, 27 ) );
-//     
-//     }
-
        function testGetUserKeys()
        {
 
@@ -201,4 +186,60 @@ class Test_Encryption_Keymanager extends \PHPUnit_Framework_TestCase
 
                $this->assertArrayHasKey('key', $sslInfoPrivate);
        }
+
+       function testFixPartialFilePath()
+       {
+
+               $partFilename = 'testfile.txt.part';
+               $filename = 'testfile.txt';
+
+               $this->assertTrue(Encryption\Keymanager::isPartialFilePath($partFilename));
+
+               $this->assertEquals('testfile.txt', Encryption\Keymanager::fixPartialFilePath($partFilename));
+
+               $this->assertFalse(Encryption\Keymanager::isPartialFilePath($filename));
+
+               $this->assertEquals('testfile.txt', Encryption\Keymanager::fixPartialFilePath($filename));
+       }
+
+       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');
+
+               // 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);
+
+               // test that data was successfully written
+               $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/');
+
+               // check if share key not exists
+               $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');
+
+               // change encryption proxy to previous state
+               \OC_FileProxy::$enabled = $proxyStatus;
+       }
 }
index e205ff2b62ede372df98893579f7652a62cff79d..1d0cbfbc1def814a512a2f05a8481eac9b251898 100755 (executable)
@@ -312,7 +312,7 @@ class Test_Encryption_Share extends \PHPUnit_Framework_TestCase
                        $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);
 
                        // 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'));
@@ -621,7 +621,8 @@ class Test_Encryption_Share extends \PHPUnit_Framework_TestCase
                $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'));
 
-               \OCA\Encryption\Helper::adminDisableRecovery('test123');
+               $this->assertTrue(\OCA\Encryption\Helper::adminEnableRecovery(null, 'test123'));
+               $this->assertTrue(\OCA\Encryption\Helper::adminDisableRecovery('test123'));
                $this->assertEquals(0, \OC_Appconfig::getValue('files_encryption', 'recoveryAdminEnabled'));
        }
 
index 633cc9e4fce726d7f65a851b1b3fbce1378ba3b0..3765d986e12c22d537ed416666617737352f725a 100644 (file)
 <?php
-// /**
-//  * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
-//  * This file is licensed under the Affero General Public License version 3 or
-//  * later.
-//  * See the COPYING-README file.
-//  */
-//  
-// namespace OCA\Encryption;
-// 
-// class Test_Stream extends \PHPUnit_Framework_TestCase {
-// 
-//     function setUp() {
-//     
-//             \OC_Filesystem::mount( 'OC_Filestorage_Local', array(), '/' );
-//     
-//             $this->empty = '';
-//     
-//             $this->stream = new Stream();
-//             
-//             $this->dataLong = file_get_contents( realpath( dirname(__FILE__).'/../lib/crypt.php' ) );
-//             $this->dataShort = 'hats';
-//             
-//             $this->emptyTmpFilePath = \OCP\Files::tmpFile();
-//             
-//             $this->dataTmpFilePath = \OCP\Files::tmpFile();
-//             
-//             file_put_contents( $this->dataTmpFilePath, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ut gravida lorem. Ut turpis felis, pulvinar a semper sed, adipiscing id dolor. Pellentesque auctor nisi id magna consequat sagittis. Curabitur dapibus enim sit amet elit pharetra tincidunt feugiat nisl imperdiet. Ut convallis libero in urna ultrices accumsan. Donec sed odio eros. Donec viverra mi quis quam pulvinar at malesuada arcu rhoncus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. In rutrum accumsan ultricies. Mauris vitae nisi at sem facilisis semper ac in est." );
-//     
-//     }
-//     
-//     function testStreamOpen() {
-//             
-//             $stream1 = new Stream();
-//             
-//             $handle1 = $stream1->stream_open( $this->emptyTmpFilePath, 'wb', array(), $this->empty );
-//             
-//             // Test that resource was returned successfully
-//             $this->assertTrue( $handle1 );
-//             
-//             // Test that file has correct size
-//             $this->assertEquals( 0, $stream1->size );
-//             
-//             // Test that path is correct
-//             $this->assertEquals( $this->emptyTmpFilePath, $stream1->rawPath );
-//             
-//             $stream2 = new Stream();
-//             
-//             $handle2 = $stream2->stream_open( 'crypt://' . $this->emptyTmpFilePath, 'wb', array(), $this->empty );
-//             
-//             // Test that protocol identifier is removed from path
-//             $this->assertEquals( $this->emptyTmpFilePath, $stream2->rawPath );
-// 
-//             // "Stat failed error" prevents this test from executing
-// //          $stream3 = new Stream();
-// //          
-// //          $handle3 = $stream3->stream_open( $this->dataTmpFilePath, 'r', array(), $this->empty );
-// //          
-// //          $this->assertEquals( 0, $stream3->size );
-//     
-//     }
-//     
-//     function testStreamWrite() {
-//             
-//             $stream1 = new Stream();
-//             
-//             $handle1 = $stream1->stream_open( $this->emptyTmpFilePath, 'r+b', array(), $this->empty );
-//             
-//             # what about the keymanager? there is no key for the newly created temporary file!
-//             
-//             $stream1->stream_write( $this->dataShort );
-//     
-//     }
-// 
-// //  function getStream( $id, $mode, $size ) {
-// //  
-// //          if ( $id === '' ) {
-// //                  
-// //                  $id = uniqid();
-// //          }
-// //          
-// //          
-// //          if ( !isset( $this->tmpFiles[$id] ) ) {
-// //          
-// //                  // If tempfile with given name does not already exist, create it
-// //                  
-// //                  $file = OCP\Files::tmpFile();
-// //                  
-// //                  $this->tmpFiles[$id] = $file;
-// //          
-// //          } else {
-// //          
-// //                  $file = $this->tmpFiles[$id];
-// //          
-// //          }
-// //          
-// //          $stream = fopen( $file, $mode );
-// //          
-// //          Stream::$sourceStreams[$id] = array( 'path' => 'dummy' . $id, 'stream' => $stream, 'size' => $size );
-// //          
-// //          return fopen( 'crypt://streams/'.$id, $mode );
-// //  
-// //  }
-// // 
-// //  function testStream(  ){
-// // 
-// //          $stream = $this->getStream( 'test1', 'w', strlen( 'foobar' ) );
-// // 
-// //          fwrite( $stream, 'foobar' );
-// // 
-// //          fclose( $stream );
-// // 
-// // 
-// //          $stream = $this->getStream( 'test1', 'r', strlen( 'foobar' ) );
-// // 
-// //          $data = fread( $stream, 6 );
-// // 
-// //          fclose( $stream );
-// // 
-// //          $this->assertEquals( 'foobar', $data );
-// // 
-// // 
-// //          $file = OC::$SERVERROOT.'/3rdparty/MDB2.php';
-// // 
-// //          $source = fopen( $file, 'r' );
-// // 
-// //          $target = $this->getStream( 'test2', 'w', 0 );
-// // 
-// //          OCP\Files::streamCopy( $source, $target );
-// // 
-// //          fclose( $target );
-// // 
-// //          fclose( $source );
-// // 
-// // 
-// //          $stream = $this->getStream( 'test2', 'r', filesize( $file ) );
-// // 
-// //          $data = stream_get_contents( $stream );
-// // 
-// //          $original = file_get_contents( $file );
-// // 
-// //          $this->assertEquals( strlen( $original ), strlen( $data ) );
-// // 
-// //          $this->assertEquals( $original, $data );
-// // 
-// //  }
-// 
-// }
-// 
-// // class Test_CryptStream extends PHPUnit_Framework_TestCase {
-// //  private $tmpFiles=array();
-// //  
-// //  function testStream(){
-// //          $stream=$this->getStream('test1','w',strlen('foobar'));
-// //          fwrite($stream,'foobar');
-// //          fclose($stream);
-// // 
-// //          $stream=$this->getStream('test1','r',strlen('foobar'));
-// //          $data=fread($stream,6);
-// //          fclose($stream);
-// //          $this->assertEquals('foobar',$data);
-// // 
-// //          $file=OC::$SERVERROOT.'/3rdparty/MDB2.php';
-// //          $source=fopen($file,'r');
-// //          $target=$this->getStream('test2','w',0);
-// //          OCP\Files::streamCopy($source,$target);
-// //          fclose($target);
-// //          fclose($source);
-// // 
-// //          $stream=$this->getStream('test2','r',filesize($file));
-// //          $data=stream_get_contents($stream);
-// //          $original=file_get_contents($file);
-// //          $this->assertEquals(strlen($original),strlen($data));
-// //          $this->assertEquals($original,$data);
-// //  }
-// // 
-// //  /**
-// //   * get a cryptstream to a temporary file
-// //   * @param string $id
-// //   * @param string $mode
-// //   * @param int size
-// //   * @return resource
-// //   */
-// //  function getStream($id,$mode,$size){
-// //          if($id===''){
-// //                  $id=uniqid();
-// //          }
-// //          if(!isset($this->tmpFiles[$id])){
-// //                  $file=OCP\Files::tmpFile();
-// //                  $this->tmpFiles[$id]=$file;
-// //          }else{
-// //                  $file=$this->tmpFiles[$id];
-// //          }
-// //          $stream=fopen($file,$mode);
-// //          OC_CryptStream::$sourceStreams[$id]=array('path'=>'dummy'.$id,'stream'=>$stream,'size'=>$size);
-// //          return fopen('crypt://streams/'.$id,$mode);
-// //  }
-// // 
-// //  function testBinary(){
-// //          $file=__DIR__.'/binary';
-// //          $source=file_get_contents($file);
-// // 
-// //          $stream=$this->getStream('test','w',strlen($source));
-// //          fwrite($stream,$source);
-// //          fclose($stream);
-// // 
-// //          $stream=$this->getStream('test','r',strlen($source));
-// //          $data=stream_get_contents($stream);
-// //          fclose($stream);
-// //          $this->assertEquals(strlen($data),strlen($source));
-// //          $this->assertEquals($source,$data);
-// // 
-// //          $file=__DIR__.'/zeros';
-// //          $source=file_get_contents($file);
-// // 
-// //          $stream=$this->getStream('test2','w',strlen($source));
-// //          fwrite($stream,$source);
-// //          fclose($stream);
-// // 
-// //          $stream=$this->getStream('test2','r',strlen($source));
-// //          $data=stream_get_contents($stream);
-// //          fclose($stream);
-// //          $this->assertEquals(strlen($data),strlen($source));
-// //          $this->assertEquals($source,$data);
-// //  }
-// // }
+/**
+ * ownCloud
+ *
+ * @author Florin Peter
+ * @copyright 2013 Florin Peter <owncloud@florin-peter.de>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+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;
+
+/**
+ * Class Test_Encryption_Stream
+ * @brief this class provide basic stream tests
+ */
+class Test_Encryption_Stream extends \PHPUnit_Framework_TestCase
+{
+
+       public $userId;
+       public $pass;
+       /**
+        * @var \OC_FilesystemView
+        */
+       public $view;
+       public $dataShort;
+       public $stateFilesTrashbin;
+
+       function setUp()
+       {
+               // reset backend
+               \OC_User::useBackend('database');
+
+               // set user id
+               \OC_User::setUserId('admin');
+               $this->userId = 'admin';
+               $this->pass = 'admin';
+
+               // init filesystem view
+               $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');
+
+               // 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);
+       }
+
+       function tearDown()
+       {
+               // reset app files_trashbin
+               if ($this->stateFilesTrashbin) {
+                       OC_App::enable('files_trashbin');
+               } else {
+                       OC_App::disable('files_trashbin');
+               }
+
+               // clear all proxies
+               \OC_FileProxy::clearProxies();
+       }
+
+       function testStreamOptions() {
+               $filename = '/tmp-' . time();
+               $view = new \OC\Files\View('/' . $this->userId . '/files');
+
+               // Save short data as encrypted file using stream wrapper
+               $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
+
+               // Test that data was successfully written
+               $this->assertTrue(is_int($cryptedFile));
+
+               $handle = $view->fopen($filename, 'r');
+
+               // check if stream is at position zero
+               $this->assertEquals(0,ftell($handle));
+
+               // set stream options
+               $this->assertTrue(flock($handle, LOCK_SH));
+               $this->assertTrue(flock($handle, LOCK_UN));
+
+               // tear down
+               $view->unlink($filename);
+       }
+
+       function testStreamSetBlocking() {
+               $filename = '/tmp-' . time();
+               $view = new \OC\Files\View('/' . $this->userId . '/files');
+
+               // Save short data as encrypted file using stream wrapper
+               $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
+
+               // Test that data was successfully written
+               $this->assertTrue(is_int($cryptedFile));
+
+               $handle = $view->fopen($filename, 'r');
+
+               // set stream options
+               $this->assertTrue(stream_set_blocking($handle,1));
+
+               // tear down
+               $view->unlink($filename);
+       }
+
+       function testStreamSetTimeout() {
+               $filename = '/tmp-' . time();
+               $view = new \OC\Files\View('/' . $this->userId . '/files');
+
+               // Save short data as encrypted file using stream wrapper
+               $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
+
+               // Test that data was successfully written
+               $this->assertTrue(is_int($cryptedFile));
+
+               $handle = $view->fopen($filename, 'r');
+
+               // set stream options
+               $this->assertFalse(stream_set_timeout($handle,1));
+
+               // tear down
+               $view->unlink($filename);
+       }
+
+       function testStreamSetWriteBuffer() {
+               $filename = '/tmp-' . time();
+               $view = new \OC\Files\View('/' . $this->userId . '/files');
+
+               // Save short data as encrypted file using stream wrapper
+               $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
+
+               // Test that data was successfully written
+               $this->assertTrue(is_int($cryptedFile));
+
+               $handle = $view->fopen($filename, 'r');
+
+               // set stream options
+               $this->assertEquals(0, stream_set_write_buffer($handle,1024));
+
+               // tear down
+               $view->unlink($filename);
+       }
+}
\ No newline at end of file
index 667ee24f04ab0002a99c190610bca9e4fddd1eba..a2be8a40417f4ed754ae477a6002dcb7cd21d0f5 100755 (executable)
@@ -217,4 +217,61 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase
 
                $this->assertEquals($file, $filename);
        }
+
+       function testIsSharedPath() {
+               $sharedPath = '/user1/files/Shared/test';
+               $path = '/user1/files/test';
+
+               $this->assertTrue($this->util->isSharedPath($sharedPath));
+
+               $this->assertFalse($this->util->isSharedPath($path));
+       }
+
+       function testEncryptLagacyFiles()
+       {
+               $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);
+
+               $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['encrypted'] = true;
+               $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);
+
+               $this->assertTrue(OCA\Encryption\Hooks::login($params));
+
+               $this->assertEquals($this->lagacyKey, $_SESSION['legacyKey']);
+
+               $files = $util->findEncFiles('/' . $this->userId . '/files/');
+
+               $this->assertTrue(is_array($files));
+
+               $found = false;
+               foreach($files['encrypted'] as $encryptedFile) {
+                       if($encryptedFile['name'] === 'legacy-encrypted-text.txt') {
+                               $found = true;
+                               break;
+                       }
+               }
+
+               $this->assertTrue($found);
+       }
 }
\ No newline at end of file