SignCoreTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. public function setUp() {
  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->at(0))
  49. ->method('getOption')
  50. ->with('privateKey')
  51. ->will($this->returnValue(null));
  52. $inputInterface
  53. ->expects($this->at(1))
  54. ->method('getOption')
  55. ->with('certificate')
  56. ->will($this->returnValue('Certificate'));
  57. $outputInterface
  58. ->expects($this->at(0))
  59. ->method('writeln')
  60. ->with('--privateKey, --certificate and --path are required.');
  61. $this->assertNull(self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]));
  62. }
  63. public function testExecuteWithMissingCertificate() {
  64. $inputInterface = $this->createMock(InputInterface::class);
  65. $outputInterface = $this->createMock(OutputInterface::class);
  66. $inputInterface
  67. ->expects($this->at(0))
  68. ->method('getOption')
  69. ->with('privateKey')
  70. ->will($this->returnValue('privateKey'));
  71. $inputInterface
  72. ->expects($this->at(1))
  73. ->method('getOption')
  74. ->with('certificate')
  75. ->will($this->returnValue(null));
  76. $outputInterface
  77. ->expects($this->at(0))
  78. ->method('writeln')
  79. ->with('--privateKey, --certificate and --path are required.');
  80. $this->assertNull(self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]));
  81. }
  82. public function testExecuteWithNotExistingPrivateKey() {
  83. $inputInterface = $this->createMock(InputInterface::class);
  84. $outputInterface = $this->createMock(OutputInterface::class);
  85. $inputInterface
  86. ->expects($this->at(0))
  87. ->method('getOption')
  88. ->with('privateKey')
  89. ->will($this->returnValue('privateKey'));
  90. $inputInterface
  91. ->expects($this->at(1))
  92. ->method('getOption')
  93. ->with('certificate')
  94. ->will($this->returnValue('certificate'));
  95. $inputInterface
  96. ->expects($this->at(2))
  97. ->method('getOption')
  98. ->with('path')
  99. ->will($this->returnValue('certificate'));
  100. $this->fileAccessHelper
  101. ->expects($this->at(0))
  102. ->method('file_get_contents')
  103. ->with('privateKey')
  104. ->will($this->returnValue(false));
  105. $outputInterface
  106. ->expects($this->at(0))
  107. ->method('writeln')
  108. ->with('Private key "privateKey" does not exists.');
  109. $this->assertNull(self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]));
  110. }
  111. public function testExecuteWithNotExistingCertificate() {
  112. $inputInterface = $this->createMock(InputInterface::class);
  113. $outputInterface = $this->createMock(OutputInterface::class);
  114. $inputInterface
  115. ->expects($this->at(0))
  116. ->method('getOption')
  117. ->with('privateKey')
  118. ->will($this->returnValue('privateKey'));
  119. $inputInterface
  120. ->expects($this->at(1))
  121. ->method('getOption')
  122. ->with('certificate')
  123. ->will($this->returnValue('certificate'));
  124. $inputInterface
  125. ->expects($this->at(2))
  126. ->method('getOption')
  127. ->with('path')
  128. ->will($this->returnValue('certificate'));
  129. $this->fileAccessHelper
  130. ->expects($this->at(0))
  131. ->method('file_get_contents')
  132. ->with('privateKey')
  133. ->will($this->returnValue(\OC::$SERVERROOT . '/tests/data/integritycheck/core.key'));
  134. $this->fileAccessHelper
  135. ->expects($this->at(1))
  136. ->method('file_get_contents')
  137. ->with('certificate')
  138. ->will($this->returnValue(false));
  139. $outputInterface
  140. ->expects($this->at(0))
  141. ->method('writeln')
  142. ->with('Certificate "certificate" does not exists.');
  143. $this->assertNull(self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]));
  144. }
  145. public function testExecuteWithException() {
  146. $inputInterface = $this->createMock(InputInterface::class);
  147. $outputInterface = $this->createMock(OutputInterface::class);
  148. $inputInterface
  149. ->expects($this->at(0))
  150. ->method('getOption')
  151. ->with('privateKey')
  152. ->will($this->returnValue('privateKey'));
  153. $inputInterface
  154. ->expects($this->at(1))
  155. ->method('getOption')
  156. ->with('certificate')
  157. ->will($this->returnValue('certificate'));
  158. $inputInterface
  159. ->expects($this->at(2))
  160. ->method('getOption')
  161. ->with('path')
  162. ->will($this->returnValue('certificate'));
  163. $this->fileAccessHelper
  164. ->expects($this->at(0))
  165. ->method('file_get_contents')
  166. ->with('privateKey')
  167. ->will($this->returnValue(\OC::$SERVERROOT . '/tests/data/integritycheck/core.key'));
  168. $this->fileAccessHelper
  169. ->expects($this->at(1))
  170. ->method('file_get_contents')
  171. ->with('certificate')
  172. ->will($this->returnValue(\OC::$SERVERROOT . '/tests/data/integritycheck/core.crt'));
  173. $this->checker
  174. ->expects($this->once())
  175. ->method('writeCoreSignature')
  176. ->willThrowException(new \Exception('My exception message'));
  177. $outputInterface
  178. ->expects($this->at(0))
  179. ->method('writeln')
  180. ->with('Error: My exception message');
  181. $this->assertEquals(1, self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]));
  182. }
  183. public function testExecute() {
  184. $inputInterface = $this->createMock(InputInterface::class);
  185. $outputInterface = $this->createMock(OutputInterface::class);
  186. $inputInterface
  187. ->expects($this->at(0))
  188. ->method('getOption')
  189. ->with('privateKey')
  190. ->will($this->returnValue('privateKey'));
  191. $inputInterface
  192. ->expects($this->at(1))
  193. ->method('getOption')
  194. ->with('certificate')
  195. ->will($this->returnValue('certificate'));
  196. $inputInterface
  197. ->expects($this->at(2))
  198. ->method('getOption')
  199. ->with('path')
  200. ->will($this->returnValue('certificate'));
  201. $this->fileAccessHelper
  202. ->expects($this->at(0))
  203. ->method('file_get_contents')
  204. ->with('privateKey')
  205. ->will($this->returnValue(\OC::$SERVERROOT . '/tests/data/integritycheck/core.key'));
  206. $this->fileAccessHelper
  207. ->expects($this->at(1))
  208. ->method('file_get_contents')
  209. ->with('certificate')
  210. ->will($this->returnValue(\OC::$SERVERROOT . '/tests/data/integritycheck/core.crt'));
  211. $this->checker
  212. ->expects($this->once())
  213. ->method('writeCoreSignature');
  214. $outputInterface
  215. ->expects($this->at(0))
  216. ->method('writeln')
  217. ->with('Successfully signed "core"');
  218. $this->assertEquals(0, self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]));
  219. }
  220. }