BIND 10 trac2004, updated. b56e5b30bdb16a5553c9c45dbe3dca73bc4613d0 [2004] Add Python unittests for the specialized exceptions that we catch

BIND 10 source code commits bind10-changes at lists.isc.org
Mon Jun 4 20:23:28 UTC 2012


The branch, trac2004 has been updated
       via  b56e5b30bdb16a5553c9c45dbe3dca73bc4613d0 (commit)
       via  c28fd5d9f54d75c77ffb8d1ef3fdd363952ee694 (commit)
      from  1fdb0ee73a22d8002cbfefe66d70cf22d5d784d8 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit b56e5b30bdb16a5553c9c45dbe3dca73bc4613d0
Author: Mukund Sivaraman <muks at isc.org>
Date:   Tue Jun 5 01:52:47 2012 +0530

    [2004] Add Python unittests for the specialized exceptions that we catch

commit c28fd5d9f54d75c77ffb8d1ef3fdd363952ee694
Author: Mukund Sivaraman <muks at isc.org>
Date:   Tue Jun 5 01:34:06 2012 +0530

    [2004] Catch std::exception before ... to print some detail if possible
    
    Also update the messages returned.

-----------------------------------------------------------------------

Summary of changes:
 src/lib/dns/python/message_python.cc            |  186 ++++++++++++-----------
 src/lib/dns/python/tests/message_python_test.py |   14 ++
 2 files changed, 111 insertions(+), 89 deletions(-)

-----------------------------------------------------------------------
diff --git a/src/lib/dns/python/message_python.cc b/src/lib/dns/python/message_python.cc
index 186cefb..f08f62c 100644
--- a/src/lib/dns/python/message_python.cc
+++ b/src/lib/dns/python/message_python.cc
@@ -219,12 +219,14 @@ Message_getHeaderFlag(s_Message* self, PyObject* args) {
     } catch (const isc::InvalidParameter& ip) {
         PyErr_Clear();
         PyErr_SetString(po_InvalidParameter, ip.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.get_header_flag(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
         PyErr_SetString(po_IscException,
-                        "Unexpected exception in getHeaderFlag");
-        return (NULL);
+                        "Unexpected exception in Message.get_header_flag()");
     }
+    return (NULL);
 }
 
 PyObject*
@@ -250,16 +252,17 @@ Message_setHeaderFlag(s_Message* self, PyObject* args) {
     } catch (const InvalidMessageOperation& imo) {
         PyErr_Clear();
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
     } catch (const isc::InvalidParameter& ip) {
         PyErr_Clear();
         PyErr_SetString(po_InvalidParameter, ip.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.set_header_flag(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
         PyErr_SetString(po_IscException,
-                        "Unexpected exception in setHeaderFlag");
-        return (NULL);
+                        "Unexpected exception in Message.set_header_flag()");
     }
+    return (NULL);
 }
 
 PyObject*
@@ -287,12 +290,14 @@ Message_setQid(s_Message* self, PyObject* args) {
         Py_RETURN_NONE;
     } catch (const InvalidMessageOperation& imo) {
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.get_qid(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
         PyErr_SetString(po_IscException,
-                        "Unexpected exception in setQid");
-        return (NULL);
+                        "Unexpected exception in Message.set_qid()");
     }
+    return (NULL);
 }
 
 PyObject*
@@ -301,11 +306,14 @@ Message_getRcode(s_Message* self) {
         return (createRcodeObject(self->cppobj->getRcode()));
     } catch (const InvalidMessageOperation& imo) {
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.get_rcode(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
-        PyErr_SetString(po_IscException, "Unexpected exception");
-        return (NULL);
+        PyErr_SetString(po_IscException,
+                        "Unexpected exception in Message.get_rcode()");
     }
+    return (NULL);
 }
 
 PyObject*
@@ -319,12 +327,14 @@ Message_setRcode(s_Message* self, PyObject* args) {
         Py_RETURN_NONE;
     } catch (const InvalidMessageOperation& imo) {
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.set_rcode(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
         PyErr_SetString(po_IscException,
-                        "Unexpected exception in setRcode");
-        return (NULL);
+                        "Unexpected exception in Message.set_rcode()");
     }
