Esempio n. 1
0
 def test_nxdomain(self):
     res = self._make_mock_resolver()
     rp = greendns.ResolverProxy()
     rp._resolver = res
     res.raises = greendns.dns.resolver.NXDOMAIN
     with tests.assert_raises(greendns.dns.resolver.NXDOMAIN):
         rp.query('host.example.com')
Esempio n. 2
0
 def test_getaliases_nxdomain(self):
     aliases_res = self._make_mock_resolver_aliases()
     rp = greendns.ResolverProxy()
     rp._resolver = aliases_res
     rp._resolver.call_count = 1
     rp._resolver.exc_type = greendns.dns.resolver.NXDOMAIN
     assert rp.getaliases('host.example.com') == []
Esempio n. 3
0
 def test_noanswer(self):
     res = self._make_mock_resolver()
     rp = greendns.ResolverProxy()
     rp._resolver = res
     res.raises = greendns.dns.resolver.NoAnswer
     with tests.assert_raises(greendns.dns.resolver.NoAnswer):
         rp.query('host.example.com')
Esempio n. 4
0
 def test_hosts_noanswer(self):
     hostsres = self._make_mock_hostsresolver()
     res = self._make_mock_resolver()
     rp = greendns.ResolverProxy(hostsres)
     rp._resolver = res
     hostsres.raises = greendns.dns.resolver.NoAnswer
     ans = rp.query('host.example.com')
     assert ans[0].address == '5.6.7.8'
Esempio n. 5
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)
Esempio n. 6
0
def test_proxy_resolve_unqualified():
    # https://github.com/eventlet/eventlet/issues/363
    rp = greendns.ResolverProxy(filename=None)
    rp._resolver.search.append(dns.name.from_text('example.com'))
    with tests.mock.patch('dns.resolver.Resolver.query', side_effect=dns.resolver.NoAnswer) as m:
        try:
            rp.query('machine')
            assert False, 'Expected NoAnswer exception'
        except dns.resolver.NoAnswer:
            pass
        assert any(call[0][0] == dns.name.from_text('machine') for call in m.call_args_list)
        assert any(call[0][0] == dns.name.from_text('machine.') for call in m.call_args_list)
Esempio n. 7
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
Esempio n. 8
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]
Esempio n. 9
0
def test_hosts_priority():
    name = 'example.com'
    addr_from_ns = '1.0.2.0'

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

    # Default behavior
    rrns = greendns.resolve(name, _proxy=rp).rrset[0]
    assert rrns.address == addr_from_ns

    # Hosts result must shadow that from nameservers
    hr.hosts.write(b'1.2.3.4 example.com\ndead:beef::1 example.com\n')
    hr.hosts.flush()
    hr._load()
    rrs4 = greendns.resolve(name, family=socket.AF_INET, _proxy=rp).rrset
    assert len(rrs4) == 1
    assert rrs4[0].address == '1.2.3.4', rrs4[0].address
    rrs6 = greendns.resolve(name, family=socket.AF_INET6, _proxy=rp).rrset
    assert len(rrs6) == 1
    assert rrs6[0].address == 'dead:beef::1', rrs6[0].address
Esempio n. 10
0
 def test_getaliases_fqdn(self):
     aliases_res = self._make_mock_resolver_aliases()
     rp = greendns.ResolverProxy()
     rp._resolver = aliases_res
     rp._resolver.call_count = 1
     assert rp.getaliases('host.example.com') == []
Esempio n. 11
0
 def test_getaliases(self):
     aliases_res = self._make_mock_resolver_aliases()
     rp = greendns.ResolverProxy()
     rp._resolver = aliases_res
     aliases = set(rp.getaliases('alias.example.com'))
     assert aliases == set(['host.example.com'])
Esempio n. 12
0
 def test_resolver(self):
     res = self._make_mock_resolver()
     rp = greendns.ResolverProxy()
     rp._resolver = res
     ans = rp.query('host.example.com')
     assert ans[0].address == '5.6.7.8'
Esempio n. 13
0
 def test_hosts(self):
     hostsres = self._make_mock_hostsresolver()
     rp = greendns.ResolverProxy(hostsres)
     ans = rp.query('host.example.com')
     assert ans[0].address == '1.2.3.4'
Esempio n. 14
0
 def test_clear(self):
     rp = greendns.ResolverProxy()
     resolver = rp._resolver
     rp.clear()
     assert rp._resolver != resolver