source: TI12-security/trunk/python/Tests/joachim/test.py @ 3498

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/Tests/joachim/test.py@4692
Revision 3498, 2.3 KB checked in by pjkersha, 12 years ago (diff)

Added comparison tests for inclusive/exclusive C14N + with/without inclusive
namespace prefixes.

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2
3from ZSI.wstools.Namespaces import DSIG, ENCRYPTION, OASIS, WSU, WSA200403, \
4                                   SOAP, SCHEMA # last included for xsi
5
6# Canonicalization
7from ZSI.wstools.c14n import Canonicalize
8
9# Include for re-parsing doc ready for canonicalization in sign method - see
10# associated note
11from xml.dom.ext.reader.PyExpat import Reader
12from xml.xpath.Context import Context
13from xml import xpath
14#import Ft.Xml.XPath as xpath
15
16def test():
17   
18    inf = open('./ChargeAmountReq_unpatched.xml')
19    xml = inf.read()
20   
21    # Namespaces for XPath searches
22    processorNss = \
23    {
24        'wsu':    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd",
25    }
26
27    docNode = Reader().fromString(xml)
28    ctxt = Context(docNode, processorNss=processorNss)
29    refNodes = xpath.Evaluate('//*[@wsu:Id]', 
30                              contextNode=docNode, 
31                              context=ctxt)
32    for refNode in refNodes:
33        # Get ref node and all it's children
34        refSubsetList = getChildNodes(refNode, [refNode])
35        refExclC14nWithPfx = Canonicalize(docNode, None, subset=refSubsetList,
36                               unsuppressedPrefixes=['SOAP-ENV', 'SOAP-ENC',
37                               'ZSI', 'ns1', 'wsu', 'xsd', 'xsi', 'ds', 'ec'])
38        refExclC14n = Canonicalize(docNode, None, subset=refSubsetList,
39                               unsuppressedPrefixes=[])
40        refInclC14n1 = Canonicalize(docNode, None, subset=refSubsetList)
41        refInclC14n2 = Canonicalize(refNode)
42
43        # For inclusive C14N either form of call to Canonicalize above will
44        # give the same result:
45        assert(refInclC14n1==refInclC14n2)
46
47        # Expected to be different as Exclusive omits surperfluous ancestor
48        # namespace declarations
49        assert(refExclC14n != refInclC14n1)
50
51        # If the right namespace declarations are explicitly included, Exclusive
52        # C14N gives the same result as inclusive C14N
53        assert(refInclC14n2==refExclC14nWithPfx)
54
55def getChildNodes(node, nodeList=None):
56
57    if node.attributes is not None:
58        nodeList += node.attributes.values() 
59    nodeList += node.childNodes
60    for childNode in node.childNodes:
61        getChildNodes(childNode, nodeList)
62    return nodeList
63
64if __name__ == "__main__":
65    test()
Note: See TracBrowser for help on using the repository browser.