예제 #1
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
예제 #2
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
예제 #3
0
 def test_noraise_nxdomain(self):
     greendns.resolver.raises = greendns.dns.resolver.NXDOMAIN
     ans = greendns.resolve('example.com', raises=False)
     assert not ans.rrset
예제 #4
0
 def test_noraise_noanswer(self):
     greendns.resolver.rrset = None
     ans = greendns.resolve('example.com', raises=False)
     assert not ans.rrset
예제 #5
0
 def test_exc(self):
     greendns.resolver.raises = greendns.dns.exception.DNSException
     with tests.assert_raises(socket.gaierror):
         greendns.resolve('host.example.com')
예제 #6
0
 def test_timeout(self):
     greendns.resolver.raises = greendns.dns.exception.Timeout
     with tests.assert_raises(socket.gaierror):
         greendns.resolve('host.example.com')
예제 #7
0
 def test_unknown_rdtype(self):
     with tests.assert_raises(socket.gaierror):
         greendns.resolve('host.example.com', socket.AF_INET6 + 1)
예제 #8
0
 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)
예제 #9
0
 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)
예제 #10
0
 def test_noraise_nxdomain(self):
     greendns.resolver.raises = greendns.dns.resolver.NXDOMAIN
     ans = greendns.resolve('example.com', raises=False)
     assert not ans.rrset
예제 #11
0
 def test_noraise_noanswer(self):
     greendns.resolver.rrset = None
     ans = greendns.resolve('example.com', raises=False)
     assert not ans.rrset
예제 #12
0
 def test_exc(self):
     greendns.resolver.raises = greendns.dns.exception.DNSException
     with tests.assert_raises(socket.gaierror):
         greendns.resolve('host.example.com')
예제 #13
0
 def test_timeout(self):
     greendns.resolver.raises = greendns.dns.exception.Timeout
     with tests.assert_raises(socket.gaierror):
         greendns.resolve('host.example.com')
예제 #14
0
 def test_unknown_rdtype(self):
     with tests.assert_raises(socket.gaierror):
         greendns.resolve('host.example.com', socket.AF_INET6 + 1)
예제 #15
0
 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)
예제 #16
0
 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)