Browse Source

[1457] tests for rrset class convert function

Jelte Jansen 13 years ago
parent
commit
e5a8ffb5a8

+ 7 - 5
src/lib/python/isc/ddns/session.py

@@ -98,11 +98,13 @@ def foreach_rr_in_rrset(rrset, method, *kwargs):
 def convert_rrset_class(rrset, rrclass):
 def convert_rrset_class(rrset, rrclass):
     '''Returns a (new) rrset with the data from the given rrset,
     '''Returns a (new) rrset with the data from the given rrset,
        but of the given class. Useful to convert from NONE and ANY to
        but of the given class. Useful to convert from NONE and ANY to
-       a real class'''
-    # QUERY, do we want to do this as a special case of the foreach_rr_in_rrset?
-    # or would that make it too complicated?
-    new_rrset = isc.dns.RRset(rrset.get_name(), rrclass, rrset.get_type(),
-                              rrset.get_ttl())
+       a real class.
+       Note that the caller should be careful what to convert;
+       and DNS error that could happen during wire-format reading
+       could technically occur here, and is not caught by this helper.
+    '''
+    new_rrset = isc.dns.RRset(rrset.get_name(), rrclass,
+                              rrset.get_type(), rrset.get_ttl())
     for rdata in rrset.get_rdata():
     for rdata in rrset.get_rdata():
         # Rdata class is nof modifiable, and must match rrset's
         # Rdata class is nof modifiable, and must match rrset's
         # class, so we need to to some ugly conversion here.
         # class, so we need to to some ugly conversion here.

+ 39 - 2
src/lib/python/isc/ddns/tests/session_tests.py

@@ -194,6 +194,43 @@ class SessionTest(unittest.TestCase):
                           "www.example.org. 3600 IN A 192.0.2.3\n",
                           "www.example.org. 3600 IN A 192.0.2.3\n",
                          ], l)
                          ], l)
 
 
+    def test_convert_rrset_class(self):
+        # Converting an RRSET to a different class should work
+        # if the rdata types can be converted
+        rrset = isc.dns.RRset(isc.dns.Name("www.example.org"),
+                                           isc.dns.RRClass.NONE(),
+                                           isc.dns.RRType.A(),
+                                           isc.dns.RRTTL(3600))
+        rrset.add_rdata(isc.dns.Rdata(rrset.get_type(),
+                                      rrset.get_class(),
+                                      "\# 04 c0 00 02 01"))
+        rrset.add_rdata(isc.dns.Rdata(rrset.get_type(),
+                                      rrset.get_class(),
+                                      "\# 04 c0 00 02 02"))
+        
+        rrset2 = convert_rrset_class(rrset, isc.dns.RRClass.IN())
+        self.assertEqual("www.example.org. 3600 IN A 192.0.2.1\n" +
+                         "www.example.org. 3600 IN A 192.0.2.2\n",
+                         str(rrset2))
+
+        rrset3 = convert_rrset_class(rrset2, isc.dns.RRClass.NONE())
+        self.assertEqual("www.example.org. 3600 CLASS254 A \\# 4 " +
+                         "c0000201\nwww.example.org. 3600 CLASS254 " +
+                         "A \\# 4 c0000202\n",
+                         str(rrset3))
+
+        # depending on what type of bad data is given, a number
+        # of different exceptions could be raised (TODO: i recall
+        # there was a ticket about making a better hierarchy for
+        # dns/parsing related exceptions)
+        self.assertRaises(InvalidRdataLength, convert_rrset_class,
+                          rrset, isc.dns.RRClass.CH())
+        rrset.add_rdata(isc.dns.Rdata(rrset.get_type(),
+                                      rrset.get_class(),
+                                      "\# 02 c0 00"))
+        self.assertRaises(DNSMessageFORMERR, convert_rrset_class,
+                          rrset, isc.dns.RRClass.IN())
+
     def __prereq_helper(self, method, expected, rrset):
     def __prereq_helper(self, method, expected, rrset):
         '''Calls the given method with self.__datasrc_client
         '''Calls the given method with self.__datasrc_client
            and the given rrset, and compares the return value.
            and the given rrset, and compares the return value.
@@ -790,10 +827,10 @@ class SessionTest(unittest.TestCase):
                                                     isc.dns.RRClass.NONE(),
                                                     isc.dns.RRClass.NONE(),
                                                     isc.dns.RRType.A(),
                                                     isc.dns.RRType.A(),
                                                     isc.dns.RRTTL(0))
                                                     isc.dns.RRTTL(0))
-        rrset_update_del_rrset_part.add_rdata(isc.dns.Rdata(rrset_update_a.get_type(),
+        rrset_update_del_rrset_part.add_rdata(isc.dns.Rdata(rrset_update_del_rrset_part.get_type(),
                                                rrset_update_del_rrset_part.get_class(),
                                                rrset_update_del_rrset_part.get_class(),
                                                "\# 04 c0 00 02 02"))
                                                "\# 04 c0 00 02 02"))
-        rrset_update_del_rrset_part.add_rdata(isc.dns.Rdata(rrset_update_a.get_type(),
+        rrset_update_del_rrset_part.add_rdata(isc.dns.Rdata(rrset_update_del_rrset_part.get_type(),
                                                rrset_update_del_rrset_part.get_class(),
                                                rrset_update_del_rrset_part.get_class(),
                                                "\# 04 c0 00 02 03"))
                                                "\# 04 c0 00 02 03"))
         self.rrset_update_del_rrset_part = rrset_update_del_rrset_part
         self.rrset_update_del_rrset_part = rrset_update_del_rrset_part