RemoteFilesystemTest.php 11 KB

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