~cbehrens/nova/lp844160-build-works-with-zones

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/twisted/names/test/test_dns.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# test-case-name: twisted.names.test.test_dns
 
2
# Copyright (c) 2001-2007 Twisted Matrix Laboratories.
 
3
# See LICENSE for details.
 
4
 
 
5
"""
 
6
Tests for twisted.names.dns.
 
7
"""
 
8
 
 
9
try:
 
10
    from cStringIO import StringIO
 
11
except ImportError:
 
12
    from StringIO import StringIO
 
13
 
 
14
import struct
 
15
 
 
16
from twisted.python.failure import Failure
 
17
from twisted.internet import address, task
 
18
from twisted.internet.error import CannotListenError, ConnectionDone
 
19
from twisted.trial import unittest
 
20
from twisted.names import dns
 
21
 
 
22
from twisted.test import proto_helpers
 
23
 
 
24
 
 
25
 
 
26
class RoundtripDNSTestCase(unittest.TestCase):
 
27
    """Encoding and then decoding various objects."""
 
28
 
 
29
    names = ["example.org", "go-away.fish.tv", "23strikesback.net"]
 
30
 
 
31
    def testName(self):
 
32
        for n in self.names:
 
33
            # encode the name
 
34
            f = StringIO()
 
35
            dns.Name(n).encode(f)
 
36
 
 
37
            # decode the name
 
38
            f.seek(0, 0)
 
39
            result = dns.Name()
 
40
            result.decode(f)
 
41
            self.assertEquals(result.name, n)
 
42
 
 
43
    def testQuery(self):
 
44
        for n in self.names:
 
45
            for dnstype in range(1, 17):
 
46
                for dnscls in range(1, 5):
 
47
                    # encode the query
 
48
                    f = StringIO()
 
49
                    dns.Query(n, dnstype, dnscls).encode(f)
 
50
 
 
51
                    # decode the result
 
52
                    f.seek(0, 0)
 
53
                    result = dns.Query()
 
54
                    result.decode(f)
 
55
                    self.assertEquals(result.name.name, n)
 
56
                    self.assertEquals(result.type, dnstype)
 
57
                    self.assertEquals(result.cls, dnscls)
 
58
 
 
59
    def testRR(self):
 
60
        # encode the RR
 
61
        f = StringIO()
 
62
        dns.RRHeader("test.org", 3, 4, 17).encode(f)
 
63
 
 
64
        # decode the result
 
65
        f.seek(0, 0)
 
66
        result = dns.RRHeader()
 
67
        result.decode(f)
 
68
        self.assertEquals(str(result.name), "test.org")
 
69
        self.assertEquals(result.type, 3)
 
70
        self.assertEquals(result.cls, 4)
 
71
        self.assertEquals(result.ttl, 17)
 
72
 
 
73
 
 
74
    def testResources(self):
 
75
        names = (
 
76
            "this.are.test.name",
 
77
            "will.compress.will.this.will.name.will.hopefully",
 
78
            "test.CASE.preSErVatIOn.YeAH",
 
79
            "a.s.h.o.r.t.c.a.s.e.t.o.t.e.s.t",
 
80
            "singleton"
 
81
        )
 
82
        for s in names:
 
83
            f = StringIO()
 
84
            dns.SimpleRecord(s).encode(f)
 
85
            f.seek(0, 0)
 
86
            result = dns.SimpleRecord()
 
87
            result.decode(f)
 
88
            self.assertEquals(str(result.name), s)
 
89
 
 
90
    def test_hashable(self):
 
91
        """
 
92
        Instances of all record types are hashable.
 
93
        """
 
94
        records = [
 
95
            dns.Record_NS, dns.Record_MD, dns.Record_MF, dns.Record_CNAME,
 
96
            dns.Record_MB, dns.Record_MG, dns.Record_MR, dns.Record_PTR,
 
97
            dns.Record_DNAME, dns.Record_A, dns.Record_SOA, dns.Record_NULL,
 
98
            dns.Record_WKS, dns.Record_SRV, dns.Record_AFSDB, dns.Record_RP,
 
99
            dns.Record_HINFO, dns.Record_MINFO, dns.Record_MX, dns.Record_TXT,
 
100
            dns.Record_AAAA, dns.Record_A6, dns.Record_NAPTR
 
101
        ]
 
102
 
 
103
        for k in records:
 
104
            k1, k2 = k(), k()
 
105
            hk1 = hash(k1)
 
106
            hk2 = hash(k2)
 
107
            self.assertEquals(hk1, hk2, "%s != %s (for %s)" % (hk1,hk2,k))
 
108
 
 
109
 
 
110
    def test_Charstr(self):
 
111
        """
 
112
        Test L{dns.Charstr} encode and decode.
 
113
        """
 
114
        for n in self.names:
 
115
            # encode the name
 
116
            f = StringIO()
 
117
            dns.Charstr(n).encode(f)
 
118
 
 
119
            # decode the name
 
120
            f.seek(0, 0)
 
121
            result = dns.Charstr()
 
122
            result.decode(f)
 
123
            self.assertEquals(result.string, n)
 
124
 
 
125
 
 
126
    def test_NAPTR(self):
 
127
        """
 
128
        Test L{dns.Record_NAPTR} encode and decode.
 
129
        """
 
130
        naptrs = [(100, 10, "u", "sip+E2U",
 
131
                   "!^.*$!sip:information@domain.tld!", ""),
 
132
                  (100, 50, "s", "http+I2L+I2C+I2R", "",
 
133
                   "_http._tcp.gatech.edu")]
 
134
 
 
135
        for (order, preference, flags, service, regexp, replacement) in naptrs:
 
136
            rin = dns.Record_NAPTR(order, preference, flags, service, regexp,
 
137
                                   replacement)
 
138
            e = StringIO()
 
139
            rin.encode(e)
 
140
            e.seek(0,0)
 
141
            rout = dns.Record_NAPTR()
 
142
            rout.decode(e)
 
143
            self.assertEquals(rin.order, rout.order)
 
144
            self.assertEquals(rin.preference, rout.preference)
 
145
            self.assertEquals(rin.flags, rout.flags)
 
146
            self.assertEquals(rin.service, rout.service)
 
147
            self.assertEquals(rin.regexp, rout.regexp)
 
148
            self.assertEquals(rin.replacement.name, rout.replacement.name)
 
149
            self.assertEquals(rin.ttl, rout.ttl)
 
150
 
 
151
 
 
152
 
 
153
class MessageTestCase(unittest.TestCase):
 
154
    def testEmptyMessage(self):
 
155
        """
 
156
        Test that a message which has been truncated causes an EOFError to
 
157
        be raised when it is parsed.
 
158
        """
 
159
        msg = dns.Message()
 
160
        self.assertRaises(EOFError, msg.fromStr, '')
 
161
 
 
162
 
 
163
    def testEmptyQuery(self):
 
164
        """
 
165
        Test that bytes representing an empty query message can be decoded
 
166
        as such.
 
167
        """
 
168
        msg = dns.Message()
 
