SignAppTest.php 7.2 KB

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