123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- <?php
- /**
- * @author Björn Schießle <schiessle@owncloud.com>
- *
- * @copyright Copyright (c) 2015, ownCloud, Inc.
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program 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, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
- */
- namespace Tests\Core\Command\Encryption;
- use OC\Core\Command\Encryption\ChangeKeyStorageRoot;
- use OC\Encryption\Util;
- use OC\Files\View;
- use OCP\IConfig;
- use OCP\IUserManager;
- use Symfony\Component\Console\Helper\QuestionHelper;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use Test\TestCase;
- class ChangeKeyStorageRootTest extends TestCase {
- /** @var ChangeKeyStorageRoot */
- protected $changeKeyStorageRoot;
- /** @var View | \PHPUnit_Framework_MockObject_MockObject */
- protected $view;
- /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */
- protected $userManager;
- /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */
- protected $config;
- /** @var Util | \PHPUnit_Framework_MockObject_MockObject */
- protected $util;
- /** @var QuestionHelper | \PHPUnit_Framework_MockObject_MockObject */
- protected $questionHelper;
- /** @var InputInterface | \PHPUnit_Framework_MockObject_MockObject */
- protected $inputInterface;
- /** @var OutputInterface | \PHPUnit_Framework_MockObject_MockObject */
- protected $outputInterface;
- /** @var \OCP\UserInterface | \PHPUnit_Framework_MockObject_MockObject */
- protected $userInterface;
- public function setUp() {
- parent::setUp();
- $this->view = $this->getMock('\OC\Files\View');
- $this->userManager = $this->getMock('\OCP\IUserManager');
- $this->config = $this->getMock('\OCP\IConfig');
- $this->util = $this->getMockBuilder('OC\Encryption\Util')->disableOriginalConstructor()->getMock();
- $this->questionHelper = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper');
- $this->inputInterface = $this->getMock('Symfony\Component\Console\Input\InputInterface');
- $this->outputInterface = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
- $this->userInterface = $this->getMock('\OCP\UserInterface');
- $outputFormaterInterface = $this->getMock('Symfony\Component\Console\Formatter\OutputFormatterInterface');
- $this->outputInterface->expects($this->any())->method('getFormatter')
- ->willReturn($outputFormaterInterface);
- $this->changeKeyStorageRoot = new ChangeKeyStorageRoot(
- $this->view,
- $this->userManager,
- $this->config,
- $this->util,
- $this->questionHelper
- );
- }
- /**
- * @dataProvider dataTestExecute
- */
- public function testExecute($newRoot, $answer, $successMoveKey) {
- $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot')
- ->setConstructorArgs(
- [
- $this->view,
- $this->userManager,
- $this->config,
- $this->util,
- $this->questionHelper
- ]
- )->setMethods(['moveAllKeys'])->getMock();
- $this->util->expects($this->once())->method('getKeyStorageRoot')
- ->willReturn('');
- $this->inputInterface->expects($this->once())->method('getArgument')
- ->with('newRoot')->willReturn($newRoot);
- if ($answer === true || $newRoot !== null) {
- $changeKeyStorageRoot->expects($this->once())->method('moveAllKeys')
- ->willReturn($successMoveKey);
- } else {
- $changeKeyStorageRoot->expects($this->never())->method('moveAllKeys');
- }
- if ($successMoveKey === true) {
- $this->util->expects($this->once())->method('setKeyStorageRoot');
- } else {
- $this->util->expects($this->never())->method('setKeyStorageRoot');
- }
- if ($newRoot === null) {
- $this->questionHelper->expects($this->once())->method('ask')->willReturn($answer);
- } else {
- $this->questionHelper->expects($this->never())->method('ask');
- }
- $this->invokePrivate(
- $changeKeyStorageRoot,
- 'execute',
- [$this->inputInterface, $this->outputInterface]
- );
- }
- public function dataTestExecute() {
- return [
- [null, true, true],
- [null, true, false],
- [null, false, null],
- ['/newRoot', null, true],
- ['/newRoot', null, false]
- ];
- }
- public function testMoveAllKeys() {
- /** @var \OC\Core\Command\Encryption\ChangeKeyStorageRoot $changeKeyStorageRoot */
- $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot')
- ->setConstructorArgs(
- [
- $this->view,
- $this->userManager,
- $this->config,
- $this->util,
- $this->questionHelper
- ]
- )->setMethods(['prepareNewRoot', 'moveSystemKeys', 'moveUserKeys'])->getMock();
- $changeKeyStorageRoot->expects($this->at(0))->method('prepareNewRoot')->with('newRoot');
- $changeKeyStorageRoot->expects($this->at(1))->method('moveSystemKeys')->with('oldRoot', 'newRoot');
- $changeKeyStorageRoot->expects($this->at(2))->method('moveUserKeys')->with('oldRoot', 'newRoot', $this->outputInterface);
- $this->invokePrivate($changeKeyStorageRoot, 'moveAllKeys', ['oldRoot', 'newRoot', $this->outputInterface]);
- }
- public function testPrepareNewRoot() {
- $this->view->expects($this->once())->method('is_dir')->with('newRoot')
- ->willReturn(true);
- $this->view->expects($this->once())->method('file_put_contents')
- ->with('newRoot/' . \OC\Encryption\Keys\Storage::KEY_STORAGE_MARKER,
- 'ownCloud will detect this folder as key storage root only if this file exists');
- $this->invokePrivate($this->changeKeyStorageRoot, 'prepareNewRoot', ['newRoot']);
- }
- /**
- * @dataProvider dataTestPrepareNewRootException
- * @expectedException \Exception
- *
- * @param bool $dirExists
- * @param bool $couldCreateFile
- */
- public function testPrepareNewRootException($dirExists, $couldCreateFile) {
- $this->view->expects($this->once())->method('is_dir')->with('newRoot')
- ->willReturn($dirExists);
- $this->view->expects($this->any())->method('file_put_contents')->willReturn($couldCreateFile);
- $this->invokePrivate($this->changeKeyStorageRoot, 'prepareNewRoot', ['newRoot']);
- }
- public function dataTestPrepareNewRootException() {
- return [
- [true, false],
- [false, true]
- ];
- }
- /**
- * @dataProvider dataTestMoveSystemKeys
- *
- * @param bool $dirExists
- * @param bool $targetExists
- * @param bool $executeRename
- */
- public function testMoveSystemKeys($dirExists, $targetExists, $executeRename) {
- $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot')
- ->setConstructorArgs(
- [
- $this->view,
- $this->userManager,
- $this->config,
- $this->util,
- $this->questionHelper
- ]
- )->setMethods(['targetExists'])->getMock();
- $this->view->expects($this->once())->method('is_dir')
- ->with('oldRoot/files_encryption')->willReturn($dirExists);
- $changeKeyStorageRoot->expects($this->any())->method('targetExists')
- ->with('newRoot/files_encryption')->willReturn($targetExists);
- if ($executeRename) {
- $this->view->expects($this->once())->method('rename')
- ->with('oldRoot/files_encryption', 'newRoot/files_encryption');
- } else {
- $this->view->expects($this->never())->method('rename');
- }
- $this->invokePrivate($changeKeyStorageRoot, 'moveSystemKeys', ['oldRoot', 'newRoot']);
- }
- public function dataTestMoveSystemKeys() {
- return [
- [true, false, true],
- [false, true, false],
- [true, true, false],
- [false, false, false]
- ];
- }
- public function testMoveUserKeys() {
- $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot')
- ->setConstructorArgs(
- [
- $this->view,
- $this->userManager,
- $this->config,
- $this->util,
- $this->questionHelper
- ]
- )->setMethods(['setupUserFS', 'moveUserEncryptionFolder'])->getMock();
- $this->userManager->expects($this->once())->method('getBackends')
- ->willReturn([$this->userInterface]);
- $this->userInterface->expects($this->once())->method('getUsers')
- ->willReturn(['user1', 'user2']);
- $changeKeyStorageRoot->expects($this->exactly(2))->method('setupUserFS');
- $changeKeyStorageRoot->expects($this->exactly(2))->method('moveUserEncryptionFolder');
- $this->invokePrivate($changeKeyStorageRoot, 'moveUserKeys', ['oldRoot', 'newRoot', $this->outputInterface]);
- }
- /**
- * @dataProvider dataTestMoveUserEncryptionFolder
- *
- * @param bool $userExists
- * @param bool $isDir
- * @param bool $targetExists
- * @param bool $shouldRename
- */
- public function testMoveUserEncryptionFolder($userExists, $isDir, $targetExists, $shouldRename) {
- $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot')
- ->setConstructorArgs(
- [
- $this->view,
- $this->userManager,
- $this->config,
- $this->util,
- $this->questionHelper
- ]
- )->setMethods(['targetExists', 'prepareParentFolder'])->getMock();
- $this->userManager->expects($this->once())->method('userExists')
- ->willReturn($userExists);
- $this->view->expects($this->any())->method('is_dir')
- ->willReturn($isDir);
- $changeKeyStorageRoot->expects($this->any())->method('targetExists')
- ->willReturn($targetExists);
- if ($shouldRename) {
- $changeKeyStorageRoot->expects($this->once())->method('prepareParentFolder')
- ->with('newRoot/user1');
- $this->view->expects($this->once())->method('rename')
- ->with('oldRoot/user1/files_encryption', 'newRoot/user1/files_encryption');
- } else {
- $changeKeyStorageRoot->expects($this->never())->method('prepareParentFolder');
- $this->view->expects($this->never())->method('rename');
- }
- $this->invokePrivate($changeKeyStorageRoot, 'moveUserEncryptionFolder', ['user1', 'oldRoot', 'newRoot']);
- }
- public function dataTestMoveUserEncryptionFolder() {
- return [
- [true, true, false, true],
- [true, false, true, false],
- [false, true, true, false],
- [false, false, true, false],
- [false, true, false, false],
- [false, true, true, false],
- [false, false, false, false]
- ];
- }
- /**
- * @dataProvider dataTestPrepareParentFolder
- */
- public function testPrepareParentFolder($path, $pathExists) {
- $this->view->expects($this->any())->method('file_exists')
- ->willReturnCallback(
- function($fileExistsPath) use ($path, $pathExists) {
- if ($path === $fileExistsPath) {
- return $pathExists;
- }
- return false;
- }
- );
- if ($pathExists === false) {
- $subDirs = explode('/', ltrim($path, '/'));
- $this->view->expects($this->exactly(count($subDirs)))->method('mkdir');
- } else {
- $this->view->expects($this->never())->method('mkdir');
- }
- $this->invokePrivate(
- $this->changeKeyStorageRoot,
- 'prepareParentFolder',
- [$path]
- );
- }
- public function dataTestPrepareParentFolder() {
- return [
- ['/user/folder/sub_folder/keystorage', true],
- ['/user/folder/sub_folder/keystorage', false]
- ];
- }
- public function testTargetExists() {
- $this->view->expects($this->once())->method('file_exists')->with('path')
- ->willReturn(false);
- $this->assertFalse(
- $this->invokePrivate($this->changeKeyStorageRoot, 'targetExists', ['path'])
- );
- }
- /**
- * @expectedException \Exception
- */
- public function testTargetExistsException() {
- $this->view->expects($this->once())->method('file_exists')->with('path')
- ->willReturn(true);
- $this->invokePrivate($this->changeKeyStorageRoot, 'targetExists', ['path']);
- }
- }
|