Ticket #6797: twisted_names_trunk_CERT_tests.diff

File twisted_names_trunk_CERT_tests.diff, 2.9 KB (added by Aaron Spike, 7 years ago)

version of patch with tests

Line 
1
2    def test_algorithmDefaultAttribute(self):
3        """
4        L{dns.Record_CERT.algorithm} is a public L{int} attribute
5        whose default value is 5 (RSA/SHA-1).
6
7        Values are defined in DNSSEC
8        https://tools.ietf.org/html/rfc4034#section-2.1.3
9        """
10        record = dns.Record_CERT()
11        self.assertEqual(record.algorithm, 5)
12
13
14    def test_algorithmOverride(self):
15        """
16        L{dns.Record_CERT.__init__} accepts a C{algorithm}
17        parameter which overrides the
18        L{dns.Record_CERT.algorithm} attribute.
19        """
20        record = dns.Record_CERT(algorithm=255)
21        self.assertEqual(record.algorithm, 255)
22
23
24    def test_publicKeyAttribute(self):
25        """
26        L{dns.Record_CERT.publicKey} is a public L{bytes} attribute
27        whose default value is C{b''}.
28
29        https://tools.ietf.org/html/rfc4034#section-2.1.4
30        """
31        record = dns.Record_CERT()
32        self.assertEqual(record.publicKey, b'')
33
34
35    def test_publicKeyOverride(self):
36        """
37        L{dns.Record_CERT.__init__} accepts a C{publicKey}
38        parameter which overrides the
39        L{dns.Record_CERT.publicKey} attribute.
40        """
41        record = dns.Record_CERT(publicKey=b'foobar')
42        self.assertEqual(record.publicKey, b'foobar')
43
44
45    def test_encode(self):
46        """
47        L{dns.Record_CERT.encode} packs the header fields and the
48        key and writes them to a file like object passed in as an
49        argument.
50        """
51        record = DNSKEY_TEST_DATA.OBJECT()
52        actualBytes = BytesIO()
53        record.encode(actualBytes)
54
55        self.assertEqual(actualBytes.getvalue(), DNSKEY_TEST_DATA.BYTES())
56
57
58    def test_decode(self):
59        """
60        L{dns.Record_CERT.decode} unpacks the header fields from a file
61        like object and populates the attributes of an existing
62        L{dns.Record_CERT} instance.
63        """
64        expectedBytes = DNSKEY_TEST_DATA.BYTES()
65        record = dns.Record_CERT()
66        record.decode(BytesIO(expectedBytes), length=len(expectedBytes))
67
68        self.assertEqual(record, DNSKEY_TEST_DATA.OBJECT())
69
70
71    def test_decodeShorterThanHeader(self):
72        """
73        L{dns.Record_CERT.decode} raises L{EOFError} if the provided
74        file object is shorter than the fixed length header parts. ie
75        everything except key.
76        """
77        record = dns.Record_CERT()
78
79        self.assertRaises(EOFError, record.decode, BytesIO(b'x'), length=1)
80
81
82    def test_decodeShorterThanKey(self):
83        """
84        L{dns.Record_CERT.decode} raises L{EOFError} if the provided
85        file object is shorter than length provided in the length
86        argument.
87        """
88        expectedBytes = DNSKEY_TEST_DATA.BYTES()
89        record = dns.Record_CERT()
90
91        self.assertRaises(
92                EOFError,
93                record.decode,
94                BytesIO(expectedBytes[:-1]), length=len(expectedBytes))