SignCoreTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\Command\Integrity;
  8. use OC\Core\Command\Integrity\SignCore;
  9. use OC\IntegrityCheck\Checker;
  10. use OC\IntegrityCheck\Helpers\FileAccessHelper;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Test\TestCase;
  14. class SignCoreTest extends TestCase {
  15. /** @var Checker|\PHPUnit\Framework\MockObject\MockObject */
  16. private $checker;
  17. /** @var FileAccessHelper|\PHPUnit\Framework\MockObject\MockObject */
  18. private $fileAccessHelper;
  19. /** @var SignCore */
  20. private $signCore;
  21. protected function setUp(): void {
  22. parent::setUp();
  23. $this->checker = $this->createMock(Checker::class);
  24. $this->fileAccessHelper = $this->createMock(FileAccessHelper::class);
  25. $this->signCore = new SignCore(
  26. $this->checker,
  27. $this->fileAccessHelper
  28. );
  29. }
  30. public function testExecuteWithMissingPrivateKey(): void {
  31. $inputInterface = $this->createMock(InputInterface::class);
  32. $outputInterface = $this->createMock(OutputInterface::class);
  33. $inputInterface
  34. ->expects($this->exactly(3))
  35. ->method('getOption')
  36. ->withConsecutive(
  37. ['privateKey'],
  38. ['certificate'],
  39. ['path'],
  40. )->willReturnOnConsecutiveCalls(
  41. null,
  42. 'certificate',
  43. 'certificate',
  44. );
  45. $outputInterface
  46. ->expects($this->any())
  47. ->method('writeln')
  48. ->withConsecutive(
  49. ['--privateKey, --certificate and --path are required.']
  50. );
  51. $this->assertSame(1, self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]));
  52. }
  53. public function testExecuteWithMissingCertificate(): void {
  54. $inputInterface = $this->createMock(InputInterface::class);
  55. $outputInterface = $this->createMock(OutputInterface::class);
  56. $inputInterface
  57. ->expects($this->exactly(3))
  58. ->method('getOption')
  59. ->withConsecutive(
  60. ['privateKey'],
  61. ['certificate'],
  62. ['path'],
  63. )->willReturnOnConsecutiveCalls(
  64. 'privateKey',
  65. null,
  66. 'certificate',
  67. );
  68. $outputInterface
  69. ->expects($this->any())
  70. ->method('writeln')
  71. ->withConsecutive(
  72. ['--privateKey, --certificate and --path are required.']
  73. );
  74. $this->assertSame(1, self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]));
  75. }
  76. public function testExecuteWithNotExistingPrivateKey(): void {
  77. $inputInterface = $this->createMock(InputInterface::class);
  78. $outputInterface = $this->createMock(OutputInterface::class);
  79. $inputInterface
  80. ->expects($this->exactly(3))
  81. ->method('getOption')
  82. ->withConsecutive(
  83. ['privateKey'],
  84. ['certificate'],
  85. ['path'],
  86. )->willReturnOnConsecutiveCalls(
  87. 'privateKey',
  88. 'certificate',
  89. 'certificate',
  90. );
  91. $this->fileAccessHelper
  92. ->expects($this->any())
  93. ->method('file_get_contents')
  94. ->withConsecutive(
  95. ['privateKey'],
  96. )
  97. ->willReturnOnConsecutiveCalls(
  98. false,
  99. );
  100. $outputInterface
  101. ->expects($this->any())
  102. ->method('writeln')
  103. ->withConsecutive(
  104. ['Private key "privateKey" does not exists.']
  105. );
  106. $this->assertSame(1, self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]));
  107. }
  108. public function testExecuteWithNotExistingCertificate(): void {
  109. $inputInterface = $this->createMock(InputInterface::class);
  110. $outputInterface = $this->createMock(OutputInterface::class);
  111. $inputInterface
  112. ->expects($this->exactly(3))
  113. ->method('getOption')
  114. ->withConsecutive(
  115. ['privateKey'],
  116. ['certificate'],
  117. ['path'],
  118. )->willReturnOnConsecutiveCalls(
  119. 'privateKey',
  120. 'certificate',
  121. 'certificate',
  122. );
  123. $this->fileAccessHelper
  124. ->expects($this->any())
  125. ->method('file_get_contents')
  126. ->withConsecutive(
  127. ['privateKey'],
  128. ['certificate'],
  129. )
  130. ->willReturnOnConsecutiveCalls(
  131. file_get_contents(\OC::$SERVERROOT . '/tests/data/integritycheck/core.key'),
  132. false,
  133. );
  134. $outputInterface
  135. ->expects($this->any())
  136. ->method('writeln')
  137. ->withConsecutive(
  138. ['Certificate "certificate" does not exists.']
  139. );
  140. $this->assertSame(1, self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]));
  141. }
  142. public function testExecuteWithException(): void {
  143. $inputInterface = $this->createMock(InputInterface::class);
  144. $outputInterface = $this->createMock(OutputInterface::class);
  145. $inputInterface
  146. ->expects($this->exactly(3))
  147. ->method('getOption')
  148. ->withConsecutive(
  149. ['privateKey'],
  150. ['certificate'],
  151. ['path'],
  152. )->willReturnOnConsecutiveCalls(
  153. 'privateKey',
  154. 'certificate',
  155. 'certificate',
  156. );
  157. $this->fileAccessHelper
  158. ->expects($this->any())
  159. ->method('file_get_contents')
  160. ->withConsecutive(
  161. ['privateKey'],
  162. ['certificate'],
  163. )
  164. ->willReturnOnConsecutiveCalls(
  165. file_get_contents(\OC::$SERVERROOT . '/tests/data/integritycheck/core.key'),
  166. file_get_contents(\OC::$SERVERROOT . '/tests/data/integritycheck/core.crt'),
  167. );
  168. $this->checker
  169. ->expects($this->once())
  170. ->method('writeCoreSignature')
  171. ->willThrowException(new \Exception('My exception message'));
  172. $outputInterface
  173. ->expects($this->any())
  174. ->method('writeln')
  175. ->withConsecutive(
  176. ['Error: My exception message']
  177. );
  178. $this->assertEquals(1, self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]));
  179. }
  180. public function testExecute(): void {
  181. $inputInterface = $this->createMock(InputInterface::class);
  182. $outputInterface = $this->createMock(OutputInterface::class);
  183. $inputInterface
  184. ->expects($this->exactly(3))
  185. ->method('getOption')
  186. ->withConsecutive(
  187. ['privateKey'],
  188. ['certificate'],
  189. ['path'],
  190. )->willReturnOnConsecutiveCalls(
  191. 'privateKey',
  192. 'certificate',
  193. 'certificate',
  194. );
  195. $this->fileAccessHelper
  196. ->expects($this->any())
  197. ->method('file_get_contents')
  198. ->withConsecutive(
  199. ['privateKey'],
  200. ['certificate'],
  201. )
  202. ->willReturnOnConsecutiveCalls(
  203. file_get_contents(\OC::$SERVERROOT . '/tests/data/integritycheck/core.key'),
  204. file_get_contents(\OC::$SERVERROOT . '/tests/data/integritycheck/core.crt'),
  205. );
  206. $this->checker
  207. ->expects($this->once())
  208. ->method('writeCoreSignature');
  209. $outputInterface
  210. ->expects($this->any())
  211. ->method('writeln')
  212. ->withConsecutive(
  213. ['Successfully signed "core"']
  214. );
  215. $this->assertEquals(0, self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]));
  216. }
  217. }