Exemplo n.º 1
0
    def testTlsClientRegister(self):
        class CustomeRequest:
            function_code = 79

        client = ModbusTlsClient()
        client.framer = Mock()
        client.register(CustomeRequest)
        assert client.framer.decoder.register.called_once_with(CustomeRequest)
Exemplo n.º 2
0
    def testSyncTlsClientInstantiation(self):
        # default SSLContext
        client = ModbusTlsClient()
        self.assertNotEqual(client, None)
        self.assertTrue(client.sslctx)

        # user defined SSLContext
        context = ssl.create_default_context()
        client = ModbusTlsClient(sslctx=context)
        self.assertNotEqual(client, None)
        self.assertEqual(client.sslctx, context)
Exemplo n.º 3
0
def run_sync_client():
    client_cert = 'client.crt'
    client_key = 'privk.key'
    did_wallet_path = "walletDid.json"
    client = ModbusTlsClient(host=os.getenv('IP_SERVER'),
                             port=8020,
                             sslctx=None,
                             certfile=client_cert,
                             keyfile=client_key,
                             did_wallet_path=did_wallet_path)
    socket, B = client.connect()
    print("B")
    print(B)
    client.close()
Exemplo n.º 4
0
    def testTlsClientRecv(self):
        ''' Test the tls client receive method'''
        client = ModbusTlsClient()
        self.assertRaises(ConnectionException, lambda: client._recv(1024))

        client.socket = mockSocket()
        self.assertEqual(b'', client._recv(0))
        self.assertEqual(b'\x00' * 4, client._recv(4))

        mock_socket = MagicMock()
        mock_socket.recv.side_effect = iter([b'\x00', b'\x01', b'\x02'])
        client.socket = mock_socket
        client.timeout = 1
        self.assertEqual(b'\x00\x01\x02', client._recv(3))
        mock_socket.recv.side_effect = iter([b'\x00', b'\x01', b'\x02'])
        self.assertEqual(b'\x00\x01', client._recv(2))
Exemplo n.º 5
0
 def testTlsClientRpr(self):
     client = ModbusTlsClient()
     rep = "<{} at {} socket={}, ipaddr={}, port={}, sslctx={}, " \
         "timeout={}>".format(
         client.__class__.__name__, hex(id(client)), client.socket,
         client.host, client.port, client.sslctx, client.timeout
     )
     self.assertEqual(repr(client), rep)
Exemplo n.º 6
0
    def testTlsClientSend(self):
        ''' Test the tls client send method'''
        client = ModbusTlsClient()
        self.assertRaises(ConnectionException, lambda: client._send(None))

        client.socket = mockSocket()
        self.assertEqual(0, client._send(None))
        self.assertEqual(4, client._send('1234'))
Exemplo n.º 7
0
    def testTlsClientConnect(self):
        ''' Test the tls client connection method'''
        with patch.object(ssl.SSLSocket, 'connect') as mock_method:
            client = ModbusTlsClient()
            self.assertTrue(client.connect())

        with patch.object(socket, 'create_connection') as mock_method:
            mock_method.side_effect = socket.error()
            client = ModbusTlsClient()
            self.assertFalse(client.connect())
Exemplo n.º 8
0
This is a simple example of writing a modbus TCP over TLS client that uses
Python builtin module ssl - TLS/SSL wrapper for socket objects for the TLS
feature.
"""
# -------------------------------------------------------------------------- #
# import necessary libraries
# -------------------------------------------------------------------------- #
import ssl
from pymodbus.client.sync import ModbusTlsClient

# -------------------------------------------------------------------------- #
# the TLS detail security can be set in SSLContext which is the context here
# -------------------------------------------------------------------------- #
context = ssl.create_default_context()
context.options |= ssl.OP_NO_SSLv2
context.options |= ssl.OP_NO_SSLv3
context.options |= ssl.OP_NO_TLSv1
context.options |= ssl.OP_NO_TLSv1_1

# -------------------------------------------------------------------------- #
# pass SSLContext which is the context here to ModbusTcpClient()
# -------------------------------------------------------------------------- #
client = ModbusTlsClient('test.host.com', 8020, sslctx=context)
client.connect()

result = client.read_coils(1, 8)
print(result.bits)

client.close()
Exemplo n.º 9
0
    def testBasicSyncTlsClient(self):
        ''' Test the basic methods for the tls sync client'''

        # receive/send
        client = ModbusTlsClient()
        client.socket = mockSocket()
        self.assertEqual(0, client._send(None))
        self.assertEqual(1, client._send(b'\x00'))
        self.assertEqual(b'\x00', client._recv(1))

        # connect/disconnect
        self.assertTrue(client.connect())
        client.close()

        # already closed socket
        client.socket = False
        client.close()

        self.assertEqual("ModbusTlsClient(localhost:802)", str(client))