1
0

SignAppTest.php 8.8 KB

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