2015-04-16 13:42:13 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
2015-06-25 09:43:55 +00:00
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
2015-04-16 13:42:13 +00:00
|
|
|
*
|
|
|
|
* @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 OCA\Encryption\Tests\Controller;
|
|
|
|
|
|
|
|
use OCA\Encryption\Controller\SettingsController;
|
|
|
|
use OCA\Encryption\Session;
|
2015-04-20 15:44:34 +00:00
|
|
|
use OCP\AppFramework\Http;
|
2015-04-16 13:42:13 +00:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class SettingsControllerTest extends TestCase {
|
|
|
|
|
|
|
|
/** @var SettingsController */
|
|
|
|
private $controller;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $requestMock;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $l10nMock;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $userManagerMock;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $userSessionMock;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $keyManagerMock;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $cryptMock;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $sessionMock;
|
|
|
|
|
2015-08-25 13:05:55 +00:00
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $ocSessionMock;
|
|
|
|
|
2015-10-13 15:54:06 +00:00
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $utilMock;
|
|
|
|
|
2015-04-20 15:44:34 +00:00
|
|
|
protected function setUp() {
|
2015-04-16 13:42:13 +00:00
|
|
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->requestMock = $this->getMock('OCP\IRequest');
|
|
|
|
|
|
|
|
$this->l10nMock = $this->getMockBuilder('OCP\IL10N')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
|
|
$this->l10nMock->expects($this->any())
|
|
|
|
->method('t')
|
|
|
|
->will($this->returnCallback(function($message) {
|
2015-04-20 15:44:34 +00:00
|
|
|
return $message;
|
|
|
|
}));
|
2015-04-16 13:42:13 +00:00
|
|
|
|
|
|
|
$this->userManagerMock = $this->getMockBuilder('OCP\IUserManager')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
|
|
$this->keyManagerMock = $this->getMockBuilder('OCA\Encryption\KeyManager')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
|
|
$this->cryptMock = $this->getMockBuilder('OCA\Encryption\Crypto\Crypt')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
|
|
$this->userSessionMock = $this->getMockBuilder('OCP\IUserSession')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->setMethods([
|
|
|
|
'isLoggedIn',
|
|
|
|
'getUID',
|
|
|
|
'login',
|
|
|
|
'logout',
|
|
|
|
'setUser',
|
|
|
|
'getUser',
|
2015-04-20 15:44:34 +00:00
|
|
|
'canChangePassword',
|
2015-04-16 13:42:13 +00:00
|
|
|
])
|
|
|
|
->getMock();
|
|
|
|
|
2015-08-25 13:05:55 +00:00
|
|
|
$this->ocSessionMock = $this->getMockBuilder('\OCP\ISession')->disableOriginalConstructor()->getMock();
|
|
|
|
|
2015-04-16 13:42:13 +00:00
|
|
|
$this->userSessionMock->expects($this->any())
|
|
|
|
->method('getUID')
|
2015-08-25 13:05:55 +00:00
|
|
|
->willReturn('testUserUid');
|
2015-04-16 13:42:13 +00:00
|
|
|
|
|
|
|
$this->userSessionMock->expects($this->any())
|
|
|
|
->method($this->anything())
|
|
|
|
->will($this->returnSelf());
|
|
|
|
|
|
|
|
$this->sessionMock = $this->getMockBuilder('OCA\Encryption\Session')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
2015-10-13 15:54:06 +00:00
|
|
|
$this->utilMock = $this->getMockBuilder('OCA\Encryption\Util')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2015-04-16 13:42:13 +00:00
|
|
|
$this->controller = new SettingsController(
|
|
|
|
'encryption',
|
|
|
|
$this->requestMock,
|
|
|
|
$this->l10nMock,
|
|
|
|
$this->userManagerMock,
|
|
|
|
$this->userSessionMock,
|
|
|
|
$this->keyManagerMock,
|
|
|
|
$this->cryptMock,
|
2015-08-25 13:05:55 +00:00
|
|
|
$this->sessionMock,
|
2015-10-13 15:54:06 +00:00
|
|
|
$this->ocSessionMock,
|
|
|
|
$this->utilMock
|
2015-04-16 13:42:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test updatePrivateKeyPassword() if wrong new password was entered
|
|
|
|
*/
|
|
|
|
public function testUpdatePrivateKeyPasswordWrongNewPassword() {
|
|
|
|
|
|
|
|
$oldPassword = 'old';
|
|
|
|
$newPassword = 'new';
|
|
|
|
|
2015-08-25 13:05:55 +00:00
|
|
|
$this->userSessionMock->expects($this->once())->method('getUID')->willReturn('uid');
|
|
|
|
|
2015-04-16 13:42:13 +00:00
|
|
|
$this->userManagerMock
|
2015-08-25 13:05:55 +00:00
|
|
|
->expects($this->exactly(2))
|
2015-04-16 13:42:13 +00:00
|
|
|
->method('checkPassword')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$result = $this->controller->updatePrivateKeyPassword($oldPassword, $newPassword);
|
|
|
|
|
|
|
|
$data = $result->getData();
|
|
|
|
|
2015-04-20 15:44:34 +00:00
|
|
|
$this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus());
|
2015-04-16 13:42:13 +00:00
|
|
|
$this->assertSame('The current log-in password was not correct, please try again.',
|
2015-04-20 16:15:06 +00:00
|
|
|
$data['message']);
|
2015-04-16 13:42:13 +00: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 15:44:34 +00:00
|
|
|
$this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus());
|
2015-04-16 13:42:13 +00:00
|
|
|
$this->assertSame('The old password was not correct, please try again.',
|
2015-04-20 16:15:06 +00:00
|
|
|
$data['message']);
|
2015-04-16 13:42:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test updatePrivateKeyPassword() with the correct old and new password
|
|
|
|
*/
|
|
|
|
public function testUpdatePrivateKeyPassword() {
|
|
|
|
|
|
|
|
$oldPassword = 'old';
|
|
|
|
$newPassword = 'new';
|
|
|
|
|
2015-08-25 13:05:55 +00:00
|
|
|
$this->ocSessionMock->expects($this->once())
|
|
|
|
->method('get')->with('loginname')->willReturn('testUser');
|
2015-04-16 13:42:13 +00:00
|
|
|
|
|
|
|
$this->userManagerMock
|
2015-08-25 13:05:55 +00:00
|
|
|
->expects($this->at(0))
|
|
|
|
->method('checkPassword')
|
|
|
|
->with('testUserUid', 'new')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->userManagerMock
|
|
|
|
->expects($this->at(1))
|
2015-04-16 13:42:13 +00:00
|
|
|
->method('checkPassword')
|
2015-08-25 13:05:55 +00:00
|
|
|
->with('testUser', 'new')
|
2015-04-16 13:42:13 +00:00
|
|
|
->willReturn(true);
|
|
|
|
|
2015-08-25 13:05:55 +00:00
|
|
|
|
|
|
|
|
2015-04-16 13:42:13 +00:00
|
|
|
$this->cryptMock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('decryptPrivateKey')
|
|
|
|
->willReturn('decryptedKey');
|
|
|
|
|
|
|
|
$this->cryptMock
|
|
|
|
->expects($this->once())
|
2015-08-07 12:04:17 +00:00
|
|
|
->method('encryptPrivateKey')
|
2015-04-16 13:42:13 +00: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 13:05:55 +00:00
|
|
|
->with($this->equalTo('testUserUid'), $this->equalTo('header.encryptedKey'));
|
2015-04-16 13:42:13 +00: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 15:44:34 +00:00
|
|
|
$this->assertSame(Http::STATUS_OK, $result->getStatus());
|
2015-04-16 13:42:13 +00:00
|
|
|
$this->assertSame('Private key password successfully updated.',
|
2015-04-20 16:15:06 +00:00
|
|
|
$data['message']);
|
2015-04-16 13:42:13 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 15:54:06 +00:00
|
|
|
function testSetEncryptHomeStorage() {
|
|
|
|
$value = true;
|
|
|
|
$this->utilMock->expects($this->once())->method('setEncryptHomeStorage')->with($value);
|
|
|
|
$this->controller->setEncryptHomeStorage($value);
|
|
|
|
}
|
|
|
|
|
2015-04-16 13:42:13 +00:00
|
|
|
}
|