SignCoreTest.php 6.9 KB

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