3
Copyright 2009 Slide, Inc.
15
# These values are built by running the seccure binary
16
# and assume the curve of DEFAULT_CURVE (currently p384)
18
DEFAULT_DATA = 'This message will be signed\n'
19
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/'
20
DEFAULT_PUBKEY = '#&M=6cSQ}m6C(hUz-7j@E=>oS#TL3F[F[a[q9S;RhMh+F#gP|Q6R}lhT_e7b'
21
DEFAULT_PRIVKEY = '!!![t{l5N^uZd=Bg(P#N|PH#IN8I0,Jq/PvdVNi^PxR,(5~p-o[^hPE#40.<|'
22
DEFAULT_PLAINTEXT = 'This is a very very secret message!\n'
25
class ECC_KeyGen_Tests(unittest.TestCase):
26
def test_GenerateBoth(self):
27
ecc = pyecc.ECC.generate()
28
print ('private', 'public', ecc._private, len(ecc._private),
29
ecc._public, len(ecc._public))
31
encrypted = ecc.encrypt(DEFAULT_PLAINTEXT)
32
assert encrypted, ('Failed to encrypt?', encrypted, ecc)
34
decrypted = ecc.decrypt(encrypted)
35
assert decrypted, ('Failed to decrypt?', decrypted, ecc)
37
assert decrypted == DEFAULT_PLAINTEXT, ('Decrypted wrong',
38
decrypted, DEFAULT_PLAINTEXT)
40
class ECC_Verify_Tests(unittest.TestCase):
42
super(ECC_Verify_Tests, self).setUp()
43
self.ecc = pyecc.ECC(public=DEFAULT_PUBKEY, private=DEFAULT_PRIVKEY)
45
def test_BasicVerification(self):
46
assert self.ecc.verify(DEFAULT_DATA, DEFAULT_SIG), ('Failed to verify signature',
47
DEFAULT_DATA, DEFAULT_SIG, DEFAULT_PUBKEY, DEFAULT_PRIVKEY)
49
def test_BadVerification(self):
50
assert self.ecc.verify(DEFAULT_DATA, "FAIL") == False , ('Verified on a bad sig',
51
DEFAULT_DATA, DEFAULT_SIG, DEFAULT_PUBKEY, DEFAULT_PRIVKEY)
53
class ECC_Sign_Tests(unittest.TestCase):
55
super(ECC_Sign_Tests, self).setUp()
56
self.ecc = pyecc.ECC(public=DEFAULT_PUBKEY, private=DEFAULT_PRIVKEY)
58
def test_BasicSign(self):
59
signature = self.ecc.sign(DEFAULT_DATA)
61
assert signature == DEFAULT_SIG, ('Failed to generate a legit signature',
62
DEFAULT_SIG, signature)
64
def test_SignNone(self):
65
signature = self.ecc.sign(None)
67
assert signature == None, ('Should have a None sig')
69
class ECC_Encrypt_Tests(unittest.TestCase):
71
super(ECC_Encrypt_Tests, self).setUp()
72
self.ecc = pyecc.ECC(public=DEFAULT_PUBKEY)
74
def test_BasicEncrypt(self):
75
encrypted = self.ecc.encrypt(DEFAULT_PLAINTEXT)
78
# Resetup self.ecc with a private key so I can decrypt
79
self.ecc = pyecc.ECC(public=DEFAULT_PUBKEY, private=DEFAULT_PRIVKEY)
80
decrypted = self.ecc.decrypt(encrypted)
81
assert decrypted == DEFAULT_PLAINTEXT
83
class ECC_Decrypt_Tests(unittest.TestCase):
85
super(ECC_Decrypt_Tests, self).setUp()
86
self.ecc = pyecc.ECC(public=DEFAULT_PUBKEY, private=DEFAULT_PRIVKEY)
88
def test_BasicDecrypt(self):
89
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"
90
decrypted = self.ecc.decrypt(encrypted)
91
assert decrypted == DEFAULT_PLAINTEXT
93
class ECC_Fail(unittest.TestCase):
95
super(ECC_Fail, self).setUp()
96
self.ecc = pyecc.ECC(public=DEFAULT_PUBKEY, private=DEFAULT_PRIVKEY)
98
def test_EncryptNullByte(self):
99
rc = self.ecc.encrypt('\x00')
100
assert rc, (rc, 'Fail')
102
def test_EmptyString(self):
103
self.failUnlessRaises(TypeError, self.ecc.encrypt, '')
105
class ECC_GC_Checks(unittest.TestCase):
107
super(ECC_GC_Checks, self).setUp()
108
self.ecc = pyecc.ECC(public=DEFAULT_PUBKEY, private=DEFAULT_PRIVKEY)
110
def test_Encrypts(self):
112
for i in xrange(LOOPS):
113
encrypted = self.ecc.encrypt(DEFAULT_PLAINTEXT)
116
l = len(gc.get_objects())
118
assert objects == l, (l, objects, 'Count is off')
121
if __name__ == '__main__':
123
items = copy.copy(locals())
124
for k, v in items.iteritems():
125
if isinstance(v, types.TypeType) and issubclass(v, unittest.TestCase):
126
suites.append(unittest.makeSuite(v))
128
runner = unittest.TextTestRunner()
129
if 'xml' in sys.argv:
131
runner = xmlrunner.XMLTestRunner(filename='Pyecc.pytests.xml')
133
results = runner.run(unittest.TestSuite(suites))