SignAppTest.php 7.8 KB

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