RemoteFilesystemTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. /**
  122. * @group slow
  123. */
  124. public function testCaptureAuthenticationParamsFromUrl()
  125. {
  126. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  127. $io->expects($this->once())
  128. ->method('setAuthentication')
  129. ->with($this->equalTo('github.com'), $this->equalTo('user'), $this->equalTo('pass'));
  130. $fs = new RemoteFilesystem($io, $this->getConfigMock());
  131. try {
  132. $fs->getContents('github.com', 'https://user:pass@github.com/composer/composer/404');
  133. } catch (\Exception $e) {
  134. $this->assertInstanceOf('Composer\Downloader\TransportException', $e);
  135. $this->assertNotEquals(200, $e->getCode());
  136. }
  137. }
  138. public function testGetContents()
  139. {
  140. $fs = new RemoteFilesystem($this->getMockBuilder('Composer\IO\IOInterface')->getMock(), $this->getConfigMock());
  141. $this->assertContains('testGetContents', $fs->getContents('http://example.org', 'file://'.__FILE__));
  142. }
  143. public function testCopy()
  144. {
  145. $fs = new RemoteFilesystem($this->getMockBuilder('Composer\IO\IOInterface')->getMock(), $this->getConfigMock());
  146. $file = tempnam(sys_get_temp_dir(), 'c');
  147. $this->assertTrue($fs->copy('http://example.org', 'file://'.__FILE__, $file));
  148. $this->assertFileExists($file);
  149. $this->assertContains('testCopy', file_get_contents($file));
  150. unlink($file);
  151. }
  152. /**
  153. * @group TLS
  154. */
  155. public function testGetOptionsForUrlCreatesSecureTlsDefaults()
  156. {
  157. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  158. $res = $this->callGetOptionsForUrl($io, array('example.org', array('ssl' => array('cafile' => '/some/path/file.crt'))), array(), 'http://www.example.org');
  159. $this->assertTrue(isset($res['ssl']['ciphers']));
  160. $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']);
  161. $this->assertTrue($res['ssl']['verify_peer']);
  162. $this->assertTrue($res['ssl']['SNI_enabled']);
  163. $this->assertEquals(7, $res['ssl']['verify_depth']);
  164. if (PHP_VERSION_ID < 50600) {
  165. $this->assertEquals('www.example.org', $res['ssl']['CN_match']);
  166. $this->assertEquals('www.example.org', $res['ssl']['SNI_server_name']);
  167. }
  168. $this->assertEquals('/some/path/file.crt', $res['ssl']['cafile']);
  169. if (version_compare(PHP_VERSION, '5.4.13') >= 0) {
  170. $this->assertTrue($res['ssl']['disable_compression']);
  171. } else {
  172. $this->assertFalse(isset($res['ssl']['disable_compression']));
  173. }
  174. }
  175. /**
  176. * Provides URLs to public downloads at BitBucket.
  177. *
  178. * @return string[][]
  179. */
  180. public function provideBitbucketPublicDownloadUrls()
  181. {
  182. return array(
  183. array('https://bitbucket.org/seldaek/composer-live-test-repo/downloads/composer-unit-test-download-me.txt', '1234'),
  184. );
  185. }
  186. /**
  187. * Tests that a BitBucket public download is correctly retrieved.
  188. *
  189. * @param string $url
  190. * @param string $contents
  191. * @dataProvider provideBitbucketPublicDownloadUrls
  192. */
  193. public function testBitBucketPublicDownload($url, $contents)
  194. {
  195. $io = $this
  196. ->getMockBuilder('Composer\IO\ConsoleIO')
  197. ->disableOriginalConstructor()
  198. ->getMock();
  199. $rfs = new RemoteFilesystem($io, $this->getConfigMock());
  200. $hostname = parse_url($url, PHP_URL_HOST);
  201. $result = $rfs->getContents($hostname, $url, false);
  202. $this->assertEquals($contents, $result);
  203. }
  204. /**
  205. * Tests that a BitBucket public download is correctly retrieved when `bitbucket-oauth` is configured.
  206. *
  207. * @param string $url
  208. * @param string $contents
  209. * @dataProvider provideBitbucketPublicDownloadUrls
  210. */
  211. public function testBitBucketPublicDownloadWithAuthConfigured($url, $contents)
  212. {
  213. $io = $this
  214. ->getMockBuilder('Composer\IO\ConsoleIO')
  215. ->disableOriginalConstructor()
  216. ->getMock();
  217. $domains = array();
  218. $io
  219. ->expects($this->any())
  220. ->method('hasAuthentication')
  221. ->will($this->returnCallback(function ($arg) use (&$domains) {
  222. $domains[] = $arg;
  223. // first time is called with bitbucket.org, then it redirects to bbuseruploads.s3.amazonaws.com so next time we have no auth configured
  224. return $arg === 'bitbucket.org';
  225. }));
  226. $io
  227. ->expects($this->at(1))
  228. ->method('getAuthentication')
  229. ->with('bitbucket.org')
  230. ->willReturn(array(
  231. 'username' => 'x-token-auth',
  232. // This token is fake, but it matches a valid token's pattern.
  233. 'password' => '1A0yeK5Po3ZEeiiRiMWLivS0jirLdoGuaSGq9NvESFx1Fsdn493wUDXC8rz_1iKVRTl1GINHEUCsDxGh5lZ=',
  234. ));
  235. $rfs = new RemoteFilesystem($io, $this->getConfigMock());
  236. $hostname = parse_url($url, PHP_URL_HOST);
  237. $result = $rfs->getContents($hostname, $url, false);
  238. $this->assertEquals($contents, $result);
  239. $this->assertEquals(array('bitbucket.org', 'bbuseruploads.s3.amazonaws.com'), $domains);
  240. }
  241. protected function callGetOptionsForUrl($io, array $args = array(), array $options = array(), $fileUrl = '')
  242. {
  243. $fs = new RemoteFilesystem($io, $this->getConfigMock(), $options);
  244. $ref = new \ReflectionMethod($fs, 'getOptionsForUrl');
  245. $prop = new \ReflectionProperty($fs, 'fileUrl');
  246. $ref->setAccessible(true);
  247. $prop->setAccessible(true);
  248. $prop->setValue($fs, $fileUrl);
  249. return $ref->invokeArgs($fs, $args);
  250. }
  251. protected function callCallbackGet(RemoteFilesystem $fs, $notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
  252. {
  253. $ref = new \ReflectionMethod($fs, 'callbackGet');
  254. $ref->setAccessible(true);
  255. $ref->invoke($fs, $notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax);
  256. }
  257. protected function setAttribute($object, $attribute, $value)
  258. {
  259. $attr = new \ReflectionProperty($object, $attribute);
  260. $attr->setAccessible(true);
  261. $attr->setValue($object, $value);
  262. }
  263. }