TMP: play with the debug info for mx records
authorKai Blin <kai@samba.org>
Sun, 13 Jan 2013 23:56:48 +0000 (00:56 +0100)
committerKai Blin <kai@samba.org>
Sun, 13 Jan 2013 23:56:56 +0000 (00:56 +0100)
Signed-off-by: Kai Blin <kai@samba.org>
source4/scripting/python/samba/tests/dns.py

index d01c8ff..bb21f8e 100644 (file)
@@ -23,6 +23,9 @@ import samba.ndr as ndr
 import samba.dcerpc.dns as dns
 from samba.tests import TestCase
 
+FILTER=''.join([(len(repr(chr(x)))==3) and chr(x) or '.' for x in range(256)])
+
+
 class DNSTest(TestCase):
 
     def errstr(self, errcode):
@@ -82,15 +85,19 @@ class DNSTest(TestCase):
         "Helper to get dns domain"
         return os.getenv('REALM', 'example.com').lower()
 
-    def dns_transaction_udp(self, packet, host=os.getenv('SERVER_IP')):
+    def dns_transaction_udp(self, packet, host=os.getenv('SERVER_IP'), dump=False):
         "send a DNS query and read the reply"
         s = None
         try:
             send_packet = ndr.ndr_pack(packet)
+            if dump:
+                print self.hexdump(send_packet)
             s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
             s.connect((host, 53))
             s.send(send_packet, 0)
             recv_packet = s.recv(2048, 0)
+            if dump:
+                print self.hexdump(recv_packet)
             return ndr.ndr_unpack(dns.name_packet, recv_packet)
         finally:
             if s is not None:
@@ -112,6 +119,15 @@ class DNSTest(TestCase):
                 if s is not None:
                     s.close()
 
+    def hexdump(self, src, length=8):
+        N=0; result=''
+        while src:
+           s,src = src[:length],src[length:]
+           hexa = ' '.join(["%02X"%ord(x) for x in s])
+           s = s.translate(FILTER)
+           result += "%04X   %-*s   %s\n" % (N, length*3, hexa, s)
+           N+=length
+        return result
 
 class TestSimpleQueries(DNSTest):
 
@@ -530,12 +546,14 @@ class TestDNSUpdates(DNSTest):
         r.length = 0xffff
         r.rdata = dns.mx_record()
         r.rdata.preference = 10
-        r.rdata.exchange = 'mail.%s' % self.get_dns_domain()
+        r.rdata.exchange = 'mail' #% self.get_dns_domain()
         updates.append(r)
         p.nscount = len(updates)
         p.nsrecs = updates
 
-        response = self.dns_transaction_udp(p)
+        print ndr.ndr_print(p)
+        response = self.dns_transaction_udp(p, dump=True)
+        print ndr.ndr_print(response)
         self.assert_dns_rcode_equals(response, dns.DNS_RCODE_OK)
 
         p = self.make_name_packet(dns.DNS_OPCODE_QUERY)
@@ -549,8 +567,10 @@ class TestDNSUpdates(DNSTest):
         response = self.dns_transaction_udp(p)
         self.assert_dns_rcode_equals(response, dns.DNS_RCODE_OK)
         self.assertEqual(response.ancount, 1)
-        self.assertEqual(response.answers[0].rdata.preference, 10)
-        self.assertEqual(response.answers[0].rdata.exchange, 'mail.%s' % self.get_dns_domain())
+        ans = response.answers[0]
+        self.assertEqual(ans.rr_type, dns.DNS_QTYPE_MX)
+        self.assertEqual(ans.rdata.preference, 10)
+        self.assertEqual(ans.rdata.exchange, 'mail.%s' % self.get_dns_domain())
 
 
 class TestComplexQueries(DNSTest):