diff options
Diffstat (limited to 'tests/lib/Session')
-rw-r--r-- | tests/lib/Session/CryptoSessionDataTest.php | 44 | ||||
-rw-r--r-- | tests/lib/Session/CryptoWrappingTest.php | 65 | ||||
-rw-r--r-- | tests/lib/Session/MemoryTest.php | 26 | ||||
-rw-r--r-- | tests/lib/Session/Session.php | 66 |
4 files changed, 201 insertions, 0 deletions
diff --git a/tests/lib/Session/CryptoSessionDataTest.php b/tests/lib/Session/CryptoSessionDataTest.php new file mode 100644 index 00000000000..a30a3297094 --- /dev/null +++ b/tests/lib/Session/CryptoSessionDataTest.php @@ -0,0 +1,44 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ + +namespace Test\Session; + +use OC\Session\CryptoSessionData; +use OC\Session\Memory; +use OCP\ISession; +use OCP\Security\ICrypto; + +class CryptoSessionDataTest extends Session { + /** @var \PHPUnit\Framework\MockObject\MockObject|ICrypto */ + protected $crypto; + + /** @var ISession */ + protected $wrappedSession; + + protected function setUp(): void { + parent::setUp(); + + $this->wrappedSession = new Memory(); + $this->crypto = $this->createMock(ICrypto::class); + $this->crypto->expects($this->any()) + ->method('encrypt') + ->willReturnCallback(function ($input) { + return '#' . $input . '#'; + }); + $this->crypto->expects($this->any()) + ->method('decrypt') + ->willReturnCallback(function ($input) { + if ($input === '') { + return ''; + } + 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..2bc09e0903a --- /dev/null +++ b/tests/lib/Session/CryptoWrappingTest.php @@ -0,0 +1,65 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ + +namespace Test\Session; + +use OC\Session\CryptoSessionData; +use OCP\ISession; +use OCP\Security\ICrypto; +use Test\TestCase; + +class CryptoWrappingTest extends TestCase { + /** @var \PHPUnit\Framework\MockObject\MockObject|ICrypto */ + protected $crypto; + + /** @var \PHPUnit\Framework\MockObject\MockObject|ISession */ + protected $wrappedSession; + + /** @var \OC\Session\CryptoSessionData */ + protected $instance; + + protected function setUp(): void { + parent::setUp(); + + $this->wrappedSession = $this->getMockBuilder(ISession::class) + ->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) { + if ($input === '') { + return ''; + } + return substr($input, 1, -1); + }); + + $this->instance = new CryptoSessionData($this->wrappedSession, $this->crypto, 'PASS'); + } + + public function testUnwrappingGet(): void { + $unencryptedValue = 'foobar'; + $encryptedValue = $this->crypto->encrypt($unencryptedValue); + + $this->wrappedSession->expects($this->once()) + ->method('get') + ->with('encrypted_session_data') + ->willReturnCallback(function () use ($encryptedValue) { + return $encryptedValue; + }); + + $this->assertSame($unencryptedValue, $this->wrappedSession->get('encrypted_session_data')); + } +} diff --git a/tests/lib/Session/MemoryTest.php b/tests/lib/Session/MemoryTest.php new file mode 100644 index 00000000000..40977f1f3fb --- /dev/null +++ b/tests/lib/Session/MemoryTest.php @@ -0,0 +1,26 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace Test\Session; + +use OC\Session\Memory; +use OCP\Session\Exceptions\SessionNotAvailableException; + +class MemoryTest extends Session { + protected function setUp(): void { + parent::setUp(); + $this->instance = new Memory(); + } + + + public function testThrowsExceptionOnGetId(): void { + $this->expectException(SessionNotAvailableException::class); + + $this->instance->getId(); + } +} diff --git a/tests/lib/Session/Session.php b/tests/lib/Session/Session.php new file mode 100644 index 00000000000..1e2e9370825 --- /dev/null +++ b/tests/lib/Session/Session.php @@ -0,0 +1,66 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace Test\Session; + +abstract class Session extends \Test\TestCase { + /** + * @var \OC\Session\Session + */ + protected $instance; + + protected function tearDown(): void { + $this->instance->clear(); + parent::tearDown(); + } + + public function testNotExistsEmpty(): void { + $this->assertFalse($this->instance->exists('foo')); + } + + public function testExistsAfterSet(): void { + $this->instance->set('foo', 1); + $this->assertTrue($this->instance->exists('foo')); + } + + public function testNotExistsAfterRemove(): void { + $this->instance->set('foo', 1); + $this->instance->remove('foo'); + $this->assertFalse($this->instance->exists('foo')); + } + + public function testGetNonExisting(): void { + $this->assertNull($this->instance->get('foo')); + } + + public function testGetAfterSet(): void { + $this->instance->set('foo', 'bar'); + $this->assertEquals('bar', $this->instance->get(('foo'))); + } + + public function testRemoveNonExisting(): void { + $this->assertFalse($this->instance->exists('foo')); + $this->instance->remove('foo'); + $this->assertFalse($this->instance->exists('foo')); + } + + public function testNotExistsAfterClear(): void { + $this->instance->set('foo', 1); + $this->instance->clear(); + $this->assertFalse($this->instance->exists('foo')); + } + + public function testArrayInterface(): void { + $this->assertFalse(isset($this->instance['foo'])); + $this->instance['foo'] = 'bar'; + $this->assertTrue(isset($this->instance['foo'])); + $this->assertEquals('bar', $this->instance['foo']); + unset($this->instance['foo']); + $this->assertFalse(isset($this->instance['foo'])); + } +} |