summaryrefslogtreecommitdiff
path: root/lib/thandy/tests.py
blob: e2fb767aea3ec72d5138aa2470dbbe29dc4e88e0 (plain)
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
# Copyright 2008 The Tor Project, Inc.  See LICENSE for licensing information.

import unittest
import doctest
import os
import tempfile

import thandy.keys
import thandy.formats
import thandy.repository
import thandy.checkJson

import thandy.tests

class CanonicalEncodingTest(unittest.TestCase):
    def test_encode(self):
        enc = thandy.formats.encodeCanonical
        self.assertEquals(enc(''), '""')
        self.assertEquals(enc('"'), '"\\""')
        self.assertEquals(enc('\t\\\n"\r'),
                          '"\t\\\\\n\\"\r"')

class CryptoTests(unittest.TestCase):
    def test_encrypt(self):
        s = "The Secret words are marzipan habidashery zeugma."
        password = "the password is swordfish."
        encrypted = thandy.keys.encryptSecret(s, password)
        self.assertNotEquals(encrypted, s)
        self.assert_(encrypted.startswith("GKEY1"))
        self.assertEquals(s, thandy.keys.decryptSecret(encrypted, password))
        self.assertRaises(thandy.BadPassword, thandy.keys.decryptSecret,
                          encrypted, "password")
        self.assertRaises(thandy.UnknownFormat, thandy.keys.decryptSecret,
                          "foobar", password)

    def test_keystore(self):
        passwd = "umfitty noonah"
        fname = tempfile.mktemp()
        ks = thandy.keys.KeyStore(fname)
        key1 = thandy.keys.RSAKey.generate(512)
        key2 = thandy.keys.RSAKey.generate(512)
        ks.addKey(key1)
        ks.addKey(key2)
        ks.save(passwd)

        ks2 = thandy.keys.KeyStore(fname)
        ks2.load(passwd)
        self.assertEquals(key1.key.n, ks2.getKey(key1.getKeyID()).key.n)

def suite():
    suite = unittest.TestSuite()

    suite.addTest(doctest.DocTestSuite(thandy.formats))
    suite.addTest(doctest.DocTestSuite(thandy.keys))
    suite.addTest(doctest.DocTestSuite(thandy.checkJson))

    loader = unittest.TestLoader()
    suite.addTest(loader.loadTestsFromModule(thandy.tests))

    return suite


if __name__ == '__main__':

    unittest.TextTestRunner(verbosity=1).run(suite())