Exemple #1
0
    def test_getaddrinfo_hosts_only_dns_error(self):
        hostsres = _make_mock_base_resolver()
        hostsres.raises = greendns.dns.resolver.NoAnswer
        greendns.resolver = greendns.ResolverProxy(hostsres())
        res = _make_mock_base_resolver()
        res.raises = greendns.dns.exception.DNSException
        greendns.resolver._resolver = res()

        with tests.assert_raises(socket.gaierror):
            greendns.getaddrinfo('example.com', 0, 0)
Exemple #2
0
    def test_getaddrinfo_hosts_only_dns_error(self):
        hostsres = _make_mock_base_resolver()
        hostsres.raises = greendns.dns.resolver.NoAnswer
        greendns.resolver = greendns.ResolverProxy(hostsres())
        res = _make_mock_base_resolver()
        res.raises = greendns.dns.exception.DNSException
        greendns.resolver._resolver = res()

        with tests.assert_raises(socket.gaierror):
            greendns.getaddrinfo('example.com', 0, 0)
Exemple #3
0
 def test_AI_ADDRCONFIG_noaddr(self):
     # If AI_ADDRCONFIG is used but there is no address we need to
     # get an exception, not an empty list.
     def getaddrinfo(addr, port, family, socktype, proto, aiflags):
         raise socket.error(socket.EAI_ADDRFAMILY,
                            'Address family for hostname not supported')
     greendns.socket.getaddrinfo = getaddrinfo
     greendns.resolve = _make_mock_resolve()
     try:
         greendns.getaddrinfo('::1', None, 0, 0, 0, socket.AI_ADDRCONFIG)
     except socket.error as e:
         assert e.errno == socket.EAI_ADDRFAMILY
Exemple #4
0
 def test_AI_ADDRCONFIG_noaddr(self):
     # If AI_ADDRCONFIG is used but there is no address we need to
     # get an exception, not an empty list.
     def getaddrinfo(addr, port, family, socktype, proto, aiflags):
         raise socket.error(socket.EAI_ADDRFAMILY,
                            'Address family for hostname not supported')
     greendns.socket.getaddrinfo = getaddrinfo
     greendns.resolve = _make_mock_resolve()
     try:
         greendns.getaddrinfo('::1', None, 0, 0, 0, socket.AI_ADDRCONFIG)
     except socket.error as e:
         assert e.errno == socket.EAI_ADDRFAMILY
def test_hosts_no_network():

    name = 'example.com'
    addr_from_ns = '1.0.2.0'
    addr6_from_ns = 'dead:beef::1'

    hr = _make_host_resolver()
    rp = greendns.ResolverProxy(hosts_resolver=hr, filename=None)
    base = _make_mock_base_resolver()
    base.rr.address = addr_from_ns
    base.rr6.address = addr6_from_ns
    rp._resolver = base()

    with tests.mock.patch.object(
            greendns,
            'resolver',
            new_callable=tests.mock.PropertyMock(return_value=rp)):
        res = greendns.getaddrinfo('example.com', 'domain', socket.AF_UNSPEC)
        # Default behavior
        addr = (addr_from_ns, 53)
        tcp = (socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP, addr)
        udp = (socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP, addr)
        addr = (addr6_from_ns, 53, 0, 0)
        tcp6 = (socket.AF_INET6, socket.SOCK_STREAM, socket.IPPROTO_TCP, addr)
        udp6 = (socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP, addr)
        filt_res = [ai[:3] + (ai[4], ) for ai in res]
        assert tcp in filt_res
        assert udp in filt_res
        assert tcp6 in filt_res
        assert udp6 in filt_res

        # Hosts result must shadow that from nameservers
        hr = _make_host_resolver()
        hr.hosts.write(b'1.2.3.4 example.com')
        hr.hosts.flush()
        hr._load()
        greendns.resolver._hosts = hr

        res = greendns.getaddrinfo('example.com', 'domain', socket.AF_UNSPEC)
        filt_res = [ai[:3] + (ai[4], ) for ai in res]

        # Make sure that only IPv4 entry from hosts is present.
        assert tcp not in filt_res
        assert udp not in filt_res
        assert tcp6 not in filt_res
        assert udp6 not in filt_res

        addr = ('1.2.3.4', 53)
        tcp = (socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP, addr)
        udp = (socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP, addr)
        assert tcp in filt_res
        assert udp in filt_res
