~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/names/test/test_client.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-17 14:52:35 UTC
  • mto: (2.2.3 sid)
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: james.westby@ubuntu.com-20070117145235-7gaj253qxi5wiq16
Tags: upstream-2.5.0
ImportĀ upstreamĀ versionĀ 2.5.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- test-case-name: twisted.names.test.test_client -*-
 
2
# Copyright (c) 2001-2006 Twisted Matrix Laboratories.
 
3
# See LICENSE for details.
 
4
 
 
5
"""
 
6
Test cases for twisted.names.client
 
7
"""
 
8
 
 
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
 
13
 
 
14
class FakeResolver(ResolverBase):
 
15
    
 
16
    def _lookup(self, name, cls, qtype, timeout):
 
17
        """
 
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
 
20
        here.
 
21
        """
 
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))
 
25
        else:
 
26
            rr = dns.RRHeader(name=name, type=qtype, cls=cls, ttl=60)
 
27
            
 
28
        results = [rr]
 
29
        authority = []
 
30
        addtional = []
 
31
        return defer.succeed((results, authority, addtional))
 
32
 
 
33
class ClientTestCase(unittest.TestCase):
 
34
 
 
35
    def setUp(self):
 
36
        """
 
37
        Replace the resolver with a FakeResolver
 
38
        """
 
39
        client.theResolver = FakeResolver()
 
40
        self.hostname = 'example.com'
 
41
        self.ghbntest = 'getHostByNameTest'
 
42
 
 
43
    def tearDown(self):
 
44
        """
 
45
        By setting the resolver to None, it will be recreated next time a name
 
46
        lookup is done.
 
47
        """
 
48
        client.theResolver = None
 
49
 
 
50
    def checkResult(self, (results, authority, additional), qtype):
 
51
        """
 
52
        Verify that the result is the same query type as what is expected.
 
53
        """
 
54
        result = results[0]
 
55
        self.assertEquals(str(result.name), self.hostname)
 
56
        self.assertEquals(result.type, qtype)
 
57
 
 
58
    def checkGetHostByName(self, result):
 
59
        """
 
60
        Test that the getHostByName query returns the 127.0.0.1 address.
 
61
        """
 
62
        self.assertEquals(result, '127.0.0.1')
 
63
 
 
64
    def test_getHostByName(self):
 
65
        """
 
66
        do a getHostByName of a value that should return 127.0.0.1.
 
67
        """
 
68
        d = client.getHostByName(self.ghbntest)
 
69
        d.addCallback(self.checkGetHostByName)
 
70
        return d
 
71
 
 
72
    def test_lookupAddress(self):
 
73
        """
 
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.
 
77
        """
 
78
        d = client.lookupAddress(self.hostname)
 
79
        d.addCallback(self.checkResult, dns.A)
 
80
        return d
 
81
 
 
82
    def test_lookupIPV6Address(self):
 
83
        """
 
84
        See L{test_lookupAddress}
 
85
        """
 
86
        d = client.lookupIPV6Address(self.hostname)
 
87
        d.addCallback(self.checkResult, dns.AAAA)
 
88
        return d
 
89
 
 
90
    def test_lookupAddress6(self):
 
91
        """
 
92
        See L{test_lookupAddress}
 
93
        """
 
94
        d = client.lookupAddress6(self.hostname)
 
95
        d.addCallback(self.checkResult, dns.A6)
 
96
        return d
 
97
 
 
98
    def test_lookupNameservers(self):
 
99
        """
 
100
        See L{test_lookupAddress}
 
101
        """
 
102
        d = client.lookupNameservers(self.hostname)
 
103
        d.addCallback(self.checkResult, dns.NS)
 
104
        return d
 
105
 
 
106
    def test_lookupCanonicalName(self):
 
107
        """
 
108
        See L{test_lookupAddress}
 
109
        """
 
110
        d = client.lookupCanonicalName(self.hostname)
 
111
        d.addCallback(self.checkResult, dns.CNAME)
 
112
        return d
 
113
 
 
114
    def test_lookupAuthority(self):
 
115
        """
 
116
        See L{test_lookupAddress}
 
117
        """
 
118
        d = client.lookupAuthority(self.hostname)
 
119
        d.addCallback(self.checkResult, dns.SOA)
 
120
        return d
 
121
 
 
122
    def test_lookupMailBox(self):
 
