Skip to content

Commit fd4f597

Browse files
authored
Merge pull request #160 from MetalHurlant/improve_cisco_ip_helper
Cisco IP helper improvement
2 parents ea5f77f + c98e615 commit fd4f597

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

fake_switches/cisco/command_processor/config_interface.py

+20-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from fake_switches.command_processing.base_command_processor import BaseCommandProcessor
1919
from fake_switches.switch_configuration import VlanPort
2020

21+
from fake_switches.utils.ip_validator import InvalidIpError, IncompleteIpError, valid_ip_v4
22+
2123

2224
class ConfigInterfaceCommandProcessor(BaseCommandProcessor):
2325
def init(self, switch_configuration, terminal_controller, logger, piping_processor, *args):
@@ -139,9 +141,19 @@ def do_ip(self, *args):
139141
self.write_line("% Invalid input detected at '^' marker.")
140142
self.write_line("")
141143
else:
142-
ip_address = IPAddress(args[1])
143-
if ip_address not in self.port.ip_helpers:
144-
self.port.ip_helpers.append(ip_address)
144+
try:
145+
ip_address = _parse_ip(args[1], 4)
146+
if ip_address is None:
147+
raise InvalidIpError
148+
if ip_address not in self.port.ip_helpers:
149+
self.port.ip_helpers.append(ip_address)
150+
except InvalidIpError:
151+
self.write_line(" ^")
152+
self.write_line("% Invalid input detected at '^' marker.")
153+
self.write_line("")
154+
except IncompleteIpError:
155+
self.write_line("% Incomplete command.")
156+
self.write_line("")
145157

146158
def do_no_ip(self, *args):
147159
if "address".startswith(args[0]):
@@ -333,7 +345,11 @@ def parse_vlan_list(param):
333345
return vlans
334346

335347

336-
def _parse_ip(ip):
348+
def _parse_ip(ip, strict_format=None):
349+
if strict_format == 4 and not valid_ip_v4(ip):
350+
return None
351+
elif strict_format and strict_format != 4:
352+
raise NotImplementedError("Ip format {} validation not supported.".format(strict_format))
337353
try:
338354
return IPAddress(ip)
339355
except:

fake_switches/utils/__init__.py

Whitespace-only changes.

fake_switches/utils/ip_validator.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def valid_ip_v4(address):
2+
parts = address.split(".")
3+
for item in parts:
4+
if not item.isdigit() or not 0 <= int(item) <= 255:
5+
raise InvalidIpError
6+
if len(parts) != 4:
7+
raise IncompleteIpError
8+
return True
9+
10+
11+
class InvalidIpError(Exception):
12+
pass
13+
14+
15+
class IncompleteIpError(Exception):
16+
pass

tests/cisco/test_cisco_switch_protocol.py

+15
Original file line numberDiff line numberDiff line change
@@ -1240,8 +1240,23 @@ def test_ip_helper(self, t):
12401240
t.write("ip helper-address")
12411241
t.readln("% Incomplete command.")
12421242
t.readln("")
1243+
1244+
t.read("my_switch(config-if)#")
1245+
t.write("ip helper-address 1.1.1")
1246+
t.readln("% Incomplete command.")
1247+
t.readln("")
12431248
t.read("my_switch(config-if)#")
1249+
t.write("ip helper-address 1.a.1")
1250+
t.readln(" ^")
1251+
t.readln("% Invalid input detected at '^' marker.") # not incomplete
1252+
t.readln("")
1253+
t.read("my_switch(config-if)#")
1254+
t.write("ip helper-address invalid.ip")
1255+
t.readln(" ^")
1256+
t.readln("% Invalid input detected at '^' marker.")
1257+
t.readln("")
12441258

1259+
t.read("my_switch(config-if)#")
12451260
t.write("ip helper-address 10.10.0.1 EXTRA INFO")
12461261
t.readln(" ^")
12471262
t.readln("% Invalid input detected at '^' marker.")

0 commit comments

Comments
 (0)