|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\XMLSecurity\Test\XML\xenc; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use SimpleSAML\XML\Chunk; |
| 10 | +use SimpleSAML\XML\DOMDocumentFactory; |
| 11 | +use SimpleSAML\XML\TestUtils\SerializableElementTestTrait; |
| 12 | +use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException; |
| 13 | +use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock; |
| 14 | +use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement; |
| 15 | +use SimpleSAML\XMLSecurity\XML\ds\AbstractKeyInfoType; |
| 16 | +use SimpleSAML\XMLSecurity\XML\ds\KeyName; |
| 17 | +use SimpleSAML\XMLSecurity\XML\ds\X509Certificate; |
| 18 | +use SimpleSAML\XMLSecurity\XML\ds\X509Data; |
| 19 | +use SimpleSAML\XMLSecurity\XML\ds\X509SubjectName; |
| 20 | +use SimpleSAML\XMLSecurity\XML\xenc\RecipientKeyInfo; |
| 21 | + |
| 22 | +use function dirname; |
| 23 | +use function openssl_x509_parse; |
| 24 | +use function str_replace; |
| 25 | +use function strval; |
| 26 | + |
| 27 | +/** |
| 28 | + * Class \SimpleSAML\XMLSecurity\Test\XML\xenc\RecipientKeyInfoTest |
| 29 | + * |
| 30 | + * @package simplesamlphp/xml-security |
| 31 | + */ |
| 32 | +#[CoversClass(AbstractDsElement::class)] |
| 33 | +#[CoversClass(AbstractKeyInfoType::class)] |
| 34 | +#[CoversClass(RecipientKeyInfo::class)] |
| 35 | +final class RecipientKeyInfoTest extends TestCase |
| 36 | +{ |
| 37 | + use SerializableElementTestTrait; |
| 38 | + |
| 39 | + /** @var string */ |
| 40 | + private static string $certificate; |
| 41 | + |
| 42 | + /** @var string[] */ |
| 43 | + private static array $certData; |
| 44 | + |
| 45 | + |
| 46 | + /** |
| 47 | + */ |
| 48 | + public function setUp(): void |
| 49 | + { |
| 50 | + self::$testedClass = RecipientKeyInfo::class; |
| 51 | + |
| 52 | + self::$xmlRepresentation = DOMDocumentFactory::fromFile( |
| 53 | + dirname(__FILE__, 3) . '/resources/xml/xenc_RecipientKeyInfo.xml', |
| 54 | + ); |
| 55 | + |
| 56 | + self::$certificate = str_replace( |
| 57 | + [ |
| 58 | + '-----BEGIN CERTIFICATE-----', |
| 59 | + '-----END CERTIFICATE-----', |
| 60 | + '-----BEGIN RSA PUBLIC KEY-----', |
| 61 | + '-----END RSA PUBLIC KEY-----', |
| 62 | + "\r\n", |
| 63 | + "\n", |
| 64 | + ], |
| 65 | + [ |
| 66 | + '', |
| 67 | + '', |
| 68 | + '', |
| 69 | + '', |
| 70 | + "\n", |
| 71 | + '', |
| 72 | + ], |
| 73 | + PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE), |
| 74 | + ); |
| 75 | + |
| 76 | + self::$certData = openssl_x509_parse( |
| 77 | + PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE), |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + /** |
| 83 | + */ |
| 84 | + public function testMarshalling(): void |
| 85 | + { |
| 86 | + $recipientKeyInfo = new RecipientKeyInfo( |
| 87 | + [ |
| 88 | + new KeyName('testkey'), |
| 89 | + new X509Data( |
| 90 | + [ |
| 91 | + new X509Certificate(self::$certificate), |
| 92 | + new X509SubjectName(self::$certData['name']), |
| 93 | + ], |
| 94 | + ), |
| 95 | + new Chunk(DOMDocumentFactory::fromString( |
| 96 | + '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>', |
| 97 | + )->documentElement), |
| 98 | + ], |
| 99 | + 'fed654', |
| 100 | + ); |
| 101 | + |
| 102 | + $this->assertEquals( |
| 103 | + self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), |
| 104 | + strval($recipientKeyInfo), |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + /** |
| 110 | + */ |
| 111 | + public function testMarshallingEmpty(): void |
| 112 | + { |
| 113 | + $this->expectException(InvalidArgumentException::class); |
| 114 | + $this->expectExceptionMessage('xenc:RecipientKeyInfo cannot be empty'); |
| 115 | + |
| 116 | + new RecipientKeyInfo([]); |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + /** |
| 121 | + */ |
| 122 | + public function testUnmarshallingEmpty(): void |
| 123 | + { |
| 124 | + $document = DOMDocumentFactory::fromString('<xenc:RecipientKeyInfo xmlns:xenc="' . RecipientKeyInfo::NS . '"/>'); |
| 125 | + |
| 126 | + $this->expectException(InvalidArgumentException::class); |
| 127 | + $this->expectExceptionMessage('xenc:RecipientKeyInfo cannot be empty'); |
| 128 | + |
| 129 | + RecipientKeyInfo::fromXML($document->documentElement); |
| 130 | + } |
| 131 | +} |
0 commit comments