Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
(True, PortRange.REGISTERED),
],
)
def test_ip_v4(self, net, with_port, port_range):
ip = net.ip_v4(with_port, port_range)
if not with_port:
assert re.match(patterns.IP_V4_REGEX, ip)
else:
port = int(ip.split(':')[-1])
port_start, port_end = port_range.value
assert port_start <= port <= port_end
(False, PortRange.ALL),
(True, PortRange.ALL),
(True, PortRange.WELL_KNOWN),
(True, PortRange.EPHEMERAL),
(True, PortRange.REGISTERED),
],
)
def test_ip_v4(self, net, with_port, port_range):
ip = net.ip_v4(with_port, port_range)
if not with_port:
assert re.match(patterns.IP_V4_REGEX, ip)
else:
port = int(ip.split(':')[-1])
port_start, port_end = port_range.value
assert port_start <= port <= port_end
def test_ip_v4(self, i1, i2):
assert i1.ip_v4() == i2.ip_v4()
assert i1.ip_v4(with_port=True) == i2.ip_v4(with_port=True)
assert i1.ip_v4(with_port=True, port_range=PortRange.ALL) == \
i2.ip_v4(with_port=True, port_range=PortRange.ALL)
def test_port(self, i1, i2):
assert i1.port() == i2.port()
assert i1.port(port_range=PortRange.REGISTERED) == \
i2.port(port_range=PortRange.REGISTERED)
def port(self, port_range: PortRange = PortRange.ALL) -> int:
"""Generate random port.
:param port_range: PortRange enum object.
:return: Port number.
:raises NonEnumerableError: if port_range is not in PortRange.
:Example:
8080
"""
if isinstance(port_range, PortRange):
return self.random.randint(*port_range.value)
else:
raise NonEnumerableError(PortRange)
def port(self, port_range: PortRange = PortRange.ALL) -> int:
"""Generate random port.
:param port_range: PortRange enum object.
:return: Port number.
:raises NonEnumerableError: if port_range is not in PortRange.
:Example:
8080
"""
if isinstance(port_range, PortRange):
return self.random.randint(*port_range.value)
else:
raise NonEnumerableError(PortRange)
def ip_v4(self, with_port: bool = False,
port_range: PortRange = PortRange.ALL) -> str:
"""Generate a random IPv4 address as string.
:param port_range: PortRange enum object.
:param with_port: Add port from PortRange to IP.
:return: IPv4 address as string.
:Example:
19.121.223.58 or 19.121.223.58:8000
"""
ip = str(self.ip_v4_object())
if with_port:
port = self.port(port_range=port_range)
return '{}:{}'.format(ip, port)
return ip
def port(self, port_range: PortRange = PortRange.ALL) -> int:
"""Generate random port.
:param port_range: PortRange enum object.
:return: Port number.
:raises NonEnumerableError: if port_range is not in PortRange.
:Example:
8080
"""
if isinstance(port_range, PortRange):
return self.random.randint(*port_range.value)
else:
raise NonEnumerableError(PortRange)