1
0

DecryptAllTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Encryption\Tests\Crypto;
  26. use OCA\Encryption\Crypto\Crypt;
  27. use OCA\Encryption\Crypto\DecryptAll;
  28. use OCA\Encryption\KeyManager;
  29. use OCA\Encryption\Session;
  30. use OCA\Encryption\Util;
  31. use Symfony\Component\Console\Helper\QuestionHelper;
  32. use Test\TestCase;
  33. class DecryptAllTest extends TestCase {
  34. /** @var DecryptAll */
  35. protected $instance;
  36. /** @var Util | \PHPUnit\Framework\MockObject\MockObject */
  37. protected $util;
  38. /** @var KeyManager | \PHPUnit\Framework\MockObject\MockObject */
  39. protected $keyManager;
  40. /** @var Crypt | \PHPUnit\Framework\MockObject\MockObject */
  41. protected $crypt;
  42. /** @var Session | \PHPUnit\Framework\MockObject\MockObject */
  43. protected $session;
  44. /** @var QuestionHelper | \PHPUnit\Framework\MockObject\MockObject */
  45. protected $questionHelper;
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $this->util = $this->getMockBuilder(Util::class)
  49. ->disableOriginalConstructor()->getMock();
  50. $this->keyManager = $this->getMockBuilder(KeyManager::class)
  51. ->disableOriginalConstructor()->getMock();
  52. $this->crypt = $this->getMockBuilder(Crypt::class)
  53. ->disableOriginalConstructor()->getMock();
  54. $this->session = $this->getMockBuilder(Session::class)
  55. ->disableOriginalConstructor()->getMock();
  56. $this->questionHelper = $this->getMockBuilder(QuestionHelper::class)
  57. ->disableOriginalConstructor()->getMock();
  58. $this->instance = new DecryptAll(
  59. $this->util,
  60. $this->keyManager,
  61. $this->crypt,
  62. $this->session,
  63. $this->questionHelper
  64. );
  65. }
  66. public function testUpdateSession() {
  67. $this->session->expects($this->once())->method('prepareDecryptAll')
  68. ->with('user1', 'key1');
  69. $this->invokePrivate($this->instance, 'updateSession', ['user1', 'key1']);
  70. }
  71. /**
  72. * @dataProvider dataTestGetPrivateKey
  73. *
  74. * @param string $user
  75. * @param string $recoveryKeyId
  76. */
  77. public function testGetPrivateKey($user, $recoveryKeyId, $masterKeyId) {
  78. $password = 'passwd';
  79. $recoveryKey = 'recoveryKey';
  80. $userKey = 'userKey';
  81. $unencryptedKey = 'unencryptedKey';
  82. $this->keyManager->expects($this->any())->method('getRecoveryKeyId')
  83. ->willReturn($recoveryKeyId);
  84. if ($user === $recoveryKeyId) {
  85. $this->keyManager->expects($this->once())->method('getSystemPrivateKey')
  86. ->with($recoveryKeyId)->willReturn($recoveryKey);
  87. $this->keyManager->expects($this->never())->method('getPrivateKey');
  88. $this->crypt->expects($this->once())->method('decryptPrivateKey')
  89. ->with($recoveryKey, $password)->willReturn($unencryptedKey);
  90. } elseif ($user === $masterKeyId) {
  91. $this->keyManager->expects($this->once())->method('getSystemPrivateKey')
  92. ->with($masterKeyId)->willReturn($masterKey);
  93. $this->keyManager->expects($this->never())->method('getPrivateKey');
  94. $this->crypt->expects($this->once())->method('decryptPrivateKey')
  95. ->with($masterKey, $password, $masterKeyId)->willReturn($unencryptedKey);
  96. } else {
  97. $this->keyManager->expects($this->never())->method('getSystemPrivateKey');
  98. $this->keyManager->expects($this->once())->method('getPrivateKey')
  99. ->with($user)->willReturn($userKey);
  100. $this->crypt->expects($this->once())->method('decryptPrivateKey')
  101. ->with($userKey, $password, $user)->willReturn($unencryptedKey);
  102. }
  103. $this->assertSame($unencryptedKey,
  104. $this->invokePrivate($this->instance, 'getPrivateKey', [$user, $password])
  105. );
  106. }
  107. public function dataTestGetPrivateKey() {
  108. return [
  109. ['user1', 'recoveryKey', 'masterKeyId'],
  110. ['recoveryKeyId', 'recoveryKeyId', 'masterKeyId'],
  111. ['masterKeyId', 'masterKeyId', 'masterKeyId']
  112. ];
  113. }
  114. }