~mgorse/duplicity/0.8-series

« back to all changes in this revision

Viewing changes to testing/unit/test_gpginterface.py

  • Committer: Kenneth Loafman
  • Date: 2018-07-27 02:18:12 UTC
  • Revision ID: kenneth@loafman.com-20180727021812-3yzv15gux0bnncds
* Checkpoint: Fixing unadorned strings for testing/unit/*.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""py-unit tests for GnuPG
 
1
u"""py-unit tests for GnuPG
2
2
 
3
3
COPYRIGHT:
4
4
 
29
29
 
30
30
from duplicity import gpginterface
31
31
 
32
 
__author__ = "Frank J. Tobin, ftobin@neverending.org"
33
 
__version__ = "0.2.2"
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 $"
35
35
 
36
36
 
37
37
class BasicTest(unittest.TestCase):
38
 
    """an initializer superclass"""
 
38
    u"""an initializer superclass"""
39
39
 
40
40
    def __init__(self, methodName=None):
41
41
        self.gnupg = gpginterface.GnuPG()
43
43
 
44
44
 
45
45
class GnuPGTests(BasicTest):
46
 
    """Tests for GnuPG class"""
 
46
    u"""Tests for GnuPG class"""
47
47
 
48
48
    def __init__(self, methodName=None):
49
49
        BasicTest.__init__(self, methodName)
50
50
 
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')
55
55
 
56
56
    def do_create_fh_operation(self, args, input,
57
57
                               passphrase=None):
58
 
        creations = ['stdin', 'stdout']
 
58
        creations = [u'stdin', u'stdout']
59
59
 
60
60
        # Make sure we're getting the passphrase to GnuPG
61
61
        # somehow!
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!"
64
64
 
65
65
        # We'll handle the passphrase manually
66
66
        if passphrase is not None:
67
 
            creations.append('passphrase')
 
67
            creations.append(u'passphrase')
68
68
 
69
69
        proc = self.gnupg.run(args, create_fhs=creations)
70
70
 
71
71
        if passphrase is not None:
72
 
            proc.handles['passphrase'].write(passphrase)
73
 
            proc.handles['passphrase'].close()
74
 
 
75
 
        proc.handles['stdin'].write(input)
76
 
        proc.handles['stdin'].close()
77
 
 
78
 
        ciphertext = proc.handles['stdout'].read()
79
 
        proc.handles['stdout'].close()
 
72
            proc.handles[u'passphrase'].write(passphrase)
 
73
            proc.handles[u'passphrase'].close()
 
74
 
 
75
        proc.handles[u'stdin'].write(input)
 
76
        proc.handles[u'stdin'].close()
 
77
 
 
78
        ciphertext = proc.handles[u'stdout'].read()
 
79
        proc.handles[u'stdout'].close()
80
80
 
81
81
        # Checking to make sure GnuPG exited successfully
82
82
        proc.wait()
89
89
        # Make sure we're getting the passphrase to GnuPG
90
90
        # somehow!
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!"
93
93
 
94
94
        creations = []
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
98
98
 
99
 
        attachments = {'stdin': stdin, 'stdout': stdout}
 
99
        attachments = {u'stdin': stdin, u'stdout': stdout}
100
100
 
101
101
        proc = self.gnupg.run(args, create_fhs=creations,
102
102
                              attach_fhs=attachments)
103
103
 
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()
107
107
 
108
108
        # Checking to make sure GnuPG exited successfully
109
109
        proc.wait()
110
110
 
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"
114
114
 
115
 
        ciphertext = self.do_create_fh_operation(['--symmetric'],
 
115
        ciphertext = self.do_create_fh_operation([u'--symmetric'],
116
116
                                                 plaintext)
117
117
 
118
 
        decryption = self.do_create_fh_operation(['--decrypt'],
 
118
        decryption = self.do_create_fh_operation([u'--decrypt'],
119
119
                                                 ciphertext,
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"
123
123
 
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__
127
127
 
128
128
        plainfile = open(plaintext_source)
129
129
        temp1 = tempfile.TemporaryFile()
130
130
        temp2 = tempfile.TemporaryFile()
131
131
 
132
 
        self.do_attach_fh_operation(['--symmetric'],
 
132
        self.do_attach_fh_operation([u'--symmetric'],
133
133
                                    stdin=plainfile, stdout=temp1)
134
134
 
135
135
        temp1.seek(0)
136
136
 
137
 
        self.do_attach_fh_operation(['--decrypt'],
 
137
        self.do_attach_fh_operation([u'--decrypt'],
138
138
                                    stdin=temp1, stdout=temp2)
139
139
 
140
140
        plainfile.seek(0)
141
141
        temp2.seek(0)
142
142
 
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"
145
145
 
146
146
 
147
147
class OptionsTests(BasicTest):
148
 
    """Tests for Options class"""
 
148
    u"""Tests for Options class"""
149
149
 
150
150
    def __init__(self, methodName=None):
151
151
        BasicTest.__init__(self, methodName)
155
155
        self.gnupg.options = gpginterface.Options()
156
156
 
157
157
    def option_to_arg(self, option):
158
 
        return '--' + option.replace('_', '-')
 
158
        return u'--' + option.replace(u'_', u'-')
159
159
 
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"""
163
163
 
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']
168
168
 
169
169
        for option in booleans:
170
170
            self.reset_options()
175
175
            result = self.gnupg.options.get_args()
176
176
 
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)
180
180
 
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"""
184
184
 
185
 
        strings = ['homedir', 'default_key', 'comment', 'compress_algo',
186
 
                   'options']
 
185
        strings = [u'homedir', u'default_key', u'comment', u'compress_algo',
 
186
                   u'options']
187
187
 
188
 
        string_value = 'test-argument'
 
188
        string_value = u'test-argument'
189
189
 
190
190
        for option in strings:
191
191
            self.reset_options()
196
196
            result = self.gnupg.options.get_args()
197
197
 
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)
201
201
 
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"""
205
205
 
206
 
        lists = ['recipients', 'encrypt_to']
207
 
        list_value = ['test1', 'test2']
 
206
        lists = [u'recipients', u'encrypt_to']
 
207
        list_value = [u'test1', u'test2']
208
208
 
209
209
        for option in lists:
210
210
            self.reset_options()
212
212
 
213
213
            # special case for recipients, since their
214
214
            # respective argument is 'recipient', not 'recipients'
215
 
            if option == 'recipients':
216
 
                arg = '--recipient'
 
215
            if option == u'recipients':
 
216
                arg = u'--recipient'
217
217
            else:
218
218
                arg = self.option_to_arg(option)
219
219
 
224
224
            result = self.gnupg.options.get_args()
225
225
 
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)
229
229
 
230
230
 
231
231
class PipesTests(unittest.TestCase):
232
 
    """Tests for Pipes class"""
 
232
    u"""Tests for Pipes class"""
233
233
 
234
234
    def test_constructor(self):
235
235
        self.pipe = gpginterface.Pipe(1, 2, 0)
251
251
 
252
252
########################################################################
253
253
 
254
 
if __name__ == "__main__":
 
254
if __name__ == u"__main__":
255
255
    unittest.main()