1
0

CryptoWrappingTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\Session;
  8. use OC\Session\CryptoSessionData;
  9. use OCP\ISession;
  10. use Test\TestCase;
  11. class CryptoWrappingTest extends TestCase {
  12. /** @var \PHPUnit\Framework\MockObject\MockObject|\OCP\Security\ICrypto */
  13. protected $crypto;
  14. /** @var \PHPUnit\Framework\MockObject\MockObject|\OCP\ISession */
  15. protected $wrappedSession;
  16. /** @var \OC\Session\CryptoSessionData */
  17. protected $instance;
  18. protected function setUp(): void {
  19. parent::setUp();
  20. $this->wrappedSession = $this->getMockBuilder(ISession::class)
  21. ->disableOriginalConstructor()
  22. ->getMock();
  23. $this->crypto = $this->getMockBuilder('OCP\Security\ICrypto')
  24. ->disableOriginalConstructor()
  25. ->getMock();
  26. $this->crypto->expects($this->any())
  27. ->method('encrypt')
  28. ->willReturnCallback(function ($input) {
  29. return $input;
  30. });
  31. $this->crypto->expects($this->any())
  32. ->method('decrypt')
  33. ->willReturnCallback(function ($input) {
  34. if ($input === '') {
  35. return '';
  36. }
  37. return substr($input, 1, -1);
  38. });
  39. $this->instance = new CryptoSessionData($this->wrappedSession, $this->crypto, 'PASS');
  40. }
  41. public function testUnwrappingGet(): void {
  42. $unencryptedValue = 'foobar';
  43. $encryptedValue = $this->crypto->encrypt($unencryptedValue);
  44. $this->wrappedSession->expects($this->once())
  45. ->method('get')
  46. ->with('encrypted_session_data')
  47. ->willReturnCallback(function () use ($encryptedValue) {
  48. return $encryptedValue;
  49. });
  50. $this->assertSame($unencryptedValue, $this->wrappedSession->get('encrypted_session_data'));
  51. }
  52. }