|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Test\SAML2\Type; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 9 | +use PHPUnit\Framework\Attributes\Group; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use SimpleSAML\Assert\AssertionFailedException; |
| 12 | +use SimpleSAML\SAML2\Exception\ProtocolViolationException; |
| 13 | +use SimpleSAML\SAML2\Type\CIDRValue; |
| 14 | +use SimpleSAML\XMLSchema\Exception\SchemaViolationException; |
| 15 | + |
| 16 | +/** |
| 17 | + * Class \SimpleSAML\Test\SAML2\Type\CIDRValueTest |
| 18 | + * |
| 19 | + * @package simplesamlphp/saml2 |
| 20 | + */ |
| 21 | +#[Group('type')] |
| 22 | +#[CoversClass(CIDRValue::class)] |
| 23 | +final class CIDRValueTest extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @param boolean $shouldPass |
| 27 | + * @param string $cidr |
| 28 | + */ |
| 29 | + #[DataProvider('provideCIDR')] |
| 30 | + public function testCIDR(bool $shouldPass, string $cidr): void |
| 31 | + { |
| 32 | + try { |
| 33 | + CIDRValue::fromString($cidr); |
| 34 | + $this->assertTrue($shouldPass); |
| 35 | + } catch (AssertionFailedException | ProtocolViolationException | SchemaViolationException $e) { |
| 36 | + $this->assertFalse($shouldPass); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + /** |
| 42 | + * @return array<string, array{0: bool, 1: string}> |
| 43 | + */ |
| 44 | + public static function provideCIDR(): array |
| 45 | + { |
| 46 | + return [ |
| 47 | + 'ipv4' => [true, '192.168.0.1/32'], |
| 48 | + 'ipv6' => [true, '2001:0000:130F:0000:0000:09C0:876A:130B/128'], |
| 49 | + 'ipv4 too long' => [false, '192.168.0.1.5/32'], |
| 50 | + 'whitespace suffix' => [true, '192.168.1.5/32 '], |
| 51 | + 'whitespace prefix' => [true, ' 192.168.1.5/32'], |
| 52 | + 'whitespace center' => [false, '192.168. 1.5/32'], |
| 53 | + 'ipv6 too long' => [false, '2001:0000:130F:0000:0000:09C0:876A:130B:130F:805B/128'], |
| 54 | + 'ipv6 mixed notation' => [false, '805B:2D9D:DC28::FC57:212.200.31.255'], |
| 55 | + 'ipv6 shortened notation' => [false, '::ffff:192.1.56.10/96'], |
| 56 | + 'ipv6 compressed notation' => [false, '::212.200.31.255'], |
| 57 | + 'ipv4 without length' => [false, '192.168.0.1'], |
| 58 | + 'ipv6 wihtout length' => [false, '2001:0000:130F:0000:0000:09C0:876A:130B'], |
| 59 | + 'ipv4 out of bounds length' => [false, '192.168.0.1/33'], |
| 60 | + 'ipv6 out of bounds length' => [false, '2001:0000:130F:0000:0000:09C0:876A:130B/129'], |
| 61 | + 'ipv4 out of bounds address' => [false, '256.168.0.1/32'], |
| 62 | + 'ipv6 out of bounds address' => [false, '2001:0000:130G:0000:0000:09C0:876A:130B/128'], |
| 63 | + ]; |
| 64 | + } |
| 65 | +} |
0 commit comments