diff options
-rw-r--r-- | tests/alltests.py | 20 | ||||
-rw-r--r-- | tests/test_bio_file.py | 70 | ||||
-rw-r--r-- | tests/test_bio_iobuf.py | 92 | ||||
-rw-r--r-- | tests/test_bio_membuf.py | 66 |
4 files changed, 248 insertions, 0 deletions
diff --git a/tests/alltests.py b/tests/alltests.py new file mode 100644 index 0000000..78ad895 --- /dev/null +++ b/tests/alltests.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python2.0 + +RCS_id = '$Id: alltests.py,v 1.1 2000/11/08 14:39:45 ngps Exp $' + +import unittest + +def suite(): + modules_to_test = ('test_bio_membuf', + 'test_bio_file', + 'test_bio_iobuf') + alltests = unittest.TestSuite() + for module in map(__import__, modules_to_test): + alltests.addTest(module.suite()) + return alltests + +if __name__ == '__main__': + import sys + if not unittest.TextTestRunner().run(suite()).wasSuccessful(): + sys.exit(1) + diff --git a/tests/test_bio_file.py b/tests/test_bio_file.py new file mode 100644 index 0000000..28cad8b --- /dev/null +++ b/tests/test_bio_file.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python2.0 + +"""Unit tests for M2Crypto.BIO.File. + +Copyright (c) 2000 Ng Pheng Siong. All rights reserved.""" + +RCS_id='$Id: test_bio_file.py,v 1.1 2000/11/08 14:41:35 ngps Exp $' + +import unittest +import M2Crypto +from M2Crypto.BIO import File, openfile +import os + +class FileTestCase(unittest.TestCase): + + def setUp(self): + self.data = 'abcdef' * 64 + self.fname = os.tmpnam() + + def tearDown(self): + try: + os.unlink(self.fname) + except OSError: + pass + + def check_openfile_rb(self): + # First create the file using Python's open(). + f = open(self.fname, 'wb') + f.write(self.data) + f.close() + # Now open the file using M2Crypto.BIO.openfile(). + f = openfile(self.fname, 'rb') + data = f.read(len(self.data)) + assert data == self.data + + def check_openfile_wb(self): + # First create the file using M2Crypto.BIO.openfile(). + f = openfile(self.fname, 'wb') + f.write(self.data) + f.close() + # Now open the file using Python's open(). + f = open(self.fname, 'rb') + data = f.read(len(self.data)) + assert data == self.data + + def check_closed(self): + f = openfile(self.fname, 'wb') + f.write(self.data) + f.close() + self.assertRaises(IOError, f.write, self.data) + + def check_use_pyfile(self): + # First create the file. + f = open(self.fname, 'wb') + f2 = File(f) + f2.write(self.data) + f2.close() + # Now read the file. + f = open(self.fname, 'rb') + data = f.read(len(self.data)) + assert data == self.data + + +def suite(): + return unittest.makeSuite(FileTestCase, 'check_') + + +if __name__ == '__main__': + unittest.TextTestRunner().run(suite()) + diff --git a/tests/test_bio_iobuf.py b/tests/test_bio_iobuf.py new file mode 100644 index 0000000..451b966 --- /dev/null +++ b/tests/test_bio_iobuf.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python2.0 + +"""Unit tests for M2Crypto.BIO.IOBuffer. + +Copyright (c) 2000 Ng Pheng Siong. All rights reserved.""" + +RCS_id='$Id: test_bio_iobuf.py,v 1.1 2000/11/08 14:40:34 ngps Exp $' + +from cStringIO import StringIO + +import unittest +import M2Crypto +from M2Crypto.BIO import IOBuffer, MemoryBuffer + +class IOBufferTestCase(unittest.TestCase): + + def setUp(self): + self._data = 'abcdef\n' + self.data = self._data * 1024 + + def tearDown(self): + pass + + def check_init_empty(self): + mb = MemoryBuffer() + io = IOBuffer(mb._ptr()) + out = io.read() + assert out is None + + def check_init_something(self): + mb = MemoryBuffer(self.data) + io = IOBuffer(mb._ptr()) + out = io.read(len(self.data)) + assert out == self.data + + def check_read_less_than(self): + chunk = len(self.data) - 7 + mb = MemoryBuffer(self.data) + io = IOBuffer(mb._ptr()) + out = io.read(chunk) + assert out == self.data[:chunk] + + def check_read_more_than(self): + chunk = len(self.data) + 8 + mb = MemoryBuffer(self.data) + io = IOBuffer(mb._ptr()) + out = io.read(chunk) + assert out == self.data + + def check_readline(self): + buf = StringIO() + mb = MemoryBuffer(self.data) + io = IOBuffer(mb._ptr()) + while 1: + out = io.readline() + if not out: + break + buf.write(out) + assert out == self._data + assert buf.getvalue() == self.data + + def check_readlines(self): + buf = StringIO() + mb = MemoryBuffer(self.data) + io = IOBuffer(mb._ptr()) + lines = io.readlines() + for line in lines: + assert line == self._data + buf.write(line) + assert buf.getvalue() == self.data + + def check_closed(self): + mb = MemoryBuffer(self.data) + io = IOBuffer(mb._ptr()) + io.close() + self.assertRaises(IOError, io.write, self.data) + assert not io.readable() and not io.writeable() + + def check_read_only(self): + mb = MemoryBuffer(self.data) + io = IOBuffer(mb._ptr(), mode='r') + self.assertRaises(IOError, io.write, self.data) + assert not io.writeable() + + +def suite(): + return unittest.makeSuite(IOBufferTestCase, 'check_') + + +if __name__ == '__main__': + unittest.TextTestRunner().run(suite()) + diff --git a/tests/test_bio_membuf.py b/tests/test_bio_membuf.py new file mode 100644 index 0000000..9ae5fe7 --- /dev/null +++ b/tests/test_bio_membuf.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python2.0 + +"""Unit tests for M2Crypto.BIO.MemoryBuffer. + +Copyright (c) 2000 Ng Pheng Siong. All rights reserved.""" + +RCS_id='$Id: test_bio_membuf.py,v 1.1 2000/11/08 14:41:57 ngps Exp $' + +import unittest +import M2Crypto +from M2Crypto.BIO import MemoryBuffer + +class MemoryBufferTestCase(unittest.TestCase): + + def setUp(self): + self.data = 'abcdef' * 64 + + def tearDown(self): + pass + + def check_init_empty(self): + mb = MemoryBuffer() + assert len(mb) == 0 + out = mb.read() + assert out is None + + def check_init_something(self): + mb = MemoryBuffer(self.data) + assert len(mb) == len(self.data) + out = mb.read() + assert out == self.data + + def check_read_less_than(self): + chunk = len(self.data) - 7 + mb = MemoryBuffer(self.data) + out = mb.read(chunk) + assert out == self.data[:chunk] and len(mb) == (len(self.data) - chunk) + + def check_read_more_than(self): + chunk = len(self.data) + 8 + mb = MemoryBuffer(self.data) + out = mb.read(chunk) + assert out == self.data and len(mb) == 0 + + def check_write_close(self): + mb = MemoryBuffer(self.data) + assert mb.writeable() + mb.write_close() + assert mb.readable() + self.assertRaises(IOError, mb.write, self.data) + assert not mb.writeable() + + def check_closed(self): + mb = MemoryBuffer(self.data) + mb.close() + self.assertRaises(IOError, mb.write, self.data) + assert not mb.readable() and not mb.writeable() + + +def suite(): + return unittest.makeSuite(MemoryBufferTestCase, 'check_') + + +if __name__ == '__main__': + unittest.TextTestRunner().run(suite()) + |