diff options
author | Markus Goetz <markus@woboq.com> | 2013-10-05 18:00:46 +0200 |
---|---|---|
committer | Bjoern Schiessle <schiessle@owncloud.com> | 2013-10-23 16:28:43 +0200 |
commit | af58360434cdd6cb808d80d20fdbd293e1c57f0c (patch) | |
tree | 51d148edb457357e7ea18f9d2cdf8421662e080d /apps/files_encryption | |
parent | 959b0f912559ae0c6751cb5c0d14a9be5b58f19b (diff) | |
download | nextcloud-server-af58360434cdd6cb808d80d20fdbd293e1c57f0c.tar.gz nextcloud-server-af58360434cdd6cb808d80d20fdbd293e1c57f0c.zip |
files_encryption: Fix getFileSize()
For certain file sizes, we rounded to the wrong chunk number
and the returned bogus results. This should fix
https://github.com/owncloud/mirall/issues/1009
Conflicts:
apps/files_encryption/tests/util.php
Diffstat (limited to 'apps/files_encryption')
-rw-r--r-- | apps/files_encryption/lib/util.php | 11 | ||||
-rwxr-xr-x | apps/files_encryption/tests/util.php | 30 |
2 files changed, 38 insertions, 3 deletions
diff --git a/apps/files_encryption/lib/util.php b/apps/files_encryption/lib/util.php index b9592a32cb2..0d34af043a1 100644 --- a/apps/files_encryption/lib/util.php +++ b/apps/files_encryption/lib/util.php @@ -508,11 +508,18 @@ class Util { ) { // get the size from filesystem - $fullPath = $this->view->getLocalFile($path); $size = $this->view->filesize($path); + // fast path, else the calculation for $lastChunkNr is bogus + if ($size === 0) { + \OC_FileProxy::$enabled = $proxyStatus; + return 0; + } + // calculate last chunk nr - $lastChunkNr = floor($size / 8192); + // next highest is end of chunks, one subtracted is last one + // we have to read the last chunk, we can't just calculate it (because of padding etc) + $lastChunkNr = ceil($size/ 8192) - 1; $lastChunkSize = $size - ($lastChunkNr * 8192); // open stream diff --git a/apps/files_encryption/tests/util.php b/apps/files_encryption/tests/util.php index eddc4c6b3ff..1b93bc36c8e 100755 --- a/apps/files_encryption/tests/util.php +++ b/apps/files_encryption/tests/util.php @@ -242,6 +242,34 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase { } /** +< * @brief Test that data that is read by the crypto stream wrapper + */ + function testGetFileSize() { + \Test_Encryption_Util::loginHelper(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER1); + + $filename = 'tmp-' . time(); + $externalFilename = '/' . $this->userId . '/files/' . $filename; + + // Test for 0 byte files + $problematicFileSizeData = ""; + $cryptedFile = $this->view->file_put_contents($externalFilename, $problematicFileSizeData); + $this->assertTrue(is_int($cryptedFile)); + $this->assertEquals($this->util->getFileSize($externalFilename), 0); + $decrypt = $this->view->file_get_contents($externalFilename); + $this->assertEquals($problematicFileSizeData, $decrypt); + $this->view->unlink($this->userId . '/files/' . $filename); + + // Test a file with 18377 bytes as in https://github.com/owncloud/mirall/issues/1009 + $problematicFileSizeData = str_pad("", 18377, "abc"); + $cryptedFile = $this->view->file_put_contents($externalFilename, $problematicFileSizeData); + $this->assertTrue(is_int($cryptedFile)); + $this->assertEquals($this->util->getFileSize($externalFilename), 18377); + $decrypt = $this->view->file_get_contents($externalFilename); + $this->assertEquals($problematicFileSizeData, $decrypt); + $this->view->unlink($this->userId . '/files/' . $filename); + } + + /** * @medium */ function testIsSharedPath() { @@ -333,7 +361,7 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase { /** * helper function to set migration status to the right value * to be able to test the migration path - * + * * @param $status needed migration status for test * @param $user for which user the status should be set * @return boolean |