1<?php
2
3/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5/**
6 * Key retrieval tests for the Crypt_GPG package.
7 *
8 * These tests require the PHPUnit 3.6 or greater package to be installed.
9 * PHPUnit is installable using PEAR. See the
10 * {@link http://www.phpunit.de/manual/3.6/en/installation.html manual}
11 * for detailed installation instructions.
12 *
13 * To run these tests, use:
14 * <code>
15 * $ phpunit GetKeysTestCase
16 * </code>
17 *
18 * LICENSE:
19 *
20 * This library is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU Lesser General Public License as
22 * published by the Free Software Foundation; either version 2.1 of the
23 * License, or (at your option) any later version.
24 *
25 * This library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
29 *
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, see
32 * <http://www.gnu.org/licenses/>
33 *
34 * @category  Encryption
35 * @package   Crypt_GPG
36 * @author    Michael Gauthier <mike@silverorange.com>
37 * @copyright 2005-2008 silverorange
38 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
39 * @version   CVS: $Id$
40 * @link      http://pear.php.net/package/Crypt_GPG
41 */
42
43/**
44 * Base test case.
45 */
46require_once 'TestCase.php';
47
48/**
49 * Tests key retrieval of Crypt_GPG.
50 *
51 * @category  Encryption
52 * @package   Crypt_GPG
53 * @author    Michael Gauthier <mike@silverorange.com>
54 * @copyright 2005-2008 silverorange
55 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
56 * @link      http://pear.php.net/package/Crypt_GPG
57 */
58class GetKeysTest extends Crypt_GPG_TestCase
59{
60    /**
61     * @group get-keys
62     */
63    public function testGetKeys()
64    {
65        $expectedKeys = array();
66
67        // {{{ first-keypair@example.com
68        $key = new Crypt_GPG_Key();
69        $expectedKeys[] = $key;
70
71        $userId = new Crypt_GPG_UserId();
72        $userId->setName('First Keypair Test Key');
73        $userId->setComment('do not encrypt important data with this key');
74        $userId->setEmail('first-keypair@example.com');
75        $key->addUserId($userId);
76
77        $subKey = new Crypt_GPG_SubKey();
78        $subKey->setId('C097D9EC94C06363');
79        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_DSA);
80        $subKey->setFingerprint('8D2299D9C5C211128B32BBB0C097D9EC94C06363');
81        $subKey->setLength(1024);
82        $subKey->setCreationDate(1221785805);
83        $subKey->setExpirationDate(0);
84        $subKey->setUsage(Crypt_GPG_SubKey::USAGE_SIGN | Crypt_GPG_SubKey::USAGE_CERTIFY);
85        $subKey->setCanEncrypt(false);
86        $subKey->setHasPrivate(true);
87        $key->addSubKey($subKey);
88
89        $subKey = new Crypt_GPG_SubKey();
90        $subKey->setId('9F93F9116728EF12');
91        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC);
92        $subKey->setFingerprint('C9C65B3BBF040E40D0EA27B79F93F9116728EF12');
93        $subKey->setLength(2048);
94        $subKey->setCreationDate(1221785821);
95        $subKey->setExpirationDate(0);
96        $subKey->setCanSign(false);
97        $subKey->setCanEncrypt(true);
98        $subKey->setHasPrivate(true);
99        $key->addSubKey($subKey);
100        // }}}
101        // {{{ second-keypair@example.com
102        $key = new Crypt_GPG_Key();
103        $expectedKeys[] = $key;
104
105        $userId = new Crypt_GPG_UserId();
106        $userId->setName('Second Keypair Test Key');
107        $userId->setComment('do not encrypt important data with this key');
108        $userId->setEmail('second-keypair@example.com');
109        $key->addUserId($userId);
110
111        $subKey = new Crypt_GPG_SubKey();
112        $subKey->setId('03CC890AFA1DAD4B');
113        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_DSA);
114        $subKey->setFingerprint('880922DBEA733E906693E4A903CC890AFA1DAD4B');
115        $subKey->setLength(1024);
116        $subKey->setCreationDate(1221785821);
117        $subKey->setExpirationDate(0);
118        $subKey->setUsage(Crypt_GPG_SubKey::USAGE_SIGN | Crypt_GPG_SubKey::USAGE_CERTIFY);
119        $subKey->setHasPrivate(true);
120        $key->addSubKey($subKey);
121
122        $subKey = new Crypt_GPG_SubKey();
123        $subKey->setId('B2F54E4757E22450');
124        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC);
125        $subKey->setFingerprint('C641EE162B46B810E8089153B2F54E4757E22450');
126        $subKey->setLength(2048);
127        $subKey->setCreationDate(1221785825);
128        $subKey->setExpirationDate(0);
129        $subKey->setCanSign(false);
130        $subKey->setCanEncrypt(true);
131        $subKey->setHasPrivate(true);
132        $key->addSubKey($subKey);
133        // }}}
134        // {{{ public-only@example.com
135        $key = new Crypt_GPG_Key();
136        $expectedKeys[] = $key;
137
138        $userId = new Crypt_GPG_UserId();
139        $userId->setName('Public Only Test Key');
140        $userId->setComment('do not encrypt important data with this key');
141        $userId->setEmail('public-only@example.com');
142        $key->addUserId($userId);
143
144        $subKey = new Crypt_GPG_SubKey();
145        $subKey->setId('ABA81EF54E8C0DEB');
146        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_DSA);
147        $subKey->setFingerprint('F83118CB6F5892DC1C3E936DABA81EF54E8C0DEB');
148        $subKey->setLength(1024);
149        $subKey->setCreationDate(1221785826);
150        $subKey->setExpirationDate(0);
151        $subKey->setUsage(Crypt_GPG_SubKey::USAGE_SIGN | Crypt_GPG_SubKey::USAGE_CERTIFY);
152        $subKey->setHasPrivate(false);
153        $key->addSubKey($subKey);
154
155        $subKey = new Crypt_GPG_SubKey();
156        $subKey->setId('BA4984433CDF4169');
157        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC);
158        $subKey->setFingerprint('B68C9DB020181C798047A6E7BA4984433CDF4169');
159        $subKey->setLength(2048);
160        $subKey->setCreationDate(1221785832);
161        $subKey->setExpirationDate(0);
162        $subKey->setCanSign(false);
163        $subKey->setCanEncrypt(true);
164        $subKey->setHasPrivate(false);
165        $key->addSubKey($subKey);
166        // }}}
167        // {{{ no-passphrase@example.com
168        $key = new Crypt_GPG_Key();
169        $expectedKeys[] = $key;
170
171        $userId = new Crypt_GPG_UserId();
172        $userId->setName('No Passphrase Public and Private Test Key');
173        $userId->setComment('do not encrypt important data with this key');
174        $userId->setEmail('no-passphrase@example.com');
175        $key->addUserId($userId);
176
177        $subKey = new Crypt_GPG_SubKey();
178        $subKey->setId('94563FB398ADA6B2');
179        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_DSA);
180        $subKey->setFingerprint('1EC9C5DBF239DD0A3A4FCD0D94563FB398ADA6B2');
181        $subKey->setLength(1024);
182        $subKey->setCreationDate(1221785833);
183        $subKey->setExpirationDate(0);
184        $subKey->setUsage(Crypt_GPG_SubKey::USAGE_SIGN | Crypt_GPG_SubKey::USAGE_CERTIFY);
185        $subKey->setHasPrivate(true);
186        $key->addSubKey($subKey);
187
188        $subKey = new Crypt_GPG_SubKey();
189        $subKey->setId('24BF380700C14B4F');
190        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC);
191        $subKey->setFingerprint('86DD46AC210531EE5A37567824BF380700C14B4F');
192        $subKey->setLength(2048);
193        $subKey->setCreationDate(1221785845);
194        $subKey->setExpirationDate(0);
195        $subKey->setCanSign(false);
196        $subKey->setCanEncrypt(true);
197        $subKey->setHasPrivate(true);
198        $key->addSubKey($subKey);
199        // }}}
200        // {{{ multiple-subkeys@example.com
201        $key = new Crypt_GPG_Key();
202        $expectedKeys[] = $key;
203
204        $userId = new Crypt_GPG_UserId();
205        $userId->setName('Multiple Subkeys');
206        $userId->setEmail('multiple-subkeys@example.com');
207        $key->addUserId($userId);
208
209        $subKey = new Crypt_GPG_SubKey();
210        $subKey->setId('B07A621DC9295765');
211        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_DSA);
212        $subKey->setFingerprint('F9DF21B5D2DD02D3DF760270B07A621DC9295765');
213        $subKey->setLength(1024);
214        $subKey->setCreationDate(1232605399);
215        $subKey->setExpirationDate(0);
216        $subKey->setUsage(Crypt_GPG_SubKey::USAGE_SIGN | Crypt_GPG_SubKey::USAGE_CERTIFY | Crypt_GPG_SubKey::USAGE_AUTHENTICATION);
217        $subKey->setHasPrivate(true);
218        $key->addSubKey($subKey);
219
220        $subKey = new Crypt_GPG_SubKey();
221        $subKey->setId('6F941ACC362453DA');
222        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC);
223        $subKey->setFingerprint('A728EE198BA2FB5C7B1C8B896F941ACC362453DA');
224        $subKey->setLength(2048);
225        $subKey->setCreationDate(1232605407);
226        $subKey->setExpirationDate(0);
227        $subKey->setCanSign(false);
228        $subKey->setCanEncrypt(true);
229        $subKey->setHasPrivate(true);
230        $key->addSubKey($subKey);
231
232        $subKey = new Crypt_GPG_SubKey();
233        $subKey->setId('2921E0D3FF5B0F4A');
234        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_RSA);
235        $subKey->setFingerprint('E1363DCE4863B824813AB2702921E0D3FF5B0F4A');
236        $subKey->setLength(2048);
237        $subKey->setCreationDate(1232605437);
238        $subKey->setExpirationDate(0);
239        $subKey->setCanSign(false);
240        $subKey->setCanEncrypt(true);
241        $subKey->setHasPrivate(true);
242        $key->addSubKey($subKey);
243        // }}}
244
245        $keys = $this->gpg->getKeys();
246        $this->assertEquals($expectedKeys, $keys);
247    }
248
249    /**
250     * @group get-keys
251     */
252    public function testGetKeysWithKeyId()
253    {
254        $keyId = 'first-keypair@example.com';
255        $expectedKeys = array();
256
257        // {{{ first-keypair@example.com
258        $key = new Crypt_GPG_Key();
259        $expectedKeys[] = $key;
260
261        $userId = new Crypt_GPG_UserId();
262        $userId->setName('First Keypair Test Key');
263        $userId->setComment('do not encrypt important data with this key');
264        $userId->setEmail('first-keypair@example.com');
265        $key->addUserId($userId);
266
267        $subKey = new Crypt_GPG_SubKey();
268        $subKey->setId('C097D9EC94C06363');
269        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_DSA);
270        $subKey->setFingerprint('8D2299D9C5C211128B32BBB0C097D9EC94C06363');
271        $subKey->setLength(1024);
272        $subKey->setCreationDate(1221785805);
273        $subKey->setExpirationDate(0);
274        $subKey->setUsage(Crypt_GPG_SubKey::USAGE_SIGN | Crypt_GPG_SubKey::USAGE_CERTIFY);
275        $subKey->setHasPrivate(true);
276        $key->addSubKey($subKey);
277
278        $subKey = new Crypt_GPG_SubKey();
279        $subKey->setId('9F93F9116728EF12');
280        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC);
281        $subKey->setFingerprint('C9C65B3BBF040E40D0EA27B79F93F9116728EF12');
282        $subKey->setLength(2048);
283        $subKey->setCreationDate(1221785821);
284        $subKey->setExpirationDate(0);
285        $subKey->setCanSign(false);
286        $subKey->setCanEncrypt(true);
287        $subKey->setHasPrivate(true);
288        $key->addSubKey($subKey);
289        // }}}
290
291        $keys = $this->gpg->getKeys($keyId);
292        $this->assertEquals($expectedKeys, $keys);
293    }
294
295    /**
296     * @group get-keys
297     */
298    public function testGetKeysNone()
299    {
300        $keyId = 'non-existent-key@example.com';
301        $expectedKeys = array();
302        $keys = $this->gpg->getKeys($keyId);
303        $this->assertEquals($expectedKeys, $keys);
304    }
305}
306