~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_gpg.py

  • Committer: Vincent Ladeuil
  • Date: 2013-08-09 15:09:23 UTC
  • mto: This revision was merged to the branch mainline in revision 6587.
  • Revision ID: v.ladeuil+lp@free.fr-20130809150923-y71dusyorep0hbkt
Fix various typos in docstrings. Rename 'buffer' to 'buf' since it's now a python builtin function.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007, 2009, 2011 Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
 
18
18
"""Tests for signing and verifying blobs of data via gpg."""
19
19
 
20
20
# import system imports here
21
 
import os
22
21
import sys
23
22
 
24
 
import bzrlib.errors as errors
25
 
import bzrlib.gpg as gpg
26
 
from bzrlib.tests import TestCase, TestCaseInTempDir
27
 
 
28
 
class FakeConfig(object):
29
 
 
30
 
    def gpg_signing_command(self):
31
 
        return "false"
32
 
        
33
 
 
34
 
class TestCommandLine(TestCase):
 
23
from bzrlib import (
 
24
    config,
 
25
    errors,
 
26
    gpg,
 
27
    tests,
 
28
    trace,
 
29
    ui,
 
30
    )
 
31
from bzrlib.tests import (
 
32
    TestCase,
 
33
    features,
 
34
    )
 
35
 
 
36
 
 
37
class FakeConfig(config.MemoryStack):
 
38
 
 
39
    def __init__(self, content=None):
 
40
        if content is None:
 
41
            content = '''
 
42
gpg_signing_key=amy@example.com
 
43
gpg_signing_command=false'''
 
44
        super(FakeConfig, self).__init__(content)
 
45
 
 
46
 
 
47
class TestCommandLine(tests.TestCase):
 
48
 
 
49
    def setUp(self):
 
50
        super(TestCommandLine, self).setUp()
 
51
        self.my_gpg = gpg.GPGStrategy(FakeConfig())
35
52
 
36
53
    def test_signing_command_line(self):
