diff options
author | Morris Jobke <hey@morrisjobke.de> | 2015-08-24 12:10:15 +0200 |
---|---|---|
committer | Morris Jobke <hey@morrisjobke.de> | 2015-08-24 12:10:15 +0200 |
commit | b3495a1dc9ca339e839a3fda169bf9ce8656484d (patch) | |
tree | 4bc396b37d231011474638cd93104c5feb51b350 /tests | |
parent | cca35f0c3e94ce5270e3f2fa3619d08a976ab650 (diff) | |
parent | 6a3fb0d3b36dce7a6583d58ded1b133f086e2a95 (diff) | |
download | nextcloud-server-b3495a1dc9ca339e839a3fda169bf9ce8656484d.tar.gz nextcloud-server-b3495a1dc9ca339e839a3fda169bf9ce8656484d.zip |
Merge pull request #18482 from owncloud/encrypt-session-data
Add a session wrapper to encrypt the data before storing it on disk
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/server.php | 1 | ||||
-rw-r--r-- | tests/lib/session/cryptosessiondatatest.php | 53 | ||||
-rw-r--r-- | tests/lib/session/cryptowrappingtest.php | 82 |
3 files changed, 136 insertions, 0 deletions
diff --git a/tests/lib/server.php b/tests/lib/server.php index 9c5c83ceb5c..bc44c50a22a 100644 --- a/tests/lib/server.php +++ b/tests/lib/server.php @@ -56,6 +56,7 @@ class Server extends \Test\TestCase { ['ContactsManager', '\OCP\Contacts\IManager'], ['Crypto', '\OC\Security\Crypto'], ['Crypto', '\OCP\Security\ICrypto'], + ['CryptoWrapper', '\OC\Session\CryptoWrapper'], ['DatabaseConnection', '\OC\DB\Connection'], ['DatabaseConnection', '\OCP\IDBConnection'], diff --git a/tests/lib/session/cryptosessiondatatest.php b/tests/lib/session/cryptosessiondatatest.php new file mode 100644 index 00000000000..ee6bcbf11c1 --- /dev/null +++ b/tests/lib/session/cryptosessiondatatest.php @@ -0,0 +1,53 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@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 Test\Session; + +use OC\Session\CryptoSessionData; + +class CryptoSessionDataTest extends Session { + /** @var \PHPUnit_Framework_MockObject_MockObject|\OCP\Security\ICrypto */ + protected $crypto; + + /** @var \OCP\ISession */ + protected $wrappedSession; + + protected function setUp() { + parent::setUp(); + + $this->wrappedSession = new \OC\Session\Memory($this->getUniqueID()); + $this->crypto = $this->getMockBuilder('OCP\Security\ICrypto') + ->disableOriginalConstructor() + ->getMock(); + $this->crypto->expects($this->any()) + ->method('encrypt') + ->willReturnCallback(function ($input) { + return '#' . $input . '#'; + }); + $this->crypto->expects($this->any()) + ->method('decrypt') + ->willReturnCallback(function ($input) { + return substr($input, 1, -1); + }); + + $this->instance = new CryptoSessionData($this->wrappedSession, $this->crypto, 'PASS'); + } +} diff --git a/tests/lib/session/cryptowrappingtest.php b/tests/lib/session/cryptowrappingtest.php new file mode 100644 index 00000000000..12b3c905b7f --- /dev/null +++ b/tests/lib/session/cryptowrappingtest.php @@ -0,0 +1,82 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@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 Test\Session; + +use OC\Session\CryptoSessionData; +use Test\TestCase; + +class CryptoWrappingTest extends TestCase { + /** @var \PHPUnit_Framework_MockObject_MockObject|\OCP\Security\ICrypto */ + protected $crypto; + + /** @var \PHPUnit_Framework_MockObject_MockObject|\OCP\ISession */ + protected $wrappedSession; + + /** @var \OC\Session\CryptoSessionData */ + protected $instance; + + protected function setUp() { + parent::setUp(); + + $this->wrappedSession = $this->getMockBuilder('OCP\ISession') + ->disableOriginalConstructor() + ->getMock(); + $this->crypto = $this->getMockBuilder('OCP\Security\ICrypto') + ->disableOriginalConstructor() + ->getMock(); + $this->crypto->expects($this->any()) + ->method('encrypt') + ->willReturnCallback(function ($input) { + return $input; + }); + $this->crypto->expects($this->any()) + ->method('decrypt') + ->willReturnCallback(function ($input) { + return substr($input, 1, -1); + }); + + $this->instance = new CryptoSessionData($this->wrappedSession, $this->crypto, 'PASS'); + } + + public function testWrappingSet() { + $unencryptedValue = 'foobar'; + + $this->wrappedSession->expects($this->once()) + ->method('set') + ->with('key', $this->crypto->encrypt(json_encode($unencryptedValue))); + $this->instance->set('key', $unencryptedValue); + } + + public function testUnwrappingGet() { + $unencryptedValue = 'foobar'; + $encryptedValue = $this->crypto->encrypt($unencryptedValue); + + $this->wrappedSession->expects($this->once()) + ->method('get') + ->with('key') + ->willReturnCallback(function () use ($encryptedValue) { + return $encryptedValue; + }); + + $this->assertSame($unencryptedValue, $this->wrappedSession->get('key')); + } +} |