19
19
# along with duplicity; if not, write to the Free Software Foundation,
20
20
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
22
import sys, os, unittest, random
25
24
from duplicity import gpg
26
25
from duplicity import path
30
class GPGTest(unittest.TestCase):
26
from . import UnitTestCase
29
class GPGTest(UnitTestCase):
33
assert not os.system("tar xzf testfiles.tar.gz > /dev/null 2>&1")
32
super(GPGTest, self).setUp()
33
self.unpack_testfiles()
34
34
self.default_profile = gpg.GPGProfile(passphrase = "foobar")
37
assert not os.system("rm -rf testfiles tempdir temp2.tar")
40
"""Delete testfiles/output and recreate"""
41
assert not os.system("rm -rf testfiles/output")
42
assert not os.system("mkdir testfiles/output")
44
36
def gpg_cycle(self, s, profile = None):
45
37
"""Test encryption/decryption cycle on string s"""
47
38
epath = path.Path("testfiles/output/encrypted_file")
49
40
profile = self.default_profile
78
69
def test_gpg_asym(self):
79
70
"""Test GPG asymmetric encryption"""
80
profile = gpg.GPGProfile(passphrase = helper.sign_passphrase,
81
recipients = [helper.encrypt_key1,
71
profile = gpg.GPGProfile(passphrase = self.sign_passphrase,
72
recipients = [self.encrypt_key1,
83
74
self.gpg_cycle("aoensutha aonetuh saoe", profile)
85
profile2 = gpg.GPGProfile(passphrase = helper.sign_passphrase,
86
recipients = [helper.encrypt_key1])
76
profile2 = gpg.GPGProfile(passphrase = self.sign_passphrase,
77
recipients = [self.encrypt_key1])
87
78
self.gpg_cycle("aoeu" * 10000, profile2)
89
80
def test_gpg_hidden_asym(self):
90
81
"""Test GPG asymmetric encryption with hidden key id"""
91
profile = gpg.GPGProfile(passphrase = helper.sign_passphrase,
92
hidden_recipients = [helper.encrypt_key1,
82
profile = gpg.GPGProfile(passphrase = self.sign_passphrase,
83
hidden_recipients = [self.encrypt_key1,
94
85
self.gpg_cycle("aoensutha aonetuh saoe", profile)
96
profile2 = gpg.GPGProfile(passphrase = helper.sign_passphrase,
97
hidden_recipients = [helper.encrypt_key1])
87
profile2 = gpg.GPGProfile(passphrase = self.sign_passphrase,
88
hidden_recipients = [self.encrypt_key1])
98
89
self.gpg_cycle("aoeu" * 10000, profile2)
100
91
def test_gpg_signing(self):
101
92
"""Test to make sure GPG reports the proper signature key"""
103
93
plaintext = "hello" * 50000
105
signing_profile = gpg.GPGProfile(passphrase = helper.sign_passphrase,
106
sign_key = helper.sign_key,
107
recipients = [helper.encrypt_key1])
95
signing_profile = gpg.GPGProfile(passphrase = self.sign_passphrase,
96
sign_key = self.sign_key,
97
recipients = [self.encrypt_key1])
109
99
epath = path.Path("testfiles/output/encrypted_file")
110
100
encrypted_signed_file = gpg.GPGFile(1, epath, signing_profile)
115
105
assert decrypted_file.read() == plaintext
116
106
decrypted_file.close()
117
107
sig = decrypted_file.get_signature()
118
assert sig == helper.sign_key, sig
108
assert sig == self.sign_key, sig
120
110
def test_gpg_signing_and_hidden_encryption(self):
121
111
"""Test to make sure GPG reports the proper signature key even with hidden encryption key id"""
123
112
plaintext = "hello" * 50000
125
signing_profile = gpg.GPGProfile(passphrase = helper.sign_passphrase,
126
sign_key = helper.sign_key,
127
hidden_recipients = [helper.encrypt_key1])
114
signing_profile = gpg.GPGProfile(passphrase = self.sign_passphrase,
115
sign_key = self.sign_key,
116
hidden_recipients = [self.encrypt_key1])
129
118
epath = path.Path("testfiles/output/encrypted_file")
130
119
encrypted_signed_file = gpg.GPGFile(1, epath, signing_profile)
135
124
assert decrypted_file.read() == plaintext
136
125
decrypted_file.close()
137
126
sig = decrypted_file.get_signature()
138
assert sig == helper.sign_key, sig
127
assert sig == self.sign_key, sig
140
129
def test_GPGWriteFile(self):
141
130
"""Test GPGWriteFile"""
143
131
size = 400 * 1000
144
132
gwfh = GPGWriteFile_Helper()
145
133
profile = gpg.GPGProfile(passphrase = "foobar")
203
191
return "e" * random.randrange(0, 15000)
206
class SHATest(unittest.TestCase):
194
class SHATest(UnitTestCase):
207
195
"""Test making sha signatures"""
209
assert not os.system("tar xzf testfiles.tar.gz > /dev/null 2>&1")
212
assert not os.system("rm -rf testfiles tempdir temp2.tar")
197
super(SHATest, self).setUp()
198
self.unpack_testfiles()
214
200
def test_sha(self):
215
201
hash = gpg.get_hash("SHA1", path.Path("testfiles/various_file_types/regular_file"))