1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
#!/usr/bin/env python
import unittest
import socket
import serial
from mock import patch, Mock
from twisted.test import test_protocols
from pymodbus.client.sync import ModbusTcpClient, ModbusUdpClient
from pymodbus.client.sync import ModbusSerialClient, BaseModbusClient
from pymodbus.exceptions import ConnectionException, NotImplementedException
from pymodbus.exceptions import ParameterException
from pymodbus.transaction import ModbusAsciiFramer, ModbusRtuFramer
from pymodbus.transaction import ModbusBinaryFramer
#---------------------------------------------------------------------------#
# Mock Classes
#---------------------------------------------------------------------------#
class mockSocket(object):
def close(self): return True
def recv(self, size): return '\x00'*size
def read(self, size): return '\x00'*size
def send(self, msg): return len(msg)
def write(self, msg): return len(msg)
def recvfrom(self, size): return ['\x00'*size]
def sendto(self, msg, *args): return len(msg)
#---------------------------------------------------------------------------#
# Fixture
#---------------------------------------------------------------------------#
class SynchronousClientTest(unittest.TestCase):
'''
This is the unittest for the pymodbus.client.sync module
'''
#-----------------------------------------------------------------------#
# Test Base Client
#-----------------------------------------------------------------------#
def testBaseModbusClient(self):
''' Test the base class for all the clients '''
client = BaseModbusClient(None)
client.transaction = None
self.assertRaises(NotImplementedException, lambda: client.connect())
self.assertRaises(NotImplementedException, lambda: client._send(None))
self.assertRaises(NotImplementedException, lambda: client._recv(None))
self.assertRaises(NotImplementedException, lambda: client.__enter__())
self.assertRaises(NotImplementedException, lambda: client.execute())
self.assertEquals("Null Transport", str(client))
client.close()
client.__exit__(0,0,0)
# a successful execute
client.connect = lambda: True
client.transaction = Mock(**{'execute.return_value': True})
self.assertEqual(client, client.__enter__())
self.assertTrue(client.execute())
# a unsuccessful connect
client.connect = lambda: False
self.assertRaises(ConnectionException, lambda: client.__enter__())
self.assertRaises(ConnectionException, lambda: client.execute())
#-----------------------------------------------------------------------#
# Test UDP Client
#-----------------------------------------------------------------------#
def testSyncUdpClientInstantiation(self):
client = ModbusUdpClient()
self.assertNotEqual(client, None)
def testBasicSyncUdpClient(self):
''' Test the basic methods for the udp sync client'''
# receive/send
client = ModbusUdpClient()
client.socket = mockSocket()
self.assertEqual(0, client._send(None))
self.assertEqual(1, client._send('\x00'))
self.assertEqual('\x00', client._recv(1))
# connect/disconnect
self.assertTrue(client.connect())
client.close()
# already closed socket
client.socket = False
client.close()
self.assertEqual("127.0.0.1:502", str(client))
def testUdpClientAddressFamily(self):
''' Test the Udp client get address family method'''
client = ModbusUdpClient()
self.assertEqual(socket.AF_INET, client._get_address_family('127.0.0.1'))
self.assertEqual(socket.AF_INET6, client._get_address_family('::1'))
def testUdpClientConnect(self):
''' Test the Udp client connection method'''
with patch.object(socket, 'socket') as mock_method:
class DummySocket(object):
def settimeout(self, *a, **kwa):
pass
mock_method.return_value = DummySocket()
client = ModbusUdpClient()
self.assertTrue(client.connect())
with patch.object(socket, 'socket') as mock_method:
mock_method.side_effect = socket.error()
client = ModbusUdpClient()
self.assertFalse(client.connect())
def testUdpClientSend(self):
''' Test the udp client send method'''
client = ModbusUdpClient()
self.assertRaises(ConnectionException, lambda: client._send(None))
client.socket = mockSocket()
self.assertEqual(0, client._send(None))
self.assertEqual(4, client._send('1234'))
def testUdpClientRecv(self):
''' Test the udp client receive method'''
client = ModbusUdpClient()
self.assertRaises(ConnectionException, lambda: client._recv(1024))
client.socket = mockSocket()
self.assertEqual('', client._recv(0))
self.assertEqual('\x00'*4, client._recv(4))
#-----------------------------------------------------------------------#
# Test TCP Client
#-----------------------------------------------------------------------#
def testSyncTcpClientInstantiation(self):
client = ModbusTcpClient()
self.assertNotEqual(client, None)
def testBasicSyncTcpClient(self):
''' Test the basic methods for the tcp sync client'''
# receive/send
client = ModbusTcpClient()
client.socket = mockSocket()
self.assertEqual(0, client._send(None))
self.assertEqual(1, client._send('\x00'))
self.assertEqual('\x00', client._recv(1))
# connect/disconnect
self.assertTrue(client.connect())
client.close()
# already closed socket
client.socket = False
client.close()
self.assertEqual("127.0.0.1:502", str(client))
def testTcpClientConnect(self):
''' Test the tcp client connection method'''
with patch.object(socket, 'create_connection') as mock_method:
mock_method.return_value = object()
client = ModbusTcpClient()
self.assertTrue(client.connect())
with patch.object(socket, 'create_connection') as mock_method:
mock_method.side_effect = socket.error()
client = ModbusTcpClient()
self.assertFalse(client.connect())
def testTcpClientSend(self):
''' Test the tcp client send method'''
client = ModbusTcpClient()
self.assertRaises(ConnectionException, lambda: client._send(None))
client.socket = mockSocket()
self.assertEqual(0, client._send(None))
self.assertEqual(4, client._send('1234'))
def testTcpClientRecv(self):
''' Test the tcp client receive method'''
client = ModbusTcpClient()
self.assertRaises(ConnectionException, lambda: client._recv(1024))
client.socket = mockSocket()
self.assertEqual('', client._recv(0))
self.assertEqual('\x00'*4, client._recv(4))
#-----------------------------------------------------------------------#
# Test Serial Client
#-----------------------------------------------------------------------#
def testSyncSerialClientInstantiation(self):
client = ModbusSerialClient()
self.assertNotEqual(client, None)
self.assertTrue(isinstance(ModbusSerialClient(method='ascii').framer, ModbusAsciiFramer))
self.assertTrue(isinstance(ModbusSerialClient(method='rtu').framer, ModbusRtuFramer))
self.assertTrue(isinstance(ModbusSerialClient(method='binary').framer, ModbusBinaryFramer))
self.assertRaises(ParameterException, lambda: ModbusSerialClient(method='something'))
def testBasicSyncSerialClient(self):
''' Test the basic methods for the serial sync client'''
# receive/send
client = ModbusSerialClient()
client.socket = mockSocket()
self.assertEqual(0, client._send(None))
self.assertEqual(1, client._send('\x00'))
self.assertEqual('\x00', client._recv(1))
# connect/disconnect
self.assertTrue(client.connect())
client.close()
# already closed socket
client.socket = False
client.close()
self.assertEqual('ascii baud[19200]', str(client))
def testSerialClientConnect(self):
''' Test the serial client connection method'''
with patch.object(serial, 'Serial') as mock_method:
mock_method.return_value = object()
client = ModbusSerialClient()
self.assertTrue(client.connect())
with patch.object(serial, 'Serial') as mock_method:
mock_method.side_effect = serial.SerialException()
client = ModbusSerialClient()
self.assertFalse(client.connect())
def testSerialClientSend(self):
''' Test the serial client send method'''
client = ModbusSerialClient()
self.assertRaises(ConnectionException, lambda: client._send(None))
client.socket = mockSocket()
self.assertEqual(0, client._send(None))
self.assertEqual(4, client._send('1234'))
def testSerialClientRecv(self):
''' Test the serial client receive method'''
client = ModbusSerialClient()
self.assertRaises(ConnectionException, lambda: client._recv(1024))
client.socket = mockSocket()
self.assertEqual('', client._recv(0))
self.assertEqual('\x00'*4, client._recv(4))
#---------------------------------------------------------------------------#
# Main
#---------------------------------------------------------------------------#
if __name__ == "__main__":
unittest.main()
|