~tdelaet/cimple/old-codebase

« back to all changes in this revision

Viewing changes to dependencies/PyECC/tests.py

  • Committer: Thomas Delaet
  • Date: 2010-01-07 14:14:32 UTC
  • Revision ID: thomas@cole-20100107141432-yhker27v3pmn62uo
first phase of rewrite with focus on storage

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
'''
3
 
    Copyright 2009 Slide, Inc.
4
 
 
5
 
'''
6
 
import copy
7
 
import gc
8
 
import sys
9
 
import types
10
 
import unittest
11
 
 
12
 
import pyecc
13
 
 
14
 
#
15
 
# These values are built by running the seccure binary 
16
 
# and assume the curve of DEFAULT_CURVE (currently p384)
17
 
#
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'
23
 
LOOPS = 100
24
 
 
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))
30
 
 
31
 
        encrypted = ecc.encrypt(DEFAULT_PLAINTEXT)
32
 
        assert encrypted, ('Failed to encrypt?', encrypted, ecc)
33
 
 
34
 
        decrypted = ecc.decrypt(encrypted)
35
 
        assert decrypted, ('Failed to decrypt?', decrypted, ecc)
36
 
        
37
 
        assert decrypted == DEFAULT_PLAINTEXT, ('Decrypted wrong',
38
 
            decrypted, DEFAULT_PLAINTEXT)
39
 
 
40
 
class ECC_Verify_Tests(unittest.TestCase):
41
 
    def setUp(self):
42
 
        super(ECC_Verify_Tests, self).setUp()
43
 
        self.ecc = pyecc.ECC(public=DEFAULT_PUBKEY, private=DEFAULT_PRIVKEY)
44
 
 
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)
48
 
 
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)
52
 
 
53
 
class ECC_Sign_Tests(unittest.TestCase):
54
 
    def setUp(self):
55
 
        super(ECC_Sign_Tests, self).setUp()
56
 
        self.ecc = pyecc.ECC(public=DEFAULT_PUBKEY, private=DEFAULT_PRIVKEY)
57
 
 
58
 
    def test_BasicSign(self):
59
 
        signature = self.ecc.sign(DEFAULT_DATA)
60
 
 
61
 
        assert signature == DEFAULT_SIG, ('Failed to generate a legit signature',
62
 
                DEFAULT_SIG, signature)
63
 
 
64
 
    def test_SignNone(self):
65
 
        signature = self.ecc.sign(None)
66
 
 
67
 
        assert signature == None, ('Should have a None sig')
68
 
 
69
 
class ECC_Encrypt_Tests(unittest.TestCase):
70
 
    def setUp(self):
71
 
        super(ECC_Encrypt_Tests, self).setUp()
72
 
        self.ecc = pyecc.ECC(public=DEFAULT_PUBKEY)
73
 
 
74
 
    def test_BasicEncrypt(self):
75
 
        encrypted = self.ecc.encrypt(DEFAULT_PLAINTEXT)
76
 
        assert encrypted
77
 
 
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
82
 
 
83
 
class ECC_Decrypt_Tests(unittest.TestCase):
84
 
    def setUp(self):
85
 
        super(ECC_Decrypt_Tests, self).setUp()
86
 
        self.ecc = pyecc.ECC(public=DEFAULT_PUBKEY, private=DEFAULT_PRIVKEY)
87
 
 
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
92
 
 
93
 
class ECC_Fail(unittest.TestCase):
94
 
    def setUp(self):
95
 
        super(ECC_Fail, self).setUp()
96
 
        self.ecc = pyecc.ECC(public=DEFAULT_PUBKEY, private=DEFAULT_PRIVKEY)
97
 
 
98
 
    def test_EncryptNullByte(self):
99
 
        rc = self.ecc.encrypt('\x00')
100
 
        assert rc, (rc, 'Fail')
101
 
 
102
 
    def test_EmptyString(self):
103
 
        self.failUnlessRaises(TypeError, self.ecc.encrypt, '')
104
 
 
105
 
class ECC_GC_Checks(unittest.TestCase):
106
 
    def setUp(self):
107
 
        super(ECC_GC_Checks, self).setUp()
108
 
        self.ecc = pyecc.ECC(public=DEFAULT_PUBKEY, private=DEFAULT_PRIVKEY)
109
 
 
110
 
    def test_Encrypts(self):
111
 
        objects = None
112
 
        for i in xrange(LOOPS):
113
 
            encrypted = self.ecc.encrypt(DEFAULT_PLAINTEXT)
114
 
            assert encrypted
115
 
 
116
 
            l = len(gc.get_objects())
117
 
            if objects:
118
 
                assert objects == l, (l, objects, 'Count is off')
119
 
            objects = l
120
 
 
121
 
if __name__ == '__main__':
122
 
    suites = []
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))
127
 
 
128
 
    runner = unittest.TextTestRunner()
129
 
    if 'xml' in sys.argv:
130
 
        import xmlrunner
131
 
        runner = xmlrunner.XMLTestRunner(filename='Pyecc.pytests.xml')
132
 
 
133
 
    results = runner.run(unittest.TestSuite(suites))