RemoteFilesystemTest.php 11 KB

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