tests_crypto_connect.py
Go to the documentation of this file.
00001 import unittest
00002 
00003 from opcua import Client
00004 from opcua import Server
00005 from opcua import ua
00006 
00007 try:
00008     from opcua.crypto import uacrypto
00009     from opcua.crypto import security_policies
00010 except ImportError:
00011     print("WARNING: CRYPTO NOT AVAILABLE, CRYPTO TESTS DISABLED!!")
00012     disable_crypto_tests = True
00013 else:
00014     disable_crypto_tests = False
00015 
00016 
00017 port_num1 = 48515
00018 port_num2 = 48512
00019 
00020 @unittest.skipIf(disable_crypto_tests, "crypto not available")
00021 class TestCryptoConnect(unittest.TestCase):
00022 
00023     '''
00024     Test connectino with a server supporting crypto 
00025 
00026     '''
00027     @classmethod
00028     def setUpClass(cls):
00029         # start our own server
00030         cls.srv_crypto = Server()
00031         cls.uri_crypto = 'opc.tcp://localhost:{0:d}'.format(port_num1)
00032         cls.srv_crypto.set_endpoint(cls.uri_crypto)
00033         # load server certificate and private key. This enables endpoints
00034         # with signing and encryption.
00035         cls.srv_crypto.load_certificate("examples/certificate-example.der")
00036         cls.srv_crypto.load_private_key("examples/private-key-example.pem")
00037         cls.srv_crypto.start()
00038 
00039         # start a server without crypto
00040         cls.srv_no_crypto = Server()
00041         cls.uri_no_crypto = 'opc.tcp://localhost:{0:d}'.format(port_num2)
00042         cls.srv_no_crypto.set_endpoint(cls.uri_no_crypto)
00043         cls.srv_no_crypto.start()
00044 
00045     @classmethod
00046     def tearDownClass(cls):
00047         # stop the server 
00048         cls.srv_no_crypto.stop()
00049         cls.srv_crypto.stop()
00050 
00051     def test_nocrypto(self):
00052         clt = Client(self.uri_no_crypto)
00053         clt.connect()
00054         try:
00055             clt.get_objects_node().get_children()
00056         finally:
00057             clt.disconnect()
00058 
00059     def test_nocrypto_feil(self):
00060         clt = Client(self.uri_no_crypto)
00061         with self.assertRaises(ua.UaError):
00062             clt.set_security_string("Basic256,Sign,examples/certificate-example.der,examples/private-key-example.pem")
00063 
00064     def test_basic256(self):
00065         clt = Client(self.uri_crypto)
00066         try:
00067             clt.set_security_string("Basic256,Sign,examples/certificate-example.der,examples/private-key-example.pem")
00068             clt.connect()
00069             self.assertTrue(clt.get_objects_node().get_children())
00070         finally:
00071             clt.disconnect()
00072 
00073     def test_basic256_encrypt(self):
00074         clt = Client(self.uri_crypto)
00075         try:
00076             clt.set_security_string("Basic256,SignAndEncrypt,examples/certificate-example.der,examples/private-key-example.pem")
00077             clt.connect()
00078             self.assertTrue(clt.get_objects_node().get_children())
00079         finally:
00080             clt.disconnect()
00081 
00082     def test_basic128Rsa15(self):
00083         clt = Client(self.uri_crypto)
00084         try:
00085             clt.set_security_string("Basic128Rsa15,Sign,examples/certificate-example.der,examples/private-key-example.pem")
00086             clt.connect()
00087             self.assertTrue(clt.get_objects_node().get_children())
00088         finally:
00089             clt.disconnect()
00090 
00091     def test_basic128Rsa15_encrypt(self):
00092         clt = Client(self.uri_crypto)
00093         try:
00094             clt.set_security_string("Basic128Rsa15,SignAndEncrypt,examples/certificate-example.der,examples/private-key-example.pem")
00095             clt.connect()
00096             self.assertTrue(clt.get_objects_node().get_children())
00097         finally:
00098             clt.disconnect()
00099 
00100     def test_basic256_encrypt_success(self):
00101         clt = Client(self.uri_crypto)
00102         try:
00103             clt.set_security(security_policies.SecurityPolicyBasic256,
00104                              'examples/certificate-example.der',
00105                              'examples/private-key-example.pem',
00106                              None,
00107                              ua.MessageSecurityMode.SignAndEncrypt
00108                              )
00109             clt.connect()
00110             self.assertTrue(clt.get_objects_node().get_children())
00111         finally:
00112             clt.disconnect()
00113 
00114     def test_basic256_encrypt_feil(self):
00115         # FIXME: how to make it feil???
00116         clt = Client(self.uri_crypto)
00117         with self.assertRaises(ua.UaError):
00118             clt.set_security(security_policies.SecurityPolicyBasic256,
00119                              'examples/certificate-example.der',
00120                              'examples/private-key-example.pem',
00121                              None,
00122                              ua.MessageSecurityMode.None_
00123                              )


ros_opcua_impl_python_opcua
Author(s): Denis Štogl , Daniel Draper
autogenerated on Sat Jun 8 2019 18:26:23