Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…bindings into develop

Overlapping changes to test script
  • Loading branch information
MelindaShore committed Nov 13, 2016
2 parents 346bd6e + f26b85c commit 5165207
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
45 changes: 45 additions & 0 deletions getdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ static void add_getdns_constants(PyObject *g);
static PyObject *ulabel_to_alabel(PyObject *self, PyObject *args, PyObject *keywds);
static PyObject *alabel_to_ulabel(PyObject *self, PyObject *args, PyObject *keywds);
static PyObject *wire_to_dict(PyObject *self, PyObject *args, PyObject *keywds);
static PyObject *file_to_list(PyObject *self, PyObject *args, PyObject *keywds);



Expand All @@ -69,6 +70,8 @@ static struct PyMethodDef getdns_methods[] = {
METH_VARARGS|METH_KEYWORDS, "return alabel from ulabel" },
{ "wire_to_dict", (PyCFunction)wire_to_dict,
METH_VARARGS, "convert a wire format buffer to a Python dictionary" },
{ "file_to_list", (PyCFunction)file_to_list,
METH_VARARGS, "read a zone from a file and return a list of records" },
{ 0, 0, 0 }
};

Expand Down Expand Up @@ -365,6 +368,48 @@ wire_to_dict(PyObject *self, PyObject *args, PyObject *keywds)
}


static PyObject *
file_to_list(PyObject *self, PyObject *args, PyObject *keywds)
{
static char *kwlist[] = { "file",
"origin",
"default_ttl" };

PyObject *py_file;
char *origin;
uint32_t default_ttl;
FILE *fp;
PyObject *py_rrlist;
getdns_return_t ret;
getdns_list *rr_list;

if (!PyArg_ParseTupleAndKeywords(args, keywds, "OsI", kwlist,
&py_file, &origin, &default_ttl)) {
PyErr_SetString(getdns_error, GETDNS_RETURN_INVALID_PARAMETER_TEXT);
return NULL;
}
if (!PyFile_Check(py_file)) {
PyErr_SetString(getdns_error, "file argument must be file object");
return NULL;
}
if ((fp = PyFile_AsFile(py_file)) == NULL) {
PyErr_SetString(getdns_error, GETDNS_RETURN_GENERIC_ERROR_TEXT);
return NULL;
}
if ((ret = getdns_fp2rr_list(fp, &rr_list, origin,
(uint32_t)default_ttl)
) != GETDNS_RETURN_GOOD) {
PyErr_SetString(getdns_error, getdns_get_errorstr_by_id(ret));
return NULL;
}
if ((py_rrlist = glist_to_plist(rr_list)) == NULL ) {
PyErr_SetString(getdns_error, GETDNS_RETURN_GENERIC_ERROR_TEXT);
return NULL;
}
return py_rrlist;
}


static PyObject *
get_errorstr_by_id(PyObject *self, PyObject *args, PyObject *keywds)
{
Expand Down
14 changes: 14 additions & 0 deletions test/example.com.zone
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
example.com. IN SOA ns1.example.com. hostmaster.example.com. (
2016111300
3600
900
1814400
3600 )

IN NS ns1.example.com.
IN NS ns2.example.com.

ns1 IN A 192.0.2.1
IN AAAA 2001:DB8::1
ns2 IN A 192.0.2.1
IN AAAA 2001:DB8::2
31 changes: 27 additions & 4 deletions test/getdns_test.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import unittest
import getdns
import inspect
import StringIO
import sys, platform
import unittest


un = platform.uname()
d = 'lib.' + un[0].lower() + '-' + un[4] + '-' + '.'.join(platform.python_version().split('.')[:2])
sys.path.append(d)

import getdns


class TestGetdnsMethods(unittest.TestCase):

def test_context(self):
c = getdns.Context()
self.assertIsNotNone(c)
Expand Down Expand Up @@ -76,6 +76,29 @@ def test_sync_general(self):
del(c)
del(r)

def test_file_to_list(self):
ns1 = {'class': 1,
'name': 'example.com.',
'rdata': {'nsdname': 'ns1.example.com.',
'rdata_raw':'ns1.example.com.'},
'ttl': 3600,
'type': 2
}
ns2 = {'class': 1,
'name': 'example.com.',
'rdata': {'nsdname': 'ns2.example.com.',
'rdata_raw': 'ns2.example.com.'},
'ttl': 3600,
'type': 2
}
f = open('example.com.zone')
r = getdns.file_to_list(f, 'example.com', 3600 )
self.assertIsInstance(r, list)
self.assertEqual(r[1], ns1)
self.assertEqual(r[2], ns2)
del(f)
del(r)


if __name__ == "__main__":
unittest.main(verbosity=2)

0 comments on commit 5165207

Please sign in to comment.