Commit 5410f9ce authored by Mukund Sivaraman's avatar Mukund Sivaraman
Browse files

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

parent 000f7efd
......@@ -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,
......@@ -135,6 +138,18 @@ RRset_getRdataCount(PyObject* self, PyObject*) {
getRdataCount()));
}
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 {
......
......@@ -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())
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment