forked from rtyler/PyECC
-
Notifications
You must be signed in to change notification settings - Fork 6
/
tests.py
133 lines (104 loc) · 4.73 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
'''
Copyright 2009 Slide, Inc.
'''
import copy
import gc
import sys
import types
import unittest
import pyecc
#
# These values are built by running the seccure binary
# and assume the curve of DEFAULT_CURVE (currently p384)
#
DEFAULT_DATA = 'This message will be signed\n'
DEFAULT_SIG = '#cE/UfJ@]qte8w-ajzi%S%tO<?$?@QK_hTL&pk-ES1L~C9~4lpm+P7ZXu[mXTJ:%tdhQa:z~~q)BAw{.3dvt!ub+s?sXyxk;S%&+^P-~%}+G3G?Oj-nSDc/'
DEFAULT_PUBKEY = '#&M=6cSQ}m6C(hUz-7j@E=>oS#TL3F[F[a[q9S;RhMh+F#gP|Q6R}lhT_e7b'
DEFAULT_PRIVKEY = '!!![t{l5N^uZd=Bg(P#N|PH#IN8I0,Jq/PvdVNi^PxR,(5~p-o[^hPE#40.<|'
DEFAULT_PLAINTEXT = 'This is a very very secret message!\n'
LOOPS = 100
class ECC_KeyGen_Tests(unittest.TestCase):
def test_GenerateBoth(self):
ecc = pyecc.ECC.generate()
print ('private', 'public', ecc._private, len(ecc._private),
ecc._public, len(ecc._public))
encrypted = ecc.encrypt(DEFAULT_PLAINTEXT)
assert encrypted, ('Failed to encrypt?', encrypted, ecc)
decrypted = ecc.decrypt(encrypted)
assert decrypted, ('Failed to decrypt?', decrypted, ecc)
assert decrypted == DEFAULT_PLAINTEXT, ('Decrypted wrong',
decrypted, DEFAULT_PLAINTEXT)
class ECC_Verify_Tests(unittest.TestCase):
def setUp(self):
super(ECC_Verify_Tests, self).setUp()
self.ecc = pyecc.ECC(public=DEFAULT_PUBKEY, private=DEFAULT_PRIVKEY)
def test_BasicVerification(self):
assert self.ecc.verify(DEFAULT_DATA, DEFAULT_SIG), ('Failed to verify signature',
DEFAULT_DATA, DEFAULT_SIG, DEFAULT_PUBKEY, DEFAULT_PRIVKEY)
def test_BadVerification(self):
assert self.ecc.verify(DEFAULT_DATA, "FAIL") == False , ('Verified on a bad sig',
DEFAULT_DATA, DEFAULT_SIG, DEFAULT_PUBKEY, DEFAULT_PRIVKEY)
class ECC_Sign_Tests(unittest.TestCase):
def setUp(self):
super(ECC_Sign_Tests, self).setUp()
self.ecc = pyecc.ECC(public=DEFAULT_PUBKEY, private=DEFAULT_PRIVKEY)
def test_BasicSign(self):
signature = self.ecc.sign(DEFAULT_DATA)
assert signature == DEFAULT_SIG, ('Failed to generate a legit signature',
DEFAULT_SIG, signature)
def test_SignNone(self):
signature = self.ecc.sign(None)
assert signature == None, ('Should have a None sig')
class ECC_Encrypt_Tests(unittest.TestCase):
def setUp(self):
super(ECC_Encrypt_Tests, self).setUp()
self.ecc = pyecc.ECC(public=DEFAULT_PUBKEY)
def test_BasicEncrypt(self):
encrypted = self.ecc.encrypt(DEFAULT_PLAINTEXT)
assert encrypted
# Resetup self.ecc with a private key so I can decrypt
self.ecc = pyecc.ECC(public=DEFAULT_PUBKEY, private=DEFAULT_PRIVKEY)
decrypted = self.ecc.decrypt(encrypted)
assert decrypted == DEFAULT_PLAINTEXT
class ECC_Decrypt_Tests(unittest.TestCase):
def setUp(self):
super(ECC_Decrypt_Tests, self).setUp()
self.ecc = pyecc.ECC(public=DEFAULT_PUBKEY, private=DEFAULT_PRIVKEY)
def test_BasicDecrypt(self):
encrypted = "\x01\xa9\xc0\x1a\x03\\h\xd8\xea,\x8f\xd6\x91W\x8d\xe74x:\x1d\xa8 \xee\x0eD\xfe\xb6\xb0P\x04\xbf\xd5=\xf1?\x00\x9cDw\xae\x0b\xc3\x05BuX\xf1\x9a\x05f\x81\xd1\x15\x8c\x80Q\xa6\xf9\xd7\xf0\x8e\x99\xf2\x11<t\xff\x92\x14\x1c%0W\x8e\x8f\n\n\x9ed\xf8\xff\xc7p\r\x03\xbbw|\xb1h\xc9\xbd+\x02\x87"
decrypted = self.ecc.decrypt(encrypted)
assert decrypted == DEFAULT_PLAINTEXT
class ECC_Fail(unittest.TestCase):
def setUp(self):
super(ECC_Fail, self).setUp()
self.ecc = pyecc.ECC(public=DEFAULT_PUBKEY, private=DEFAULT_PRIVKEY)
def test_EncryptNullByte(self):
rc = self.ecc.encrypt('\x00')
assert rc, (rc, 'Fail')
def test_EmptyString(self):
self.failUnlessRaises(TypeError, self.ecc.encrypt, '')
class ECC_GC_Checks(unittest.TestCase):
def setUp(self):
super(ECC_GC_Checks, self).setUp()
self.ecc = pyecc.ECC(public=DEFAULT_PUBKEY, private=DEFAULT_PRIVKEY)
def test_Encrypts(self):
objects = None
for i in xrange(LOOPS):
encrypted = self.ecc.encrypt(DEFAULT_PLAINTEXT)
assert encrypted
l = len(gc.get_objects())
if objects:
assert objects == l, (l, objects, 'Count is off')
objects = l
if __name__ == '__main__':
suites = []
items = copy.copy(locals())
for k, v in items.iteritems():
if isinstance(v, types.TypeType) and issubclass(v, unittest.TestCase):
suites.append(unittest.makeSuite(v))
runner = unittest.TextTestRunner()
if 'xml' in sys.argv:
import xmlrunner
runner = xmlrunner.XMLTestRunner(filename='Pyecc.pytests.xml')
results = runner.run(unittest.TestSuite(suites))