Browse Source

[1396] Add Python binding for RRset::getLength()

Mukund Sivaraman 11 years ago
parent
commit
5410f9ce9d
2 changed files with 33 additions and 0 deletions
  1. 15 0
      src/lib/dns/python/rrset_python.cc
  2. 18 0
      src/lib/dns/python/tests/rrset_python_test.py

+ 15 - 0
src/lib/dns/python/rrset_python.cc

@@ -53,6 +53,7 @@ int RRset_init(s_RRset* self, PyObject* args);
 void RRset_destroy(s_RRset* self);
 
 PyObject* RRset_getRdataCount(PyObject* self, PyObject* args);
+PyObject* RRset_getLength(PyObject* self, PyObject* args);
 PyObject* RRset_getName(PyObject* self, PyObject* args);
 PyObject* RRset_getClass(PyObject* self, PyObject* args);
 PyObject* RRset_getType(PyObject* self, PyObject* args);
@@ -70,6 +71,8 @@ PyObject* RRset_removeRRsig(PyObject* self, PyObject* args);
 PyMethodDef RRset_methods[] = {
     { "get_rdata_count", RRset_getRdataCount, METH_NOARGS,
       "Returns the number of rdata fields." },
+    { "get_length", RRset_getLength, METH_NOARGS,
+      "Returns the wire format length of the RRset." },
     { "get_name", RRset_getName, METH_NOARGS,
       "Returns the name of the RRset, as a Name object." },
     { "get_class", RRset_getClass, METH_NOARGS,
@@ -136,6 +139,18 @@ RRset_getRdataCount(PyObject* self, PyObject*) {
 }
 
 PyObject*
+RRset_getLength(PyObject* self, PyObject*) {
+    try {
+        return (Py_BuildValue("H", static_cast<const s_RRset*>(self)->cppobj->
+                              getLength()));
+    } catch (const EmptyRRset& ers) {
+        PyErr_Clear();
+        PyErr_SetString(po_EmptyRRset, ers.what());
+        return (NULL);
+    }
+}
+
+PyObject*
 RRset_getName(PyObject* self, PyObject*) {
     try {
         return (createNameObject(static_cast<const s_RRset*>(self)->cppobj->

+ 18 - 0
src/lib/dns/python/tests/rrset_python_test.py

@@ -45,6 +45,24 @@ class TestModuleSpec(unittest.TestCase):
             self.assertEqual(i, self.rrset_a_empty.get_rdata_count())
             self.rrset_a_empty.add_rdata(Rdata(RRType("A"), RRClass("IN"), "192.0.2.1"))
 
+    def test_get_length(self):
+        # Empty RRset should throw
+        self.assertRaises(EmptyRRset, self.rrset_a_empty.get_length);
+
+        # Unless it is type ANY or NONE:
+        # test.example.com = 1 + 4 + 1 + 7 + 1 + 3 + 1 = 18 octets
+        # TYPE field = 2 octets
+        # CLASS field = 2 octets
+        # TTL field = 2 octets
+        # RDLENGTH field = 2 octets
+        # Total = 18 + 2 + 2 + 2 + 2 = 26 octets
+        self.assertEqual(26, self.rrset_any_a_empty.get_length())
+
+        # Single A RR:
+        # 26 octets (above) + 4 octets (A RDATA) = 30 octets
+        # With 2 A RRs:
+        self.assertEqual(30 + 30, self.rrset_a.get_length())
+
     def test_get_name(self):
         self.assertEqual(self.test_name, self.rrset_a.get_name())
         self.assertEqual(self.test_domain, self.rrset_ns.get_name())