37
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
38
 
        self.assertEqual(['false',  '--clearsign'],
 
54
        self.assertEqual(['false',  '--clearsign', '-u', 'amy@example.com'],
 
55
                         self.my_gpg._command_line())
 
56
 
 
57
    def test_signing_command_line_from_default(self):
 
58
        # Using 'default' for gpg_signing_key will use the mail part of 'email'
 
59
        my_gpg = gpg.GPGStrategy(FakeConfig('''
 
60
email=Amy <amy@example.com>
 
61
gpg_signing_key=default
 
62
gpg_signing_command=false'''))
 
63
        self.assertEqual(['false',  '--clearsign', '-u', 'amy@example.com'],
 
64
                         my_gpg._command_line())
 
65
 
 
66
    def test_signing_command_line_from_email(self):
 
67
        # Not setting gpg_signing_key will use the mail part of 'email'
 
68
        my_gpg = gpg.GPGStrategy(FakeConfig('''
 
69
email=Amy <amy@example.com>
 
70
gpg_signing_command=false'''))
 
71
        self.assertEqual(['false',  '--clearsign', '-u', 'amy@example.com'],
39
72
                         my_gpg._command_line())
40
73
 
41
74
    def test_checks_return_code(self):
42
75
        # This test needs a unix like platform - one with 'false' to run.
43
76
        # if you have one, please make this work :)
44
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
45
 
        self.assertRaises(errors.SigningFailed, my_gpg.sign, 'content')
46
 
 
47
 
    def test_returns_output(self):
48
 
        # This test needs a 'cat' command or similar to work.
49
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
50
 
        content = "some content\nwith newlines\n"
51
 
 
 
77
        self.assertRaises(errors.SigningFailed, self.my_gpg.sign, 'content')
 
78
 
 
79
    def assertProduces(self, content):
 
80
        # This needs a 'cat' command or similar to work.
52
81
        if sys.platform == 'win32':
53
82
            # Windows doesn't come with cat, and we don't require it
54
83
            # so lets try using python instead.
55
 
            # But stupid windows and line-ending conversions. 
 
84
            # But stupid windows and line-ending conversions.
56
85
            # It is too much work to make sys.stdout be in binary mode.
57
86
            # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65443
58
 
            my_gpg._command_line = lambda:[sys.executable, '-c',
 
87
            self.my_gpg._command_line = lambda:[sys.executable, '-c',
59
88
                    'import sys; sys.stdout.write(sys.stdin.read())']
60
89
            new_content = content.replace('\n', '\r\n')
61
90
 
62
 
            self.assertEqual(new_content, my_gpg.sign(content))
 
91
            self.assertEqual(new_content, self.my_gpg.sign(content))
63
92
        else:
64
 
            my_gpg._command_line = lambda:['cat', '-']
65
 
            self.assertEqual(content, my_gpg.sign(content))
 
93
            self.my_gpg._command_line = lambda:['cat', '-']
 
94
            self.assertEqual(content, self.my_gpg.sign(content))
 
95
 
 
96
    def test_returns_output(self):
 
97
        content = "some content\nwith newlines\n"
 
98
        self.assertProduces(content)
 
99
 
 
100
    def test_clears_progress(self):
 
101
        content = "some content\nwith newlines\n"
 
102
        old_clear_term = ui.ui_factory.clear_term
 
103
        clear_term_called = []
 
104
        def clear_term():
 
105
            old_clear_term()
 
106
            clear_term_called.append(True)
 
107
        ui.ui_factory.clear_term = clear_term
 
108
        try:
 
109
            self.assertProduces(content)
 
110
        finally:
 
111
            ui.ui_factory.clear_term = old_clear_term
 
112
        self.assertEqual([True], clear_term_called)
 
113
 
 
114
    def test_aborts_on_unicode(self):
 
115
        """You can't sign Unicode text; it must be encoded first."""
 
116
        self.assertRaises(errors.BzrBadParameterUnicode,
 
117
                          self.assertProduces, u'foo')
 
118
 
 
119
 
 
120
class TestVerify(TestCase):
 
121
 
 
122
    def import_keys(self):
 
123
        from StringIO import StringIO
 
124
        import gpgme
 
125
        context = gpgme.Context()
 
126
 
 
127
        key = StringIO("""-----BEGIN PGP PUBLIC KEY BLOCK-----
 
128
Version: GnuPG v1.4.11 (GNU/Linux)
 
129
 
 
130
mQENBE343IgBCADwzPW7kmKb2bjB+UU+1ER/ABMZspvtoZMPusUw7bk6coXHF/0W
 
131
u1K/hSYeX9xaGOfOQw41r/g13MoR9dsL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eX
 
147
P6ktvXbX2Iu7VDNoCvRXM9+ntyJtsXCjNXg4pTGHS/XO4nm2db4FUZOBcVMb1vCc
 
148
VtFjLTcbCqJqpoJWUtsLcNqDqMHOQDpe6KTNTQARAQABiQEfBBgBAgAJBQJN+NyI
 
149
AhsMAAoJEIdoGx7jCA5FrR8IANnOF3PUj1TbRcwV6RoWmHsFQHrPmM8ogXia1Lsv
 
150
jE1iEWoC+muvKh6Oydf90k6ZslS7rdDnp2qzYY8W/TiDkxP+fvsZ4mMi1Y0F+3ty
 
151
1jzWhcsnB2VrJSiavxEXk0tKPrNv4EUGWG6wHsC9TBj37If+nrMyim94VHvI0eHm
 
152
X8yMlN4O3HfmgD9CbJdUxueP3e31OIYuwh/6F7GII8TNEVHU/8vh/mQcCxppNbc+
 
153
boff+kIsoa/TAMLwtJoSrX1nXm0K3vZePRLnIgmwVzdkOIkaRJUG2tSQFvkfhvtE
 
154
LhnkL5l4MO0wrUds0UWRwa3d7j/P2ExrqXdlLmEzrifWyEQ=
 
155
=hUJn
 
156
-----END PGP PUBLIC KEY BLOCK-----
 
157
""")
 
158
 
 
159
        secret_key = StringIO("""-----BEGIN PGP PRIVATE KEY BLOCK-----
 
160
Version: GnuPG v1.4.11 (GNU/Linux)
 
161
 
 
162
lQOYBE343IgBCADwzPW7kmKb2bjB+UU+1ER/ABMZspvtoZMPusUw7bk6coXHF/0W
 
163
u1K/hSYeX9xaGOfOQw41r/g13MoR9dsL6L84RLiisf38rRoBZt+d5bCbZA5Xo801
 
164
2PeoBoGo6u5oOYKAFLMvrUitPiiE0IT/oQTfC4YUrLN4A+9W0QZruPGIpIXwmZXr
 
165
L0zsqYfNqIN0ompeJenVpKpvm3loJ/zfK7R3EJ3hsv6nkUmWCFsP1Pw3UV1YuCmw
 
166
Mkdn1U7DaOql1WjXgj9ABQDJrun2TGsqrSRzBODtHKA/uOX0K3VfKBU8VZo3dXUm
 
167
1Q4ZeZC39L9qJGTH8TQYlwBLe1yAOp+vx7QJABEB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NbytiYF7Hbat8GWVZqY1u
 
199
CZr7BklpIVWlk62ll0psMIPVyANi7YT332LLqYmBBADJKTx2dariG/kWU2W/9VEO
 
200
2VZpqsqazAxOoFEIOpcOlByhhyw5g0IKu0UyzHkhoCje0cWxpdSBFG432b8zL0AT
 
201
Z0RycfUG7Sgp9CpY1h8Cc/HbBa8xo1fSM7zplPQrHBqUzlVVBq6HOkUq+7qsPFWc
 
202
RRie95VsDmIMKQKPJHeYHQQA3EYGit+QHV0dccAInghEsf/mq8Gfnvo6HPYhWcDC
 
203
DTM39NhNlnl1WkTFCd2TWc+TWQ4KlRsh6bMjUpNa2qjrUl90fLekbogcxxMhcwa6
 
204
xgzEANZfwqdY0u3aB/CyZ6odfThwcAoeqoMpw34CfeKEroubpi2n8wKByrN2MQXJ
 
205
4vEEAJbXZOqgAcFAFBUVb5mVT0s2lJMagZFPdhRJz2bttz01s/B8aca6CrDpFRjT
 
206
03zRFUZjwDYqZDWBC181dCE9yla4OkWd5QyRKSS2EE02KEYqRzT0RngQn7s4AW2r
 
207
326up3Jhleln3hgD4Kk3V3KHmyK8zqZA0qWzry4Vl2jjkbnAPB2JAR8EGAECAAkF
 
208
Ak343IgCGwwACgkQh2gbHuMIDkWtHwgA2c4Xc9SPVNtFzBXpGhaYewVAes+YzyiB
 
209
eJrUuy+MTWIRagL6a68qHo7J1/3STpmyVLut0OenarNhjxb9OIOTE/5++xniYyLV
 
210
jQX7e3LWPNaFyycHZWslKJq/EReTS0o+s2/gRQZYbrAewL1MGPfsh/6eszKKb3hU
 
211
e8jR4eZfzIyU3g7cd+aAP0Jsl1TG54/d7fU4hi7CH/oXsYgjxM0RUdT/y+H+ZBwL
 
212
Gmk1tz5uh9/6Qiyhr9MAwvC0mhKtfWdebQre9l49EuciCbBXN2Q4iRpElQba1JAW
 
213
+R+G+0QuGeQvmXgw7TCtR2zRRZHBrd3uP8/YTGupd2UuYTOuJ9bIRA==
 
214
=LXn0
 
215
-----END PGP PRIVATE KEY BLOCK-----
 
216
""")
 
217
 
 
218
        revoked_key = StringIO("""-----BEGIN PGP PUBLIC KEY BLOCK-----
 
219
Version: GnuPG v1.4.11 (GNU/Linux)
 
220
 
 
221
mI0ETjlW5gEEAOb/6P+TVM59E897wRtatxys2BhsHCXM4T7xjIiANfDwejDdifqh
 
222
tluTfSJLLxPembtrrEjux1C0AJgc+f0MIfsc3Pr3eFJzKB2ot/1IVG1/1KnA0zt3
 
223
W2xPT3lRib27WJ9Fag+dMtQaIzgJ7/n2DFxsFZ33FD2kxrEXB2exGg6FABEBAAGI
 
224
pgQgAQIAEAUCTjlXkAkdAHJldm9rZWQACgkQjs6dvEpb0cQPHAP/Wi9rbx0e+1Sf
 
225
ziGgyVdr3m3A6uvze5oXKVgFRbGRUYSH4/I8GW0W9x4TcRg9h+YaQ8NUdADr9kNE
 
226
tKAljLqYA5qdqSfYuaij1M++Xj+KUZ359R74sHuQqwnRy1XXQNfRs/QpXA7vLdds
 
227
rjg+pbWuXO92TZJUdnqtWW+VEyZBsPy0G3Rlc3Qga2V5IDx0ZXN0QGV4YW1wbGUu
 
228
Y29tPoi4BBMBAgAiBQJOOVbmAhsDBgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIXgAAK
 
229
CRCOzp28SlvRxNWzA/42WVmI0b+6mF/imEOlY1TiyvrcpK250rkSDsCtL4lOwy7G
 
230
antZhpgNfnXRd/ySfsS3EB6dpOWgOSxGRvWQhA+vxBT9BYNk49qd3JIrSaSWpR12
 
231
rET8qO1rEQQFWsw03CxTGujxGlmEO+a1yguRXp2UWaY7FngcQmD+8q7BUIVm7riN
 
232
BE45VuYBBADTEH2jHTjNCc5CMOhea6EJTrkx3upcEqB2oyhWeSWJiBGOxlcddsjo
 
233
3J3/EmBB8kK1hM9TidD3SG64x1N287lg8ELJBlKv+pQVyxohGJ1u/THgpTDMMQcL
 
234
luG5rAHQGSfyzKTiOnaTyBYg3M/nzgUOU9dKEFB0EA3tjUXFOT+r3wARAQABiJ8E
 
235
GAECAAkFAk45VuYCGwwACgkQjs6dvEpb0cRSLQP/fzCWX2lXwlwWiVF8BOPF7o9z
 
236
icHErc7/X17RGb4qj1kVf+UkRdUWJrbEVh4h6MncBIuA70WsYogiw+Kz/0LCtQAR
 
237
YUJsPy/EL++OKPH1aFasOdTxwkTka85+RdYqhP1+z/aYLFMWq6mRFI+o6x2k5mGi
 
238
7dMv2kKTJPoXUpiXJbg=
 
239
=hLYO
 
240
-----END PGP PUBLIC KEY BLOCK-----
 
241
""")
 
242
 
 
243
        expired_key = StringIO("""-----BEGIN PGP PUBLIC KEY BLOCK-----
 
244
Version: GnuPG v1.4.11 (GNU/Linux)
 
245
 
 
246
mI0ETjZ6PAEEALkR4GcFQidCCxV7pgQwQd5MZua0YO2l92fVqHX+PhnZ6egCLKdD
 
247
2bWlMUd6MLPF3FlRL7BBAxvW/DazkBOp7ljsnpMpptEzY49Uem1irYLYiVb9zK96
 
248
0sQZzFxFkfEYetQEXC68mIck8tbySOX5NAOw++3jFm3J7dsU1R3XtYzRABEBAAG0
 
249
G3Rlc3Qga2V5IDx0ZXN0QGV4YW1wbGUuY29tPoi+BBMBAgAoBQJONno8AhsDBQkA
 
250
AVGABgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIXgAAKCRAc4m97T40VEz+DA/9PBphG
 
251
Yp9cHVaHSfTUKGTGgIbvRe60sFNpDCYZeAGDrygOMuI8MNzbVpwefRBFHVPx7jWd
 
252
rrYMsLkcsNUS9D0baU+0D/qp7JVg7ZSQtG0O6IG4eTZhibteY1fu0+unlXmg9NHx
 
253
5VvhwzBiJDYji00M2p/CZEMiYFUuy76CsxUpN7iNBE42ejwBBACkv2/mX7IPQg0C
 
254
A3KSrJsJv+sdvKm4b4xuI4OwagwTIVz4KlTqV4IBrVjSBfwyMXucXz0bTW85qjgA
 
255
+n67td8vyjYYZUEz1uY9lSquQQDnAN0txL3cLHZXWiWOkmzZVddQtlflK2a/J9o0
 
256
QkHPVUm+hc4l64dIzStrNl2S66fAvQARAQABiKUEGAECAA8FAk42ejwCGwwFCQAB
 
257
UYAACgkQHOJve0+NFROEYQP/epg+o8iBs31hkSERyZjrRR66LpywezWj30Rn/3mX
 
258
Fzi9HkF4xLemWOzdNt9C5PYrOep85PQg8haEjknxVjZFS0ikT1h3OWk/TF1ZrLVm
 
259
WzyX8DaHQEjKpLJJjXcAbTiZBNMk0QaVC9RvIeHpCf3n3DC49DdjsPJRMKOn8KDi
 
260
kRk=
 
261
=p0gt
 
262
-----END PGP PUBLIC KEY BLOCK-----
 
263
""")
 
264
        context.import_(key)
 
265
        context.import_(secret_key)
 
266
        context.import_(revoked_key)
 
267
        context.import_(expired_key)
 
268
 
 
269
    def test_verify_untrusted_but_accepted(self):
 
270
        #untrusted by gpg but listed as acceptable_keys by user
 
271
        self.requireFeature(features.gpgme)
 
272
        self.import_keys()
 
273
 
 
274
        content = """-----BEGIN PGP SIGNED MESSAGE-----
 
275
Hash: SHA1
 
276
 
 
277
bazaar-ng testament short form 1
 
278
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
 
279
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
 
280
-----BEGIN PGP SIGNATURE-----
 
281
Version: GnuPG v1.4.11 (GNU/Linux)
 
282
 
 
283
iQEcBAEBAgAGBQJN+ekFAAoJEIdoGx7jCA5FGtEH/i+XxJRvqU6wdBtLVrGBMAGk
 
284
FZ5VP+KyXYtymSbgSstj/vM12NeMIeFs3xGnNnYuX1MIcY6We5TKtCH0epY6ym5+
 
285
6g2Q2QpQ5/sT2d0mWzR0K4uVngmxVQaXTdk5PdZ40O7ULeDLW6CxzxMHyUL1rsIx
 
286
7UBUTBh1O/1n3ZfD99hUkm3hVcnsN90uTKH59zV9NWwArU0cug60+5eDKJhSJDbG
 
287
rIwlqbFAjDZ7L/48e+IaYIJwBZFzMBpJKdCxzALLtauMf+KK8hGiL2hrRbWm7ty6
 
288
NgxfkMYOB4rDPdSstT35N+5uBG3n/UzjxHssi0svMfVETYYX40y57dm2eZQXFp8=
 
289
=iwsn
 
290
-----END PGP SIGNATURE-----
 
291
"""
 
292
        plain = """bazaar-ng testament short form 1
 
293
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
 
294
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
 
295
"""
 
296
        my_gpg = gpg.GPGStrategy(FakeConfig())
 
297
        my_gpg.set_acceptable_keys("bazaar@example.com")
 
298
        self.assertEqual((gpg.SIGNATURE_VALID, None), my_gpg.verify(content,
 
299
                            plain))
 
300
 
 
301
    def test_verify_unacceptable_key(self):
 
302
        self.requireFeature(features.gpgme)
 
303
        self.import_keys()
 
304
 
 
305
        content = """-----BEGIN PGP SIGNED MESSAGE-----
 
306
Hash: SHA1
 
307
 
 
308
bazaar-ng testament short form 1
 
309
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
 
310
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
 
311
-----BEGIN PGP SIGNATURE-----
 
312
Version: GnuPG v1.4.11 (GNU/Linux)
 
313
 
 
314
iQEcBAEBAgAGBQJN+ekFAAoJEIdoGx7jCA5FGtEH/i+XxJRvqU6wdBtLVrGBMAGk
 
315
FZ5VP+KyXYtymSbgSstj/vM12NeMIeFs3xGnNnYuX1MIcY6We5TKtCH0epY6ym5+
 
316
6g2Q2QpQ5/sT2d0mWzR0K4uVngmxVQaXTdk5PdZ40O7ULeDLW6CxzxMHyUL1rsIx
 
317
7UBUTBh1O/1n3ZfD99hUkm3hVcnsN90uTKH59zV9NWwArU0cug60+5eDKJhSJDbG
 
318
rIwlqbFAjDZ7L/48e+IaYIJwBZFzMBpJKdCxzALLtauMf+KK8hGiL2hrRbWm7ty6
 
319
NgxfkMYOB4rDPdSstT35N+5uBG3n/UzjxHssi0svMfVETYYX40y57dm2eZQXFp8=
 
320
=iwsn
 
321
-----END PGP SIGNATURE-----
 
322
"""
 
323
        plain = """bazaar-ng testament short form 1
 
324
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
 
325
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
 
326
"""
 
327
        my_gpg = gpg.GPGStrategy(FakeConfig())
 
328
        my_gpg.set_acceptable_keys("foo@example.com")
 
329
        self.assertEqual((gpg.SIGNATURE_KEY_MISSING, u'E3080E45'),
 
330
                         my_gpg.verify(content, plain))
 
331
 
 
332
    def test_verify_valid_but_untrusted(self):
 
333
        self.requireFeature(features.gpgme)
 
334
        self.import_keys()
 
335
 
 
336
        content = """-----BEGIN PGP SIGNED MESSAGE-----
 
337
Hash: SHA1
 
338
 
 
339
bazaar-ng testament short form 1
 
340
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
 
341
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
 
342
-----BEGIN PGP SIGNATURE-----
 
343
Version: GnuPG v1.4.11 (GNU/Linux)
 
344
 
 
345
iQEcBAEBAgAGBQJN+ekFAAoJEIdoGx7jCA5FGtEH/i+XxJRvqU6wdBtLVrGBMAGk
 
346
FZ5VP+KyXYtymSbgSstj/vM12NeMIeFs3xGnNnYuX1MIcY6We5TKtCH0epY6ym5+
 
347
6g2Q2QpQ5/sT2d0mWzR0K4uVngmxVQaXTdk5PdZ40O7ULeDLW6CxzxMHyUL1rsIx
 
348
7UBUTBh1O/1n3ZfD99hUkm3hVcnsN90uTKH59zV9NWwArU0cug60+5eDKJhSJDbG
 
349
rIwlqbFAjDZ7L/48e+IaYIJwBZFzMBpJKdCxzALLtauMf+KK8hGiL2hrRbWm7ty6
 
350
NgxfkMYOB4rDPdSstT35N+5uBG3n/UzjxHssi0svMfVETYYX40y57dm2eZQXFp8=
 
351
=iwsn
 
352
-----END PGP SIGNATURE-----
 
353
"""
 
354
        plain = """bazaar-ng testament short form 1
 
355
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
 
356
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
 
357
"""
 
358
        my_gpg = gpg.GPGStrategy(FakeConfig())
 
359
        self.assertEqual((gpg.SIGNATURE_NOT_VALID, None), my_gpg.verify(content,
 
360
                            plain))
 
361
 
 
362
    def test_verify_bad_testament(self):
 
363
        self.requireFeature(features.gpgme)
 
364
        self.import_keys()
 
365
 
 
366
        content = """-----BEGIN PGP SIGNED MESSAGE-----
 
367
Hash: SHA1
 
368
 
 
369
bazaar-ng testament short form 1
 
370
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
 
371
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
 
372
-----BEGIN PGP SIGNATURE-----
 
373
Version: GnuPG v1.4.11 (GNU/Linux)
 
374
 
 
375
iQEcBAEBAgAGBQJN+ekFAAoJEIdoGx7jCA5FGtEH/i+XxJRvqU6wdBtLVrGBMAGk
 
376
FZ5VP+KyXYtymSbgSstj/vM12NeMIeFs3xGnNnYuX1MIcY6We5TKtCH0epY6ym5+
 
377
6g2Q2QpQ5/sT2d0mWzR0K4uVngmxVQaXTdk5PdZ40O7ULeDLW6CxzxMHyUL1rsIx
 
378
7UBUTBh1O/1n3ZfD99hUkm3hVcnsN90uTKH59zV9NWwArU0cug60+5eDKJhSJDbG
 
379
rIwlqbFAjDZ7L/48e+IaYIJwBZFzMBpJKdCxzALLtauMf+KK8hGiL2hrRbWm7ty6
 
380
NgxfkMYOB4rDPdSstT35N+5uBG3n/UzjxHssi0svMfVETYYX40y57dm2eZQXFp8=
 
381
=iwsn
 
382
-----END PGP SIGNATURE-----
 
383
"""
 
384
        plain = """bazaar-ng testament short form 1
 
385
revision-id: doctor@example.com-20110527185938-hluafawphszb8dl1
 
386
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
 
387
"""
 
388
        my_gpg = gpg.GPGStrategy(FakeConfig())
 
389
        my_gpg.set_acceptable_keys("bazaar@example.com")
 
390
        self.assertEqual((gpg.SIGNATURE_NOT_VALID, None), my_gpg.verify(content,
 
391
                            plain))
 
392
 
 
393
 
 
394
    def test_verify_revoked_signature(self):
 
395
        self.requireFeature(features.gpgme)
 
396
        self.import_keys()
 
397
 
 
398
        content = """-----BEGIN PGP SIGNED MESSAGE-----
 
399
Hash: SHA1
 
400
 
 
401
asdf
 
402
-----BEGIN PGP SIGNATURE-----
 
403
Version: GnuPG v1.4.11 (GNU/Linux)
 
404
 
 
405
iJwEAQECAAYFAk45V18ACgkQjs6dvEpb0cSIZQP/eOGTXGPlrNwvDkcX2d8O///I
 
406
ecB4sUIUEpv1XAk1MkNu58lsjjK72lRaLusEGqd7HwrFmpxVeVs0oWLg23PNPCFs
 
407
yJBID9ma+VxFVPtkEFnrc1R72sBJLfBcTxMkwVTC8eeznjdtn+cg+aLkxbPdrGnr
 
408
JFA6kUIJU2w9LU/b88Y=
 
409
=UuRX
 
410
-----END PGP SIGNATURE-----
 
411
"""
 
412
        plain = """asdf\n"""
 
413
        my_gpg = gpg.GPGStrategy(FakeConfig())
 
414
        my_gpg.set_acceptable_keys("test@example.com")
 
415
        self.assertEqual((gpg.SIGNATURE_NOT_VALID, None), my_gpg.verify(content,
 
416
                            plain))
 
417
 
 
418
    def test_verify_invalid(self):
 
419
        self.requireFeature(features.gpgme)
 
420
        self.import_keys()
 
421
        content = """-----BEGIN PGP SIGNED MESSAGE-----
 
422
Hash: SHA1
 
423
 
 
424
bazaar-ng testament short form 1
 
425
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
 
426
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
 
427
-----BEGIN PGP SIGNATURE-----
 
428
Version: GnuPG v1.4.11 (GNU/Linux)
 
429
 
 
430
iEYEARECAAYFAk33gYsACgkQpQbm1N1NUIhiDACglOuQDlnSF4NxfHSkN/zrmFy8
 
431
nswAoNGXAVuR9ONasAKIGBNUE0b+lols
 
432
=SOuC
 
433
-----END PGP SIGNATURE-----
 
434
"""
 
435
        plain = """bazaar-ng testament short form 1
 
436
revision-id: amy@example.com-20110527185938-hluafawphszb8dl1
 
437
sha1: 6411f9bdf6571200357140c9ce7c0f50106ac9a4
 
438
"""
 
439
        my_gpg = gpg.GPGStrategy(FakeConfig())
 
440
        self.assertEqual((gpg.SIGNATURE_NOT_VALID, None),
 
441
                            my_gpg.verify(content, plain))
 
442
 
 
443
    def test_verify_expired_but_valid(self):
 
444
        self.requireFeature(features.gpgme)
 
445
        self.import_keys()
 
446
        content = """-----BEGIN PGP SIGNED MESSAGE-----
 
447
Hash: SHA1
 
448
 
 
449
bazaar-ng testament short form 1
 
450
revision-id: test@example.com-20110801100657-f1dr1nompeex723z
 
451
sha1: 59ab434be4c2d5d646dee84f514aa09e1b72feeb
 
452
-----BEGIN PGP SIGNATURE-----
 
453
Version: GnuPG v1.4.10 (GNU/Linux)
 
454
 
 
455
iJwEAQECAAYFAk42esUACgkQHOJve0+NFRPc5wP7BoZkzBU8JaHMLv/LmqLr0sUz
 
456
zuE51ofZZ19L7KVtQWsOi4jFy0fi4A5TFwO8u9SOfoREGvkw292Uty9subSouK5/
 
457
mFmDOYPQ+O83zWgYZsBmMJWYDZ+X9I6XXZSbPtV/7XyTjaxtl5uRnDVJjg+AzKvD
 
458
dTp8VatVVrwuvzOPDVc=
 
459
=uHen
 
460
-----END PGP SIGNATURE-----
 
461
"""
 
462
        plain = """bazaar-ng testament short form 1
 
463
revision-id: test@example.com-20110801100657-f1dr1nompeex723z
 
464
sha1: 59ab434be4c2d5d646dee84f514aa09e1b72feeb
 
465
"""
 
466
        my_gpg = gpg.GPGStrategy(FakeConfig())
 
467
        self.assertEqual((gpg.SIGNATURE_EXPIRED, u'4F8D1513'),
 
468
                            my_gpg.verify(content, plain))
 
469
 
 
470
    def test_verify_unknown_key(self):
 
471
        self.requireFeature(features.gpgme)
 
472
        self.import_keys()
 
473
        content = """-----BEGIN PGP SIGNED MESSAGE-----
 
474
Hash: SHA1
 
475
 
 
476
asdf
 
477
-----BEGIN PGP SIGNATURE-----
 
478
Version: GnuPG v1.4.11 (GNU/Linux)
 
479
 
 
480
iQEcBAEBAgAGBQJOORKwAAoJENf6AkFdUeVvJDYH/1Cz+AJn1Jvy5n64o+0fZ5Ow
 
481
Y7UQb4QQTIOV7jI7n4hv/yBzuHrtImFzYvQl/o2Ezzi8B8L5gZtQy+xCUF+Q8iWs
 
482
gytZ5JUtSze7hDZo1NUl4etjoRGYqRfrUcvE2LkVH2dFbDGyyQfVmoeSHa5akuuP
 
483
QZmyg2F983rACVIpGvsqTH6RcBdvE9vx68lugeKQA8ArDn39/74FBFipFzrXSPij
 
484
eKFpl+yZmIb3g6HkPIC8o4j/tMvc37xF1OG5sBu8FT0+FC+VgY7vAblneDftAbyP
 
485
sIODx4WcfJtjLG/qkRYqJ4gDHo0eMpTJSk2CWebajdm4b+JBrM1F9mgKuZFLruE=
 
486
=RNR5
 
487
-----END PGP SIGNATURE-----
 
488
"""
 
489
        plain = "asdf\n"
 
490
        my_gpg = gpg.GPGStrategy(FakeConfig())
 
491
        self.assertEqual((gpg.SIGNATURE_KEY_MISSING, u'5D51E56F'),
 
492
                            my_gpg.verify(content, plain))
 
493
 
 
494
    def test_set_acceptable_keys(self):
 
495
        self.requireFeature(features.gpgme)
 
496
        self.import_keys()
 
497
        my_gpg = gpg.GPGStrategy(FakeConfig())
 
498
        my_gpg.set_acceptable_keys("bazaar@example.com")
 
499
        self.assertEqual(my_gpg.acceptable_keys,
 
500
                         [u'B5DEED5FCB15DAE6ECEF919587681B1EE3080E45'])
 
501
 
 
502
    def test_set_acceptable_keys_unknown(self):
 
503
        self.requireFeature(features.gpgme)
 
504
        my_gpg = gpg.GPGStrategy(FakeConfig())
 
505
        self.notes = []
 
506
        def note(*args):
 
507
            self.notes.append(args[0] % args[1:])
 
508
        self.overrideAttr(trace, 'note', note)
 
509
        my_gpg.set_acceptable_keys("unknown")
 
510
        self.assertEqual(my_gpg.acceptable_keys, [])
 
511
        self.assertEqual(self.notes,
 
512
            ['No GnuPG key results for pattern: unknown'])
66
513
 
67
514
 
68
515
class TestDisabled(TestCase):
70
517
    def test_sign(self):
71
518
        self.assertRaises(errors.SigningFailed,
72
519
                          gpg.DisabledGPGStrategy(None).sign, 'content')
 
520
 
 
521
    def test_verify(self):
 
522
        self.assertRaises(errors.SignatureVerificationFailed,
 
523
                          gpg.DisabledGPGStrategy(None).verify, 'content',
 
524
                          'testament')