From d22795d68b4937bc6dba6d742b0f7b503cb32228 Mon Sep 17 00:00:00 2001
From: Florin Peter <github@florin-peter.de>
Date: Mon, 29 Apr 2013 23:41:49 +0200
Subject: [PATCH] fixed test for crypt and keymanager disabled encryption file
 proxy in test/lib/cache/file.php

---
 apps/files_encryption/test/crypt.php      | 87 +++++++++++++----------
 apps/files_encryption/test/keymanager.php | 37 ++++++----
 tests/lib/cache/file.php                  |  7 +-
 3 files changed, 78 insertions(+), 53 deletions(-)

diff --git a/apps/files_encryption/test/crypt.php b/apps/files_encryption/test/crypt.php
index 9c5e43e242..7f9572f426 100755
--- a/apps/files_encryption/test/crypt.php
+++ b/apps/files_encryption/test/crypt.php
@@ -34,7 +34,9 @@ use OCA\Encryption;
 class Test_Crypt extends \PHPUnit_Framework_TestCase {
 	
 	function setUp() {
-		
+        // reset backend
+        \OC_User::useBackend('database');
+
 		// set content for encrypting / decrypting in tests
 		$this->dataLong = file_get_contents( realpath( dirname(__FILE__).'/../lib/crypt.php' ) );
 		$this->dataShort = 'hats';
@@ -54,13 +56,10 @@ class Test_Crypt extends \PHPUnit_Framework_TestCase {
 		$this->pass = 'admin';
 
         $userHome = \OC_User::getHome($this->userId);
-        if(!file_exists($userHome)) {
-            mkdir($userHome, 0777, true);
-        }
-        $dataDir = str_replace('/'.$this->userId, '', $userHome);
+        $this->dataDir = str_replace('/'.$this->userId, '', $userHome);
 
-        \OC\Files\Filesystem::mount( 'OC_Filestorage_Local', array('datadir' => $dataDir), '/' );
         \OC\Files\Filesystem::init($this->userId, '/');
+        \OC\Files\Filesystem::mount( 'OC_Filestorage_Local', array('datadir' => $this->dataDir), '/' );
 	}
 	
 	function tearDown() {
@@ -225,7 +224,7 @@ class Test_Crypt extends \PHPUnit_Framework_TestCase {
 // 		
 // 	}
 	
-	function testSymmetricStreamEncryptShortFileContent() { 
+	function testSymmetricStreamEncryptShortFileContent() {
 		
 		$filename = 'tmp-'.time().'.test';
 
@@ -234,9 +233,15 @@ class Test_Crypt extends \PHPUnit_Framework_TestCase {
 		// Test that data was successfully written
 		$this->assertTrue( is_int( $cryptedFile ) );
 
+        // Disable encryption proxy to prevent recursive calls
+        $proxyStatus = \OC_FileProxy::$enabled;
+        \OC_FileProxy::$enabled = false;
+
         // Get file contents without using any wrapper to get it's actual contents on disk
-		$absolutePath = \OC\Files\Filesystem::getLocalFile($this->userId . '/files/' . $filename);
-        $retreivedCryptedFile = file_get_contents($absolutePath);
+        $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 );
@@ -261,7 +266,11 @@ class Test_Crypt extends \PHPUnit_Framework_TestCase {
 
 		// Check that decrypted data matches
 		$this->assertEquals( $this->dataShort, $manualDecrypt );
-		
+
+        // Teardown
+        $this->view->unlink( $filename );
+
+        Encryption\Keymanager::deleteFileKey( $this->view, $this->userId, $filename );
 	}
 	
 	/**
@@ -273,7 +282,7 @@ class Test_Crypt extends \PHPUnit_Framework_TestCase {
 	function testSymmetricStreamEncryptLongFileContent() {
 		
 		// Generate a a random filename
-		$filename = 'tmp-'.time();
+		$filename = 'tmp-'.time().'.test';
 		
 		// Save long data as encrypted file using stream wrapper
 		$cryptedFile = file_put_contents( 'crypt://' . $filename, $this->dataLong.$this->dataLong );
@@ -281,12 +290,18 @@ class Test_Crypt extends \PHPUnit_Framework_TestCase {
 		// Test that data was successfully written
 		$this->assertTrue( is_int( $cryptedFile ) );
 		
-		// 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 );
-		
-// 		echo "\n\n\$retreivedCryptedFile = $retreivedCryptedFile\n\n";
-		
-		// Check that the file was encrypted before being written to disk
+		// Disable encryption proxy to prevent recursive calls
+        $proxyStatus = \OC_FileProxy::$enabled;
+        \OC_FileProxy::$enabled = false;
+
+        // Get file contents without using any wrapper to get it's actual contents on disk
+        $retreivedCryptedFile = $this->view->file_get_contents($this->userId . '/files/' . $filename);
+
+        // Re-enable proxy - our work is done
+        \OC_FileProxy::$enabled = $proxyStatus;
+
+
+        // Check that the file was encrypted before being written to disk
 		$this->assertNotEquals( $this->dataLong.$this->dataLong, $retreivedCryptedFile );
 		
 		// Manuallly split saved file into separate IVs and encrypted chunks
@@ -298,39 +313,35 @@ class Test_Crypt extends \PHPUnit_Framework_TestCase {
 		$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[12].$r[13] );//.$r[11], $r[12].$r[13], $r[14] );
 		
 		//print_r($e);
-		
-		
-		// Get private key
-		$encryptedPrivateKey = Encryption\Keymanager::getPrivateKey( $this->view, $this->userId );
-		
-		$decryptedPrivateKey = Encryption\Crypt::symmetricDecryptFileContent( $encryptedPrivateKey, $this->pass );
-		
-		
-		// Get keyfile
-		$encryptedKeyfile = Encryption\Keymanager::getFileKey( $this->view, $this->userId, $filename );
-		
-		$decryptedKeyfile = Encryption\Crypt::keyDecrypt( $encryptedKeyfile, $decryptedPrivateKey );
-		
-		
+
+        // Get the encrypted keyfile
+        $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 );
+
+        // get session
+        $session = new Encryption\Session( $this->view );
+
+        // get private key
+        $privateKey = $session->getPrivateKey( $this->userId );
+
+        // Decrypt keyfile with shareKey
+        $plainKeyfile = Encryption\Crypt::multiKeyDecrypt( $encKeyfile, $shareKey, $privateKey );
+
 		// Set var for reassembling decrypted content
 		$decrypt = '';
 		
 		// Manually decrypt chunk
 		foreach ($e as $e) {
-		
-// 			echo "\n\$e = $e";
 			
-			$chunkDecrypt = Encryption\Crypt::symmetricDecryptFileContent( $e, $decryptedKeyfile );
+			$chunkDecrypt = Encryption\Crypt::symmetricDecryptFileContent( $e, $plainKeyfile );
 			
 			// Assemble decrypted chunks
 			$decrypt .= $chunkDecrypt;
 			
-// 			echo "\n\$chunkDecrypt = $chunkDecrypt";
-			
 		}
 		
-// 		echo "\n\$decrypt = $decrypt";
-		
 		$this->assertEquals( $this->dataLong.$this->dataLong, $decrypt );
 		
 		// Teardown
diff --git a/apps/files_encryption/test/keymanager.php b/apps/files_encryption/test/keymanager.php
index bf453fe316..3dba6d0df9 100644
--- a/apps/files_encryption/test/keymanager.php
+++ b/apps/files_encryption/test/keymanager.php
@@ -24,7 +24,9 @@ use OCA\Encryption;
 class Test_Keymanager extends \PHPUnit_Framework_TestCase {
 	
 	function setUp() {
-		
+        // reset backend
+        \OC_User::useBackend('database');
+
 		\OC_FileProxy::$enabled = false;
 		
 		// set content for encrypting / decrypting in tests
@@ -44,9 +46,12 @@ class Test_Keymanager extends \PHPUnit_Framework_TestCase {
 		\OC_User::setUserId( 'admin' );
 		$this->userId = 'admin';
 		$this->pass = 'admin';
-		
-		\OC_Filesystem::init( '/' );
-		\OC_Filesystem::mount( 'OC_Filestorage_Local', array('datadir' => \OC_User::getHome($this->userId)), '/' );
+
+        $userHome = \OC_User::getHome($this->userId);
+        $this->dataDir = str_replace('/'.$this->userId, '', $userHome);
+
+        \OC_Filesystem::init( $this->userId, '/' );
+		\OC_Filesystem::mount( 'OC_Filestorage_Local', array('datadir' => $this->dataDir), '/' );
 	
 	}
 	
@@ -61,7 +66,7 @@ class Test_Keymanager extends \PHPUnit_Framework_TestCase {
 		$key = Encryption\Keymanager::getPrivateKey( $this->view, $this->userId );
 		 
 		// Will this length vary? Perhaps we should use a range instead
-		$this->assertEquals( 2296, strlen( $key ) );
+		$this->assertEquals( 4388, strlen( $key ) );
 	
 	}
 	
@@ -69,7 +74,7 @@ class Test_Keymanager extends \PHPUnit_Framework_TestCase {
 
 		$key = Encryption\Keymanager::getPublicKey( $this->view, $this->userId );
 		
-		$this->assertEquals( 451, strlen( $key ) );
+		$this->assertEquals( 800, strlen( $key ) );
 		
 		$this->assertEquals( '-----BEGIN PUBLIC KEY-----', substr( $key, 0, 26 ) );
 	}
@@ -81,11 +86,19 @@ class Test_Keymanager extends \PHPUnit_Framework_TestCase {
 	
 		$key = Encryption\Crypt::symmetricEncryptFileContentKeyfile( $this->randomKey, 'hat' );
 		
-		$path = 'unittest-'.time().'txt';
-		
+		$file = 'unittest-'.time().'.txt';
+
+        // Disable encryption proxy to prevent recursive calls
+        $proxyStatus = \OC_FileProxy::$enabled;
+        \OC_FileProxy::$enabled = false;
+
+        $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, $path, $this->userId, $key['key'] );
+		Encryption\Keymanager::setFileKey( $this->view, $file, $this->userId, $key['key'] );
 	
 	}
 	
@@ -109,9 +122,9 @@ class Test_Keymanager extends \PHPUnit_Framework_TestCase {
 	
 		$keys = Encryption\Keymanager::getUserKeys( $this->view, $this->userId );
 		
-		$this->assertEquals( 451, strlen( $keys['publicKey'] ) );
+		$this->assertEquals( 800, strlen( $keys['publicKey'] ) );
 		$this->assertEquals( '-----BEGIN PUBLIC KEY-----', substr( $keys['publicKey'], 0, 26 ) );
-		$this->assertEquals( 2296, strlen( $keys['privateKey'] ) );
+		$this->assertEquals( 4388, strlen( $keys['privateKey'] ) );
 	
 	}
 	
diff --git a/tests/lib/cache/file.php b/tests/lib/cache/file.php
index 5dcd326880..d113f90768 100644
--- a/tests/lib/cache/file.php
+++ b/tests/lib/cache/file.php
@@ -33,9 +33,10 @@ class Test_Cache_File extends Test_Cache {
 		OC_Hook::clear('OC_Filesystem');
 		
 		//enable only the encryption hook if needed
-		if(OC_App::isEnabled('files_encryption')) {
-			OC_FileProxy::register(new OC_FileProxy_Encryption());
-		}
+        //not used right now
+		//if(OC_App::isEnabled('files_encryption')) {
+		//	OC_FileProxy::register(new OCA\Encryption\Proxy());
+		//}
 		
 		//set up temporary storage
 		\OC\Files\Filesystem::clearMounts();
-- 
GitLab