1
"""py-unit tests for GnuPG
5
Copyright (C) 2001 Frank J. Tobin, ftobin@neverending.org
9
This library is free software; you can redistribute it and/or
10
modify it under the terms of the GNU Lesser General Public
11
License as published by the Free Software Foundation; either
12
version 2.1 of the License, or (at your option) any later version.
14
This library is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
Lesser General Public License for more details.
19
You should have received a copy of the GNU Lesser General Public
20
License along with this library; if not, write to the Free Software
21
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
or see http://www.gnu.org/copyleft/lesser.html
31
sys.path.insert(0, "../src")
34
__author__ = "Frank J. Tobin, ftobin@neverending.org"
36
__revision__ = "$Id: GnuPGInterfacetest.py,v 1.1 2002/10/29 01:49:47 bescoto Exp $"
38
class BasicTest(unittest.TestCase):
39
"""an initializer superclass"""
41
def __init__(self, methodName=None):
42
self.gnupg = GnuPGInterface.GnuPG()
43
unittest.TestCase.__init__(self, methodName)
46
class GnuPGTests(BasicTest):
47
"""Tests for GnuPG class"""
49
def __init__(self, methodName=None):
50
BasicTest.__init__(self, methodName)
52
self.gnupg.passphrase = "Three blind mice"
53
self.gnupg.options.armor = 1
54
self.gnupg.options.meta_interactive = 0
55
self.gnupg.options.extra_args.append('--no-secmem-warning')
57
def do_create_fh_operation(self, args, input,
59
creations = ['stdin', 'stdout']
61
# Make sure we're getting the passphrase to GnuPG
63
assert passphrase != None or self.gnupg.passphrase != None, \
64
"No way to send the passphrase to GnuPG!"
66
# We'll handle the passphrase manually
67
if passphrase != None: creations.append('passphrase')
69
proc = self.gnupg.run( args, create_fhs=creations )
71
if passphrase != 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()
81
# Checking to make sure GnuPG exited successfully
87
def do_attach_fh_operation(self, args, stdin, stdout,
90
# Make sure we're getting the passphrase to GnuPG
92
assert passphrase != None or self.gnupg.passphrase != None, \
93
"No way to send the passphrase to GnuPG!"
96
# We'll handle the passphrase manually
97
if passphrase != None: handles.append('passphrase')
99
attachments = { 'stdin': stdin, 'stdout': stdout }
101
proc = self.gnupg.run( args, create_fhs=creations,
102
attach_fhs=attachments )
104
if passphrase != None:
105
proc.handles['passphrase'].write(passphrase)
106
proc.handles['passphrase'].close()
108
# Checking to make sure GnuPG exited successfully
112
def test_create_fhs_solely(self):
113
"""Do GnuPG operations using solely the create_fhs feature"""
114
plaintext = "Three blind mice"
116
ciphertext = self.do_create_fh_operation( ['--symmetric'],
119
decryption = self.do_create_fh_operation( ['--decrypt'],
121
self.gnupg.passphrase )
122
assert decryption == plaintext, \
123
"GnuPG decrypted output does not match original input"
126
def test_attach_fhs(self):
127
"""Do GnuPG operations using the attach_fhs feature"""
128
plaintext_source = '/etc/motd'
130
plainfile = open(plaintext_source)
131
temp1 = tempfile.TemporaryFile()
132
temp2 = tempfile.TemporaryFile()
134
self.do_attach_fh_operation( ['--symmetric'],
135
stdin=plainfile, stdout=temp1 )
139
self.do_attach_fh_operation( ['--decrypt'],
140
stdin=temp1, stdout=temp2 )
145
assert fh_cmp(plainfile, temp2), \
146
"GnuPG decrypted output does not match original input"
149
class OptionsTests(BasicTest):
150
"""Tests for Options class"""
152
def __init__(self, methodName=None):
153
BasicTest.__init__(self, methodName)
156
def reset_options(self):
157
self.gnupg.options = GnuPGInterface.Options()
159
def option_to_arg(self, option):
160
return '--' + option.replace('_', '-')
162
def test_boolean_args(self):
163
"""test Options boolean options that they generate
166
booleans = [ 'armor', 'no_greeting', 'no_verbose',
167
'batch', 'always_trust', 'rfc1991',
168
'quiet', 'openpgp', 'force_v3_sigs',
169
'no_options', 'textmode' ]
171
for option in booleans:
173
setattr(self.gnupg.options, option, 1)
174
arg = self.option_to_arg(option)
177
result = self.gnupg.options.get_args()
179
assert should_be == result, \
180
"failure to set option '%s'; should be %s, but result is %s" \
181
% (option, should_be, result)
183
def test_string_args(self):
184
"""test Options string-taking options that they generate
187
strings = [ 'homedir', 'default_key', 'comment', 'compress_algo',
190
string_value = 'test-argument'
192
for option in strings:
194
setattr(self.gnupg.options, option, string_value)
195
arg = self.option_to_arg(option)
197
should_be = [arg, string_value]
198
result = self.gnupg.options.get_args()
200
assert should_be == result, \
201
"failure to set option '%s'; should be %s, but result is %s" \
202
% (option, should_be, result)
204
def test_list_args(self):
205
"""test Options string-taking options that they generate
208
lists = [ 'recipients', 'encrypt_to' ]
209
list_value = ['test1', 'test2']
213
setattr(self.gnupg.options, option, list_value)
215
# special case for recipients, since their
216
# respective argument is 'recipient', not 'recipients'
217
if option == 'recipients': arg = '--recipient'
218
else: arg = self.option_to_arg(option)
221
for v in list_value: should_be.extend([arg, v])
223
result = self.gnupg.options.get_args()
225
assert should_be == result, \
226
"failure to set option '%s'; should be %s, but result is %s" \
227
% (option, should_be, result)
230
class PipesTests(unittest.TestCase):
231
"""Tests for Pipes class"""
233
def test_constructor(self):
234
self.pipe = GnuPGInterface.Pipe(1, 2, 0)
235
assert self.pipe.parent == 1
236
assert self.pipe.child == 2
237
assert not self.pipe.direct
239
########################################################################
241
def fh_cmp(f1, f2, bufsize=8192):
243
b1 = f1.read(bufsize)
244
b2 = f2.read(bufsize)
245
if b1 != b2: return 0
248
########################################################################
250
if __name__ == "__main__":