LCOV - code coverage report
Current view: top level - dns/python - tsigerror_python.cc (source / functions) Hit Total Coverage
Test: report.info Lines: 45 69 65.2 %
Date: 2012-05-15 Functions: 8 8 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 31 84 36.9 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
       2                 :            : //
       3                 :            : // Permission to use, copy, modify, and/or distribute this software for any
       4                 :            : // purpose with or without fee is hereby granted, provided that the above
       5                 :            : // copyright notice and this permission notice appear in all copies.
       6                 :            : //
       7                 :            : // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
       8                 :            : // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
       9                 :            : // AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
      10                 :            : // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
      11                 :            : // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
      12                 :            : // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
      13                 :            : // PERFORMANCE OF THIS SOFTWARE.
      14                 :            : 
      15                 :            : #include <Python.h>
      16                 :            : 
      17                 :            : #include <string>
      18                 :            : #include <stdexcept>
      19                 :            : 
      20                 :            : #include <util/python/pycppwrapper_util.h>
      21                 :            : 
      22                 :            : #include <dns/tsigerror.h>
      23                 :            : 
      24                 :            : #include "pydnspp_common.h"
      25                 :            : #include "rcode_python.h"
      26                 :            : #include "tsigerror_python.h"
      27                 :            : 
      28                 :            : using namespace std;
      29                 :            : using namespace isc::util::python;
      30                 :            : using namespace isc::dns;
      31                 :            : using namespace isc::dns::python;
      32                 :            : 
      33                 :            : // For each class, we need a struct, a helper functions (init, destroy,
      34                 :            : // and static wrappers around the methods we export), a list of methods,
      35                 :            : // and a type description
      36                 :            : 
      37                 :            : // Import pydoc text
      38                 :            : #include "tsigerror_python_inc.cc"
      39                 :            : 
      40                 :            : namespace {
      41                 :            : // The s_* Class simply covers one instantiation of the object
      42                 :            : class s_TSIGError : public PyObject {
      43                 :            : public:
      44                 :            :     s_TSIGError() : cppobj(NULL) {};
      45                 :            :     const TSIGError* cppobj;
      46                 :            : };
      47                 :            : 
      48                 :            : // Shortcut type which would be convenient for adding class variables safely.
      49                 :            : typedef CPPPyObjectContainer<s_TSIGError, TSIGError> TSIGErrorContainer;
      50                 :            : 
      51                 :            : //
      52                 :            : // We declare the functions here, the definitions are below
      53                 :            : // the type definition of the object, since both can use the other
      54                 :            : //
      55                 :            : 
      56                 :            : // General creation and destruction
      57                 :            : int TSIGError_init(s_TSIGError* self, PyObject* args);
      58                 :            : void TSIGError_destroy(s_TSIGError* self);
      59                 :            : 
      60                 :            : // These are the functions we export
      61                 :            : PyObject* TSIGError_getCode(const s_TSIGError* const self);
      62                 :            : PyObject* TSIGError_toText(const s_TSIGError* const self);
      63                 :            : PyObject* TSIGError_toRcode(const s_TSIGError* const self);
      64                 :            : PyObject* TSIGError_str(PyObject* self);
      65                 :            : PyObject* TSIGError_richcmp(const s_TSIGError* const self,
      66                 :            :                             const s_TSIGError* const other, int op);
      67                 :            : 
      68                 :            : // These are the functions we export
      69                 :            : // For a minimal support, we don't need them.
      70                 :            : 
      71                 :            : // This list contains the actual set of functions we have in
      72                 :            : // python. Each entry has
      73                 :            : // 1. Python method name
      74                 :            : // 2. Our static function here
      75                 :            : // 3. Argument type
      76                 :            : // 4. Documentation
      77                 :            : PyMethodDef TSIGError_methods[] = {
      78                 :            :     { "get_code", reinterpret_cast<PyCFunction>(TSIGError_getCode),
      79                 :            :       METH_NOARGS,
      80                 :            :       TSIGError_getCode_doc },
      81                 :            :     { "to_text", reinterpret_cast<PyCFunction>(TSIGError_toText), METH_NOARGS,
      82                 :            :       TSIGError_toText_doc },
      83                 :            :     { "to_rcode", reinterpret_cast<PyCFunction>(TSIGError_toRcode),
      84                 :            :       METH_NOARGS,
      85                 :            :       TSIGError_toRcode_doc },
      86                 :            :     { NULL, NULL, 0, NULL }
      87                 :            : };
      88                 :            : 
      89                 :            : int
      90                 :         26 : TSIGError_init(s_TSIGError* self, PyObject* args) {
      91                 :            :     try {
      92                 :            :         // Constructor from the code value
      93                 :         26 :         long code = 0;
      94 [ +  - ][ +  + ]:         26 :         if (PyArg_ParseTuple(args, "l", &code)) {
      95         [ +  + ]:         14 :             if (code < 0 || code > 0xffff) {
      96         [ +  - ]:          2 :                 PyErr_SetString(PyExc_ValueError, "TSIG error out of range");
      97                 :            :                 return (-1);
      98                 :            :             }
      99         [ +  - ]:         12 :             self->cppobj = new TSIGError(code);
     100                 :         12 :             return (0);
     101                 :            :         }
     102                 :            : 
     103                 :            :         // Constructor from Rcode
     104         [ +  - ]:         12 :         PyErr_Clear();
     105                 :            :         PyObject* py_rcode;
     106 [ +  - ][ +  + ]:         12 :         if (PyArg_ParseTuple(args, "O!", &rcode_type, &py_rcode)) {
     107 [ +  - ][ +  - ]:         11 :             self->cppobj = new TSIGError(PyRcode_ToRcode(py_rcode));
                 [ +  + ]
     108                 :         10 :             return (0);
     109                 :            :         }
     110                 :          2 :     } catch (const isc::OutOfRange& ex) {
     111                 :            :         const string ex_what = "Failed to construct TSIGError object: " +
     112 [ -  + ][ -  + ]:          2 :             string(ex.what());
     113         [ -  + ]:          1 :         PyErr_SetString(PyExc_ValueError, ex_what.c_str());
     114                 :            :         return (-1);
     115                 :          0 :     } catch (const exception& ex) {
     116                 :            :         const string ex_what = "Failed to construct TSIGError object: " +
     117 [ #  # ][ #  # ]:          0 :             string(ex.what());
     118         [ #  # ]:          0 :         PyErr_SetString(po_IscException, ex_what.c_str());
     119                 :            :         return (-1);
     120      [ +  -  - ]:          1 :     } catch (...) {
     121                 :            :         PyErr_SetString(po_IscException,
     122         [ #  # ]:          0 :                         "Unexpected exception in constructing TSIGError");
     123                 :            :         return (-1);
     124                 :            :     }
     125                 :            : 
     126                 :            :     PyErr_SetString(PyExc_TypeError,
     127                 :          1 :                     "Invalid arguments to TSIGError constructor");
     128                 :            : 
     129                 :         26 :     return (-1);
     130                 :            : }
     131                 :            : 
     132                 :            : void
     133                 :        124 : TSIGError_destroy(s_TSIGError* const self) {
     134                 :        124 :     delete self->cppobj;
     135                 :        124 :     self->cppobj = NULL;
     136                 :        124 :     Py_TYPE(self)->tp_free(self);
     137                 :        124 : }
     138                 :            : 
     139                 :            : PyObject*
     140                 :         14 : TSIGError_getCode(const s_TSIGError* const self) {
     141                 :         14 :     return (Py_BuildValue("I", self->cppobj->getCode()));
     142                 :            : }
     143                 :            : 
     144                 :            : PyObject*
     145                 :         19 : TSIGError_toText(const s_TSIGError* const self) {
     146                 :            :     try {
     147                 :            :         // toText() could throw, so we need to catch any exceptions below.
     148 [ +  - ][ +  - ]:         19 :         return (Py_BuildValue("s", self->cppobj->toText().c_str()));
     149                 :          0 :     } catch (const exception& ex) {
     150                 :            :         const string ex_what =
     151                 :            :             "Failed to convert TSIGError object to text: " +
     152 [ #  # ][ #  # ]:          0 :             string(ex.what());
     153         [ #  # ]:          0 :         PyErr_SetString(po_IscException, ex_what.c_str());
     154         [ #  # ]:          0 :     } catch (...) {
     155                 :            :         PyErr_SetString(PyExc_SystemError, "Unexpected failure in "
     156         [ #  # ]:          0 :                         "converting TSIGError object to text");
     157                 :            :     }
     158                 :            :     return (NULL);
     159                 :            : }
     160                 :            : 
     161                 :            : PyObject*
     162                 :         13 : TSIGError_str(PyObject* self) {
     163                 :            :     // Simply call the to_text method we already defined
     164                 :            :     return (PyObject_CallMethod(self, const_cast<char*>("to_text"),
     165                 :         13 :                                 const_cast<char*>("")));
     166                 :            : }
     167                 :            : 
     168                 :            : PyObject*
     169                 :          7 : TSIGError_toRcode(const s_TSIGError* const self) {
     170                 :            :     try {
     171 [ +  - ][ +  - ]:          7 :         return (createRcodeObject(self->cppobj->toRcode()));
     172                 :          0 :     } catch (const exception& ex) {
     173                 :            :         const string ex_what =
     174 [ #  # ][ #  # ]:          0 :             "Failed to convert TSIGError to Rcode: " + string(ex.what());
     175         [ #  # ]:          0 :         PyErr_SetString(po_IscException, ex_what.c_str());
     176         [ #  # ]:          0 :     } catch (...) {
     177                 :            :         PyErr_SetString(PyExc_SystemError, "Unexpected failure in "
     178         [ #  # ]:          0 :                         "converting TSIGError to Rcode");
     179                 :            :     }
     180                 :            :     return (NULL);
     181                 :            : }
     182                 :            : 
     183                 :            : PyObject*
     184                 :         99 : TSIGError_richcmp(const s_TSIGError* const self,
     185                 :            :                    const s_TSIGError* const other,
     186                 :            :                    const int op)
     187                 :            : {
     188                 :         99 :     bool c = false;
     189                 :            : 
     190                 :            :     // Check for null and if the types match. If different type,
     191                 :            :     // simply return False
     192 [ +  - ][ -  + ]:         99 :     if (other == NULL || (self->ob_type != other->ob_type)) {
     193                 :          0 :         Py_RETURN_FALSE;
     194                 :            :     }
     195                 :            : 
     196                 :            :     // Only equals and not equals here, unorderable type
     197   [ -  -  +  +  :         99 :     switch (op) {
                -  -  - ]
     198                 :            :     case Py_LT:
     199                 :          0 :         PyErr_SetString(PyExc_TypeError, "Unorderable type; TSIGError");
     200                 :          0 :         return (NULL);
     201                 :            :     case Py_LE:
     202                 :          0 :         PyErr_SetString(PyExc_TypeError, "Unorderable type; TSIGError");
     203                 :          0 :         return (NULL);
     204                 :            :     case Py_EQ:
     205                 :        118 :         c = (*self->cppobj == *other->cppobj);
     206                 :         59 :         break;
     207                 :            :     case Py_NE:
     208                 :         80 :         c = (*self->cppobj != *other->cppobj);
     209                 :         40 :         break;
     210                 :            :     case Py_GT:
     211                 :          0 :         PyErr_SetString(PyExc_TypeError, "Unorderable type; TSIGError");
     212                 :          0 :         return (NULL);
     213                 :            :     case Py_GE:
     214                 :          0 :         PyErr_SetString(PyExc_TypeError, "Unorderable type; TSIGError");
     215                 :          0 :         return (NULL);
     216                 :            :     }
     217         [ +  + ]:         99 :     if (c) {
     218                 :         78 :         Py_RETURN_TRUE;
     219                 :            :     } else {
     220                 :         99 :         Py_RETURN_FALSE;
     221                 :            :     }
     222                 :            : }
     223                 :            : } // end of unnamed namespace
     224                 :            : 
     225                 :            : namespace isc {
     226                 :            : namespace dns {
     227                 :            : namespace python {
     228                 :            : // This defines the complete type for reflection in python and
     229                 :            : // parsing of PyObject* to s_TSIGError
     230                 :            : // Most of the functions are not actually implemented and NULL here.
     231                 :            : PyTypeObject tsigerror_type = {
     232                 :            :     PyVarObject_HEAD_INIT(NULL, 0)
     233                 :            :     "pydnspp.TSIGError",
     234                 :            :     sizeof(s_TSIGError),                 // tp_basicsize
     235                 :            :     0,                                  // tp_itemsize
     236                 :            :     reinterpret_cast<destructor>(TSIGError_destroy),       // tp_dealloc
     237                 :            :     NULL,                               // tp_print
     238                 :            :     NULL,                               // tp_getattr
     239                 :            :     NULL,                               // tp_setattr
     240                 :            :     NULL,                               // tp_reserved
     241                 :            :     NULL,                               // tp_repr
     242                 :            :     NULL,                               // tp_as_number
     243                 :            :     NULL,                               // tp_as_sequence
     244                 :            :     NULL,                               // tp_as_mapping
     245                 :            :     NULL,                               // tp_hash
     246                 :            :     NULL,                               // tp_call
     247                 :            :     // THIS MAY HAVE TO BE CHANGED TO NULL:
     248                 :            :     TSIGError_str,                       // tp_str
     249                 :            :     NULL,                               // tp_getattro
     250                 :            :     NULL,                               // tp_setattro
     251                 :            :     NULL,                               // tp_as_buffer
     252                 :            :     Py_TPFLAGS_DEFAULT,                 // tp_flags
     253                 :            :     TSIGError_doc,
     254                 :            :     NULL,                               // tp_traverse
     255                 :            :     NULL,                               // tp_clear
     256                 :            :     // THIS MAY HAVE TO BE CHANGED TO NULL:
     257                 :            :     reinterpret_cast<richcmpfunc>(TSIGError_richcmp), // tp_richcompare
     258                 :            :     0,                                  // tp_weaklistoffset
     259                 :            :     NULL,                               // tp_iter
     260                 :            :     NULL,                               // tp_iternext
     261                 :            :     TSIGError_methods,                   // tp_methods
     262                 :            :     NULL,                               // tp_members
     263                 :            :     NULL,                               // tp_getset
     264                 :            :     NULL,                               // tp_base
     265                 :            :     NULL,                               // tp_dict
     266                 :            :     NULL,                               // tp_descr_get
     267                 :            :     NULL,                               // tp_descr_set
     268                 :            :     0,                                  // tp_dictoffset
     269                 :            :     reinterpret_cast<initproc>(TSIGError_init),            // tp_init
     270                 :            :     NULL,                               // tp_alloc
     271                 :            :     PyType_GenericNew,                  // tp_new
     272                 :            :     NULL,                               // tp_free
     273                 :            :     NULL,                               // tp_is_gc
     274                 :            :     NULL,                               // tp_bases
     275                 :            :     NULL,                               // tp_mro
     276                 :            :     NULL,                               // tp_cache
     277                 :            :     NULL,                               // tp_subclasses
     278                 :            :     NULL,                               // tp_weaklist
     279                 :            :     NULL,                               // tp_del
     280                 :            :     0                                   // tp_version_tag
     281                 :            : };
     282                 :            : 
     283                 :            : PyObject*
     284                 :        649 : createTSIGErrorObject(const TSIGError& source) {
     285                 :        649 :     TSIGErrorContainer container(PyObject_New(s_TSIGError, &tsigerror_type));
     286 [ +  - ][ +  - ]:        649 :     container.set(new TSIGError(source));
     287                 :        649 :     return (container.release());
     288                 :            : }
     289                 :            : } // namespace python
     290                 :            : } // namespace dns
     291                 :          0 : } // namespace isc

Generated by: LCOV version 1.9