[svn] commit: r3130 - in /branches/trac358/src/lib/dns: message.cc message.h python/message_python.cc python/tests/message_python_test.py tests/message_unittest.cc
BIND 10 source code commits
bind10-changes at lists.isc.org
Thu Oct 7 11:24:18 UTC 2010
Author: jinmei
Date: Thu Oct 7 11:24:18 2010
New Revision: 3130
Log:
a few more final changes, assuming no one has started review:
- made python tests for header flags more consistent with C++ tests
- tightened parameter validations against invalid header flags
- added more documentation for Message:get/setHeaderFlag()
Modified:
branches/trac358/src/lib/dns/message.cc
branches/trac358/src/lib/dns/message.h
branches/trac358/src/lib/dns/python/message_python.cc
branches/trac358/src/lib/dns/python/tests/message_python_test.py
branches/trac358/src/lib/dns/tests/message_unittest.cc
Modified: branches/trac358/src/lib/dns/message.cc
==============================================================================
--- branches/trac358/src/lib/dns/message.cc (original)
+++ branches/trac358/src/lib/dns/message.cc Thu Oct 7 11:24:18 2010
@@ -176,7 +176,7 @@
bool
Message::getHeaderFlag(const HeaderFlag flag) const {
- if ((flag & ~HEADERFLAG_MASK) != 0) {
+ if (flag == 0 || (flag & ~HEADERFLAG_MASK) != 0) {
isc_throw(InvalidParameter,
"Message::getHeaderFlag:: Invalid flag is specified: " <<
flag);
@@ -190,7 +190,7 @@
isc_throw(InvalidMessageOperation,
"setHeaderFlag performed in non-render mode");
}
- if ((flag & ~HEADERFLAG_MASK) != 0) {
+ if (flag == 0 || (flag & ~HEADERFLAG_MASK) != 0) {
isc_throw(InvalidParameter,
"Message::getHeaderFlag:: Invalid flag is specified: " <<
flag);
Modified: branches/trac358/src/lib/dns/message.h
==============================================================================
--- branches/trac358/src/lib/dns/message.h (original)
+++ branches/trac358/src/lib/dns/message.h Thu Oct 7 11:24:18 2010
@@ -264,14 +264,41 @@
public:
/// \brief Return whether the specified header flag bit is set in the
/// header section.
+ ///
+ /// This method is basically exception free, but if
+ /// \c flag is not a valid constant of the \c HeaderFlag type,
+ /// an exception of class \c InvalidParameter will be thrown.
+ ///
+ /// \param flag The header flag constant to test.
+ /// \return \c true if the specified flag is set; otherwise \c false.
bool getHeaderFlag(const HeaderFlag flag) const;
/// \brief Set or clear the specified header flag bit in the header
/// section.
///
- /// This method is only allowed in the \c RENDER mode;
- /// if the \c Message is in other mode, an exception of class
- /// InvalidMessageOperation will be thrown.
+ /// The optional parameter \c on indicates the operation mode,
+ /// set or clear; if it's \c true the corresponding flag will be set;
+ /// otherwise the flag will be cleared.
+ /// In either case the original state of the flag does not affect the
+ /// operation; for example, if a flag is already set and the "set"
+ /// operation is attempted, it effectively results in no operation.
+ ///
+ /// The parameter \c on can be omitted, in which case a value of \c true
+ /// (i.e., set operation) will be assumed.
+ /// This is based on the observation that the flag would have to be set
+ /// in the vast majority of the cases where an application needs to
+ /// use this method.
+ ///
+ /// This method is only allowed in the \c RENDER mode;
+ /// if the \c Message is in other mode, an exception of class
+ /// InvalidMessageOperation will be thrown.
+ ///
+ /// If \c flag is not a valid constant of the \c HeaderFlag type,
+ /// an exception of class \c InvalidParameter will be thrown.
+ ///
+ /// \param flag The header flag constant to set or clear.
+ /// \param on If \c true the flag will be set; otherwise the flag will be
+ /// cleared.
void setHeaderFlag(const HeaderFlag flag, const bool on = true);
/// \brief Return the query ID given in the header section of the message.
Modified: branches/trac358/src/lib/dns/python/message_python.cc
==============================================================================
--- branches/trac358/src/lib/dns/python/message_python.cc (original)
+++ branches/trac358/src/lib/dns/python/message_python.cc Thu Oct 7 11:24:18 2010
@@ -276,13 +276,17 @@
static PyObject*
Message_setHeaderFlag(s_Message* self, PyObject* args) {
- unsigned int messageflag;
+ int messageflag;
PyObject *on = Py_True;
- if (!PyArg_ParseTuple(args, "I|O!", &messageflag, &PyBool_Type, &on)) {
+ if (!PyArg_ParseTuple(args, "i|O!", &messageflag, &PyBool_Type, &on)) {
PyErr_Clear();
PyErr_SetString(PyExc_TypeError,
"no valid type in set_header_flag argument");
+ return (NULL);
+ }
+ if (messageflag < 0) {
+ PyErr_SetString(PyExc_TypeError, "invalid Message header flag");
return (NULL);
}
Modified: branches/trac358/src/lib/dns/python/tests/message_python_test.py
==============================================================================
--- branches/trac358/src/lib/dns/python/tests/message_python_test.py (original)
+++ branches/trac358/src/lib/dns/python/tests/message_python_test.py Thu Oct 7 11:24:18 2010
@@ -85,18 +85,36 @@
self.assertRaises(TypeError, Message, 3)
self.assertRaises(TypeError, Message, "wrong")
- def test_get_header_flag(self):
+ def test_header_flag(self): # set and get methods
self.assertRaises(TypeError, self.p.get_header_flag, "wrong")
- self.assertFalse(self.p.get_header_flag(Message.HEADERFLAG_AA))
-
- def test_set_clear_header_flag(self):
self.assertRaises(TypeError, self.r.set_header_flag, "wrong")
+ self.assertFalse(self.r.get_header_flag(Message.HEADERFLAG_QR))
self.assertFalse(self.r.get_header_flag(Message.HEADERFLAG_AA))
- self.r.set_header_flag(Message.HEADERFLAG_AA)
+ self.assertFalse(self.r.get_header_flag(Message.HEADERFLAG_TC))
+ self.assertFalse(self.r.get_header_flag(Message.HEADERFLAG_RD))
+ self.assertFalse(self.r.get_header_flag(Message.HEADERFLAG_RA))
+ self.assertFalse(self.r.get_header_flag(Message.HEADERFLAG_AD))
+ self.assertFalse(self.r.get_header_flag(Message.HEADERFLAG_CD))
+
+ self.r.set_header_flag(Message.HEADERFLAG_QR)
+ self.assertTrue(self.r.get_header_flag(Message.HEADERFLAG_QR))
+
+ self.r.set_header_flag(Message.HEADERFLAG_AA, True)
self.assertTrue(self.r.get_header_flag(Message.HEADERFLAG_AA))
+
self.r.set_header_flag(Message.HEADERFLAG_AA, False)
self.assertFalse(self.r.get_header_flag(Message.HEADERFLAG_AA))
+
+ self.assertRaises(InvalidParameter, self.r.set_header_flag, 0)
+ self.assertRaises(InvalidParameter, self.r.set_header_flag, 0x7000)
+ self.assertRaises(InvalidParameter, self.r.set_header_flag, 0x0800)
+ self.assertRaises(InvalidParameter, self.r.set_header_flag, 0x10000)
+ self.assertRaises(TypeError, self.r.set_header_flag, 0x80000000)
+ # this would cause overflow and result in a "valid" flag
+ self.assertRaises(TypeError, self.r.set_header_flag,
+ Message.HEADERFLAG_AA | 0x100000000)
+ self.assertRaises(TypeError, self.r.set_header_flag, -1)
self.assertRaises(InvalidMessageOperation,
self.p.set_header_flag, Message.HEADERFLAG_AA)
Modified: branches/trac358/src/lib/dns/tests/message_unittest.cc
==============================================================================
--- branches/trac358/src/lib/dns/tests/message_unittest.cc (original)
+++ branches/trac358/src/lib/dns/tests/message_unittest.cc Thu Oct 7 11:24:18 2010
@@ -125,6 +125,8 @@
// Invalid flag values
EXPECT_THROW(message_render.setHeaderFlag(
+ static_cast<Message::HeaderFlag>(0)), InvalidParameter);
+ EXPECT_THROW(message_render.setHeaderFlag(
static_cast<Message::HeaderFlag>(0x7000)),
InvalidParameter);
EXPECT_THROW(message_render.setHeaderFlag(
More information about the bind10-changes
mailing list