Skip to content

Commit 371d7c7

Browse files
committed
Add HexBinaryElementTrait
1 parent 525095b commit 371d7c7

File tree

4 files changed

+209
-0
lines changed

4 files changed

+209
-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: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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\HexBinaryElementTrait;
13+
use SimpleSAML\XML\DOMDocumentFactory;
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('3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f');
50+
51+
$this->assertEquals(
52+
XMLDumper::dumpDOMDocumentXMLWithBase64Content(self::$xmlRepresentation),
53+
strval($hexBinaryElement),
54+
);
55+
}
56+
57+
58+
/**
59+
*/
60+
public function testUnmarshalling(): void
61+
{
62+
/** @var \DOMElement $xml */
63+
$xml = self::$xmlRepresentation->documentElement;
64+
$hexBinaryElement = HexBinaryElement::fromXML($xml);
65+
66+
$this->assertEquals(
67+
'3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f',
68+
$hexBinaryElement->getContent(),
69+
);
70+
}
71+
72+
73+
/**
74+
* @param non-empty-string $xml
75+
*/
76+
#[DataProvider('provideHexBinaryCases')]
77+
public function testHexBinaryCases(string $xml): void
78+
{
79+
$xmlRepresentation = DOMDocumentFactory::fromString($xml);
80+
/** @var \DOMElement $xmlElement */
81+
$xmlElement = $xmlRepresentation->documentElement;
82+
83+
$hexBinary = HexBinaryElement::fromXML($xmlElement);
84+
85+
$this->assertStringContainsString($hexBinary->getRawContent(), $xml);
86+
}
87+
88+
/**
89+
* @return array<string, array{0: string}>
90+
*/
91+
public static function provideHexBinaryCases(): array
92+
{
93+
return [
94+
'inline' => [
95+
<<<XML
96+
<ssp:HexBinaryElement xmlns:ssp="urn:x-simplesamlphp:namespace">3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f</ssp:HexBinaryElement>
97+
XML
98+
,
99+
],
100+
'multiline' => [
101+
<<<XML
102+
<ssp:HexBinaryElement xmlns:ssp="urn:x-simplesamlphp:namespace">
103+
3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f
104+
</ssp:HexBinaryElement>
105+
XML
106+
,
107+
],
108+
];
109+
}
110+
}
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)