class HostsTestCase(TestCase): """ Tests for the I{hosts(5)}-based L{twisted.names.hosts.Resolver}. """ def setUp(self): f = open('EtcHosts', 'w') f.write(''' 1.1.1.1 EXAMPLE EXAMPLE.EXAMPLETHING ::2 mixed 1.1.1.2 MIXED ::1 ip6thingy 1.1.1.3 multiple 1.1.1.4 multiple ::3 ip6-multiple ::4 ip6-multiple ''') f.close() self.ttl = 4200 self.resolver = Resolver('EtcHosts', self.ttl) def testGetHostByName(self): data = [('EXAMPLE', '1.1.1.1'), ('EXAMPLE.EXAMPLETHING', '1.1.1.1'), ('MIXED', '1.1.1.2'), ] ds = [self.resolver.getHostByName(n).addCallback(self.assertEqual, ip) for n, ip in data] return gatherResults(ds) def test_lookupAddress(self): """ L{hosts.Resolver.lookupAddress} returns a L{Deferred} which fires with A records from the hosts file. """ d = self.resolver.lookupAddress('multiple') def resolved((results, authority, additional)): self.assertEqual( (RRHeader("multiple", A, IN, self.ttl, Record_A("1.1.1.3", self.ttl)), RRHeader("multiple", A, IN, self.ttl, Record_A("1.1.1.4", self.ttl))), results) d.addCallback(resolved) return d def test_lookupIPV6Address(self): """ L{hosts.Resolver.lookupIPV6Address} returns a L{Deferred} which fires with AAAA records from the hosts file. """ d = self.resolver.lookupIPV6Address('ip6-multiple') def resolved((results, authority, additional)): self.assertEqual( (RRHeader("ip6-multiple", AAAA, IN, self.ttl, Record_AAAA("::3", self.ttl)), RRHeader("ip6-multiple", AAAA, IN, self.ttl, Record_AAAA("::4", self.ttl))), results) d.addCallback(resolved) return d def test_lookupAllRecords(self): """ L{hosts.Resolver.lookupAllRecords} returns a L{Deferred} which fires with A records from the hosts file. """ d = self.resolver.lookupAllRecords('mixed') def resolved((results, authority, additional)): self.assertEqual( (RRHeader("mixed", A, IN, self.ttl, Record_A("1.1.1.2", self.ttl)),), results) d.addCallback(resolved) return d def testNotImplemented(self): return self.assertFailure(self.resolver.lookupMailExchange('EXAMPLE'), NotImplementedError) def testQuery(self): d = self.resolver.query(Query('EXAMPLE')) d.addCallback(lambda x: self.assertEqual(x[0][0].payload.dottedQuad(), '1.1.1.1')) return d def test_lookupAddressNotFound(self): """ L{hosts.Resolver.lookupAddress} returns a L{Deferred} which fires with L{dns.DomainError} if the name passed in has no addresses in the hosts file. """ return self.assertFailure(self.resolver.lookupAddress('foueoa'), DomainError) def test_lookupIPV6AddressNotFound(self): """ Like L{test_lookupAddressNotFound}, but for L{hosts.Resolver.lookupIPV6Address}. """ return self.assertFailure(self.resolver.lookupIPV6Address('foueoa'), DomainError) def test_lookupAllRecordsNotFound(self): """ Like L{test_lookupAddressNotFound}, but for L{hosts.Resolver.lookupAllRecords}. """ return self.assertFailure(self.resolver.lookupAllRecords('foueoa'), DomainError)
class HostsTests(SynchronousTestCase, GoodTempPathMixin): """ Tests for the I{hosts(5)}-based L{twisted.names.hosts.Resolver}. """ def setUp(self): f = self.path() f.setContent( b""" 1.1.1.1 EXAMPLE EXAMPLE.EXAMPLETHING ::2 mixed 1.1.1.2 MIXED ::1 ip6thingy 1.1.1.3 multiple 1.1.1.4 multiple ::3 ip6-multiple ::4 ip6-multiple not-an-ip malformed malformed # malformed 1.1.1.5 malformed ::5 malformed """ ) self.ttl = 4200 self.resolver = Resolver(f.path, self.ttl) def test_defaultPath(self): """ The default hosts file used by L{Resolver} is I{/etc/hosts} if no value is given for the C{file} initializer parameter. """ resolver = Resolver() self.assertEqual(b"/etc/hosts", resolver.file) def test_getHostByName(self): """ L{hosts.Resolver.getHostByName} returns a L{Deferred} which fires with a string giving the address of the queried name as found in the resolver's hosts file. """ data = [ (b"EXAMPLE", "1.1.1.1"), (b"EXAMPLE.EXAMPLETHING", "1.1.1.1"), (b"MIXED", "1.1.1.2"), ] ds = [ self.resolver.getHostByName(n).addCallback(self.assertEqual, ip) for n, ip in data ] self.successResultOf(gatherResults(ds)) def test_lookupAddress(self): """ L{hosts.Resolver.lookupAddress} returns a L{Deferred} which fires with A records from the hosts file. """ d = self.resolver.lookupAddress(b"multiple") answers, authority, additional = self.successResultOf(d) self.assertEqual( ( RRHeader(b"multiple", A, IN, self.ttl, Record_A("1.1.1.3", self.ttl)), RRHeader(b"multiple", A, IN, self.ttl, Record_A("1.1.1.4", self.ttl)), ), answers, ) def test_lookupIPV6Address(self): """ L{hosts.Resolver.lookupIPV6Address} returns a L{Deferred} which fires with AAAA records from the hosts file. """ d = self.resolver.lookupIPV6Address(b"ip6-multiple") answers, authority, additional = self.successResultOf(d) self.assertEqual( ( RRHeader( b"ip6-multiple", AAAA, IN, self.ttl, Record_AAAA("::3", self.ttl) ), RRHeader( b"ip6-multiple", AAAA, IN, self.ttl, Record_AAAA("::4", self.ttl) ), ), answers, ) def test_lookupAllRecords(self): """ L{hosts.Resolver.lookupAllRecords} returns a L{Deferred} which fires with A records from the hosts file. """ d = self.resolver.lookupAllRecords(b"mixed") answers, authority, additional = self.successResultOf(d) self.assertEqual( (RRHeader(b"mixed", A, IN, self.ttl, Record_A("1.1.1.2", self.ttl)),), answers, ) def test_notImplemented(self): """ L{hosts.Resolver} fails with L{NotImplementedError} for L{IResolver} methods it doesn't implement. """ self.failureResultOf( self.resolver.lookupMailExchange(b"EXAMPLE"), NotImplementedError ) def test_query(self): d = self.resolver.query(Query(b"EXAMPLE")) [answer], authority, additional = self.successResultOf(d) self.assertEqual(answer.payload.dottedQuad(), "1.1.1.1") def test_lookupAddressNotFound(self): """ L{hosts.Resolver.lookupAddress} returns a L{Deferred} which fires with L{dns.DomainError} if the name passed in has no addresses in the hosts file. """ self.failureResultOf(self.resolver.lookupAddress(b"foueoa"), DomainError) def test_lookupIPV6AddressNotFound(self): """ Like L{test_lookupAddressNotFound}, but for L{hosts.Resolver.lookupIPV6Address}. """ self.failureResultOf(self.resolver.lookupIPV6Address(b"foueoa"), DomainError) def test_lookupAllRecordsNotFound(self): """ Like L{test_lookupAddressNotFound}, but for L{hosts.Resolver.lookupAllRecords}. """ self.failureResultOf(self.resolver.lookupAllRecords(b"foueoa"), DomainError) def test_lookupMalformed(self): """ L{hosts.Resolver.lookupAddress} returns a L{Deferred} which fires with the valid addresses from the hosts file, ignoring any entries that aren't valid IP addresses. """ d = self.resolver.lookupAddress(b"malformed") [answer], authority, additional = self.successResultOf(d) self.assertEqual( RRHeader(b"malformed", A, IN, self.ttl, Record_A("1.1.1.5", self.ttl)), answer, ) def test_lookupIPV6Malformed(self): """ Like L{test_lookupAddressMalformed}, but for L{hosts.Resolver.lookupIPV6Address}. """ d = self.resolver.lookupIPV6Address(b"malformed") [answer], authority, additional = self.successResultOf(d) self.assertEqual( RRHeader(b"malformed", AAAA, IN, self.ttl, Record_AAAA("::5", self.ttl)), answer, )
class HostsTests(TestCase, GoodTempPathMixin): """ Tests for the I{hosts(5)}-based L{twisted.names.hosts.Resolver}. """ def setUp(self): f = self.path() f.setContent(b''' 1.1.1.1 EXAMPLE EXAMPLE.EXAMPLETHING ::2 mixed 1.1.1.2 MIXED ::1 ip6thingy 1.1.1.3 multiple 1.1.1.4 multiple ::3 ip6-multiple ::4 ip6-multiple ''') self.ttl = 4200 self.resolver = Resolver(f.path, self.ttl) def test_defaultPath(self): """ The default hosts file used by L{Resolver} is I{/etc/hosts} if no value is given for the C{file} initializer parameter. """ resolver = Resolver() self.assertEqual(b"/etc/hosts", resolver.file) def test_getHostByName(self): """ L{hosts.Resolver.getHostByName} returns a L{Deferred} which fires with a string giving the address of the queried name as found in the resolver's hosts file. """ data = [(b'EXAMPLE', '1.1.1.1'), (b'EXAMPLE.EXAMPLETHING', '1.1.1.1'), (b'MIXED', '1.1.1.2'), ] ds = [self.resolver.getHostByName(n).addCallback(self.assertEqual, ip) for n, ip in data] return gatherResults(ds) def test_lookupAddress(self): """ L{hosts.Resolver.lookupAddress} returns a L{Deferred} which fires with A records from the hosts file. """ d = self.resolver.lookupAddress(b'multiple') def resolved(results): answers, authority, additional = results self.assertEqual( (RRHeader(b"multiple", A, IN, self.ttl, Record_A("1.1.1.3", self.ttl)), RRHeader(b"multiple", A, IN, self.ttl, Record_A("1.1.1.4", self.ttl))), answers) d.addCallback(resolved) return d def test_lookupIPV6Address(self): """ L{hosts.Resolver.lookupIPV6Address} returns a L{Deferred} which fires with AAAA records from the hosts file. """ d = self.resolver.lookupIPV6Address(b'ip6-multiple') def resolved(results): answers, authority, additional = results self.assertEqual( (RRHeader(b"ip6-multiple", AAAA, IN, self.ttl, Record_AAAA("::3", self.ttl)), RRHeader(b"ip6-multiple", AAAA, IN, self.ttl, Record_AAAA("::4", self.ttl))), answers) d.addCallback(resolved) return d def test_lookupAllRecords(self): """ L{hosts.Resolver.lookupAllRecords} returns a L{Deferred} which fires with A records from the hosts file. """ d = self.resolver.lookupAllRecords(b'mixed') def resolved(results): answers, authority, additional = results self.assertEqual( (RRHeader(b"mixed", A, IN, self.ttl, Record_A("1.1.1.2", self.ttl)),), answers) d.addCallback(resolved) return d def test_notImplemented(self): return self.assertFailure(self.resolver.lookupMailExchange(b'EXAMPLE'), NotImplementedError) def test_query(self): d = self.resolver.query(Query(b'EXAMPLE')) d.addCallback(lambda x: self.assertEqual(x[0][0].payload.dottedQuad(), '1.1.1.1')) return d def test_lookupAddressNotFound(self): """ L{hosts.Resolver.lookupAddress} returns a L{Deferred} which fires with L{dns.DomainError} if the name passed in has no addresses in the hosts file. """ return self.assertFailure(self.resolver.lookupAddress(b'foueoa'), DomainError) def test_lookupIPV6AddressNotFound(self): """ Like L{test_lookupAddressNotFound}, but for L{hosts.Resolver.lookupIPV6Address}. """ return self.assertFailure(self.resolver.lookupIPV6Address(b'foueoa'), DomainError) def test_lookupAllRecordsNotFound(self): """ Like L{test_lookupAddressNotFound}, but for L{hosts.Resolver.lookupAllRecords}. """ return self.assertFailure(self.resolver.lookupAllRecords(b'foueoa'), DomainError)
class HostsTests(TestCase, GoodTempPathMixin): """ Tests for the I{hosts(5)}-based L{twisted.names.hosts.Resolver}. """ def setUp(self): f = self.path() f.setContent( b""" 1.1.1.1 EXAMPLE EXAMPLE.EXAMPLETHING ::2 mixed 1.1.1.2 MIXED ::1 ip6thingy 1.1.1.3 multiple 1.1.1.4 multiple ::3 ip6-multiple ::4 ip6-multiple """ ) self.ttl = 4200 self.resolver = Resolver(f.path, self.ttl) def test_defaultPath(self): """ The default hosts file used by L{Resolver} is I{/etc/hosts} if no value is given for the C{file} initializer parameter. """ resolver = Resolver() self.assertEqual(b"/etc/hosts", resolver.file) def test_getHostByName(self): """ L{hosts.Resolver.getHostByName} returns a L{Deferred} which fires with a string giving the address of the queried name as found in the resolver's hosts file. """ data = [(b"EXAMPLE", "1.1.1.1"), (b"EXAMPLE.EXAMPLETHING", "1.1.1.1"), (b"MIXED", "1.1.1.2")] ds = [self.resolver.getHostByName(n).addCallback(self.assertEqual, ip) for n, ip in data] return gatherResults(ds) def test_lookupAddress(self): """ L{hosts.Resolver.lookupAddress} returns a L{Deferred} which fires with A records from the hosts file. """ d = self.resolver.lookupAddress(b"multiple") def resolved(results): answers, authority, additional = results self.assertEqual( ( RRHeader(b"multiple", A, IN, self.ttl, Record_A("1.1.1.3", self.ttl)), RRHeader(b"multiple", A, IN, self.ttl, Record_A("1.1.1.4", self.ttl)), ), answers, ) d.addCallback(resolved) return d def test_lookupIPV6Address(self): """ L{hosts.Resolver.lookupIPV6Address} returns a L{Deferred} which fires with AAAA records from the hosts file. """ d = self.resolver.lookupIPV6Address(b"ip6-multiple") def resolved(results): answers, authority, additional = results self.assertEqual( ( RRHeader(b"ip6-multiple", AAAA, IN, self.ttl, Record_AAAA("::3", self.ttl)), RRHeader(b"ip6-multiple", AAAA, IN, self.ttl, Record_AAAA("::4", self.ttl)), ), answers, ) d.addCallback(resolved) return d def test_lookupAllRecords(self): """ L{hosts.Resolver.lookupAllRecords} returns a L{Deferred} which fires with A records from the hosts file. """ d = self.resolver.lookupAllRecords(b"mixed") def resolved(results): answers, authority, additional = results self.assertEqual((RRHeader(b"mixed", A, IN, self.ttl, Record_A("1.1.1.2", self.ttl)),), answers) d.addCallback(resolved) return d def test_notImplemented(self): return self.assertFailure(self.resolver.lookupMailExchange(b"EXAMPLE"), NotImplementedError) def test_query(self): d = self.resolver.query(Query(b"EXAMPLE")) d.addCallback(lambda x: self.assertEqual(x[0][0].payload.dottedQuad(), "1.1.1.1")) return d def test_lookupAddressNotFound(self): """ L{hosts.Resolver.lookupAddress} returns a L{Deferred} which fires with L{dns.DomainError} if the name passed in has no addresses in the hosts file. """ return self.assertFailure(self.resolver.lookupAddress(b"foueoa"), DomainError) def test_lookupIPV6AddressNotFound(self): """ Like L{test_lookupAddressNotFound}, but for L{hosts.Resolver.lookupIPV6Address}. """ return self.assertFailure(self.resolver.lookupIPV6Address(b"foueoa"), DomainError) def test_lookupAllRecordsNotFound(self): """ Like L{test_lookupAddressNotFound}, but for L{hosts.Resolver.lookupAllRecords}. """ return self.assertFailure(self.resolver.lookupAllRecords(b"foueoa"), DomainError)
class HostsTestCase(TestCase): """ Tests for the I{hosts(5)}-based L{twisted.names.hosts.Resolver}. """ def setUp(self): f = open('EtcHosts', 'w') f.write(''' 1.1.1.1 EXAMPLE EXAMPLE.EXAMPLETHING ::2 mixed 1.1.1.2 MIXED ::1 ip6thingy 1.1.1.3 multiple 1.1.1.4 multiple ::3 ip6-multiple ::4 ip6-multiple ''') f.close() self.ttl = 4200 self.resolver = Resolver('EtcHosts', self.ttl) def testGetHostByName(self): data = [ ('EXAMPLE', '1.1.1.1'), ('EXAMPLE.EXAMPLETHING', '1.1.1.1'), ('MIXED', '1.1.1.2'), ] ds = [ self.resolver.getHostByName(n).addCallback(self.assertEqual, ip) for n, ip in data ] return gatherResults(ds) def test_lookupAddress(self): """ L{hosts.Resolver.lookupAddress} returns a L{Deferred} which fires with A records from the hosts file. """ d = self.resolver.lookupAddress('multiple') def resolved((results, authority, additional)): self.assertEqual((RRHeader("multiple", A, IN, self.ttl, Record_A("1.1.1.3", self.ttl)), RRHeader("multiple", A, IN, self.ttl, Record_A("1.1.1.4", self.ttl))), results) d.addCallback(resolved) return d def test_lookupIPV6Address(self): """ L{hosts.Resolver.lookupIPV6Address} returns a L{Deferred} which fires with AAAA records from the hosts file. """ d = self.resolver.lookupIPV6Address('ip6-multiple') def resolved((results, authority, additional)): self.assertEqual((RRHeader("ip6-multiple", AAAA, IN, self.ttl, Record_AAAA("::3", self.ttl)), RRHeader("ip6-multiple", AAAA, IN, self.ttl, Record_AAAA("::4", self.ttl))), results) d.addCallback(resolved) return d def test_lookupAllRecords(self): """ L{hosts.Resolver.lookupAllRecords} returns a L{Deferred} which fires with A records from the hosts file. """ d = self.resolver.lookupAllRecords('mixed') def resolved((results, authority, additional)): self.assertEqual((RRHeader("mixed", A, IN, self.ttl, Record_A("1.1.1.2", self.ttl)), ), results) d.addCallback(resolved) return d def testNotImplemented(self): return self.assertFailure(self.resolver.lookupMailExchange('EXAMPLE'), NotImplementedError) def testQuery(self): d = self.resolver.query(Query('EXAMPLE')) d.addCallback(lambda x: self.assertEqual(x[0][0].payload.dottedQuad(), '1.1.1.1')) return d def test_lookupAddressNotFound(self): """ L{hosts.Resolver.lookupAddress} returns a L{Deferred} which fires with L{dns.DomainError} if the name passed in has no addresses in the hosts file. """ return self.assertFailure(self.resolver.lookupAddress('foueoa'), DomainError) def test_lookupIPV6AddressNotFound(self): """ Like L{test_lookupAddressNotFound}, but for L{hosts.Resolver.lookupIPV6Address}. """ return self.assertFailure(self.resolver.lookupIPV6Address('foueoa'), DomainError) def test_lookupAllRecordsNotFound(self): """ Like L{test_lookupAddressNotFound}, but for L{hosts.Resolver.lookupAllRecords}. """ return self.assertFailure(self.resolver.lookupAllRecords('foueoa'), DomainError)