169
        msg.fromStr(
 
170
            '\x01\x00' # Message ID
 
171
            '\x00' # answer bit, opCode nibble, auth bit, trunc bit, recursive bit
 
172
            '\x00' # recursion bit, empty bit, empty bit, empty bit, response code nibble
 
173
            '\x00\x00' # number of queries
 
174
            '\x00\x00' # number of answers
 
175
            '\x00\x00' # number of authorities
 
176
            '\x00\x00' # number of additionals
 
177
            )
 
178
        self.assertEquals(msg.id, 256)
 
179
        self.failIf(msg.answer, "Message was not supposed to be an answer.")
 
180
        self.assertEquals(msg.opCode, dns.OP_QUERY)
 
181
        self.failIf(msg.auth, "Message was not supposed to be authoritative.")
 
182
        self.failIf(msg.trunc, "Message was not supposed to be truncated.")
 
183
        self.assertEquals(msg.queries, [])
 
184
        self.assertEquals(msg.answers, [])
 
185
        self.assertEquals(msg.authority, [])
 
186
        self.assertEquals(msg.additional, [])
 
187
 
 
188
 
 
189
    def testNULL(self):
 
190
        bytes = ''.join([chr(i) for i in range(256)])
 
191
        rec = dns.Record_NULL(bytes)
 
192
        rr = dns.RRHeader('testname', dns.NULL, payload=rec)
 
193
        msg1 = dns.Message()
 
194
        msg1.answers.append(rr)
 
195
        s = StringIO()
 
196
        msg1.encode(s)
 
197
        s.seek(0, 0)
 
198
        msg2 = dns.Message()
 
199
        msg2.decode(s)
 
200
 
 
201
        self.failUnless(isinstance(msg2.answers[0].payload, dns.Record_NULL))
 
202
        self.assertEquals(msg2.answers[0].payload.payload, bytes)
 
203
 
 
204
 
 
205
 
 
206
class TestController(object):
 
207
    """
 
208
    Pretend to be a DNS query processor for a DNSDatagramProtocol.
 
209
 
 
210
    @ivar messages: the list of received messages.
 
211
    @type messages: C{list} of (msg, protocol, address)
 
212
    """
 
213
 
 
214
    def __init__(self):
 
215
        """
 
216
        Initialize the controller: create a list of messages.
 
217
        """
 
218
        self.messages = []
 
219
 
 
220
 
 
221
    def messageReceived(self, msg, proto, addr):
 
222
        """
 
223
        Save the message so that it can be checked during the tests.
 
224
        """
 
225
        self.messages.append((msg, proto, addr))
 
226
 
 
227
 
 
228
 
 
229
class DatagramProtocolTestCase(unittest.TestCase):
 
230
    """
 
231
    Test various aspects of L{dns.DNSDatagramProtocol}.
 
232
    """
 
233
 
 
234
    def setUp(self):
 
235
        """
 
236
        Create a L{dns.DNSDatagramProtocol} with a deterministic clock.
 
237
        """
 
238
        self.clock = task.Clock()
 
239
        self.controller = TestController()
 
240
        self.proto = dns.DNSDatagramProtocol(self.controller)
 
241
        transport = proto_helpers.FakeDatagramTransport()
 
242
        self.proto.makeConnection(transport)
 
243
        self.proto.callLater = self.clock.callLater
 
244
 
 
245
 
 
246
    def test_truncatedPacket(self):
 
247
        """
 
248
        Test that when a short datagram is received, datagramReceived does
 
249
        not raise an exception while processing it.
 
250
        """
 
251
        self.proto.datagramReceived('',
 
252
            address.IPv4Address('UDP', '127.0.0.1', 12345))
 
253
        self.assertEquals(self.controller.messages, [])
 
254
 
 
255
 
 
256
    def test_simpleQuery(self):
 
257
        """
 
258
        Test content received after a query.
 
259
        """
 
260
        d = self.proto.query(('127.0.0.1', 21345), [dns.Query('foo')])
 
261
        self.assertEquals(len(self.proto.liveMessages.keys()), 1)
 
262
        m = dns.Message()
 
263
        m.id = self.proto.liveMessages.items()[0][0]
 
264
        m.answers = [dns.RRHeader(payload=dns.Record_A(address='1.2.3.4'))]
 
265
        called = False
 
266
        def cb(result):
 
267
            self.assertEquals(result.answers[0].payload.dottedQuad(), '1.2.3.4')
 
268
        d.addCallback(cb)
 
269
        self.proto.datagramReceived(m.toStr(), ('127.0.0.1', 21345))
 
270
        return d
 
271
 
 
272
 
 
273
    def test_queryTimeout(self):
 
274
        """
 
275
        Test that query timeouts after some seconds.
 
276
        """
 
277
        d = self.proto.query(('127.0.0.1', 21345), [dns.Query('foo')])
 
278
        self.assertEquals(len(self.proto.liveMessages), 1)
 
279
        self.clock.advance(10)
 
280
        self.assertFailure(d, dns.DNSQueryTimeoutError)
 
281
        self.assertEquals(len(self.proto.liveMessages), 0)
 
282
        return d
 
283
 
 
284
 
 
285
    def test_writeError(self):
 
286
        """
 
287
        Exceptions raised by the transport's write method should be turned into
 
288
        C{Failure}s passed to errbacks of the C{Deferred} returned by
 
289
        L{DNSDatagramProtocol.query}.
 
290
        """
 
291
        def writeError(message, addr):
 
292
            raise RuntimeError("bar")
 
293
        self.proto.transport.write = writeError
 
294
 
 
295
        d = self.proto.query(('127.0.0.1', 21345), [dns.Query('foo')])
 
296
        return self.assertFailure(d, RuntimeError)
 
297
 
 
298
 
 
299
    def test_listenError(self):
 
300
        """
 
301
        Exception L{CannotListenError} raised by C{listenUDP} should be turned
 
302
        into a C{Failure} passed to errback of the C{Deferred} returned by
 
303
        L{DNSDatagramProtocol.query}.
 
304
        """
 
305
        def startListeningError():
 
306
            raise CannotListenError(None, None, None)
 
307
        self.proto.startListening = startListeningError
 
308
        # Clean up transport so that the protocol calls startListening again
 
309
        self.proto.transport = None
 
310
 
 
311
        d = self.proto.query(('127.0.0.1', 21345), [dns.Query('foo')])
 
312
        return self.assertFailure(d, CannotListenError)
 
313
 
 
314
 
 
315
 
 
316
class TestTCPController(TestController):
 
317
    """
 
318
    Pretend to be a DNS query processor for a DNSProtocol.
 
319
 
 
320
    @ivar connections: A list of L{DNSProtocol} instances which have
 
321
        notified this controller that they are connected and have not
 
322
        yet notified it that their connection has been lost.
 
323
    """
 
324
    def __init__(self):
 
325
        TestController.__init__(self)
 
326
        self.connections = []
 
327
 
 
328
 
 
329
    def connectionMade(self, proto):
 
330
        self.connections.append(proto)
 
331
 
 
332
 
 
333
    def connectionLost(self, proto):
 
