TlsHelperTest.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\TlsHelper;
  13. use Composer\Test\TestCase;
  14. class TlsHelperTest extends TestCase
  15. {
  16. /** @dataProvider dataCheckCertificateHost */
  17. public function testCheckCertificateHost($expectedResult, $hostname, $certNames)
  18. {
  19. $certificate['subject']['commonName'] = $expectedCn = array_shift($certNames);
  20. $certificate['extensions']['subjectAltName'] = $certNames ? 'DNS:'.implode(',DNS:', $certNames) : '';
  21. $result = TlsHelper::checkCertificateHost($certificate, $hostname, $foundCn);
  22. if (true === $expectedResult) {
  23. $this->assertTrue($result);
  24. $this->assertSame($expectedCn, $foundCn);
  25. } else {
  26. $this->assertFalse($result);
  27. $this->assertNull($foundCn);
  28. }
  29. }
  30. public function dataCheckCertificateHost()
  31. {
  32. return array(
  33. array(true, 'getcomposer.org', array('getcomposer.org')),
  34. array(true, 'getcomposer.org', array('getcomposer.org', 'packagist.org')),
  35. array(true, 'getcomposer.org', array('packagist.org', 'getcomposer.org')),
  36. array(true, 'foo.getcomposer.org', array('*.getcomposer.org')),
  37. array(false, 'xyz.foo.getcomposer.org', array('*.getcomposer.org')),
  38. array(true, 'foo.getcomposer.org', array('getcomposer.org', '*.getcomposer.org')),
  39. array(true, 'foo.getcomposer.org', array('foo.getcomposer.org', 'foo*.getcomposer.org')),
  40. array(true, 'foo1.getcomposer.org', array('foo.getcomposer.org', 'foo*.getcomposer.org')),
  41. array(true, 'foo2.getcomposer.org', array('foo.getcomposer.org', 'foo*.getcomposer.org')),
  42. array(false, 'foo2.another.getcomposer.org', array('foo.getcomposer.org', 'foo*.getcomposer.org')),
  43. array(false, 'test.example.net', array('**.example.net', '**.example.net')),
  44. array(false, 'test.example.net', array('t*t.example.net', 't*t.example.net')),
  45. array(false, 'xyz.example.org', array('*z.example.org', '*z.example.org')),
  46. array(false, 'foo.bar.example.com', array('foo.*.example.com', 'foo.*.example.com')),
  47. array(false, 'example.com', array('example.*', 'example.*')),
  48. array(true, 'localhost', array('localhost')),
  49. array(false, 'localhost', array('*')),
  50. array(false, 'localhost', array('local*')),
  51. array(false, 'example.net', array('*.net', '*.org', 'ex*.net')),
  52. array(true, 'example.net', array('*.net', '*.org', 'example.net')),
  53. );
  54. }
  55. public function testGetCertificateNames()
  56. {
  57. $certificate['subject']['commonName'] = 'example.net';
  58. $certificate['extensions']['subjectAltName'] = 'DNS: example.com, IP: 127.0.0.1, DNS: getcomposer.org, Junk: blah, DNS: composer.example.org';
  59. $names = TlsHelper::getCertificateNames($certificate);
  60. $this->assertSame('example.net', $names['cn']);
  61. $this->assertSame(array(
  62. 'example.com',
  63. 'getcomposer.org',
  64. 'composer.example.org',
  65. ), $names['san']);
  66. }
  67. }