+    return (NULL);
 }
 
 PyObject*
@@ -333,17 +343,14 @@ Message_getOpcode(s_Message* self) {
         return (createOpcodeObject(self->cppobj->getOpcode()));
     } catch (const InvalidMessageOperation& imo) {
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
     } catch (const exception& ex) {
-        const string ex_what =
-            "Failed to get message opcode: " + string(ex.what());
+        const string ex_what = "Error in Message.get_opcode(): " + string(ex.what());
         PyErr_SetString(po_IscException, ex_what.c_str());
-        return (NULL);
     } catch (...) {
         PyErr_SetString(po_IscException,
-                        "Unexpected exception getting opcode from message");
-        return (NULL);
+                        "Unexpected exception in Message.get_opcode()");
     }
+    return (NULL);
 }
 
 PyObject*
@@ -357,12 +364,14 @@ Message_setOpcode(s_Message* self, PyObject* args) {
         Py_RETURN_NONE;
     } catch (const InvalidMessageOperation& imo) {
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.set_opcode(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
         PyErr_SetString(po_IscException,
-                        "Unexpected exception in setOpcode");
-        return (NULL);
+                        "Unexpected exception in Message.set_opcode()");
     }
+    return (NULL);
 }
 
 PyObject*
@@ -374,12 +383,11 @@ Message_getEDNS(s_Message* self) {
     try {
         return (createEDNSObject(*src));
     } catch (const exception& ex) {
-        const string ex_what =
-            "Failed to get EDNS from message: " + string(ex.what());
+        const string ex_what = "Error in Message.get_edns(): " + string(ex.what());
         PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
         PyErr_SetString(PyExc_SystemError,
-                        "Unexpected failure getting EDNS from message");
+                        "Unexpected exception in Message.get_edns()");
     }
     return (NULL);
 }
@@ -395,12 +403,14 @@ Message_setEDNS(s_Message* self, PyObject* args) {
         Py_RETURN_NONE;
     } catch (const InvalidMessageOperation& imo) {
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.set_edns(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
         PyErr_SetString(po_IscException,
-                        "Unexpected exception in setEDNS");
-        return (NULL);
+                        "Unexpected exception in Message.set_edns()");
     }
+    return (NULL);
 }
 
 PyObject*
@@ -416,13 +426,11 @@ Message_getTSIGRecord(s_Message* self) {
     } catch (const InvalidMessageOperation& ex) {
         PyErr_SetString(po_InvalidMessageOperation, ex.what());
     } catch (const exception& ex) {
-        const string ex_what =
-            "Unexpected failure in getting TSIGRecord from message: " +
-            string(ex.what());
+        const string ex_what = "Error in Message.get_tsig_record(): " + string(ex.what());
         PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
-        PyErr_SetString(PyExc_SystemError, "Unexpected failure in "
-                        "getting TSIGRecord from message");
+        PyErr_SetString(po_IscException,
+                        "Unexpected exception in Message.get_tsig_record()");
     }
     return (NULL);
 }
@@ -441,12 +449,14 @@ Message_getRRCount(s_Message* self, PyObject* args) {
                                   static_cast<Message::Section>(section))));
     } catch (const isc::OutOfRange& ex) {
         PyErr_SetString(PyExc_OverflowError, ex.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.get_rr_count(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
         PyErr_SetString(po_IscException,
-                        "Unexpected exception in getRRCount");
-        return (NULL);
+                        "Unexpected exception in Message.get_rr_count()");
     }
+    return (NULL);
 }
 
 // This is a helper templated class commonly used for getQuestion and
@@ -487,13 +497,11 @@ Message_getQuestion(PyObject* po_self, PyObject*) {
     } catch (const InvalidMessageSection& ex) {
         PyErr_SetString(po_InvalidMessageSection, ex.what());
     } catch (const exception& ex) {
-        const string ex_what =
-            "Unexpected failure in Message.get_question: " +
-            string(ex.what());
+        const string ex_what = "Error in Message.get_question(): " + string(ex.what());
         PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
-        PyErr_SetString(PyExc_SystemError,
-                        "Unexpected failure in Message.get_question");
+        PyErr_SetString(po_IscException,
+                        "Unexpected exception in Message.get_question()");
     }
     return (NULL);
 }
