From 3ad13edad1f0da543f4e7b38aff62f7c91b58052 Mon Sep 17 00:00:00 2001 From: "Sybren A. St?vel" Date: Sun, 31 Jul 2011 20:47:49 +0200 Subject: Made hashing efficient for large files --- tests/test_bigfile.py | 37 +++++++++++++++++ tests/test_blocks.py | 106 ------------------------------------------------- tests/test_varblock.py | 77 +++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 106 deletions(-) create mode 100644 tests/test_bigfile.py delete mode 100644 tests/test_blocks.py create mode 100644 tests/test_varblock.py (limited to 'tests') diff --git a/tests/test_bigfile.py b/tests/test_bigfile.py new file mode 100644 index 0000000..ffca5b0 --- /dev/null +++ b/tests/test_bigfile.py @@ -0,0 +1,37 @@ +'''Tests block operations.''' + +from StringIO import StringIO +import unittest + +import rsa +from rsa import bigfile, varblock + +class BigfileTest(unittest.TestCase): + + def test_encrypt_decrypt_bigfile(self): + + # Expected block size + 11 bytes padding + pub_key, priv_key = rsa.newkeys((6 + 11) * 8) + + # Encrypt the file + message = '123456Sybren' + infile = StringIO(message) + outfile = StringIO() + + bigfile.encrypt_bigfile(infile, outfile, pub_key) + + # Test + crypto = outfile.getvalue() + + cryptfile = StringIO(crypto) + clearfile = StringIO() + + bigfile.decrypt_bigfile(cryptfile, clearfile, priv_key) + self.assertEquals(clearfile.getvalue(), message) + + # We have 2x6 bytes in the message, so that should result in two + # bigfile. + cryptfile.seek(0) + varblocks = list(varblock.yield_varblocks(cryptfile)) + self.assertEqual(2, len(varblocks)) + diff --git a/tests/test_blocks.py b/tests/test_blocks.py deleted file mode 100644 index 22d6500..0000000 --- a/tests/test_blocks.py +++ /dev/null @@ -1,106 +0,0 @@ -'''Tests block operations.''' - -from StringIO import StringIO -import unittest - -import rsa -from rsa import blocks - -class VarintTest(unittest.TestCase): - - def test_read_varint(self): - - encoded = '\xac\x02crummy' - infile = StringIO(encoded) - - (decoded, read) = blocks.read_varint(infile) - - # Test the returned values - self.assertEqual(300, decoded) - self.assertEqual(2, read) - - # The rest of the file should be untouched - self.assertEqual('crummy', infile.read()) - - def test_read_zero(self): - - encoded = '\x00crummy' - infile = StringIO(encoded) - - (decoded, read) = blocks.read_varint(infile) - - # Test the returned values - self.assertEqual(0, decoded) - self.assertEqual(1, read) - - # The rest of the file should be untouched - self.assertEqual('crummy', infile.read()) - - def test_write_varint(self): - - expected = '\xac\x02' - outfile = StringIO() - - written = blocks.write_varint(outfile, 300) - - # Test the returned values - self.assertEqual(expected, outfile.getvalue()) - self.assertEqual(2, written) - - - def test_write_zero(self): - - outfile = StringIO() - written = blocks.write_varint(outfile, 0) - - # Test the returned values - self.assertEqual('\x00', outfile.getvalue()) - self.assertEqual(1, written) - - -class VarblockTest(unittest.TestCase): - - def test_yield_varblock(self): - infile = StringIO('\x01\x0512345\x06Sybren') - - varblocks = list(blocks.yield_varblocks(infile)) - self.assertEqual(['12345', 'Sybren'], varblocks) - -class FixedblockTest(unittest.TestCase): - - def test_yield_fixedblock(self): - - infile = StringIO('123456Sybren') - - fixedblocks = list(blocks.yield_fixedblocks(infile, 6)) - self.assertEqual(['123456', 'Sybren'], fixedblocks) - -class BigfileTest(unittest.TestCase): - - def test_encrypt_decrypt_bigfile(self): - - # Expected block size + 11 bytes padding - pub_key, priv_key = rsa.newkeys((6 + 11) * 8) - - # Encrypt the file - message = '123456Sybren' - infile = StringIO(message) - outfile = StringIO() - - blocks.encrypt_bigfile(infile, outfile, pub_key) - - # Test - crypto = outfile.getvalue() - - cryptfile = StringIO(crypto) - clearfile = StringIO() - - blocks.decrypt_bigfile(cryptfile, clearfile, priv_key) - self.assertEquals(clearfile.getvalue(), message) - - # We have 2x6 bytes in the message, so that should result in two - # blocks. - cryptfile.seek(0) - varblocks = list(blocks.yield_varblocks(cryptfile)) - self.assertEqual(2, len(varblocks)) - diff --git a/tests/test_varblock.py b/tests/test_varblock.py new file mode 100644 index 0000000..d8addb4 --- /dev/null +++ b/tests/test_varblock.py @@ -0,0 +1,77 @@ +'''Tests varblock operations.''' + +from StringIO import StringIO +import unittest + +import rsa +from rsa import varblock + +class VarintTest(unittest.TestCase): + + def test_read_varint(self): + + encoded = '\xac\x02crummy' + infile = StringIO(encoded) + + (decoded, read) = varblock.read_varint(infile) + + # Test the returned values + self.assertEqual(300, decoded) + self.assertEqual(2, read) + + # The rest of the file should be untouched + self.assertEqual('crummy', infile.read()) + + def test_read_zero(self): + + encoded = '\x00crummy' + infile = StringIO(encoded) + + (decoded, read) = varblock.read_varint(infile) + + # Test the returned values + self.assertEqual(0, decoded) + self.assertEqual(1, read) + + # The rest of the file should be untouched + self.assertEqual('crummy', infile.read()) + + def test_write_varint(self): + + expected = '\xac\x02' + outfile = StringIO() + + written = varblock.write_varint(outfile, 300) + + # Test the returned values + self.assertEqual(expected, outfile.getvalue()) + self.assertEqual(2, written) + + + def test_write_zero(self): + + outfile = StringIO() + written = varblock.write_varint(outfile, 0) + + # Test the returned values + self.assertEqual('\x00', outfile.getvalue()) + self.assertEqual(1, written) + + +class VarblockTest(unittest.TestCase): + + def test_yield_varblock(self): + infile = StringIO('\x01\x0512345\x06Sybren') + + varblocks = list(varblock.yield_varblocks(infile)) + self.assertEqual(['12345', 'Sybren'], varblocks) + +class FixedblockTest(unittest.TestCase): + + def test_yield_fixedblock(self): + + infile = StringIO('123456Sybren') + + fixedblocks = list(varblock.yield_fixedblocks(infile, 6)) + self.assertEqual(['123456', 'Sybren'], fixedblocks) + -- cgit v1.2.1