SessionTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Clark Tomlinson <fallen013@gmail.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Encryption\Tests;
  26. use OCA\Encryption\Session;
  27. use OCP\ISession;
  28. use Test\TestCase;
  29. class SessionTest extends TestCase {
  30. private static $tempStorage = [];
  31. /**
  32. * @var Session
  33. */
  34. private $instance;
  35. /** @var \OCP\ISession|\PHPUnit_Framework_MockObject_MockObject */
  36. private $sessionMock;
  37. /**
  38. * @expectedException \OCA\Encryption\Exceptions\PrivateKeyMissingException
  39. * @expectedExceptionMessage Private Key missing for user: please try to log-out and log-in again
  40. */
  41. public function testThatGetPrivateKeyThrowsExceptionWhenNotSet() {
  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. * @expectedException \Exception
  73. * @expectExceptionMessage 'Please activate decrypt all mode first'
  74. */
  75. public function testGetDecryptAllUidException() {
  76. $this->instance->getDecryptAllUid();
  77. }
  78. /**
  79. * @expectedException \Exception
  80. * @expectExceptionMessage 'No uid found while in decrypt all mode'
  81. */
  82. public function testGetDecryptAllUidException2() {
  83. $this->instance->prepareDecryptAll(null, 'key');
  84. $this->instance->getDecryptAllUid();
  85. }
  86. /**
  87. * @expectedException \OCA\Encryption\Exceptions\PrivateKeyMissingException
  88. * @expectExceptionMessage 'Please activate decrypt all mode first'
  89. */
  90. public function testGetDecryptAllKeyException() {
  91. $this->instance->getDecryptAllKey();
  92. }
  93. /**
  94. * @expectedException \OCA\Encryption\Exceptions\PrivateKeyMissingException
  95. * @expectExceptionMessage 'No key found while in decrypt all mode'
  96. */
  97. public function testGetDecryptAllKeyException2() {
  98. $this->instance->prepareDecryptAll('user', null);
  99. $this->instance->getDecryptAllKey();
  100. }
  101. /**
  102. *
  103. */
  104. public function testSetAndGetStatusWillSetAndReturn() {
  105. // Check if get status will return 0 if it has not been set before
  106. $this->assertEquals(0, $this->instance->getStatus());
  107. $this->instance->setStatus(Session::NOT_INITIALIZED);
  108. $this->assertEquals(0, $this->instance->getStatus());
  109. $this->instance->setStatus(Session::INIT_EXECUTED);
  110. $this->assertEquals(1, $this->instance->getStatus());
  111. $this->instance->setStatus(Session::INIT_SUCCESSFUL);
  112. $this->assertEquals(2, $this->instance->getStatus());
  113. }
  114. /**
  115. * @dataProvider dataTestIsReady
  116. *
  117. * @param int $status
  118. * @param bool $expected
  119. */
  120. public function testIsReady($status, $expected) {
  121. /** @var Session | \PHPUnit_Framework_MockObject_MockObject $instance */
  122. $instance = $this->getMockBuilder(Session::class)
  123. ->setConstructorArgs([$this->sessionMock])
  124. ->setMethods(['getStatus'])->getMock();
  125. $instance->expects($this->once())->method('getStatus')
  126. ->willReturn($status);
  127. $this->assertSame($expected, $instance->isReady());
  128. }
  129. public function dataTestIsReady() {
  130. return [
  131. [Session::INIT_SUCCESSFUL, true],
  132. [Session::INIT_EXECUTED, false],
  133. [Session::NOT_INITIALIZED, false],
  134. ];
  135. }
  136. /**
  137. * @param $key
  138. * @param $value
  139. */
  140. public function setValueTester($key, $value) {
  141. self::$tempStorage[$key] = $value;
  142. }
  143. /**
  144. * @param $key
  145. */
  146. public function removeValueTester($key) {
  147. unset(self::$tempStorage[$key]);
  148. }
  149. /**
  150. * @param $key
  151. * @return mixed
  152. */
  153. public function getValueTester($key) {
  154. if (!empty(self::$tempStorage[$key])) {
  155. return self::$tempStorage[$key];
  156. }
  157. return null;
  158. }
  159. /**
  160. *
  161. */
  162. public function testClearWillRemoveValues() {
  163. $this->instance->setPrivateKey('privateKey');
  164. $this->instance->setStatus('initStatus');
  165. $this->instance->prepareDecryptAll('user', 'key');
  166. $this->assertNotEmpty(self::$tempStorage);
  167. $this->instance->clear();
  168. $this->assertEmpty(self::$tempStorage);
  169. }
  170. /**
  171. *
  172. */
  173. protected function setUp() {
  174. parent::setUp();
  175. $this->sessionMock = $this->createMock(ISession::class);
  176. $this->sessionMock->expects($this->any())
  177. ->method('set')
  178. ->will($this->returnCallback([$this, "setValueTester"]));
  179. $this->sessionMock->expects($this->any())
  180. ->method('get')
  181. ->will($this->returnCallback([$this, "getValueTester"]));
  182. $this->sessionMock->expects($this->any())
  183. ->method('remove')
  184. ->will($this->returnCallback([$this, "removeValueTester"]));
  185. $this->instance = new Session($this->sessionMock);
  186. }
  187. protected function tearDown() {
  188. self::$tempStorage = [];
  189. parent::tearDown();
  190. }
  191. }