From de9d3797ffc712f8fa3552411c82b24486461daf Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Mon, 21 Jul 2014 23:36:20 +0200 Subject: [PATCH] update existing unit tests --- apps/files_encryption/lib/crypt.php | 2 +- apps/files_encryption/tests/crypt.php | 129 ++------------------- apps/files_encryption/tests/keymanager.php | 4 +- apps/files_encryption/tests/share.php | 6 +- apps/files_encryption/tests/util.php | 2 +- 5 files changed, 16 insertions(+), 127 deletions(-) diff --git a/apps/files_encryption/lib/crypt.php b/apps/files_encryption/lib/crypt.php index 8ca96899f88..7974598729e 100755 --- a/apps/files_encryption/lib/crypt.php +++ b/apps/files_encryption/lib/crypt.php @@ -312,7 +312,7 @@ class Crypt { * * This function decrypts a file */ - public static function symmetricDecryptFileContent($keyfileContent, $passphrase = '', $cipher = 'AES-128-CFB') { + public static function symmetricDecryptFileContent($keyfileContent, $passphrase = '', $cipher = Crypt::DEFAULT_CIPHER) { if (!$keyfileContent) { diff --git a/apps/files_encryption/tests/crypt.php b/apps/files_encryption/tests/crypt.php index a1a51c749b0..d1ff6eec7dc 100755 --- a/apps/files_encryption/tests/crypt.php +++ b/apps/files_encryption/tests/crypt.php @@ -121,7 +121,9 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase { // test successful decrypt $crypted = Encryption\Crypt::symmetricEncryptFileContent($this->genPrivateKey, 'hat'); - $decrypted = Encryption\Crypt::decryptPrivateKey($crypted, 'hat'); + $header = Encryption\Crypt::generateHeader(); + + $decrypted = Encryption\Crypt::decryptPrivateKey($header . $crypted, 'hat'); $this->assertEquals($this->genPrivateKey, $decrypted); @@ -158,8 +160,6 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase { $filename = 'tmp-' . uniqid() . '.test'; - $util = new Encryption\Util(new \OC\Files\View(), $this->userId); - $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/'. $filename, $this->dataShort); // Test that data was successfully written @@ -178,26 +178,11 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase { // Check that the file was encrypted before being written to disk $this->assertNotEquals($this->dataShort, $retreivedCryptedFile); - // Get the encrypted keyfile - $encKeyfile = Encryption\Keymanager::getFileKey($this->view, $util, $filename); - - // Attempt to fetch the user's shareKey - $shareKey = Encryption\Keymanager::getShareKey($this->view, $this->userId, $util, $filename); - - // get session - $session = new \OCA\Encryption\Session($this->view); - - // get private key - $privateKey = $session->getPrivateKey($this->userId); - - // Decrypt keyfile with shareKey - $plainKeyfile = Encryption\Crypt::multiKeyDecrypt($encKeyfile, $shareKey, $privateKey); - - // Manually decrypt - $manualDecrypt = Encryption\Crypt::symmetricDecryptFileContent($retreivedCryptedFile, $plainKeyfile); + // Get file contents with the encryption wrapper + $decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename); // Check that decrypted data matches - $this->assertEquals($this->dataShort, $manualDecrypt); + $this->assertEquals($this->dataShort, $decrypted); // Teardown $this->view->unlink($this->userId . '/files/' . $filename); @@ -217,8 +202,6 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase { // Generate a a random filename $filename = 'tmp-' . uniqid() . '.test'; - $util = new Encryption\Util(new \OC\Files\View(), $this->userId); - // Save long data as encrypted file using stream wrapper $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong . $this->dataLong); @@ -239,50 +222,9 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase { // 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 - $r = preg_split('/(00iv00.{16,18})/', $retreivedCryptedFile, NULL, PREG_SPLIT_DELIM_CAPTURE); - - //print_r($r); - - // Join IVs and their respective data chunks - $e = array(); - $i = 0; - while ($i < count($r)-1) { - $e[] = $r[$i] . $r[$i+1]; - $i = $i + 2; - } - - //print_r($e); - - // Get the encrypted keyfile - $encKeyfile = Encryption\Keymanager::getFileKey($this->view, $util, $filename); - - // Attempt to fetch the user's shareKey - $shareKey = Encryption\Keymanager::getShareKey($this->view, $this->userId, $util, $filename); - - // get session - $session = new \OCA\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 = ''; + $decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename); - // Manually decrypt chunk - foreach ($e as $chunk) { - - $chunkDecrypt = Encryption\Crypt::symmetricDecryptFileContent($chunk, $plainKeyfile); - - // Assemble decrypted chunks - $decrypt .= $chunkDecrypt; - - } - - $this->assertEquals($this->dataLong . $this->dataLong, $decrypt); + $this->assertEquals($this->dataLong . $this->dataLong, $decrypted); // Teardown @@ -292,59 +234,6 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase { } - /** - * @medium - * Test that data that is read by the crypto stream wrapper - */ - function testSymmetricStreamDecryptShortFileContent() { - - $filename = 'tmp-' . uniqid(); - - // Save long data as encrypted file using stream wrapper - $cryptedFile = file_put_contents('crypt:///'. $this->userId . '/files/' . $filename, $this->dataShort); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - // Disable encryption proxy to prevent recursive calls - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = false; - - $this->assertTrue(Encryption\Crypt::isEncryptedMeta($filename)); - - \OC_FileProxy::$enabled = $proxyStatus; - - // Get file decrypted contents - $decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename); - - $this->assertEquals($this->dataShort, $decrypt); - - // tear down - $this->view->unlink($this->userId . '/files/' . $filename); - } - - /** - * @medium - */ - function testSymmetricStreamDecryptLongFileContent() { - - $filename = 'tmp-' . uniqid(); - - // Save long data as encrypted file using stream wrapper - $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong); - - // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - // Get file decrypted contents - $decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename); - - $this->assertEquals($this->dataLong, $decrypt); - - // tear down - $this->view->unlink($this->userId . '/files/' . $filename); - } - /** * @medium */ @@ -354,7 +243,7 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase { $this->assertFalse(Encryption\Crypt::isCatfileContent($this->legacyEncryptedData)); - $keyfileContent = Encryption\Crypt::symmetricEncryptFileContent($this->dataUrl, 'hat'); + $keyfileContent = Encryption\Crypt::symmetricEncryptFileContent($this->dataUrl, 'hat', 'AES-128-CFB'); $this->assertTrue(Encryption\Crypt::isCatfileContent($keyfileContent)); diff --git a/apps/files_encryption/tests/keymanager.php b/apps/files_encryption/tests/keymanager.php index e779f8341e6..b5537837051 100644 --- a/apps/files_encryption/tests/keymanager.php +++ b/apps/files_encryption/tests/keymanager.php @@ -107,7 +107,7 @@ class Test_Encryption_Keymanager extends \PHPUnit_Framework_TestCase { $key = Encryption\Keymanager::getPrivateKey($this->view, $this->userId); - $privateKey = Encryption\Crypt::symmetricDecryptFileContent($key, $this->pass); + $privateKey = Encryption\Crypt::decryptPrivateKey($key, $this->pass); $res = openssl_pkey_get_private($privateKey); @@ -189,7 +189,7 @@ class Test_Encryption_Keymanager extends \PHPUnit_Framework_TestCase { $this->assertArrayHasKey('key', $sslInfoPublic); - $privateKey = Encryption\Crypt::symmetricDecryptFileContent($keys['privateKey'], $this->pass); + $privateKey = Encryption\Crypt::decryptPrivateKey($keys['privateKey'], $this->pass); $resPrivate = openssl_pkey_get_private($privateKey); diff --git a/apps/files_encryption/tests/share.php b/apps/files_encryption/tests/share.php index 7bbea6488bc..1f1304bb527 100755 --- a/apps/files_encryption/tests/share.php +++ b/apps/files_encryption/tests/share.php @@ -540,9 +540,9 @@ class Test_Encryption_Share extends \PHPUnit_Framework_TestCase { . $this->filename . '.' . $publicShareKeyId . '.shareKey')); // some hacking to simulate public link - $GLOBALS['app'] = 'files_sharing'; - $GLOBALS['fileOwner'] = \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1; - \OC_User::setUserId(false); + //$GLOBALS['app'] = 'files_sharing'; + //$GLOBALS['fileOwner'] = \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1; + \Test_Encryption_Util::logoutHelper(); // get file contents $retrievedCryptedFile = file_get_contents('crypt:///' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); diff --git a/apps/files_encryption/tests/util.php b/apps/files_encryption/tests/util.php index 3142b83c5a6..480f14852f3 100755 --- a/apps/files_encryption/tests/util.php +++ b/apps/files_encryption/tests/util.php @@ -490,7 +490,7 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase { public static function logoutHelper() { \OC_Util::tearDownFS(); - \OC_User::setUserId(''); + \OC_User::setUserId(false); \OC\Files\Filesystem::tearDown(); } -- 2.39.5