2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2019-11-29 16:47:40 +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\NoProxyPattern;
|
2020-02-07 03:18:45 +00:00
|
|
|
use Composer\Test\TestCase;
|
2019-11-29 16:47:40 +00:00
|
|
|
|
|
|
|
class NoProxyPatternTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @dataProvider dataHostName
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testHostName(string $noproxy, string $url, bool $expected): void
|
2019-11-29 16:47:40 +00:00
|
|
|
{
|
|
|
|
$matcher = new NoProxyPattern($noproxy);
|
|
|
|
$url = $this->getUrl($url);
|
|
|
|
$this->assertEquals($expected, $matcher->test($url));
|
|
|
|
}
|
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
public static function dataHostName(): array
|
2019-11-29 16:47:40 +00:00
|
|
|
{
|
|
|
|
$noproxy = 'foobar.com, .barbaz.net';
|
|
|
|
|
|
|
|
// noproxy, url, expected
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
'match as foobar.com' => [$noproxy, 'foobar.com', true],
|
|
|
|
'match foobar.com' => [$noproxy, 'www.foobar.com', true],
|
|
|
|
'no match foobar.com' => [$noproxy, 'foofoobar.com', false],
|
|
|
|
'match .barbaz.net 1' => [$noproxy, 'barbaz.net', true],
|
|
|
|
'match .barbaz.net 2' => [$noproxy, 'www.barbaz.net', true],
|
|
|
|
'no match .barbaz.net' => [$noproxy, 'barbarbaz.net', false],
|
|
|
|
'no match wrong domain' => [$noproxy, 'barbaz.com', false],
|
|
|
|
'no match FQDN' => [$noproxy, 'foobar.com.', false],
|
|
|
|
];
|
2019-11-29 16:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataIpAddress
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testIpAddress(string $noproxy, string $url, bool $expected): void
|
2019-11-29 16:47:40 +00:00
|
|
|
{
|
|
|
|
$matcher = new NoProxyPattern($noproxy);
|
|
|
|
$url = $this->getUrl($url);
|
|
|
|
$this->assertEquals($expected, $matcher->test($url));
|
|
|
|
}
|
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
public static function dataIpAddress(): array
|
2019-11-29 16:47:40 +00:00
|
|
|
{
|
2019-11-30 15:45:54 +00:00
|
|
|
$noproxy = '192.168.1.1, 2001:db8::52:0:1';
|
2019-11-29 16:47:40 +00:00
|
|
|
|
|
|
|
// noproxy, url, expected
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
'match exact IPv4' => [$noproxy, '192.168.1.1', true],
|
|
|
|
'no match IPv4' => [$noproxy, '192.168.1.4', false],
|
|
|
|
'match exact IPv6' => [$noproxy, '[2001:db8:0:0:0:52:0:1]', true],
|
|
|
|
'no match IPv6' => [$noproxy, '[2001:db8:0:0:0:52:0:2]', false],
|
|
|
|
'match mapped IPv4' => [$noproxy, '[::FFFF:C0A8:0101]', true],
|
|
|
|
'no match mapped IPv4' => [$noproxy, '[::FFFF:C0A8:0104]', false],
|
|
|
|
];
|
2019-11-29 16:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataIpRange
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testIpRange(string $noproxy, string $url, bool $expected): void
|
2019-11-29 16:47:40 +00:00
|
|
|
{
|
|
|
|
$matcher = new NoProxyPattern($noproxy);
|
|
|
|
$url = $this->getUrl($url);
|
|
|
|
$this->assertEquals($expected, $matcher->test($url));
|
|
|
|
}
|
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
public static function dataIpRange(): array
|
2019-11-29 16:47:40 +00:00
|
|
|
{
|
2019-11-30 15:45:54 +00:00
|
|
|
$noproxy = '10.0.0.0/30, 2002:db8:a::45/121';
|
2019-11-29 16:47:40 +00:00
|
|
|
|
|
|
|
// noproxy, url, expected
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
'match IPv4/CIDR' => [$noproxy, '10.0.0.2', true],
|
|
|
|
'no match IPv4/CIDR' => [$noproxy, '10.0.0.4', false],
|
|
|
|
'match IPv6/CIDR' => [$noproxy, '[2002:db8:a:0:0:0:0:7f]', true],
|
|
|
|
'no match IPv6' => [$noproxy, '[2002:db8:a:0:0:0:0:ff]', false],
|
|
|
|
'match mapped IPv4' => [$noproxy, '[::FFFF:0A00:0002]', true],
|
|
|
|
'no match mapped IPv4' => [$noproxy, '[::FFFF:0A00:0004]', false],
|
|
|
|
];
|
2019-11-29 16:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataPort
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testPort(string $noproxy, string $url, bool $expected): void
|
2019-11-29 16:47:40 +00:00
|
|
|
{
|
|
|
|
$matcher = new NoProxyPattern($noproxy);
|
|
|
|
$url = $this->getUrl($url);
|
|
|
|
$this->assertEquals($expected, $matcher->test($url));
|
|
|
|
}
|
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
public static function dataPort(): array
|
2019-11-29 16:47:40 +00:00
|
|
|
{
|
|
|
|
$noproxy = '192.168.1.2:81, 192.168.1.3:80, [2001:db8::52:0:2]:443, [2001:db8::52:0:3]:80';
|
|
|
|
|
|
|
|
// noproxy, url, expected
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
'match IPv4 port' => [$noproxy, '192.168.1.3', true],
|
|
|
|
'no match IPv4 port' => [$noproxy, '192.168.1.2', false],
|
|
|
|
'match IPv6 port' => [$noproxy, '[2001:db8::52:0:3]', true],
|
|
|
|
'no match IPv6 port' => [$noproxy, '[2001:db8::52:0:2]', false],
|
|
|
|
];
|
2019-11-29 16:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends a scheme to the test url if it is missing
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
private function getUrl(string $url): string
|
2019-11-29 16:47:40 +00:00
|
|
|
{
|
|
|
|
if (parse_url($url, PHP_URL_SCHEME)) {
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
$scheme = 'http';
|
|
|
|
|
|
|
|
if (strpos($url, '[') !== 0 && strrpos($url, ':') !== false) {
|
2022-08-17 12:20:07 +00:00
|
|
|
[, $port] = explode(':', $url);
|
2019-11-29 16:47:40 +00:00
|
|
|
|
|
|
|
if ($port === '443') {
|
|
|
|
$scheme = 'https';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf('%s://%s', $scheme, $url);
|
|
|
|
}
|
|
|
|
}
|