Browse Source

[1298] tests for format_ functions

Jelte Jansen 13 years ago
parent
commit
10c84106e8
3 changed files with 59 additions and 9 deletions
  1. 46 0
      src/bin/xfrin/tests/xfrin_test.py
  2. 12 8
      src/bin/xfrin/xfrin.py.in
  3. 1 1
      src/bin/xfrin/xfrin_messages.mes

+ 46 - 0
src/bin/xfrin/tests/xfrin_test.py

@@ -2146,6 +2146,52 @@ class TestMain(unittest.TestCase):
         MockXfrin.check_command_hook = raise_exception
         MockXfrin.check_command_hook = raise_exception
         main(MockXfrin, False)
         main(MockXfrin, False)
 
 
+class TestFormatting(unittest.TestCase):
+    # If the formatting functions are moved to a more general library
+    # (ticket #1379), these tests should be moved with them.
+    def test_format_zone_str(self):
+        self.assertEqual("example.com/IN",
+                         format_zone_str(isc.dns.Name("example.com"),
+                         isc.dns.RRClass("IN")))
+        self.assertEqual("example.com/CH",
+                         format_zone_str(isc.dns.Name("example.com"),
+                         isc.dns.RRClass("CH")))
+        self.assertEqual("example.org/IN",
+                         format_zone_str(isc.dns.Name("example.org"),
+                         isc.dns.RRClass("IN")))
+    
+    def test_format_addrinfo(self):
+        # This test may need to be updated if the input type is changed,
+        # right now it is a nested tuple:
+        # (family, sockettype, (address, port))
+        # of which sockettype is ignored
+        self.assertEqual("192.0.2.1:53",
+                         format_addrinfo((socket.AF_INET, None,
+                                          ("192.0.2.1", 53))))
+        self.assertEqual("192.0.2.2:53",
+                         format_addrinfo((socket.AF_INET, None,
+                                          ("192.0.2.2", 53))))
+        self.assertEqual("192.0.2.1:54",
+                         format_addrinfo((socket.AF_INET, None,
+                                          ("192.0.2.1", 54))))
+        self.assertEqual("[::1]:53",
+                         format_addrinfo((socket.AF_INET6, None,
+                                          ("::1", 53))))
+        self.assertEqual("[::2]:53",
+                         format_addrinfo((socket.AF_INET6, None,
+                                          ("::2", 53))))
+        self.assertEqual("[::1]:54",
+                         format_addrinfo((socket.AF_INET6, None,
+                                          ("::1", 54))))
+        self.assertEqual("/some/file",
+                         format_addrinfo((socket.AF_UNIX, None,
+                                          "/some/file")))
+        self.assertRaises(TypeError, format_addrinfo, 1)
+        self.assertRaises(TypeError, format_addrinfo,
+                                     (socket.AF_INET, "asdf"))
+        self.assertRaises(TypeError, format_addrinfo,
+                                     (socket.AF_INET, "asdf", ()))
+
 if __name__== "__main__":
 if __name__== "__main__":
     try:
     try:
         isc.log.resetUnitTestRootLogger()
         isc.log.resetUnitTestRootLogger()

+ 12 - 8
src/bin/xfrin/xfrin.py.in

@@ -129,7 +129,7 @@ def format_zone_str(zone_name, zone_class):
        zone_name (isc.dns.Name) name to format
        zone_name (isc.dns.Name) name to format
        zone_class (isc.dns.RRClass) class to format
        zone_class (isc.dns.RRClass) class to format
     """
     """
-    return zone_name.to_text() + '/' + str(zone_class)
+    return zone_name.to_text(True) + '/' + str(zone_class)
 
 
 def format_addrinfo(addrinfo):
 def format_addrinfo(addrinfo):
     """Helper function to format the addrinfo as a string of the form
     """Helper function to format the addrinfo as a string of the form
@@ -141,12 +141,16 @@ def format_addrinfo(addrinfo):
                  depending on the family, either a 2-tuple with the address
                  depending on the family, either a 2-tuple with the address
                  and port, or a filename
                  and port, or a filename
     """
     """
-    if addrinfo[0] == socket.AF_INET:
-        return str(addrinfo[2][0]) + ":" + str(addrinfo[2][1])
-    elif addrinfo[0] == socket.AF_INET6:
-        return "[" + str(addrinfo[2][0]) + "]:" + str(addrinfo[2][1])
-    else:
-        return str(addrinfo[2])
+    try:
+        if addrinfo[0] == socket.AF_INET:
+            return str(addrinfo[2][0]) + ":" + str(addrinfo[2][1])
+        elif addrinfo[0] == socket.AF_INET6:
+            return "[" + str(addrinfo[2][0]) + "]:" + str(addrinfo[2][1])
+        else:
+            return str(addrinfo[2])
+    except IndexError:
+        raise TypeError("addrinfo argument to format_addrinfo() does not"
+                        "appear to be consisting of (family, socktype, (addr, port))")
 
 
 def get_soa_serial(soa_rdata):
 def get_soa_serial(soa_rdata):
     '''Extract the serial field of an SOA RDATA and returns it as an intger.
     '''Extract the serial field of an SOA RDATA and returns it as an intger.
@@ -1093,7 +1097,7 @@ class Xfrin:
                 if zone_info is None:
                 if zone_info is None:
                     # TODO what to do? no info known about zone. defaults?
                     # TODO what to do? no info known about zone. defaults?
                     errmsg = "Got notification to retransfer unknown zone " + zone_str
                     errmsg = "Got notification to retransfer unknown zone " + zone_str
-                    logger.error(XFRIN_RETRANSFER_UNKNOWN_ZONE, zone_str)
+                    logger.info(XFRIN_RETRANSFER_UNKNOWN_ZONE, zone_str)
                     answer = create_answer(1, errmsg)
                     answer = create_answer(1, errmsg)
                 else:
                 else:
                     master_addr = zone_info.get_master_addr_info()
                     master_addr = zone_info.get_master_addr_info()

+ 1 - 1
src/bin/xfrin/xfrin_messages.mes

@@ -70,7 +70,7 @@ was killed.
 There was a problem sending a message to the zone manager. This most
 There was a problem sending a message to the zone manager. This most
 likely means that the msgq daemon has quit or was killed.
 likely means that the msgq daemon has quit or was killed.
 
 
-% XFRIN_NOTIFY_UNKNOWN_MASTER got notification to retransfer zone %1 from %2/%3, expected %4/%5
+% XFRIN_NOTIFY_UNKNOWN_MASTER got notification to retransfer zone %1 from %2, expected %3
 The system received a notify for the given zone, but the address it came
 The system received a notify for the given zone, but the address it came
 from does not match the master address in the Xfrin configuration. The notify
 from does not match the master address in the Xfrin configuration. The notify
 is ignored. This may indicate that the configuration for the master is wrong,
 is ignored. This may indicate that the configuration for the master is wrong,