2016-01-24 18:52:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Test\Util;
|
|
|
|
|
|
|
|
use Composer\Util\TlsHelper;
|
2020-02-07 03:18:45 +00:00
|
|
|
use Composer\Test\TestCase;
|
2016-01-24 18:52:09 +00:00
|
|
|
|
2017-11-04 14:52:13 +00:00
|
|
|
class TlsHelperTest extends TestCase
|
2016-01-24 18:52:09 +00:00
|
|
|
{
|
2021-10-27 14:18:46 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider dataCheckCertificateHost
|
|
|
|
*
|
|
|
|
* @param bool $expectedResult
|
|
|
|
* @param string $hostname
|
|
|
|
* @param string[] $certNames
|
|
|
|
*/
|
2016-01-24 18:52:09 +00:00
|
|
|
public function testCheckCertificateHost($expectedResult, $hostname, $certNames)
|
|
|
|
{
|
|
|
|
$certificate['subject']['commonName'] = $expectedCn = array_shift($certNames);
|
|
|
|
$certificate['extensions']['subjectAltName'] = $certNames ? 'DNS:'.implode(',DNS:', $certNames) : '';
|
|
|
|
|
2022-01-04 10:15:06 +00:00
|
|
|
// @phpstan-ignore-next-line
|
2016-01-24 18:52:09 +00:00
|
|
|
$result = TlsHelper::checkCertificateHost($certificate, $hostname, $foundCn);
|
|
|
|
|
|
|
|
if (true === $expectedResult) {
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertSame($expectedCn, $foundCn);
|
|
|
|
} else {
|
|
|
|
$this->assertFalse($result);
|
|
|
|
$this->assertNull($foundCn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataCheckCertificateHost()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(true, 'getcomposer.org', array('getcomposer.org')),
|
|
|
|
array(true, 'getcomposer.org', array('getcomposer.org', 'packagist.org')),
|
|
|
|
array(true, 'getcomposer.org', array('packagist.org', 'getcomposer.org')),
|
|
|
|
array(true, 'foo.getcomposer.org', array('*.getcomposer.org')),
|
|
|
|
array(false, 'xyz.foo.getcomposer.org', array('*.getcomposer.org')),
|
|
|
|
array(true, 'foo.getcomposer.org', array('getcomposer.org', '*.getcomposer.org')),
|
|
|
|
array(true, 'foo.getcomposer.org', array('foo.getcomposer.org', 'foo*.getcomposer.org')),
|
|
|
|
array(true, 'foo1.getcomposer.org', array('foo.getcomposer.org', 'foo*.getcomposer.org')),
|
|
|
|
array(true, 'foo2.getcomposer.org', array('foo.getcomposer.org', 'foo*.getcomposer.org')),
|
|
|
|
array(false, 'foo2.another.getcomposer.org', array('foo.getcomposer.org', 'foo*.getcomposer.org')),
|
|
|
|
array(false, 'test.example.net', array('**.example.net', '**.example.net')),
|
|
|
|
array(false, 'test.example.net', array('t*t.example.net', 't*t.example.net')),
|
|
|
|
array(false, 'xyz.example.org', array('*z.example.org', '*z.example.org')),
|
|
|
|
array(false, 'foo.bar.example.com', array('foo.*.example.com', 'foo.*.example.com')),
|
|
|
|
array(false, 'example.com', array('example.*', 'example.*')),
|
|
|
|
array(true, 'localhost', array('localhost')),
|
|
|
|
array(false, 'localhost', array('*')),
|
|
|
|
array(false, 'localhost', array('local*')),
|
|
|
|
array(false, 'example.net', array('*.net', '*.org', 'ex*.net')),
|
|
|
|
array(true, 'example.net', array('*.net', '*.org', 'example.net')),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetCertificateNames()
|
|
|
|
{
|
|
|
|
$certificate['subject']['commonName'] = 'example.net';
|
|
|
|
$certificate['extensions']['subjectAltName'] = 'DNS: example.com, IP: 127.0.0.1, DNS: getcomposer.org, Junk: blah, DNS: composer.example.org';
|
|
|
|
|
2022-01-04 10:15:06 +00:00
|
|
|
// @phpstan-ignore-next-line
|
2016-01-24 18:52:09 +00:00
|
|
|
$names = TlsHelper::getCertificateNames($certificate);
|
|
|
|
|
|
|
|
$this->assertSame('example.net', $names['cn']);
|
|
|
|
$this->assertSame(array(
|
|
|
|
'example.com',
|
|
|
|
'getcomposer.org',
|
|
|
|
'composer.example.org',
|
|
|
|
), $names['san']);
|
|
|
|
}
|
|
|
|
}
|