334
        self.connections.remove(proto)
 
335
 
 
336
 
 
337
 
 
338
class DNSProtocolTestCase(unittest.TestCase):
 
339
    """
 
340
    Test various aspects of L{dns.DNSProtocol}.
 
341
    """
 
342
 
 
343
    def setUp(self):
 
344
        """
 
345
        Create a L{dns.DNSProtocol} with a deterministic clock.
 
346
        """
 
347
        self.clock = task.Clock()
 
348
        self.controller = TestTCPController()
 
349
        self.proto = dns.DNSProtocol(self.controller)
 
350
        self.proto.makeConnection(proto_helpers.StringTransport())
 
351
        self.proto.callLater = self.clock.callLater
 
352
 
 
353
 
 
354
    def test_connectionTracking(self):
 
355
        """
 
356
        L{dns.DNSProtocol} calls its controller's C{connectionMade}
 
357
        method with itself when it is connected to a transport and its
 
358
        controller's C{connectionLost} method when it is disconnected.
 
359
        """
 
360
        self.assertEqual(self.controller.connections, [self.proto])
 
361
        self.proto.connectionLost(
 
362
            Failure(ConnectionDone("Fake Connection Done")))
 
363
        self.assertEqual(self.controller.connections, [])
 
364
 
 
365
 
 
366
    def test_queryTimeout(self):
 
367
        """
 
368
        Test that query timeouts after some seconds.
 
369
        """
 
370
        d = self.proto.query([dns.Query('foo')])
 
371
        self.assertEquals(len(self.proto.liveMessages), 1)
 
372
        self.clock.advance(60)
 
373
        self.assertFailure(d, dns.DNSQueryTimeoutError)
 
374
        self.assertEquals(len(self.proto.liveMessages), 0)
 
375
        return d
 
376
 
 
377
 
 
378
    def test_simpleQuery(self):
 
379
        """
 
380
        Test content received after a query.
 
381
        """
 
382
        d = self.proto.query([dns.Query('foo')])
 
383
        self.assertEquals(len(self.proto.liveMessages.keys()), 1)
 
384
        m = dns.Message()
 
385
        m.id = self.proto.liveMessages.items()[0][0]
 
386
        m.answers = [dns.RRHeader(payload=dns.Record_A(address='1.2.3.4'))]
 
387
        called = False
 
388
        def cb(result):
 
389
            self.assertEquals(result.answers[0].payload.dottedQuad(), '1.2.3.4')
 
390
        d.addCallback(cb)
 
391
        s = m.toStr()
 
392
        s = struct.pack('!H', len(s)) + s
 
393
        self.proto.dataReceived(s)
 
394
        return d
 
395
 
 
396
 
 
397
    def test_writeError(self):
 
398
        """
 
399
        Exceptions raised by the transport's write method should be turned into
 
400
        C{Failure}s passed to errbacks of the C{Deferred} returned by
 
401
        L{DNSProtocol.query}.
 
402
        """
 
403
        def writeError(message):
 
404
            raise RuntimeError("bar")
 
405
        self.proto.transport.write = writeError
 
406
 
 
407
        d = self.proto.query([dns.Query('foo')])
 
408
        return self.assertFailure(d, RuntimeError)
 
409
 
 
410
 
 
411
 
 
412
class ReprTests(unittest.TestCase):
 
413
    """
 
414
    Tests for the C{__repr__} implementation of record classes.
 
415
    """
 
416
    def test_ns(self):
 
417
        """
 
418
        The repr of a L{dns.Record_NS} instance includes the name of the
 
419
        nameserver and the TTL of the record.
 
420
        """
 
421
        self.assertEqual(
 
422
            repr(dns.Record_NS('example.com', 4321)),
 
423
            "<NS name=example.com ttl=4321>")
 
424
 
 
425
 
 
426
    def test_md(self):
 
427
        """
 
428
        The repr of a L{dns.Record_MD} instance includes the name of the
 
429
        mail destination and the TTL of the record.
 
430
        """
 
431
        self.assertEqual(
 
432
            repr(dns.Record_MD('example.com', 4321)),
 
433
            "<MD name=example.com ttl=4321>")
 
434
 
 
435
 
 
436
    def test_mf(self):
 
437
        """
 
438
        The repr of a L{dns.Record_MF} instance includes the name of the
 
439
        mail forwarder and the TTL of the record.
 
440
        """
 
441
        self.assertEqual(
 
442
            repr(dns.Record_MF('example.com', 4321)),
 
443
            "<MF name=example.com ttl=4321>")
 
444
 
 
445
 
 
446
    def test_cname(self):
 
447
        """
 
448
        The repr of a L{dns.Record_CNAME} instance includes the name of the
 
449
        mail forwarder and the TTL of the record.
 
450
        """
 
451
        self.assertEqual(
 
452
            repr(dns.Record_CNAME('example.com', 4321)),
 
453
            "<CNAME name=example.com ttl=4321>")
 
454
 
 
455
 
 
456
    def test_mb(self):
 
457
        """
 
458
        The repr of a L{dns.Record_MB} instance includes the name of the
 
459
        mailbox and the TTL of the record.
 
460
        """
 
461
        self.assertEqual(
 
462
            repr(dns.Record_MB('example.com', 4321)),
 
463
            "<MB name=example.com ttl=4321>")
 
464
 
 
465
 
 
466
    def test_mg(self):
 
467
        """
 
468
        The repr of a L{dns.Record_MG} instance includes the name of the
 
469
        mail group memeber and the TTL of the record.
 
470
        """
 
471
        self.assertEqual(
 
472
            repr(dns.Record_MG('example.com', 4321)),
 
473
            "<MG name=example.com ttl=4321>")
 
474
 
 
475
 
 
476
    def test_mr(self):
 
477
        """
 
478
        The repr of a L{dns.Record_MR} instance includes the name of the
 
479
        mail rename domain and the TTL of the record.
 
480
        """
 
481
        self.assertEqual(
 
482
            repr(dns.Record_MR('example.com', 4321)),
 
483
            "<MR name=example.com ttl=4321>")
 
484
 
 
485
 
 
486
    def test_ptr(self):
 
487
        """
 
488
        The repr of a L{dns.Record_PTR} instance includes the name of the
 
489
        pointer and the TTL of the record.
 
490
        """
 
491
        self.assertEqual(
 
492
            repr(dns.Record_PTR('example.com', 4321)),
 
493
            "<PTR name=example.com ttl=4321>")
 
494
 
 
495
 
 
496
    def test_dname(self):
 
497
        """
 
498
        The repr of a L{dns.Record_DNAME} instance includes the name of the
 
499
        non-terminal DNS name redirection and the TTL of the record.
 
500
        """
 
501
        self.assertEqual(
 
502
            repr(dns.Record_DNAME('example.com', 4321)),
 
503
            "<DNAME name=example.com ttl=4321>")
 
504
 
 
505
 
 
506
    def test_a(self):
 
507
        """
 
508
        The repr of a L{dns.Record_A} instance includes the dotted-quad
 
509
        string representation of the address it is for and the TTL of the
 
510
        record.
 
511
        """
 
