RecoveryTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Clark Tomlinson <fallen013@gmail.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Encryption\Tests;
  29. use OC\Files\View;
  30. use OCA\Encryption\Crypto\Crypt;
  31. use OCA\Encryption\KeyManager;
  32. use OCA\Encryption\Recovery;
  33. use OCP\Encryption\IFile;
  34. use OCP\IConfig;
  35. use OCP\IUser;
  36. use OCP\IUserSession;
  37. use PHPUnit\Framework\MockObject\MockObject;
  38. use Test\TestCase;
  39. class RecoveryTest extends TestCase {
  40. private static $tempStorage = [];
  41. /**
  42. * @var \OCP\Encryption\IFile|\PHPUnit\Framework\MockObject\MockObject
  43. */
  44. private $fileMock;
  45. /**
  46. * @var \OC\Files\View|\PHPUnit\Framework\MockObject\MockObject
  47. */
  48. private $viewMock;
  49. /**
  50. * @var \OCP\IUserSession|\PHPUnit\Framework\MockObject\MockObject
  51. */
  52. private $userSessionMock;
  53. /**
  54. * @var MockObject|IUser
  55. */
  56. private $user;
  57. /**
  58. * @var \OCA\Encryption\KeyManager|\PHPUnit\Framework\MockObject\MockObject
  59. */
  60. private $keyManagerMock;
  61. /**
  62. * @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject
  63. */
  64. private $configMock;
  65. /**
  66. * @var \OCA\Encryption\Crypto\Crypt|\PHPUnit\Framework\MockObject\MockObject
  67. */
  68. private $cryptMock;
  69. /**
  70. * @var Recovery
  71. */
  72. private $instance;
  73. public function testEnableAdminRecoverySuccessful() {
  74. $this->keyManagerMock->expects($this->exactly(2))
  75. ->method('recoveryKeyExists')
  76. ->willReturnOnConsecutiveCalls(false, true);
  77. $this->cryptMock->expects($this->once())
  78. ->method('createKeyPair')
  79. ->willReturn([
  80. 'publicKey' => 'privateKey',
  81. 'privateKey' => 'publicKey',
  82. ]);
  83. $this->keyManagerMock->expects($this->once())
  84. ->method('setRecoveryKey')
  85. ->willReturn(false);
  86. $this->keyManagerMock->expects($this->exactly(2))
  87. ->method('checkRecoveryPassword')
  88. ->willReturnOnConsecutiveCalls(true, true);
  89. $this->assertTrue($this->instance->enableAdminRecovery('password'));
  90. $this->assertArrayHasKey('recoveryAdminEnabled', self::$tempStorage);
  91. $this->assertEquals(1, self::$tempStorage['recoveryAdminEnabled']);
  92. $this->assertTrue($this->instance->enableAdminRecovery('password'));
  93. }
  94. public function testEnableAdminRecoveryCouldNotCheckPassword() {
  95. $this->keyManagerMock->expects($this->exactly(2))
  96. ->method('recoveryKeyExists')
  97. ->willReturnOnConsecutiveCalls(false, true);
  98. $this->cryptMock->expects($this->once())
  99. ->method('createKeyPair')
  100. ->willReturn([
  101. 'publicKey' => 'privateKey',
  102. 'privateKey' => 'publicKey',
  103. ]);
  104. $this->keyManagerMock->expects($this->once())
  105. ->method('setRecoveryKey')
  106. ->willReturn(false);
  107. $this->keyManagerMock->expects($this->exactly(2))
  108. ->method('checkRecoveryPassword')
  109. ->willReturnOnConsecutiveCalls(true, false);
  110. $this->assertTrue($this->instance->enableAdminRecovery('password'));
  111. $this->assertArrayHasKey('recoveryAdminEnabled', self::$tempStorage);
  112. $this->assertEquals(1, self::$tempStorage['recoveryAdminEnabled']);
  113. $this->assertFalse($this->instance->enableAdminRecovery('password'));
  114. }
  115. public function testEnableAdminRecoveryCouldNotCreateKey() {
  116. $this->keyManagerMock->expects($this->once())
  117. ->method('recoveryKeyExists')
  118. ->willReturn(false);
  119. $this->cryptMock->expects($this->once())
  120. ->method('createKeyPair')
  121. ->willReturn(false);
  122. $this->assertFalse($this->instance->enableAdminRecovery('password'));
  123. }
  124. public function testChangeRecoveryKeyPasswordSuccessful() {
  125. $this->assertFalse($this->instance->changeRecoveryKeyPassword('password',
  126. 'passwordOld'));
  127. $this->keyManagerMock->expects($this->once())
  128. ->method('getSystemPrivateKey');
  129. $this->cryptMock->expects($this->once())
  130. ->method('decryptPrivateKey');
  131. $this->cryptMock->expects($this->once())
  132. ->method('encryptPrivateKey')
  133. ->willReturn(true);
  134. $this->assertTrue($this->instance->changeRecoveryKeyPassword('password',
  135. 'passwordOld'));
  136. }
  137. public function testChangeRecoveryKeyPasswordCouldNotDecryptPrivateRecoveryKey() {
  138. $this->assertFalse($this->instance->changeRecoveryKeyPassword('password', 'passwordOld'));
  139. $this->keyManagerMock->expects($this->once())
  140. ->method('getSystemPrivateKey');
  141. $this->cryptMock->expects($this->once())
  142. ->method('decryptPrivateKey')
  143. ->willReturn(false);
  144. $this->assertFalse($this->instance->changeRecoveryKeyPassword('password', 'passwordOld'));
  145. }
  146. public function testDisableAdminRecovery() {
  147. $this->keyManagerMock->expects($this->exactly(2))
  148. ->method('checkRecoveryPassword')
  149. ->willReturnOnConsecutiveCalls(true, false);
  150. $this->assertArrayHasKey('recoveryAdminEnabled', self::$tempStorage);
  151. $this->assertTrue($this->instance->disableAdminRecovery('password'));
  152. $this->assertEquals(0, self::$tempStorage['recoveryAdminEnabled']);
  153. $this->assertFalse($this->instance->disableAdminRecovery('password'));
  154. }
  155. public function testIsRecoveryEnabledForUser() {
  156. $this->configMock->expects($this->exactly(2))
  157. ->method('getUserValue')
  158. ->willReturnOnConsecutiveCalls('1', '0');
  159. $this->assertTrue($this->instance->isRecoveryEnabledForUser());
  160. $this->assertFalse($this->instance->isRecoveryEnabledForUser('admin'));
  161. }
  162. public function testIsRecoveryKeyEnabled() {
  163. $this->assertFalse($this->instance->isRecoveryKeyEnabled());
  164. self::$tempStorage['recoveryAdminEnabled'] = '1';
  165. $this->assertTrue($this->instance->isRecoveryKeyEnabled());
  166. }
  167. public function testSetRecoveryFolderForUser() {
  168. $this->viewMock->expects($this->exactly(2))
  169. ->method('getDirectoryContent')
  170. ->willReturn([]);
  171. $this->assertTrue($this->instance->setRecoveryForUser(0));
  172. $this->assertTrue($this->instance->setRecoveryForUser('1'));
  173. }
  174. public function testRecoverUserFiles() {
  175. $this->viewMock->expects($this->once())
  176. ->method('getDirectoryContent')
  177. ->willReturn([]);
  178. $this->cryptMock->expects($this->once())
  179. ->method('decryptPrivateKey');
  180. $this->instance->recoverUsersFiles('password', 'admin');
  181. $this->addToAssertionCount(1);
  182. }
  183. public function testRecoverFile() {
  184. $this->keyManagerMock->expects($this->once())
  185. ->method('getEncryptedFileKey')
  186. ->willReturn(true);
  187. $this->keyManagerMock->expects($this->once())
  188. ->method('getShareKey')
  189. ->willReturn(true);
  190. $this->cryptMock->expects($this->once())
  191. ->method('multiKeyDecrypt')
  192. ->willReturn(true);
  193. $this->fileMock->expects($this->once())
  194. ->method('getAccessList')
  195. ->willReturn(['users' => ['admin']]);
  196. $this->keyManagerMock->expects($this->once())
  197. ->method('getPublicKey')
  198. ->willReturn('publicKey');
  199. $this->keyManagerMock->expects($this->once())
  200. ->method('addSystemKeys')
  201. ->with($this->anything(), $this->anything(), $this->equalTo('admin'))
  202. ->willReturn(['admin' => 'publicKey']);
  203. $this->cryptMock->expects($this->once())
  204. ->method('multiKeyEncrypt');
  205. $this->keyManagerMock->expects($this->once())
  206. ->method('setAllFileKeys');
  207. $this->assertNull(self::invokePrivate($this->instance,
  208. 'recoverFile',
  209. ['/', 'testkey', 'admin']));
  210. }
  211. protected function setUp(): void {
  212. parent::setUp();
  213. $this->user = $this->createMock(IUser::class);
  214. $this->user->expects($this->any())
  215. ->method('getUID')
  216. ->willReturn('admin');
  217. $this->userSessionMock = $this->createMock(IUserSession::class);
  218. $this->userSessionMock->expects($this->any())
  219. ->method('getUser')
  220. ->willReturn($this->user);
  221. $this->userSessionMock->expects($this->any())
  222. ->method('isLoggedIn')
  223. ->willReturn(true);
  224. $this->cryptMock = $this->getMockBuilder(Crypt::class)->disableOriginalConstructor()->getMock();
  225. $this->keyManagerMock = $this->getMockBuilder(KeyManager::class)->disableOriginalConstructor()->getMock();
  226. $this->configMock = $this->createMock(IConfig::class);
  227. $this->fileMock = $this->createMock(IFile::class);
  228. $this->viewMock = $this->createMock(View::class);
  229. $this->configMock->expects($this->any())
  230. ->method('setAppValue')
  231. ->willReturnCallback([$this, 'setValueTester']);
  232. $this->configMock->expects($this->any())
  233. ->method('getAppValue')
  234. ->willReturnCallback([$this, 'getValueTester']);
  235. $this->instance = new Recovery($this->userSessionMock,
  236. $this->cryptMock,
  237. $this->keyManagerMock,
  238. $this->configMock,
  239. $this->fileMock,
  240. $this->viewMock);
  241. }
  242. /**
  243. * @param $app
  244. * @param $key
  245. * @param $value
  246. */
  247. public function setValueTester($app, $key, $value) {
  248. self::$tempStorage[$key] = $value;
  249. }
  250. /**
  251. * @param $key
  252. */
  253. public function removeValueTester($key) {
  254. unset(self::$tempStorage[$key]);
  255. }
  256. /**
  257. * @param $app
  258. * @param $key
  259. * @return mixed
  260. */
  261. public function getValueTester($app, $key) {
  262. if (!empty(self::$tempStorage[$key])) {
  263. return self::$tempStorage[$key];
  264. }
  265. return null;
  266. }
  267. }