Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pymodbus/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ def __init__(
:param trace_connect: Called when connected/disconnected
:param custom_pdu: list of ModbusPDU custom classes
"""
if not sslctx:
raise TypeError(
"Cannot start server without a certificate, please add sslctx="
)
self.tls_setup = CommParams(
comm_type=CommType.TLS,
comm_name="server_listener",
Expand Down
6 changes: 6 additions & 0 deletions test/server/test_server_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import pytest

from examples.helper import generate_ssl, get_certificate
from pymodbus import FramerType, ModbusDeviceIdentification
from pymodbus.exceptions import NoSuchIdException
from pymodbus.server import (
Expand Down Expand Up @@ -162,6 +163,11 @@ async def start_server(
framer=FramerType.TLS,
identity=self.identity,
address=serv_addr,
sslctx=generate_ssl(
False,
certfile=get_certificate("crt"),
keyfile=get_certificate("key"),
),
)
elif do_udp:
self.server = ModbusUdpServer(
Expand Down
26 changes: 24 additions & 2 deletions test/server/test_startstop.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pytest

from examples.helper import generate_ssl, get_certificate
from pymodbus.datastore import ModbusDeviceContext, ModbusServerContext
from pymodbus.server import (
ModbusBaseServer,
Expand Down Expand Up @@ -59,9 +60,23 @@ async def test_StartAsyncTlsServer(self, mock_method):
"""Test StartAsyncTlsServer."""
mock_method.return_value = True
await StartAsyncTlsServer(
ModbusServerContext(devices=ModbusDeviceContext(), single=True)
ModbusServerContext(devices=ModbusDeviceContext(), single=True),
sslctx=generate_ssl(
False,
certfile=get_certificate("crt"),
keyfile=get_certificate("key"),
),
)

@mock.patch("pymodbus.server.ModbusBaseServer.serve_forever")
async def test_tls_no_cert(self, mock_method):
"""Test StartAsyncTlsServer."""
mock_method.return_value = True
with pytest.raises(TypeError):
await StartAsyncTlsServer(
ModbusServerContext(devices=ModbusDeviceContext(), single=True),
)

@mock.patch("pymodbus.server.ModbusBaseServer.serve_forever")
async def test_StartAsyncUdpServer(self, mock_method):
"""Test StartAsyncUdpServer."""
Expand Down Expand Up @@ -104,7 +119,14 @@ def test_StartTcpServer(self, mock_method):
def test_StartTlsServer(self, mock_method):
"""Test StartTlsServer."""
mock_method.return_value = True
StartTlsServer(ModbusServerContext(devices=ModbusDeviceContext(), single=True))
StartTlsServer(
ModbusServerContext(devices=ModbusDeviceContext(), single=True),
sslctx=generate_ssl(
False,
certfile=get_certificate("crt"),
keyfile=get_certificate("key"),
),
)

@mock.patch("pymodbus.server.ModbusBaseServer.serve_forever")
def test_StartUdpServer(self, mock_method):
Expand Down