Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def testVal_IOSIntfLine_ipv4_addr_object01(parse_c03_factory):
cfg = parse_c03_factory
result_correct = {
'interface Serial 1/0': IPv4Obj('1.1.1.1/30', strict=False),
'interface Serial 1/1': IPv4Obj('1.1.1.9/31', strict=False),
'interface GigabitEthernet4/1': IPv4Obj('127.0.0.1/32',
strict=False),
'interface GigabitEthernet4/2': IPv4Obj('127.0.0.1/32',
strict=False),
'interface GigabitEthernet4/3': IPv4Obj('127.0.0.1/32',
strict=False),
'interface GigabitEthernet4/4': IPv4Obj('127.0.0.1/32',
strict=False),
'interface GigabitEthernet4/5': IPv4Obj('127.0.0.1/32',
strict=False),
'interface GigabitEthernet4/6': IPv4Obj('127.0.0.1/32',
strict=False),
'interface GigabitEthernet4/7': IPv4Obj('127.0.0.1/32',
strict=False),
'interface GigabitEthernet4/8.120': IPv4Obj('1.1.2.254/24',
def testIPv4Obj_contain():
## Test ccp_util.IPv4Obj.__contains__()
##
## Test whether a prefix is or is not contained in another prefix
results_correct = [
('1.0.0.0/8', '0.0.0.0/0', True), # Is 1.0.0.0/8 in 0.0.0.0/0?
('0.0.0.0/0', '1.0.0.0/8', False), # Is 0.0.0.0/0 in 1.0.0.0/8?
('1.1.1.0/27', '1.0.0.0/8', True), # Is 1.1.1.0/27 in 1.0.0.0/8?
('1.1.1.0/27', '9.9.9.9/32', False), # Is 1.1.1.0/27 in 9.9.9.9/32?
('9.9.9.0/27', '9.9.9.9/32', False), # Is 9.9.9.0/27 in 9.9.9.9/32?
]
for prefix1, prefix2, result_correct in results_correct:
## 'foo in bar' tests bar.__contains__(foo)
test_result = IPv4Obj(prefix1) in IPv4Obj(prefix2)
assert test_result==result_correct
'interface Serial 1/1': True,
'interface GigabitEthernet4/1': None,
'interface GigabitEthernet4/2': None,
'interface GigabitEthernet4/3': None,
'interface GigabitEthernet4/4': None,
'interface GigabitEthernet4/5': None,
'interface GigabitEthernet4/6': None,
'interface GigabitEthernet4/7': None,
'interface GigabitEthernet4/8.120': True,
'interface ATM5/0/0': None,
'interface ATM5/0/0.32 point-to-point': True,
'interface ATM5/0/1': None,
}
test_result = dict()
## Parse all interface objects in c01 and check in_ipv4_subnets
test_network1 = IPv4Obj('1.1.0.0/23', strict=False)
test_network2 = IPv4Obj('1.1.2.0/23', strict=False)
networks = set([test_network1, test_network2])
for intf_obj in cfg.find_objects('^interface'):
test_result[intf_obj.text] = intf_obj.in_ipv4_subnets(networks)
assert result_correct==test_result
def testIPv4Obj_parse():
## Ensure that IPv4Obj can correctly parse various inputs
test_strings = [
'1.0.0.1/24',
'1.0.0.1/32',
'1.0.0.1 255.255.255.0',
'1.0.0.1 255.255.255.255',
'1.0.0.1 255.255.255.0',
'1.0.0.1 255.255.255.255',
'1.0.0.1/255.255.255.0',
'1.0.0.1/255.255.255.255',
]
for test_string in test_strings:
test_result = IPv4Obj(test_string)
assert isinstance(test_result, IPv4Obj)
def ipv4_addr_object(self):
"""Return a ccp_util.IPv4Obj object representing the address on this interface; if there is no address, return IPv4Obj('127.0.0.1/32')"""
try:
return IPv4Obj('%s/%s' % (self.ipv4_addr, self.ipv4_masklength))
except DynamicAddressException as e:
raise DynamicAddressException(e)
except:
return self.default_ipv4_addr_object
def __init__(self, *args, **kwargs):
super(BaseASAIntfLine, self).__init__(*args, **kwargs)
self.ifindex = None # Optional, for user use
self.default_ipv4_addr_object = IPv4Obj('127.0.0.1/32',
strict=False)
def network_object(self):
try:
if self.address_family=='ip':
return IPv4Obj('%s/%s' % (self.network, self.netmask),
strict=False)
elif self.address_family=='ipv6':
return IPv6Network('%s/%s' % (self.network, self.netmask))
except:
return None
def in_ipv4_subnet(self, ipv4network=IPv4Obj('0.0.0.0/32', strict=False)):
"""Accept two string arguments for network and netmask, and return a boolean for whether this interface is within the requested subnet. Return None if there is no address on the interface"""
if not (str(self.ipv4_addr_object.ip)=="127.0.0.1"):
try:
# Return a boolean for whether the interface is in that network and mask
return self.ipv4_addr_object in ipv4network
except:
raise ValueError("FATAL: %s.in_ipv4_subnet(ipv4network={0}) is an invalid arg".format(ipv4network))
else:
return None
def ipv4_addr_object(self):
"""Return a ccp_util.IPv4Obj object representing the address on this interface; if there is no address, return IPv4Obj('127.0.0.1/32')"""
try:
return IPv4Obj('%s/%s' % (self.ipv4_addr, self.ipv4_netmask))
except:
return self.default_ipv4_addr_object
def ip_network_object(self):
# Simplified on 2014-12-02
try:
return IPv4Obj(
'{0}/{1}'.format(self.ipv4_addr, self.ipv4_netmask),
strict=False)
except DynamicAddressException as e:
raise DynamicAddressException(e)
except (Exception) as e:
return self.default_ipv4_addr_object