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

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@4739
Revision 4739, 6.5 KB checked in by pjkersha, 11 years ago (diff)

Refactored x509, xmlsec, XMLSecDoc and combinedservices unit tests separating out test files into the config dir.

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