123
        """
 
124
        See L{test_lookupAddress}
 
125
        """
 
126
        d = client.lookupMailBox(self.hostname)
 
127
        d.addCallback(self.checkResult, dns.MB)
 
128
        return d
 
129
 
 
130
    def test_lookupMailGroup(self):
 
131
        """
 
132
        See L{test_lookupAddress}
 
133
        """
 
134
        d = client.lookupMailGroup(self.hostname)
 
135
        d.addCallback(self.checkResult, dns.MG)
 
136
        return d
 
137
 
 
138
    def test_lookupMailRename(self):
 
139
        """
 
140
        See L{test_lookupAddress}
 
141
        """
 
142
        d = client.lookupMailRename(self.hostname)
 
143
        d.addCallback(self.checkResult, dns.MR)
 
144
        return d
 
145
 
 
146
    def test_lookupNull(self):
 
147
        """
 
148
        See L{test_lookupAddress}
 
149
        """
 
150
        d = client.lookupNull(self.hostname)
 
151
        d.addCallback(self.checkResult, dns.NULL)
 
152
        return d
 
153
 
 
154
    def test_lookupWellKnownServices(self):
 
155
        """
 
156
        See L{test_lookupAddress}
 
157
        """
 
158
        d = client.lookupWellKnownServices(self.hostname)
 
159
        d.addCallback(self.checkResult, dns.WKS)
 
160
        return d
 
161
 
 
162
    def test_lookupPointer(self):
 
163
        """
 
164
        See L{test_lookupAddress}
 
165
        """
 
166
        d = client.lookupPointer(self.hostname)
 
167
        d.addCallback(self.checkResult, dns.PTR)
 
168
        return d
 
169
 
 
170
    def test_lookupHostInfo(self):
 
171
        """
 
172
        See L{test_lookupAddress}
 
173
        """
 
174
        d = client.lookupHostInfo(self.hostname)
 
175
        d.addCallback(self.checkResult, dns.HINFO)
 
176
        return d
 
177
 
 
178
    def test_lookupMailboxInfo(self):
 
179
        """
 
180
        See L{test_lookupAddress}
 
181
        """
 
182
        d = client.lookupMailboxInfo(self.hostname)
 
183
        d.addCallback(self.checkResult, dns.MINFO)
 
184
        return d
 
185
 
 
186
    def test_lookupMailExchange(self):
 
187
        """
 
188
        See L{test_lookupAddress}
 
189
        """
 
190
        d = client.lookupMailExchange(self.hostname)
 
191
        d.addCallback(self.checkResult, dns.MX)
 
192
        return d
 
193
 
 
194
    def test_lookupText(self):
 
195
        """
 
196
        See L{test_lookupAddress}
 
197
        """
 
198
        d = client.lookupText(self.hostname)
 
199
        d.addCallback(self.checkResult, dns.TXT)
 
200
        return d
 
201
 
 
202
    def test_lookupResponsibility(self):
 
203
        """
 
204
        See L{test_lookupAddress}
 
205
        """
 
206
        d = client.lookupResponsibility(self.hostname)
 
207
        d.addCallback(self.checkResult, dns.RP)
 
208
        return d
 
209
 
 
210
    def test_lookupAFSDatabase(self):
 
211
        """
 
212
        See L{test_lookupAddress}
 
213
        """
 
214
        d = client.lookupAFSDatabase(self.hostname)
 
215
        d.addCallback(self.checkResult, dns.AFSDB)
 
216
        return d
 
217
 
 
218
    def test_lookupService(self):
 
219
        """
 
220
        See L{test_lookupAddress}
 
221
        """
 
222
        d = client.lookupService(self.hostname)
 
223
        d.addCallback(self.checkResult, dns.SRV)
 
224
        return d
 
225
 
 
226
    def test_lookupZone(self):
 
227
        """
 
228
        See L{test_lookupAddress}
 
229
        """
 
230
        d = client.lookupZone(self.hostname)
 
231
        d.addCallback(self.checkResult, dns.AXFR)
 
232
        return d
 
233
 
 
234
    def test_lookupAllRecords(self):
 
235
        """
 
236
        See L{test_lookupAddress}
 
237
        """
 
238
        d = client.lookupAllRecords(self.hostname)
 
239
        d.addCallback(self.checkResult, dns.ALL_RECORDS)
 
240
        return d
 
241