cryptowrappingtest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Session;
  22. use OC\Session\CryptoSessionData;
  23. use Test\TestCase;
  24. class CryptoWrappingTest extends TestCase {
  25. /** @var \PHPUnit_Framework_MockObject_MockObject|\OCP\Security\ICrypto */
  26. protected $crypto;
  27. /** @var \PHPUnit_Framework_MockObject_MockObject|\OCP\ISession */
  28. protected $wrappedSession;
  29. /** @var \OC\Session\CryptoSessionData */
  30. protected $instance;
  31. protected function setUp() {
  32. parent::setUp();
  33. $this->wrappedSession = $this->getMockBuilder('OCP\ISession')
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $this->crypto = $this->getMockBuilder('OCP\Security\ICrypto')
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $this->crypto->expects($this->any())
  40. ->method('encrypt')
  41. ->willReturnCallback(function ($input) {
  42. return $input;
  43. });
  44. $this->crypto->expects($this->any())
  45. ->method('decrypt')
  46. ->willReturnCallback(function ($input) {
  47. return substr($input, 1, -1);
  48. });
  49. $this->instance = new CryptoSessionData($this->wrappedSession, $this->crypto, 'PASS');
  50. }
  51. public function testUnwrappingGet() {
  52. $unencryptedValue = 'foobar';
  53. $encryptedValue = $this->crypto->encrypt($unencryptedValue);
  54. $this->wrappedSession->expects($this->once())
  55. ->method('get')
  56. ->with('encrypted_session_data')
  57. ->willReturnCallback(function () use ($encryptedValue) {
  58. return $encryptedValue;
  59. });
  60. $this->assertSame($unencryptedValue, $this->wrappedSession->get('encrypted_session_data'));
  61. }
  62. }