]>
Commit | Line | Data |
---|---|---|
b7ed5e7b RG |
1 | #!/usr/bin/env python |
2 | import dns | |
3 | from dnsdisttests import DNSDistTest | |
4 | ||
5 | class TestSize(DNSDistTest): | |
6 | ||
7 | _payloadSize = 49 | |
8 | _config_template = """ | |
9 | addAction(PayloadSizeRule("smaller", %d), SpoofAction("192.0.2.1")) | |
10 | addAction(PayloadSizeRule("greater", %d), SpoofAction("192.0.2.2")) | |
11 | addAction(PayloadSizeRule("equal", %d), SpoofAction("192.0.2.3")) | |
12 | newServer{address="127.0.0.1:%d"} | |
13 | """ | |
14 | _config_params = ['_payloadSize', '_payloadSize', '_payloadSize', '_testServerPort'] | |
15 | ||
16 | def testPayloadSize(self): | |
17 | """ | |
18 | Size: Check that PayloadSizeRule works | |
19 | """ | |
20 | name = 'payload.size.tests.powerdns.com.' | |
21 | query = dns.message.make_query(name, 'A', 'IN') | |
22 | query.flags &= ~dns.flags.RD | |
23 | expectedResponse = dns.message.make_response(query) | |
24 | rrset = dns.rrset.from_text(name, | |
25 | 60, | |
26 | dns.rdataclass.IN, | |
27 | dns.rdatatype.A, | |
28 | '192.0.2.3') | |
29 | expectedResponse.answer.append(rrset) | |
3e3081b3 | 30 | self.assertEqual(len(query.to_wire()), self._payloadSize) |
b7ed5e7b RG |
31 | |
32 | for method in ("sendUDPQuery", "sendTCPQuery"): | |
33 | sender = getattr(self, method) | |
34 | (_, receivedResponse) = sender(query, response=None, useQueue=False) | |
35 | self.assertTrue(receivedResponse) | |
36 | self.assertEqual(receivedResponse, expectedResponse) | |
3e3081b3 RG |
37 | |
38 | class TestTruncateOversizedResponse(DNSDistTest): | |
39 | ||
40 | _payloadSize = 512 | |
41 | _config_template = """ | |
42 | addResponseAction(PayloadSizeRule("greater", %d), TCResponseAction()) | |
43 | newServer{address="127.0.0.1:%d"} | |
44 | """ | |
45 | _config_params = ['_payloadSize', '_testServerPort'] | |
46 | ||
47 | def testTruncateOversizedResponse(self): | |
48 | """ | |
49 | Size: Truncate oversized response | |
50 | """ | |
51 | name = 'truncate-oversized.size.tests.powerdns.com.' | |
52 | query = dns.message.make_query(name, 'TXT', 'IN') | |
53 | query.flags &= ~dns.flags.RD | |
54 | expectedResponse = dns.message.make_response(query) | |
55 | expectedResponse.flags |= dns.flags.TC | |
56 | ||
57 | backendResponse = dns.message.make_response(query) | |
58 | content = '' | |
59 | for i in range(2): | |
60 | if len(content) > 0: | |
61 | content = content + ' ' | |
62 | content = content + 'A' * 255 | |
63 | rrset = dns.rrset.from_text(name, | |
64 | 3600, | |
65 | dns.rdataclass.IN, | |
66 | dns.rdatatype.TXT, | |
67 | content) | |
68 | backendResponse.answer.append(rrset) | |
69 | self.assertGreater(len(backendResponse.to_wire()), self._payloadSize) | |
70 | ||
71 | (receivedQuery, receivedResponse) = self.sendUDPQuery(query, response=backendResponse) | |
72 | self.assertTrue(receivedQuery) | |
73 | self.assertTrue(receivedResponse) | |
74 | self.assertEqual(receivedResponse, expectedResponse) | |
75 | ||
76 | (receivedQuery, receivedResponse) = self.sendTCPQuery(query, response=backendResponse) | |
77 | self.assertTrue(receivedQuery) | |
78 | self.assertTrue(receivedResponse) | |
79 | self.assertEqual(receivedResponse, backendResponse) |