Exemple #6
0
def test_hosts_no_network():

    name = 'example.com'
    addr_from_ns = '1.0.2.0'
    addr6_from_ns = 'dead:beef::1'

    hr = _make_host_resolver()
    rp = greendns.ResolverProxy(hosts_resolver=hr, filename=None)
    base = _make_mock_base_resolver()
    base.rr.address = addr_from_ns
    base.rr6.address = addr6_from_ns
    rp._resolver = base()

    with tests.mock.patch.object(greendns, 'resolver',
                                 new_callable=tests.mock.PropertyMock(return_value=rp)):
        res = greendns.getaddrinfo('example.com', 'domain', socket.AF_UNSPEC)
        # Default behavior
        addr = (addr_from_ns, 53)
        tcp = (socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP, addr)
        udp = (socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP, addr)
        addr = (addr6_from_ns, 53, 0, 0)
        tcp6 = (socket.AF_INET6, socket.SOCK_STREAM, socket.IPPROTO_TCP, addr)
        udp6 = (socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP, addr)
        filt_res = [ai[:3] + (ai[4],) for ai in res]
        assert tcp in filt_res
        assert udp in filt_res
        assert tcp6 in filt_res
        assert udp6 in filt_res

        # Hosts result must shadow that from nameservers
        hr = _make_host_resolver()
        hr.hosts.write(b'1.2.3.4 example.com')
        hr.hosts.flush()
        hr._load()
        greendns.resolver._hosts = hr

        res = greendns.getaddrinfo('example.com', 'domain', socket.AF_UNSPEC)
        filt_res = [ai[:3] + (ai[4],) for ai in res]

        # Make sure that only IPv4 entry from hosts is present.
        assert tcp not in filt_res
        assert udp not in filt_res
        assert tcp6 not in filt_res
        assert udp6 not in filt_res

        addr = ('1.2.3.4', 53)
        tcp = (socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP, addr)
        udp = (socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP, addr)
        assert tcp in filt_res
        assert udp in filt_res
Exemple #7
0
 def test_v4mapped(self):
     greendns.resolve = _make_mock_resolve()
     greendns.resolve.add('example.com', '1.2.3.4')
     res = greendns.getaddrinfo('example.com', 80,
                                socket.AF_INET6, 0, 0, socket.AI_V4MAPPED)
     addrs = set(ai[-1] for ai in res)
     assert addrs == set([('::ffff:1.2.3.4', 80, 0, 0)])
def _get_transport(name):
    # Annoyingly, pysnmp does not automatically determine ipv6 v ipv4
    res = getaddrinfo(name, 161, 0, socket.SOCK_DGRAM)
    if res[0][0] == socket.AF_INET6:
        return snmp.Udp6TransportTarget(res[0][4])
    else:
        return snmp.UdpTransportTarget(res[0][4])
Exemple #9
0
 def test_canonname(self):
     greendns.resolve = _make_mock_resolve()
     greendns.resolve.add('host.example.com', '1.2.3.4')
     greendns.resolve_cname = self._make_mock_resolve_cname()
     res = greendns.getaddrinfo('host.example.com', 0, 0, 0, 0,
                                socket.AI_CANONNAME)
     assert res[0][3] == 'cname.example.com'
Exemple #10
0
 def test_v4mapped(self):
     greendns.resolve = _make_mock_resolve()
     greendns.resolve.add('example.com', '1.2.3.4')
     res = greendns.getaddrinfo('example.com', 80, socket.AF_INET6, 0, 0,
                                socket.AI_V4MAPPED)
     addrs = set(ai[-1] for ai in res)
     assert addrs == set([('::ffff:1.2.3.4', 80, 0, 0)])
Exemple #11
0
 def test_canonname(self):
     greendns.resolve = _make_mock_resolve()
     greendns.resolve.add('host.example.com', '1.2.3.4')
     greendns.resolve_cname = self._make_mock_resolve_cname()
     res = greendns.getaddrinfo('host.example.com', 0,
                                0, 0, 0, socket.AI_CANONNAME)
     assert res[0][3] == 'cname.example.com'
Exemple #12
0
def _get_transport(name):
    # Annoyingly, pysnmp does not automatically determine ipv6 v ipv4
    res = getaddrinfo(name, 161, 0, socket.SOCK_DGRAM)
    if res[0][0] == socket.AF_INET6:
        return snmp.Udp6TransportTarget(res[0][4])
    else:
        return snmp.UdpTransportTarget(res[0][4])
Exemple #13
0
 def test_getaddrinfo_inet6(self):
     greendns.resolve = _make_mock_resolve()
     greendns.resolve.add('example.com', '::1')
     res = greendns.getaddrinfo('example.com', 'ssh', socket.AF_INET6)
     addr = ('::1', 22, 0, 0)
     tcp = (socket.AF_INET6, socket.SOCK_STREAM, socket.IPPROTO_TCP, addr)
     udp = (socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP, addr)
     assert tcp in [ai[:3] + (ai[4],) for ai in res]
     assert udp in [ai[:3] + (ai[4],) for ai in res]
