def test_isIPv4_randomIP4(self): """Test :func:`addr.isIPv4` with a random IPv4 address. This test asserts that the returned value is a :obj:`bool`. Because the IP being tested is random, it *could* randomly be an invalid IP address and thus :func:`~bridgdb.addr.isIPv4` would return ``False``). """ randomAddr = ipaddr.IPv4Address(random.getrandbits(32)).compressed log.msg("Testing randomly generated IPv4 address: %s" % randomAddr) result = addr.isIPv4(randomAddr) self.assertTrue(isinstance(result, bool), "addr.isIPv4() should be boolean: %r" % type(result))
def runTestForIPv6(self, testAddress): """Test :func:`addr.isIPv4` with the specified IPv6 **testAddress**. This test asserts that the returned value is ``False``. :param str testAddress: A string which specifies the IPv6 address to test, which should cause :func:`addr.isIPv4` to return ``False``. """ result = addr.isIPv4(testAddress) log.msg("addr.isIPv4(%r) => %s" % (testAddress, result)) self.assertTrue(isinstance(result, bool), "addr.isIPv4() should be boolean: %r" % type(result)) self.assertFalse(result, "addr.isIPv4(%r) should be False!" % testAddress)
def doubleCheck(self, match): """Additional check to ensure that **match** is an IPv4 address.""" if addr.isIPv4(match): return True
def parseALine(line, fingerprint=None): """Parse an 'a'-line of a bridge networkstatus document. From torspec.git/dir-spec.txt, commit 36761c7d553d L1499-1512: | | "a" SP address ":" port NL | | [Any number.] | | Present only if the OR has at least one IPv6 address. | | Address and portlist are as for "or-address" as specified in | 2.1. | | (Only included when the vote or consensus is generated with | consensus-method 14 or later.) :param string line: An 'a'-line from an bridge-network-status descriptor. :type fingerprint: string or None :param fingerprint: A string which identifies which OR the descriptor we're parsing came from (since the 'a'-line doesn't tell us, this can help make the log messages clearer). :raises: :exc:`NetworkstatusParsingError` :rtype: tuple :returns: A 2-tuple of a string respresenting the IP address and a :class:`bridgedb.parse.addr.PortList`. """ ip = None portlist = None if line.startswith('a '): line = line[2:] # Chop off the 'a ' else: logging.warn("Networkstatus parser received non 'a'-line for %r:"\ " %r" % (fingerprint or 'Unknown', line)) try: ip, portlist = line.rsplit(':', 1) except ValueError as error: logging.error("Bad separator in networkstatus 'a'-line: %r" % line) return (None, None) if ip.startswith('[') and ip.endswith(']'): ip = ip.strip('[]') try: if not addr.isIPAddress(ip): raise NetworkstatusParsingError( "Got invalid IP Address in networkstatus 'a'-line for %r: %r" % (fingerprint or 'Unknown', line)) if addr.isIPv4(ip): warnings.warn(FutureWarning( "Got IPv4 address in networkstatus 'a'-line! "\ "Networkstatus document format may have changed!")) except NetworkstatusParsingError as error: logging.error(error) ip, portlist = None, None try: portlist = addr.PortList(portlist) if not portlist: raise NetworkstatusParsingError( "Got invalid portlist in 'a'-line for %r!\n Line: %r" % (fingerprint or 'Unknown', line)) except (addr.InvalidPort, NetworkstatusParsingError) as error: logging.error(error) portlist = None else: logging.debug("Parsed networkstatus ORAddress line for %r:"\ "\n Address: %s \tPorts: %s" % (fingerprint or 'Unknown', ip, portlist)) finally: return (ip, portlist)