SessionTest.php 6.2 KB

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