summaryrefslogtreecommitdiffstats
path: root/src/lib/dns/python/tests/rrset_python_test.py
blob: d848d278b6db9544e6e560eae2ad653b9c5a3516 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Copyright (C) 2010  Internet Systems Consortium.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#
# Tests for the rrtype part of the pydnspp module
#

import sys
import unittest
import os
from pydnspp import *

class TestModuleSpec(unittest.TestCase):

    def setUp(self):
        self.test_name = Name("test.example.com")
        self.test_domain = Name("example.com")
        self.test_nsname = Name("ns.example.com")
        self.rrset_a = RRset(self.test_name, RRClass("IN"), RRType("A"), RRTTL(3600))
        self.rrset_a_empty = RRset(self.test_name, RRClass("IN"), RRType("A"), RRTTL(3600))
        self.rrset_any_a_empty = RRset(self.test_name, RRClass("ANY"), RRType("A"), RRTTL(3600))
        self.rrset_ns = RRset(self.test_domain, RRClass("IN"), RRType("NS"), RRTTL(86400))
        self.rrset_ch_txt = RRset(self.test_domain, RRClass("CH"), RRType("TXT"), RRTTL(0))
        self.MAX_RDATA_COUNT = 100
        self.rrset_a.add_rdata(Rdata(RRType("A"), RRClass("IN"), "192.0.2.1"));
        self.rrset_a.add_rdata(Rdata(RRType("A"), RRClass("IN"), "192.0.2.2"));

    def test_init(self):
        self.assertRaises(TypeError, RRset)

    def test_get_rdata_count(self):
        for i in range(0, self.MAX_RDATA_COUNT):
            self.assertEqual(i, self.rrset_a_empty.get_rdata_count())
            self.rrset_a_empty.add_rdata(Rdata(RRType("A"), RRClass("IN"), "192.0.2.1"))

    def test_get_length(self):
        # Empty RRset should throw
        self.assertRaises(EmptyRRset, self.rrset_a_empty.get_length);

        # Unless it is type ANY or NONE:
        # test.example.com = 1 + 4 + 1 + 7 + 1 + 3 + 1 = 18 octets
        # TYPE field = 2 octets
        # CLASS field = 2 octets
        # TTL field = 4 octets
        # RDLENGTH field = 2 octets
        # Total = 18 + 2 + 2 + 4 + 2 = 28 octets
        self.assertEqual(28, self.rrset_any_a_empty.get_length())

        # Single A RR:
        # 28 octets (above) + 4 octets (A RDATA) = 32 octets
        # With 2 A RRs:
        self.assertEqual(32 + 32, self.rrset_a.get_length())

    def test_get_name(self):
        self.assertEqual(self.test_name, self.rrset_a.get_name())
        self.assertEqual(self.test_domain, self.rrset_ns.get_name())

    def test_get_class(self):
        self.assertEqual(RRClass("IN"), self.rrset_a.get_class());
        self.assertEqual(RRClass("CH"), self.rrset_ch_txt.get_class());

    def test_get_type(self):
        self.assertEqual(RRType("A"), self.rrset_a.get_type());
        self.assertEqual(RRType("NS"), self.rrset_ns.get_type());
        self.assertEqual(RRType("TXT"), self.rrset_ch_txt.get_type());

    def test_get_ttl(self):
        self.assertEqual(RRTTL(3600), self.rrset_a.get_ttl());
        self.assertEqual(RRTTL(86400), self.rrset_ns.get_ttl());
        self.assertEqual(RRTTL(0), self.rrset_ch_txt.get_ttl());

    def test_set_ttl(self):
        self.rrset_a.set_ttl(RRTTL(86400));
        self.assertEqual(RRTTL(86400), self.rrset_a.get_ttl());
        self.rrset_a.set_ttl(RRTTL(0));
        self.assertEqual(RRTTL(0), self.rrset_a.get_ttl());
        self.assertRaises(TypeError, self.rrset_a.set_ttl, 1)

    def test_add_rdata(self):
        # no iterator to read out yet (TODO: add addition test once implemented)

        # This should result in TypeError, but FreeBSD 9.1 cannot correctly
        # catch the expected internal C++ exception, resulting in SystemError.
        # In general it's not a good practice to weaken the test condition for
        # a limited set of buggy environment, but this seems to be the only
        # case it could fail this way, so we'd live with it.  See #2887.
        self.assertRaises((TypeError, SystemError), self.rrset_a.add_rdata,
                          Rdata(RRType("NS"), RRClass("IN"), "test.name."))

    def test_to_text(self):
        self.assertEqual("test.example.com. 3600 IN A 192.0.2.1\n"
                         "test.example.com. 3600 IN A 192.0.2.2\n",
                         self.rrset_a.to_text());
        self.assertEqual("test.example.com. 3600 IN A 192.0.2.1\n"
                         "test.example.com. 3600 IN A 192.0.2.2\n",
                         str(self.rrset_a));

        self.assertRaises(EmptyRRset, self.rrset_a_empty.to_text)

        self.assertEqual("test.example.com. 3600 ANY A\n",
                         self.rrset_any_a_empty.to_text())

    def test_to_wire_buffer(self):
        exp_buffer = bytearray(b'\x04test\x07example\x03com\x00\x00\x01\x00\x01\x00\x00\x0e\x10\x00\x04\xc0\x00\x02\x01\x04test\x07example\x03com\x00\x00\x01\x00\x01\x00\x00\x0e\x10\x00\x04\xc0\x00\x02\x02')
        buffer = bytearray()
        self.rrset_a.to_wire(buffer)
        self.assertEqual(exp_buffer, buffer)

        self.assertRaises(EmptyRRset, self.rrset_a_empty.to_wire, buffer);
        self.assertRaises(TypeError, self.rrset_a.to_wire, 1)

        exp_buffer = bytearray(b'\x04test\x07example\x03com\x00\x00\x01\x00\xff\x00\x00\x0e\x10\x00\x00')
        buffer = bytearray()
        self.rrset_any_a_empty.to_wire(buffer)
        self.assertEqual(exp_buffer, buffer)

    def test_to_wire_renderer(self):
        exp_buffer = bytearray(b'\x04test\x07example\x03com\x00\x00\x01\x00\x01\x00\x00\x0e\x10\x00\x04\xc0\x00\x02\x01\xc0\x00\x00\x01\x00\x01\x00\x00\x0e\x10\x00\x04\xc0\x00\x02\x02')
        mr = MessageRenderer()
        self.rrset_a.to_wire(mr)
        self.assertEqual(exp_buffer, mr.get_data())

    def test_get_rdata(self):
        rdata = [ Rdata(RRType("A"), RRClass("IN"), "192.0.2.1"),
                  Rdata(RRType("A"), RRClass("IN"), "192.0.2.2")
                ]
        self.assertEqual(rdata, self.rrset_a.get_rdata())
        self.assertEqual([], self.rrset_a_empty.get_rdata())

        # We always make a new deep copy in get_rdata(), so the reference
        # count of the returned list and its each item should be 1; otherwise
        # they would leak.
        self.assertEqual(1, sys.getrefcount(self.rrset_a.get_rdata()))
        self.assertEqual(1, sys.getrefcount(self.rrset_a.get_rdata()[0]))

if __name__ == '__main__':
    unittest.main()