DecryptAllTest.php 4.4 KB

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