@@ -523,13 +531,11 @@ Message_getSection(PyObject* po_self, PyObject* args) {
     } catch (const InvalidMessageSection& ex) {
         PyErr_SetString(po_InvalidMessageSection, ex.what());
     } catch (const exception& ex) {
-        const string ex_what =
-            "Unexpected failure in Message.get_section: " +
-            string(ex.what());
+        const string ex_what = "Error in Message.get_section(): " + string(ex.what());
         PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
-        PyErr_SetString(PyExc_SystemError,
-                        "Unexpected failure in Message.get_section");
+        PyErr_SetString(po_IscException,
+                        "Unexpected exception in Message.get_section()");
     }
     return (NULL);
 }
@@ -553,12 +559,14 @@ Message_addQuestion(s_Message* self, PyObject* args) {
     } catch (const InvalidMessageOperation& imo) {
         PyErr_Clear();
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.add_question(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
         PyErr_SetString(po_IscException,
-                        "Unexpected exception in addQuestion");
-        return (NULL);
+                        "Unexpected exception in Message.add_question()");
     }
+    return (NULL);
 }
 
 PyObject*
@@ -577,15 +585,16 @@ Message_addRRset(s_Message* self, PyObject* args) {
         Py_RETURN_NONE;
     } catch (const InvalidMessageOperation& imo) {
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
     } catch (const isc::OutOfRange& ex) {
         PyErr_SetString(PyExc_OverflowError, ex.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.add_rrset(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
         PyErr_SetString(po_IscException,
-                        "Unexpected exception in adding RRset");
-        return (NULL);
+                        "Unexpected exception in Message.add_rrset()");
     }
+    return (NULL);
 }
 
 PyObject*
@@ -607,9 +616,12 @@ Message_clear(s_Message* self, PyObject* args) {
                 return (NULL);
             }
         }
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.clear(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
         PyErr_SetString(po_IscException,
-                        "Unexpected exception when clearing message");
+                        "Unexpected exception in Message.clear()");
     }
     return (NULL);
 }
@@ -627,15 +639,16 @@ Message_clearSection(PyObject* pyself, PyObject* args) {
         Py_RETURN_NONE;
     } catch (const InvalidMessageOperation& imo) {
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
     } catch (const isc::OutOfRange& ex) {
         PyErr_SetString(PyExc_OverflowError, ex.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.clear_section(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
         PyErr_SetString(po_IscException,
-                        "Unexpected exception in clearSection");
-        return (NULL);
+                        "Unexpected exception in Message.clear_section()");
     }
+    return (NULL);
 }
 
 PyObject*
@@ -646,11 +659,14 @@ Message_makeResponse(s_Message* self) {
     } catch (const InvalidMessageOperation& imo) {
         PyErr_Clear();
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.make_response(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
-        PyErr_SetString(po_IscException, "Unexpected exception in Message.makeResponse");
-        return (NULL);
+        PyErr_SetString(po_IscException,
+                        "Unexpected exception in Message.make_response()");
     }
+    return (NULL);
 }
 
 PyObject*
@@ -661,11 +677,14 @@ Message_toText(s_Message* self) {
     } catch (const InvalidMessageOperation& imo) {
         PyErr_Clear();
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.to_text(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
-        PyErr_SetString(po_IscException, "Unexpected exception");
-        return (NULL);
+        PyErr_SetString(po_IscException,
+                        "Unexpected exception in Message.to_text()");
     }
+    return (NULL);
 }
 
 PyObject*
@@ -696,22 +715,18 @@ Message_toWire(s_Message* self, PyObject* args) {
         } catch (const InvalidMessageOperation& imo) {
             PyErr_Clear();
             PyErr_SetString(po_InvalidMessageOperation, imo.what());
-            return (NULL);
         } catch (const TSIGContextError& ex) {
             // toWire() with a TSIG context can fail due to this if the
             // python program has a bug.
             PyErr_SetString(po_TSIGContextError, ex.what());
-            return (NULL);
-        } catch (const std::exception& ex) {
-            // Other exceptions should be rare (most likely an implementation
-            // bug)
-            PyErr_SetString(po_TSIGContextError, ex.what());
-            return (NULL);
+        } catch (const exception& ex) {
+            const string ex_what = "Error in Message.to_wire(): " + string(ex.what());
+            PyErr_SetString(po_TSIGContextError, ex_what.c_str());
         } catch (...) {
-            PyErr_SetString(PyExc_RuntimeError,
-                            "Unexpected C++ exception in Message.to_wire");
-            return (NULL);
+            PyErr_SetString(po_IscException,
+                            "Unexpected exception in Message.to_wire()");
         }
