Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ifduyue committed Jan 28, 2015
1 parent 709f0ee commit f063eef
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,50 @@
import os
import binascii

import unittest
import random
import xxtea
import os


class TestXXTEA(unittest.TestCase):

def test_hex(self):
self.assertEqual(xxtea.encrypt_hex('How do you do?', 'Fine. And you? '), b'78f465eb1b4985887d11842ede853621')
self.assertEqual(xxtea.decrypt_hex('78f465eb1b4985887d11842ede853621', 'Fine. And you? '), b'How do you do?')
data = b'How do you do?'
key = b'Fine. And you? '
hexenc = b'78f465eb1b4985887d11842ede853621'
self.assertEqual(xxtea.encrypt_hex(data, key), hexenc)
self.assertEqual(xxtea.decrypt_hex(hexenc, key), data)

def test_raw(self):
encd = xxtea.encrypt('How do you do?', 'Fine. And you? ')
decd = xxtea.decrypt(encd, 'Fine. And you? ')
self.assertEqual(b'How do you do?', decd)
data = b'How do you do?'
key = b'Fine. And you? '
hexenc = b'78f465eb1b4985887d11842ede853621'
enc = binascii.a2b_hex(hexenc)

self.assertEqual(xxtea.encrypt(data, key), enc)
self.assertEqual(xxtea.decrypt(enc, key), data)

def test_urandom(self):
for i in range(2048):
key = os.urandom(16)

data = os.urandom(i)
encd = xxtea.encrypt(data, key)
decd = xxtea.decrypt(encd, key)
self.assertEqual(data, decd)

enc = xxtea.encrypt(data, key)
dec = xxtea.decrypt(enc, key)
self.assertEqual(data, dec)

data = b'\0' * i
encd = xxtea.encrypt(data, key)
decd = xxtea.decrypt(encd, key)
self.assertEqual(data, decd)
enc = xxtea.encrypt(data, key)
dec = xxtea.decrypt(enc, key)
self.assertEqual(data, dec)

def test_hex_encode(self):
for i in range(2048):
key = os.urandom(16)
data = os.urandom(i)

enc = xxtea.encrypt(data, key)
hexenc = xxtea.encrypt_hex(data, key)
self.assertEqual(binascii.b2a_hex(enc), hexenc)

if __name__ == '__main__':
unittest.main()

0 comments on commit f063eef

Please sign in to comment.