1
# -*- test-case-name: twisted.names.test.test_client -*-
2
# Copyright (c) 2001-2006 Twisted Matrix Laboratories.
3
# See LICENSE for details.
6
Test cases for twisted.names.client
9
from twisted.names import client, dns
10
from twisted.trial import unittest
11
from twisted.names.common import ResolverBase
12
from twisted.internet import defer
14
class FakeResolver(ResolverBase):
16
def _lookup(self, name, cls, qtype, timeout):
18
The getHostByNameTest does a different type of query that requires it
19
return an A record from an ALL_RECORDS lookup, so we accomodate that
22
if name == 'getHostByNameTest':
23
rr = dns.RRHeader(name=name, type=dns.A, cls=cls, ttl=60,
24
payload=dns.Record_A(address='127.0.0.1', ttl=60))
26
rr = dns.RRHeader(name=name, type=qtype, cls=cls, ttl=60)
31
return defer.succeed((results, authority, addtional))
33
class ClientTestCase(unittest.TestCase):
37
Replace the resolver with a FakeResolver
39
client.theResolver = FakeResolver()
40
self.hostname = 'example.com'
41
self.ghbntest = 'getHostByNameTest'
45
By setting the resolver to None, it will be recreated next time a name
48
client.theResolver = None
50
def checkResult(self, (results, authority, additional), qtype):
52
Verify that the result is the same query type as what is expected.
55
self.assertEquals(str(result.name), self.hostname)
56
self.assertEquals(result.type, qtype)
58
def checkGetHostByName(self, result):
60
Test that the getHostByName query returns the 127.0.0.1 address.
62
self.assertEquals(result, '127.0.0.1')
64
def test_getHostByName(self):
66
do a getHostByName of a value that should return 127.0.0.1.
68
d = client.getHostByName(self.ghbntest)
69
d.addCallback(self.checkGetHostByName)
72
def test_lookupAddress(self):
74
Do a lookup and test that the resolver will issue the correct type of
75
query type. We do this by checking that FakeResolver returns a result
76
record with the same query type as what we issued.
78
d = client.lookupAddress(self.hostname)
79
d.addCallback(self.checkResult, dns.A)
82
def test_lookupIPV6Address(self):
84
See L{test_lookupAddress}
86
d = client.lookupIPV6Address(self.hostname)
87
d.addCallback(self.checkResult, dns.AAAA)
90
def test_lookupAddress6(self):
92
See L{test_lookupAddress}
94
d = client.lookupAddress6(self.hostname)
95
d.addCallback(self.checkResult, dns.A6)
98
def test_lookupNameservers(self):
100
See L{test_lookupAddress}
102
d = client.lookupNameservers(self.hostname)
103
d.addCallback(self.checkResult, dns.NS)
106
def test_lookupCanonicalName(self):
108
See L{test_lookupAddress}
110
d = client.lookupCanonicalName(self.hostname)
111
d.addCallback(self.checkResult, dns.CNAME)
114
def test_lookupAuthority(self):
116
See L{test_lookupAddress}
118
d = client.lookupAuthority(self.hostname)
119
d.addCallback(self.checkResult, dns.SOA)
122
def test_lookupMailBox(self):
124
See L{test_lookupAddress}
126
d = client.lookupMailBox(self.hostname)
127
d.addCallback(self.checkResult, dns.MB)
130
def test_lookupMailGroup(self):
132
See L{test_lookupAddress}
134
d = client.lookupMailGroup(self.hostname)
135
d.addCallback(self.checkResult, dns.MG)
138
def test_lookupMailRename(self):
140
See L{test_lookupAddress}
142
d = client.lookupMailRename(self.hostname)
143
d.addCallback(self.checkResult, dns.MR)
146
def test_lookupNull(self):
148
See L{test_lookupAddress}
150
d = client.lookupNull(self.hostname)
151
d.addCallback(self.checkResult, dns.NULL)
154
def test_lookupWellKnownServices(self):
156
See L{test_lookupAddress}
158
d = client.lookupWellKnownServices(self.hostname)
159
d.addCallback(self.checkResult, dns.WKS)
162
def test_lookupPointer(self):
164
See L{test_lookupAddress}
166
d = client.lookupPointer(self.hostname)
167
d.addCallback(self.checkResult, dns.PTR)
170
def test_lookupHostInfo(self):
172
See L{test_lookupAddress}
174
d = client.lookupHostInfo(self.hostname)
175
d.addCallback(self.checkResult, dns.HINFO)
178
def test_lookupMailboxInfo(self):
180
See L{test_lookupAddress}
182
d = client.lookupMailboxInfo(self.hostname)
183
d.addCallback(self.checkResult, dns.MINFO)
186
def test_lookupMailExchange(self):
188
See L{test_lookupAddress}
190
d = client.lookupMailExchange(self.hostname)
191
d.addCallback(self.checkResult, dns.MX)
194
def test_lookupText(self):
196
See L{test_lookupAddress}
198
d = client.lookupText(self.hostname)
199
d.addCallback(self.checkResult, dns.TXT)
202
def test_lookupResponsibility(self):
204
See L{test_lookupAddress}
206
d = client.lookupResponsibility(self.hostname)
207
d.addCallback(self.checkResult, dns.RP)
210
def test_lookupAFSDatabase(self):
212
See L{test_lookupAddress}
214
d = client.lookupAFSDatabase(self.hostname)
215
d.addCallback(self.checkResult, dns.AFSDB)
218
def test_lookupService(self):
220
See L{test_lookupAddress}
222
d = client.lookupService(self.hostname)
223
d.addCallback(self.checkResult, dns.SRV)
226
def test_lookupZone(self):
228
See L{test_lookupAddress}
230
d = client.lookupZone(self.hostname)
231
d.addCallback(self.checkResult, dns.AXFR)
234
def test_lookupAllRecords(self):
236
See L{test_lookupAddress}
238
d = client.lookupAllRecords(self.hostname)
239
d.addCallback(self.checkResult, dns.ALL_RECORDS)