Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python2 support for pyhermes #91

Merged
merged 5 commits into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@

dependencies:
pre:
- sudo apt-get update && sudo DEBIAN_FRONTEND=noninteractive apt-get -y install libssl-dev build-essential
- sudo apt-get update && sudo DEBIAN_FRONTEND=noninteractive apt-get -y install libssl-dev build-essential python-dev python3-dev python-setuptools python3-setuptools
- git clone https://github.com/cossacklabs/themis && cd themis && sudo make install

override:
- make
- sudo make install
- sudo pip install tox
- pyenv global 2.7.12 3.3.6 3.4.4 3.5.3 3.6.2

test:
override:
- make test
- make test
# run only TestHermesTransport until add support of running stores
- cd pyhermes && tox -e py33,py34,py35,py36,py27 -- python -m unittest test.TestHermesTransport
2 changes: 2 additions & 0 deletions pyhermes/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include *.c
include *.h
26 changes: 10 additions & 16 deletions pyhermes/py_midhermes.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,24 @@ static int MidHermes_init(pyhermes_MidHermesObject *self, PyObject *args, PyObje

PyObject *transport = PyObject_CallMethod(credential_store_transport, "get_hermes_transport", NULL);
if (!HermesTransportWrapper_Check(transport)) {
//Py_DECREF(transport);
PyErr_SetString(PyExc_TypeError, "credential_store return transport with incorrect type");
return -1;
}
hm_rpc_transport_t *credential_hermes_transport = ((pyhermes_HermesTransportWrapperObject_t *) transport)->hermes_transport;
//Py_DECREF(transport);

transport = PyObject_CallMethod(key_store_transport, "get_hermes_transport", NULL);
if (HermesTransportWrapper_Check(transport) != 1) {
//Py_DECREF(transport);
PyErr_SetString(PyExc_TypeError, "credential_store return transport with incorrect type");
return -1;
}
hm_rpc_transport_t *key_hermes_transport = ((pyhermes_HermesTransportWrapperObject_t *) transport)->hermes_transport;
//Py_DECREF(transport);

transport = PyObject_CallMethod(data_store_transport, "get_hermes_transport", NULL);
if (HermesTransportWrapper_Check(transport) != 1) {
//Py_DECREF(transport);
PyErr_SetString(PyExc_TypeError, "credential_store return transport with incorrect type");
return -1;
}
hm_rpc_transport_t *data_hermes_transport = ((pyhermes_HermesTransportWrapperObject_t *) transport)->hermes_transport;
//Py_DECREF(transport);

if (!(credential_store_transport)
|| !(data_store_transport)
Expand Down Expand Up @@ -113,7 +107,7 @@ static PyObject *MidHermes_addBlock(pyhermes_MidHermesObject *self, PyObject *ar
size_t block_id_length = 0, block_length = 0, meta_length = 0;
static char *kwlist[] = {"id", "data", "meta", NULL};
if (!PyArg_ParseTupleAndKeywords(
args, kwds, "y#y#y#", kwlist, &block_id, &block_id_length, &block, &block_length, &meta, &meta_length)) {
args, kwds, "s#s#s#", kwlist, &block_id, &block_id_length, &block, &block_length, &meta, &meta_length)) {
PyErr_SetString(HermesError, "MidHermes.addBlock invalid parameters");
return NULL;
}
Expand All @@ -131,7 +125,7 @@ static PyObject *MidHermes_updBlock(pyhermes_MidHermesObject *self, PyObject *ar
size_t block_id_length = 0, block_length = 0, meta_length = 0;
static char *kwlist[] = {"id", "data", "meta", NULL};
if (!PyArg_ParseTupleAndKeywords(
args, kwds, "y#y#y#", kwlist, &block_id, &block_id_length, &block, &block_length, &meta, &meta_length)) {
args, kwds, "s#s#s#", kwlist, &block_id, &block_id_length, &block, &block_length, &meta, &meta_length)) {
PyErr_SetString(HermesError, "MidHermes.updBlock invalid parameters");
return NULL;
}
Expand All @@ -148,7 +142,7 @@ static PyObject *MidHermes_delBlock(pyhermes_MidHermesObject *self, PyObject *ar
const char *block_id = NULL;
size_t block_id_length = 0;
static char *kwlist[] = {"id", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "y#", kwlist, &block_id, &block_id_length)) {
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s#", kwlist, &block_id, &block_id_length)) {
PyErr_SetString(HermesError, "MidHermes.delBlock invalid parameters");
return NULL;
}
Expand All @@ -163,7 +157,7 @@ static PyObject *MidHermes_getBlock(pyhermes_MidHermesObject *self, PyObject *ar
const char *block_id = NULL, *block = NULL, *meta = NULL;
size_t block_id_length = 0, block_length = 0, meta_length = 0;
static char *kwlist[] = {"id", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "y#", kwlist, &block_id, &block_id_length)) {
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s#", kwlist, &block_id, &block_id_length)) {
PyErr_SetString(HermesError, "MidHermes.getBlock invalid parameters");
return NULL;
}
Expand All @@ -173,14 +167,14 @@ static PyObject *MidHermes_getBlock(pyhermes_MidHermesObject *self, PyObject *ar
PyErr_SetString(HermesError, "MidHermes.getBlock error");
return NULL;
}
return Py_BuildValue("y#y#", block, block_length, meta, meta_length);
return Py_BuildValue("OO", PyBytes_FromStringAndSize(block, block_length), PyBytes_FromStringAndSize(meta, meta_length));
}

static PyObject *MidHermes_rotateBlock(pyhermes_MidHermesObject *self, PyObject *args, PyObject *kwds) {
const char *block_id = NULL;
size_t block_id_length = 0;
static char *kwlist[] = {"id", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "y#", kwlist, &block_id, &block_id_length)) {
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s#", kwlist, &block_id, &block_id_length)) {
PyErr_SetString(HermesError, "MidHermes.rotateBlock invalid parameters");
return NULL;
}
Expand All @@ -196,7 +190,7 @@ static PyObject *MidHermes_grantReadAccess(pyhermes_MidHermesObject *self, PyObj
const char *block_id = NULL, *user_id = NULL;
size_t block_id_length = 0, user_id_length = 0;
static char *kwlist[] = {"id", "user", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "y#y#", kwlist, &block_id, &block_id_length, &user_id,
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s#s#", kwlist, &block_id, &block_id_length, &user_id,
&user_id_length)) {
PyErr_SetString(HermesError, "MidHermes.grantReadAccess invalid parameters");
return NULL;
Expand All @@ -213,7 +207,7 @@ static PyObject *MidHermes_grantUpdateAccess(pyhermes_MidHermesObject *self, PyO
const char *block_id = NULL, *user_id = NULL;
size_t block_id_length = 0, user_id_length = 0;
static char *kwlist[] = {"id", "user", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "y#y#", kwlist, &block_id, &block_id_length, &user_id,
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s#s#", kwlist, &block_id, &block_id_length, &user_id,
&user_id_length)) {
PyErr_SetString(HermesError, "MidHermes.grantUpdateAccess invalid parameters");
return NULL;
Expand All @@ -230,7 +224,7 @@ static PyObject *MidHermes_denyReadAccess(pyhermes_MidHermesObject *self, PyObje
const char *block_id = NULL, *user_id = NULL;
size_t block_id_length = 0, user_id_length = 0;
static char *kwlist[] = {"id", "user", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "y#y#", kwlist, &block_id, &block_id_length, &user_id,
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s#s#", kwlist, &block_id, &block_id_length, &user_id,
&user_id_length)) {
PyErr_SetString(HermesError, "MidHermes.denyReadAccess invalid parameters");
return NULL;
Expand All @@ -247,7 +241,7 @@ static PyObject *MidHermes_denyUpdateAccess(pyhermes_MidHermesObject *self, PyOb
const char *block_id = NULL, *user_id = NULL;
size_t block_id_length = 0, user_id_length = 0;
static char *kwlist[] = {"id", "user", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "y#y#", kwlist, &block_id, &block_id_length, &user_id,
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s#s#", kwlist, &block_id, &block_id_length, &user_id,
&user_id_length)) {
PyErr_SetString(HermesError, "MidHermes.denyUpdateAccess invalid parameters");
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion pyhermes/py_transport_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ PyTypeObject pyhermes_HermesTransportWrapperType = {
NULL,//(initproc)MidHermes_init, /* tp_init */
0, /* tp_alloc */
HermesTransportWrapper_new, /* tp_new */
};
};
8 changes: 3 additions & 5 deletions pyhermes/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
sources=['pyhermes.c', 'transport.c', 'py_secure_transport.c', 'py_midhermes.c', 'py_transport_wrapper.c',
'py_transport.c'
],
include_dirs=['../include'],
library_dirs=['../build'],
libraries=['hermes_mid_hermes', 'hermes_mid_hermes_ll', 'hermes_credential_store', 'hermes_data_store',
'hermes_key_store', 'hermes_rpc', 'hermes_common', 'themis', 'soter', 'hermes_secure_transport'])

Expand All @@ -42,14 +40,14 @@
classifiers=[
"Intended Audience :: Developers",
"Natural Language :: English",
#"Operating System :: MacOS :: MacOS X",
"Operating System :: MacOS :: MacOS X",
"Operating System :: POSIX",
#"Operating System :: POSIX :: BSD",
"Operating System :: POSIX :: Linux",
"Programming Language :: Python",
#"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2",
#"Programming Language :: Python :: 2.6",
#"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.2",
"Programming Language :: Python :: 3.3",
Expand Down
20 changes: 11 additions & 9 deletions pyhermes/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,23 @@
# coding: utf-8
import socket
import base64
import threading

try:
# Python3
from queue import Queue
except ImportError:
# Python2
from Queue import Queue

import threading
from unittest import TestCase, main

import pyhermes

QUEUE_TIMEOUT = THREAD_TIMEOUT = 2


class TCPTransport:
class TCPTransport(object):
def __init__(self, host, port):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((host, port))
Expand Down Expand Up @@ -63,7 +65,7 @@ def receive(self, needed_length):
return b''.join(output)


class BufferTransport:
class BufferTransport(object):
def __init__(self, buffer_in, buffer_out):
self.buffer_in = buffer_in
self.buffer_out = buffer_out
Expand Down Expand Up @@ -92,9 +94,9 @@ def create_transport(self, user_id, private, public_id, public, transport, is_se
def f():
hermes_transport = pyhermes.SecureHermesTransport(user_id, private, public_id, public, transport, is_server)
if is_server:
result.put(hermes_transport, True)
result.put((hermes_transport, True), timeout=QUEUE_TIMEOUT)
else:
result.put(hermes_transport, False)
result.put((hermes_transport, False), timeout=QUEUE_TIMEOUT)

return f

Expand Down Expand Up @@ -122,8 +124,8 @@ def test_secure_transport(self):
transportA, transportB = [result.get(timeout=QUEUE_TIMEOUT) for _ in range(2)]

def test_transport(self):
buffer_in = Queue(5)
buffer_out = Queue(5)
buffer_in = Queue()
buffer_out = Queue()
transportClient = BufferTransport(buffer_out, buffer_in)
transportServer = BufferTransport(buffer_in, buffer_out)
hermes_transport_server = pyhermes.HermesTransport(transportServer)
Expand Down Expand Up @@ -178,7 +180,7 @@ def test_secure_midhermes(self):
data_store_transport2 = pyhermes.SecureHermesTransport(
self.USER_ID1, self.PRIVATE_KEY1, self.DATA_STORE_ID, self.DATA_STORE_PUBLIC,
TCPTransport("127.0.0.1", 8890), False)
except (ConnectionRefusedError, pyhermes.HermesTransportError):
except (IOError, pyhermes.HermesTransportError):
self.skipTest("credential|key|data store service not started on 8888|8889|8890 port respectively")
return

Expand All @@ -195,7 +197,7 @@ def test_simple_transport_mid_hermes(self):
credential_store_transport2 = pyhermes.HermesTransport(TCPTransport("127.0.0.1", 8888))
key_store_transport2 = pyhermes.HermesTransport(TCPTransport("127.0.0.1", 8889))
data_store_transport2 = pyhermes.HermesTransport(TCPTransport("127.0.0.1", 8890))
except ConnectionRefusedError:
except IOError:
self.skipTest("credential|key|data store service not started on 8888|8889|8890 port respectively")
return
self._test_mid_hermes(credential_store_transport1, key_store_transport1, data_store_transport1,
Expand Down
4 changes: 4 additions & 0 deletions pyhermes/tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[tox]
envlist = py2,py3
[testenv]
commands={posargs:python -m unittest}
25 changes: 20 additions & 5 deletions pyhermes/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,29 @@


#include "transport.h"
#include <hermes/common/errors.h>


uint32_t transport_send(void *transport, const uint8_t *buf, const size_t buf_length) {
if (!transport || !buf || !buf_length) {
return HM_FAIL;
}
PyObject_CallMethod((PyObject *) transport, "send", "y#", (const char *) buf, buf_length);
PyObject* buf_object = PyBytes_FromStringAndSize((char*)buf, buf_length);
if (!buf_object){
return HM_FAIL;
}
PyObject* method_name = PyUnicode_FromString("send");
if(!method_name){
Py_DECREF(buf_object);
return HM_FAIL;
}

PyObject* object = (PyObject*) transport;
Py_INCREF(object);

PyObject_CallMethodObjArgs(object, method_name, buf_object, NULL);

Py_DECREF(object);
Py_DECREF(method_name);
Py_DECREF(buf_object);
return HM_SUCCESS;
}

Expand All @@ -40,7 +55,7 @@ uint32_t transport_recv(void *transport, uint8_t *buf, size_t buf_length) {
return HM_FAIL;
}
if (!(PyBytes_Check(result) || ((size_t) PyBytes_Size(result) != buf_length))) {
Py_XDECREF(result);
Py_DECREF(result);
return HM_FAIL;
}
memcpy(buf, PyBytes_AsString(result), buf_length);
Expand All @@ -64,7 +79,7 @@ uint32_t transport_destroy(hm_rpc_transport_t **transport) {
if (!transport || !(*transport) || !((*transport)->user_data)) {
return HM_FAIL;
}
Py_XDECREF((PyObject *) (*transport)->user_data);
Py_DECREF((PyObject *) (*transport)->user_data);
free(*transport);
*transport = NULL;
return HM_SUCCESS;
Expand Down
2 changes: 2 additions & 0 deletions pyhermes/transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
#define TRANSPORT_H

#include <Python.h>
#include "bytesobject.h"
#include <hermes/rpc/transport.h>
#include <hermes/common/errors.h>

extern PyObject *HermesTransportError;

Expand Down