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
def test_noraise_nxdomain(self): greendns.resolver.raises = greendns.dns.resolver.NXDOMAIN ans = greendns.resolve('example.com', raises=False) assert not ans.rrset
def test_noraise_noanswer(self): greendns.resolver.rrset = None ans = greendns.resolve('example.com', raises=False) assert not ans.rrset
def test_exc(self): greendns.resolver.raises = greendns.dns.exception.DNSException with tests.assert_raises(socket.gaierror): greendns.resolve('host.example.com')
def test_timeout(self): greendns.resolver.raises = greendns.dns.exception.Timeout with tests.assert_raises(socket.gaierror): greendns.resolve('host.example.com')
def test_unknown_rdtype(self): with tests.assert_raises(socket.gaierror): greendns.resolve('host.example.com', socket.AF_INET6 + 1)
def test_AAAA(self): greendns.resolver.rr.address = 'dead:beef::1' ans = greendns.resolve('host.example.com', socket.AF_INET6) assert ans[0].address == 'dead:beef::1' assert greendns.resolver.args == ('host.example.com', dns.rdatatype.AAAA)
def test_A(self): ans = greendns.resolve('host.example.com', socket.AF_INET) assert ans[0].address == '1.2.3.4' assert greendns.resolver.args == ('host.example.com', dns.rdatatype.A)