diff --git a/t/026_do-not-move-namespace-to-parent.t b/t/026_do-not-move-namespace-to-parent.t new file mode 100644 index 0000000..0a93349 --- /dev/null +++ b/t/026_do-not-move-namespace-to-parent.t @@ -0,0 +1,113 @@ +use Test::More; + +use XML::Sig; +use XML::LibXML; + +my $xml = <<'THIRDPARTY'; + +http://oam.convenios.gov.br:14100/oam/fed + + + + +http://oam.convenios.gov.br:14100/oam/fed + +id-KWKgL-WLpVBKBuqBOSa3fJ4Jq-xZLQzbL-t0Y7il + + + + + + +http://localhost:8080/sales-post-sig/ + + + + +ConveniosScheme + + + + +THIRDPARTY + +local $XML::LibXML::skipXMLDeclaration = $self->{ no_xml_declaration }; +my $orig = XML::LibXML->load_xml( string => $xml ); +my $oxc = XML::LibXML::XPathContext->new($orig); +$oxc->registerNs('dsig', 'http://www.w3.org/2000/09/xmldsig#'); +$oxc->registerNs('saml', 'urn:oasis:names:tc:SAML:2.0:assertion'); +$oxc->registerNs('samlp', 'urn:oasis:names:tc:SAML:2.0:protocol'); + +my $uri = qr/http\:\/\/www\.w3\.org\/2000\/09\/xmldsig\#/; + +my $attributes = get_attributes($oxc, '/samlp:Response/saml:Assertion'); +my ($names, $uris) = get_namespaces($attributes); + +ok(!grep ( /dsig/ , @{$names}), 'Did not find dsig in Original Assertion'); +my $p = grep ( $uri, @{$uris}); +ok(!grep ( /$uri/ , @{$uris}), 'Did not find http://www.w3.org/2000/09/xmldsig# in Original Assertion'); + +$attributes = get_attributes($oxc, '/samlp:Response'); +($names, $uris) = get_namespaces($attributes); + +ok(!grep ( /dsig/ , @{$names}), 'Did not find dsig in Original Response'); +my $p = grep ( $uri, @{$uris}); +ok(!grep ( /$uri/ , @{$uris}), 'Did not find http://www.w3.org/2000/09/xmldsig# in Original Response'); + +my $sig = XML::Sig->new( + { + key => 't/rsa.private.key', + cert => 't/rsa.cert.pem', + id_attr => '//saml:Assertion' + }); + +my $signed = $sig->sign($xml); +my $dom = XML::LibXML->load_xml( string => $signed ); + +my $xc = XML::LibXML::XPathContext->new($dom); +$xc->registerNs('dsig', 'http://www.w3.org/2000/09/xmldsig#'); +$xc->registerNs('saml', 'urn:oasis:names:tc:SAML:2.0:assertion'); +$xc->registerNs('samlp', 'urn:oasis:names:tc:SAML:2.0:protocol'); + +$attributes = get_attributes($xc, '/samlp:Response/saml:Assertion'); +($names, $uris) = get_namespaces($attributes); + +ok(!grep ( /dsig/ , @{$names}), 'Did not find dsig in Assertion'); +my $p = grep ( $uri, @{$uris}); +ok(!grep ( /$uri/ , @{$uris}), 'Did not find http://www.w3.org/2000/09/xmldsig# in Assertion'); + +$attributes = get_attributes($xc, '/samlp:Response'); +($names, $uris) = get_namespaces($attributes); + +ok(!grep ( /dsig/ , @{$names}), 'Did not find dsig in Response'); +my $p = grep ( $uri, @{$uris}); +ok(!grep ( /$uri/ , @{$uris}), 'Did not find http://www.w3.org/2000/09/xmldsig# in Response'); + +sub get_attributes { + my $xpc = shift; + my $xpath = shift; + + my $nodes = $xpc->findnodes($xpath); + if ($nodes->size == 0) { + die "Unable to find a samlp:Response"; + } + + my $node = $nodes->get_node(1); + + my @attributes = $node->attributes(); + return \@attributes; +} + +sub get_namespaces { + my $nslist = shift; + my @localnames; + my @uri; + foreach my $ns (@{$nslist}){ + next if (ref $ns ne 'XML::LibXML::Namespace'); + push @localnames, $ns->getLocalName; + push @uri, $ns->getData(); + } + return \@localnames, \@uri; +} + +done_testing;