Industrial Protocol Testing: Modbus, OPC-UA, and PLC Automation Testing
Industrial control systems run manufacturing plants, power grids, and water treatment facilities. Yet most ICS testing is still done manually — an engineer connects a laptop to a PLC, sends a few commands, and checks the outputs by eye. This approach misses regressions, misses edge cases, and doesn't scale.
This guide covers automated testing for industrial protocols: Modbus, OPC-UA, and PLC ladder logic. Same discipline as software testing, applied to the shop floor.
Why Industrial Protocol Testing Is Hard
Industrial systems have constraints that make automated testing challenging:
- Real-time requirements — SCADA systems have millisecond timing requirements
- Hardware dependencies — many tests require actual PLCs, sensors, and actuators
- Legacy protocols — Modbus hasn't changed since 1979; documentation is sparse
- Safety criticality — a bad test command could cause physical damage
- Network topology — RS-485 bus architectures don't work like Ethernet
Despite these constraints, significant test coverage is achievable with a good simulator strategy.
Modbus Testing
Modbus is the lingua franca of industrial automation. Two variants are common:
- Modbus TCP — runs over Ethernet on port 502
- Modbus RTU — runs over RS-485 serial
Testing with pymodbus
pip install pymodbus pytest# tests/test_modbus_device.py
import pytest
from pymodbus.client import ModbusTcpClient, ModbusSerialClient
from pymodbus.exceptions import ModbusException
MODBUS_HOST = "192.168.1.100"
MODBUS_PORT = 502
UNIT_ID = 1
@pytest.fixture
def modbus_client():
"""Connected Modbus TCP client."""
client = ModbusTcpClient(MODBUS_HOST, port=MODBUS_PORT)
connected = client.connect()
if not connected:
pytest.skip(f"Cannot connect to Modbus device at {MODBUS_HOST}:{MODBUS_PORT}")
yield client
client.close()
def test_read_holding_registers(modbus_client):
"""Should read holding registers without error."""
# Read 10 registers starting at address 0
result = modbus_client.read_holding_registers(address=0, count=10, unit=UNIT_ID)
assert not result.isError(), f"Modbus error: {result}"
assert len(result.registers) == 10
def test_device_status_register(modbus_client):
"""Device status register (address 100) must indicate ready state."""
result = modbus_client.read_holding_registers(address=100, count=1, unit=UNIT_ID)
assert not result.isError()
status = result.registers[0]
READY_STATUS = 0x0001
assert status & READY_STATUS, f"Device not in ready state. Status: {status:#06x}"
def test_write_and_read_back(modbus_client):
"""Write to output register and verify readback."""
test_register = 200 # Safe test register
test_value = 0x1234
# Write
write_result = modbus_client.write_register(
address=test_register, value=test_value, unit=UNIT_ID
)
assert not write_result.isError(), f"Write failed: {write_result}"
# Read back
read_result = modbus_client.read_holding_registers(
address=test_register, count=1, unit=UNIT_ID
)
assert not read_result.isError()
assert read_result.registers[0] == test_value
def test_read_coils(modbus_client):
"""Digital I/O coils should be readable."""
result = modbus_client.read_coils(address=0, count=8, unit=UNIT_ID)
assert not result.isError()
assert len(result.bits) >= 8
# Each bit should be True or False
assert all(isinstance(b, bool) for b in result.bits[:8])
def test_exception_code_on_invalid_address(modbus_client):
"""Device should return exception on out-of-range register access."""
result = modbus_client.read_holding_registers(
address=9999, count=1, unit=UNIT_ID
)
# Should return an error (exception response), not crash
assert result.isError(), \
"Device should return exception for out-of-range address"
def test_response_time(modbus_client):
"""Modbus response must arrive within 100ms."""
import time
start = time.perf_counter()
result = modbus_client.read_holding_registers(address=0, count=1, unit=UNIT_ID)
elapsed_ms = (time.perf_counter() - start) * 1000
assert not result.isError()
assert elapsed_ms < 100, f"Response took {elapsed_ms:.1f}ms, expected < 100ms"Modbus Simulator for CI
# tests/modbus_simulator.py
from pymodbus.server import StartTcpServer
from pymodbus.datastore import ModbusSequentialDataBlock, ModbusSlaveContext, ModbusServerContext
def create_test_datastore():
"""Create a Modbus datastore with test data."""
store = ModbusSlaveContext(
di=ModbusSequentialDataBlock(0, [True, False] * 50), # Discrete inputs
co=ModbusSequentialDataBlock(0, [False] * 100), # Coils
hr=ModbusSequentialDataBlock(0, [0] * 1000), # Holding registers
ir=ModbusSequentialDataBlock(0, [100, 200, 300] + [0]*97), # Input registers
)
# Set status register to "ready"
store.setValues(3, 100, [0x0001]) # Holding register 100 = ready
return ModbusServerContext(slaves=store, single=True)
if __name__ == "__main__":
context = create_test_datastore()
StartTcpServer(context, address=("127.0.0.1", 5020))OPC-UA Testing
OPC-UA is the modern standard for industrial communication, used in Industry 4.0 and IIoT. It's more capable than Modbus (security, complex data types, events) but also more complex to test.
Testing with opcua-asyncio
pip install asyncua pytest pytest-asyncio# tests/test_opcua_server.py
import asyncio
import pytest
from asyncua import Client, Node, ua
OPC_UA_URL = "opc.tcp://192.168.1.100:4840"
@pytest.fixture
async def opcua_client():
"""Connected OPC-UA client."""
async with Client(url=OPC_UA_URL) as client:
yield client
@pytest.mark.asyncio
async def test_server_reachable(opcua_client):
"""OPC-UA server must accept connections."""
# Connection established in fixture — if we're here, it worked
assert opcua_client is not None
@pytest.mark.asyncio
async def test_server_info(opcua_client):
"""Server should expose required product info."""
node = opcua_client.get_node("i=2261") # ServerStatus node
server_state = await node.get_value()
# ServerState 0 = Running
assert server_state == 0, f"Server state {server_state} — expected Running (0)"
@pytest.mark.asyncio
async def test_required_nodes_exist(opcua_client):
"""Required OPC-UA nodes must be present."""
REQUIRED_NODES = [
"ns=2;s=Temperature",
"ns=2;s=Pressure",
"ns=2;s=DeviceStatus",
"ns=2;s=StartCommand",
]
for node_id in REQUIRED_NODES:
node = opcua_client.get_node(node_id)
try:
value = await node.get_value()
assert value is not None, f"Node {node_id} returned None"
except Exception as e:
pytest.fail(f"Node {node_id} not accessible: {e}")
@pytest.mark.asyncio
async def test_temperature_node_data_type(opcua_client):
"""Temperature node must be a Float."""
node = opcua_client.get_node("ns=2;s=Temperature")
data_value = await node.read_data_value()
assert data_value.Value.VariantType == ua.VariantType.Float, \
f"Expected Float, got {data_value.Value.VariantType}"
@pytest.mark.asyncio
async def test_temperature_in_valid_range(opcua_client):
"""Temperature reading must be in physically plausible range."""
node = opcua_client.get_node("ns=2;s=Temperature")
temperature = await node.get_value()
assert -50 <= temperature <= 500, \
f"Temperature {temperature}°C outside valid range"
@pytest.mark.asyncio
async def test_write_command_node(opcua_client):
"""Should be able to write start command."""
cmd_node = opcua_client.get_node("ns=2;s=StartCommand")
await cmd_node.set_value(ua.DataValue(ua.Variant(False, ua.VariantType.Boolean)))
readback = await cmd_node.get_value()
assert readback == False
@pytest.mark.asyncio
async def test_subscription_fires_on_value_change(opcua_client):
"""OPC-UA subscription should deliver events on value change."""
received_events = []
class EventHandler:
def datachange_notification(self, node, val, data):
received_events.append((node, val))
subscription = await opcua_client.create_subscription(100, EventHandler())
node = opcua_client.get_node("ns=2;s=Temperature")
handle = await subscription.subscribe_data_change(node)
# Wait for at least one update (device should update temperature periodically)
await asyncio.sleep(2.0)
await subscription.unsubscribe(handle)
await subscription.delete()
assert len(received_events) > 0, "No OPC-UA subscription events received in 2 seconds"OPC-UA Security Testing
@pytest.mark.asyncio
async def test_anonymous_access_restricted():
"""Anonymous access should be disabled or read-only."""
from asyncua.crypto.security_policies import SecurityPolicyBasic256Sha256
# Attempt anonymous connection
async with Client(url=OPC_UA_URL) as client:
try:
cmd_node = client.get_node("ns=2;s=StartCommand")
await cmd_node.set_value(ua.DataValue(ua.Variant(True, ua.VariantType.Boolean)))
# If we get here, anonymous write succeeded — that's a security issue
pytest.fail("Anonymous write to command node should be rejected")
except ua.UaStatusCodeError as e:
# Expected: BadUserAccessDenied or similar
assert "Access" in str(e) or "Denied" in str(e)PLC Ladder Logic Testing
Testing PLC programs requires either hardware or a simulator. Most major PLC vendors provide software PLCs:
- CODESYS — supports software PLC (SoftPLC) for Siemens, Beckhoff, etc.
- OpenPLC — open-source IEC 61131-3 runtime with simulation support
- Factory I/O — 3D factory simulation that connects to any PLC
# tests/test_plc_logic.py — using OpenPLC's Python API
import pytest
from openplc_client import OpenPLCClient
PLC_HOST = "127.0.0.1"
PLC_PORT = 43628
@pytest.fixture
def plc():
"""Connected OpenPLC client."""
client = OpenPLCClient(PLC_HOST, PLC_PORT)
client.connect()
yield client
client.disconnect()
def test_start_stop_sequence(plc):
"""Motor start/stop sequence must work correctly."""
# Initial state: motor stopped
assert plc.read_output("%QX0.0") == False, "Motor should start stopped"
# Press START button
plc.write_input("%IX0.0", True) # START button = ON
plc.run_scan_cycle()
plc.write_input("%IX0.0", False) # Release button
plc.run_scan_cycle()
assert plc.read_output("%QX0.0") == True, "Motor should be running after START"
# Press STOP button
plc.write_input("%IX0.1", True) # STOP button = ON
plc.run_scan_cycle()
assert plc.read_output("%QX0.0") == False, "Motor should be stopped after STOP"
def test_emergency_stop_overrides_start(plc):
"""E-STOP must always stop the motor regardless of other inputs."""
# Start the motor
plc.write_input("%IX0.0", True)
plc.run_scan_cycle()
plc.write_input("%IX0.0", False)
plc.run_scan_cycle()
assert plc.read_output("%QX0.0") == True
# Simultaneously: START held + E-STOP activated
plc.write_input("%IX0.0", True) # START
plc.write_input("%IX0.2", True) # E-STOP
plc.run_scan_cycle()
# Motor MUST be stopped — E-STOP has absolute priority
assert plc.read_output("%QX0.0") == False, \
"E-STOP must override START command"
def test_interlocks(plc):
"""Conveyor should not start while a guard is open."""
plc.write_input("%IX1.0", False) # Guard open
plc.write_input("%IX0.0", True) # START pressed
plc.run_scan_cycle()
plc.write_input("%IX0.0", False)
plc.run_scan_cycle()
assert plc.read_output("%QX0.0") == False, \
"Machine should not start with guard open"Protocol Fuzzing
For security-critical industrial systems, test with malformed inputs:
# tests/test_modbus_fuzzing.py
import pytest
from pymodbus.client import ModbusTcpClient
def test_oversized_register_count(modbus_client):
"""Request for too many registers should return exception, not crash."""
# Modbus max registers per request = 125
result = modbus_client.read_holding_registers(
address=0, count=200, unit=1 # Exceeds max
)
assert result.isError()
def test_invalid_function_code(modbus_client):
"""Invalid function code should return exception."""
from pymodbus.pdu import ModbusRequest
import struct
# Send raw PDU with invalid function code
raw_request = struct.pack(">BH", 0x64, 0x0000) # FC 100, addr 0
# (implementation depends on pymodbus internals)
@pytest.mark.parametrize("address", [0, 1, 100, 9998, 9999])
def test_boundary_addresses(modbus_client, address):
"""Boundary register addresses should return clean responses."""
result = modbus_client.read_holding_registers(
address=address, count=1, unit=1
)
# Should either succeed or return a proper Modbus exception
# Should NOT crash the server or cause a timeout
assert result is not NoneCI Pipeline for Industrial Protocol Tests
# .github/workflows/industrial-tests.yml
name: Industrial Protocol Tests
on:
push:
paths: ['firmware/**', 'tests/**']
jobs:
modbus-sim-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: pip install pymodbus pytest asyncua
- name: Start Modbus simulator
run: python tests/modbus_simulator.py &
- name: Wait for simulator
run: sleep 2
- name: Run Modbus tests (simulator)
run: |
pytest tests/test_modbus_device.py \
--modbus-host 127.0.0.1 \
--modbus-port 5020 \
-v
hardware-tests:
runs-on: [self-hosted, plc-lab] # Hardware runner in PLC lab
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v4
- run: pytest tests/test_modbus_device.py tests/test_opcua_server.py tests/test_plc_logic.py -vSummary
Industrial protocol testing follows the same principles as any software testing — just with different tools:
- Use simulators for CI — Modbus and OPC-UA simulators let you run tests on every commit
- Test protocol compliance — function codes, data types, exception handling
- Test safety logic — E-STOP, interlocks, and guard conditions require 100% test coverage
- Test real-time behavior — response time requirements are specs, test them
- Fuzz protocol boundaries — invalid inputs should return exceptions, not crashes
- Reserve hardware tests for timing, electrical, and integration tests that can't be simulated
The cost of a regression in an industrial system — production downtime, equipment damage, safety incidents — makes comprehensive automated testing non-negotiable.