2015-08-24 13:03:53 +03:00
|
|
|
<?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\DecryptAll;
|
2016-09-01 10:20:54 +03:00
|
|
|
use OCP\App\IAppManager;
|
|
|
|
use OCP\Encryption\IManager;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2015-08-24 13:03:53 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class DecryptAllTest extends TestCase {
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\IConfig */
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\Encryption\IManager */
|
|
|
|
protected $encryptionManager;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\App\IAppManager */
|
|
|
|
protected $appManager;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Input\InputInterface */
|
|
|
|
protected $consoleInput;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Output\OutputInterface */
|
|
|
|
protected $consoleOutput;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Helper\QuestionHelper */
|
|
|
|
protected $questionHelper;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | \OC\Encryption\DecryptAll */
|
|
|
|
protected $decryptAll;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2015-08-24 13:03:53 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2016-09-01 10:20:54 +03:00
|
|
|
$this->config = $this->getMockBuilder(IConfig::class)
|
2015-08-24 13:03:53 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-09-01 10:20:54 +03:00
|
|
|
$this->encryptionManager = $this->getMockBuilder(IManager::class)
|
2015-08-24 13:03:53 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-09-01 10:20:54 +03:00
|
|
|
$this->appManager = $this->getMockBuilder(IAppManager::class)
|
2015-08-24 13:03:53 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-09-01 10:20:54 +03:00
|
|
|
$this->questionHelper = $this->getMockBuilder(QuestionHelper::class)
|
2015-08-24 13:03:53 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-09-01 10:20:54 +03:00
|
|
|
$this->decryptAll = $this->getMockBuilder(\OC\Encryption\DecryptAll::class)
|
2015-08-24 13:03:53 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2016-09-01 10:20:54 +03:00
|
|
|
$this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
|
2018-07-29 18:02:42 +03:00
|
|
|
$this->consoleInput->expects($this->any())
|
|
|
|
->method('isInteractive')
|
|
|
|
->willReturn(true);
|
2016-09-01 10:20:54 +03:00
|
|
|
$this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
|
2015-08-24 13:03:53 +03:00
|
|
|
|
|
|
|
$this->config->expects($this->any())
|
|
|
|
->method('getSystemValue')
|
2017-02-23 08:02:31 +03:00
|
|
|
->with('maintenance', false)
|
2015-08-24 13:03:53 +03:00
|
|
|
->willReturn(false);
|
|
|
|
$this->appManager->expects($this->any())
|
|
|
|
->method('isEnabledForUser')
|
|
|
|
->with('files_trashbin')->willReturn(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-23 08:02:31 +03:00
|
|
|
public function testMaintenanceAndTrashbin() {
|
2015-09-18 09:53:02 +03:00
|
|
|
|
2015-08-24 13:03:53 +03:00
|
|
|
// on construct we enable single-user-mode and disable the trash bin
|
|
|
|
$this->config->expects($this->at(1))
|
|
|
|
->method('setSystemValue')
|
2017-02-23 08:02:31 +03:00
|
|
|
->with('maintenance', true);
|
2015-08-24 13:03:53 +03:00
|
|
|
$this->appManager->expects($this->once())
|
|
|
|
->method('disableApp')
|
|
|
|
->with('files_trashbin');
|
|
|
|
|
|
|
|
// on destruct wi disable single-user-mode again and enable the trash bin
|
|
|
|
$this->config->expects($this->at(2))
|
|
|
|
->method('setSystemValue')
|
2017-02-23 08:02:31 +03:00
|
|
|
->with('maintenance', false);
|
2015-08-24 13:03:53 +03:00
|
|
|
$this->appManager->expects($this->once())
|
|
|
|
->method('enableApp')
|
|
|
|
->with('files_trashbin');
|
|
|
|
|
|
|
|
$instance = new DecryptAll(
|
|
|
|
$this->encryptionManager,
|
|
|
|
$this->appManager,
|
|
|
|
$this->config,
|
|
|
|
$this->decryptAll,
|
|
|
|
$this->questionHelper
|
|
|
|
);
|
2017-02-23 08:02:31 +03:00
|
|
|
$this->invokePrivate($instance, 'forceMaintenanceAndTrashbin');
|
2015-08-24 13:03:53 +03:00
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->invokePrivate($instance, 'wasTrashbinEnabled')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse(
|
2017-02-23 08:02:31 +03:00
|
|
|
$this->invokePrivate($instance, 'wasMaintenanceModeEnabled')
|
2015-08-24 13:03:53 +03:00
|
|
|
);
|
2017-02-23 08:02:31 +03:00
|
|
|
$this->invokePrivate($instance, 'resetMaintenanceAndTrashbin');
|
2015-08-24 13:03:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestExecute
|
|
|
|
*/
|
|
|
|
public function testExecute($encryptionEnabled, $continue) {
|
|
|
|
|
|
|
|
$instance = new DecryptAll(
|
|
|
|
$this->encryptionManager,
|
|
|
|
$this->appManager,
|
|
|
|
$this->config,
|
|
|
|
$this->decryptAll,
|
|
|
|
$this->questionHelper
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->encryptionManager->expects($this->once())
|
|
|
|
->method('isEnabled')
|
|
|
|
->willReturn($encryptionEnabled);
|
|
|
|
|
|
|
|
$this->consoleInput->expects($this->any())
|
|
|
|
->method('getArgument')
|
|
|
|
->with('user')
|
|
|
|
->willReturn('user1');
|
|
|
|
|
|
|
|
if ($encryptionEnabled) {
|
2018-10-29 17:36:16 +03:00
|
|
|
$this->config->expects($this->at(1))
|
2015-08-24 13:03:53 +03:00
|
|
|
->method('setAppValue')
|
|
|
|
->with('core', 'encryption_enabled', 'no');
|
|
|
|
$this->questionHelper->expects($this->once())
|
|
|
|
->method('ask')
|
|
|
|
->willReturn($continue);
|
|
|
|
if ($continue) {
|
|
|
|
$this->decryptAll->expects($this->once())
|
|
|
|
->method('decryptAll')
|
|
|
|
->with($this->consoleInput, $this->consoleOutput, 'user1');
|
|
|
|
} else {
|
|
|
|
$this->decryptAll->expects($this->never())->method('decryptAll');
|
2018-10-29 17:36:16 +03:00
|
|
|
$this->config->expects($this->at(2))
|
2015-08-24 13:03:53 +03:00
|
|
|
->method('setAppValue')
|
|
|
|
->with('core', 'encryption_enabled', 'yes');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->config->expects($this->never())->method('setAppValue');
|
|
|
|
$this->decryptAll->expects($this->never())->method('decryptAll');
|
|
|
|
$this->questionHelper->expects($this->never())->method('ask');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->invokePrivate($instance, 'execute', [$this->consoleInput, $this->consoleOutput]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestExecute() {
|
|
|
|
return [
|
|
|
|
[true, true],
|
|
|
|
[true, false],
|
|
|
|
[false, true],
|
|
|
|
[false, false]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2015-08-24 13:03:53 +03:00
|
|
|
public function testExecuteFailure() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
|
2015-08-24 13:03:53 +03:00
|
|
|
$instance = new DecryptAll(
|
|
|
|
$this->encryptionManager,
|
|
|
|
$this->appManager,
|
|
|
|
$this->config,
|
|
|
|
$this->decryptAll,
|
|
|
|
$this->questionHelper
|
|
|
|
);
|
|
|
|
|
2018-10-29 17:36:16 +03:00
|
|
|
$this->config->expects($this->at(1))
|
2015-08-24 13:03:53 +03:00
|
|
|
->method('setAppValue')
|
|
|
|
->with('core', 'encryption_enabled', 'no');
|
|
|
|
|
|
|
|
// make sure that we enable encryption again after a exception was thrown
|
2018-10-29 17:36:16 +03:00
|
|
|
$this->config->expects($this->at(4))
|
2015-08-24 13:03:53 +03:00
|
|
|
->method('setAppValue')
|
|
|
|
->with('core', 'encryption_enabled', 'yes');
|
|
|
|
|
|
|
|
$this->encryptionManager->expects($this->once())
|
|
|
|
->method('isEnabled')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->consoleInput->expects($this->any())
|
|
|
|
->method('getArgument')
|
|
|
|
->with('user')
|
|
|
|
->willReturn('user1');
|
|
|
|
|
|
|
|
$this->questionHelper->expects($this->once())
|
|
|
|
->method('ask')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->decryptAll->expects($this->once())
|
|
|
|
->method('decryptAll')
|
|
|
|
->with($this->consoleInput, $this->consoleOutput, 'user1')
|
|
|
|
->willReturnCallback(function() { throw new \Exception(); });
|
|
|
|
|
|
|
|
$this->invokePrivate($instance, 'execute', [$this->consoleInput, $this->consoleOutput]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|