TlsHelperTest.php 3.3 KB

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