Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When C14N fails try comparison without canonicalization #88

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/DOMNodeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,15 @@ public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = f
private function nodeToText(DOMNode $node, bool $canonicalize, bool $ignoreCase): string
{
if ($canonicalize) {
/** @psalm-var string|false $c14n */
$c14n = @$node->C14N();

if ($c14n === false || $c14n === '') { // try node-to-text without canonicalize
return $this->nodeToText($node, false, $ignoreCase);
}

$document = new DOMDocument;
@$document->loadXML($node->C14N());
$document->loadXML($c14n);

$node = $document;
}
Expand Down
60 changes: 60 additions & 0 deletions tests/DOMNodeComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,50 @@ public function assertEqualsFailsProvider()
];
}

public function assertEqualsSucceedsWithNonCanonicalizableNodesProvider()
{
$document = new DOMDocument;

return [
[$document, new DOMDocument],
[$document->createElement('foo'), $document->createElement('foo')],
[
$this->createDOMDocument(
'<!DOCTYPE foo SYSTEM "foo.dtd" [<!ENTITY ns_foo "http://uri.tld/foo">]><i:foo xmlns:i="&ns_foo;"/>'
),
$this->createDOMDocument(
'<!DOCTYPE foo SYSTEM "foo.dtd" [<!ENTITY ns_foo "http://uri.tld/foo">]><i:foo xmlns:i="&ns_foo;"/>'
),
],
];
}

public function assertEqualsFailsWithNonCanonicalizableNodesProvider()
{
// empty document makes C14N return empty string
$document = new DOMDocument;

// nodes created but not appended to the document makes C14N return empty string
$nodeFoo = $document->createElement('foo');
$nodeBar = $document->createElement('bar');

// documents with xmlns definitions to xml entities makes C14N return false
$documentNsUriIsEntityFoo = $this->createDOMDocument( // root element is i:foo
'<!DOCTYPE foo SYSTEM "foo.dtd" [<!ENTITY ns_foo "http://uri.tld/foo">]><i:foo xmlns:i="&ns_foo;"/>'
);
$documentNsUriIsEntityBar = $this->createDOMDocument( // root element is i:bar
'<!DOCTYPE foo SYSTEM "foo.dtd" [<!ENTITY ns_foo "http://uri.tld/foo">]><i:bar xmlns:i="&ns_foo;"/>'
);

return [
[$document, $nodeFoo],
[$document, $documentNsUriIsEntityFoo],
[$nodeFoo, $nodeBar],
[$nodeFoo, $documentNsUriIsEntityFoo],
[$documentNsUriIsEntityFoo, $documentNsUriIsEntityBar],
];
}

/**
* @dataProvider acceptsSucceedsProvider
*/
Expand Down Expand Up @@ -169,6 +213,22 @@ public function testAssertEqualsFails($expected, $actual): void
$this->comparator->assertEquals($expected, $actual);
}

/**
* @dataProvider assertEqualsSucceedsWithNonCanonicalizableNodesProvider
*/
public function testAssertEqualsSucceedsWithNonCanonicalizableNodes($expected, $actual): void
{
$this->testAssertEqualsSucceeds($expected, $actual);
}

/**
* @dataProvider assertEqualsFailsWithNonCanonicalizableNodesProvider
*/
public function testAssertEqualsFailsWithNonCanonicalizableNodes($expected, $actual): void
{
$this->testAssertEqualsFails($expected, $actual);
}

private function createDOMDocument($content)
{
$document = new DOMDocument;
Expand Down