UserHooksTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Clark Tomlinson <fallen013@gmail.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  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\Hooks;
  29. use OCA\Encryption\Crypto\Crypt;
  30. use OCA\Encryption\Hooks\UserHooks;
  31. use OCA\Encryption\KeyManager;
  32. use OCA\Encryption\Recovery;
  33. use OCA\Encryption\Session;
  34. use OCA\Encryption\Users\Setup;
  35. use OCA\Encryption\Util;
  36. use OCP\ILogger;
  37. use OCP\IUser;
  38. use OCP\IUserManager;
  39. use OCP\IUserSession;
  40. use PHPUnit\Framework\MockObject\MockObject;
  41. use Test\TestCase;
  42. /**
  43. * Class UserHooksTest
  44. *
  45. * @group DB
  46. * @package OCA\Encryption\Tests\Hooks
  47. */
  48. class UserHooksTest extends TestCase {
  49. /**
  50. * @var \PHPUnit\Framework\MockObject\MockObject
  51. */
  52. private $utilMock;
  53. /**
  54. * @var \PHPUnit\Framework\MockObject\MockObject
  55. */
  56. private $recoveryMock;
  57. /**
  58. * @var \PHPUnit\Framework\MockObject\MockObject
  59. */
  60. private $sessionMock;
  61. /**
  62. * @var \PHPUnit\Framework\MockObject\MockObject
  63. */
  64. private $keyManagerMock;
  65. /**
  66. * @var \PHPUnit\Framework\MockObject\MockObject
  67. */
  68. private $userManagerMock;
  69. /**
  70. * @var \PHPUnit\Framework\MockObject\MockObject
  71. */
  72. private $userSetupMock;
  73. /**
  74. * @var \PHPUnit\Framework\MockObject\MockObject
  75. */
  76. private $userSessionMock;
  77. /**
  78. * @var MockObject|IUser
  79. */
  80. private $user;
  81. /**
  82. * @var \PHPUnit\Framework\MockObject\MockObject
  83. */
  84. private $cryptMock;
  85. /**
  86. * @var \PHPUnit\Framework\MockObject\MockObject
  87. */
  88. private $loggerMock;
  89. /**
  90. * @var UserHooks
  91. */
  92. private $instance;
  93. private $params = ['uid' => 'testUser', 'password' => 'password'];
  94. public function testLogin() {
  95. $this->userSetupMock->expects($this->once())
  96. ->method('setupUser')
  97. ->willReturnOnConsecutiveCalls(true, false);
  98. $this->keyManagerMock->expects($this->once())
  99. ->method('init')
  100. ->with('testUser', 'password');
  101. $this->assertNull($this->instance->login($this->params));
  102. }
  103. public function testLogout() {
  104. $this->sessionMock->expects($this->once())
  105. ->method('clear');
  106. $this->instance->logout();
  107. $this->addToAssertionCount(1);
  108. }
  109. public function testPostCreateUser() {
  110. $this->userSetupMock->expects($this->once())
  111. ->method('setupUser');
  112. $this->instance->postCreateUser($this->params);
  113. $this->addToAssertionCount(1);
  114. }
  115. public function testPostDeleteUser() {
  116. $this->keyManagerMock->expects($this->once())
  117. ->method('deletePublicKey')
  118. ->with('testUser');
  119. $this->instance->postDeleteUser($this->params);
  120. $this->addToAssertionCount(1);
  121. }
  122. public function testPrePasswordReset() {
  123. $params = ['uid' => 'user1'];
  124. $expected = ['user1' => true];
  125. $this->instance->prePasswordReset($params);
  126. $passwordResetUsers = $this->invokePrivate($this->instance, 'passwordResetUsers');
  127. $this->assertSame($expected, $passwordResetUsers);
  128. }
  129. public function testPostPasswordReset() {
  130. $params = ['uid' => 'user1', 'password' => 'password'];
  131. $this->invokePrivate($this->instance, 'passwordResetUsers', [['user1' => true]]);
  132. $this->keyManagerMock->expects($this->once())->method('backupUserKeys')
  133. ->with('passwordReset', 'user1');
  134. $this->keyManagerMock->expects($this->once())->method('deleteUserKeys')
  135. ->with('user1');
  136. $this->userSetupMock->expects($this->once())->method('setupUser')
  137. ->with('user1', 'password');
  138. $this->instance->postPasswordReset($params);
  139. $passwordResetUsers = $this->invokePrivate($this->instance, 'passwordResetUsers');
  140. $this->assertEmpty($passwordResetUsers);
  141. }
  142. /**
  143. * @dataProvider dataTestPreSetPassphrase
  144. */
  145. public function testPreSetPassphrase($canChange) {
  146. /** @var UserHooks | \PHPUnit\Framework\MockObject\MockObject $instance */
  147. $instance = $this->getMockBuilder(UserHooks::class)
  148. ->setConstructorArgs(
  149. [
  150. $this->keyManagerMock,
  151. $this->userManagerMock,
  152. $this->loggerMock,
  153. $this->userSetupMock,
  154. $this->userSessionMock,
  155. $this->utilMock,
  156. $this->sessionMock,
  157. $this->cryptMock,
  158. $this->recoveryMock
  159. ]
  160. )
  161. ->setMethods(['setPassphrase'])
  162. ->getMock();
  163. $userMock = $this->createMock(IUser::class);
  164. $this->userManagerMock->expects($this->once())
  165. ->method('get')
  166. ->with($this->params['uid'])
  167. ->willReturn($userMock);
  168. $userMock->expects($this->once())
  169. ->method('canChangePassword')
  170. ->willReturn($canChange);
  171. if ($canChange) {
  172. // in this case the password will be changed in the post hook
  173. $instance->expects($this->never())->method('setPassphrase');
  174. } else {
  175. // if user can't change the password we update the encryption
  176. // key password already in the pre hook
  177. $instance->expects($this->once())
  178. ->method('setPassphrase')
  179. ->with($this->params);
  180. }
  181. $instance->preSetPassphrase($this->params);
  182. }
  183. public function dataTestPreSetPassphrase() {
  184. return [
  185. [true],
  186. [false]
  187. ];
  188. }
  189. public function XtestSetPassphrase() {
  190. $this->sessionMock->expects($this->once())
  191. ->method('getPrivateKey')
  192. ->willReturn(true);
  193. $this->cryptMock->expects($this->exactly(4))
  194. ->method('encryptPrivateKey')
  195. ->willReturn(true);
  196. $this->cryptMock->expects($this->any())
  197. ->method('generateHeader')
  198. ->willReturn(Crypt::HEADER_START . ':Cipher:test:' . Crypt::HEADER_END);
  199. $this->keyManagerMock->expects($this->exactly(4))
  200. ->method('setPrivateKey')
  201. ->willReturnCallback(function ($user, $key) {
  202. $header = substr($key, 0, strlen(Crypt::HEADER_START));
  203. $this->assertSame(
  204. Crypt::HEADER_START,
  205. $header, 'every encrypted file should start with a header');
  206. });
  207. $this->assertNull($this->instance->setPassphrase($this->params));
  208. $this->params['recoveryPassword'] = 'password';
  209. $this->recoveryMock->expects($this->exactly(3))
  210. ->method('isRecoveryEnabledForUser')
  211. ->with('testUser1')
  212. ->willReturnOnConsecutiveCalls(true, false);
  213. $this->instance = $this->getMockBuilder(UserHooks::class)
  214. ->setConstructorArgs(
  215. [
  216. $this->keyManagerMock,
  217. $this->userManagerMock,
  218. $this->loggerMock,
  219. $this->userSetupMock,
  220. $this->userSessionMock,
  221. $this->utilMock,
  222. $this->sessionMock,
  223. $this->cryptMock,
  224. $this->recoveryMock
  225. ]
  226. )->setMethods(['initMountPoints'])->getMock();
  227. $this->instance->expects($this->exactly(3))->method('initMountPoints');
  228. $this->params['uid'] = 'testUser1';
  229. // Test first if statement
  230. $this->assertNull($this->instance->setPassphrase($this->params));
  231. // Test Second if conditional
  232. $this->keyManagerMock->expects($this->exactly(2))
  233. ->method('userHasKeys')
  234. ->with('testUser1')
  235. ->willReturn(true);
  236. $this->assertNull($this->instance->setPassphrase($this->params));
  237. // Test third and final if condition
  238. $this->utilMock->expects($this->once())
  239. ->method('userHasFiles')
  240. ->with('testUser1')
  241. ->willReturn(false);
  242. $this->cryptMock->expects($this->once())
  243. ->method('createKeyPair');
  244. $this->keyManagerMock->expects($this->once())
  245. ->method('setPrivateKey');
  246. $this->recoveryMock->expects($this->once())
  247. ->method('recoverUsersFiles')
  248. ->with('password', 'testUser1');
  249. $this->assertNull($this->instance->setPassphrase($this->params));
  250. }
  251. public function testSetPassphraseResetUserMode() {
  252. $params = ['uid' => 'user1', 'password' => 'password'];
  253. $this->invokePrivate($this->instance, 'passwordResetUsers', [[$params['uid'] => true]]);
  254. $this->sessionMock->expects($this->never())->method('getPrivateKey');
  255. $this->keyManagerMock->expects($this->never())->method('setPrivateKey');
  256. $this->assertTrue($this->instance->setPassphrase($params));
  257. $this->invokePrivate($this->instance, 'passwordResetUsers', [[]]);
  258. }
  259. public function XtestSetPasswordNoUser() {
  260. $userSessionMock = $this->getMockBuilder(IUserSession::class)
  261. ->disableOriginalConstructor()
  262. ->getMock();
  263. $userSessionMock->expects($this->any())->method('getUser')->willReturn(null);
  264. $this->recoveryMock->expects($this->once())
  265. ->method('isRecoveryEnabledForUser')
  266. ->with('testUser')
  267. ->willReturn(false);
  268. $userHooks = $this->getMockBuilder(UserHooks::class)
  269. ->setConstructorArgs(
  270. [
  271. $this->keyManagerMock,
  272. $this->userManagerMock,
  273. $this->loggerMock,
  274. $this->userSetupMock,
  275. $userSessionMock,
  276. $this->utilMock,
  277. $this->sessionMock,
  278. $this->cryptMock,
  279. $this->recoveryMock
  280. ]
  281. )->setMethods(['initMountPoints'])->getMock();
  282. /** @var \OCA\Encryption\Hooks\UserHooks $userHooks */
  283. $this->assertNull($userHooks->setPassphrase($this->params));
  284. }
  285. protected function setUp(): void {
  286. parent::setUp();
  287. $this->loggerMock = $this->createMock(ILogger::class);
  288. $this->keyManagerMock = $this->getMockBuilder(KeyManager::class)
  289. ->disableOriginalConstructor()
  290. ->getMock();
  291. $this->userManagerMock = $this->getMockBuilder(IUserManager::class)
  292. ->disableOriginalConstructor()
  293. ->getMock();
  294. $this->userSetupMock = $this->getMockBuilder(Setup::class)
  295. ->disableOriginalConstructor()
  296. ->getMock();
  297. $this->user = $this->createMock(IUser::class);
  298. $this->user->expects($this->any())
  299. ->method('getUID')
  300. ->willReturn('testUser');
  301. $this->userSessionMock = $this->createMock(IUserSession::class);
  302. $this->userSessionMock->expects($this->any())
  303. ->method('getUser')
  304. ->willReturn($this->user);
  305. $utilMock = $this->getMockBuilder(Util::class)
  306. ->disableOriginalConstructor()
  307. ->getMock();
  308. $sessionMock = $this->getMockBuilder(Session::class)
  309. ->disableOriginalConstructor()
  310. ->getMock();
  311. $this->cryptMock = $this->getMockBuilder(Crypt::class)
  312. ->disableOriginalConstructor()
  313. ->getMock();
  314. $recoveryMock = $this->getMockBuilder(Recovery::class)
  315. ->disableOriginalConstructor()
  316. ->getMock();
  317. $this->sessionMock = $sessionMock;
  318. $this->recoveryMock = $recoveryMock;
  319. $this->utilMock = $utilMock;
  320. $this->utilMock->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
  321. $this->instance = $this->getMockBuilder(UserHooks::class)
  322. ->setConstructorArgs(
  323. [
  324. $this->keyManagerMock,
  325. $this->userManagerMock,
  326. $this->loggerMock,
  327. $this->userSetupMock,
  328. $this->userSessionMock,
  329. $this->utilMock,
  330. $this->sessionMock,
  331. $this->cryptMock,
  332. $this->recoveryMock
  333. ]
  334. )->setMethods(['setupFS'])->getMock();
  335. }
  336. }