From c78e3c4a7fa1d2f474ab58551e67a50e093f6ed8 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Thu, 29 Jan 2015 13:09:44 +0100 Subject: make sure that we always create a public share key for remote shares --- apps/files_encryption/lib/util.php | 2 +- apps/files_encryption/tests/share.php | 123 +++++++++++++++++++++++++--------- 2 files changed, 93 insertions(+), 32 deletions(-) (limited to 'apps/files_encryption') diff --git a/apps/files_encryption/lib/util.php b/apps/files_encryption/lib/util.php index b300999ff24..14d0a0bc4b9 100644 --- a/apps/files_encryption/lib/util.php +++ b/apps/files_encryption/lib/util.php @@ -1109,7 +1109,7 @@ class Util { // Find out who, if anyone, is sharing the file $result = \OCP\Share::getUsersSharingFile($ownerPath, $owner); $userIds = \array_merge($userIds, $result['users']); - if ($result['public']) { + if ($result['public'] || $result['remote']) { $userIds[] = $this->publicShareKeyId; } diff --git a/apps/files_encryption/tests/share.php b/apps/files_encryption/tests/share.php index 8ecdbabed39..5632607319b 100755 --- a/apps/files_encryption/tests/share.php +++ b/apps/files_encryption/tests/share.php @@ -89,6 +89,8 @@ class Share extends TestCase { // login as first user self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); + + $this->createMocks(); } protected function tearDown() { @@ -99,6 +101,8 @@ class Share extends TestCase { \OC_App::disable('files_trashbin'); } + $this->restoreHttpHelper(); + parent::tearDown(); } @@ -115,18 +119,43 @@ class Share extends TestCase { parent::tearDownAfterClass(); } - /** - * @medium - */ - function testDeclineServer2ServerShare() { - + private function createMocks() { $config = $this->getMockBuilder('\OCP\IConfig') ->disableOriginalConstructor()->getMock(); $certificateManager = $this->getMock('\OCP\ICertificateManager'); $httpHelperMock = $this->getMockBuilder('\OC\HTTPHelper') ->setConstructorArgs(array($config, $certificateManager)) ->getMock(); - $httpHelperMock->expects($this->once())->method('post')->with($this->anything())->will($this->returnValue(true)); + $httpHelperMock->expects($this->any())->method('post')->with($this->anything())->will($this->returnValue(array('success' => true, 'result' => "{'ocs' : { 'meta' : { 'statuscode' : 100 }}}"))); + + $this->registerHttpHelper($httpHelperMock); + } + + /** + * Register an http helper mock for testing purposes. + * @param $httpHelper http helper mock + */ + private function registerHttpHelper($httpHelper) { + $this->oldHttpHelper = \OC::$server->query('HTTPHelper'); + \OC::$server->registerService('HTTPHelper', function ($c) use ($httpHelper) { + return $httpHelper; + }); + } + + /** + * Restore the original http helper + */ + private function restoreHttpHelper() { + $oldHttpHelper = $this->oldHttpHelper; + \OC::$server->registerService('HTTPHelper', function ($c) use ($oldHttpHelper) { + return $oldHttpHelper; + }); + } + + /** + * @medium + */ + function testDeclineServer2ServerShare() { self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); @@ -167,11 +196,9 @@ class Share extends TestCase { $share = $query->fetch(); - $this->registerHttpHelper($httpHelperMock); $_POST['token'] = $token; $s2s = new \OCA\Files_Sharing\API\Server2Server(); $s2s->declineShare(array('id' => $share['id'])); - $this->restoreHttpHelper(); $this->assertFalse($this->view->file_exists( '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' @@ -179,28 +206,6 @@ class Share extends TestCase { } - - /** - * Register an http helper mock for testing purposes. - * @param $httpHelper http helper mock - */ - private function registerHttpHelper($httpHelper) { - $this->oldHttpHelper = \OC::$server->query('HTTPHelper'); - \OC::$server->registerService('HTTPHelper', function ($c) use ($httpHelper) { - return $httpHelper; - }); - } - - /** - * Restore the original http helper - */ - private function restoreHttpHelper() { - $oldHttpHelper = $this->oldHttpHelper; - \OC::$server->registerService('HTTPHelper', function ($c) use ($oldHttpHelper) { - return $oldHttpHelper; - }); - } - /** * @medium * @param bool $withTeardown @@ -607,7 +612,63 @@ class Share extends TestCase { } - function testPublicShareFile() { + function testRemoteShareFile() { + // login as admin + //self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); + + // save file with content + $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->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; + + // get the file info from previous created file + $fileInfo = $this->view->getFileInfo( + '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); + + // check if we have a valid file info + $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo); + + // check if the unencrypted file size is stored + $this->assertGreaterThan(0, $fileInfo['unencrypted_size']); + + // re-enable the file proxy + \OC_FileProxy::$enabled = $proxyStatus; + + // share the file + \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_REMOTE, 'user1@server1', \OCP\Constants::PERMISSION_ALL); + + $publicShareKeyId = \OC::$server->getAppConfig()->getValue('files_encryption', 'publicShareKeyId'); + + // check if share key for public exists + $this->assertTrue($this->view->file_exists( + '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' + . $this->filename . '/' . $publicShareKeyId . '.shareKey')); + + // unshare the file + \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_REMOTE, 'user1@server1'); + + // check if share key not exists + $this->assertFalse($this->view->file_exists( + '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' + . $this->filename . '/' . $publicShareKeyId . '.shareKey')); + + // cleanup + $this->view->chroot('/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/'); + $this->view->unlink($this->filename); + $this->view->chroot('/'); + + // check if share key not exists + $this->assertFalse($this->view->file_exists( + '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/keys/' + . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); + } + + function testPublicShareFile() { // login as admin self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); -- cgit v1.2.3 From 9a7dd57bc862c2e2b2aafd1d17b99cea9fd748ae Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 29 Jan 2015 16:09:35 +0100 Subject: Fix 2 assertions --- apps/files_encryption/tests/share.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'apps/files_encryption') diff --git a/apps/files_encryption/tests/share.php b/apps/files_encryption/tests/share.php index 5632607319b..44fd2ac9c7c 100755 --- a/apps/files_encryption/tests/share.php +++ b/apps/files_encryption/tests/share.php @@ -218,7 +218,7 @@ class Share extends TestCase { $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort); // test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); + $this->assertInternalType('int', $cryptedFile); // disable encryption proxy to prevent recursive calls $proxyStatus = \OC_FileProxy::$enabled; @@ -229,7 +229,7 @@ class Share extends TestCase { '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); // check if we have a valid file info - $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo); + $this->assertInstanceOf('\\OC\\Files\\FileInfo', $fileInfo); // check if the unencrypted file size is stored $this->assertGreaterThan(0, $fileInfo['unencrypted_size']); @@ -1384,4 +1384,4 @@ class Share extends TestCase { \OC\Files\Filesystem::unlink($folder); } -} \ No newline at end of file +} -- cgit v1.2.3 From 4a5f626979136b71775e062cf1baf79945b886d6 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 29 Jan 2015 16:54:27 +0100 Subject: Fix more assertions --- apps/files_encryption/tests/share.php | 52 +++++++++++++++++------------------ 1 file changed, 26 insertions(+), 26 deletions(-) (limited to 'apps/files_encryption') diff --git a/apps/files_encryption/tests/share.php b/apps/files_encryption/tests/share.php index 44fd2ac9c7c..37793369956 100755 --- a/apps/files_encryption/tests/share.php +++ b/apps/files_encryption/tests/share.php @@ -163,7 +163,7 @@ class Share extends TestCase { $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort); // test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); + $this->assertInternalType('int', $cryptedFile); // get the file info from previous created file $fileInfo = $this->view->getFileInfo( @@ -229,7 +229,7 @@ class Share extends TestCase { '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); // check if we have a valid file info - $this->assertInstanceOf('\\OC\\Files\\FileInfo', $fileInfo); + $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); // check if the unencrypted file size is stored $this->assertGreaterThan(0, $fileInfo['unencrypted_size']); @@ -412,7 +412,7 @@ class Share extends TestCase { . $this->filename, $this->dataShort); // test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); + $this->assertInternalType('int', $cryptedFile); // disable encryption proxy to prevent recursive calls $proxyStatus = \OC_FileProxy::$enabled; @@ -423,7 +423,7 @@ class Share extends TestCase { '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files' . $this->folder1); // check if we have a valid file info - $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo); + $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); // re-enable the file proxy \OC_FileProxy::$enabled = $proxyStatus; @@ -501,7 +501,7 @@ class Share extends TestCase { . $this->subfolder); // check if we have a valid file info - $this->assertTrue($fileInfoSubFolder instanceof \OC\Files\FileInfo); + $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfoSubFolder); // re-enable the file proxy \OC_FileProxy::$enabled = $proxyStatus; @@ -535,7 +535,7 @@ class Share extends TestCase { . $this->subsubfolder . '/' . $this->filename); // check if we have fileInfos - $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo); + $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); // share the file with user3 \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER4, \OCP\Constants::PERMISSION_ALL); @@ -620,7 +620,7 @@ class Share extends TestCase { $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort); // test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); + $this->assertInternalType('int', $cryptedFile); // disable encryption proxy to prevent recursive calls $proxyStatus = \OC_FileProxy::$enabled; @@ -631,7 +631,7 @@ class Share extends TestCase { '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); // check if we have a valid file info - $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo); + $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); // check if the unencrypted file size is stored $this->assertGreaterThan(0, $fileInfo['unencrypted_size']); @@ -676,7 +676,7 @@ class Share extends TestCase { $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort); // test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); + $this->assertInternalType('int', $cryptedFile); // disable encryption proxy to prevent recursive calls $proxyStatus = \OC_FileProxy::$enabled; @@ -687,7 +687,7 @@ class Share extends TestCase { '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); // check if we have a valid file info - $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo); + $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); // check if the unencrypted file size is stored $this->assertGreaterThan(0, $fileInfo['unencrypted_size']); @@ -754,7 +754,7 @@ class Share extends TestCase { $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort); // test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); + $this->assertInternalType('int', $cryptedFile); // disable encryption proxy to prevent recursive calls $proxyStatus = \OC_FileProxy::$enabled; @@ -765,7 +765,7 @@ class Share extends TestCase { '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); // check if we have a valid file info - $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo); + $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); // check if the unencrypted file size is stored $this->assertGreaterThan(0, $fileInfo['unencrypted_size']); @@ -860,8 +860,8 @@ class Share extends TestCase { . $this->filename, $this->dataShort); // test that data was successfully written - $this->assertTrue(is_int($cryptedFile1)); - $this->assertTrue(is_int($cryptedFile2)); + $this->assertInternalType('int', $cryptedFile1); + $this->assertInternalType('int', $cryptedFile2); // check if share key for admin and recovery exists $this->assertTrue($this->view->file_exists( @@ -967,8 +967,8 @@ class Share extends TestCase { . $this->filename, $this->dataShort); // test that data was successfully written - $this->assertTrue(is_int($cryptedFile1)); - $this->assertTrue(is_int($cryptedFile2)); + $this->assertInternalType('int', $cryptedFile1); + $this->assertInternalType('int', $cryptedFile2); // check if share key for user and recovery exists $this->assertTrue($this->view->file_exists( @@ -1055,7 +1055,7 @@ class Share extends TestCase { $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort); // test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); + $this->assertInternalType('int', $cryptedFile); // disable encryption proxy to prevent recursive calls $proxyStatus = \OC_FileProxy::$enabled; @@ -1066,7 +1066,7 @@ class Share extends TestCase { '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); // check if we have a valid file info - $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo); + $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); // check if the unencrypted file size is stored $this->assertGreaterThan(0, $fileInfo['unencrypted_size']); @@ -1138,14 +1138,14 @@ class Share extends TestCase { $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort); // test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); + $this->assertInternalType('int', $cryptedFile); // get the file info from previous created file $fileInfo = $this->view->getFileInfo( '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); // check if we have a valid file info - $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo); + $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); // share the file \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER2, \OCP\Constants::PERMISSION_ALL); @@ -1204,14 +1204,14 @@ class Share extends TestCase { $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort); // test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); + $this->assertInternalType('int', $cryptedFile); // get the file info from previous created file $fileInfo = $this->view->getFileInfo( '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); // check if we have a valid file info - $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo); + $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); // share the file \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, self::TEST_ENCRYPTION_SHARE_GROUP1, \OCP\Constants::PERMISSION_ALL); @@ -1283,7 +1283,7 @@ class Share extends TestCase { $cryptedFile = \OC\Files\Filesystem::file_put_contents($folder . $filename, $this->dataShort); // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); + $this->assertInternalType('int', $cryptedFile); // Get file decrypted contents $decrypt = \OC\Files\Filesystem::file_get_contents($folder . $filename); @@ -1295,7 +1295,7 @@ class Share extends TestCase { // get the file info from previous created file $fileInfo = \OC\Files\Filesystem::getFileInfo('/newfolder'); - $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo); + $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); // share the folder \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER2, \OCP\Constants::PERMISSION_ALL); @@ -1341,7 +1341,7 @@ class Share extends TestCase { $cryptedFile = \OC\Files\Filesystem::file_put_contents($folder . $filename, $this->dataShort); // Test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); + $this->assertInternalType('int', $cryptedFile); // Get file decrypted contents $decrypt = \OC\Files\Filesystem::file_get_contents($folder . $filename); @@ -1353,7 +1353,7 @@ class Share extends TestCase { // get the file info from previous created file $fileInfo = \OC\Files\Filesystem::getFileInfo($folder); - $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo); + $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); // share the folder \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER2, \OCP\Constants::PERMISSION_ALL); -- cgit v1.2.3 From 333f4e7913ff8085f46f80db5bf1224fb1cd6e0c Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 29 Jan 2015 16:57:46 +0100 Subject: Fix intendation --- apps/files_encryption/tests/share.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'apps/files_encryption') diff --git a/apps/files_encryption/tests/share.php b/apps/files_encryption/tests/share.php index 37793369956..a59838ede1c 100755 --- a/apps/files_encryption/tests/share.php +++ b/apps/files_encryption/tests/share.php @@ -668,7 +668,7 @@ class Share extends TestCase { . $this->filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); } - function testPublicShareFile() { + function testPublicShareFile() { // login as admin self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); @@ -676,7 +676,7 @@ class Share extends TestCase { $cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort); // test that data was successfully written - $this->assertInternalType('int', $cryptedFile); + $this->assertInternalType('int', $cryptedFile); // disable encryption proxy to prevent recursive calls $proxyStatus = \OC_FileProxy::$enabled; @@ -687,7 +687,7 @@ class Share extends TestCase { '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename); // check if we have a valid file info - $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); + $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo); // check if the unencrypted file size is stored $this->assertGreaterThan(0, $fileInfo['unencrypted_size']); -- cgit v1.2.3