]> www.wagner.pp.ru Git - oss/ctypescrypto.git/commitdiff
Covered bio.py with tests
authorVictor Wagner <wagner@atlas-card.ru>
Thu, 5 Jun 2014 09:22:41 +0000 (13:22 +0400)
committerVictor Wagner <wagner@atlas-card.ru>
Thu, 5 Jun 2014 09:22:41 +0000 (13:22 +0400)
ctypescrypto/bio.py
tests/testbio.py [new file with mode: 0644]

index 1263ce64dc12a305924a0ce8dedb6a309b1d0a73..92de35c5dd27fe4eba15966b038e6aa24590d732 100644 (file)
@@ -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<len(data):
+                       raise IOError("Not all data were successfully written")
+
 #FIXME TODO - BIO should have stream-like interface
 libcrypto.BIO_s_mem.restype=c_void_p
 libcrypto.BIO_new.restype=c_void_p
 libcrypto.BIO_new.argtypes=(c_void_p,)
-libcrypto.BIO_get_mem_data.restype=c_long
-libcrypto.BIO_get_mem_data.argtypes=(c_void_p,POINTER(c_char_p))
+libcrypto.BIO_ctrl.restype=c_long
+libcrypto.BIO_ctrl.argtypes=(c_void_p,c_int,c_long,POINTER(c_char_p))
+libcrypto.BIO_read.argtypes=(c_void_p,c_char_p,c_int)
+libcrypto.BIO_write.argtypes=(c_void_p,c_char_p,c_int)
diff --git a/tests/testbio.py b/tests/testbio.py
new file mode 100644 (file)
index 0000000..d174a83
--- /dev/null
@@ -0,0 +1,88 @@
+from ctypescrypto.bio import Membio
+import unittest
+
+class TestRead(unittest.TestCase):
+       def test_readshortstr(self):
+               s="A quick brown fox jumps over a lazy dog"
+               bio=Membio(s)
+               data=bio.read()
+               del bio
+               self.assertEqual(data,s)
+       def test_readlongstr(self):
+               poem='''Eyes of grey--a sodden quay,
+Driving rain and falling tears,
+As the steamer wears to sea
+In a parting storm of cheers.
+
+Sing, for Faith and Hope are high--
+None so true as you and I--
+Sing the Lovers' Litany:
+"Love like ours can never die!"
+
+Eyes of black--a throbbing keel,
+Milky foam to left and right;
+Whispered converse near the wheel
+In the brilliant tropic night.
+
+Cross that rules the Southern Sky!
+Stars that sweep and wheel and fly,
+Hear the Lovers' Litany:
+Love like ours can never die!"
+
+Eyes of brown--a dusty plain
+Split and parched with heat of June,
+Flying hoof and tightened rein,
+Hearts that beat the old, old tune.
+
+Side by side the horses fly,
+Frame we now the old reply
+Of the Lovers' Litany:
+"Love like ours can never die!"
+
+Eyes of blue--the Simla Hills
+Silvered with the moonlight hoar;
+Pleading of the waltz that thrills,
+Dies and echoes round Benmore.
+
+"Mabel," "Officers," "Goodbye,"
+Glamour, wine, and witchery--
+On my soul's sincerity,
+"Love like ours can never die!"
+
+Maidens of your charity,
+Pity my most luckless state.
+Four times Cupid's debtor I--
+Bankrupt in quadruplicate.
+
+Yet, despite this evil case,
+And a maiden showed me grace,
+Four-and-forty times would I
+Sing the Lovers' Litany:
+"Love like ours can never die!"'''
+               bio=Membio(poem)
+               data=bio.read()
+               self.assertEqual(data,poem)
+               del bio
+       def test_readparts(self):
+               s="A quick brown fox jumps over the lazy dog"
+               bio=Membio(s)
+               a=bio.read(10)
+               self.assertEqual(a,s[0:10])
+               b=bio.read(9)
+               self.assertEqual(b,s[10:19])
+               c=bio.read()
+               self.assertEqual(c,s[19:])
+               d=bio.read()
+               self.assertEqual(d,"")
+
+class TestWrite(unittest.TestCase):
+       def test_write(self):
+               b=Membio()
+               b.write("A quick brown ")
+               b.write("fox jumps over ")
+               b.write("the lazy dog.")
+               self.assertEqual(str(b),"A quick brown fox jumps over the lazy dog.")
+
+
+if __name__ == '__main__':
+       unittest.main()