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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-01-16 19:56:10 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060116195610-ykmxbia4mnnod9o2
Tags: 2.1.0-0ubuntu2
debian/copyright: Include copyright for python 2.3; some 2.3 files
are included in the upstream tarball, but not in the binary packages.

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-2004 Twisted Matrix Laboratories.
3
 
# See LICENSE for details.
4
 
 
5
 
"""
6
 
Tests for twisted.names.dns.
7
 
"""
8
 
 
9
 
import socket
10
 
 
11
 
try:
12
 
    from cStringIO import StringIO
13
 
except ImportError:
14
 
    from StringIO import StringIO
15
 
 
16
 
from twisted.trial import unittest
17
 
from twisted.names import dns
18
 
 
19
 
class RoundtripDNSTestCase(unittest.TestCase):
20
 
    """Encoding and then decoding various objects."""
21
 
 
22
 
    names = ["example.org", "go-away.fish.tv", "23strikesback.net"]
23
 
 
24
 
    def testName(self):
25
 
        for n in self.names:
26
 
            # encode the name
27
 
            f = StringIO()
28
 
            dns.Name(n).encode(f)
29
 
 
30
 
            # decode the name
31
 
            f.seek(0, 0)
32
 
            result = dns.Name()
33
 
            result.decode(f)
34
 
            self.assertEquals(result.name, n)
35
 
 
36
 
    def testQuery(self):
37
 
        for n in self.names:
38
 
            for dnstype in range(1, 17):
39
 
                for dnscls in range(1, 5):
40
 
                    # encode the query
41
 
                    f = StringIO()
42
 
                    dns.Query(n, dnstype, dnscls).encode(f)
43
 
 
44
 
                    # decode the result
45
 
                    f.seek(0, 0)
46
 
                    result = dns.Query()
47
 
                    result.decode(f)
48
 
                    self.assertEquals(result.name.name, n)
49
 
                    self.assertEquals(result.type, dnstype)
50
 
                    self.assertEquals(result.cls, dnscls)
51
 
 
52
 
    def testRR(self):
53
 
        # encode the RR
54
 
        f = StringIO()
55
 
        dns.RRHeader("test.org", 3, 4, 17).encode(f)
56
 
 
57
 
        # decode the result
58
 
        f.seek(0, 0)
59
 
        result = dns.RRHeader()
60
 
        result.decode(f)
61
 
        self.assertEquals(str(result.name), "test.org")
62
 
        self.assertEquals(result.type, 3)
63
 
        self.assertEquals(result.cls, 4)
64
 
        self.assertEquals(result.ttl, 17)
65
 
 
66
 
 
67
 
    def testResources(self):
68
 
        names = (
69
 
            "this.are.test.name",
70
 
            "will.compress.will.this.will.name.will.hopefully",
71
 
            "test.CASE.preSErVatIOn.YeAH",
72
 
            "a.s.h.o.r.t.c.a.s.e.t.o.t.e.s.t",
73
 
            "singleton"
74
 
        )
75
 
        for s in names:
76
 
            f = StringIO()
77
 
            dns.SimpleRecord(s).encode(f)
78
 
            f.seek(0, 0)
79
 
            result = dns.SimpleRecord()
80
 
            result.decode(f)
81
 
            self.assertEquals(str(result.name), s)
82
 
 
83
 
    def testHashable(self):
84
 
        records = [
85
 
            dns.Record_NS, dns.Record_MD, dns.Record_MF, dns.Record_CNAME,
86
 
            dns.Record_MB, dns.Record_MG, dns.Record_MR, dns.Record_PTR,
87
 
            dns.Record_DNAME, dns.Record_A, dns.Record_SOA, dns.Record_NULL,
88
 
            dns.Record_WKS, dns.Record_SRV, dns.Record_AFSDB, dns.Record_RP,
89
 
            dns.Record_HINFO, dns.Record_MINFO, dns.Record_MX, dns.Record_TXT,
90
 
            dns.Record_AAAA, dns.Record_A6
91
 
        ]
92
 
 
93
 
        for k in records:
94
 
            k1, k2 = k(), k()
95
 
            hk1 = hash(k1)
96
 
            hk2 = hash(k2)
97
 
            self.assertEquals(hk1, hk2, "%s != %s (for %s)" % (hk1,hk2,k))
98
 
 
99
 
 
100
 
class Encoding(unittest.TestCase):
101
 
    def testNULL(self):
102
 
        bytes = ''.join([chr(i) for i in range(256)])
103
 
        rec = dns.Record_NULL(bytes)
104
 
        rr = dns.RRHeader('testname', dns.NULL, payload=rec)
105
 
        msg1 = dns.Message()
106
 
        msg1.answers.append(rr)
107
 
        s = StringIO()
108
 
        msg1.encode(s)
109
 
        s.seek(0, 0)
110
 
        msg2 = dns.Message()
111
 
        msg2.decode(s)
112
 
 
113
 
        self.failUnless(isinstance(msg2.answers[0].payload, dns.Record_NULL))
114
 
        self.assertEquals(msg2.answers[0].payload.payload, bytes)
115