Exemple #14
0
 def test_getaddrinfo_inet6(self):
     greendns.resolve = _make_mock_resolve()
     greendns.resolve.add('example.com', '::1')
     res = greendns.getaddrinfo('example.com', 'domain', socket.AF_INET6)
     addr = ('::1', 53, 0, 0)
     tcp = (socket.AF_INET6, socket.SOCK_STREAM, socket.IPPROTO_TCP, addr)
     udp = (socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP, addr)
     assert tcp in [ai[:3] + (ai[4], ) for ai in res]
     assert udp in [ai[:3] + (ai[4], ) for ai in res]
 def test_getaddrinfo_inet(self):
     greendns.resolve = _make_mock_resolve()
     greendns.resolve.add('example.com', '127.0.0.2')
     res = greendns.getaddrinfo('example.com', 'ssh', socket.AF_INET)
     addr = ('127.0.0.2', 22)
     tcp = (socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP, addr)
     udp = (socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP, addr)
     assert tcp in [ai[:3] + (ai[4], ) for ai in res]
     assert udp in [ai[:3] + (ai[4], ) for ai in res]
Exemple #16
0
 def test_getaddrinfo_inet(self):
     greendns.resolve = _make_mock_resolve()
     greendns.resolve.add('example.com', '127.0.0.2')
     res = greendns.getaddrinfo('example.com', 'domain', socket.AF_INET)
     addr = ('127.0.0.2', 53)
     tcp = (socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP, addr)
     udp = (socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP, addr)
     assert tcp in [ai[:3] + (ai[4],) for ai in res]
     assert udp in [ai[:3] + (ai[4],) for ai in res]
Exemple #17
0
 def test_v4mapped_all(self):
     greendns.resolve = _make_mock_resolve()
     greendns.resolve.add('example.com', '1.2.3.4')
     greendns.resolve.add('example.com', 'dead:beef::1')
     res = greendns.getaddrinfo('example.com', 80, socket.AF_INET6, 0, 0,
                                socket.AI_V4MAPPED | socket.AI_ALL)
     addrs = set(ai[-1] for ai in res)
     for addr in addrs:
         assert addr in [('::ffff:1.2.3.4', 80, 0, 0),
                         ('dead:beef::1', 80, 0, 0)]
Exemple #18
0
 def test_v4mapped_all(self):
     greendns.resolve = _make_mock_resolve()
     greendns.resolve.add('example.com', '1.2.3.4')
     greendns.resolve.add('example.com', 'dead:beef::1')
     res = greendns.getaddrinfo('example.com', 80, socket.AF_INET6, 0, 0,
                                socket.AI_V4MAPPED | socket.AI_ALL)
     addrs = set(ai[-1] for ai in res)
     for addr in addrs:
         assert addr in [('::ffff:1.2.3.4', 80, 0, 0),
                         ('dead:beef::1', 80, 0, 0)]
Exemple #19
0
 def test_getaddrinfo_idn(self):
     greendns.resolve = _make_mock_resolve()
     idn_name = u'евентлет.com'
     greendns.resolve.add(idn_name.encode('idna').decode('ascii'), '127.0.0.2')
     res = greendns.getaddrinfo(idn_name, 'domain')
     addr = ('127.0.0.2', 53)
     tcp = (socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP, addr)
     udp = (socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP, addr)
     filt_res = [ai[:3] + (ai[4],) for ai in res]
     assert tcp in filt_res
     assert udp in filt_res
Exemple #20
0
 def test_getaddrinfo_idn(self):
     greendns.resolve = _make_mock_resolve()
     idn_name = u'евентлет.com'
     greendns.resolve.add(idn_name.encode('idna').decode('ascii'), '127.0.0.2')
     res = greendns.getaddrinfo(idn_name, 'ssh')
     addr = ('127.0.0.2', 22)
     tcp = (socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP, addr)
     udp = (socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP, addr)
     filt_res = [ai[:3] + (ai[4],) for ai in res]
     assert tcp in filt_res
     assert udp in filt_res
