From 42c8fb7c2935b3a157cbac2798b6522735ef2cdb Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Fri, 30 Oct 2015 15:15:28 +0100 Subject: [PATCH] Raise an error when you use a port outside the ranges Fix #739 --- gns3server/modules/port_manager.py | 4 ++++ tests/modules/test_port_manager.py | 28 +++++++++++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/gns3server/modules/port_manager.py b/gns3server/modules/port_manager.py index 71409dc8..96f1f622 100644 --- a/gns3server/modules/port_manager.py +++ b/gns3server/modules/port_manager.py @@ -203,6 +203,8 @@ class PortManager: if port in self._used_tcp_ports: raise HTTPConflict(text="TCP port {} already in use on host".format(port, self._console_host)) + if port < self._console_port_range[0] or port > self._console_port_range[1]: + raise HTTPConflict(text="TCP port {} is outside the range {}-{}".format(port, self._console_port_range[0], self._console_port_range[1])) self._used_tcp_ports.add(port) project.record_tcp_port(port) log.debug("TCP port {} has been reserved".format(port)) @@ -249,6 +251,8 @@ class PortManager: if port in self._used_udp_ports: raise HTTPConflict(text="UDP port {} already in use on host".format(port, self._console_host)) + if port < self._udp_port_range[0] or port > self._udp_port_range[1]: + raise HTTPConflict(text="UDP port {} is outside the range {}-{}".format(port, self._udp_port_range[0], self._udp_port_range[1])) self._used_udp_ports.add(port) project.record_udp_port(port) log.debug("UDP port {} has been reserved".format(port)) diff --git a/tests/modules/test_port_manager.py b/tests/modules/test_port_manager.py index 2f3b791e..e1bb3342 100644 --- a/tests/modules/test_port_manager.py +++ b/tests/modules/test_port_manager.py @@ -26,25 +26,39 @@ from gns3server.modules.project import Project def test_reserve_tcp_port(): pm = PortManager() project = Project() - pm.reserve_tcp_port(4242, project) + pm.reserve_tcp_port(2001, project) with pytest.raises(aiohttp.web.HTTPConflict): - pm.reserve_tcp_port(4242, project) + pm.reserve_tcp_port(2001, project) + + +def test_reserve_tcp_port_outside_range(): + pm = PortManager() + project = Project() + with pytest.raises(aiohttp.web.HTTPConflict): + pm.reserve_tcp_port(80, project) def test_reserve_udp_port(): pm = PortManager() project = Project() - pm.reserve_udp_port(4242, project) + pm.reserve_udp_port(10000, project) with pytest.raises(aiohttp.web.HTTPConflict): - pm.reserve_udp_port(4242, project) + pm.reserve_udp_port(10000, project) + + +def test_reserve_udp_port_outside_range(): + pm = PortManager() + project = Project() + with pytest.raises(aiohttp.web.HTTPConflict): + pm.reserve_udp_port(80, project) def test_release_udp_port(): pm = PortManager() project = Project() - pm.reserve_udp_port(4242, project) - pm.release_udp_port(4242, project) - pm.reserve_udp_port(4242, project) + pm.reserve_udp_port(10000, project) + pm.release_udp_port(10000, project) + pm.reserve_udp_port(10000, project) def test_find_unused_port():