512
        self.assertEqual(
 
513
            repr(dns.Record_A('1.2.3.4', 567)),
 
514
            '<A address=1.2.3.4 ttl=567>')
 
515
 
 
516
 
 
517
    def test_soa(self):
 
518
        """
 
519
        The repr of a L{dns.Record_SOA} instance includes all of the
 
520
        authority fields.
 
521
        """
 
522
        self.assertEqual(
 
523
            repr(dns.Record_SOA(mname='mName', rname='rName', serial=123,
 
524
                                refresh=456, retry=789, expire=10,
 
525
                                minimum=11, ttl=12)),
 
526
            "<SOA mname=mName rname=rName serial=123 refresh=456 "
 
527
            "retry=789 expire=10 minimum=11 ttl=12>")
 
528
 
 
529
 
 
530
    def test_null(self):
 
531
        """
 
532
        The repr of a L{dns.Record_NULL} instance includes the repr of its
 
533
        payload and the TTL of the record.
 
534
        """
 
535
        self.assertEqual(
 
536
            repr(dns.Record_NULL('abcd', 123)),
 
537
            "<NULL payload='abcd' ttl=123>")
 
538
 
 
539
 
 
540
    def test_wks(self):
 
541
        """
 
542
        The repr of a L{dns.Record_WKS} instance includes the dotted-quad
 
543
        string representation of the address it is for, the IP protocol
 
544
        number it is for, and the TTL of the record.
 
545
        """
 
546
        self.assertEqual(
 
547
            repr(dns.Record_WKS('2.3.4.5', 7, ttl=8)),
 
548
            "<WKS address=2.3.4.5 protocol=7 ttl=8>")
 
549
 
 
550
 
 
551
    def test_aaaa(self):
 
552
        """
 
553
        The repr of a L{dns.Record_AAAA} instance includes the colon-separated
 
554
        hex string representation of the address it is for and the TTL of the
 
555
        record.
 
556
        """
 
557
        self.assertEqual(
 
558
            repr(dns.Record_AAAA('8765::1234', ttl=10)),
 
559
            "<AAAA address=8765::1234 ttl=10>")
 
560
 
 
561
 
 
562
    def test_a6(self):
 
563
        """
 
564
        The repr of a L{dns.Record_A6} instance includes the colon-separated
 
565
        hex string representation of the address it is for and the TTL of the
 
566
        record.
 
567
        """
 
568
        self.assertEqual(
 
569
            repr(dns.Record_A6(0, '1234::5678', 'foo.bar', ttl=10)),
 
570
            "<A6 suffix=1234::5678 prefix=foo.bar ttl=10>")
 
571
 
 
572
 
 
573
    def test_srv(self):
 
574
        """
 
575
        The repr of a L{dns.Record_SRV} instance includes the name and port of
 
576
        the target and the priority, weight, and TTL of the record.
 
577
        """
 
578
        self.assertEqual(
 
579
            repr(dns.Record_SRV(1, 2, 3, 'example.org', 4)),
 
580
            "<SRV priority=1 weight=2 target=example.org port=3 ttl=4>")
 
581
 
 
582
 
 
583
    def test_naptr(self):
 
584
        """
 
585
        The repr of a L{dns.Record_NAPTR} instance includes the order,
 
586
        preference, flags, service, regular expression, replacement, and TTL of
 
587
        the record.
 
588
        """
 
589
        self.assertEqual(
 
590
            repr(dns.Record_NAPTR(5, 9, "S", "http", "/foo/bar/i", "baz", 3)),
 
591
            "<NAPTR order=5 preference=9 flags=S service=http "
 
592
            "regexp=/foo/bar/i replacement=baz ttl=3>")
 
593
 
 
594
 
 
595
    def test_afsdb(self):
 
596
        """
 
597
        The repr of a L{dns.Record_AFSDB} instance includes the subtype,
 
598
        hostname, and TTL of the record.
 
599
        """
 
600
        self.assertEqual(
 
601
            repr(dns.Record_AFSDB(3, 'example.org', 5)),
 
602
            "<AFSDB subtype=3 hostname=example.org ttl=5>")
 
603
 
 
604
 
 
605
    def test_rp(self):
 
606
        """
 
607
        The repr of a L{dns.Record_RP} instance includes the mbox, txt, and TTL
 
608
        fields of the record.
 
609
        """
 
610
        self.assertEqual(
 
611
            repr(dns.Record_RP('alice.example.com', 'admin.example.com', 3)),
 
612
            "<RP mbox=alice.example.com txt=admin.example.com ttl=3>")
 
613
 
 
614
 
 
615
    def test_hinfo(self):
 
616
        """
 
617
        The repr of a L{dns.Record_HINFO} instance includes the cpu, os, and
 
618
        TTL fields of the record.
 
619
        """
 
620
        self.assertEqual(
 
621
            repr(dns.Record_HINFO('sparc', 'minix', 12)),
 
622
            "<HINFO cpu='sparc' os='minix' ttl=12>")
 
623
 
 
624
 
 
625
    def test_minfo(self):
 
626
        """
 
627
        The repr of a L{dns.Record_MINFO} instance includes the rmailbx,
 
628
        emailbx, and TTL fields of the record.
 
629
        """
 
630
        self.assertEqual(
 
631
            repr(dns.Record_MINFO('alice.example.com', 'bob.example.com', 15)),
 
632
            "<MINFO responsibility=alice.example.com "
 
633
            "errors=bob.example.com ttl=15>")
 
634
 
 
635
 
 
636
    def test_mx(self):
 
637
        """
 
638
        The repr of a L{dns.Record_MX} instance includes the preference, name,
 
639
        and TTL fields of the record.
 
640
        """
 
641
        self.assertEqual(
 
642
            repr(dns.Record_MX(13, 'mx.example.com', 2)),
 
643
            "<MX preference=13 name=mx.example.com ttl=2>")
 
644
 
 
645
 
 
646
    def test_txt(self):
 
647
        """
 
648
        The repr of a L{dns.Record_TXT} instance includes the data and ttl
 
649
        fields of the record.
 
650
        """
 
651
        self.assertEqual(
 
652
            repr(dns.Record_TXT("foo", "bar", ttl=15)),
 
653
            "<TXT data=['foo', 'bar'] ttl=15>")
 
654
 
 
655
 
 
656
 
 
657
class _Equal(object):
 
658
    """
 
659
    A class the instances of which are equal to anything and everything.
 
660
    """
 
661
    def __eq__(self, other):
 
662
        return True
 
663
 
 
664
 
 
665
    def __ne__(self, other):
 
666
        return False
 
667
 
 
668
 
 
669
 
 
670
class _NotEqual(object):
 
671
    """
 
672
    A class the instances of which are equal to nothing.
 
673
    """
 
674
    def __eq__(self, other):
 
675
        return False
 
676
 
 
677
 
 
678
    def __ne__(self, other):
 
679
        return True
 
680
 
 
681
 
 
682
 
 
683
class EqualityTests(unittest.TestCase):
 
