~ed.so/duplicity/reuse-passphrase-for-signing-fix

« back to all changes in this revision

Viewing changes to testing/gpgtest.py

  • Committer: bescoto
  • Date: 2002-10-29 01:49:46 UTC
  • Revision ID: vcs-imports@canonical.com-20021029014946-3m4rmm5plom7pl6q
Initial checkin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import generators
 
2
import sys
 
3
sys.path.insert(0, "../src")
 
4
import os, unittest, cStringIO, random
 
5
import gpg, path
 
6
 
 
7
default_profile = gpg.GPGProfile(passphrase = "foobar")
 
8
 
 
9
class GPGTest(unittest.TestCase):
 
10
        """Test GPGFile"""
 
11
        def deltmp(self):
 
12
                """Delete testfiles/output and recreate"""
 
13
                assert not os.system("rm -rf testfiles/output")
 
14
                assert not os.system("mkdir testfiles/output")
 
15
 
 
16
        def gpg_cycle(self, s, profile = None):
 
17
                """Test encryption/decryption cycle on string s"""
 
18
                self.deltmp()
 
19
                epath = path.Path("testfiles/output/encrypted_file")
 
20
                if not profile: profile = default_profile
 
21
                encrypted_file = gpg.GPGFile(1, epath, profile)
 
22
                encrypted_file.write(s)
 
23
                encrypted_file.close()
 
24
 
 
25
                epath2 = path.Path("testfiles/output/encrypted_file")
 
26
                decrypted_file = gpg.GPGFile(0, epath2, profile)
 
27
                dec_buf = decrypted_file.read()
 
28
                decrypted_file.close()
 
29
 
 
30
                assert s == dec_buf, (len(s), len(dec_buf))
 
31
 
 
32
        def test_gpg1(self):
 
33
                """Test gpg short strings"""
 
34
                self.gpg_cycle("hello, world")
 
35
                self.gpg_cycle("ansoetuh aoetnuh aoenstuh aoetnuh asoetuh saoteuh ")
 
36
 
 
37
        def test_gpg2(self):
 
38
                """Test gpg long strings easily compressed"""
 
39
                self.gpg_cycle(" " * 50000)
 
40
                self.gpg_cycle("aoeu" * 1000000)
 
41
 
 
42
        def test_gpg3(self):
 
43
                """Test on random data - must have /dev/urandom device"""
 
44
                infp = open("/dev/urandom", "rb")
 
45
                rand_buf = infp.read(120000)
 
46
                infp.close()
 
47
                self.gpg_cycle(rand_buf)
 
48
 
 
49
        def test_gpg_asym(self):
 
50
                """Test GPG asymmetric encryption"""
 
51
                profile = gpg.GPGProfile(passphrase = "foobar",
 
52
                                                                 recipients = ["mpf@stanford.edu",
 
53
                                                                                           "duplicity_test@foo.edu"])
 
54
                self.gpg_cycle("aoensutha aonetuh saoe", profile)
 
55
 
 
56
                profile2 = gpg.GPGProfile(passphrase = "foobar",
 
57
                                                                  recipients = ["duplicity_test@foo.edu"])
 
58
                self.gpg_cycle("aoeu" * 10000, profile2)
 
59
 
 
60
        def test_gpg_signing(self):
 
61
                """Test to make sure GPG reports the proper signature key"""
 
62
                self.deltmp()
 
63
                plaintext = "hello" * 50000
 
64
                duplicity_keyid = "AA0E73D2"
 
65
 
 
66
                signing_profile = gpg.GPGProfile(passphrase = "foobar",
 
67
                                                                                 sign_key = duplicity_keyid,
 
68
                                                                                 recipients = [duplicity_keyid])
 
69
 
 
70
                epath = path.Path("testfiles/output/encrypted_file")
 
71
                encrypted_signed_file = gpg.GPGFile(1, epath, signing_profile)
 
72
                encrypted_signed_file.write(plaintext)
 
73
                encrypted_signed_file.close()
 
74
 
 
75
                decrypted_file = gpg.GPGFile(0, epath, signing_profile)
 
76
                assert decrypted_file.read() == plaintext
 
77
                decrypted_file.close()
 
78
                sig = decrypted_file.get_signature()
 
79
                assert sig == duplicity_keyid, sig
 
80
 
 
81
        def test_GPGWriteFile(self):
 
82
                """Test GPGWriteFile"""
 
83
                self.deltmp()
 
84
                size = 250000
 
85
                gwfh = GPGWriteFile_Helper()
 
86
                profile = gpg.GPGProfile(passphrase = "foobar")
 
87
                for i in range(10):
 
88
                        gpg.GPGWriteFile(gwfh, "testfiles/output/gpgwrite.gpg",
 
89
                                                         profile, size = size)
 
90
                        #print os.stat("testfiles/output/gpgwrite.gpg").st_size - size
 
91
                        assert size - 32 * 1024 <= os.stat("testfiles/output/gpgwrite.gpg").st_size <= size + 32 * 1024
 
92
 
 
93
class GPGWriteHelper2:
 
94
        def __init__(self, data): self.data = data
 
95
 
 
96
class GPGWriteFile_Helper:
 
97
        """Used in test_GPGWriteFile above"""
 
98
        def __init__(self):
 
99
                self.from_random_fp = open("/dev/urandom", "rb")
 
100
                self.set_next_block()
 
101
 
 
102
        def set_next_block(self):
 
103
                self.next_block_length = random.randrange(0, 40000)
 
104
                block_data = self.from_random_fp.read(self.next_block_length)
 
105
                self.next_block = GPGWriteHelper2(block_data)
 
106
 
 
107
        def peek(self): return self.next_block
 
108
 
 
109
        def next(self):
 
110
                result = self.next_block
 
111
                self.set_next_block()
 
112
                return result
 
113
 
 
114
        def get_footer(self):
 
115
                return "e" * random.randrange(0, 15000)
 
116
 
 
117
 
 
118
class SHATest(unittest.TestCase):
 
119
        """Test making sha signatures"""
 
120
        def test_sha(self):
 
121
                hash = gpg.get_hash("SHA1", path.Path("testfiles/various_file_types/regular_file"))
 
122
                assert hash == "886d722999862724e1e62d0ac51c468ee336ef8e", hash
 
123
 
 
124
 
 
125
if __name__ == "__main__": unittest.main()