Exemple #21
0
def node_by_manager(manager):
    """Lookup a node by manager

    Search for a node according to a given network address.
    Rather than do a simple equality, it uses getaddrinfo
    to allow name or ip and different forms of ip.  For
    example, 'fe80::0001' will match 'fe80::01' and
    '127.000.000.001' will match '127.0.0.1'

    :param manager: The ip or resolvable name of the manager

    :returns: The node name (if any)
    """

    manageraddresses = []
    for tmpaddr in greendns.getaddrinfo(manager, None):
        manageraddresses.append(tmpaddr[4][0])
    cfm = configmanager.ConfigManager(None)
    if manager in manager_to_nodemap:
        # We have a stored hint as to the most probably correct answer
        # put that node at the head of the list in hopes of reducing
        # iterations for a lookup in a large environment
        # However we don't trust the answer either, since
        # reconfiguration could have changed it and this mapping
        # is not hooked into getting updates
        check_nodes = itertools.chain(
            (manager_to_nodemap[manager],), cfm.list_nodes())
    else:
        check_nodes = cfm.list_nodes()
    hmattribs = cfm.get_node_attributes(check_nodes,
                                        ('hardwaremanagement.manager',))
    for node in hmattribs:
        currhm = hmattribs[node]['hardwaremanagement.manager']['value']
        if currhm in manageraddresses:
            manager_to_nodemap[manager] = node
            return node
        for curraddr in greendns.getaddrinfo(currhm, None):
            curraddr = curraddr[4][0]
            if curraddr in manageraddresses:
                manager_to_nodemap[manager] = node
                return node
Exemple #22
0
 def test_getaddrinfo(self):
     greendns.resolve = _make_mock_resolve()
     greendns.resolve.add('example.com', '127.0.0.2')
     greendns.resolve.add('example.com', '::1')
     res = greendns.getaddrinfo('example.com', 'ssh')
     addr = ('127.0.0.2', 22)
     tcp = (socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP, addr)
     udp = (socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP, addr)
     addr = ('::1', 22, 0, 0)
     tcp6 = (socket.AF_INET6, socket.SOCK_STREAM, socket.IPPROTO_TCP, addr)
     udp6 = (socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP, addr)
     filt_res = [ai[:3] + (ai[4],) for ai in res]
     assert tcp in filt_res
     assert udp in filt_res
     assert tcp6 in filt_res
     assert udp6 in filt_res
Exemple #23
0
    def test_getaddrinfo_hosts_only_ans_with_error(self):
        def clear_raises(res_self):
            res_self.raises = None
            return greendns.dns.resolver.NoAnswer()

        hostsres = _make_mock_base_resolver()
        hostsres.raises = clear_raises
        hostsres.rr.address = '1.2.3.4'
        greendns.resolver = greendns.ResolverProxy(hostsres())
        res = _make_mock_base_resolver()
        res.raises = greendns.dns.exception.DNSException
        greendns.resolver._resolver = res()

        result = greendns.getaddrinfo('example.com', 0, 0)
        addr = [('1.2.3.4', 0)] * len(result)
        assert addr == [ai[-1] for ai in result]
Exemple #24
0
    def test_getaddrinfo_hosts_only_ans_with_error(self):
        def clear_raises(res_self):
            res_self.raises = None
            return greendns.dns.resolver.NoAnswer()

        hostsres = _make_mock_base_resolver()
        hostsres.raises = clear_raises
        hostsres.rr.address = '1.2.3.4'
        greendns.resolver = greendns.ResolverProxy(hostsres())
        res = _make_mock_base_resolver()
        res.raises = greendns.dns.exception.DNSException
        greendns.resolver._resolver = res()

        result = greendns.getaddrinfo('example.com', 0, 0)
        addr = [('1.2.3.4', 0)] * len(result)
        assert addr == [ai[-1] for ai in result]
Exemple #25
0
 def test_AI_ADDRCONFIG(self):
     # When the users sets AI_ADDRCONFIG but only has an IPv4
     # address configured we will iterate over the results, but the
     # call for the IPv6 address will fail rather then return an
     # empty list.  In that case we should catch the exception and
     # only return the ones which worked.
     def getaddrinfo(addr, port, family, socktype, proto, aiflags):
         if addr == '127.0.0.1':
             return [(socket.AF_INET, 1, 0, '', ('127.0.0.1', 0))]
         elif addr == '::1' and aiflags & socket.AI_ADDRCONFIG:
             raise socket.error(socket.EAI_ADDRFAMILY,
                                'Address family for hostname not supported')
         elif addr == '::1' and not aiflags & socket.AI_ADDRCONFIG:
             return [(socket.AF_INET6, 1, 0, '', ('::1', 0, 0, 0))]
     greendns.socket.getaddrinfo = getaddrinfo
     greendns.resolve = _make_mock_resolve()
     greendns.resolve.add('localhost', '127.0.0.1')
     greendns.resolve.add('localhost', '::1')
     res = greendns.getaddrinfo('localhost', None,
                                0, 0, 0, socket.AI_ADDRCONFIG)
     assert res == [(socket.AF_INET, 1, 0, '', ('127.0.0.1', 0))]
