~launchpad-pqm/pygpgme/devel

« back to all changes in this revision

Viewing changes to gpgme/tests/test_genkey.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2008-10-22 21:47:47 UTC
  • mfrom: (47.1.1 pygpgme-genkey)
  • Revision ID: launchpad@pqm.canonical.com-20081022214747-h2sphj9zcuh1c1cq
[r=gmb] Merging trunk. Support for key generation and minor fixes in
        the test suite (GPG_AGENT_INFO handling).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# pygpgme - a Python wrapper for the gpgme library
 
2
# Copyright (C) 2006  James Henstridge
 
3
#
 
4
# This library is free software; you can redistribute it and/or
 
5
# modify it under the terms of the GNU Lesser General Public
 
6
# License as published by the Free Software Foundation; either
 
7
# version 2.1 of the License, or (at your option) any later version.
 
8
#
 
9
# This library is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
# Lesser General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU Lesser General Public
 
15
# License along with this library; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
17
 
 
18
import unittest
 
19
import StringIO
 
20
 
 
21
import gpgme
 
22
from gpgme.tests.util import GpgHomeTestCase
 
23
 
 
24
 
 
25
# See /usr/share/doc/gnupg/DETAILS.gz
 
26
 
 
27
# XXX we are using a passwordless key because the passphrase_cb
 
28
# backend seems to be currently broken.
 
29
 
 
30
signing_only_param = """
 
31
<GnupgKeyParms format="internal">
 
32
  Key-Type: RSA
 
33
  Key-Usage: sign
 
34
  Key-Length: 1024
 
35
  Name-Real: Testing
 
36
  Name-Comment: comment
 
37
  Name-Email: someone@example.com
 
38
  Expire-Date: 0
 
39
</GnupgKeyParms>
 
40
"""
 
41
 
 
42
 
 
43
class GenerateKeyTestCase(GpgHomeTestCase):
 
44
 
 
45
    def assertCanSign(self, key):
 
46
        """Check that the given key can be used to create signatures."""
 
47
        ctx = gpgme.Context()
 
48
        ctx.signers = [key]
 
49
 
 
50
        plaintext = StringIO.StringIO('Hello World\n')
 
51
        signature = StringIO.StringIO()
 
52
 
 
53
        ctx.armor = True
 
54
        new_sigs = ctx.sign(
 
55
            plaintext, signature, gpgme.SIG_MODE_DETACH)
 
56
 
 
57
        signature.seek(0)
 
58
        plaintext.seek(0)
 
59
 
 
60
        sigs = ctx.verify(signature, plaintext, None)
 
61
        self.assertEqual(len(sigs), 1)
 
62
        self.assertEqual(sigs[0].fpr, key.subkeys[0].fpr)
 
63
 
 
64
    def test_generate_signing_only_keys(self):
 
65
        ctx = gpgme.Context()
 
66
        result = ctx.genkey(signing_only_param)
 
67
 
 
68
        self.assertEqual(result.primary, True)
 
69
        self.assertEqual(result.sub, False)
 
70
        self.assertEqual(len(result.fpr), 40)
 
71
 
 
72
        # The generated key is part of the current keyring.
 
73
        key = ctx.get_key(result.fpr, True)
 
74
        self.assertEqual(key.revoked, False)
 
75
        self.assertEqual(key.expired, False)
 
76
        self.assertEqual(key.secret, True)
 
77
        self.assertEqual(key.protocol, gpgme.PROTOCOL_OpenPGP)
 
78
 
 
79
        # Single signing-only RSA key.
 
80
        self.assertEqual(len(key.subkeys), 1)
 
81
        subkey = key.subkeys[0]
 
82
        self.assertEqual(subkey.secret, True)
 
83
        self.assertEqual(subkey.pubkey_algo, gpgme.PK_RSA)
 
84
        self.assertEqual(subkey.length, 1024)
 
85
 
 
86
        self.assertEqual(key.can_sign, True)
 
87
        self.assertEqual(key.can_encrypt, False)
 
88
 
 
89
        # The only UID available matches the given parameters.
 
90
        [uid] = key.uids
 
91
        self.assertEqual(uid.name, 'Testing')
 
92
        self.assertEqual(uid.comment, 'comment')
 
93
        self.assertEqual(uid.email, 'someone@example.com')
 
94
 
 
95
        # Finally check if the generated key can perform signatures.
 
96
        self.assertCanSign(key)
 
97
 
 
98
    def test_invalid_parameters(self):
 
99
        ctx = gpgme.Context()
 
100
        try:
 
101
            ctx.genkey('garbage parameters')
 
102
        except gpgme.GpgmeError, exc:
 
103
            self.assertTrue(hasattr(exc, "result"))
 
104
            result = exc.result
 
105
            self.assertEqual(result.primary, False)
 
106
            self.assertEqual(result.sub, False)
 
107
            self.assertEqual(result.fpr, None)
 
108
        else:
 
109
            self.fail("GpgmeError not raised")
 
110
 
 
111
 
 
112
def test_suite():
 
113
    loader = unittest.TestLoader()
 
114
    return loader.loadTestsFromName(__name__)
 
115
 
 
116
 
 
117
if __name__ == '__main__':
 
118
    unittest.main()