SessionTest.php 5.4 KB

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