684
    """
 
685
    Tests for the equality and non-equality behavior of record classes.
 
686
    """
 
687
    def _equalityTest(self, firstValueOne, secondValueOne, valueTwo):
 
688
        """
 
689
        Assert that C{firstValueOne} is equal to C{secondValueOne} but not
 
690
        equal to C{valueOne} and that it defines equality cooperatively with
 
691
        other types it doesn't know about.
 
692
        """
 
693
        # This doesn't use assertEqual and assertNotEqual because the exact
 
694
        # operator those functions use is not very well defined.  The point
 
695
        # of these assertions is to check the results of the use of specific
 
696
        # operators (precisely to ensure that using different permutations
 
697
        # (eg "x == y" or "not (x != y)") which should yield the same results
 
698
        # actually does yield the same result). -exarkun
 
699
        self.assertTrue(firstValueOne == firstValueOne)
 
700
        self.assertTrue(firstValueOne == secondValueOne)
 
701
        self.assertFalse(firstValueOne == valueTwo)
 
702
        self.assertFalse(firstValueOne != firstValueOne)
 
703
        self.assertFalse(firstValueOne != secondValueOne)
 
704
        self.assertTrue(firstValueOne != valueTwo)
 
705
        self.assertTrue(firstValueOne == _Equal())
 
706
        self.assertFalse(firstValueOne != _Equal())
 
707
        self.assertFalse(firstValueOne == _NotEqual())
 
708
        self.assertTrue(firstValueOne != _NotEqual())
 
709
 
 
710
 
 
711
    def _simpleEqualityTest(self, cls):
 
712
        # Vary the TTL
 
713
        self._equalityTest(
 
714
            cls('example.com', 123),
 
715
            cls('example.com', 123),
 
716
            cls('example.com', 321))
 
717
        # Vary the name
 
718
        self._equalityTest(
 
719
            cls('example.com', 123),
 
720
            cls('example.com', 123),
 
721
            cls('example.org', 123))
 
722
 
 
723
 
 
724
    def test_rrheader(self):
 
725
        """
 
726
        Two L{dns.RRHeader} instances compare equal if and only if they have
 
727
        the same name, type, class, time to live, payload, and authoritative
 
728
        bit.
 
729
        """
 
730
        # Vary the name
 
731
        self._equalityTest(
 
732
            dns.RRHeader('example.com', payload=dns.Record_A('1.2.3.4')),
 
733
            dns.RRHeader('example.com', payload=dns.Record_A('1.2.3.4')),
 
734
            dns.RRHeader('example.org', payload=dns.Record_A('1.2.3.4')))
 
735
 
 
736
        # Vary the payload
 
737
        self._equalityTest(
 
738
            dns.RRHeader('example.com', payload=dns.Record_A('1.2.3.4')),
 
739
            dns.RRHeader('example.com', payload=dns.Record_A('1.2.3.4')),
 
740
            dns.RRHeader('example.com', payload=dns.Record_A('1.2.3.5')))
 
741
 
 
742
        # Vary the type.  Leave the payload as None so that we don't have to
 
743
        # provide non-equal values.
 
744
        self._equalityTest(
 
745
            dns.RRHeader('example.com', dns.A),
 
746
            dns.RRHeader('example.com', dns.A),
 
747
            dns.RRHeader('example.com', dns.MX))
 
748
 
 
749
        # Probably not likely to come up.  Most people use the internet.
 
750
        self._equalityTest(
 
751
            dns.RRHeader('example.com', cls=dns.IN, payload=dns.Record_A('1.2.3.4')),
 
752
            dns.RRHeader('example.com', cls=dns.IN, payload=dns.Record_A('1.2.3.4')),
 
753
            dns.RRHeader('example.com', cls=dns.CS, payload=dns.Record_A('1.2.3.4')))
 
754
 
 
755
        # Vary the ttl
 
756
        self._equalityTest(
 
757
            dns.RRHeader('example.com', ttl=60, payload=dns.Record_A('1.2.3.4')),
 
758
            dns.RRHeader('example.com', ttl=60, payload=dns.Record_A('1.2.3.4')),
 
759
            dns.RRHeader('example.com', ttl=120, payload=dns.Record_A('1.2.3.4')))
 
760
 
 
761
        # Vary the auth bit
 
762
        self._equalityTest(
 
763
            dns.RRHeader('example.com', auth=1, payload=dns.Record_A('1.2.3.4')),
 
764
            dns.RRHeader('example.com', auth=1, payload=dns.Record_A('1.2.3.4')),
 
765
            dns.RRHeader('example.com', auth=0, payload=dns.Record_A('1.2.3.4')))
 
766
 
 
767
 
 
768
    def test_ns(self):
 
769
        """
 
770
        Two L{dns.Record_NS} instances compare equal if and only if they have
 
771
        the same name and TTL.
 
772
        """
 
773
        self._simpleEqualityTest(dns.Record_NS)
 
774
 
 
775
 
 
776
    def test_md(self):
 
777
        """
 
778
        Two L{dns.Record_MD} instances compare equal if and only if they have
 
779
        the same name and TTL.
 
780
        """
 
781
        self._simpleEqualityTest(dns.Record_MD)
 
782
 
 
783
 
 
784
    def test_mf(self):
 
785
        """
 
786
        Two L{dns.Record_MF} instances compare equal if and only if they have
 
787
        the same name and TTL.
 
788
        """
 
789
        self._simpleEqualityTest(dns.Record_MF)
 
790
 
 
791
 
 
792
    def test_cname(self):
 
793
        """
 
794
        Two L{dns.Record_CNAME} instances compare equal if and only if they
 
795
        have the same name and TTL.
 
796
        """
 
797
        self._simpleEqualityTest(dns.Record_CNAME)
 
798
 
 
799
 
 
800
    def test_mb(self):
 
801
        """
 
802
        Two L{dns.Record_MB} instances compare equal if and only if they have
 
803
        the same name and TTL.
 
804
        """
 
805
        self._simpleEqualityTest(dns.Record_MB)
 
806
 
 
807
 
 
808
    def test_mg(self):
 
809
        """
 
810
        Two L{dns.Record_MG} instances compare equal if and only if they have
 
811
        the same name and TTL.
 
812
        """
 
813
        self._simpleEqualityTest(dns.Record_MG)
 
814
 
 
815
 
 
816
    def test_mr(self):
 
817
        """
 
818
        Two L{dns.Record_MR} instances compare equal if and only if they have
 
819
        the same name and TTL.
 
820
        """
 
821
        self._simpleEqualityTest(dns.Record_MR)
 
822
 
 
823
 
 
824
    def test_ptr(self):
 
825
        """
 
826
        Two L{dns.Record_PTR} instances compare equal if and only if they have
 
827
        the same name and TTL.
 
828
        """
 
829
        self._simpleEqualityTest(dns.Record_PTR)
 
830
 
 
831
 
 
832
    def test_dname(self):
 
833
        """
 
834
        Two L{dns.Record_MD} instances compare equal if and only if they have
 
835
        the same name and TTL.
 
836
        """
 
