import cStringIO
from Ft.Xml.Domlette import Print, CanonicalPrint
from Ft.Xml import Parse
from cStringIO import StringIO
import sha, base64
WSA200403_NS = 'http://schemas.xmlsoap.org/ws/2004/03/addressing'
#Note: c14n spec is at: http://www.w3.org/TR/xml-c14n
#exclusive c14n spec is at: http://www.w3.org/TR/xml-exc-c14n/
C14N_EXCL1_DIGEST = "xSOXT+dlQwo5uT9PbK08of6W9PM="
C14N_EXCL1 = """http://bosshog.lbl.gov:9999/wsrf/services/SecureCounterService10577413"""
C14N_INC1_DIGEST = "qdU4f7/+BeHV/JlVGIPM90fNeV8="
C14N_INC1 = """http://bosshog.lbl.gov:9999/wsrf/services/SecureCounterService10577413"""
C14N_EXCL2_DIGEST = "+IEqF6DRo36Bh93A06S7C4Cmcuo="
C14N_EXCL2 = """http://bosshog.lbl.gov:9999/wsrf/services/SecureCounterService10577413"""
C14N_EXCL3_DIGEST = "VJvTr+Mx3TeWsQY6iwGbhAJ9/eA="
C14N_EXCL3 = """http://131.243.2.147:8888/wsrf/services/DelegationService8adaa710-ba01-11da-bc99-cbed73daa755"""
XML_INST1 = """
m9pihAqIBdcdk7ytDvccj89eWi8=
ofD+Ket5kzR2u/5jWKbFTMtmigk=
SoQ7RlJa3r94weDWBuWAg/BvydQ=
z6sCEkkRJrCuY/C0S5b+46WfyMs=
+IEqF6DRo36Bh93A06S7C4Cmcuo=
NFltkKAJpmMkPbJQj5MW1qVceto=
AAAAAAAAAAMAAAvZTrXlZjRSO7tP12tId+lehprEKgk=
3b1ef410-ab3d-11da-9436-88b687faed94uuid:3d592ca0-ab3d-11da-9436-88b687faed94http://schemas.xmlsoap.org/ws/2004/03/addressing/role/anonymoushttp://counter.com/CounterPortType/addResponsehttp://bosshog.lbl.gov:9999/wsrf/services/SecureCounterService10577413uuid:1141449047.0513"""
def Test(tester):
dom = Parse(XML_INST1)
el = filter(lambda E: (E.namespaceURI, E.localName) == (WSA200403_NS, "From"),
dom.childNodes[0].childNodes[0].childNodes)[0]
tester.startGroup('eclusive c14n (WS-Addressing example)')
tester.startTest('regular c14n')
stream = StringIO()
CanonicalPrint(el, stream, exclusive=False)
cxml = stream.getvalue().strip()
d1 = base64.encodestring(sha.sha(C14N_INC1).digest()).strip()
d2 = base64.encodestring(sha.sha(cxml).digest()).strip()
#tester.compare(d1, d2)
#tester.compare(d1, C14N_INC1_DIGEST)
tester.compare(C14N_INC1, cxml)
tester.testDone()
tester.startTest('exclusive c14n')
stream = StringIO()
CanonicalPrint(el, stream, exclusive=True)
cxml = stream.getvalue().strip()
tester.compare(C14N_EXCL1, cxml)
tester.testDone()
tester.startTest('exclusive c14n with inclusivePrefixes')
stream = StringIO()
CanonicalPrint(el, stream, exclusive=True, inclusivePrefixes=['xsi', 'xsd'])
cxml = stream.getvalue().strip()
tester.compare(C14N_EXCL2, cxml)
tester.testDone()
tester.groupDone()
del dom
del el
return