Exemple #26
0
 def test_AI_ADDRCONFIG(self):
     # When the users sets AI_ADDRCONFIG but only has an IPv4
     # address configured we will iterate over the results, but the
     # call for the IPv6 address will fail rather then return an
     # empty list.  In that case we should catch the exception and
     # only return the ones which worked.
     def getaddrinfo(addr, port, family, socktype, proto, aiflags):
         if addr == '127.0.0.1':
             return [(socket.AF_INET, 1, 0, '', ('127.0.0.1', 0))]
         elif addr == '::1' and aiflags & socket.AI_ADDRCONFIG:
             raise socket.error(socket.EAI_ADDRFAMILY,
                                'Address family for hostname not supported')
         elif addr == '::1' and not aiflags & socket.AI_ADDRCONFIG:
             return [(socket.AF_INET6, 1, 0, '', ('::1', 0, 0, 0))]
     greendns.socket.getaddrinfo = getaddrinfo
     greendns.resolve = _make_mock_resolve()
     greendns.resolve.add('localhost', '127.0.0.1')
     greendns.resolve.add('localhost', '::1')
     res = greendns.getaddrinfo('localhost', None,
                                0, 0, 0, socket.AI_ADDRCONFIG)
     assert res == [(socket.AF_INET, 1, 0, '', ('127.0.0.1', 0))]
Exemple #27
0
 def test_host_none_passive(self):
     res = greendns.getaddrinfo(None, 80, 0, 0, 0, socket.AI_PASSIVE)
     for addr in set(ai[-1] for ai in res):
         assert addr in [('0.0.0.0', 80), ('::', 80, 0, 0)]
Exemple #28
0
 def test_host_none(self):
     res = greendns.getaddrinfo(None, 80)
     for addr in set(ai[-1] for ai in res):
         assert addr in [('127.0.0.1', 80), ('::1', 80, 0, 0)]
Exemple #29
0
 def test_getaddrinfo_only_aaaa_ans(self):
     greendns.resolve = _make_mock_resolve()
     greendns.resolve.add('example.com', 'dead:beef::1')
     res = greendns.getaddrinfo('example.com', 0)
     addr = [('dead:beef::1', 0, 0, 0)] * len(res)
     assert addr == [ai[-1] for ai in res]
Exemple #30
0
 def test_getaddrinfo_only_aaaa_ans(self):
     greendns.resolve = _make_mock_resolve()
     greendns.resolve.add('example.com', 'dead:beef::1')
     res = greendns.getaddrinfo('example.com', 0)
     addr = [('dead:beef::1', 0, 0, 0)] * len(res)
     assert addr == [ai[-1] for ai in res]
Exemple #31
0
 def test_noport(self):
     greendns.resolve = _make_mock_resolve()
     greendns.resolve.add('example.com', '1.2.3.4')
     ai = greendns.getaddrinfo('example.com', None)
     assert ai[0][-1][1] == 0
Exemple #32
0
 def test_numerichost(self):
     greendns.resolve = _make_mock_resolve()
     greendns.resolve.add('example.com', '1.2.3.4')
     with tests.assert_raises(socket.gaierror):
         greendns.getaddrinfo('example.com', 80, 0, 0, 0, socket.AI_NUMERICHOST)
Exemple #33
0
 def test_host_none(self):
     res = greendns.getaddrinfo(None, 80)
     for addr in set(ai[-1] for ai in res):
         assert addr in [('127.0.0.1', 80), ('::1', 80, 0, 0)]
Exemple #34
0
 def test_numerichost(self):
     greendns.resolve = _make_mock_resolve()
     greendns.resolve.add('example.com', '1.2.3.4')
     with tests.assert_raises(socket.gaierror):
         greendns.getaddrinfo('example.com', 80, 0, 0, 0,
                              socket.AI_NUMERICHOST)
Exemple #35
0
 def test_noport(self):
     greendns.resolve = _make_mock_resolve()
     greendns.resolve.add('example.com', '1.2.3.4')
     ai = greendns.getaddrinfo('example.com', None)
     assert ai[0][-1][1] == 0
Exemple #36
0
 def test_host_none_passive(self):
     res = greendns.getaddrinfo(None, 80, 0, 0, 0, socket.AI_PASSIVE)
     for addr in set(ai[-1] for ai in res):
         assert addr in [('0.0.0.0', 80), ('::', 80, 0, 0)]