1 | #!/usr/bin/env python |
---|
2 | """WS-Security Digital Signature unit tests |
---|
3 | |
---|
4 | NERC DataGrid Project |
---|
5 | """ |
---|
6 | __author__ = "P J Kershaw" |
---|
7 | __date__ = "13/12/06" |
---|
8 | __copyright__ = "(C) 2009 Science and Technology Facilities Council" |
---|
9 | __license__ = "BSD - see LICENSE file in top-level directory" |
---|
10 | __contact__ = "Philip.Kershaw@stfc.ac.uk" |
---|
11 | __revision__ = '$Id: $' |
---|
12 | import logging |
---|
13 | logging.basicConfig(level=logging.DEBUG) |
---|
14 | |
---|
15 | import unittest |
---|
16 | import os |
---|
17 | import sys |
---|
18 | |
---|
19 | from os.path import join, dirname, abspath |
---|
20 | mkPath = lambda file: join(os.environ['NDGSEC_WSSECLNT_UNITTEST_DIR'], file) |
---|
21 | |
---|
22 | from ConfigParser import SafeConfigParser |
---|
23 | |
---|
24 | from EchoService_services import EchoServiceLocator |
---|
25 | |
---|
26 | from ndg.wssecurity.common.signaturehandler.foursuite import SignatureHandler |
---|
27 | from ndg.wssecurity.common.signaturehandler import (NoSignatureFound, |
---|
28 | TimestampError) |
---|
29 | from ndg.wssecurity.common.utils import DomletteReader, DomletteElementProxy |
---|
30 | |
---|
31 | |
---|
32 | class EchoClientTestCase(unittest.TestCase): |
---|
33 | |
---|
34 | def setUp(self): |
---|
35 | super(EchoClientTestCase, self).setUp() |
---|
36 | |
---|
37 | if 'NDGSEC_INT_DEBUG' in os.environ: |
---|
38 | import pdb |
---|
39 | pdb.set_trace() |
---|
40 | |
---|
41 | if 'NDGSEC_WSSECLNT_UNITTEST_DIR' not in os.environ: |
---|
42 | os.environ['NDGSEC_WSSECLNT_UNITTEST_DIR'] = \ |
---|
43 | abspath(dirname(__file__)) |
---|
44 | |
---|
45 | if 'NDGSEC_TEST_CONFIG_DIR' not in os.environ: |
---|
46 | os.environ['NDGSEC_TEST_CONFIG_DIR'] = \ |
---|
47 | abspath(join(dirname(dirname(dirname(dirname(__file__)))), |
---|
48 | 'config')) |
---|
49 | |
---|
50 | configFilePath = mkPath('echoClientTest.cfg') |
---|
51 | self.cfg = SafeConfigParser() |
---|
52 | self.cfg.read(configFilePath) |
---|
53 | uri = self.cfg.get('setUp', 'uri') |
---|
54 | |
---|
55 | # Signature handler object is passed to binding |
---|
56 | sigHandler = SignatureHandler(cfg=configFilePath, |
---|
57 | cfgFileSection='setUp') |
---|
58 | |
---|
59 | locator = EchoServiceLocator() |
---|
60 | self.clnt = locator.getEcho(uri, |
---|
61 | readerclass=DomletteReader, |
---|
62 | writerclass=DomletteElementProxy, |
---|
63 | sig_handler=sigHandler, |
---|
64 | tracefile=sys.stderr) |
---|
65 | |
---|
66 | |
---|
67 | def test01Echo(self): |
---|
68 | |
---|
69 | resp = self.clnt.Echo("Hello from client") |
---|
70 | print "Message returned was: %s" % resp |
---|
71 | |
---|
72 | |
---|
73 | def test02ServerRaiseMissingTimestampError(self): |
---|
74 | # Get server to catch that no timestamp was provided |
---|
75 | |
---|
76 | self.clnt.binding.sig_handler.addTimestamp = False |
---|
77 | try: |
---|
78 | resp = self.clnt.Echo("Hello again from client") |
---|
79 | |
---|
80 | except NoSignatureFound: |
---|
81 | print("PASSED - server rejected client message with no timestamp") |
---|
82 | else: |
---|
83 | self.fail("Expecting error from server because client didn't set " |
---|
84 | "a timestamp element") |
---|
85 | |
---|
86 | def test03ClientRaiseTimestampError(self): |
---|
87 | # Get client to catch a mismatch in the created time for the server |
---|
88 | # response by adding a clock skew to the client |
---|
89 | |
---|
90 | self.clnt.binding.sig_handler.timestampClockSkew = -300.0 |
---|
91 | try: |
---|
92 | resp = self.clnt.Echo("Hello again from client") |
---|
93 | |
---|
94 | except TimestampError: |
---|
95 | print "PASSED - client rejected server message created timestamp" |
---|
96 | else: |
---|
97 | self.fail("Expecting error from client because client set a " |
---|
98 | "a timestamp clock skew") |
---|
99 | |
---|
100 | if __name__ == "__main__": |
---|
101 | unittest.main() |
---|