PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Composer/Test/Util/RemoteFilesystemTest.php

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