2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2016-01-24 18:52:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 string[] $certNames
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testCheckCertificateHost(bool $expectedResult, string $hostname, array $certNames): void
|
2016-01-24 18:52:09 +00:00
|
|
|
{
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 12:42:28 +00:00
|
|
|
public function dataCheckCertificateHost(): array
|
2016-01-24 18:52:09 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
[true, 'getcomposer.org', ['getcomposer.org']],
|
|
|
|
[true, 'getcomposer.org', ['getcomposer.org', 'packagist.org']],
|
|
|
|
[true, 'getcomposer.org', ['packagist.org', 'getcomposer.org']],
|
|
|
|
[true, 'foo.getcomposer.org', ['*.getcomposer.org']],
|
|
|
|
[false, 'xyz.foo.getcomposer.org', ['*.getcomposer.org']],
|
|
|
|
[true, 'foo.getcomposer.org', ['getcomposer.org', '*.getcomposer.org']],
|
|
|
|
[true, 'foo.getcomposer.org', ['foo.getcomposer.org', 'foo*.getcomposer.org']],
|
|
|
|
[true, 'foo1.getcomposer.org', ['foo.getcomposer.org', 'foo*.getcomposer.org']],
|
|
|
|
[true, 'foo2.getcomposer.org', ['foo.getcomposer.org', 'foo*.getcomposer.org']],
|
|
|
|
[false, 'foo2.another.getcomposer.org', ['foo.getcomposer.org', 'foo*.getcomposer.org']],
|
|
|
|
[false, 'test.example.net', ['**.example.net', '**.example.net']],
|
|
|
|
[false, 'test.example.net', ['t*t.example.net', 't*t.example.net']],
|
|
|
|
[false, 'xyz.example.org', ['*z.example.org', '*z.example.org']],
|
|
|
|
[false, 'foo.bar.example.com', ['foo.*.example.com', 'foo.*.example.com']],
|
|
|
|
[false, 'example.com', ['example.*', 'example.*']],
|
|
|
|
[true, 'localhost', ['localhost']],
|
|
|
|
[false, 'localhost', ['*']],
|
|
|
|
[false, 'localhost', ['local*']],
|
|
|
|
[false, 'example.net', ['*.net', '*.org', 'ex*.net']],
|
|
|
|
[true, 'example.net', ['*.net', '*.org', 'example.net']],
|
|
|
|
];
|
2016-01-24 18:52:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetCertificateNames(): void
|
2016-01-24 18:52:09 +00:00
|
|
|
{
|
|
|
|
$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);
|
|
|
|
|
2022-11-03 20:47:26 +00:00
|
|
|
self::assertIsArray($names);
|
2016-01-24 18:52:09 +00:00
|
|
|
$this->assertSame('example.net', $names['cn']);
|
2022-08-17 12:20:07 +00:00
|
|
|
$this->assertSame([
|
2016-01-24 18:52:09 +00:00
|
|
|
'example.com',
|
|
|
|
'getcomposer.org',
|
|
|
|
'composer.example.org',
|
2022-08-17 12:20:07 +00:00
|
|
|
], $names['san']);
|
2016-01-24 18:52:09 +00:00
|
|
|
}
|
|
|
|
}
|