source: TI12-security/trunk/python/ndg.security.test/ndg/security/test/wsSecurity/client/echoClientTest.py @ 3115

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

security/python/ndg.security.test/ndg/security/test/wsSecurity/client: converting into unit test

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2#
3# Exampe echo client, to show extended code generation in ZSI
4#
5# Import the client proxy object
6from EchoServer_client import EchoServerSOAP
7import sys
8
9import wsSecurity
10
11import unittest
12import os
13import sys
14import getpass
15import traceback
16
17from ConfigParser import SafeConfigParser
18from ndg.security.common import wsSecurity
19
20class WSSecurityClientTestCase(unittest.TestCase):
21   
22    def setUp(self):
23       
24        configParser = SafeConfigParser()
25        configParser.read("./myProxyClientTest.cfg")
26       
27        self.cfg = {}
28        for section in configParser.sections():
29            self.cfg[section] = dict(configParser.items(section))
30           
31        self.clnt = WSSecurityClient(\
32                          propFilePath=self.cfg['setUp']['propfilepath'])
33       
34
35    def test1Echo(self):
36        '''test1Echo: test signed message and signed response from server'''
37           
38        passphrase = self.cfg['test1Echo'].get('passphrase')
39        if passphrase is None:
40            passphrase = getpass.getpass(\
41                                 prompt="\ntest1Echo cred. pass-phrase: ")
42           
43        ownerPassphrase = self.cfg['test1Echo'].get('ownerpassphrase')
44        if ownerPassphrase is None:
45            ownerPassphrase = getpass.getpass(\
46                              prompt="\ntest1Echo cred. owner pass-phrase: ")
47           
48        try:
49            self.clnt.store(self.cfg['test1Echo']['username'],
50                        passphrase,
51                        self.cfg['test1Echo']['certfile'],
52                        self.cfg['test1Echo']['keyfile'],
53                        ownerCertFile=self.cfg['test1Echo']['ownercertfile'],
54                        ownerKeyFile=self.cfg['test1Echo']['ownerkeyfile'],
55                        ownerPassphrase=ownerPassphrase,
56                        force=False)
57            print "Store creds for user %s" % \
58                                            self.cfg['test1Echo']['username']
59        except:
60            self.fail(traceback.print_exc())
61           
62#_____________________________________________________________________________       
63class WSSecurityClientTestSuite(unittest.TestSuite):
64    def __init__(self):
65        map = map(WSSecurityClientTestCase,
66                  (
67                    "test1Echo",
68                  ))
69        unittest.TestSuite.__init__(self, map)
70                                       
71if __name__ == "__main__":
72    unittest.main()
73# Lambda used by WS-Security handler to check which operation has been
74# invoked
75isEchoRequest = lambda sw: sw.body.node.childNodes[0].localName=='Echo'
76isEchoEncrRequest = lambda sw: sw.body.node.childNodes[0].localName=='EchoEncr'
77
78isEchoResponse = lambda ps: \
79ps.dom.childNodes[1].childNodes[1].childNodes[0].localName == 'EchoResponse'
80isEchoEncrResponse = lambda ps: \
81ps.dom.childNodes[1].childNodes[1].childNodes[0].localName=='EchoEncrResponse'
82
83class WSSEhandler:
84    def __init__(self, sigHandler=None, encrHandler=None):
85        self.sigHandler = sigHandler
86        self.encrHandler = encrHandler
87       
88    def sign(self, sw):
89        if self.sigHandler:
90            self.sigHandler.sign(sw)
91           
92    def verify(self, ps):
93        if self.sigHandler:
94            self.sigHandler.verify(ps)
95       
96    def encrypt(self, sw):
97        if self.encrHandler:
98            self.encrHandler.encrypt(sw)
99           
100    def decrypt(self, ps):
101        if self.encrHandler:
102            self.encrHandler.decrypt(ps)
103
104
105#priKeyPwd = open('../tmp2').read().strip()
106#certFilePath = '../Junk-cert.pem'
107#priKeyFilePath = '../Junk-key.pem'
108priKeyPwd = None
109certFilePath = '../webSphereTestcert.pem'
110priKeyFilePath = '../webSphereTestkey.pem'
111
112# Signature handler object is passed to binding
113sigHandler = wsSecurity.SignatureHandler(certFilePath=certFilePath,
114                                         priKeyFilePath=priKeyFilePath,
115                                         priKeyPwd=priKeyPwd)
116
117encrHandler = wsSecurity.EncryptionHandler(certFilePath=certFilePath,
118                                           priKeyFilePath=priKeyFilePath,
119                                           priKeyPwd=priKeyPwd)
120
121# Test encryption only
122#wsseHandler = WSSEhandler(sigHandler, encrHandler)
123wsseHandler = WSSEhandler(encrHandler=encrHandler)
124   
125       
126# Instantiate a client proxy object, then call it
127#wsURL = "http://192.100.78.234:9081/EchoServiceWeb/services/EchoServer"
128wsURL = "http://localhost:7000"
129echoSrv = EchoServerSOAP(wsURL,
130                         sig_handler=sigHandler,
131                         encr_handler=encrHandler,
132                         tracefile=sys.stdout)
133try:
134    #import pdb;pdb.set_trace()
135    print echoSrv.Echo("Test String")
136    #print echoSrv.EchoEncr("Test Secret")
137except Exception, e:
138    print "Failed to echo: ", e
139
Note: See TracBrowser for help on using the repository browser.