Example #1
0
 def str_to_hostentry(entry, sanitize=False, single_format=False):
     """
     Transform a line from a hosts file into an instance of HostsEntry
     :param entry: A line from the hosts file
     :return: An instance of HostsEntry
     """
     line_parts = entry.strip().split()
     address_part = line_parts[0]
     if is_ipv4(address_part) and valid_hostnames(line_parts[1:]):
         if sanitize:
             address_part = '0.0.0.0'
         return HostsEntry(entry_type='ipv4',
                           address=address_part,
                           names=line_parts[1:])
     elif is_ipv6(address_part) and valid_hostnames(line_parts[1:]):
         if sanitize:
             address_part = '::'
         return HostsEntry(entry_type='ipv6',
                           address=address_part,
                           names=line_parts[1:])
     elif single_format and len(line_parts) == 1 and valid_hostnames(
             line_parts):
         address_part = '0.0.0.0'
         return HostsEntry(entry_type='ipv4',
                           address=address_part,
                           names=line_parts)
     else:
         return False
Example #2
0
 def str_to_hostentry(entry):
     """
     Transform a line from a hosts file into an instance of HostsEntry
     :param entry: A line from the hosts file
     :return: An instance of HostsEntry
     """
     line_parts = entry.strip().split()
     if is_ipv4(line_parts[0]) and valid_hostnames(line_parts[1:]):
         return HostsEntry(entry_type='ipv4',
                           address=line_parts[0],
                           names=line_parts[1:])
     elif is_ipv6(line_parts[0]) and valid_hostnames(line_parts[1:]):
         return HostsEntry(entry_type='ipv6',
                           address=line_parts[0],
                           names=line_parts[1:])
     else:
         return False
Example #3
0
 def str_to_hostentry(entry):
     """
     Transform a line from a hosts file into an instance of HostsEntry
     :param entry: A line from the hosts file
     :return: An instance of HostsEntry
     """
     line_parts = entry.strip().split()
     if is_ipv4(line_parts[0]) and valid_hostnames(line_parts[1:]):
         return HostsEntry(entry_type='ipv4',
                           address=line_parts[0],
                           names=line_parts[1:])
     elif is_ipv6(line_parts[0]) and valid_hostnames(line_parts[1:]):
         return HostsEntry(entry_type='ipv6',
                           address=line_parts[0],
                           names=line_parts[1:])
     else:
         return False
def test_hostname_validation_failure_with_leading_hyphen():
    """
    Test function returns False if a hostname with a leading hyphen is specified
    """
    assert not valid_hostnames(['example.com', '-example'])
def test_hostname_validation_failure_too_long():
    """
    Test function returns False if a hostname over 255 chars is specified
    """
    long_hostname = 'x' * 256
    assert not valid_hostnames(['example.com', long_hostname])
def test_hostname_validation_success():
    """
    Test function returns True if valid hostnames are specified
    """
    assert valid_hostnames(['example.com', 'example'])