tests_crypto_connect.py
Go to the documentation of this file.
1 import unittest
2 
3 from opcua import Client
4 from opcua import Server
5 from opcua import ua
6 
7 try:
8  from opcua.crypto import uacrypto
9  from opcua.crypto import security_policies
10 except ImportError:
11  print("WARNING: CRYPTO NOT AVAILABLE, CRYPTO TESTS DISABLED!!")
12  disable_crypto_tests = True
13 else:
14  disable_crypto_tests = False
15 
16 
17 port_num1 = 48515
18 port_num2 = 48512
19 
20 @unittest.skipIf(disable_crypto_tests, "crypto not available")
21 class TestCryptoConnect(unittest.TestCase):
22 
23  '''
24  Test connectino with a server supporting crypto
25 
26  '''
27  @classmethod
28  def setUpClass(cls):
29  # start our own server
30  cls.srv_crypto = Server()
31  cls.uri_crypto = 'opc.tcp://localhost:{0:d}'.format(port_num1)
32  cls.srv_crypto.set_endpoint(cls.uri_crypto)
33  # load server certificate and private key. This enables endpoints
34  # with signing and encryption.
35  cls.srv_crypto.load_certificate("examples/certificate-example.der")
36  cls.srv_crypto.load_private_key("examples/private-key-example.pem")
37  cls.srv_crypto.start()
38 
39  # start a server without crypto
40  cls.srv_no_crypto = Server()
41  cls.uri_no_crypto = 'opc.tcp://localhost:{0:d}'.format(port_num2)
42  cls.srv_no_crypto.set_endpoint(cls.uri_no_crypto)
43  cls.srv_no_crypto.start()
44 
45  @classmethod
46  def tearDownClass(cls):
47  # stop the server
48  cls.srv_no_crypto.stop()
49  cls.srv_crypto.stop()
50 
51  def test_nocrypto(self):
52  clt = Client(self.uri_no_crypto)
53  clt.connect()
54  try:
55  clt.get_objects_node().get_children()
56  finally:
57  clt.disconnect()
58 
59  def test_nocrypto_feil(self):
60  clt = Client(self.uri_no_crypto)
61  with self.assertRaises(ua.UaError):
62  clt.set_security_string("Basic256,Sign,examples/certificate-example.der,examples/private-key-example.pem")
63 
64  def test_basic256(self):
65  clt = Client(self.uri_crypto)
66  try:
67  clt.set_security_string("Basic256,Sign,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 
74  clt = Client(self.uri_crypto)
75  try:
76  clt.set_security_string("Basic256,SignAndEncrypt,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(self):
83  clt = Client(self.uri_crypto)
84  try:
85  clt.set_security_string("Basic128Rsa15,Sign,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 
92  clt = Client(self.uri_crypto)
93  try:
94  clt.set_security_string("Basic128Rsa15,SignAndEncrypt,examples/certificate-example.der,examples/private-key-example.pem")
95  clt.connect()
96  self.assertTrue(clt.get_objects_node().get_children())
97  finally:
98  clt.disconnect()
99 
101  clt = Client(self.uri_crypto)
102  try:
103  clt.set_security(security_policies.SecurityPolicyBasic256,
104  'examples/certificate-example.der',
105  'examples/private-key-example.pem',
106  None,
107  ua.MessageSecurityMode.SignAndEncrypt
108  )
109  clt.connect()
110  self.assertTrue(clt.get_objects_node().get_children())
111  finally:
112  clt.disconnect()
113 
115  # FIXME: how to make it feil???
116  clt = Client(self.uri_crypto)
117  with self.assertRaises(ua.UaError):
118  clt.set_security(security_policies.SecurityPolicyBasic256,
119  'examples/certificate-example.der',
120  'examples/private-key-example.pem',
121  None,
122  ua.MessageSecurityMode.None_
123  )


ros_opcua_impl_python_opcua
Author(s): Denis Štogl , Daniel Draper
autogenerated on Tue Jan 19 2021 03:12:44