Skip to content

Commit 83e5414

Browse files
committed
Add HexBinaryElementTrait
1 parent 525095b commit 83e5414

File tree

4 files changed

+211
-0
lines changed

4 files changed

+211
-0
lines changed

src/HexBinaryElementTrait.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML;
6+
7+
use DOMElement;
8+
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Exception\SchemaViolationException;
10+
11+
/**
12+
* Trait grouping common functionality for simple elements with hexbinary textContent
13+
*
14+
* @package simplesamlphp/xml-common
15+
*/
16+
trait HexBinaryElementTrait
17+
{
18+
use StringElementTrait;
19+
20+
21+
/**
22+
* Sanitize the content of the element.
23+
*
24+
* Note: There are no processing rules for xs:hexBinary regarding whitespace. General consensus is to strip them
25+
*
26+
* @param string $content The unsanitized textContent
27+
* @throws \Exception on failure
28+
* @return string
29+
*/
30+
protected function sanitizeContent(string $content): string
31+
{
32+
return str_replace(["\f", "\r", "\n", "\t", "\v", ' '], '', $content);
33+
}
34+
35+
36+
/**
37+
* Validate the content of the element.
38+
*
39+
* @param string $content The value to go in the XML textContent
40+
* @throws \Exception on failure
41+
* @return void
42+
*/
43+
protected function validateContent(string $content): void
44+
{
45+
// Note: content must already be sanitized before validating
46+
Assert::regex(
47+
$this->sanitizeContent($content),
48+
'/([0-9A-F]{2})*/i',
49+
SchemaViolationException::class,
50+
);
51+
}
52+
53+
54+
/** @return string */
55+
abstract public static function getLocalName(): string;
56+
57+
58+
/**
59+
* Create a document structure for this element
60+
*
61+
* @param \DOMElement|null $parent The element we should append to.
62+
* @return \DOMElement
63+
*/
64+
abstract public function instantiateParentElement(?DOMElement $parent = null): DOMElement;
65+
}

tests/Utils/HexBinaryElement.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\XML;
6+
7+
use SimpleSAML\XML\AbstractElement;
8+
use SimpleSAML\XML\HexBinaryElementTrait;
9+
10+
/**
11+
* Empty shell class for testing HexBinaryElement.
12+
*
13+
* @package simplesaml/xml-common
14+
*/
15+
final class HexBinaryElement extends AbstractElement
16+
{
17+
use HexBinaryElementTrait;
18+
19+
/** @var string */
20+
public const NS = 'urn:x-simplesamlphp:namespace';
21+
22+
/** @var string */
23+
public const NS_PREFIX = 'ssp';
24+
25+
26+
/**
27+
* @param string $content
28+
*/
29+
public function __construct(string $content)
30+
{
31+
$this->setContent($content);
32+
}
33+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\XML;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\TestCase;
10+
use SimpleSAML\Test\XML\XMLDumper;
11+
use SimpleSAML\XML\AbstractElement;
12+
use SimpleSAML\XML\DOMDocumentFactory;
13+
use SimpleSAML\XML\HexBinaryElementTrait;
14+
use SimpleSAML\XML\StringElementTrait;
15+
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
16+
17+
use function dirname;
18+
use function strval;
19+
20+
/**
21+
* Class \SimpleSAML\XML\HexBinaryElementTraitTest
22+
*
23+
* @package simplesamlphp\xml-common
24+
*/
25+
#[CoversClass(SerializableElementTestTrait::class)]
26+
#[CoversClass(HexBinaryElementTrait::class)]
27+
#[CoversClass(StringElementTrait::class)]
28+
#[CoversClass(AbstractElement::class)]
29+
final class HexBinaryElementTraitTest extends TestCase
30+
{
31+
use SerializableElementTestTrait;
32+
33+
34+
/**
35+
*/
36+
public static function setUpBeforeClass(): void
37+
{
38+
self::$testedClass = HexBinaryElement::class;
39+
40+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
41+
dirname(__FILE__, 2) . '/resources/xml/ssp_HexBinaryElement.xml',
42+
);
43+
}
44+
45+
/**
46+
*/
47+
public function testMarshalling(): void
48+
{
49+
$hexBinaryElement = new HexBinaryElement(
50+
'3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f',
51+
);
52+
53+
$this->assertEquals(
54+
XMLDumper::dumpDOMDocumentXMLWithBase64Content(self::$xmlRepresentation),
55+
strval($hexBinaryElement),
56+
);
57+
}
58+
59+
60+
/**
61+
*/
62+
public function testUnmarshalling(): void
63+
{
64+
/** @var \DOMElement $xml */
65+
$xml = self::$xmlRepresentation->documentElement;
66+
$hexBinaryElement = HexBinaryElement::fromXML($xml);
67+
68+
$this->assertEquals(
69+
'3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f',
70+
$hexBinaryElement->getContent(),
71+
);
72+
}
73+
74+
75+
/**
76+
* @param non-empty-string $xml
77+
*/
78+
#[DataProvider('provideHexBinaryCases')]
79+
public function testHexBinaryCases(string $xml): void
80+
{
81+
$xmlRepresentation = DOMDocumentFactory::fromString($xml);
82+
/** @var \DOMElement $xmlElement */
83+
$xmlElement = $xmlRepresentation->documentElement;
84+
85+
$hexBinary = HexBinaryElement::fromXML($xmlElement);
86+
87+
$this->assertStringContainsString($hexBinary->getRawContent(), $xml);
88+
}
89+
90+
/**
91+
* @return array<string, array{0: string}>
92+
*/
93+
public static function provideHexBinaryCases(): array
94+
{
95+
return [
96+
'inline' => [
97+
<<<XML
98+
<ssp:HexBinaryElement xmlns:ssp="urn:x-simplesamlphp:namespace">3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f</ssp:HexBinaryElement>
99+
XML
100+
,
101+
],
102+
'multiline' => [
103+
<<<XML
104+
<ssp:HexBinaryElement xmlns:ssp="urn:x-simplesamlphp:namespace">
105+
3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f
106+
</ssp:HexBinaryElement>
107+
XML
108+
,
109+
],
110+
];
111+
}
112+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<ssp:HexBinaryElement xmlns:ssp="urn:x-simplesamlphp:namespace">3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f</ssp:HexBinaryElement>

0 commit comments

Comments
 (0)