837
        self._simpleEqualityTest(dns.Record_DNAME)
 
838
 
 
839
 
 
840
    def test_a(self):
 
841
        """
 
842
        Two L{dns.Record_A} instances compare equal if and only if they have
 
843
        the same address and TTL.
 
844
        """
 
845
        # Vary the TTL
 
846
        self._equalityTest(
 
847
            dns.Record_A('1.2.3.4', 5),
 
848
            dns.Record_A('1.2.3.4', 5),
 
849
            dns.Record_A('1.2.3.4', 6))
 
850
        # Vary the address
 
851
        self._equalityTest(
 
852
            dns.Record_A('1.2.3.4', 5),
 
853
            dns.Record_A('1.2.3.4', 5),
 
854
            dns.Record_A('1.2.3.5', 5))
 
855
 
 
856
 
 
857
    def test_soa(self):
 
858
        """
 
859
        Two L{dns.Record_SOA} instances compare equal if and only if they have
 
860
        the same mname, rname, serial, refresh, minimum, expire, retry, and
 
861
        ttl.
 
862
        """
 
863
        # Vary the mname
 
864
        self._equalityTest(
 
865
            dns.Record_SOA('mname', 'rname', 123, 456, 789, 10, 20, 30),
 
866
            dns.Record_SOA('mname', 'rname', 123, 456, 789, 10, 20, 30),
 
867
            dns.Record_SOA('xname', 'rname', 123, 456, 789, 10, 20, 30))
 
868
        # Vary the rname
 
869
        self._equalityTest(
 
870
            dns.Record_SOA('mname', 'rname', 123, 456, 789, 10, 20, 30),
 
871
            dns.Record_SOA('mname', 'rname', 123, 456, 789, 10, 20, 30),
 
872
            dns.Record_SOA('mname', 'xname', 123, 456, 789, 10, 20, 30))
 
873
        # Vary the serial
 
874
        self._equalityTest(
 
875
            dns.Record_SOA('mname', 'rname', 123, 456, 789, 10, 20, 30),
 
876
            dns.Record_SOA('mname', 'rname', 123, 456, 789, 10, 20, 30),
 
877
            dns.Record_SOA('mname', 'rname', 1, 456, 789, 10, 20, 30))
 
878
        # Vary the refresh
 
879
        self._equalityTest(
 
880
            dns.Record_SOA('mname', 'rname', 123, 456, 789, 10, 20, 30),
 
881
            dns.Record_SOA('mname', 'rname', 123, 456, 789, 10, 20, 30),
 
882
            dns.Record_SOA('mname', 'rname', 123, 1, 789, 10, 20, 30))
 
883
        # Vary the minimum
 
884
        self._equalityTest(
 
885
            dns.Record_SOA('mname', 'rname', 123, 456, 789, 10, 20, 30),
 
886
            dns.Record_SOA('mname', 'rname', 123, 456, 789, 10, 20, 30),
 
887
            dns.Record_SOA('mname', 'rname', 123, 456, 1, 10, 20, 30))
 
888
        # Vary the expire
 
889
        self._equalityTest(
 
890
            dns.Record_SOA('mname', 'rname', 123, 456, 789, 10, 20, 30),
 
891
            dns.Record_SOA('mname', 'rname', 123, 456, 789, 10, 20, 30),
 
892
            dns.Record_SOA('mname', 'rname', 123, 456, 789, 1, 20, 30))
 
893
        # Vary the retry
 
894
        self._equalityTest(
 
895
            dns.Record_SOA('mname', 'rname', 123, 456, 789, 10, 20, 30),
 
896
            dns.Record_SOA('mname', 'rname', 123, 456, 789, 10, 20, 30),
 
897
            dns.Record_SOA('mname', 'rname', 123, 456, 789, 10, 1, 30))
 
898
        # Vary the ttl
 
899
        self._equalityTest(
 
900
            dns.Record_SOA('mname', 'rname', 123, 456, 789, 10, 20, 30),
 
901
            dns.Record_SOA('mname', 'rname', 123, 456, 789, 10, 20, 30),
 
902
            dns.Record_SOA('mname', 'xname', 123, 456, 789, 10, 20, 1))
 
903
 
 
904
 
 
905
    def test_null(self):
 
906
        """
 
907
        Two L{dns.Record_NULL} instances compare equal if and only if they have
 
908
        the same payload and ttl.
 
909
        """
 
910
        # Vary the payload
 
911
        self._equalityTest(
 
912
            dns.Record_NULL('foo bar', 10),
 
913
            dns.Record_NULL('foo bar', 10),
 
914
            dns.Record_NULL('bar foo', 10))
 
915
        # Vary the ttl
 
916
        self._equalityTest(
 
917
            dns.Record_NULL('foo bar', 10),
 
918
            dns.Record_NULL('foo bar', 10),
 
919
            dns.Record_NULL('foo bar', 100))
 
920
 
 
921
 
 
922
    def test_wks(self):
 
923
        """
 
924
        Two L{dns.Record_WKS} instances compare equal if and only if they have
 
925
        the same address, protocol, map, and ttl.
 
926
        """
 
927
        # Vary the address
 
928
        self._equalityTest(
 
929
            dns.Record_WKS('1.2.3.4', 1, 'foo', 2),
 
930
            dns.Record_WKS('1.2.3.4', 1, 'foo', 2),
 
931
            dns.Record_WKS('4.3.2.1', 1, 'foo', 2))
 
932
        # Vary the protocol
 
933
        self._equalityTest(
 
934
            dns.Record_WKS('1.2.3.4', 1, 'foo', 2),
 
935
            dns.Record_WKS('1.2.3.4', 1, 'foo', 2),
 
936
            dns.Record_WKS('1.2.3.4', 100, 'foo', 2))
 
937
        # Vary the map
 
938
        self._equalityTest(
 
939
            dns.Record_WKS('1.2.3.4', 1, 'foo', 2),
 
940
            dns.Record_WKS('1.2.3.4', 1, 'foo', 2),
 
941
            dns.Record_WKS('1.2.3.4', 1, 'bar', 2))
 
942
        # Vary the ttl
 
943
        self._equalityTest(
 
944
            dns.Record_WKS('1.2.3.4', 1, 'foo', 2),
 
945
            dns.Record_WKS('1.2.3.4', 1, 'foo', 2),
 
946
            dns.Record_WKS('1.2.3.4', 1, 'foo', 200))
 
947
 
 
948
 
 
949
    def test_aaaa(self):
 
950
        """
 
951
        Two L{dns.Record_AAAA} instances compare equal if and only if they have
 
952
        the same address and ttl.
 
953
        """
 
954
        # Vary the address
 
955
        self._equalityTest(
 
956
            dns.Record_AAAA('1::2', 1),
 
957
            dns.Record_AAAA('1::2', 1),
 
958
            dns.Record_AAAA('2::1', 1))
 
959
        # Vary the ttl
 
960
        self._equalityTest(
 
961
            dns.Record_AAAA('1::2', 1),
 
962
            dns.Record_AAAA('1::2', 1),
 
963
            dns.Record_AAAA('1::2', 10))
 
