SessionTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Clark Tomlinson <fallen013@gmail.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Encryption\Tests;
  28. use OCA\Encryption\Session;
  29. use OCP\ISession;
  30. use Test\TestCase;
  31. class SessionTest extends TestCase {
  32. private static $tempStorage = [];
  33. /**
  34. * @var Session
  35. */
  36. private $instance;
  37. /** @var \OCP\ISession|\PHPUnit\Framework\MockObject\MockObject */
  38. private $sessionMock;
  39. public function testThatGetPrivateKeyThrowsExceptionWhenNotSet() {
  40. $this->expectException(\OCA\Encryption\Exceptions\PrivateKeyMissingException::class);
  41. $this->expectExceptionMessage('Private Key missing for user: please try to log-out and log-in again');
  42. $this->instance->getPrivateKey();
  43. }
  44. /**
  45. * @depends testThatGetPrivateKeyThrowsExceptionWhenNotSet
  46. */
  47. public function testSetAndGetPrivateKey() {
  48. $this->instance->setPrivateKey('dummyPrivateKey');
  49. $this->assertEquals('dummyPrivateKey', $this->instance->getPrivateKey());
  50. }
  51. /**
  52. * @depends testSetAndGetPrivateKey
  53. */
  54. public function testIsPrivateKeySet() {
  55. $this->instance->setPrivateKey('dummyPrivateKey');
  56. $this->assertTrue($this->instance->isPrivateKeySet());
  57. unset(self::$tempStorage['privateKey']);
  58. $this->assertFalse($this->instance->isPrivateKeySet());
  59. // Set private key back so we can test clear method
  60. self::$tempStorage['privateKey'] = 'dummyPrivateKey';
  61. }
  62. public function testDecryptAllModeActivated() {
  63. $this->instance->prepareDecryptAll('user1', 'usersKey');
  64. $this->assertTrue($this->instance->decryptAllModeActivated());
  65. $this->assertSame('user1', $this->instance->getDecryptAllUid());
  66. $this->assertSame('usersKey', $this->instance->getDecryptAllKey());
  67. }
  68. public function testDecryptAllModeDeactivated() {
  69. $this->assertFalse($this->instance->decryptAllModeActivated());
  70. }
  71. /**
  72. * @expectExceptionMessage 'Please activate decrypt all mode first'
  73. */
  74. public function testGetDecryptAllUidException() {
  75. $this->expectException(\Exception::class);
  76. $this->instance->getDecryptAllUid();
  77. }
  78. /**
  79. * @expectExceptionMessage 'No uid found while in decrypt all mode'
  80. */
  81. public function testGetDecryptAllUidException2() {
  82. $this->expectException(\Exception::class);
  83. $this->instance->prepareDecryptAll(null, 'key');
  84. $this->instance->getDecryptAllUid();
  85. }
  86. /**
  87. * @expectExceptionMessage 'Please activate decrypt all mode first'
  88. */
  89. public function testGetDecryptAllKeyException() {
  90. $this->expectException(\OCA\Encryption\Exceptions\PrivateKeyMissingException::class);
  91. $this->instance->getDecryptAllKey();
  92. }
  93. /**
  94. * @expectExceptionMessage 'No key found while in decrypt all mode'
  95. */
  96. public function testGetDecryptAllKeyException2() {
  97. $this->expectException(\OCA\Encryption\Exceptions\PrivateKeyMissingException::class);
  98. $this->instance->prepareDecryptAll('user', null);
  99. $this->instance->getDecryptAllKey();
  100. }
  101. public function testSetAndGetStatusWillSetAndReturn() {
  102. // Check if get status will return 0 if it has not been set before
  103. $this->assertEquals(0, $this->instance->getStatus());
  104. $this->instance->setStatus(Session::NOT_INITIALIZED);
  105. $this->assertEquals(0, $this->instance->getStatus());
  106. $this->instance->setStatus(Session::INIT_EXECUTED);
  107. $this->assertEquals(1, $this->instance->getStatus());
  108. $this->instance->setStatus(Session::INIT_SUCCESSFUL);
  109. $this->assertEquals(2, $this->instance->getStatus());
  110. }
  111. /**
  112. * @dataProvider dataTestIsReady
  113. *
  114. * @param int $status
  115. * @param bool $expected
  116. */
  117. public function testIsReady($status, $expected) {
  118. /** @var Session | \PHPUnit\Framework\MockObject\MockObject $instance */
  119. $instance = $this->getMockBuilder(Session::class)
  120. ->setConstructorArgs([$this->sessionMock])
  121. ->setMethods(['getStatus'])->getMock();
  122. $instance->expects($this->once())->method('getStatus')
  123. ->willReturn($status);
  124. $this->assertSame($expected, $instance->isReady());
  125. }
  126. public function dataTestIsReady() {
  127. return [
  128. [Session::INIT_SUCCESSFUL, true],
  129. [Session::INIT_EXECUTED, false],
  130. [Session::NOT_INITIALIZED, false],
  131. ];
  132. }
  133. /**
  134. * @param $key
  135. * @param $value
  136. */
  137. public function setValueTester($key, $value) {
  138. self::$tempStorage[$key] = $value;
  139. }
  140. /**
  141. * @param $key
  142. */
  143. public function removeValueTester($key) {
  144. unset(self::$tempStorage[$key]);
  145. }
  146. /**
  147. * @param $key
  148. * @return mixed
  149. */
  150. public function getValueTester($key) {
  151. if (!empty(self::$tempStorage[$key])) {
  152. return self::$tempStorage[$key];
  153. }
  154. return null;
  155. }
  156. public function testClearWillRemoveValues() {
  157. $this->instance->setPrivateKey('privateKey');
  158. $this->instance->setStatus('initStatus');
  159. $this->instance->prepareDecryptAll('user', 'key');
  160. $this->assertNotEmpty(self::$tempStorage);
  161. $this->instance->clear();
  162. $this->assertEmpty(self::$tempStorage);
  163. }
  164. protected function setUp(): void {
  165. parent::setUp();
  166. $this->sessionMock = $this->createMock(ISession::class);
  167. $this->sessionMock->expects($this->any())
  168. ->method('set')
  169. ->willReturnCallback([$this, "setValueTester"]);
  170. $this->sessionMock->expects($this->any())
  171. ->method('get')
  172. ->willReturnCallback([$this, "getValueTester"]);
  173. $this->sessionMock->expects($this->any())
  174. ->method('remove')
  175. ->willReturnCallback([$this, "removeValueTester"]);
  176. $this->instance = new Session($this->sessionMock);
  177. }
  178. protected function tearDown(): void {
  179. self::$tempStorage = [];
  180. parent::tearDown();
  181. }
  182. }