Skip to content

Commit 06d7285

Browse files
committed
Test signable
1 parent bb9c926 commit 06d7285

6 files changed

Lines changed: 99 additions & 3 deletions

File tree

bin/generate_CustomSignable.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@
1414
$privateKey = PEMCertificatesMock::getPrivateKey(XMLSecurityKey::RSA_SHA256, PEMCertificatesMock::SELFSIGNED_PRIVATE_KEY);
1515
$x = $signable->sign($privateKey);
1616
echo $x;
17-
//var_dump($x);

tests/XML/CustomSignable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ final class CustomSignable extends AbstractXMLElement implements SignableElement
2424
use SignableElementTrait;
2525

2626
/** @var string */
27-
public const NS = 'urn:simplesamlphp:test';
27+
public const NS = 'urn:oasis:names:tc:SAML:2.0:assertion';
2828

2929
/** @var string */
30-
public const NS_PREFIX = 'ssp';
30+
public const NS_PREFIX = 'saml';
3131

3232
/** @var \SimpleSAML\XML\Chunk $element */
3333
protected $element;

tests/XML/CustomSignableTest.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\Test\XML;
6+
7+
use DOMDocument;
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\XML\DOMDocumentFactory;
10+
use SimpleSAML\XML\Chunk;
11+
use SimpleSAML\XMLSecurity\Test\XML\CustomSignable;
12+
13+
/**
14+
* Class \SimpleSAML\XMLSecurity\XML\CustomSignableTest
15+
*
16+
* @covers \SimpleSAML\XMLSecurity\XML\SignableElementTrait
17+
* @covers \SimpleSAML\XMLSecurity\Test\XML\CustomSignable
18+
*
19+
* @package simplesamlphp/xml-security
20+
*/
21+
final class SignableElementTest extends TestCase
22+
{
23+
/** @var \DOMDocument */
24+
private DOMDocument $document;
25+
26+
27+
/**
28+
*/
29+
public function setUp(): void
30+
{
31+
$this->document = DOMDocumentFactory::fromFile(
32+
dirname(dirname(__FILE__)) . '/resources/xml/custom_CustomSignable.xml'
33+
);
34+
}
35+
36+
37+
/**
38+
*/
39+
public function testMarshalling(): void
40+
{
41+
$document = DOMDocumentFactory::fromString(
42+
'<some>Chunk</some>'
43+
);
44+
45+
$customSignable = new CustomSignable(new Chunk($document->documentElement));
46+
$this->assertFalse($customSignable->isEmptyElement());
47+
$this->assertEquals(
48+
$this->document->saveXML($this->document->documentElement),
49+
strval($customSignable)
50+
);
51+
}
52+
53+
54+
/**
55+
*/
56+
public function testUnmarshalling(): void
57+
{
58+
$customSignable = CustomSignable::fromXML($this->document->documentElement);
59+
60+
$customSignableElement = $customSignable->getElement();
61+
$customSignableElement = $customSignableElement->getXML();
62+
63+
$this->assertEquals('some', $customSignableElement->tagName);
64+
$this->assertEquals(
65+
'Chunk',
66+
$customSignableElement->textContent
67+
);
68+
}
69+
70+
71+
/**
72+
* Test serialization / unserialization
73+
*/
74+
public function testSerialization(): void
75+
{
76+
$this->assertEquals(
77+
$this->document->saveXML($this->document->documentElement),
78+
strval(unserialize(serialize(CustomSignable::fromXML($this->document->documentElement))))
79+
);
80+
}
81+
}
82+

tests/XML/CustomSigned.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,17 @@ public static function fromXML(DOMElement $xml): object
3434
Assert::same($xml->localName, 'CustomSignable', InvalidDOMElementException::class);
3535
Assert::same($xml->namespaceURI, CustomSignable::NS, InvalidDOMElementException::class);
3636

37+
var_dump($xml->ownerDocument->saveXML());
38+
// $signatureElement = XMLUtils::xpQuery($xml, './ds:Signature');
39+
// Assert::minCount($signatureElement, 1, MissingElementException::class);
40+
// Assert::minCount($signatureElement, 1, TooManyElementsException::class);
3741
$signature = Signature::getChildrenOfClass($xml);
3842
Assert::minCount($signature, 1, MissingElementException::class);
3943
Assert::minCount($signature, 1, TooManyElementsException::class);
4044

45+
// $clone = clone $signatureElement[0];
46+
// $signature = Signature::fromXML($clone);
47+
4148
return new self(
4249
$xml,
4350
CustomSignable::fromXML($xml),
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<saml:CustomSignable xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
2+
<some>Chunk</some>
3+
</saml:CustomSignable>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<saml:CustomSignable xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="pfxb5a135ff-d197-6b97-c615-dd9001fd3b59"><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
2+
<ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
3+
<ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
4+
<ds:Reference URI="#pfxb5a135ff-d197-6b97-c615-dd9001fd3b59"><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><ds:DigestValue>UFc2JQ3NbwaTn4RolCpUGudOo1fiLf+qAVznEK3tipQ=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>zNL052dCUhVupXy6vUNK831roWBwwRpIVroBaQbA3Bteff7dk94y9r9a6l1/muwcFy6CNVT1EiJ6v8G3a92imw9q4dfzi4TvXkyzm/s31OqmYZspDgVtRhypx2FF7zqNyU68NVZHFRlkApKoZY6dWFFm6ipnryrjUgtrWx8zDtk=</ds:SignatureValue>
5+
</ds:Signature><some>Chunk</some></saml:CustomSignable>

0 commit comments

Comments
 (0)