1
0
Fork 0
composer/tests/Composer/Test/Util/TlsHelperTest.php

84 lines
3.3 KiB
PHP
Raw Normal View History

2022-02-23 15:58:18 +00:00
<?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;
2020-02-07 03:18:45 +00:00
use Composer\Test\TestCase;
class TlsHelperTest extends TestCase
{
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
{
$certificate['subject']['commonName'] = $expectedCn = array_shift($certNames);
$certificate['extensions']['subjectAltName'] = $certNames ? 'DNS:'.implode(',DNS:', $certNames) : '';
// @phpstan-ignore-next-line
$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
{
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']],
];
}
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-next-line
$names = TlsHelper::getCertificateNames($certificate);
$this->assertSame('example.net', $names['cn']);
2022-08-17 12:20:07 +00:00
$this->assertSame([
'example.com',
'getcomposer.org',
'composer.example.org',
2022-08-17 12:20:07 +00:00
], $names['san']);
}
}