RecoveryTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * @author Clark Tomlinson <fallen013@gmail.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OCA\Encryption\Tests;
  22. use OCA\Encryption\Recovery;
  23. use Test\TestCase;
  24. class RecoveryTest extends TestCase {
  25. private static $tempStorage = [];
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $fileMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $viewMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $userSessionMock;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $keyManagerMock;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $configMock;
  46. /**
  47. * @var \PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $cryptMock;
  50. /**
  51. * @var Recovery
  52. */
  53. private $instance;
  54. public function testEnableAdminRecovery() {
  55. $this->keyManagerMock->expects($this->exactly(2))
  56. ->method('recoveryKeyExists')
  57. ->willReturnOnConsecutiveCalls(false, true);
  58. $this->cryptMock->expects($this->once())
  59. ->method('createKeyPair')
  60. ->willReturn(true);
  61. $this->keyManagerMock->expects($this->once())
  62. ->method('setRecoveryKey')
  63. ->willReturn(false);
  64. $this->keyManagerMock->expects($this->exactly(2))
  65. ->method('checkRecoveryPassword')
  66. ->willReturnOnConsecutiveCalls(true, false);
  67. $this->assertTrue($this->instance->enableAdminRecovery('password'));
  68. $this->assertArrayHasKey('recoveryAdminEnabled', self::$tempStorage);
  69. $this->assertEquals(1, self::$tempStorage['recoveryAdminEnabled']);
  70. $this->assertFalse($this->instance->enableAdminRecovery('password'));
  71. }
  72. public function testChangeRecoveryKeyPassword() {
  73. $this->assertFalse($this->instance->changeRecoveryKeyPassword('password',
  74. 'passwordOld'));
  75. $this->keyManagerMock->expects($this->once())
  76. ->method('getSystemPrivateKey');
  77. $this->cryptMock->expects($this->once())
  78. ->method('decryptPrivateKey');
  79. $this->cryptMock->expects($this->once())
  80. ->method('symmetricEncryptFileContent')
  81. ->willReturn(true);
  82. $this->assertTrue($this->instance->changeRecoveryKeyPassword('password',
  83. 'passwordOld'));
  84. }
  85. public function testDisableAdminRecovery() {
  86. $this->keyManagerMock->expects($this->exactly(2))
  87. ->method('checkRecoveryPassword')
  88. ->willReturnOnConsecutiveCalls(true, false);
  89. $this->assertArrayHasKey('recoveryAdminEnabled', self::$tempStorage);
  90. $this->assertTrue($this->instance->disableAdminRecovery('password'));
  91. $this->assertEquals(0, self::$tempStorage['recoveryAdminEnabled']);
  92. $this->assertFalse($this->instance->disableAdminRecovery('password'));
  93. }
  94. public function testIsRecoveryEnabledForUser() {
  95. $this->configMock->expects($this->exactly(2))
  96. ->method('getUserValue')
  97. ->willReturnOnConsecutiveCalls('1', '0');
  98. $this->assertTrue($this->instance->isRecoveryEnabledForUser());
  99. $this->assertFalse($this->instance->isRecoveryEnabledForUser('admin'));
  100. }
  101. public function testIsRecoveryKeyEnabled() {
  102. $this->assertFalse($this->instance->isRecoveryKeyEnabled());
  103. self::$tempStorage['recoveryAdminEnabled'] = '1';
  104. $this->assertTrue($this->instance->isRecoveryKeyEnabled());
  105. }
  106. public function testSetRecoveryFolderForUser() {
  107. $this->viewMock->expects($this->exactly(2))
  108. ->method('getDirectoryContent')
  109. ->willReturn([]);
  110. $this->assertTrue($this->instance->setRecoveryForUser(0));
  111. $this->assertTrue($this->instance->setRecoveryForUser('1'));
  112. }
  113. public function testRecoverUserFiles() {
  114. $this->viewMock->expects($this->once())
  115. ->method('getDirectoryContent')
  116. ->willReturn([]);
  117. $this->cryptMock->expects($this->once())
  118. ->method('decryptPrivateKey');
  119. $this->assertNull($this->instance->recoverUsersFiles('password',
  120. 'admin'));
  121. }
  122. public function testRecoverFile() {
  123. $this->keyManagerMock->expects($this->once())
  124. ->method('getEncryptedFileKey')
  125. ->willReturn(true);
  126. $this->keyManagerMock->expects($this->once())
  127. ->method('getShareKey')
  128. ->willReturn(true);
  129. $this->cryptMock->expects($this->once())
  130. ->method('multiKeyDecrypt')
  131. ->willReturn(true);
  132. $this->fileMock->expects($this->once())
  133. ->method('getAccessList')
  134. ->willReturn(['users' => ['admin']]);
  135. $this->keyManagerMock->expects($this->once())
  136. ->method('getPublicKey')
  137. ->willReturn('publicKey');
  138. $this->keyManagerMock->expects($this->once())
  139. ->method('addSystemKeys')
  140. ->willReturn(['admin' => 'publicKey']);
  141. $this->cryptMock->expects($this->once())
  142. ->method('multiKeyEncrypt');
  143. $this->keyManagerMock->expects($this->once())
  144. ->method('setAllFileKeys');
  145. $this->assertNull(\Test_Helper::invokePrivate($this->instance,
  146. 'recoverFile',
  147. ['/', 'testkey']));
  148. }
  149. protected function setUp() {
  150. parent::setUp();
  151. $this->userSessionMock = $this->getMockBuilder('OCP\IUserSession')
  152. ->disableOriginalConstructor()
  153. ->setMethods([
  154. 'isLoggedIn',
  155. 'getUID',
  156. 'login',
  157. 'logout',
  158. 'setUser',
  159. 'getUser'
  160. ])
  161. ->getMock();
  162. $this->userSessionMock->expects($this->any())->method('getUID')->will($this->returnValue('admin'));
  163. $this->userSessionMock->expects($this->any())
  164. ->method($this->anything())
  165. ->will($this->returnSelf());
  166. $this->cryptMock = $this->getMockBuilder('OCA\Encryption\Crypto\Crypt')->disableOriginalConstructor()->getMock();
  167. $randomMock = $this->getMock('OCP\Security\ISecureRandom');
  168. $this->keyManagerMock = $this->getMockBuilder('OCA\Encryption\KeyManager')->disableOriginalConstructor()->getMock();
  169. $this->configMock = $this->getMock('OCP\IConfig');
  170. $keyStorageMock = $this->getMock('OCP\Encryption\Keys\IStorage');
  171. $this->fileMock = $this->getMock('OCP\Encryption\IFile');
  172. $this->viewMock = $this->getMock('OC\Files\View');
  173. $this->configMock->expects($this->any())
  174. ->method('setAppValue')
  175. ->will($this->returnCallback([$this, 'setValueTester']));
  176. $this->configMock->expects($this->any())
  177. ->method('getAppValue')
  178. ->will($this->returnCallback([$this, 'getValueTester']));
  179. $this->instance = new Recovery($this->userSessionMock,
  180. $this->cryptMock,
  181. $randomMock,
  182. $this->keyManagerMock,
  183. $this->configMock,
  184. $keyStorageMock,
  185. $this->fileMock,
  186. $this->viewMock);
  187. }
  188. /**
  189. * @param $app
  190. * @param $key
  191. * @param $value
  192. */
  193. public function setValueTester($app, $key, $value) {
  194. self::$tempStorage[$key] = $value;
  195. }
  196. /**
  197. * @param $key
  198. */
  199. public function removeValueTester($key) {
  200. unset(self::$tempStorage[$key]);
  201. }
  202. /**
  203. * @param $app
  204. * @param $key
  205. * @return mixed
  206. */
  207. public function getValueTester($app, $key) {
  208. if (!empty(self::$tempStorage[$key])) {
  209. return self::$tempStorage[$key];
  210. }
  211. return null;
  212. }
  213. }