964
 
 
965
 
 
966
    def test_a6(self):
 
967
        """
 
968
        Two L{dns.Record_A6} instances compare equal if and only if they have
 
969
        the same prefix, prefix length, suffix, and ttl.
 
970
        """
 
971
        # Note, A6 is crazy, I'm not sure these values are actually legal.
 
972
        # Hopefully that doesn't matter for this test. -exarkun
 
973
 
 
974
        # Vary the prefix length
 
975
        self._equalityTest(
 
976
            dns.Record_A6(16, '::abcd', 'example.com', 10),
 
977
            dns.Record_A6(16, '::abcd', 'example.com', 10),
 
978
            dns.Record_A6(32, '::abcd', 'example.com', 10))
 
979
        # Vary the suffix
 
980
        self._equalityTest(
 
981
            dns.Record_A6(16, '::abcd', 'example.com', 10),
 
982
            dns.Record_A6(16, '::abcd', 'example.com', 10),
 
983
            dns.Record_A6(16, '::abcd:0', 'example.com', 10))
 
984
        # Vary the prefix
 
985
        self._equalityTest(
 
986
            dns.Record_A6(16, '::abcd', 'example.com', 10),
 
987
            dns.Record_A6(16, '::abcd', 'example.com', 10),
 
988
            dns.Record_A6(16, '::abcd', 'example.org', 10))
 
989
        # Vary the ttl
 
990
        self._equalityTest(
 
991
            dns.Record_A6(16, '::abcd', 'example.com', 10),
 
992
            dns.Record_A6(16, '::abcd', 'example.com', 10),
 
993
            dns.Record_A6(16, '::abcd', 'example.com', 100))
 
994
 
 
995
 
 
996
    def test_srv(self):
 
997
        """
 
998
        Two L{dns.Record_SRV} instances compare equal if and only if they have
 
999
        the same priority, weight, port, target, and ttl.
 
1000
        """
 
1001
        # Vary the priority
 
1002
        self._equalityTest(
 
1003
            dns.Record_SRV(10, 20, 30, 'example.com', 40),
 
1004
            dns.Record_SRV(10, 20, 30, 'example.com', 40),
 
1005
            dns.Record_SRV(100, 20, 30, 'example.com', 40))
 
1006
        # Vary the weight
 
1007
        self._equalityTest(
 
1008
            dns.Record_SRV(10, 20, 30, 'example.com', 40),
 
1009
            dns.Record_SRV(10, 20, 30, 'example.com', 40),
 
1010
            dns.Record_SRV(10, 200, 30, 'example.com', 40))
 
1011
        # Vary the port
 
1012
        self._equalityTest(
 
1013
            dns.Record_SRV(10, 20, 30, 'example.com', 40),
 
1014
            dns.Record_SRV(10, 20, 30, 'example.com', 40),
 
1015
            dns.Record_SRV(10, 20, 300, 'example.com', 40))
 
1016
        # Vary the target
 
1017
        self._equalityTest(
 
1018
            dns.Record_SRV(10, 20, 30, 'example.com', 40),
 
1019
            dns.Record_SRV(10, 20, 30, 'example.com', 40),
 
1020
            dns.Record_SRV(10, 20, 30, 'example.org', 40))
 
1021
        # Vary the ttl
 
1022
        self._equalityTest(
 
1023
            dns.Record_SRV(10, 20, 30, 'example.com', 40),
 
1024
            dns.Record_SRV(10, 20, 30, 'example.com', 40),
 
1025
            dns.Record_SRV(10, 20, 30, 'example.com', 400))
 
1026
 
 
1027
 
 
1028
    def test_naptr(self):
 
1029
        """
 
1030
        Two L{dns.Record_NAPTR} instances compare equal if and only if they
 
1031
        have the same order, preference, flags, service, regexp, replacement,
 
1032
        and ttl.
 
1033
        """
 
1034
        # Vary the order
 
1035
        self._equalityTest(
 
1036
            dns.Record_NAPTR(1, 2, "u", "sip+E2U", "/foo/bar/", "baz", 12),
 
1037
            dns.Record_NAPTR(1, 2, "u", "sip+E2U", "/foo/bar/", "baz", 12),
 
1038
            dns.Record_NAPTR(2, 2, "u", "sip+E2U", "/foo/bar/", "baz", 12))
 
1039
        # Vary the preference
 
1040
        self._equalityTest(
 
1041
            dns.Record_NAPTR(1, 2, "u", "sip+E2U", "/foo/bar/", "baz", 12),
 
1042
            dns.Record_NAPTR(1, 2, "u", "sip+E2U", "/foo/bar/", "baz", 12),
 
1043
            dns.Record_NAPTR(1, 3, "u", "sip+E2U", "/foo/bar/", "baz", 12))
 
1044
        # Vary the flags
 
1045
        self._equalityTest(
 
1046
            dns.Record_NAPTR(1, 2, "u", "sip+E2U", "/foo/bar/", "baz", 12),
 
1047
            dns.Record_NAPTR(1, 2, "u", "sip+E2U", "/foo/bar/", "baz", 12),
 
1048
            dns.Record_NAPTR(1, 2, "p", "sip+E2U", "/foo/bar/", "baz", 12))
 
1049
        # Vary the service
 
1050
        self._equalityTest(
 
1051
            dns.Record_NAPTR(1, 2, "u", "sip+E2U", "/foo/bar/", "baz", 12),
 
1052
            dns.Record_NAPTR(1, 2, "u", "sip+E2U", "/foo/bar/", "baz", 12),
 
1053
            dns.Record_NAPTR(1, 2, "u", "http", "/foo/bar/", "baz", 12))
 
1054
        # Vary the regexp
 
1055
        self._equalityTest(
 
1056
            dns.Record_NAPTR(1, 2, "u", "sip+E2U", "/foo/bar/", "baz", 12),
 
1057
            dns.Record_NAPTR(1, 2, "u", "sip+E2U", "/foo/bar/", "baz", 12),
 
1058
            dns.Record_NAPTR(1, 2, "u", "sip+E2U", "/bar/foo/", "baz", 12))
 
1059
        # Vary the replacement
 
1060
        self._equalityTest(
 
1061
            dns.Record_NAPTR(1, 2, "u", "sip+E2U", "/foo/bar/", "baz", 12),
 
1062
            dns.Record_NAPTR(1, 2, "u", "sip+E2U", "/foo/bar/", "baz", 12),
 
1063
            dns.Record_NAPTR(1, 2, "u", "sip+E2U", "/bar/foo/", "quux", 12))
 
1064
        # Vary the ttl
 
1065
        self._equalityTest(
 
1066
            dns.Record_NAPTR(1, 2, "u", "sip+E2U", "/foo/bar/", "baz", 12),
 
1067
            dns.Record_NAPTR(1, 2, "u", "sip+E2U", "/foo/bar/", "baz", 12),
 
1068
            dns.Record_NAPTR(1, 2, "u", "sip+E2U", "/bar/foo/", "baz", 5))
 
