mirror of
https://github.com/nextcloud/server.git
synced 2026-04-23 23:27:46 -04:00
update existing unit tests
This commit is contained in:
parent
4bbdcfbccf
commit
de9d3797ff
5 changed files with 16 additions and 127 deletions
|
|
@ -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) {
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
$decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename);
|
||||
|
||||
//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 = '';
|
||||
|
||||
// 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));
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue