1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
<?php declare(strict_types=1);
/*
* 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;
use Composer\Test\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
class TlsHelperTest extends TestCase
{
/**
* @param string[] $certNames
*/
#[DataProvider('dataCheckCertificateHost')]
public function testCheckCertificateHost(bool $expectedResult, string $hostname, array $certNames): void
{
$certificate['subject']['commonName'] = $expectedCn = array_shift($certNames);
$certificate['extensions']['subjectAltName'] = $certNames ? 'DNS:'.implode(',DNS:', $certNames) : '';
// @phpstan-ignore staticMethod.deprecatedClass
$result = TlsHelper::checkCertificateHost($certificate, $hostname, $foundCn);
if (true === $expectedResult) {
self::assertTrue($result);
self::assertSame($expectedCn, $foundCn);
} else {
self::assertFalse($result);
self::assertNull($foundCn);
}
}
public static function dataCheckCertificateHost(): array
{
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']],
];
}
public function testGetCertificateNames(): void
{
$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';
// @phpstan-ignore staticMethod.deprecatedClass
$names = TlsHelper::getCertificateNames($certificate);
self::assertIsArray($names);
self::assertSame('example.net', $names['cn']);
self::assertSame([
'example.com',
'getcomposer.org',
'composer.example.org',
], $names['san']);
}
}
|