| 1 |
# -*- cperl -*- |
| 2 |
# $Id$ |
| 3 |
|
| 4 |
## |
| 5 |
# these testcases are for xml canonization interfaces. |
| 6 |
# |
| 7 |
|
| 8 |
# should be 23. |
| 9 |
use Test::More tests => 23; |
| 10 |
use strict; |
| 11 |
use warnings; |
| 12 |
|
| 13 |
use XML::LibXML; |
| 14 |
use XML::LibXML::Common qw(:libxml); |
| 15 |
|
| 16 |
my $parser = XML::LibXML->new; |
| 17 |
|
| 18 |
{ |
| 19 |
my $doc = $parser->parse_string( "<a><b/> <c/> <!-- d --> </a>" ); |
| 20 |
|
| 21 |
my $c14n_res = $doc->toStringC14N(); |
| 22 |
# TEST |
| 23 |
is( $c14n_res, "<a><b></b> <c></c> </a>", ' TODO : Add test name' ); |
| 24 |
|
| 25 |
$c14n_res = $doc->toStringC14N(1); |
| 26 |
# TEST |
| 27 |
is( $c14n_res, "<a><b></b> <c></c> <!-- d --> </a>", ' TODO : Add test name' ); |
| 28 |
} |
| 29 |
|
| 30 |
{ |
| 31 |
my $doc = $parser->parse_string( '<a><b/><![CDATA[ >e&f<]]><!-- d --> </a>' ); |
| 32 |
|
| 33 |
my $c14n_res = $doc->toStringC14N(); |
| 34 |
# TEST |
| 35 |
is( $c14n_res, '<a><b></b> >e&f< </a>', ' TODO : Add test name' ); |
| 36 |
$c14n_res = $doc->toStringC14N(1); |
| 37 |
# TEST |
| 38 |
is( $c14n_res, '<a><b></b> >e&f<<!-- d --> </a>', ' TODO : Add test name' ); |
| 39 |
} |
| 40 |
|
| 41 |
{ |
| 42 |
my $doc = $parser->parse_string( '<a a="foo"/>' ); |
| 43 |
|
| 44 |
my $c14n_res; |
| 45 |
$c14n_res = $doc->toStringC14N(0); |
| 46 |
# TEST |
| 47 |
is( $c14n_res, '<a a="foo"></a>', ' TODO : Add test name' ); |
| 48 |
} |
| 49 |
|
| 50 |
{ |
| 51 |
my $doc = $parser->parse_string( '<b:a xmlns:b="http://foo"/>' ); |
| 52 |
|
| 53 |
my $c14n_res; |
| 54 |
$c14n_res = $doc->toStringC14N(0); |
| 55 |
# TEST |
| 56 |
is( $c14n_res, '<b:a xmlns:b="http://foo"></b:a>', ' TODO : Add test name' ); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
# ----------------------------------------------------------------- # |
| 61 |
# The C14N says: remove unused namespaces, libxml2 just orders them |
| 62 |
# ----------------------------------------------------------------- # |
| 63 |
{ |
| 64 |
my $doc = $parser->parse_string( '<b:a xmlns:b="http://foo" xmlns:a="xml://bar"/>' ); |
| 65 |
|
| 66 |
my $c14n_res; |
| 67 |
$c14n_res = $doc->toStringC14N(0); |
| 68 |
# TEST |
| 69 |
is( $c14n_res, '<b:a xmlns:a="xml://bar" xmlns:b="http://foo"></b:a>', ' TODO : Add test name' ); |
| 70 |
|
| 71 |
# would be correct, but will not work. |
| 72 |
# ok( $c14n_res, '<b:a xmlns:b="http://foo"></b:a>' ); |
| 73 |
} |
| 74 |
|
| 75 |
# ----------------------------------------------------------------- # |
| 76 |
# The C14N says: remove redundant namespaces |
| 77 |
# ----------------------------------------------------------------- # |
| 78 |
{ |
| 79 |
my $doc = $parser->parse_string( '<b:a xmlns:b="http://foo"><b:b xmlns:b="http://foo"/></b:a>' ); |
| 80 |
|
| 81 |
my $c14n_res; |
| 82 |
$c14n_res = $doc->toStringC14N(0); |
| 83 |
# TEST |
| 84 |
is( $c14n_res, '<b:a xmlns:b="http://foo"><b:b></b:b></b:a>', ' TODO : Add test name' ); |
| 85 |
} |
| 86 |
|
| 87 |
{ |
| 88 |
my $doc = $parser->parse_string( '<a xmlns="xml://foo"/>' ); |
| 89 |
|
| 90 |
my $c14n_res; |
| 91 |
$c14n_res = $doc->toStringC14N(0); |
| 92 |
# TEST |
| 93 |
is( $c14n_res, '<a xmlns="xml://foo"></a>', ' TODO : Add test name' ); |
| 94 |
} |
| 95 |
|
| 96 |
{ |
| 97 |
my $doc = $parser->parse_string( <<EOX ); |
| 98 |
<?xml version="1.0" encoding="iso-8859-1"?> |
| 99 |
<a><b/></a> |
| 100 |
EOX |
| 101 |
|
| 102 |
my $c14n_res; |
| 103 |
$c14n_res = $doc->toStringC14N(0); |
| 104 |
# TEST |
| 105 |
is( $c14n_res, '<a><b></b></a>', ' TODO : Add test name' ); |
| 106 |
} |
| 107 |
|
| 108 |
# canonize with xpath expressions |
| 109 |
{ |
| 110 |
my $doc = $parser->parse_string( <<EOX ); |
| 111 |
<?xml version="1.0" encoding="iso-8859-1"?> |
| 112 |
<a><b><c/><d/></b></a> |
| 113 |
EOX |
| 114 |
my $c14n_res; |
| 115 |
$c14n_res = $doc->toStringC14N(0, "//d" ); |
| 116 |
# TEST |
| 117 |
is( $c14n_res, '<d></d>', ' TODO : Add test name' ); |
| 118 |
} |
| 119 |
|
| 120 |
{ |
| 121 |
my $doc = $parser->parse_string( <<EOX ); |
| 122 |
<?xml version="1.0" encoding="iso-8859-1"?> |
| 123 |
<a xmlns="http://foo/test#"><b><c/><d><e/></d></b></a> |
| 124 |
EOX |
| 125 |
my $rootnode=$doc->documentElement; |
| 126 |
my $c14n_res; |
| 127 |
$c14n_res = $rootnode->toStringC14N(0, "//*[local-name()='d']"); |
| 128 |
# TEST |
| 129 |
is( $c14n_res, '<d></d>', ' TODO : Add test name' ); |
| 130 |
($rootnode) = $doc->findnodes("//*[local-name()='d']"); |
| 131 |
$c14n_res = $rootnode->toStringC14N(); |
| 132 |
# TEST |
| 133 |
is( $c14n_res, '<d xmlns="http://foo/test#"><e></e></d>', ' TODO : Add test name' ); |
| 134 |
$rootnode = $doc->documentElement->firstChild; |
| 135 |
$c14n_res = $rootnode->toStringC14N(0); |
| 136 |
# TEST |
| 137 |
is( $c14n_res, '<b xmlns="http://foo/test#"><c></c><d><e></e></d></b>', ' TODO : Add test name' ); |
| 138 |
} |
| 139 |
|
| 140 |
# exclusive canonicalization |
| 141 |
|
| 142 |
if (20620 > XML::LibXML::LIBXML_VERSION) { |
| 143 |
skip("skipping Exclusive C14N tests for libxml2 < 2.6.17") for 15..20; |
| 144 |
} else { |
| 145 |
my $xml1 = <<EOX; |
| 146 |
<n0:local xmlns:n0="http://something.org" xmlns:n3="ftp://example.org"> |
| 147 |
<n1:elem2 xmlns:n1="http://example.net" xml:lang="en"> |
| 148 |
<n3:stuff xmlns:n3="ftp://example.org"/> |
| 149 |
</n1:elem2> |
| 150 |
</n0:local> |
| 151 |
EOX |
| 152 |
|
| 153 |
my $xml2 = <<EOX; |
| 154 |
<n2:pdu xmlns:n1="http://example.com" |
| 155 |
xmlns:n2="http://foo.example" |
| 156 |
xml:lang="fr" |
| 157 |
xml:space="preserve"> |
| 158 |
<n1:elem2 xmlns:n1="http://example.net" xml:lang="en"> |
| 159 |
<n3:stuff xmlns:n3="ftp://example.org"/> |
| 160 |
</n1:elem2> |
| 161 |
</n2:pdu> |
| 162 |
EOX |
| 163 |
my $xpath = "(//. | //@* | //namespace::*)[ancestor-or-self::*[name()='n1:elem2']]"; |
| 164 |
my $result = qq(<n1:elem2 xmlns:n1="http://example.net" xml:lang="en">\n <n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>\n </n1:elem2>); |
| 165 |
my $result_n0n2 = qq(<n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xml:lang="en">\n <n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>\n </n1:elem2>); |
| 166 |
my $doc1 = $parser->parse_string( $xml1 ); |
| 167 |
my $doc2 = $parser->parse_string( $xml2 ); |
| 168 |
|
| 169 |
{ |
| 170 |
my $c14n_res = $doc1->toStringEC14N(0, $xpath); |
| 171 |
# TEST |
| 172 |
is( $c14n_res, $result, ' TODO : Add test name'); |
| 173 |
} |
| 174 |
{ |
| 175 |
my $c14n_res = $doc2->toStringEC14N(0, $xpath); |
| 176 |
# TEST |
| 177 |
is( $c14n_res, $result, ' TODO : Add test name'); |
| 178 |
} |
| 179 |
{ |
| 180 |
my $c14n_res = $doc1->toStringEC14N(0, $xpath,[]); |
| 181 |
# TEST |
| 182 |
is( $c14n_res, $result, ' TODO : Add test name'); |
| 183 |
} |
| 184 |
{ |
| 185 |
my $c14n_res = $doc2->toStringEC14N(0, $xpath,[]); |
| 186 |
# TEST |
| 187 |
is( $c14n_res, $result, ' TODO : Add test name'); |
| 188 |
} |
| 189 |
{ |
| 190 |
my $c14n_res = $doc2->toStringEC14N(0, $xpath,['n1','n3']); |
| 191 |
# TEST |
| 192 |
is( $c14n_res, $result, ' TODO : Add test name'); |
| 193 |
} |
| 194 |
{ |
| 195 |
my $c14n_res = $doc2->toStringEC14N(0, $xpath,['n0','n2']); |
| 196 |
# TEST |
| 197 |
is( $c14n_res, $result_n0n2, ' TODO : Add test name'); |
| 198 |
} |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
{ |
| 203 |
|
| 204 |
my $xml = <<'EOF'; |
| 205 |
<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsrl="http://docs.oasis-open.org/wsrf/rl-2" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:Profile="urn:ehealth:profiles:timestamping:1.0" xmlns:tsa="http://www.behealth.be/webservices/tsa" xmlns:urn="urn:oasis:names:tc:dss:1.0:core:schema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsrp="http://docs.oasis-open.org/wsrf/rp-2" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Header><wsa:Action wsu:Id="Action">http://www.behealth.be/webservices/tsa/TSConsultTSBagRequest</wsa:Action><wsa:To wsu:Id="To">https://www.ehealth.fgov.be/timestampauthority_1_5/timestampauthority</wsa:To><wsa:MessageID wsu:Id="MessageID">urn:www.sve.man.ac.uk-54690551758351720271010843310</wsa:MessageID><wsa:ReplyTo wsu:Id="ReplyTo"><wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address></wsa:ReplyTo></soapenv:Header><soapenv:Body wsu:Id="myBody"><TSConsultTSBagRequest xmlns="http://www.behealth.be/webservices/tsa"><tsa:IDHospital>tsa_0406798006_01</tsa:IDHospital><tsa:TSList><tsa:sequenceNumber>80300231753732</tsa:sequenceNumber><tsa:dateTime>1226995312781</tsa:dateTime></tsa:TSList></TSConsultTSBagRequest></soapenv:Body></soapenv:Envelope> |
| 206 |
EOF |
| 207 |
|
| 208 |
my $xpath = q{(//. | //@* | //namespace::*)[ancestor-or-self::x:MessageID]}; |
| 209 |
my $xpath2 = q{(//. | //@* | //namespace::*)[ancestor-or-self::*[local-name()='MessageID' and namespace-uri()='http://www.w3.org/2005/08/addressing']]}; |
| 210 |
|
| 211 |
my $doc = XML::LibXML->load_xml(string=>$xml); |
| 212 |
my $xpc = XML::LibXML::XPathContext->new($doc); |
| 213 |
$xpc->registerNs(x => "http://www.w3.org/2005/08/addressing"); |
| 214 |
my $expect = '<wsa:MessageID xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="MessageID">urn:www.sve.man.ac.uk-54690551758351720271010843310</wsa:MessageID>'; |
| 215 |
# TEST |
| 216 |
|
| 217 |
is( $doc->toStringEC14N( 0, $xpath2, [qw(soap)] ), $expect, ' TODO : Add test name' ); |
| 218 |
# TEST |
| 219 |
|
| 220 |
is( $doc->toStringEC14N( 0, $xpath, $xpc, [qw(soap)] ), $expect, ' TODO : Add test name' ); |
| 221 |
# TEST |
| 222 |
|
| 223 |
is( $doc->toStringEC14N( 0, $xpath2, $xpc, [qw(soap)] ), $expect, ' TODO : Add test name' ); |
| 224 |
|
| 225 |
} |