PassphraseServiceTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Encryption\Tests;
  8. use OCA\Encryption\Crypto\Crypt;
  9. use OCA\Encryption\KeyManager;
  10. use OCA\Encryption\Recovery;
  11. use OCA\Encryption\Services\PassphraseService;
  12. use OCA\Encryption\Session;
  13. use OCA\Encryption\Util;
  14. use OCP\IUser;
  15. use OCP\IUserManager;
  16. use OCP\IUserSession;
  17. use PHPUnit\Framework\MockObject\MockObject;
  18. use Psr\Log\LoggerInterface;
  19. use Test\TestCase;
  20. /**
  21. * @group DB
  22. */
  23. class PassphraseServiceTest extends TestCase {
  24. protected Util&MockObject $util;
  25. protected Crypt&MockObject $crypt;
  26. protected Session&MockObject $session;
  27. protected Recovery&MockObject $recovery;
  28. protected KeyManager&MockObject $keyManager;
  29. protected IUserManager&MockObject $userManager;
  30. protected IUserSession&MockObject $userSession;
  31. protected PassphraseService $instance;
  32. public function setUp(): void {
  33. parent::setUp();
  34. $this->util = $this->createMock(Util::class);
  35. $this->crypt = $this->createMock(Crypt::class);
  36. $this->session = $this->createMock(Session::class);
  37. $this->recovery = $this->createMock(Recovery::class);
  38. $this->keyManager = $this->createMock(KeyManager::class);
  39. $this->userManager = $this->createMock(IUserManager::class);
  40. $this->userSession = $this->createMock(IUserSession::class);
  41. $this->instance = new PassphraseService(
  42. $this->util,
  43. $this->crypt,
  44. $this->session,
  45. $this->recovery,
  46. $this->keyManager,
  47. $this->createMock(LoggerInterface::class),
  48. $this->userManager,
  49. $this->userSession,
  50. );
  51. }
  52. public function testSetProcessingReset(): void {
  53. $this->instance->setProcessingReset('userId');
  54. $this->assertEquals(['userId' => true], $this->invokePrivate($this->instance, 'passwordResetUsers'));
  55. }
  56. public function testUnsetProcessingReset(): void {
  57. $this->instance->setProcessingReset('userId');
  58. $this->assertEquals(['userId' => true], $this->invokePrivate($this->instance, 'passwordResetUsers'));
  59. $this->instance->setProcessingReset('userId', false);
  60. $this->assertEquals([], $this->invokePrivate($this->instance, 'passwordResetUsers'));
  61. }
  62. /**
  63. * Check that the passphrase setting skips if a reset is processed
  64. */
  65. public function testSetPassphraseResetUserMode(): void {
  66. $this->session->expects(self::never())
  67. ->method('getPrivateKey');
  68. $this->keyManager->expects(self::never())
  69. ->method('setPrivateKey');
  70. $this->instance->setProcessingReset('userId');
  71. $this->assertTrue($this->instance->setPassphraseForUser('userId', 'password'));
  72. }
  73. public function testSetPassphrase_currentUser() {
  74. $instance = $this->getMockBuilder(PassphraseService::class)
  75. ->onlyMethods(['initMountPoints'])
  76. ->setConstructorArgs([
  77. $this->util,
  78. $this->crypt,
  79. $this->session,
  80. $this->recovery,
  81. $this->keyManager,
  82. $this->createMock(LoggerInterface::class),
  83. $this->userManager,
  84. $this->userSession,
  85. ])
  86. ->getMock();
  87. $user = $this->createMock(IUser::class);
  88. $user->method('getUID')->willReturn('testUser');
  89. $this->userSession->expects(self::atLeastOnce())
  90. ->method('getUser')
  91. ->willReturn($user);
  92. $this->userManager->expects(self::atLeastOnce())
  93. ->method('get')
  94. ->with('testUser')
  95. ->willReturn($user);
  96. $this->session->expects(self::any())
  97. ->method('getPrivateKey')
  98. ->willReturn('private-key');
  99. $this->crypt->expects(self::any())
  100. ->method('encryptPrivateKey')
  101. ->with('private-key')
  102. ->willReturn('encrypted-key');
  103. $this->crypt->expects(self::any())
  104. ->method('generateHeader')
  105. ->willReturn('crypt-header: ');
  106. $this->keyManager->expects(self::atLeastOnce())
  107. ->method('setPrivateKey')
  108. ->with('testUser', 'crypt-header: encrypted-key');
  109. $this->assertTrue($instance->setPassphraseForUser('testUser', 'password'));
  110. }
  111. public function testSetPassphrase_currentUserFails() {
  112. $instance = $this->getMockBuilder(PassphraseService::class)
  113. ->onlyMethods(['initMountPoints'])
  114. ->setConstructorArgs([
  115. $this->util,
  116. $this->crypt,
  117. $this->session,
  118. $this->recovery,
  119. $this->keyManager,
  120. $this->createMock(LoggerInterface::class),
  121. $this->userManager,
  122. $this->userSession,
  123. ])
  124. ->getMock();
  125. $user = $this->createMock(IUser::class);
  126. $user->method('getUID')->willReturn('testUser');
  127. $this->userManager->expects(self::atLeastOnce())
  128. ->method('get')
  129. ->with('testUser')
  130. ->willReturn($user);
  131. $this->userSession->expects(self::atLeastOnce())
  132. ->method('getUser')
  133. ->willReturn($user);
  134. $this->session->expects(self::any())
  135. ->method('getPrivateKey')
  136. ->willReturn('private-key');
  137. $this->crypt->expects(self::any())
  138. ->method('encryptPrivateKey')
  139. ->with('private-key')
  140. ->willReturn(false);
  141. $this->keyManager->expects(self::never())
  142. ->method('setPrivateKey');
  143. $this->assertFalse($instance->setPassphraseForUser('testUser', 'password'));
  144. }
  145. public function testSetPassphrase_currentUserNotExists() {
  146. $instance = $this->getMockBuilder(PassphraseService::class)
  147. ->onlyMethods(['initMountPoints'])
  148. ->setConstructorArgs([
  149. $this->util,
  150. $this->crypt,
  151. $this->session,
  152. $this->recovery,
  153. $this->keyManager,
  154. $this->createMock(LoggerInterface::class),
  155. $this->userManager,
  156. $this->userSession,
  157. ])
  158. ->getMock();
  159. $user = $this->createMock(IUser::class);
  160. $user->method('getUID')->willReturn('testUser');
  161. $this->userManager->expects(self::atLeastOnce())
  162. ->method('get')
  163. ->with('testUser')
  164. ->willReturn(null);
  165. $this->userSession->expects(self::never())
  166. ->method('getUser');
  167. $this->keyManager->expects(self::never())
  168. ->method('setPrivateKey');
  169. $this->assertFalse($instance->setPassphraseForUser('testUser', 'password'));
  170. }
  171. }