source: TI12-security/trunk/python/ndg.security.test/ndg/security/test/xmlsec/etree/test_etree.py @ 4680

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.test/ndg/security/test/xmlsec/etree/test_etree.py@4680
Revision 4680, 6.4 KB checked in by pjkersha, 11 years ago (diff)

Global replace to fix copyright from STFC & NERC to STFC alone because it's not possible to have copyright held by two orgs.

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2"""NDG XML Security unit tests for ElementTree based implementation
3
4NERC Data Grid Project
5"""
6__author__ = "P J Kershaw"
7__date__ = "03/01/09"
8__copyright__ = "(C) 2008 STFC"
9__license__ = \
10"""This software may be distributed under the terms of the Q Public
11License, version 1.0 or later."""
12__contact__ = "Philip.Kershaw@stfc.ac.uk"
13__revision__ = '$Id$'
14
15import unittest
16import os
17import sys
18import getpass
19import traceback
20import logging
21logging.basicConfig(level=logging.DEBUG)
22log = logging.getLogger(__name__)
23
24from ConfigParser import SafeConfigParser
25from ndg.security.common.xmlsec.etree import XMLSecDoc
26
27from os.path import expandvars as xpdVars
28from os.path import join as jnPath
29mkPath=lambda file: jnPath(os.environ['NDGSEC_XMLSEC_ETREE_UNITTEST_DIR'],file)
30
31class XMLSecDocTestCase(unittest.TestCase):
32   
33    def setUp(self):
34       
35        if 'NDGSEC_INT_DEBUG' in os.environ:
36            import pdb
37            pdb.set_trace()
38       
39        if 'NDGSEC_XMLSEC_ETREE_UNITTEST_DIR' not in os.environ:
40            os.environ['NDGSEC_XMLSEC_ETREE_UNITTEST_DIR'] = \
41                os.path.abspath(os.path.dirname(__file__))
42       
43        configParser = SafeConfigParser()
44        configFilePath = jnPath(os.environ['NDGSEC_XMLSEC_ETREE_UNITTEST_DIR'],
45                                "etree.cfg")
46        configParser.read(configFilePath)
47       
48        self.cfg = {}
49        for section in configParser.sections():
50            self.cfg[section] = dict(configParser.items(section))
51
52        self.strXML = """<saml:Assertion
53xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion"
54Issuer="http://badc.nerc.ac.uk"
55MajorVersion="1"
56MinorVersion="1">
57    <saml:Conditions NotAfter="y" NotBefore="x"/>
58    <saml:AuthenticationStatement
59    AuthenticationInstant="..." AuthenticationMethod="...">
60        <saml:Subject></saml:Subject>
61    </saml:AuthenticationStatement>
62    <saml:AttributeStatement>
63        <saml:Subject></saml:Subject>
64        <saml:Attribute
65        AttributeName="urn:mace:dir:attribute-def:eduPersonAffiliation"
66        AttributeNamespace="urn:mace:shibboleth:1.0:attributeNamespace:uri">
67            <saml:AttributeValue>member</saml:AttributeValue>
68            <saml:AttributeValue>student</saml:AttributeValue>
69        </saml:Attribute>
70    </saml:AttributeStatement>
71</saml:Assertion>
72"""         
73        self.xmlSecDoc = XMLSecDoc()
74       
75    def test1Parse(self):
76        '''test1Parse: parse an XML document'''
77           
78        self.xmlSecDoc.parse(self.strXML)
79
80
81    def test2SignWithInclC14N(self): 
82        '''test2SignWithInclC14N: sign document using inclusive
83        Canonicalization'''
84           
85        self.xmlSecDoc.filePath = \
86                    xpdVars(self.cfg['test2SignWithInclC14N']['filepath'])
87        self.xmlSecDoc.certFilePathList = \
88                    xpdVars(self.cfg['test2SignWithInclC14N']['signingcertfilepath'])
89        self.xmlSecDoc.signingKeyFilePath = \
90                    xpdVars(self.cfg['test2SignWithInclC14N']['signingprikeyfilepath'])
91
92        keyPwd = self.cfg['test2SignWithInclC14N'].get('signingprikeypwd')
93        if keyPwd is None:
94            self.xmlSecDoc.signingKeyPwd = getpass.getpass(prompt=\
95                                                "\ntest2SignWithInclC14N "
96                                                "private key password: ")
97       
98        self.xmlSecDoc.applyEnvelopedSignature(xmlTxt=self.strXML)
99        self.xmlSecDoc.write()
100   
101    def test3SignWithExclC14N(self): 
102        '''test3SignWithExclC14N: sign document using exclusive
103        Canonicalization'''
104           
105        self.xmlSecDoc.filePath = \
106                    xpdVars(self.cfg['test3SignWithExclC14N']['filepath'])
107        self.xmlSecDoc.certFilePathList = \
108                    xpdVars(self.cfg['test3SignWithExclC14N']['signingcertfilepath'])
109        self.xmlSecDoc.signingKeyFilePath = \
110                    xpdVars(self.cfg['test3SignWithExclC14N']['keyfile'])
111
112        keyPwd = self.cfg['test3SignWithExclC14N'].get('signingprikeypwd')
113        if keyPwd is None:
114            self.xmlSecDoc.signingKeyPwd = getpass.getpass(prompt=\
115                                            "\ntest3SignWithExclC14N "
116                                            "private key password: ")
117       
118        self.xmlSecDoc.applyEnvelopedSignature(xmlTxt=self.strXML,
119                                       refC14nKw=dict(exclusive=True),
120                                       signedInfoC14nKw=dict(exclusive=True))
121        self.xmlSecDoc.write() 
122       
123         
124    def test4Write(self):
125        '''test4Write: write document'''
126           
127        self.test1Parse()
128        self.xmlSecDoc.filePath = xpdVars(self.cfg['test4Write']['filepath'])
129        self.xmlSecDoc.write()
130
131       
132    def test5Read(self):
133        '''test5Read: read document'''
134           
135        self.xmlSecDoc.filePath = xpdVars(self.cfg['test5Read']['filepath'])
136        self.xmlSecDoc.read()
137
138
139    def test6VerifyInclC14nDoc(self):
140        '''test6VerifyInclC14nDoc: check signature of XML document'''
141           
142        self.xmlSecDoc.filePath = \
143            xpdVars(self.cfg['test6VerifyInclC14nDoc']['filepath'])
144        self.xmlSecDoc.certFilePathList = \
145        xpdVars(self.cfg['test6VerifyInclC14nDoc']['certfilepathlist']).split()
146       
147        self.xmlSecDoc.read()
148        self.xmlSecDoc.verifyEnvelopedSignature()
149
150
151    def test7VerifyExclC14nDoc(self):
152        '''test7VerifyExclC14nDoc: check signature of XML document'''
153           
154        self.xmlSecDoc.filePath = \
155            xpdVars(self.cfg['test7VerifyExclC14nDoc']['filepath'])
156        self.xmlSecDoc.certFilePathList = \
157        xpdVars(self.cfg['test7VerifyExclC14nDoc']['certfilepathlist']).split()
158       
159        self.xmlSecDoc.read()
160        self.xmlSecDoc.verifyEnvelopedSignature()
161       
162 
163#_____________________________________________________________________________       
164class XMLSecDocTestSuite(unittest.TestSuite):
165    def __init__(self):
166        map = map(XMLSecDocTestCase,
167                  (
168                   "test1Parse",
169                   "test2SignWithInclC14N", 
170                   "test3SignWithExclC14N",
171                   "test4Write", 
172                   "test5Read", 
173                   "test6VerifyInclC14nDoc",
174                   "test7VerifyExclC14nDoc"
175                  ))
176        unittest.TestSuite.__init__(self, map)
177                                       
178if __name__ == "__main__":
179    unittest.main()
Note: See TracBrowser for help on using the repository browser.