30
30
from duplicity import gpginterface
32
__author__ = "Frank J. Tobin, ftobin@neverending.org"
34
__revision__ = "$Id: GnuPGInterfacetest.py,v 1.11 2009/06/06 17:35:19 loafman Exp $"
32
__author__ = u"Frank J. Tobin, ftobin@neverending.org"
33
__version__ = u"0.2.2"
34
__revision__ = u"$Id: GnuPGInterfacetest.py,v 1.11 2009/06/06 17:35:19 loafman Exp $"
37
37
class BasicTest(unittest.TestCase):
38
"""an initializer superclass"""
38
u"""an initializer superclass"""
40
40
def __init__(self, methodName=None):
41
41
self.gnupg = gpginterface.GnuPG()
45
45
class GnuPGTests(BasicTest):
46
"""Tests for GnuPG class"""
46
u"""Tests for GnuPG class"""
48
48
def __init__(self, methodName=None):
49
49
BasicTest.__init__(self, methodName)
51
self.gnupg.passphrase = "Three blind mice"
51
self.gnupg.passphrase = u"Three blind mice"
52
52
self.gnupg.options.armor = 1
53
53
self.gnupg.options.meta_interactive = 0
54
self.gnupg.options.extra_args.append('--no-secmem-warning')
54
self.gnupg.options.extra_args.append(u'--no-secmem-warning')
56
56
def do_create_fh_operation(self, args, input,
58
creations = ['stdin', 'stdout']
58
creations = [u'stdin', u'stdout']
60
60
# Make sure we're getting the passphrase to GnuPG
62
62
assert passphrase is not None or self.gnupg.passphrase is not None, \
63
"No way to send the passphrase to GnuPG!"
63
u"No way to send the passphrase to GnuPG!"
65
65
# We'll handle the passphrase manually
66
66
if passphrase is not None:
67
creations.append('passphrase')
67
creations.append(u'passphrase')
69
69
proc = self.gnupg.run(args, create_fhs=creations)
71
71
if passphrase is not None:
72
proc.handles['passphrase'].write(passphrase)
73
proc.handles['passphrase'].close()
75
proc.handles['stdin'].write(input)
76
proc.handles['stdin'].close()
78
ciphertext = proc.handles['stdout'].read()
79
proc.handles['stdout'].close()
72
proc.handles[u'passphrase'].write(passphrase)
73
proc.handles[u'passphrase'].close()
75
proc.handles[u'stdin'].write(input)
76
proc.handles[u'stdin'].close()
78
ciphertext = proc.handles[u'stdout'].read()
79
proc.handles[u'stdout'].close()
81
81
# Checking to make sure GnuPG exited successfully
89
89
# Make sure we're getting the passphrase to GnuPG
91
91
assert passphrase is not None or self.gnupg.passphrase is not None, \
92
"No way to send the passphrase to GnuPG!"
92
u"No way to send the passphrase to GnuPG!"
95
95
# We'll handle the passphrase manually
96
96
if passphrase is not None:
97
proc.handles.append('passphrase') # @UndefinedVariable
97
proc.handles.append(u'passphrase') # @UndefinedVariable
99
attachments = {'stdin': stdin, 'stdout': stdout}
99
attachments = {u'stdin': stdin, u'stdout': stdout}
101
101
proc = self.gnupg.run(args, create_fhs=creations,
102
102
attach_fhs=attachments)
104
104
if passphrase is not None:
105
proc.handles['passphrase'].write(passphrase)
106
proc.handles['passphrase'].close()
105
proc.handles[u'passphrase'].write(passphrase)
106
proc.handles[u'passphrase'].close()
108
108
# Checking to make sure GnuPG exited successfully
111
111
def test_create_fhs_solely(self):
112
"""Do GnuPG operations using solely the create_fhs feature"""
113
plaintext = "Three blind mice"
112
u"""Do GnuPG operations using solely the create_fhs feature"""
113
plaintext = u"Three blind mice"
115
ciphertext = self.do_create_fh_operation(['--symmetric'],
115
ciphertext = self.do_create_fh_operation([u'--symmetric'],
118
decryption = self.do_create_fh_operation(['--decrypt'],
118
decryption = self.do_create_fh_operation([u'--decrypt'],
120
120
self.gnupg.passphrase)
121
121
assert decryption == plaintext, \
122
"GnuPG decrypted output does not match original input"
122
u"GnuPG decrypted output does not match original input"
124
124
def test_attach_fhs(self):
125
"""Do GnuPG operations using the attach_fhs feature"""
125
u"""Do GnuPG operations using the attach_fhs feature"""
126
126
plaintext_source = __file__
128
128
plainfile = open(plaintext_source)
129
129
temp1 = tempfile.TemporaryFile()
130
130
temp2 = tempfile.TemporaryFile()
132
self.do_attach_fh_operation(['--symmetric'],
132
self.do_attach_fh_operation([u'--symmetric'],
133
133
stdin=plainfile, stdout=temp1)
137
self.do_attach_fh_operation(['--decrypt'],
137
self.do_attach_fh_operation([u'--decrypt'],
138
138
stdin=temp1, stdout=temp2)
140
140
plainfile.seek(0)
143
143
assert fh_cmp(plainfile, temp2), \
144
"GnuPG decrypted output does not match original input"
144
u"GnuPG decrypted output does not match original input"
147
147
class OptionsTests(BasicTest):
148
"""Tests for Options class"""
148
u"""Tests for Options class"""
150
150
def __init__(self, methodName=None):
151
151
BasicTest.__init__(self, methodName)
155
155
self.gnupg.options = gpginterface.Options()
157
157
def option_to_arg(self, option):
158
return '--' + option.replace('_', '-')
158
return u'--' + option.replace(u'_', u'-')
160
160
def test_boolean_args(self):
161
"""test Options boolean options that they generate
161
u"""test Options boolean options that they generate
162
162
proper arguments"""
164
booleans = ['armor', 'no_greeting', 'no_verbose',
165
'batch', 'always_trust', 'rfc1991',
166
'quiet', 'openpgp', 'force_v3_sigs',
167
'no_options', 'textmode']
164
booleans = [u'armor', u'no_greeting', u'no_verbose',
165
u'batch', u'always_trust', u'rfc1991',
166
u'quiet', u'openpgp', u'force_v3_sigs',
167
u'no_options', u'textmode']
169
169
for option in booleans:
170
170
self.reset_options()
175
175
result = self.gnupg.options.get_args()
177
177
assert should_be == result, \
178
"failure to set option '%s'; should be %s, but result is %s" \
178
u"failure to set option '%s'; should be %s, but result is %s" \
179
179
% (option, should_be, result)
181
181
def test_string_args(self):
182
"""test Options string-taking options that they generate
182
u"""test Options string-taking options that they generate
183
183
proper arguments"""
185
strings = ['homedir', 'default_key', 'comment', 'compress_algo',
185
strings = [u'homedir', u'default_key', u'comment', u'compress_algo',
188
string_value = 'test-argument'
188
string_value = u'test-argument'
190
190
for option in strings:
191
191
self.reset_options()
196
196
result = self.gnupg.options.get_args()
198
198
assert should_be == result, \
199
"failure to set option '%s'; should be %s, but result is %s" \
199
u"failure to set option '%s'; should be %s, but result is %s" \
200
200
% (option, should_be, result)
202
202
def test_list_args(self):
203
"""test Options string-taking options that they generate
203
u"""test Options string-taking options that they generate
204
204
proper arguments"""
206
lists = ['recipients', 'encrypt_to']
207
list_value = ['test1', 'test2']
206
lists = [u'recipients', u'encrypt_to']
207
list_value = [u'test1', u'test2']
209
209
for option in lists:
210
210
self.reset_options()
224
224
result = self.gnupg.options.get_args()
226
226
assert should_be == result, \
227
"failure to set option '%s'; should be %s, but result is %s" \
227
u"failure to set option '%s'; should be %s, but result is %s" \
228
228
% (option, should_be, result)
231
231
class PipesTests(unittest.TestCase):
232
"""Tests for Pipes class"""
232
u"""Tests for Pipes class"""
234
234
def test_constructor(self):
235
235
self.pipe = gpginterface.Pipe(1, 2, 0)