1
from __future__ import generators
3
sys.path.insert(0, "../src")
4
import os, unittest, cStringIO, random
7
default_profile = gpg.GPGProfile(passphrase = "foobar")
9
class GPGTest(unittest.TestCase):
12
"""Delete testfiles/output and recreate"""
13
assert not os.system("rm -rf testfiles/output")
14
assert not os.system("mkdir testfiles/output")
16
def gpg_cycle(self, s, profile = None):
17
"""Test encryption/decryption cycle on string s"""
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()
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()
30
assert s == dec_buf, (len(s), len(dec_buf))
33
"""Test gpg short strings"""
34
self.gpg_cycle("hello, world")
35
self.gpg_cycle("ansoetuh aoetnuh aoenstuh aoetnuh asoetuh saoteuh ")
38
"""Test gpg long strings easily compressed"""
39
self.gpg_cycle(" " * 50000)
40
self.gpg_cycle("aoeu" * 1000000)
43
"""Test on random data - must have /dev/urandom device"""
44
infp = open("/dev/urandom", "rb")
45
rand_buf = infp.read(120000)
47
self.gpg_cycle(rand_buf)
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)
56
profile2 = gpg.GPGProfile(passphrase = "foobar",
57
recipients = ["duplicity_test@foo.edu"])
58
self.gpg_cycle("aoeu" * 10000, profile2)
60
def test_gpg_signing(self):
61
"""Test to make sure GPG reports the proper signature key"""
63
plaintext = "hello" * 50000
64
duplicity_keyid = "AA0E73D2"
66
signing_profile = gpg.GPGProfile(passphrase = "foobar",
67
sign_key = duplicity_keyid,
68
recipients = [duplicity_keyid])
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()
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
81
def test_GPGWriteFile(self):
82
"""Test GPGWriteFile"""
85
gwfh = GPGWriteFile_Helper()
86
profile = gpg.GPGProfile(passphrase = "foobar")
88
gpg.GPGWriteFile(gwfh, "testfiles/output/gpgwrite.gpg",
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
93
class GPGWriteHelper2:
94
def __init__(self, data): self.data = data
96
class GPGWriteFile_Helper:
97
"""Used in test_GPGWriteFile above"""
99
self.from_random_fp = open("/dev/urandom", "rb")
100
self.set_next_block()
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)
107
def peek(self): return self.next_block
110
result = self.next_block
111
self.set_next_block()
114
def get_footer(self):
115
return "e" * random.randrange(0, 15000)
118
class SHATest(unittest.TestCase):
119
"""Test making sha signatures"""
121
hash = gpg.get_hash("SHA1", path.Path("testfiles/various_file_types/regular_file"))
122
assert hash == "886d722999862724e1e62d0ac51c468ee336ef8e", hash
125
if __name__ == "__main__": unittest.main()