ManagerTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Test\Security\IdentityProof;
  22. use OC\Security\IdentityProof\Key;
  23. use OC\Security\IdentityProof\Manager;
  24. use OCP\Files\IAppData;
  25. use OCP\Files\SimpleFS\ISimpleFile;
  26. use OCP\Files\SimpleFS\ISimpleFolder;
  27. use OCP\IUser;
  28. use OCP\Security\ICrypto;
  29. use Test\TestCase;
  30. class ManagerTest extends TestCase {
  31. /** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
  32. private $appData;
  33. /** @var ICrypto|\PHPUnit_Framework_MockObject_MockObject */
  34. private $crypto;
  35. /** @var Manager|\PHPUnit_Framework_MockObject_MockObject */
  36. private $manager;
  37. public function setUp() {
  38. parent::setUp();
  39. $this->appData = $this->createMock(IAppData::class);
  40. $this->crypto = $this->createMock(ICrypto::class);
  41. $this->manager = $this->getMockBuilder(Manager::class)
  42. ->setConstructorArgs([
  43. $this->appData,
  44. $this->crypto
  45. ])
  46. ->setMethods(['generateKeyPair'])
  47. ->getMock();
  48. }
  49. public function testGetKeyWithExistingKey() {
  50. $user = $this->createMock(IUser::class);
  51. $user
  52. ->expects($this->once())
  53. ->method('getUID')
  54. ->willReturn('MyUid');
  55. $folder = $this->createMock(ISimpleFolder::class);
  56. $privateFile = $this->createMock(ISimpleFile::class);
  57. $privateFile
  58. ->expects($this->once())
  59. ->method('getContent')
  60. ->willReturn('EncryptedPrivateKey');
  61. $publicFile = $this->createMock(ISimpleFile::class);
  62. $publicFile
  63. ->expects($this->once())
  64. ->method('getContent')
  65. ->willReturn('MyPublicKey');
  66. $this->crypto
  67. ->expects($this->once())
  68. ->method('decrypt')
  69. ->with('EncryptedPrivateKey')
  70. ->willReturn('MyPrivateKey');
  71. $folder
  72. ->expects($this->at(0))
  73. ->method('getFile')
  74. ->with('private')
  75. ->willReturn($privateFile);
  76. $folder
  77. ->expects($this->at(1))
  78. ->method('getFile')
  79. ->with('public')
  80. ->willReturn($publicFile);
  81. $this->appData
  82. ->expects($this->once())
  83. ->method('getFolder')
  84. ->with('MyUid')
  85. ->willReturn($folder);
  86. $expected = new Key('MyPublicKey', 'MyPrivateKey');
  87. $this->assertEquals($expected, $this->manager->getKey($user));
  88. }
  89. public function testGetKeyWithNotExistingKey() {
  90. $user = $this->createMock(IUser::class);
  91. $user
  92. ->expects($this->exactly(3))
  93. ->method('getUID')
  94. ->willReturn('MyUid');
  95. $this->appData
  96. ->expects($this->at(0))
  97. ->method('getFolder')
  98. ->with('MyUid')
  99. ->willThrowException(new \Exception());
  100. $this->manager
  101. ->expects($this->once())
  102. ->method('generateKeyPair')
  103. ->willReturn(['MyNewPublicKey', 'MyNewPrivateKey']);
  104. $this->appData
  105. ->expects($this->at(1))
  106. ->method('newFolder')
  107. ->with('MyUid');
  108. $folder = $this->createMock(ISimpleFolder::class);
  109. $this->crypto
  110. ->expects($this->once())
  111. ->method('encrypt')
  112. ->with('MyNewPrivateKey')
  113. ->willReturn('MyNewEncryptedPrivateKey');
  114. $privateFile = $this->createMock(ISimpleFile::class);
  115. $privateFile
  116. ->expects($this->once())
  117. ->method('putContent')
  118. ->with('MyNewEncryptedPrivateKey');
  119. $publicFile = $this->createMock(ISimpleFile::class);
  120. $publicFile
  121. ->expects($this->once())
  122. ->method('putContent')
  123. ->with('MyNewPublicKey');
  124. $folder
  125. ->expects($this->at(0))
  126. ->method('newFile')
  127. ->with('private')
  128. ->willReturn($privateFile);
  129. $folder
  130. ->expects($this->at(1))
  131. ->method('newFile')
  132. ->with('public')
  133. ->willReturn($publicFile);
  134. $this->appData
  135. ->expects($this->at(2))
  136. ->method('getFolder')
  137. ->with('MyUid')
  138. ->willReturn($folder);
  139. $expected = new Key('MyNewPublicKey', 'MyNewPrivateKey');
  140. $this->assertEquals($expected, $this->manager->getKey($user));
  141. }
  142. public function testGenerateKeyPair() {
  143. $manager = new Manager(
  144. $this->appData,
  145. $this->crypto
  146. );
  147. $data = 'MyTestData';
  148. list($resultPublicKey, $resultPrivateKey) = $this->invokePrivate($manager, 'generateKeyPair');
  149. openssl_sign($data, $signature, $resultPrivateKey);
  150. $details = openssl_pkey_get_details(openssl_pkey_get_public($resultPublicKey));
  151. $this->assertSame(1, openssl_verify($data, $signature, $resultPublicKey));
  152. $this->assertSame(2048, $details['bits']);
  153. }
  154. }