1069
 
 
1070
 
 
1071
    def test_afsdb(self):
 
1072
        """
 
1073
        Two L{dns.Record_AFSDB} instances compare equal if and only if they
 
1074
        have the same subtype, hostname, and ttl.
 
1075
        """
 
1076
        # Vary the subtype
 
1077
        self._equalityTest(
 
1078
            dns.Record_AFSDB(1, 'example.com', 2),
 
1079
            dns.Record_AFSDB(1, 'example.com', 2),
 
1080
            dns.Record_AFSDB(2, 'example.com', 2))
 
1081
        # Vary the hostname
 
1082
        self._equalityTest(
 
1083
            dns.Record_AFSDB(1, 'example.com', 2),
 
1084
            dns.Record_AFSDB(1, 'example.com', 2),
 
1085
            dns.Record_AFSDB(1, 'example.org', 2))
 
1086
        # Vary the ttl
 
1087
        self._equalityTest(
 
1088
            dns.Record_AFSDB(1, 'example.com', 2),
 
1089
            dns.Record_AFSDB(1, 'example.com', 2),
 
1090
            dns.Record_AFSDB(1, 'example.com', 3))
 
1091
 
 
1092
 
 
1093
    def test_rp(self):
 
1094
        """
 
1095
        Two L{Record_RP} instances compare equal if and only if they have the
 
1096
        same mbox, txt, and ttl.
 
1097
        """
 
1098
        # Vary the mbox
 
1099
        self._equalityTest(
 
1100
            dns.Record_RP('alice.example.com', 'alice is nice', 10),
 
1101
            dns.Record_RP('alice.example.com', 'alice is nice', 10),
 
1102
            dns.Record_RP('bob.example.com', 'alice is nice', 10))
 
1103
        # Vary the txt
 
1104
        self._equalityTest(
 
1105
            dns.Record_RP('alice.example.com', 'alice is nice', 10),
 
1106
            dns.Record_RP('alice.example.com', 'alice is nice', 10),
 
1107
            dns.Record_RP('alice.example.com', 'alice is not nice', 10))
 
1108
        # Vary the ttl
 
1109
        self._equalityTest(
 
1110
            dns.Record_RP('alice.example.com', 'alice is nice', 10),
 
1111
            dns.Record_RP('alice.example.com', 'alice is nice', 10),
 
1112
            dns.Record_RP('alice.example.com', 'alice is nice', 100))
 
1113
 
 
1114
 
 
1115
    def test_hinfo(self):
 
1116
        """
 
1117
        Two L{dns.Record_HINFO} instances compare equal if and only if they
 
1118
        have the same cpu, os, and ttl.
 
1119
        """
 
1120
        # Vary the cpu
 
1121
        self._equalityTest(
 
1122
            dns.Record_HINFO('x86-64', 'plan9', 10),
 
1123
            dns.Record_HINFO('x86-64', 'plan9', 10),
 
1124
            dns.Record_HINFO('i386', 'plan9', 10))
 
1125
        # Vary the os
 
1126
        self._equalityTest(
 
1127
            dns.Record_HINFO('x86-64', 'plan9', 10),
 
1128
            dns.Record_HINFO('x86-64', 'plan9', 10),
 
1129
            dns.Record_HINFO('x86-64', 'plan11', 10))
 
1130
        # Vary the ttl
 
1131
        self._equalityTest(
 
1132
            dns.Record_HINFO('x86-64', 'plan9', 10),
 
1133
            dns.Record_HINFO('x86-64', 'plan9', 10),
 
1134
            dns.Record_HINFO('x86-64', 'plan9', 100))
 
1135
 
 
1136
 
 
1137
    def test_minfo(self):
 
1138
        """
 
1139
        Two L{dns.Record_MINFO} instances compare equal if and only if they
 
1140
        have the same rmailbx, emailbx, and ttl.
 
1141
        """
 
1142
        # Vary the rmailbx
 
1143
        self._equalityTest(
 
1144
            dns.Record_MINFO('rmailbox', 'emailbox', 10),
 
1145
            dns.Record_MINFO('rmailbox', 'emailbox', 10),
 
1146
            dns.Record_MINFO('someplace', 'emailbox', 10))
 
1147
        # Vary the emailbx
 
1148
        self._equalityTest(
 
1149
            dns.Record_MINFO('rmailbox', 'emailbox', 10),
 
1150
            dns.Record_MINFO('rmailbox', 'emailbox', 10),
 
1151
            dns.Record_MINFO('rmailbox', 'something', 10))
 
1152
        # Vary the ttl
 
1153
        self._equalityTest(
 
1154
            dns.Record_MINFO('rmailbox', 'emailbox', 10),
 
1155
            dns.Record_MINFO('rmailbox', 'emailbox', 10),
 
1156
            dns.Record_MINFO('rmailbox', 'emailbox', 100))
 
1157
 
 
1158
 
 
1159
    def test_mx(self):
 
1160
        """
 
1161
        Two L{dns.Record_MX} instances compare equal if and only if they have
 
1162
        the same preference, name, and ttl.
 
1163
        """
 
1164
        # Vary the preference
 
1165
        self._equalityTest(
 
1166
            dns.Record_MX(10, 'example.org', 20),
 
1167
            dns.Record_MX(10, 'example.org', 20),
 
1168
            dns.Record_MX(100, 'example.org', 20))
 
1169
        # Vary the name
 
1170
        self._equalityTest(
 
1171
            dns.Record_MX(10, 'example.org', 20),
 
1172
            dns.Record_MX(10, 'example.org', 20),
 
1173
            dns.Record_MX(10, 'example.net', 20))
 
1174
        # Vary the ttl
 
1175
        self._equalityTest(
 
1176
            dns.Record_MX(10, 'example.org', 20),
 
1177
            dns.Record_MX(10, 'example.org', 20),
 
1178
            dns.Record_MX(10, 'example.org', 200))
 
1179
 
 
1180
 
 
1181
    def test_txt(self):
 
1182
        """
 
1183
        Two L{dns.Record_TXT} instances compare equal if and only if they have
 
1184
        the same data and ttl.
 
1185
        """
 
1186
        # Vary the length of the data
 
1187
        self._equalityTest(
 
1188
            dns.Record_TXT(['foo', 'bar'], 10),
 
1189
            dns.Record_TXT(['foo', 'bar'], 10),
 
1190
            dns.Record_TXT(['foo', 'bar', 'baz'], 10))
 
1191
        # Vary the value of the data
 
1192
        self._equalityTest(
 
1193
            dns.Record_TXT(['foo', 'bar'], 10),
 
1194
            dns.Record_TXT(['foo', 'bar'], 10),
 
1195
            dns.Record_TXT(['bar', 'foo'], 10))
 
1196
        # Vary the ttl
 
1197
        self._equalityTest(
 
1198
            dns.Record_TXT(['foo', 'bar'], 10),
 
1199
            dns.Record_TXT(['foo', 'bar'], 10),
 
1200
            dns.Record_TXT(['foo', 'bar'], 100))