+        return (NULL);
     }
     PyErr_Clear();
     PyErr_SetString(PyExc_TypeError,
@@ -739,29 +754,22 @@ Message_fromWire(PyObject* pyself, PyObject* args) {
             Py_RETURN_NONE;
         } catch (const InvalidMessageOperation& imo) {
             PyErr_SetString(po_InvalidMessageOperation, imo.what());
-            return (NULL);
         } catch (const DNSMessageFORMERR& dmfe) {
             PyErr_SetString(po_DNSMessageFORMERR, dmfe.what());
-            return (NULL);
         } catch (const DNSMessageBADVERS& dmfe) {
             PyErr_SetString(po_DNSMessageBADVERS, dmfe.what());
-            return (NULL);
         } catch (const MessageTooShort& mts) {
             PyErr_SetString(po_MessageTooShort, mts.what());
-            return (NULL);
         } catch (const InvalidBufferPosition& ex) {
             PyErr_SetString(po_DNSMessageFORMERR, ex.what());
-            return (NULL);
         } catch (const exception& ex) {
-            const string ex_what =
-                "Error in Message.from_wire: " + string(ex.what());
-            PyErr_SetString(PyExc_RuntimeError, ex_what.c_str());
-            return (NULL);
+            const string ex_what = "Error in Message.from_wire(): " + string(ex.what());
+            PyErr_SetString(po_IscException, ex_what.c_str());
         } catch (...) {
-            PyErr_SetString(PyExc_RuntimeError,
-                            "Unexpected exception in Message.from_wire");
-            return (NULL);
+            PyErr_SetString(po_IscException,
+                            "Unexpected exception in Message.from_wire()");
         }
+        return (NULL);
     }
 
     PyErr_SetString(PyExc_TypeError,
diff --git a/src/lib/dns/python/tests/message_python_test.py b/src/lib/dns/python/tests/message_python_test.py
index 1ec0e99..1ac612b 100644
--- a/src/lib/dns/python/tests/message_python_test.py
+++ b/src/lib/dns/python/tests/message_python_test.py
@@ -118,6 +118,11 @@ class MessageTest(unittest.TestCase):
         self.assertFalse(self.r.get_header_flag(Message.HEADERFLAG_AD))
         self.assertFalse(self.r.get_header_flag(Message.HEADERFLAG_CD))
 
+        # 0 passed as flag should raise
+        self.assertRaises(InvalidParameter, self.r.get_header_flag, 0)
+        # unused bit
+        self.assertRaises(InvalidParameter, self.r.get_header_flag, 0x80000000)
+
         self.r.set_header_flag(Message.HEADERFLAG_QR)
         self.assertTrue(self.r.get_header_flag(Message.HEADERFLAG_QR))
 
@@ -267,6 +272,15 @@ class MessageTest(unittest.TestCase):
         self.assertEqual(1, sys.getrefcount(self.r.get_question()))
         self.assertEqual(1, sys.getrefcount(self.r.get_question()[0]))
 
+        # Message.add_question() called in non-RENDER mode should assert
+        self.r.clear(Message.PARSE)
+        self.assertRaises(InvalidMessageOperation, self.r.add_question, q)
+
+    def test_make_response(self):
+        # Message.make_response() called in non-PARSE mode should assert
+        self.r.clear(Message.RENDER)
+        self.assertRaises(InvalidMessageOperation, self.r.make_response)
+
     def test_add_rrset(self):
         self.assertRaises(TypeError, self.r.add_rrset, "wrong")
         self.assertRaises(TypeError, self.r.add_rrset)



More information about the bind10-changes mailing list