source: TI12-security/trunk/python/ndg.security.test/ndg/security/test/configfileparsers/test_configfileparsers.py @ 4319

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.test/ndg/security/test/configfileparsers/test_configfileparsers.py@4319
Revision 4319, 4.7 KB checked in by pjkersha, 12 years ago (diff)

Major surgery on ConfigFileParsers? to enable support for nesting of component sections e.g.

[mySection]
sessionManager.credentialWallet.caCertFilePathList=ca/ndg-test-ca.crt

... yields a properties dictionary with:
mySection?sessionManager?credentialWallet?caCertFilePathList?='ca/ndg-test-ca.crt'

XML properties parsing is untested - will add warning for now but may deprecate.

Line 
1#!/usr/bin/env python
2"""Unit tests for Credential Wallet class
3
4NERC Data Grid Project
5"""
6__author__ = "P J Kershaw"
7__date__ = "03/10/08"
8__copyright__ = "(C) 2008 STFC & NERC"
9__license__ = \
10"""This software may be distributed under the terms of the Q Public
11License, version 1.0 or later."""
12__contact__ = "P.J.Kershaw@rl.ac.uk"
13__revision__ = '$Id$'
14
15import unittest
16import os, sys, getpass, re
17import traceback
18
19from ndg.security.common.utils.ConfigFileParsers import \
20    CaseSensitiveConfigParser, INIPropertyFile, readAndValidateProperties
21from ConfigParser import SafeConfigParser
22
23from os.path import expandvars as xpdVars
24from os.path import join as jnPath
25mkPath=lambda file: jnPath(os.environ['NDGSEC_CONFIGFILEPARSERS_UNITTEST_DIR'],
26                           file)
27
28import logging
29logging.basicConfig(level=logging.DEBUG)
30
31
32class ConfigFileParsersTestCase(unittest.TestCase):
33    """Unit test case for ndg.security.common.utils.ConfigFileParsers
34    module.
35    """
36   
37    def setUp(self):
38       
39        if 'NDGSEC_INT_DEBUG' in os.environ:
40            import pdb
41            pdb.set_trace()
42       
43        if 'NDGSEC_CONFIGFILEPARSERS_UNITTEST_DIR' not in os.environ:
44            os.environ['NDGSEC_CONFIGFILEPARSERS_UNITTEST_DIR'] = \
45                os.path.abspath(os.path.dirname(__file__))
46       
47        self.cfg = CaseSensitiveConfigParser()
48        self.configFilePath = mkPath("test.cfg")     
49
50    def test1CaseSensitiveConfigParser(self):
51        caseSensitiveCfg = CaseSensitiveConfigParser()
52        caseSensitiveCfg.read(self.configFilePath)
53       
54        cfg = SafeConfigParser()
55        cfg.read(self.configFilePath)
56        cfgVal = cfg.getboolean('test1CaseSensitiveConfigParser', 
57                                'CaseSensitiveOption')
58        caseSensitiveVal=caseSensitiveCfg.getboolean(
59                                            'test1CaseSensitiveConfigParser', 
60                                            'CaseSensitiveOption')
61        assert(caseSensitiveVal != cfgVal)
62       
63    def test2INIPropertyFile(self):
64        cfgFile = INIPropertyFile()
65        validKeys = {'name': NotImplemented, 'useSSL': NotImplemented,
66                     'attCertLifetime': 2000}
67        prop = cfgFile(self.configFilePath, validKeys,
68                       sections=('test2INIPropertyFile',),
69                       prefix='attributeAuthority')
70        print "properties ..."
71        print prop
72        print "prop['test2INIPropertyFile']['attributeAuthority']['name']=%s"%\
73            prop['test2INIPropertyFile']['attributeAuthority']['name']
74           
75        print("prop['test2INIPropertyFile']['attributeAuthority']['useSSL']"
76              "=%s" % prop['test2INIPropertyFile']['attributeAuthority']
77              ['useSSL'])
78        print("prop['test2INIPropertyFile']['attributeAuthority']"
79              "['attCertLifetime']=%s" % prop['test2INIPropertyFile']
80              ['attributeAuthority']['attCertLifetime'])
81       
82        assert(isinstance(prop['test2INIPropertyFile']['attributeAuthority']
83                          ['attCertLifetime'], float))
84       
85        assert(isinstance(prop['test2INIPropertyFile']['attributeAuthority']
86                          ['useSSL'], bool))
87           
88    def test3ReadAndValidateProperties(self):
89       
90        # keys set to NotImplemented must be present in the config, others
91        # accept defaults as given.  A key set to a populated dict denotes
92        # a subcomponent.
93        validKeys = {
94            'sslCertFile': NotImplemented,
95            'sslKeyFile': NotImplemented,
96            'sslCACertFilePathList': [],
97            'credentialWallet': {
98                'attributeAuthorityURI': 'A DEFAULT VALUE',
99                'caCertFilePathList': [],
100                'mapFromTrustedHosts': False,
101                'attCertRefreshElapse': -1
102            }
103        }
104
105        prop = readAndValidateProperties(self.configFilePath, validKeys,
106                               sections=('test3ReadAndValidateProperties',),
107                               prefix='sessionManager')
108        print "properties ..."
109        print prop
110        assert(prop.keys()==['test3ReadAndValidateProperties'])
111       
112        assert(prop['test3ReadAndValidateProperties']['sessionManager']
113               ['sslCertFile'])
114        assert('credentialWallet' in prop['test3ReadAndValidateProperties']
115               ['sessionManager'])
116       
117        # attributeAuthorityURI is not present in the config so it should be
118        # set to its default value
119        assert(prop['test3ReadAndValidateProperties']['sessionManager']
120            ['credentialWallet']['attributeAuthorityURI']=='A DEFAULT VALUE')
121       
122if __name__ == "__main__":
123    unittest.main()       
Note: See TracBrowser for help on using the repository browser.