2015-08-24 16:56:04 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Kenneth Newwood <kenneth@newwood.name>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-08-24 16:56:04 +03:00
|
|
|
*
|
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-08-24 16:56:04 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-05-12 10:42:19 +03:00
|
|
|
namespace OCA\Encryption\Tests\Crypto;
|
2015-08-24 16:56:04 +03:00
|
|
|
|
2017-10-26 14:46:16 +03:00
|
|
|
use OC\Files\View;
|
2015-08-24 16:56:04 +03:00
|
|
|
use OCA\Encryption\Crypto\EncryptAll;
|
2017-10-26 14:46:16 +03:00
|
|
|
use OCA\Encryption\KeyManager;
|
|
|
|
use OCA\Encryption\Users\Setup;
|
|
|
|
use OCA\Encryption\Util;
|
2018-10-24 17:15:17 +03:00
|
|
|
use OCP\Files\FileInfo;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\Mail\IMailer;
|
2017-10-26 14:46:16 +03:00
|
|
|
use OCP\Security\ISecureRandom;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\UserInterface;
|
2016-09-02 11:29:05 +03:00
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
|
2017-10-26 14:46:16 +03:00
|
|
|
use Symfony\Component\Console\Helper\ProgressBar;
|
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2015-08-24 16:56:04 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class EncryptAllTest extends TestCase {
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | \OCA\Encryption\KeyManager */
|
|
|
|
protected $keyManager;
|
|
|
|
|
2016-03-02 13:16:53 +03:00
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | \OCA\Encryption\Util */
|
|
|
|
protected $util;
|
|
|
|
|
2015-08-24 16:56:04 +03:00
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\IUserManager */
|
|
|
|
protected $userManager;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | \OCA\Encryption\Users\Setup */
|
|
|
|
protected $setupUser;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | \OC\Files\View */
|
|
|
|
protected $view;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\IConfig */
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\Mail\IMailer */
|
|
|
|
protected $mailer;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\IL10N */
|
|
|
|
protected $l;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Helper\QuestionHelper */
|
|
|
|
protected $questionHelper;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Input\InputInterface */
|
|
|
|
protected $inputInterface;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Output\OutputInterface */
|
|
|
|
protected $outputInterface;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\UserInterface */
|
|
|
|
protected $userInterface;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\Security\ISecureRandom */
|
|
|
|
protected $secureRandom;
|
|
|
|
|
|
|
|
/** @var EncryptAll */
|
|
|
|
protected $encryptAll;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2015-08-24 16:56:04 +03:00
|
|
|
parent::setUp();
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->setupUser = $this->getMockBuilder(Setup::class)
|
2015-08-24 16:56:04 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->keyManager = $this->getMockBuilder(KeyManager::class)
|
2015-08-24 16:56:04 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->util = $this->getMockBuilder(Util::class)
|
2016-03-02 13:16:53 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->userManager = $this->getMockBuilder(IUserManager::class)
|
2015-08-24 16:56:04 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->view = $this->getMockBuilder(View::class)
|
2015-08-24 16:56:04 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->config = $this->getMockBuilder(IConfig::class)
|
2015-08-24 16:56:04 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->mailer = $this->getMockBuilder(IMailer::class)
|
2015-08-24 16:56:04 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->l = $this->getMockBuilder(IL10N::class)
|
2015-08-24 16:56:04 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->questionHelper = $this->getMockBuilder(QuestionHelper::class)
|
2015-08-24 16:56:04 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->inputInterface = $this->getMockBuilder(InputInterface::class)
|
2015-08-24 16:56:04 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->outputInterface = $this->getMockBuilder(OutputInterface::class)
|
2015-08-24 16:56:04 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->userInterface = $this->getMockBuilder(UserInterface::class)
|
2015-08-24 16:56:04 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
|
|
|
|
|
|
$this->outputInterface->expects($this->any())->method('getFormatter')
|
2016-09-02 11:29:05 +03:00
|
|
|
->willReturn($this->createMock(OutputFormatterInterface::class));
|
2015-08-24 16:56:04 +03:00
|
|
|
|
|
|
|
$this->userManager->expects($this->any())->method('getBackends')->willReturn([$this->userInterface]);
|
|
|
|
$this->userInterface->expects($this->any())->method('getUsers')->willReturn(['user1', 'user2']);
|
|
|
|
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->secureRandom = $this->getMockBuilder(ISecureRandom::class)->disableOriginalConstructor()->getMock();
|
2015-08-24 16:56:04 +03:00
|
|
|
$this->secureRandom->expects($this->any())->method('generate')->willReturn('12345678');
|
|
|
|
|
|
|
|
|
|
|
|
$this->encryptAll = new EncryptAll(
|
|
|
|
$this->setupUser,
|
|
|
|
$this->userManager,
|
|
|
|
$this->view,
|
|
|
|
$this->keyManager,
|
2016-03-02 13:16:53 +03:00
|
|
|
$this->util,
|
2015-08-24 16:56:04 +03:00
|
|
|
$this->config,
|
|
|
|
$this->mailer,
|
|
|
|
$this->l,
|
|
|
|
$this->questionHelper,
|
|
|
|
$this->secureRandom
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEncryptAll() {
|
|
|
|
/** @var EncryptAll | \PHPUnit_Framework_MockObject_MockObject $encryptAll */
|
2017-10-26 14:46:16 +03:00
|
|
|
$encryptAll = $this->getMockBuilder(EncryptAll::class)
|
2015-08-24 16:56:04 +03:00
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
|
|
|
$this->setupUser,
|
|
|
|
$this->userManager,
|
|
|
|
$this->view,
|
|
|
|
$this->keyManager,
|
2016-03-02 13:16:53 +03:00
|
|
|
$this->util,
|
2015-08-24 16:56:04 +03:00
|
|
|
$this->config,
|
|
|
|
$this->mailer,
|
|
|
|
$this->l,
|
|
|
|
$this->questionHelper,
|
|
|
|
$this->secureRandom
|
|
|
|
]
|
|
|
|
)
|
|
|
|
->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
|
|
|
|
->getMock();
|
|
|
|
|
2016-03-02 13:16:53 +03:00
|
|
|
$this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
|
2015-08-24 16:56:04 +03:00
|
|
|
$encryptAll->expects($this->at(0))->method('createKeyPairs')->with();
|
2017-09-11 13:52:25 +03:00
|
|
|
$encryptAll->expects($this->at(1))->method('outputPasswords')->with();
|
|
|
|
$encryptAll->expects($this->at(2))->method('encryptAllUsersFiles')->with();
|
2015-08-24 16:56:04 +03:00
|
|
|
|
|
|
|
$encryptAll->encryptAll($this->inputInterface, $this->outputInterface);
|
|
|
|
}
|
|
|
|
|
2016-03-02 13:27:29 +03:00
|
|
|
public function testEncryptAllWithMasterKey() {
|
|
|
|
/** @var EncryptAll | \PHPUnit_Framework_MockObject_MockObject $encryptAll */
|
2017-10-26 14:46:16 +03:00
|
|
|
$encryptAll = $this->getMockBuilder(EncryptAll::class)
|
2016-03-02 13:27:29 +03:00
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
|
|
|
$this->setupUser,
|
|
|
|
$this->userManager,
|
|
|
|
$this->view,
|
|
|
|
$this->keyManager,
|
|
|
|
$this->util,
|
|
|
|
$this->config,
|
|
|
|
$this->mailer,
|
|
|
|
$this->l,
|
|
|
|
$this->questionHelper,
|
|
|
|
$this->secureRandom
|
|
|
|
]
|
|
|
|
)
|
|
|
|
->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(true);
|
|
|
|
$encryptAll->expects($this->never())->method('createKeyPairs');
|
|
|
|
$this->keyManager->expects($this->once())->method('validateMasterKey');
|
|
|
|
$encryptAll->expects($this->at(0))->method('encryptAllUsersFiles')->with();
|
|
|
|
$encryptAll->expects($this->never())->method('outputPasswords');
|
|
|
|
|
|
|
|
$encryptAll->encryptAll($this->inputInterface, $this->outputInterface);
|
|
|
|
}
|
|
|
|
|
2015-08-24 16:56:04 +03:00
|
|
|
public function testCreateKeyPairs() {
|
|
|
|
/** @var EncryptAll | \PHPUnit_Framework_MockObject_MockObject $encryptAll */
|
2017-10-26 14:46:16 +03:00
|
|
|
$encryptAll = $this->getMockBuilder(EncryptAll::class)
|
2015-08-24 16:56:04 +03:00
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
|
|
|
$this->setupUser,
|
|
|
|
$this->userManager,
|
|
|
|
$this->view,
|
|
|
|
$this->keyManager,
|
2016-03-02 13:16:53 +03:00
|
|
|
$this->util,
|
2015-08-24 16:56:04 +03:00
|
|
|
$this->config,
|
|
|
|
$this->mailer,
|
|
|
|
$this->l,
|
|
|
|
$this->questionHelper,
|
|
|
|
$this->secureRandom
|
|
|
|
]
|
|
|
|
)
|
|
|
|
->setMethods(['setupUserFS', 'generateOneTimePassword'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
|
|
// set protected property $output
|
|
|
|
$this->invokePrivate($encryptAll, 'output', [$this->outputInterface]);
|
|
|
|
|
|
|
|
$this->keyManager->expects($this->exactly(2))->method('userHasKeys')
|
|
|
|
->willReturnCallback(
|
|
|
|
function ($user) {
|
|
|
|
if ($user === 'user1') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$encryptAll->expects($this->once())->method('setupUserFS')->with('user1');
|
|
|
|
$encryptAll->expects($this->once())->method('generateOneTimePassword')->with('user1')->willReturn('password');
|
|
|
|
$this->setupUser->expects($this->once())->method('setupUser')->with('user1', 'password');
|
|
|
|
|
|
|
|
$this->invokePrivate($encryptAll, 'createKeyPairs');
|
|
|
|
|
|
|
|
$userPasswords = $this->invokePrivate($encryptAll, 'userPasswords');
|
|
|
|
|
|
|
|
// we only expect the skipped user, because generateOneTimePassword which
|
|
|
|
// would set the user with the new password was mocked.
|
|
|
|
// This method will be tested separately
|
|
|
|
$this->assertSame(1, count($userPasswords));
|
|
|
|
$this->assertSame('', $userPasswords['user2']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEncryptAllUsersFiles() {
|
|
|
|
/** @var EncryptAll | \PHPUnit_Framework_MockObject_MockObject $encryptAll */
|
2017-10-26 14:46:16 +03:00
|
|
|
$encryptAll = $this->getMockBuilder(EncryptAll::class)
|
2015-08-24 16:56:04 +03:00
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
|
|
|
$this->setupUser,
|
|
|
|
$this->userManager,
|
|
|
|
$this->view,
|
|
|
|
$this->keyManager,
|
2016-03-02 13:16:53 +03:00
|
|
|
$this->util,
|
2015-08-24 16:56:04 +03:00
|
|
|
$this->config,
|
|
|
|
$this->mailer,
|
|
|
|
$this->l,
|
|
|
|
$this->questionHelper,
|
|
|
|
$this->secureRandom
|
|
|
|
]
|
|
|
|
)
|
|
|
|
->setMethods(['encryptUsersFiles'])
|
|
|
|
->getMock();
|
|
|
|
|
2016-03-02 13:16:53 +03:00
|
|
|
$this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
|
|
|
|
|
2015-08-24 16:56:04 +03:00
|
|
|
// set protected property $output
|
|
|
|
$this->invokePrivate($encryptAll, 'output', [$this->outputInterface]);
|
|
|
|
$this->invokePrivate($encryptAll, 'userPasswords', [['user1' => 'pwd1', 'user2' => 'pwd2']]);
|
|
|
|
|
|
|
|
$encryptAll->expects($this->at(0))->method('encryptUsersFiles')->with('user1');
|
|
|
|
$encryptAll->expects($this->at(1))->method('encryptUsersFiles')->with('user2');
|
|
|
|
|
|
|
|
$this->invokePrivate($encryptAll, 'encryptAllUsersFiles');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEncryptUsersFiles() {
|
|
|
|
/** @var EncryptAll | \PHPUnit_Framework_MockObject_MockObject $encryptAll */
|
2017-10-26 14:46:16 +03:00
|
|
|
$encryptAll = $this->getMockBuilder(EncryptAll::class)
|
2015-08-24 16:56:04 +03:00
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
|
|
|
$this->setupUser,
|
|
|
|
$this->userManager,
|
|
|
|
$this->view,
|
|
|
|
$this->keyManager,
|
2016-03-02 13:16:53 +03:00
|
|
|
$this->util,
|
2015-08-24 16:56:04 +03:00
|
|
|
$this->config,
|
|
|
|
$this->mailer,
|
|
|
|
$this->l,
|
|
|
|
$this->questionHelper,
|
|
|
|
$this->secureRandom
|
|
|
|
]
|
|
|
|
)
|
2016-03-02 13:16:53 +03:00
|
|
|
->setMethods(['encryptFile', 'setupUserFS'])
|
2015-08-24 16:56:04 +03:00
|
|
|
->getMock();
|
|
|
|
|
2016-03-02 13:16:53 +03:00
|
|
|
$this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
|
2015-08-24 16:56:04 +03:00
|
|
|
|
|
|
|
$this->view->expects($this->at(0))->method('getDirectoryContent')
|
|
|
|
->with('/user1/files')->willReturn(
|
|
|
|
[
|
|
|
|
['name' => 'foo', 'type'=>'dir'],
|
|
|
|
['name' => 'bar', 'type'=>'file'],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->view->expects($this->at(3))->method('getDirectoryContent')
|
|
|
|
->with('/user1/files/foo')->willReturn(
|
|
|
|
[
|
|
|
|
['name' => 'subfile', 'type'=>'file']
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->view->expects($this->any())->method('is_dir')
|
|
|
|
->willReturnCallback(
|
2020-04-09 14:53:40 +03:00
|
|
|
function ($path) {
|
2015-08-24 16:56:04 +03:00
|
|
|
if ($path === '/user1/files/foo') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-03-02 13:16:53 +03:00
|
|
|
$encryptAll->expects($this->at(1))->method('encryptFile')->with('/user1/files/bar');
|
|
|
|
$encryptAll->expects($this->at(2))->method('encryptFile')->with('/user1/files/foo/subfile');
|
2015-08-24 16:56:04 +03:00
|
|
|
|
2018-01-25 13:32:27 +03:00
|
|
|
$this->outputInterface->expects($this->any())
|
|
|
|
->method('getFormatter')
|
|
|
|
->willReturn($this->createMock(OutputFormatterInterface::class));
|
|
|
|
$progressBar = new ProgressBar($this->outputInterface);
|
2015-08-24 16:56:04 +03:00
|
|
|
|
|
|
|
$this->invokePrivate($encryptAll, 'encryptUsersFiles', ['user1', $progressBar, '']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGenerateOneTimePassword() {
|
|
|
|
$password = $this->invokePrivate($this->encryptAll, 'generateOneTimePassword', ['user1']);
|
|
|
|
$this->assertTrue(is_string($password));
|
|
|
|
$this->assertSame(8, strlen($password));
|
|
|
|
|
|
|
|
$userPasswords = $this->invokePrivate($this->encryptAll, 'userPasswords');
|
|
|
|
$this->assertSame(1, count($userPasswords));
|
|
|
|
$this->assertSame($password, $userPasswords['user1']);
|
|
|
|
}
|
|
|
|
|
2018-10-24 17:15:17 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataTestEncryptFile
|
|
|
|
* @param $isEncrypted
|
|
|
|
*/
|
|
|
|
public function testEncryptFile($isEncrypted) {
|
|
|
|
$fileInfo = $this->createMock(FileInfo::class);
|
|
|
|
$fileInfo->expects($this->any())->method('isEncrypted')
|
|
|
|
->willReturn($isEncrypted);
|
|
|
|
$this->view->expects($this->any())->method('getFileInfo')
|
|
|
|
->willReturn($fileInfo);
|
|
|
|
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($isEncrypted) {
|
2018-10-24 17:15:17 +03:00
|
|
|
$this->view->expects($this->never())->method('copy');
|
|
|
|
$this->view->expects($this->never())->method('rename');
|
|
|
|
} else {
|
|
|
|
$this->view->expects($this->once())->method('copy');
|
|
|
|
$this->view->expects($this->once())->method('rename');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->invokePrivate($this->encryptAll, 'encryptFile', ['foo.txt'])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestEncryptFile() {
|
|
|
|
return [
|
|
|
|
[true],
|
|
|
|
[false],
|
|
|
|
];
|
|
|
|
}
|
2015-08-24 16:56:04 +03:00
|
|
|
}
|