RemoteFilesystemTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Util;
  12. use Composer\Util\RemoteFilesystem;
  13. use PHPUnit\Framework\TestCase;
  14. class RemoteFilesystemTest extends TestCase
  15. {
  16. private function getConfigMock()
  17. {
  18. $config = $this->getMockBuilder('Composer\Config')->getMock();
  19. $config->expects($this->any())
  20. ->method('get')
  21. ->will($this->returnCallback(function ($key) {
  22. if ($key === 'github-domains' || $key === 'gitlab-domains') {
  23. return array();
  24. }
  25. }));
  26. return $config;
  27. }
  28. public function testGetOptionsForUrl()
  29. {
  30. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  31. $io
  32. ->expects($this->once())
  33. ->method('hasAuthentication')
  34. ->will($this->returnValue(false))
  35. ;
  36. $res = $this->callGetOptionsForUrl($io, array('http://example.org', array()));
  37. $this->assertTrue(isset($res['http']['header']) && is_array($res['http']['header']), 'getOptions must return an array with headers');
  38. }
  39. public function testGetOptionsForUrlWithAuthorization()
  40. {
  41. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  42. $io
  43. ->expects($this->once())
  44. ->method('hasAuthentication')
  45. ->will($this->returnValue(true))
  46. ;
  47. $io
  48. ->expects($this->once())
  49. ->method('getAuthentication')
  50. ->will($this->returnValue(array('username' => 'login', 'password' => 'password')))
  51. ;
  52. $options = $this->callGetOptionsForUrl($io, array('http://example.org', array()));
  53. $found = false;
  54. foreach ($options['http']['header'] as $header) {
  55. if (0 === strpos($header, 'Authorization: Basic')) {
  56. $found = true;
  57. }
  58. }
  59. $this->assertTrue($found, 'getOptions must have an Authorization header');
  60. }
  61. public function testGetOptionsForUrlWithStreamOptions()
  62. {
  63. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  64. $io
  65. ->expects($this->once())
  66. ->method('hasAuthentication')
  67. ->will($this->returnValue(true))
  68. ;
  69. $streamOptions = array('ssl' => array(
  70. 'allow_self_signed' => true,
  71. ));
  72. $res = $this->callGetOptionsForUrl($io, array('https://example.org', array()), $streamOptions);
  73. $this->assertTrue(isset($res['ssl']) && isset($res['ssl']['allow_self_signed']) && true === $res['ssl']['allow_self_signed'], 'getOptions must return an array with a allow_self_signed set to true');
  74. }
  75. public function testGetOptionsForUrlWithCallOptionsKeepsHeader()
  76. {
  77. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  78. $io
  79. ->expects($this->once())
  80. ->method('hasAuthentication')
  81. ->will($this->returnValue(true))
  82. ;
  83. $streamOptions = array('http' => array(
  84. 'header' => 'Foo: bar',
  85. ));
  86. $res = $this->callGetOptionsForUrl($io, array('https://example.org', $streamOptions));
  87. $this->assertTrue(isset($res['http']['header']), 'getOptions must return an array with a http.header key');
  88. $found = false;
  89. foreach ($res['http']['header'] as $header) {
  90. if ($header === 'Foo: bar') {
  91. $found = true;
  92. }
  93. }
  94. $this->assertTrue($found, 'getOptions must have a Foo: bar header');
  95. $this->assertGreaterThan(1, count($res['http']['header']));
  96. }
  97. public function testCallbackGetFileSize()
  98. {
  99. $fs = new RemoteFilesystem($this->getMockBuilder('Composer\IO\IOInterface')->getMock(), $this->getConfigMock());
  100. $this->callCallbackGet($fs, STREAM_NOTIFY_FILE_SIZE_IS, 0, '', 0, 0, 20);
  101. $this->assertAttributeEquals(20, 'bytesMax', $fs);
  102. }
  103. public function testCallbackGetNotifyProgress()
  104. {
  105. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  106. $io
  107. ->expects($this->once())
  108. ->method('overwriteError')
  109. ;
  110. $fs = new RemoteFilesystem($io, $this->getConfigMock());
  111. $this->setAttribute($fs, 'bytesMax', 20);
  112. $this->setAttribute($fs, 'progress', true);
  113. $this->callCallbackGet($fs, STREAM_NOTIFY_PROGRESS, 0, '', 0, 10, 20);
  114. $this->assertAttributeEquals(50, 'lastProgress', $fs);
  115. }
  116. public function testCallbackGetPassesThrough404()
  117. {
  118. $fs = new RemoteFilesystem($this->getMockBuilder('Composer\IO\IOInterface')->getMock(), $this->getConfigMock());
  119. $this->assertNull($this->callCallbackGet($fs, STREAM_NOTIFY_FAILURE, 0, 'HTTP/1.1 404 Not Found', 404, 0, 0));
  120. }
  121. public function testGetContents()
  122. {
  123. $fs = new RemoteFilesystem($this->getMockBuilder('Composer\IO\IOInterface')->getMock(), $this->getConfigMock());
  124. $this->assertContains('testGetContents', $fs->getContents('http://example.org', 'file://'.__FILE__));
  125. }
  126. public function testCopy()
  127. {
  128. $fs = new RemoteFilesystem($this->getMockBuilder('Composer\IO\IOInterface')->getMock(), $this->getConfigMock());
  129. $file = tempnam(sys_get_temp_dir(), 'c');
  130. $this->assertTrue($fs->copy('http://example.org', 'file://'.__FILE__, $file));
  131. $this->assertFileExists($file);
  132. $this->assertContains('testCopy', file_get_contents($file));
  133. unlink($file);
  134. }
  135. /**
  136. * @group TLS
  137. */
  138. public function testGetOptionsForUrlCreatesSecureTlsDefaults()
  139. {
  140. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  141. $res = $this->callGetOptionsForUrl($io, array('example.org', array('ssl' => array('cafile' => '/some/path/file.crt'))), array(), 'http://www.example.org');
  142. $this->assertTrue(isset($res['ssl']['ciphers']));
  143. $this->assertRegExp("|!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA|", $res['ssl']['ciphers']);
  144. $this->assertTrue($res['ssl']['verify_peer']);
  145. $this->assertTrue($res['ssl']['SNI_enabled']);
  146. $this->assertEquals(7, $res['ssl']['verify_depth']);
  147. if (PHP_VERSION_ID < 50600) {
  148. $this->assertEquals('www.example.org', $res['ssl']['CN_match']);
  149. $this->assertEquals('www.example.org', $res['ssl']['SNI_server_name']);
  150. }
  151. $this->assertEquals('/some/path/file.crt', $res['ssl']['cafile']);
  152. if (version_compare(PHP_VERSION, '5.4.13') >= 0) {
  153. $this->assertTrue($res['ssl']['disable_compression']);
  154. } else {
  155. $this->assertFalse(isset($res['ssl']['disable_compression']));
  156. }
  157. }
  158. /**
  159. * Provides URLs to public downloads at BitBucket.
  160. *
  161. * @return string[][]
  162. */
  163. public function provideBitbucketPublicDownloadUrls()
  164. {
  165. return array(
  166. array('https://bitbucket.org/seldaek/composer-live-test-repo/downloads/composer-unit-test-download-me.txt', '1234'),
  167. );
  168. }
  169. /**
  170. * Tests that a BitBucket public download is correctly retrieved.
  171. *
  172. * @param string $url
  173. * @param string $contents
  174. * @dataProvider provideBitbucketPublicDownloadUrls
  175. */
  176. public function testBitBucketPublicDownload($url, $contents)
  177. {
  178. $io = $this
  179. ->getMockBuilder('Composer\IO\ConsoleIO')
  180. ->disableOriginalConstructor()
  181. ->getMock();
  182. $rfs = new RemoteFilesystem($io, $this->getConfigMock());
  183. $hostname = parse_url($url, PHP_URL_HOST);
  184. $result = $rfs->getContents($hostname, $url, false);
  185. $this->assertEquals($contents, $result);
  186. }
  187. /**
  188. * Tests that a BitBucket public download is correctly retrieved when `bitbucket-oauth` is configured.
  189. *
  190. * @param string $url
  191. * @param string $contents
  192. * @dataProvider provideBitbucketPublicDownloadUrls
  193. */
  194. public function testBitBucketPublicDownloadWithAuthConfigured($url, $contents)
  195. {
  196. $io = $this
  197. ->getMockBuilder('Composer\IO\ConsoleIO')
  198. ->disableOriginalConstructor()
  199. ->getMock();
  200. $domains = array();
  201. $io
  202. ->expects($this->any())
  203. ->method('hasAuthentication')
  204. ->will($this->returnCallback(function ($arg) use (&$domains) {
  205. $domains[] = $arg;
  206. // first time is called with bitbucket.org, then it redirects to bbuseruploads.s3.amazonaws.com so next time we have no auth configured
  207. return $arg === 'bitbucket.org';
  208. }));
  209. $io
  210. ->expects($this->at(1))
  211. ->method('getAuthentication')
  212. ->with('bitbucket.org')
  213. ->willReturn(array(
  214. 'username' => 'x-token-auth',
  215. // This token is fake, but it matches a valid token's pattern.
  216. 'password' => '1A0yeK5Po3ZEeiiRiMWLivS0jirLdoGuaSGq9NvESFx1Fsdn493wUDXC8rz_1iKVRTl1GINHEUCsDxGh5lZ=',
  217. ));
  218. $rfs = new RemoteFilesystem($io, $this->getConfigMock());
  219. $hostname = parse_url($url, PHP_URL_HOST);
  220. $result = $rfs->getContents($hostname, $url, false);
  221. $this->assertEquals($contents, $result);
  222. $this->assertEquals(array('bitbucket.org', 'bbuseruploads.s3.amazonaws.com'), $domains);
  223. }
  224. protected function callGetOptionsForUrl($io, array $args = array(), array $options = array(), $fileUrl = '')
  225. {
  226. $fs = new RemoteFilesystem($io, $this->getConfigMock(), $options);
  227. $ref = new \ReflectionMethod($fs, 'getOptionsForUrl');
  228. $prop = new \ReflectionProperty($fs, 'fileUrl');
  229. $ref->setAccessible(true);
  230. $prop->setAccessible(true);
  231. $prop->setValue($fs, $fileUrl);
  232. return $ref->invokeArgs($fs, $args);
  233. }
  234. protected function callCallbackGet(RemoteFilesystem $fs, $notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
  235. {
  236. $ref = new \ReflectionMethod($fs, 'callbackGet');
  237. $ref->setAccessible(true);
  238. $ref->invoke($fs, $notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax);
  239. }
  240. protected function setAttribute($object, $attribute, $value)
  241. {
  242. $attr = new \ReflectionProperty($object, $attribute);
  243. $attr->setAccessible(true);
  244. $attr->setValue($object, $value);
  245. }
  246. }