1
|
|
|
import unittest |
2
|
|
|
|
3
|
|
|
from opcua import Client |
4
|
|
|
from opcua import Server |
5
|
|
|
from opcua import ua |
6
|
|
|
from opcua.crypto import security_policies |
7
|
|
|
|
8
|
|
|
port_num1 = 48515 |
9
|
|
|
port_num2 = 48512 |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class TestCryptoConnect(unittest.TestCase): |
13
|
|
|
|
14
|
|
|
''' |
15
|
|
|
Test connectino with a server supporting crypto |
16
|
|
|
|
17
|
|
|
''' |
18
|
|
|
@classmethod |
19
|
|
|
def setUpClass(self): |
20
|
|
|
# start our own server |
21
|
|
|
self.srv_crypto = Server() |
22
|
|
|
self.uri_crypto = 'opc.tcp://localhost:%d' % port_num1 |
23
|
|
|
self.srv_crypto.set_endpoint(self.uri_crypto) |
24
|
|
|
# load server certificate and private key. This enables endpoints |
25
|
|
|
# with signing and encryption. |
26
|
|
|
self.srv_crypto.load_certificate("examples/certificate-example.der") |
27
|
|
|
self.srv_crypto.load_private_key("examples/private-key-example.pem") |
28
|
|
|
self.srv_crypto.start() |
29
|
|
|
|
30
|
|
|
# start a server without crypto |
31
|
|
|
self.srv_no_crypto = Server() |
32
|
|
|
self.uri_no_crypto = 'opc.tcp://localhost:%d' % port_num2 |
33
|
|
|
self.srv_no_crypto.set_endpoint(self.uri_no_crypto) |
34
|
|
|
self.srv_no_crypto.start() |
35
|
|
|
|
36
|
|
|
@classmethod |
37
|
|
|
def tearDownClass(self): |
38
|
|
|
# stop the server |
39
|
|
|
self.srv_no_crypto.stop() |
40
|
|
|
self.srv_crypto.stop() |
41
|
|
|
|
42
|
|
|
def test_nocrypto(self): |
43
|
|
|
clt = Client(self.uri_no_crypto) |
44
|
|
|
clt.connect() |
45
|
|
|
try: |
46
|
|
|
clt.get_objects_node().get_children() |
47
|
|
|
finally: |
48
|
|
|
clt.disconnect() |
49
|
|
|
|
50
|
|
|
def test_nocrypto_feil(self): |
51
|
|
|
clt = Client(self.uri_no_crypto) |
52
|
|
|
with self.assertRaises(ua.UaError): |
53
|
|
|
clt.set_security_string("Basic256,Sign,examples/certificate-example.der,examples/private-key-example.pem") |
54
|
|
|
|
55
|
|
|
def test_basic256(self): |
56
|
|
|
clt = Client(self.uri_crypto) |
57
|
|
|
try: |
58
|
|
|
clt.set_security_string("Basic256,Sign,examples/certificate-example.der,examples/private-key-example.pem") |
59
|
|
|
clt.connect() |
60
|
|
|
self.assertTrue(clt.get_objects_node().get_children()) |
61
|
|
|
finally: |
62
|
|
|
clt.disconnect() |
63
|
|
|
|
64
|
|
|
def test_basic256_encrypt(self): |
65
|
|
|
clt = Client(self.uri_crypto) |
66
|
|
|
try: |
67
|
|
|
clt.set_security_string("Basic256,SignAndEncrypt,examples/certificate-example.der,examples/private-key-example.pem") |
68
|
|
|
clt.connect() |
69
|
|
|
self.assertTrue(clt.get_objects_node().get_children()) |
70
|
|
|
finally: |
71
|
|
|
clt.disconnect() |
72
|
|
|
|
73
|
|
|
def test_basic128Rsa15(self): |
74
|
|
|
clt = Client(self.uri_crypto) |
75
|
|
|
try: |
76
|
|
|
clt.set_security_string("Basic128Rsa15,Sign,examples/certificate-example.der,examples/private-key-example.pem") |
77
|
|
|
clt.connect() |
78
|
|
|
self.assertTrue(clt.get_objects_node().get_children()) |
79
|
|
|
finally: |
80
|
|
|
clt.disconnect() |
81
|
|
|
|
82
|
|
|
def test_basic128Rsa15_encrypt(self): |
83
|
|
|
clt = Client(self.uri_crypto) |
84
|
|
|
try: |
85
|
|
|
clt.set_security_string("Basic128Rsa15,SignAndEncrypt,examples/certificate-example.der,examples/private-key-example.pem") |
86
|
|
|
clt.connect() |
87
|
|
|
self.assertTrue(clt.get_objects_node().get_children()) |
88
|
|
|
finally: |
89
|
|
|
clt.disconnect() |
90
|
|
|
|
91
|
|
|
def test_basic256_encrypt_success(self): |
92
|
|
|
clt = Client(self.uri_crypto) |
93
|
|
|
try: |
94
|
|
|
clt.set_security(security_policies.SecurityPolicyBasic256, |
95
|
|
|
'examples/certificate-example.der', |
96
|
|
|
'examples/private-key-example.pem', |
97
|
|
|
None, |
98
|
|
|
ua.MessageSecurityMode.SignAndEncrypt |
99
|
|
|
) |
100
|
|
|
clt.connect() |
101
|
|
|
self.assertTrue(clt.get_objects_node().get_children()) |
102
|
|
|
finally: |
103
|
|
|
clt.disconnect() |
104
|
|
|
|
105
|
|
|
def test_basic256_encrypt_feil(self): |
106
|
|
|
# FIXME: how to make it feil??? |
107
|
|
|
clt = Client(self.uri_crypto) |
108
|
|
|
with self.assertRaises(ua.UaError): |
109
|
|
|
clt.set_security(security_policies.SecurityPolicyBasic256, |
110
|
|
|
'examples/certificate-example.der', |
111
|
|
|
'examples/private-key-example.pem', |
112
|
|
|
None, |
113
|
|
|
ua.MessageSecurityMode.None_ |
114
|
|
|
) |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
|
118
|
|
|
|
119
|
|
|
|
120
|
|
|
|