source: TI12-security/trunk/python/MyProxyClient/test/test_myproxyclient.py @ 4621

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/MyProxyClient/test/test_myproxyclient.py@4621
Revision 4621, 7.8 KB checked in by pjkersha, 11 years ago (diff)

Adding unit tests directory from ndg.security.test

  • Property svn:executable set to *
  • Property svn:keywords set to Id
Line 
1#!/usr/bin/env python
2"""NDG MyProxy client unit tests
3
4NERC Data Grid Project
5"""
6__author__ = "P J Kershaw"
7__date__ = "02/07/07"
8__copyright__ = "(C) 2007 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__ = "Philip.Kershaw@stfc.ac.uk"
13__revision__ = '$Id$'
14
15import unittest
16import os
17import sys
18import getpass
19import traceback
20
21from ConfigParser import SafeConfigParser
22from ndg.security.common.myproxy import MyProxyClient
23
24xpdVars = os.path.expandvars
25jnPath = os.path.join
26mkPath = lambda file: jnPath(os.environ['NDGSEC_MYPROXY_UNITTEST_DIR'], file)
27
28class MyProxyClientTestCase(unittest.TestCase):
29   
30    def setUp(self):
31       
32        if 'NDGSEC_INT_DEBUG' in os.environ:
33            import pdb
34            pdb.set_trace()
35       
36        if 'NDGSEC_MYPROXY_UNITTEST_DIR' not in os.environ:
37            os.environ['NDGSEC_MYPROXY_UNITTEST_DIR'] = \
38                os.path.abspath(os.path.dirname(__file__))
39               
40        configParser = SafeConfigParser()
41        configFilePath = jnPath(os.environ['NDGSEC_MYPROXY_UNITTEST_DIR'],
42                                "myProxyClientTest.cfg")
43        configParser.read(configFilePath)
44       
45        self.cfg = {}
46        for section in configParser.sections():
47            self.cfg[section] = dict(configParser.items(section))
48           
49        self.clnt = MyProxyClient(\
50                    propFilePath=xpdVars(self.cfg['setUp']['propfilepath']))
51       
52
53    def test1Store(self):
54        '''test1Store: upload X509 cert and private key to repository'''
55           
56        passphrase = self.cfg['test1Store'].get('passphrase')
57        if passphrase is None:
58            passphrase = getpass.getpass(\
59                                 prompt="\ntest1Store cred. pass-phrase: ")
60           
61        ownerPassphrase = self.cfg['test1Store'].get('ownerpassphrase')
62        if ownerPassphrase is None:
63            ownerPassphrase = getpass.getpass(\
64                              prompt="\ntest1Store cred. owner pass-phrase: ")
65
66        certFile = xpdVars(self.cfg['test1Store']['signingcertfilepath'])
67        keyFile = xpdVars(self.cfg['test1Store']['signingprikeyfilepath'])
68        ownerCertFile = xpdVars(self.cfg['test1Store']['ownercertfile'])
69        ownerKeyFile = xpdVars(self.cfg['test1Store']['ownerkeyfile'])
70           
71        try:
72            self.clnt.store(self.cfg['test1Store']['username'],
73                            passphrase,
74                            certFile,
75                            keyFile,
76                            ownerCertFile=ownerCertFile,
77                            ownerKeyFile=ownerKeyFile,
78                            ownerPassphrase=ownerPassphrase,
79                            force=False)
80            print "Store creds for user %s" % \
81                                            self.cfg['test1Store']['username']
82        except:
83            self.fail(traceback.print_exc())
84   
85   
86    def test2GetDelegation(self):
87        '''test2GetDelegation: retrieve proxy cert./private key'''
88        passphrase = self.cfg['test2GetDelegation'].get('passphrase')
89        if passphrase is None:
90            passphrase = getpass.getpass(\
91                                 prompt="\ntest2GetDelegation pass-phrase: ")
92         
93        try:
94            proxyCertFile = \
95                xpdVars(self.cfg['test2GetDelegation']['proxycertfileout'])
96            proxyKeyFile = \
97                xpdVars(self.cfg['test2GetDelegation']['proxykeyfileout'])
98
99            creds = self.clnt.getDelegation(\
100                                  self.cfg['test2GetDelegation']['username'], 
101                                  passphrase)
102            print "proxy credentials:" 
103            print ''.join(creds)
104            open(proxyCertFile, 'w').write(creds[0]+''.join(creds[2:]))           
105            open(proxyKeyFile, 'w').write(creds[1])
106        except:
107            self.fail(traceback.print_exc())
108
109
110    def test3Info(self):
111        '''test3Info: Retrieve information about a given credential'''
112       
113        # ownerpassphrase can be omitted from the congif file in which case
114        # the get call below would return None
115        ownerPassphrase = self.cfg['test3Info'].get('ownerpassphrase')
116        if ownerPassphrase is None:
117            ownerPassphrase = getpass.getpass(\
118                              prompt="\ntest3Info owner creds pass-phrase: ")
119
120        try:
121            credExists, errorTxt, fields = self.clnt.info(
122                             self.cfg['test3Info']['username'],
123                             xpdVars(self.cfg['test3Info']['ownercertfile']),
124                             xpdVars(self.cfg['test3Info']['ownerkeyfile']),
125                             ownerPassphrase=ownerPassphrase)
126            print "test3Info... "
127            print "credExists: %s" % credExists
128            print "errorTxt: " + errorTxt
129            print "fields: %s" % fields
130        except:
131            self.fail(traceback.print_exc())
132
133
134    def test4ChangePassphrase(self):       
135        """test4ChangePassphrase: change pass-phrase protecting a given
136        credential"""
137        try:
138            passphrase=self.cfg['test4ChangePassphrase'].get('passphrase')
139            if passphrase is None:
140                passphrase = getpass.getpass(\
141                             prompt="test4ChangePassphrase - pass-phrase: ")
142           
143            newPassphrase = \
144                        self.cfg['test4ChangePassphrase'].get('newpassphrase')
145            if newPassphrase is None:
146                newPassphrase = getpass.getpass(\
147                        prompt="test4ChangePassphrase - new pass-phrase: ")
148   
149                confirmNewPassphrase = getpass.getpass(\
150                prompt="test4ChangePassphrase - confirm new pass-phrase: ")
151   
152                if newPassphrase != confirmNewPassphrase:
153                    self.fail("New and confirmed new password don't match")
154                   
155            ownerPassphrase = \
156                self.cfg['test4ChangePassphrase'].get('ownerpassphrase') or \
157                passphrase
158   
159            self.clnt.changePassphrase(
160                self.cfg['test4ChangePassphrase']['username'],
161                passphrase,
162                newPassphrase, 
163                xpdVars(self.cfg['test4ChangePassphrase']['ownercertfile']),
164                xpdVars(self.cfg['test4ChangePassphrase']['ownerkeyfile']),
165                ownerPassphrase=ownerPassphrase)
166            print "Change pass-phrase"
167        except:
168            self.fail(traceback.print_exc())
169
170
171    def test5Destroy(self):
172        '''test5Destroy: destroy credentials for a given user'''
173
174        ownerPassphrase = self.cfg['test5Destroy'].get('ownerpassphrase')
175        if ownerPassphrase is None:
176            ownerPassphrase = getpass.getpass(\
177                          prompt="\ntest5Destroy cred. owner pass-phrase: ")
178
179        try:
180            self.clnt.destroy(self.cfg['test5Destroy']['username'], 
181            ownerCertFile=xpdVars(self.cfg['test5Destroy']['ownercertfile']),
182            ownerKeyFile=xpdVars(self.cfg['test5Destroy']['ownerkeyfile']),
183            ownerPassphrase=ownerPassphrase)
184            print "Destroy creds for user %s" % \
185                                        self.cfg['test5Destroy']['username']
186        except:
187            self.fail(traceback.print_exc())
188       
189 
190#_____________________________________________________________________________       
191class MyProxyClientTestSuite(unittest.TestSuite):
192    def __init__(self):
193        map = map(MyProxyClientTestCase,
194                  (
195                    "test1Store",
196                    "test2GetDelegation",
197                    "test3Info",
198                    "test4ChangePassphrase",
199                    "test5Destroy",
200                  ))
201        unittest.TestSuite.__init__(self, map)
202                                       
203if __name__ == "__main__":
204    unittest.main()
Note: See TracBrowser for help on using the repository browser.