~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_gpg.py

(jameinel) (bug #780544) when updating the WT,
 allow it to be done with a fast delta,
 rather than setting the state from scratch. (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
from bzrlib import errors, ui
24
24
import bzrlib.gpg as gpg
25
25
from bzrlib.tests import TestCase
26
 
from bzrlib.tests import features
27
26
 
28
27
class FakeConfig(object):
29
28
 
30
29
    def gpg_signing_command(self):
31
30
        return "false"
32
31
 
33
 
    def acceptable_keys(self):
34
 
        return None
35
 
 
36
32
 
37
33
class TestCommandLine(TestCase):
38
34
 
88
84
        self.assertRaises(errors.BzrBadParameterUnicode,
89
85
                          self.assertProduces, u'foo')
90
86
 
91
 
    def import_keys(self):
92
 
        from StringIO import StringIO
93
 
        import gpgme
94
 
        context = gpgme.Context()
95
 
 
96
 
        key = StringIO("""-----BEGIN PGP PUBLIC KEY BLOCK-----
97
 
Version: GnuPG v1.4.11 (GNU/Linux)
98
 
 
99
 
mQENBE343IgBCADwzPW7kmKb2bjB+UU+1ER/ABMZspvtoZMPusUw7bk6coXHF/0W
100
 
u1K/hSYeX9xaGOfOQw41r/g13MoR9dsL6L84RLiisf38rRoBZt+d5bCbZA5Xo801
101
 
2PeoBoGo6u5oOYKAFLMvrUitPiiE0IT/oQTfC4YUrLN4A+9W0QZruPGIpIXwmZXr
102
 
L0zsqYfNqIN0ompeJenVpKpvm3loJ/zfK7R3EJ3hsv6nkUmWCFsP1Pw3UV1YuCmw
103
 
Mkdn1U7DaOql1WjXgj9ABQDJrun2TGsqrSRzBODtHKA/uOX0K3VfKBU8VZo3dXUm
104
 
1Q4ZeZC39L9qJGTH8TQYlwBLe1yAOp+vx7QJABEBAAG0JEJhemFhciBUZXN0IEtl
105
 
eSA8YmF6YWFyQGV4YW1wbGUuY29tPokBOAQTAQIAIgUCTfjciAIbAwYLCQgHAwIG
106
 
FQgCCQoLBBYCAwECHgECF4AACgkQh2gbHu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Nbc+
122
 
boff+kIsoa/TAMLwtJoSrX1nXm0K3vZePRLnIgmwVzdkOIkaRJUG2tSQFvkfhvtE
123
 
LhnkL5l4MO0wrUds0UWRwa3d7j/P2ExrqXdlLmEzrifWyEQ=
124
 
=hUJn
125
 
-----END PGP PUBLIC KEY BLOCK-----
126
 
""")
127
 
 
128
 
        secret_key = StringIO("""-----BEGIN PGP PRIVATE KEY BLOCK-----
129
 
Version: GnuPG v1.4.11 (GNU/Linux)
130
 
 
131
 
lQOYBE343IgBCADwzPW7kmKb2bjB+UU+1ER/ABMZspvtoZMPusUw7bk6coXHF/0W
132
 
u1K/hSYeX9xaGOfOQw41r/g13MoR9dsL6L84RLiisf38rRoBZt+d5bCbZA5Xo801
133
 
2PeoBoGo6u5oOYKAFLMvrUitPiiE0IT/oQTfC4YUrLN4A+9W0QZruPGIpIXwmZXr
134
 
L0zsqYfNqIN0ompeJenVpKpvm3loJ/zfK7R3EJ3hsv6nkUmWCFsP1Pw3UV1YuCmw
135
 
Mkdn1U7DaOql1WjXgj9ABQDJrun2TGsqrSRzBODtHKA/uOX0K3VfKBU8VZo3dXUm
136
 
1Q4ZeZC39L9qJGTH8TQYlwBLe1yAOp+vx7QJABEBAAEAB/0RJTbV991SOtVfPQVu
137
 
LM+tD0SiOXJwIBIINlngsFHWVIiBSDb6uF8dneMR70IRnuEFHFyAUXA7PZDxvcSu
138
 
phAqIdKCWxQPkAULAS0o4U2K3ZFGh4uOqvfZ8eSnh1rETFv7Yf3u23K89cZiy99n
139
 
EtWgSqzC/2z5PaZ7/alsYCBqhHuyd4Phaud7qv7FTz8mFrCf+CCY+D08wbnZBu4g
140
 
N9tBwoxT/UKRfv3nghIh9v+3qWfBEFGhrYbt92XKFbHOQeATZz8AGIv1eqN/+ZQY
141
 
oYmvVfO3GkrWaRoPeJNLqSDEn/45O1Uh9MJ4mQclXqB0QzMShle8uusHxIeJSQsR
142
 
z//VBAD11WS7qSgCeiHR+4jDzrrlb2snnA2bfDToEomDxd/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rN2MQXJ
174
 
4vEEAJbXZOqgAcFAFBUVb5mVT0s2lJMagZFPdhRJz2bttz01s/B8aca6CrDpFRjT
175
 
03zRFUZjwDYqZDWBC181dCE9yla4OkWd5QyRKSS2EE02KEYqRzT0RngQn7s4AW2r
176
 
326up3Jhleln3hgD4Kk3V3KHmyK8zqZA0qWzry4Vl2jjkbnAPB2JAR8EGAECAAkF
177
 
Ak343IgCGwwACgkQh2gbHuMIDkWtHwgA2c4Xc9SPVNtFzBXpGhaYewVAes+YzyiB
178
 
eJrUuy+MTWIRagL6a68qHo7J1/3STpmyVLut0OenarNhjxb9OIOTE/5++xniYyLV
179
 
jQX7e3LWPNaFyycHZWslKJq/EReTS0o+s2/gRQZYbrAewL1MGPfsh/6eszKKb3hU
180
 
e8jR4eZfzIyU3g7cd+aAP0Jsl1TG54/d7fU4hi7CH/oXsYgjxM0RUdT/y+H+ZBwL
181
 
Gmk1tz5uh9/6Qiyhr9MAwvC0mhKtfWdebQre9l49EuciCbBXN2Q4iRpElQba1JAW
182
 
+R+G+0QuGeQvmXgw7TCtR2zRRZHBrd3uP8/YTGupd2UuYTOuJ9bIRA==
183
 
=LXn0
184
 
-----END PGP PRIVATE KEY BLOCK-----
185
 
""")
186
 
 
187
 
        context.import_(key)
188
 
        context.import_(secret_key)
189
 
 
190
 
    def test_verify_valid(self):
191
 
        self.requireFeature(features.gpgme)
192
 
        self.import_keys()
193
 
            
194
 
        content = """-----BEGIN PGP SIGNED MESSAGE-----
195
 
Hash: SHA1
196
 
 
197
 
bazaar-ng testament short form 1
198
 
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
199
 
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
200
 
-----BEGIN PGP SIGNATURE-----
201
 
Version: GnuPG v1.4.11 (GNU/Linux)
202
 
 
203
 
iQEcBAEBAgAGBQJN+ekFAAoJEIdoGx7jCA5FGtEH/i+XxJRvqU6wdBtLVrGBMAGk
204
 
FZ5VP+KyXYtymSbgSstj/vM12NeMIeFs3xGnNnYuX1MIcY6We5TKtCH0epY6ym5+
205
 
6g2Q2QpQ5/sT2d0mWzR0K4uVngmxVQaXTdk5PdZ40O7ULeDLW6CxzxMHyUL1rsIx
206
 
7UBUTBh1O/1n3ZfD99hUkm3hVcnsN90uTKH59zV9NWwArU0cug60+5eDKJhSJDbG
207
 
rIwlqbFAjDZ7L/48e+IaYIJwBZFzMBpJKdCxzALLtauMf+KK8hGiL2hrRbWm7ty6
208
 
NgxfkMYOB4rDPdSstT35N+5uBG3n/UzjxHssi0svMfVETYYX40y57dm2eZQXFp8=
209
 
=iwsn
210
 
-----END PGP SIGNATURE-----
211
 
"""
212
 
        plain = """bazaar-ng testament short form 1
213
 
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
214
 
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
215
 
"""
216
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
217
 
        my_gpg.set_acceptable_keys("bazaar@example.com")
218
 
        self.assertEqual((gpg.SIGNATURE_VALID, None), my_gpg.verify(content,
219
 
                            plain))
220
 
 
221
 
    def test_verify_bad_testament(self):
222
 
        self.requireFeature(features.gpgme)
223
 
        self.import_keys()
224
 
            
225
 
        content = """-----BEGIN PGP SIGNED MESSAGE-----
226
 
Hash: SHA1
227
 
 
228
 
bazaar-ng testament short form 1
229
 
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
230
 
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
231
 
-----BEGIN PGP SIGNATURE-----
232
 
Version: GnuPG v1.4.11 (GNU/Linux)
233
 
 
234
 
iQEcBAEBAgAGBQJN+ekFAAoJEIdoGx7jCA5FGtEH/i+XxJRvqU6wdBtLVrGBMAGk
235
 
FZ5VP+KyXYtymSbgSstj/vM12NeMIeFs3xGnNnYuX1MIcY6We5TKtCH0epY6ym5+
236
 
6g2Q2QpQ5/sT2d0mWzR0K4uVngmxVQaXTdk5PdZ40O7ULeDLW6CxzxMHyUL1rsIx
237
 
7UBUTBh1O/1n3ZfD99hUkm3hVcnsN90uTKH59zV9NWwArU0cug60+5eDKJhSJDbG
238
 
rIwlqbFAjDZ7L/48e+IaYIJwBZFzMBpJKdCxzALLtauMf+KK8hGiL2hrRbWm7ty6
239
 
NgxfkMYOB4rDPdSstT35N+5uBG3n/UzjxHssi0svMfVETYYX40y57dm2eZQXFp8=
240
 
=iwsn
241
 
-----END PGP SIGNATURE-----
242
 
"""
243
 
        plain = """bazaar-ng testament short form 1
244
 
revision-id: doctor@example.com-20110527185938-hluafawphszb8dl1
245
 
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
246
 
"""
247
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
248
 
        my_gpg.set_acceptable_keys("bazaar@example.com")
249
 
        self.assertEqual((gpg.SIGNATURE_NOT_VALID, None), my_gpg.verify(content,
250
 
                            plain))
251
 
 
252
 
    def test_verify_invalid(self):
253
 
        self.requireFeature(features.gpgme)
254
 
        content = """-----BEGIN PGP SIGNED MESSAGE-----
255
 
Hash: SHA1
256
 
 
257
 
bazaar-ng testament short form 1
258
 
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
259
 
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
260
 
-----BEGIN PGP SIGNATURE-----
261
 
Version: GnuPG v1.4.11 (GNU/Linux)
262
 
 
263
 
iEYEARECAAYFAk33gYsACgkQpQbm1N1NUIhiDACglOuQDlnSF4NxfHSkN/zrmFy8
264
 
nswAoNGXAVuR9ONasAKIGBNUE0b+lols
265
 
=SOuC
266
 
-----END PGP SIGNATURE-----
267
 
"""
268
 
        plain = """bazaar-ng testament short form 1
269
 
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
270
 
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
271
 
"""
272
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
273
 
        self.assertEqual((gpg.SIGNATURE_NOT_VALID, None),
274
 
                            my_gpg.verify(content, plain))
275
 
 
276
 
    def test_set_acceptable_keys(self):
277
 
        self.requireFeature(features.gpgme)
278
 
        self.import_keys()
279
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
280
 
        my_gpg.set_acceptable_keys("bazaar@example.com")
281
 
        self.assertEqual(my_gpg.acceptable_keys,
282
 
                         [u'B5DEED5FCB15DAE6ECEF919587681B1EE3080E45'])
283
 
 
284
 
    def test_set_acceptable_keys_unknown(self):
285
 
        self.requireFeature(features.gpgme)
286
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
287
 
        my_gpg.set_acceptable_keys("unknown")
288
 
        self.assertEqual(my_gpg.acceptable_keys, [])
289
 
 
290
87
 
291
88
class TestDisabled(TestCase):
292
89
 
293
90
    def test_sign(self):
294
91
        self.assertRaises(errors.SigningFailed,
295
92
                          gpg.DisabledGPGStrategy(None).sign, 'content')
296
 
 
297
 
    def test_verify(self):
298
 
        self.assertRaises(errors.SignatureVerificationFailed,
299
 
                          gpg.DisabledGPGStrategy(None).verify, 'content',
300
 
                          'testament')