2015-04-16 16:42:13 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2020-03-31 11:49:10 +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 Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-04-16 16:42:13 +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-04-16 16:42:13 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Encryption\Tests\Controller;
|
|
|
|
|
|
|
|
use OCA\Encryption\Controller\SettingsController;
|
2017-10-26 14:46:16 +03:00
|
|
|
use OCA\Encryption\Crypto\Crypt;
|
|
|
|
use OCA\Encryption\KeyManager;
|
2015-04-16 16:42:13 +03:00
|
|
|
use OCA\Encryption\Session;
|
2017-10-26 14:46:16 +03:00
|
|
|
use OCA\Encryption\Util;
|
2015-04-20 18:44:34 +03:00
|
|
|
use OCP\AppFramework\Http;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\IL10N;
|
2016-09-02 11:29:05 +03:00
|
|
|
use OCP\IRequest;
|
2017-10-26 14:46:16 +03:00
|
|
|
use OCP\ISession;
|
2019-08-26 16:22:00 +03:00
|
|
|
use OCP\IUser;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\IUserManager;
|
2017-10-26 14:46:16 +03:00
|
|
|
use OCP\IUserSession;
|
2019-08-26 16:22:00 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2015-04-16 16:42:13 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class SettingsControllerTest extends TestCase {
|
|
|
|
|
|
|
|
/** @var SettingsController */
|
|
|
|
private $controller;
|
|
|
|
|
2016-05-12 10:42:19 +03:00
|
|
|
/** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */
|
2015-04-16 16:42:13 +03:00
|
|
|
private $requestMock;
|
|
|
|
|
2016-05-12 10:42:19 +03:00
|
|
|
/** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject */
|
2015-04-16 16:42:13 +03:00
|
|
|
private $l10nMock;
|
|
|
|
|
2016-05-12 10:42:19 +03:00
|
|
|
/** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject */
|
2015-04-16 16:42:13 +03:00
|
|
|
private $userManagerMock;
|
|
|
|
|
2016-05-12 10:42:19 +03:00
|
|
|
/** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject */
|
2015-04-16 16:42:13 +03:00
|
|
|
private $userSessionMock;
|
|
|
|
|
2016-05-12 10:42:19 +03:00
|
|
|
/** @var \OCA\Encryption\KeyManager|\PHPUnit_Framework_MockObject_MockObject */
|
2015-04-16 16:42:13 +03:00
|
|
|
private $keyManagerMock;
|
|
|
|
|
2016-05-12 10:42:19 +03:00
|
|
|
/** @var \OCA\Encryption\Crypto\Crypt|\PHPUnit_Framework_MockObject_MockObject */
|
2015-04-16 16:42:13 +03:00
|
|
|
private $cryptMock;
|
|
|
|
|
2016-05-12 10:42:19 +03:00
|
|
|
/** @var \OCA\Encryption\Session|\PHPUnit_Framework_MockObject_MockObject */
|
2015-04-16 16:42:13 +03:00
|
|
|
private $sessionMock;
|
2019-08-26 16:22:00 +03:00
|
|
|
/** @var MockObject|IUser */
|
|
|
|
private $user;
|
2015-04-16 16:42:13 +03:00
|
|
|
|
2016-05-12 10:42:19 +03:00
|
|
|
/** @var \OCP\ISession|\PHPUnit_Framework_MockObject_MockObject */
|
2015-08-25 16:05:55 +03:00
|
|
|
private $ocSessionMock;
|
|
|
|
|
2016-05-12 10:42:19 +03:00
|
|
|
/** @var \OCA\Encryption\Util|\PHPUnit_Framework_MockObject_MockObject */
|
2015-10-13 18:54:06 +03:00
|
|
|
private $utilMock;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2015-04-16 16:42:13 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2016-09-02 11:29:05 +03:00
|
|
|
$this->requestMock = $this->createMock(IRequest::class);
|
2015-04-16 16:42:13 +03:00
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->l10nMock = $this->getMockBuilder(IL10N::class)
|
2015-04-16 16:42:13 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
|
|
$this->l10nMock->expects($this->any())
|
|
|
|
->method('t')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($message) {
|
2015-04-20 18:44:34 +03:00
|
|
|
return $message;
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2015-04-16 16:42:13 +03:00
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->userManagerMock = $this->getMockBuilder(IUserManager::class)
|
2015-04-16 16:42:13 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->keyManagerMock = $this->getMockBuilder(KeyManager::class)
|
2015-04-16 16:42:13 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->cryptMock = $this->getMockBuilder(Crypt::class)
|
2015-04-16 16:42:13 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->ocSessionMock = $this->getMockBuilder(ISession::class)->disableOriginalConstructor()->getMock();
|
2015-08-25 16:05:55 +03:00
|
|
|
|
2019-08-26 16:22:00 +03:00
|
|
|
$this->user = $this->createMock(IUser::class);
|
|
|
|
$this->user->expects($this->any())
|
2015-04-16 16:42:13 +03:00
|
|
|
->method('getUID')
|
2015-08-25 16:05:55 +03:00
|
|
|
->willReturn('testUserUid');
|
2015-04-16 16:42:13 +03:00
|
|
|
|
2019-08-26 16:22:00 +03:00
|
|
|
$this->userSessionMock = $this->createMock(IUserSession::class);
|
2015-04-16 16:42:13 +03:00
|
|
|
$this->userSessionMock->expects($this->any())
|
2019-08-26 16:22:00 +03:00
|
|
|
->method('getUser')
|
|
|
|
->willReturn($this->user);
|
2015-04-16 16:42:13 +03:00
|
|
|
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->sessionMock = $this->getMockBuilder(Session::class)
|
2015-04-16 16:42:13 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->utilMock = $this->getMockBuilder(Util::class)
|
2015-10-13 18:54:06 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2015-04-16 16:42:13 +03:00
|
|
|
$this->controller = new SettingsController(
|
|
|
|
'encryption',
|
|
|
|
$this->requestMock,
|
|
|
|
$this->l10nMock,
|
|
|
|
$this->userManagerMock,
|
|
|
|
$this->userSessionMock,
|
|
|
|
$this->keyManagerMock,
|
|
|
|
$this->cryptMock,
|
2015-08-25 16:05:55 +03:00
|
|
|
$this->sessionMock,
|
2015-10-13 18:54:06 +03:00
|
|
|
$this->ocSessionMock,
|
|
|
|
$this->utilMock
|
2015-04-16 16:42:13 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test updatePrivateKeyPassword() if wrong new password was entered
|
|
|
|
*/
|
|
|
|
public function testUpdatePrivateKeyPasswordWrongNewPassword() {
|
|
|
|
$oldPassword = 'old';
|
|
|
|
$newPassword = 'new';
|
|
|
|
|
2019-08-26 16:22:00 +03:00
|
|
|
$this->user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('uid');
|
2015-08-25 16:05:55 +03:00
|
|
|
|
2015-04-16 16:42:13 +03:00
|
|
|
$this->userManagerMock
|
2015-08-25 16:05:55 +03:00
|
|
|
->expects($this->exactly(2))
|
2015-04-16 16:42:13 +03:00
|
|
|
->method('checkPassword')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$result = $this->controller->updatePrivateKeyPassword($oldPassword, $newPassword);
|
|
|
|
|
|
|
|
$data = $result->getData();
|
|
|
|
|
2015-04-20 18:44:34 +03:00
|
|
|
$this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus());
|
2015-04-16 16:42:13 +03:00
|
|
|
$this->assertSame('The current log-in password was not correct, please try again.',
|
2015-04-20 19:15:06 +03:00
|
|
|
$data['message']);
|
2015-04-16 16:42:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test updatePrivateKeyPassword() if wrong old password was entered
|
|
|
|
*/
|
|
|
|
public function testUpdatePrivateKeyPasswordWrongOldPassword() {
|
|
|
|
$oldPassword = 'old';
|
|
|
|
$newPassword = 'new';
|
|
|
|
|
|
|
|
$this->userManagerMock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('checkPassword')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->cryptMock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('decryptPrivateKey')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$result = $this->controller->updatePrivateKeyPassword($oldPassword, $newPassword);
|
|
|
|
|
|
|
|
$data = $result->getData();
|
|
|
|
|
2015-04-20 18:44:34 +03:00
|
|
|
$this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus());
|
2015-04-16 16:42:13 +03:00
|
|
|
$this->assertSame('The old password was not correct, please try again.',
|
2015-04-20 19:15:06 +03:00
|
|
|
$data['message']);
|
2015-04-16 16:42:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test updatePrivateKeyPassword() with the correct old and new password
|
|
|
|
*/
|
|
|
|
public function testUpdatePrivateKeyPassword() {
|
|
|
|
$oldPassword = 'old';
|
|
|
|
$newPassword = 'new';
|
|
|
|
|
2015-08-25 16:05:55 +03:00
|
|
|
$this->ocSessionMock->expects($this->once())
|
|
|
|
->method('get')->with('loginname')->willReturn('testUser');
|
2015-04-16 16:42:13 +03:00
|
|
|
|
|
|
|
$this->userManagerMock
|
2015-08-25 16:05:55 +03:00
|
|
|
->expects($this->at(0))
|
|
|
|
->method('checkPassword')
|
|
|
|
->with('testUserUid', 'new')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->userManagerMock
|
|
|
|
->expects($this->at(1))
|
2015-04-16 16:42:13 +03:00
|
|
|
->method('checkPassword')
|
2015-08-25 16:05:55 +03:00
|
|
|
->with('testUser', 'new')
|
2015-04-16 16:42:13 +03:00
|
|
|
->willReturn(true);
|
|
|
|
|
2015-08-25 16:05:55 +03:00
|
|
|
|
|
|
|
|
2015-04-16 16:42:13 +03:00
|
|
|
$this->cryptMock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('decryptPrivateKey')
|
|
|
|
->willReturn('decryptedKey');
|
|
|
|
|
|
|
|
$this->cryptMock
|
|
|
|
->expects($this->once())
|
2015-08-07 15:04:17 +03:00
|
|
|
->method('encryptPrivateKey')
|
2015-04-16 16:42:13 +03:00
|
|
|
->willReturn('encryptedKey');
|
|
|
|
|
|
|
|
$this->cryptMock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('generateHeader')
|
|
|
|
->willReturn('header.');
|
|
|
|
|
|
|
|
// methods which must be called after successful changing the key password
|
|
|
|
$this->keyManagerMock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setPrivateKey')
|
2015-08-25 16:05:55 +03:00
|
|
|
->with($this->equalTo('testUserUid'), $this->equalTo('header.encryptedKey'));
|
2015-04-16 16:42:13 +03:00
|
|
|
|
|
|
|
$this->sessionMock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setPrivateKey')
|
|
|
|
->with($this->equalTo('decryptedKey'));
|
|
|
|
|
|
|
|
$this->sessionMock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setStatus')
|
|
|
|
->with($this->equalTo(Session::INIT_SUCCESSFUL));
|
|
|
|
|
|
|
|
$result = $this->controller->updatePrivateKeyPassword($oldPassword, $newPassword);
|
|
|
|
|
|
|
|
$data = $result->getData();
|
|
|
|
|
2015-04-20 18:44:34 +03:00
|
|
|
$this->assertSame(Http::STATUS_OK, $result->getStatus());
|
2015-04-16 16:42:13 +03:00
|
|
|
$this->assertSame('Private key password successfully updated.',
|
2015-04-20 19:15:06 +03:00
|
|
|
$data['message']);
|
2015-04-16 16:42:13 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 17:51:06 +03:00
|
|
|
public function testSetEncryptHomeStorage() {
|
2015-10-13 18:54:06 +03:00
|
|
|
$value = true;
|
|
|
|
$this->utilMock->expects($this->once())->method('setEncryptHomeStorage')->with($value);
|
|
|
|
$this->controller->setEncryptHomeStorage($value);
|
|
|
|
}
|
2015-04-16 16:42:13 +03:00
|
|
|
}
|