1
0

SessionTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Encryption\Tests;
  8. use OCA\Encryption\Exceptions\PrivateKeyMissingException;
  9. use OCA\Encryption\Session;
  10. use OCP\ISession;
  11. use Test\TestCase;
  12. class SessionTest extends TestCase {
  13. private static $tempStorage = [];
  14. /**
  15. * @var Session
  16. */
  17. private $instance;
  18. /** @var ISession|\PHPUnit\Framework\MockObject\MockObject */
  19. private $sessionMock;
  20. public function testThatGetPrivateKeyThrowsExceptionWhenNotSet(): void {
  21. $this->expectException(PrivateKeyMissingException::class);
  22. $this->expectExceptionMessage('Private Key missing for user: please try to log-out and log-in again');
  23. $this->instance->getPrivateKey();
  24. }
  25. /**
  26. * @depends testThatGetPrivateKeyThrowsExceptionWhenNotSet
  27. */
  28. public function testSetAndGetPrivateKey(): void {
  29. $this->instance->setPrivateKey('dummyPrivateKey');
  30. $this->assertEquals('dummyPrivateKey', $this->instance->getPrivateKey());
  31. }
  32. /**
  33. * @depends testSetAndGetPrivateKey
  34. */
  35. public function testIsPrivateKeySet(): void {
  36. $this->instance->setPrivateKey('dummyPrivateKey');
  37. $this->assertTrue($this->instance->isPrivateKeySet());
  38. unset(self::$tempStorage['privateKey']);
  39. $this->assertFalse($this->instance->isPrivateKeySet());
  40. // Set private key back so we can test clear method
  41. self::$tempStorage['privateKey'] = 'dummyPrivateKey';
  42. }
  43. public function testDecryptAllModeActivated(): void {
  44. $this->instance->prepareDecryptAll('user1', 'usersKey');
  45. $this->assertTrue($this->instance->decryptAllModeActivated());
  46. $this->assertSame('user1', $this->instance->getDecryptAllUid());
  47. $this->assertSame('usersKey', $this->instance->getDecryptAllKey());
  48. }
  49. public function testDecryptAllModeDeactivated(): void {
  50. $this->assertFalse($this->instance->decryptAllModeActivated());
  51. }
  52. /**
  53. * @expectExceptionMessage 'Please activate decrypt all mode first'
  54. */
  55. public function testGetDecryptAllUidException(): void {
  56. $this->expectException(\Exception::class);
  57. $this->instance->getDecryptAllUid();
  58. }
  59. /**
  60. * @expectExceptionMessage 'No uid found while in decrypt all mode'
  61. */
  62. public function testGetDecryptAllUidException2(): void {
  63. $this->expectException(\Exception::class);
  64. $this->instance->prepareDecryptAll(null, 'key');
  65. $this->instance->getDecryptAllUid();
  66. }
  67. /**
  68. * @expectExceptionMessage 'Please activate decrypt all mode first'
  69. */
  70. public function testGetDecryptAllKeyException(): void {
  71. $this->expectException(PrivateKeyMissingException::class);
  72. $this->instance->getDecryptAllKey();
  73. }
  74. /**
  75. * @expectExceptionMessage 'No key found while in decrypt all mode'
  76. */
  77. public function testGetDecryptAllKeyException2(): void {
  78. $this->expectException(PrivateKeyMissingException::class);
  79. $this->instance->prepareDecryptAll('user', null);
  80. $this->instance->getDecryptAllKey();
  81. }
  82. public function testSetAndGetStatusWillSetAndReturn(): void {
  83. // Check if get status will return 0 if it has not been set before
  84. $this->assertEquals(0, $this->instance->getStatus());
  85. $this->instance->setStatus(Session::NOT_INITIALIZED);
  86. $this->assertEquals(0, $this->instance->getStatus());
  87. $this->instance->setStatus(Session::INIT_EXECUTED);
  88. $this->assertEquals(1, $this->instance->getStatus());
  89. $this->instance->setStatus(Session::INIT_SUCCESSFUL);
  90. $this->assertEquals(2, $this->instance->getStatus());
  91. }
  92. /**
  93. * @dataProvider dataTestIsReady
  94. *
  95. * @param int $status
  96. * @param bool $expected
  97. */
  98. public function testIsReady($status, $expected): void {
  99. /** @var Session | \PHPUnit\Framework\MockObject\MockObject $instance */
  100. $instance = $this->getMockBuilder(Session::class)
  101. ->setConstructorArgs([$this->sessionMock])
  102. ->setMethods(['getStatus'])->getMock();
  103. $instance->expects($this->once())->method('getStatus')
  104. ->willReturn($status);
  105. $this->assertSame($expected, $instance->isReady());
  106. }
  107. public function dataTestIsReady() {
  108. return [
  109. [Session::INIT_SUCCESSFUL, true],
  110. [Session::INIT_EXECUTED, false],
  111. [Session::NOT_INITIALIZED, false],
  112. ];
  113. }
  114. /**
  115. * @param $key
  116. * @param $value
  117. */
  118. public function setValueTester($key, $value) {
  119. self::$tempStorage[$key] = $value;
  120. }
  121. /**
  122. * @param $key
  123. */
  124. public function removeValueTester($key) {
  125. unset(self::$tempStorage[$key]);
  126. }
  127. /**
  128. * @param $key
  129. * @return mixed
  130. */
  131. public function getValueTester($key) {
  132. if (!empty(self::$tempStorage[$key])) {
  133. return self::$tempStorage[$key];
  134. }
  135. return null;
  136. }
  137. public function testClearWillRemoveValues(): void {
  138. $this->instance->setPrivateKey('privateKey');
  139. $this->instance->setStatus('initStatus');
  140. $this->instance->prepareDecryptAll('user', 'key');
  141. $this->assertNotEmpty(self::$tempStorage);
  142. $this->instance->clear();
  143. $this->assertEmpty(self::$tempStorage);
  144. }
  145. protected function setUp(): void {
  146. parent::setUp();
  147. $this->sessionMock = $this->createMock(ISession::class);
  148. $this->sessionMock->expects($this->any())
  149. ->method('set')
  150. ->willReturnCallback([$this, 'setValueTester']);
  151. $this->sessionMock->expects($this->any())
  152. ->method('get')
  153. ->willReturnCallback([$this, 'getValueTester']);
  154. $this->sessionMock->expects($this->any())
  155. ->method('remove')
  156. ->willReturnCallback([$this, 'removeValueTester']);
  157. $this->instance = new Session($this->sessionMock);
  158. }
  159. protected function tearDown(): void {
  160. self::$tempStorage = [];
  161. parent::tearDown();
  162. }
  163. }