SessionTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. * @author Thomas Müller <thomas.mueller@tmit.eu>
  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. /**
  40. * @expectedException \OCA\Encryption\Exceptions\PrivateKeyMissingException
  41. * @expectedExceptionMessage Private Key missing for user: please try to log-out and log-in again
  42. */
  43. public function testThatGetPrivateKeyThrowsExceptionWhenNotSet() {
  44. $this->instance->getPrivateKey();
  45. }
  46. /**
  47. * @depends testThatGetPrivateKeyThrowsExceptionWhenNotSet
  48. */
  49. public function testSetAndGetPrivateKey() {
  50. $this->instance->setPrivateKey('dummyPrivateKey');
  51. $this->assertEquals('dummyPrivateKey', $this->instance->getPrivateKey());
  52. }
  53. /**
  54. * @depends testSetAndGetPrivateKey
  55. */
  56. public function testIsPrivateKeySet() {
  57. $this->instance->setPrivateKey('dummyPrivateKey');
  58. $this->assertTrue($this->instance->isPrivateKeySet());
  59. unset(self::$tempStorage['privateKey']);
  60. $this->assertFalse($this->instance->isPrivateKeySet());
  61. // Set private key back so we can test clear method
  62. self::$tempStorage['privateKey'] = 'dummyPrivateKey';
  63. }
  64. public function testDecryptAllModeActivated() {
  65. $this->instance->prepareDecryptAll('user1', 'usersKey');
  66. $this->assertTrue($this->instance->decryptAllModeActivated());
  67. $this->assertSame('user1', $this->instance->getDecryptAllUid());
  68. $this->assertSame('usersKey', $this->instance->getDecryptAllKey());
  69. }
  70. public function testDecryptAllModeDeactivated() {
  71. $this->assertFalse($this->instance->decryptAllModeActivated());
  72. }
  73. /**
  74. * @expectedException \Exception
  75. * @expectExceptionMessage 'Please activate decrypt all mode first'
  76. */
  77. public function testGetDecryptAllUidException() {
  78. $this->instance->getDecryptAllUid();
  79. }
  80. /**
  81. * @expectedException \Exception
  82. * @expectExceptionMessage 'No uid found while in decrypt all mode'
  83. */
  84. public function testGetDecryptAllUidException2() {
  85. $this->instance->prepareDecryptAll(null, 'key');
  86. $this->instance->getDecryptAllUid();
  87. }
  88. /**
  89. * @expectedException \OCA\Encryption\Exceptions\PrivateKeyMissingException
  90. * @expectExceptionMessage 'Please activate decrypt all mode first'
  91. */
  92. public function testGetDecryptAllKeyException() {
  93. $this->instance->getDecryptAllKey();
  94. }
  95. /**
  96. * @expectedException \OCA\Encryption\Exceptions\PrivateKeyMissingException
  97. * @expectExceptionMessage 'No key found while in decrypt all mode'
  98. */
  99. public function testGetDecryptAllKeyException2() {
  100. $this->instance->prepareDecryptAll('user', null);
  101. $this->instance->getDecryptAllKey();
  102. }
  103. /**
  104. *
  105. */
  106. public function testSetAndGetStatusWillSetAndReturn() {
  107. // Check if get status will return 0 if it has not been set before
  108. $this->assertEquals(0, $this->instance->getStatus());
  109. $this->instance->setStatus(Session::NOT_INITIALIZED);
  110. $this->assertEquals(0, $this->instance->getStatus());
  111. $this->instance->setStatus(Session::INIT_EXECUTED);
  112. $this->assertEquals(1, $this->instance->getStatus());
  113. $this->instance->setStatus(Session::INIT_SUCCESSFUL);
  114. $this->assertEquals(2, $this->instance->getStatus());
  115. }
  116. /**
  117. * @dataProvider dataTestIsReady
  118. *
  119. * @param int $status
  120. * @param bool $expected
  121. */
  122. public function testIsReady($status, $expected) {
  123. /** @var Session | \PHPUnit_Framework_MockObject_MockObject $instance */
  124. $instance = $this->getMockBuilder(Session::class)
  125. ->setConstructorArgs([$this->sessionMock])
  126. ->setMethods(['getStatus'])->getMock();
  127. $instance->expects($this->once())->method('getStatus')
  128. ->willReturn($status);
  129. $this->assertSame($expected, $instance->isReady());
  130. }
  131. public function dataTestIsReady() {
  132. return [
  133. [Session::INIT_SUCCESSFUL, true],
  134. [Session::INIT_EXECUTED, false],
  135. [Session::NOT_INITIALIZED, false],
  136. ];
  137. }
  138. /**
  139. * @param $key
  140. * @param $value
  141. */
  142. public function setValueTester($key, $value) {
  143. self::$tempStorage[$key] = $value;
  144. }
  145. /**
  146. * @param $key
  147. */
  148. public function removeValueTester($key) {
  149. unset(self::$tempStorage[$key]);
  150. }
  151. /**
  152. * @param $key
  153. * @return mixed
  154. */
  155. public function getValueTester($key) {
  156. if (!empty(self::$tempStorage[$key])) {
  157. return self::$tempStorage[$key];
  158. }
  159. return null;
  160. }
  161. /**
  162. *
  163. */
  164. public function testClearWillRemoveValues() {
  165. $this->instance->setPrivateKey('privateKey');
  166. $this->instance->setStatus('initStatus');
  167. $this->instance->prepareDecryptAll('user', 'key');
  168. $this->assertNotEmpty(self::$tempStorage);
  169. $this->instance->clear();
  170. $this->assertEmpty(self::$tempStorage);
  171. }
  172. /**
  173. *
  174. */
  175. protected function setUp() {
  176. parent::setUp();
  177. $this->sessionMock = $this->createMock(ISession::class);
  178. $this->sessionMock->expects($this->any())
  179. ->method('set')
  180. ->will($this->returnCallback([$this, "setValueTester"]));
  181. $this->sessionMock->expects($this->any())
  182. ->method('get')
  183. ->will($this->returnCallback([$this, "getValueTester"]));
  184. $this->sessionMock->expects($this->any())
  185. ->method('remove')
  186. ->will($this->returnCallback([$this, "removeValueTester"]));
  187. $this->instance = new Session($this->sessionMock);
  188. }
  189. protected function tearDown() {
  190. self::$tempStorage = [];
  191. parent::tearDown();
  192. }
  193. }