From e3bfb3cbff37b907742c1a959a1ac44ad2ccda8c Mon Sep 17 00:00:00 2001 From: Victor Wagner Date: Thu, 5 Jun 2014 13:22:41 +0400 Subject: [PATCH] Covered bio.py with tests --- ctypescrypto/bio.py | 46 +++++++++++++++++++++--- tests/testbio.py | 88 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 tests/testbio.py diff --git a/ctypescrypto/bio.py b/ctypescrypto/bio.py index 1263ce6..92de35c 100644 --- a/ctypescrypto/bio.py +++ b/ctypescrypto/bio.py @@ -1,5 +1,5 @@ from ctypescrypto import libcrypto -from ctypes import c_char_p, c_int, string_at +from ctypes import c_char_p, c_void_p, c_int, string_at, c_long,POINTER,byref, create_string_buffer class Membio: """ Provides interface to OpenSSL memory bios @@ -14,17 +14,53 @@ class Membio: method=libcrypto.BIO_s_mem() self.bio=libcrypto.BIO_new(method) else: - self.bio=libcrypto.BIO_new_mem_buf(c_char_p(data),len(data))q + self.bio=libcrypto.BIO_new_mem_buf(c_char_p(data),len(data)) def __del__(self): libcrypto.BIO_free(self.bio) del(self.bio) def __str__(self): p=c_char_p(None) - l=BIO_get_mem_data(self.bio,byref(p)) + l=libcrypto.BIO_ctrl(self.bio,3,0,byref(p)) return string_at(p,l) + def read(self,length=None): + if not length is None: + if type(length)!=type(0): + raise TypeError("length to read should be number") + buf=create_string_buffer(length) + readbytes=libcrypto.BIO_read(self.bio,buf,length) + if readbytes==-2: + raise NotImplementedError("Function is not supported by this BIO") + if readbytes==-1: + raise IOError + if readbytes==0: + return "" + return buf.raw[:readbytes] + else: + buf=create_string_buffer(1024) + out="" + r=1 + while r>0: + r=libcrypto.BIO_read(self.bio,buf,1024) + if r==-2: + raise NotImplementedError("Function is not supported by this BIO") + if r==-1: + raise IOError + if (r>0): + out+=buf.raw[:r] + return out + + def write(self,data): + r=libcrypto.BIO_write(self.bio,data,len(data)) + if r==-2: + raise NotImplementedError("Function not supported by this BIO") + if r