1
0

DecryptAllTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Encryption\Tests\Crypto;
  8. use OCA\Encryption\Crypto\Crypt;
  9. use OCA\Encryption\Crypto\DecryptAll;
  10. use OCA\Encryption\KeyManager;
  11. use OCA\Encryption\Session;
  12. use OCA\Encryption\Util;
  13. use Symfony\Component\Console\Helper\QuestionHelper;
  14. use Test\TestCase;
  15. class DecryptAllTest extends TestCase {
  16. /** @var DecryptAll */
  17. protected $instance;
  18. /** @var Util | \PHPUnit\Framework\MockObject\MockObject */
  19. protected $util;
  20. /** @var KeyManager | \PHPUnit\Framework\MockObject\MockObject */
  21. protected $keyManager;
  22. /** @var Crypt | \PHPUnit\Framework\MockObject\MockObject */
  23. protected $crypt;
  24. /** @var Session | \PHPUnit\Framework\MockObject\MockObject */
  25. protected $session;
  26. /** @var QuestionHelper | \PHPUnit\Framework\MockObject\MockObject */
  27. protected $questionHelper;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. $this->util = $this->getMockBuilder(Util::class)
  31. ->disableOriginalConstructor()->getMock();
  32. $this->keyManager = $this->getMockBuilder(KeyManager::class)
  33. ->disableOriginalConstructor()->getMock();
  34. $this->crypt = $this->getMockBuilder(Crypt::class)
  35. ->disableOriginalConstructor()->getMock();
  36. $this->session = $this->getMockBuilder(Session::class)
  37. ->disableOriginalConstructor()->getMock();
  38. $this->questionHelper = $this->getMockBuilder(QuestionHelper::class)
  39. ->disableOriginalConstructor()->getMock();
  40. $this->instance = new DecryptAll(
  41. $this->util,
  42. $this->keyManager,
  43. $this->crypt,
  44. $this->session,
  45. $this->questionHelper
  46. );
  47. }
  48. public function testUpdateSession(): void {
  49. $this->session->expects($this->once())->method('prepareDecryptAll')
  50. ->with('user1', 'key1');
  51. $this->invokePrivate($this->instance, 'updateSession', ['user1', 'key1']);
  52. }
  53. /**
  54. * @dataProvider dataTestGetPrivateKey
  55. *
  56. * @param string $user
  57. * @param string $recoveryKeyId
  58. */
  59. public function testGetPrivateKey($user, $recoveryKeyId, $masterKeyId): void {
  60. $password = 'passwd';
  61. $recoveryKey = 'recoveryKey';
  62. $userKey = 'userKey';
  63. $unencryptedKey = 'unencryptedKey';
  64. $this->keyManager->expects($this->any())->method('getRecoveryKeyId')
  65. ->willReturn($recoveryKeyId);
  66. if ($user === $recoveryKeyId) {
  67. $this->keyManager->expects($this->once())->method('getSystemPrivateKey')
  68. ->with($recoveryKeyId)->willReturn($recoveryKey);
  69. $this->keyManager->expects($this->never())->method('getPrivateKey');
  70. $this->crypt->expects($this->once())->method('decryptPrivateKey')
  71. ->with($recoveryKey, $password)->willReturn($unencryptedKey);
  72. } elseif ($user === $masterKeyId) {
  73. $this->keyManager->expects($this->once())->method('getSystemPrivateKey')
  74. ->with($masterKeyId)->willReturn($masterKey);
  75. $this->keyManager->expects($this->never())->method('getPrivateKey');
  76. $this->crypt->expects($this->once())->method('decryptPrivateKey')
  77. ->with($masterKey, $password, $masterKeyId)->willReturn($unencryptedKey);
  78. } else {
  79. $this->keyManager->expects($this->never())->method('getSystemPrivateKey');
  80. $this->keyManager->expects($this->once())->method('getPrivateKey')
  81. ->with($user)->willReturn($userKey);
  82. $this->crypt->expects($this->once())->method('decryptPrivateKey')
  83. ->with($userKey, $password, $user)->willReturn($unencryptedKey);
  84. }
  85. $this->assertSame($unencryptedKey,
  86. $this->invokePrivate($this->instance, 'getPrivateKey', [$user, $password])
  87. );
  88. }
  89. public function dataTestGetPrivateKey() {
  90. return [
  91. ['user1', 'recoveryKey', 'masterKeyId'],
  92. ['recoveryKeyId', 'recoveryKeyId', 'masterKeyId'],
  93. ['masterKeyId', 'masterKeyId', 'masterKeyId']
  94. ];
  95. }
  96. }