123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- <?php
- /**
- * ownCloud
- *
- * @copyright (C) 2015 ownCloud, Inc.
- *
- * @author Bjoern Schiessle <schiessle@owncloud.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
- *
- * You should have received a copy of the GNU Affero General Public
- * License along with this library. If not, see <http://www.gnu.org/licenses/>.
- */
- namespace Test\Encryption\Keys;
- use OC\Encryption\Keys\Storage;
- use Test\TestCase;
- class StorageTest extends TestCase {
- /** @var Storage */
- protected $storage;
- /** @var \PHPUnit_Framework_MockObject_MockObject */
- protected $util;
- /** @var \PHPUnit_Framework_MockObject_MockObject */
- protected $view;
- public function setUp() {
- parent::setUp();
- $this->util = $this->getMockBuilder('OC\Encryption\Util')
- ->disableOriginalConstructor()
- ->getMock();
- $this->view = $this->getMockBuilder('OC\Files\View')
- ->disableOriginalConstructor()
- ->getMock();
- $this->storage = new Storage('encModule', $this->view, $this->util);
- }
- public function testSetFileKey() {
- $this->util->expects($this->any())
- ->method('getUidAndFilename')
- ->willReturn(array('user1', '/files/foo.txt'));
- $this->util->expects($this->any())
- ->method('stripPartialFileExtension')
- ->willReturnArgument(0);
- $this->util->expects($this->any())
- ->method('isSystemWideMountPoint')
- ->willReturn(false);
- $this->view->expects($this->once())
- ->method('file_put_contents')
- ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'),
- $this->equalTo('key'))
- ->willReturn(strlen('key'));
- $this->assertTrue(
- $this->storage->setFileKey('user1/files/foo.txt', 'fileKey', 'key')
- );
- }
- public function testGetFileKey() {
- $this->util->expects($this->any())
- ->method('getUidAndFilename')
- ->willReturn(array('user1', '/files/foo.txt'));
- $this->util->expects($this->any())
- ->method('stripPartialFileExtension')
- ->willReturnArgument(0);
- $this->util->expects($this->any())
- ->method('isSystemWideMountPoint')
- ->willReturn(false);
- $this->view->expects($this->once())
- ->method('file_get_contents')
- ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'))
- ->willReturn('key');
- $this->view->expects($this->once())
- ->method('file_exists')
- ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'))
- ->willReturn(true);
- $this->assertSame('key',
- $this->storage->getFileKey('user1/files/foo.txt', 'fileKey')
- );
- }
- public function testSetFileKeySystemWide() {
- $this->util->expects($this->any())
- ->method('getUidAndFilename')
- ->willReturn(array('user1', '/files/foo.txt'));
- $this->util->expects($this->any())
- ->method('isSystemWideMountPoint')
- ->willReturn(true);
- $this->util->expects($this->any())
- ->method('stripPartialFileExtension')
- ->willReturnArgument(0);
- $this->view->expects($this->once())
- ->method('file_put_contents')
- ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'),
- $this->equalTo('key'))
- ->willReturn(strlen('key'));
- $this->assertTrue(
- $this->storage->setFileKey('user1/files/foo.txt', 'fileKey', 'key')
- );
- }
- public function testGetFileKeySystemWide() {
- $this->util->expects($this->any())
- ->method('getUidAndFilename')
- ->willReturn(array('user1', '/files/foo.txt'));
- $this->util->expects($this->any())
- ->method('stripPartialFileExtension')
- ->willReturnArgument(0);
- $this->util->expects($this->any())
- ->method('isSystemWideMountPoint')
- ->willReturn(true);
- $this->view->expects($this->once())
- ->method('file_get_contents')
- ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
- ->willReturn('key');
- $this->view->expects($this->once())
- ->method('file_exists')
- ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
- ->willReturn(true);
- $this->assertSame('key',
- $this->storage->getFileKey('user1/files/foo.txt', 'fileKey')
- );
- }
- public function testSetSystemUserKey() {
- $this->view->expects($this->once())
- ->method('file_put_contents')
- ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'),
- $this->equalTo('key'))
- ->willReturn(strlen('key'));
- $this->assertTrue(
- $this->storage->setSystemUserKey('shareKey_56884', 'key')
- );
- }
- public function testSetUserKey() {
- $this->view->expects($this->once())
- ->method('file_put_contents')
- ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'),
- $this->equalTo('key'))
- ->willReturn(strlen('key'));
- $this->assertTrue(
- $this->storage->setUserKey('user1', 'publicKey', 'key')
- );
- }
- public function testGetSystemUserKey() {
- $this->view->expects($this->once())
- ->method('file_get_contents')
- ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
- ->willReturn('key');
- $this->view->expects($this->once())
- ->method('file_exists')
- ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
- ->willReturn(true);
- $this->assertSame('key',
- $this->storage->getSystemUserKey('shareKey_56884')
- );
- }
- public function testGetUserKey() {
- $this->view->expects($this->once())
- ->method('file_get_contents')
- ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
- ->willReturn('key');
- $this->view->expects($this->once())
- ->method('file_exists')
- ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
- ->willReturn(true);
- $this->assertSame('key',
- $this->storage->getUserKey('user1', 'publicKey')
- );
- }
- public function testDeleteUserKey() {
- $this->view->expects($this->once())
- ->method('unlink')
- ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
- ->willReturn(true);
- $this->assertTrue(
- $this->storage->deleteUserKey('user1', 'publicKey')
- );
- }
- public function testDeleteSystemUserKey() {
- $this->view->expects($this->once())
- ->method('unlink')
- ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
- ->willReturn(true);
- $this->assertTrue(
- $this->storage->deleteSystemUserKey('shareKey_56884')
- );
- }
- public function testDeleteFileKeySystemWide() {
- $this->util->expects($this->any())
- ->method('getUidAndFilename')
- ->willReturn(array('user1', '/files/foo.txt'));
- $this->util->expects($this->any())
- ->method('stripPartialFileExtension')
- ->willReturnArgument(0);
- $this->util->expects($this->any())
- ->method('isSystemWideMountPoint')
- ->willReturn(true);
- $this->view->expects($this->once())
- ->method('unlink')
- ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
- ->willReturn(true);
- $this->assertTrue(
- $this->storage->deleteFileKey('user1/files/foo.txt', 'fileKey')
- );
- }
- public function testDeleteFileKey() {
- $this->util->expects($this->any())
- ->method('getUidAndFilename')
- ->willReturn(array('user1', '/files/foo.txt'));
- $this->util->expects($this->any())
- ->method('stripPartialFileExtension')
- ->willReturnArgument(0);
- $this->util->expects($this->any())
- ->method('isSystemWideMountPoint')
- ->willReturn(false);
- $this->view->expects($this->once())
- ->method('unlink')
- ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'))
- ->willReturn(true);
- $this->assertTrue(
- $this->storage->deleteFileKey('user1/files/foo.txt', 'fileKey')
- );
- }
- /**
- * @dataProvider dataProviderCopyRename
- */
- public function testRenameKeys($source, $target, $systemWideMount, $expectedSource, $expectedTarget) {
- $this->view->expects($this->any())
- ->method('file_exists')
- ->willReturn(true);
- $this->view->expects($this->any())
- ->method('is_dir')
- ->willReturn(true);
- $this->view->expects($this->once())
- ->method('rename')
- ->with(
- $this->equalTo($expectedSource),
- $this->equalTo($expectedTarget))
- ->willReturn(true);
- $this->util->expects($this->any())
- ->method('getUidAndFilename')
- ->will($this->returnCallback(array($this, 'getUidAndFilenameCallback')));
- $this->util->expects($this->any())
- ->method('isSystemWideMountPoint')
- ->willReturn($systemWideMount);
- $this->storage->renameKeys($source, $target);
- }
- /**
- * @dataProvider dataProviderCopyRename
- */
- public function testCopyKeys($source, $target, $systemWideMount, $expectedSource, $expectedTarget) {
- $this->view->expects($this->any())
- ->method('file_exists')
- ->willReturn(true);
- $this->view->expects($this->any())
- ->method('is_dir')
- ->willReturn(true);
- $this->view->expects($this->once())
- ->method('copy')
- ->with(
- $this->equalTo($expectedSource),
- $this->equalTo($expectedTarget))
- ->willReturn(true);
- $this->util->expects($this->any())
- ->method('getUidAndFilename')
- ->will($this->returnCallback(array($this, 'getUidAndFilenameCallback')));
- $this->util->expects($this->any())
- ->method('isSystemWideMountPoint')
- ->willReturn($systemWideMount);
- $this->storage->copyKeys($source, $target);
- }
- public function getUidAndFilenameCallback() {
- $args = func_get_args();
- $path = $args[0];
- $parts = explode('/', $path);
- return array($parts[1], '/' . implode('/', array_slice($parts, 2)));
- }
- public function dataProviderCopyRename() {
- return array(
- array('/user1/files/foo.txt', '/user1/files/bar.txt', false,
- '/user1/files_encryption/keys/files/foo.txt/', '/user1/files_encryption/keys/files/bar.txt/'),
- array('/user1/files/foo/foo.txt', '/user1/files/bar.txt', false,
- '/user1/files_encryption/keys/files/foo/foo.txt/', '/user1/files_encryption/keys/files/bar.txt/'),
- array('/user1/files/foo.txt', '/user1/files/foo/bar.txt', false,
- '/user1/files_encryption/keys/files/foo.txt/', '/user1/files_encryption/keys/files/foo/bar.txt/'),
- array('/user1/files/foo.txt', '/user1/files/foo/bar.txt', true,
- '/files_encryption/keys/files/foo.txt/', '/files_encryption/keys/files/foo/bar.txt/'),
- );
- }
- public function testKeySetPreparation() {
- $this->view->expects($this->any())
- ->method('file_exists')
- ->willReturn(false);
- $this->view->expects($this->any())
- ->method('is_dir')
- ->willReturn(false);
- $this->view->expects($this->any())
- ->method('mkdir')
- ->will($this->returnCallback(array($this, 'mkdirCallback')));
- $this->mkdirStack = array(
- '/user1/files_encryption/keys/foo',
- '/user1/files_encryption/keys',
- '/user1/files_encryption',
- '/user1');
- \Test_Helper::invokePrivate($this->storage, 'keySetPreparation', array('/user1/files_encryption/keys/foo'));
- }
- public function mkdirCallback() {
- $args = func_get_args();
- $expected = array_pop($this->mkdirStack);
- $this->assertSame($expected, $args[0]);
- }
- }
|