Skip to content

Commit

Permalink
Use XPath to parse large metadata, so we can continue on errors and k…
Browse files Browse the repository at this point in the history
…now which entities have issues
  • Loading branch information
tvdijen committed Jul 23, 2024
1 parent 01b79cb commit 72b572e
Showing 1 changed file with 43 additions and 24 deletions.
67 changes: 43 additions & 24 deletions tests/InterOperability/EntitiesDescriptorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,74 @@
namespace SimpleSAML\Test\SAML2;

use DOMElement;
use Exception;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Assert\AssertionFailedException;
use SimpleSAML\SAML2\Constants as C;
use SimpleSAML\SAML2\Utils\XPath;
use SimpleSAML\SAML2\XML\md\EntitiesDescriptor;
use SimpleSAML\SAML2\XML\md\EntityDescriptor;
use SimpleSAML\XML\DOMDocumentFactory;

use function array_merge;
use function sprintf;

/**
* Class \SimpleSAML\SAML2\EntitiesDescriptorTest
*
* @package simplesamlphp\saml2
*/
final class EntitiesDescriptorTest extends TestCase
{
private int $failures;

/**
* @param boolean $shouldPass
* @param \DOMElement $metadata;
*/
#[DataProvider('provideMetadata')]
public function testUnmarshalling(bool $shouldPass, DOMElement $metadata): void
public function testUnmarshalling(DOMElement $metadata): void
{
// Test for an EntitiesDescriptor
$xpCache = XPath::getXPath($metadata);
$entityDescriptorElements = XPath::xpQuery($metadata, './saml_metadata:EntitiesDescriptor', $xpCache);
$this->failures = 0;

// foreach (
$this->assertCount(1, $entityDescriptorElements);
return;
$this->parseMetadata($metadata);

// Test ordering of AuthnRequest contents
/** @psalm-var \DOMElement[] $authnRequestElements */
$authnRequestElements = XPath::xpQuery(
$authnRequestElement,
'./saml_assertion:Subject/following-sibling::*',
$xpCache,
);
$this->assertEquals(0, $this->failures);

Check failure on line 40 in tests/InterOperability/EntitiesDescriptorTest.php

View workflow job for this annotation

GitHub Actions / Interoperability tests, PHP 8.2, ubuntu-latest

Failed asserting that 6 matches expected 0.

Check failure on line 40 in tests/InterOperability/EntitiesDescriptorTest.php

View workflow job for this annotation

GitHub Actions / Interoperability tests, PHP 8.2, ubuntu-latest

Failed asserting that 1 matches expected 0.

Check failure on line 40 in tests/InterOperability/EntitiesDescriptorTest.php

View workflow job for this annotation

GitHub Actions / Interoperability tests, PHP 8.2, ubuntu-latest

Failed asserting that 6 matches expected 0.

Check failure on line 40 in tests/InterOperability/EntitiesDescriptorTest.php

View workflow job for this annotation

GitHub Actions / Interoperability tests, PHP 8.2, ubuntu-latest

Failed asserting that 1 matches expected 0.
}


/**
*
*/
private function parseMetadata(DOMElement $metadata): void
{
$xpCache = XPath::getXPath($metadata);
if ($metadata->localName === 'EntitiesDescriptor') {
// Test for an EntitiesDescriptor or EntityDescriptor
$entityDescriptorElements = XPath::xpQuery($metadata, './saml_metadata:EntityDescriptor', $xpCache);
$entitiesDescriptorElements = XPath::xpQuery($metadata, './saml_metadata:EntitiesDescriptor', $xpCache);
$descriptors = array_merge($entityDescriptorElements ?? [], $entitiesDescriptorElement ?? []);
foreach ($descriptors as $descriptor) {
$this->parseMetadata($descriptor);
}
} elseif ($metadata->localName === 'EntityDescriptor') {
$entityID = XPath::xpQuery($metadata, './@entityID', $xpCache);

try {
EntityDescriptor::fromXML($metadata);
} catch (Exception $e) {
$this->failures = $this->failures + 1;

try {
EntitiesDescriptor::fromXML($metadata);
$this->assertTrue($shouldPass);
} catch (AssertionFailedException $e) {
fwrite(STDERR, $e->getFile() . '(' . strval($e->getLine()) . '):' . $e->getMessage());
fwrite(STDERR, $e->getTraceAsString());
$this->assertFalse($shouldPass);
echo "Failure: " . $entityID[0]->value . PHP_EOL;
echo " " . $e->getMessage() . PHP_EOL;
ob_flush();
}
} else {
throw new Exception(sprintf(
"Shouldn't happen. Element %s:%s was found.",
$metadata->namespaceURI,
$metadata->localName,
));
}
}

Expand All @@ -63,11 +84,9 @@ public static function provideMetadata(): array
{
return [
'eduGAIN' => [
true,
DOMDocumentFactory::fromFile('/tmp/metadata/edugain.xml')->documentElement,
],
'GRNET' => [
true,
DOMDocumentFactory::fromFile('/tmp/metadata/grnet.xml')->documentElement,
],
];
Expand Down

0 comments on commit 72b572e

Please sign in to comment.