1<?php
2
3/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5/**
6 * Key generation 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 KeyGeneratorTestCase
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-2011 silverorange
38 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
39 * @version   CVS: $Id: GetKeysTestCase.php 274158 2009-01-22 06:33:54Z gauthierm $
40 * @link      http://pear.php.net/package/Crypt_GPG
41 */
42
43/**
44 * Base test case.
45 */
46require_once 'TestCase.php';
47
48/**
49 * The Crypt_GPG class to test
50 */
51require_once 'Crypt/GPG/KeyGenerator.php';
52
53/**
54 * Tests key generation of Crypt_GPG.
55 *
56 * @category  Encryption
57 * @package   Crypt_GPG
58 * @author    Michael Gauthier <mike@silverorange.com>
59 * @copyright 2005-2011 silverorange
60 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
61 * @link      http://pear.php.net/package/Crypt_GPG
62 */
63class KeyGeneratorTest extends Crypt_GPG_TestCase
64{
65    protected function assertKeyEquals(
66        Crypt_GPG_Key $key1,
67        Crypt_GPG_Key $key2
68    ) {
69        $userIds1 = $key1->getUserIds();
70        $userIds2 = $key2->getUserIds();
71        $userId1  = $userIds1[0];
72        $userId2  = $userIds2[0];
73        $subKeys1 = $key1->getSubKeys();
74        $subKeys2 = $key2->getSubKeys();
75        $subKeyA1 = $subKeys1[0];
76        $subKeyB1 = $subKeys1[1];
77        $subKeyA2 = $subKeys2[0];
78        $subKeyB2 = $subKeys2[1];
79
80        $this->assertEquals(
81            $userId1->getName(),
82            $userId2->getName(),
83            'User id names do not match.'
84        );
85
86        $this->assertEquals(
87            $userId1->getEmail(),
88            $userId2->getEmail(),
89            'User id email addresses do not match.'
90        );
91
92        $this->assertEquals(
93            $userId1->getComment(),
94            $userId2->getComment(),
95            'User id comments do not match.'
96        );
97
98        $this->assertEquals(
99            $subKeyA1->getAlgorithm(),
100            $subKeyA2->getAlgorithm(),
101            'Primary key algorithms do not match.'
102        );
103
104        $this->assertEquals(
105            $subKeyA1->getLength(),
106            $subKeyA2->getLength(),
107            'Primary key lengths do not match.'
108        );
109
110        $this->assertEquals(
111            $subKeyA1->getExpirationDate(),
112            $subKeyA2->getExpirationDate(),
113            'Primary key expiration dates do not match.'
114        );
115
116        $this->assertEquals(
117            $subKeyA1->canSign(),
118            $subKeyA2->canSign(),
119            'Primary key signing abilities do not match.'
120        );
121
122        $this->assertEquals(
123            $subKeyA1->canEncrypt(),
124            $subKeyA2->canEncrypt(),
125            'Primary key encrypting abilities do not match.'
126        );
127
128        $this->assertEquals(
129            $subKeyA1->hasPrivate(),
130            $subKeyA2->hasPrivate(),
131            'Primary key private keys do not match.'
132        );
133
134        $this->assertEquals(
135            $subKeyB1->getAlgorithm(),
136            $subKeyB2->getAlgorithm(),
137            'Secondary key algorithms do not match.'
138        );
139
140        $this->assertEquals(
141            $subKeyB1->getLength(),
142            $subKeyB2->getLength(),
143            'Secondary key lengths do not match.'
144        );
145
146        $this->assertEquals(
147            $subKeyB1->getExpirationDate(),
148            $subKeyB2->getExpirationDate(),
149            'Secondary key expiration dates do not match.'
150        );
151
152        $this->assertEquals(
153            $subKeyB1->canSign(),
154            $subKeyB2->canSign(),
155            'Secondary key signing abilities do not match.'
156        );
157
158        $this->assertEquals(
159            $subKeyB1->canEncrypt(),
160            $subKeyB2->canEncrypt(),
161            'Secondary key encrypting abilities do not match.'
162        );
163
164        $this->assertEquals(
165            $subKeyB1->hasPrivate(),
166            $subKeyB2->hasPrivate(),
167            'Secondary key private keys do not match.'
168        );
169    }
170
171    public function setUp(): void
172    {
173        parent::setUp();
174        $this->generator = new Crypt_GPG_KeyGenerator($this->getOptions());
175    }
176
177    /**
178     * @group mutators
179     */
180    public function testSetExpirationDate_zero()
181    {
182        $expectedDate = 0;
183        $this->generator->setExpirationDate(0);
184
185        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'expirationDate');
186        $this->assertSame($expectedDate, $value, 'Setting expiration date to zero failed.');
187    }
188
189    /**
190     * @group mutators
191     */
192    public function testSetExpirationDate_integer()
193    {
194        $expectedDate = 2000000000;
195        $this->generator->setExpirationDate(2000000000);
196
197        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'expirationDate');
198        $this->assertSame($expectedDate, $value, 'Setting expiration date by integer failed.');
199    }
200
201    /**
202     * @group mutators
203     */
204    public function testSetExpirationDate_string()
205    {
206        date_default_timezone_set('UTC');
207
208        $expectedDate = 2000000000;
209        $this->generator->setExpirationDate('2033-05-18T03:33:20');
210
211        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'expirationDate');
212        $this->assertSame($expectedDate, $value, 'Setting expiration date by string failed.');
213    }
214
215    /**
216     * @group mutators
217     */
218    public function testSetExpirationDate_invalid_format()
219    {
220        $this->expectException('InvalidArgumentException');
221
222        date_default_timezone_set('UTC');
223
224        $this->generator->setExpirationDate('this is not a date');
225    }
226
227    /**
228     * @group mutators
229     */
230    public function testSetExpirationDate_too_early_date()
231    {
232        $this->expectException('InvalidArgumentException');
233
234        $this->generator->setExpirationDate(1301088055);
235    }
236
237    /**
238     * @group mutators
239     */
240    public function testSetExpirationDate_today()
241    {
242        $this->expectException('InvalidArgumentException');
243
244        $this->generator->setExpirationDate(time());
245    }
246
247    /**
248     * @group mutators
249     */
250    public function testSetExpirationDate_too_late_date()
251    {
252        $this->expectException('InvalidArgumentException');
253
254        $this->generator->setExpirationDate(2147483648);
255    }
256
257    /**
258     * @group mutators
259     */
260    public function testSetPassphrase()
261    {
262        $expectedPassphrase = 'test1';
263        $this->generator->setPassphrase('test1');
264
265        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'passphrase');
266        $this->assertSame($expectedPassphrase, $value, 'Setting passphrase failed.');
267    }
268
269    /**
270     * @group mutators
271     */
272    public function testSetKeyParams_algorithm()
273    {
274        $expectedAlgorithm = Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN;
275        $expectedSize      = 1024;
276        $expectedUsage     = Crypt_GPG_SubKey::USAGE_SIGN
277            | Crypt_GPG_SubKey::USAGE_CERTIFY;
278
279        $this->generator->setKeyParams(
280            Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN
281        );
282
283        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'keyAlgorithm');
284        $this->assertSame($expectedAlgorithm, $value, 'Setting key algorithm failed.');
285
286        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'keySize');
287        $this->assertSame($expectedSize, $value, 'Setting key algorithm changed key size.');
288
289        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'keyUsage');
290        $this->assertSame($expectedUsage, $value, 'Setting key algorithm changed key usage.');
291    }
292
293    /**
294     * @group mutators
295     */
296    public function testSetKeyParams_algorithm_and_size()
297    {
298        $expectedAlgorithm = Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN;
299        $expectedSize      = 512;
300        $expectedUsage     = Crypt_GPG_SubKey::USAGE_SIGN
301            | Crypt_GPG_SubKey::USAGE_CERTIFY;
302
303        $this->generator->setKeyParams(
304            Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN,
305            512
306        );
307
308
309        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'keyAlgorithm');
310        $this->assertSame($expectedAlgorithm, $value, 'Setting key algorithm failed.');
311
312        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'keySize');
313        $this->assertSame($expectedSize, $value, 'Setting key size failed.');
314
315        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'keyUsage');
316        $this->assertSame($expectedUsage, $value, 'Setting key algorithm and size changed key usage.');
317    }
318
319    /**
320     * @group mutators
321     */
322    public function testSetKeyParams_algorithm_size_and_usage()
323    {
324        $expectedAlgorithm = Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN;
325        $expectedSize      = 512;
326        $expectedUsage     = Crypt_GPG_SubKey::USAGE_SIGN
327            | Crypt_GPG_SubKey::USAGE_CERTIFY
328            | Crypt_GPG_SubKey::USAGE_ENCRYPT;
329
330        $this->generator->setKeyParams(
331            Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN,
332            512,
333              Crypt_GPG_SubKey::USAGE_SIGN
334            | Crypt_GPG_SubKey::USAGE_CERTIFY
335            | Crypt_GPG_SubKey::USAGE_ENCRYPT
336        );
337
338
339        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'keyAlgorithm');
340        $this->assertSame($expectedAlgorithm, $value, 'Setting key algorithm failed.');
341
342        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'keySize');
343        $this->assertSame($expectedSize, $value, 'Setting key size failed.');
344
345        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'keyUsage');
346        $this->assertSame($expectedUsage, $value, 'Setting key algorithm and size changed key usage.');
347    }
348
349    /**
350     * @group mutators
351     */
352    public function testSetKeyParams_invalid_algorithm()
353    {
354        $this->expectException('Crypt_GPG_InvalidKeyParamsException');
355
356        $this->generator->setKeyParams(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC);
357    }
358
359    /**
360     * @group mutators
361     */
362    public function testSetKeyParams_invalid_dsa_usage()
363    {
364        $this->expectException('Crypt_GPG_InvalidKeyParamsException');
365
366        $this->generator->setKeyParams(
367            Crypt_GPG_SubKey::ALGORITHM_DSA,
368            2048,
369            Crypt_GPG_SubKey::USAGE_ENCRYPT | Crypt_GPG_SubKey::USAGE_CERTIFY
370        );
371    }
372
373    /**
374     * @group mutators
375     */
376    public function testSetSubKeyParams_algorithm()
377    {
378        $expectedAlgorithm = Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN;
379        $expectedSize      = 2048;
380        $expectedUsage     = Crypt_GPG_SubKey::USAGE_ENCRYPT;
381
382        $this->generator->setSubKeyParams(
383            Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN
384        );
385
386        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'subKeyAlgorithm');
387        $this->assertSame($expectedAlgorithm, $value, 'Setting sub-key algorithm failed.');
388
389        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'subKeySize');
390        $this->assertSame($expectedSize, $value, 'Setting sub-key algorithm changed key size.');
391
392        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'subKeyUsage');
393        $this->assertSame($expectedUsage, $value, 'Setting sub-key algorithm changed key usage.');
394    }
395
396    /**
397     * @group mutators
398     */
399    public function testSetSubKeyParams_algorithm_and_size()
400    {
401        $expectedAlgorithm = Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN;
402        $expectedSize      = 1024;
403        $expectedUsage     = Crypt_GPG_SubKey::USAGE_ENCRYPT;
404
405        $this->generator->setSubKeyParams(
406            Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN,
407            1024
408        );
409
410        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'subKeyAlgorithm');
411        $this->assertSame($expectedAlgorithm, $value, 'Setting sub-key algorithm failed.');
412
413        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'subKeySize');
414        $this->assertSame($expectedSize, $value, 'Setting sub-key algorithm changed key size.');
415
416        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'subKeyUsage');
417        $this->assertSame($expectedUsage, $value, 'Setting sub-key algorithm changed key usage.');
418    }
419
420    /**
421     * @group mutators
422     */
423    public function testSetSubKeyParams_algorithm_size_and_usage()
424    {
425        $expectedAlgorithm = Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN;
426        $expectedSize      = 1024;
427        $expectedUsage     = Crypt_GPG_SubKey::USAGE_SIGN
428            | Crypt_GPG_SubKey::USAGE_ENCRYPT;
429
430        $this->generator->setSubKeyParams(
431            Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN,
432            1024,
433              Crypt_GPG_SubKey::USAGE_SIGN
434            | Crypt_GPG_SubKey::USAGE_ENCRYPT
435        );
436
437        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'subKeyAlgorithm');
438        $this->assertSame($expectedAlgorithm, $value, 'Setting sub-key algorithm failed.');
439
440        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'subKeySize');
441        $this->assertSame($expectedSize, $value, 'Setting sub-key algorithm changed key size.');
442
443        $value = $this->getPropertyValue('Crypt_GPG_KeyGenerator', $this->generator, 'subKeyUsage');
444        $this->assertSame($expectedUsage, $value, 'Setting sub-key algorithm changed key usage.');
445    }
446
447    /**
448     * @group mutators
449     */
450    public function testSetSubKeyParams_invalid_elgamal_usage()
451    {
452        $this->expectException('Crypt_GPG_InvalidKeyParamsException');
453
454        $this->generator->setSubKeyParams(
455            Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC,
456            2048,
457            Crypt_GPG_SubKey::USAGE_SIGN | Crypt_GPG_SubKey::USAGE_ENCRYPT
458        );
459    }
460
461    /**
462     * @group mutators
463     */
464    public function testSetSubKeyParams_invalid_dsa_usage()
465    {
466        $this->expectException('Crypt_GPG_InvalidKeyParamsException');
467
468        $this->generator->setSubKeyParams(
469            Crypt_GPG_SubKey::ALGORITHM_DSA,
470            2048,
471            Crypt_GPG_SubKey::USAGE_SIGN | Crypt_GPG_SubKey::USAGE_ENCRYPT
472        );
473    }
474
475    /**
476     * @group generate-key
477     */
478    public function testGenerateKeyWithName()
479    {
480        if (!$this->config['enable-key-generation']) {
481            $this->markTestSkipped(
482                'Key generation tests are disabled. To run key generation '
483                . 'tests, enable them in the test configuration. See the '
484                . 'configuration in \'config.php.dist\' for an exampe.'
485            );
486        }
487
488        // {{{ generate-test@example.com
489        $expectedKey = new Crypt_GPG_Key();
490
491        $userId = new Crypt_GPG_UserId();
492        $userId->setName('Test Keypair');
493        $expectedKey->addUserId($userId);
494
495        $subKey = new Crypt_GPG_SubKey();
496        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_DSA);
497        $subKey->setLength(1024);
498        $subKey->setExpirationDate(0);
499        $subKey->setCanSign(true);
500        $subKey->setCanEncrypt(false);
501        $subKey->setHasPrivate(true);
502        $expectedKey->addSubKey($subKey);
503
504        $subKey = new Crypt_GPG_SubKey();
505        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC);
506        $subKey->setLength(2048);
507        $subKey->setExpirationDate(0);
508        $subKey->setCanSign(false);
509        $subKey->setCanEncrypt(true);
510        $subKey->setHasPrivate(true);
511        $expectedKey->addSubKey($subKey);
512        // }}}
513
514        $key = $this->generator->generateKey('Test Keypair');
515
516        $this->assertKeyEquals($expectedKey, $key);
517    }
518
519    /**
520     * @group generate-key
521     */
522    public function testGenerateKeyWithNameAndEmail()
523    {
524        if (!$this->config['enable-key-generation']) {
525            $this->markTestSkipped(
526                'Key generation tests are disabled. To run key generation '
527                . 'tests, enable them in the test configuration. See the '
528                . 'configuration in \'config.php.dist\' for an exampe.'
529            );
530        }
531
532        // {{{ generate-test@example.com
533        $expectedKey = new Crypt_GPG_Key();
534
535        $userId = new Crypt_GPG_UserId();
536        $userId->setName('Test Keypair');
537        $userId->setEmail('generate-test@example.com');
538        $expectedKey->addUserId($userId);
539
540        $subKey = new Crypt_GPG_SubKey();
541        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_DSA);
542        $subKey->setLength(1024);
543        $subKey->setExpirationDate(0);
544        $subKey->setCanSign(true);
545        $subKey->setCanEncrypt(false);
546        $subKey->setHasPrivate(true);
547        $expectedKey->addSubKey($subKey);
548
549        $subKey = new Crypt_GPG_SubKey();
550        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC);
551        $subKey->setLength(2048);
552        $subKey->setExpirationDate(0);
553        $subKey->setCanSign(false);
554        $subKey->setCanEncrypt(true);
555        $subKey->setHasPrivate(true);
556        $expectedKey->addSubKey($subKey);
557        // }}}
558
559        $key = $this->generator->generateKey(
560            'Test Keypair',
561            'generate-test@example.com'
562        );
563
564        $this->assertKeyEquals($expectedKey, $key);
565    }
566
567    /**
568     * @group generate-key
569     */
570    public function testGenerateKeyWithNameEmailAndComment()
571    {
572        if (!$this->config['enable-key-generation']) {
573            $this->markTestSkipped(
574                'Key generation tests are disabled. To run key generation '
575                . 'tests, enable them in the test configuration. See the '
576                . 'configuration in \'config.php.dist\' for an exampe.'
577            );
578        }
579
580        // {{{ generate-test@example.com
581        $expectedKey = new Crypt_GPG_Key();
582
583        $userId = new Crypt_GPG_UserId();
584        $userId->setName('Test Keypair');
585        $userId->setComment('do not use this key');
586        $userId->setEmail('generate-test@example.com');
587        $expectedKey->addUserId($userId);
588
589        $subKey = new Crypt_GPG_SubKey();
590        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_DSA);
591        $subKey->setLength(1024);
592        $subKey->setExpirationDate(0);
593        $subKey->setCanSign(true);
594        $subKey->setCanEncrypt(false);
595        $subKey->setHasPrivate(true);
596        $expectedKey->addSubKey($subKey);
597
598        $subKey = new Crypt_GPG_SubKey();
599        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC);
600        $subKey->setLength(2048);
601        $subKey->setExpirationDate(0);
602        $subKey->setCanSign(false);
603        $subKey->setCanEncrypt(true);
604        $subKey->setHasPrivate(true);
605        $expectedKey->addSubKey($subKey);
606        // }}}
607
608        $key = $this->generator->generateKey(
609            'Test Keypair',
610            'generate-test@example.com',
611            'do not use this key'
612        );
613
614        $this->assertKeyEquals($expectedKey, $key);
615    }
616
617    /**
618     * @group generate-key
619     */
620    public function testGenerateKeyWithUserId()
621    {
622        if (!$this->config['enable-key-generation']) {
623            $this->markTestSkipped(
624                'Key generation tests are disabled. To run key generation '
625                . 'tests, enable them in the test configuration. See the '
626                . 'configuration in \'config.php.dist\' for an exampe.'
627            );
628        }
629
630        // {{{ generate-test@example.com
631        $expectedKey = new Crypt_GPG_Key();
632
633        $userId = new Crypt_GPG_UserId();
634        $userId->setName('Test Keypair');
635        $userId->setEmail('generate-test@example.com');
636        $expectedKey->addUserId($userId);
637
638        $subKey = new Crypt_GPG_SubKey();
639        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_DSA);
640        $subKey->setLength(1024);
641        $subKey->setExpirationDate(0);
642        $subKey->setCanSign(true);
643        $subKey->setCanEncrypt(false);
644        $subKey->setHasPrivate(true);
645        $expectedKey->addSubKey($subKey);
646
647        $subKey = new Crypt_GPG_SubKey();
648        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC);
649        $subKey->setLength(2048);
650        $subKey->setExpirationDate(0);
651        $subKey->setCanSign(false);
652        $subKey->setCanEncrypt(true);
653        $subKey->setHasPrivate(true);
654        $expectedKey->addSubKey($subKey);
655        // }}}
656
657        $key = $this->generator->generateKey(
658            new Crypt_GPG_UserId(
659                'Test Keypair <generate-test@example.com>'
660            )
661        );
662
663        $this->assertKeyEquals($expectedKey, $key);
664    }
665
666    /**
667     * @group generate-key
668     */
669    public function testGenerateKeyWithPassphrase()
670    {
671        if (!$this->config['enable-key-generation']) {
672            $this->markTestSkipped(
673                'Key generation tests are disabled. To run key generation '
674                . 'tests, enable them in the test configuration. See the '
675                . 'configuration in \'config.php.dist\' for an exampe.'
676            );
677        }
678
679        // {{{ generate-test@example.com
680        $expectedKey = new Crypt_GPG_Key();
681
682        $userId = new Crypt_GPG_UserId();
683        $userId->setName('Test Keypair');
684        $userId->setEmail('generate-test@example.com');
685        $expectedKey->addUserId($userId);
686
687        $subKey = new Crypt_GPG_SubKey();
688        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_DSA);
689        $subKey->setLength(1024);
690        $subKey->setExpirationDate(0);
691        $subKey->setCanSign(true);
692        $subKey->setCanEncrypt(false);
693        $subKey->setHasPrivate(true);
694        $expectedKey->addSubKey($subKey);
695
696        $subKey = new Crypt_GPG_SubKey();
697        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC);
698        $subKey->setLength(2048);
699        $subKey->setExpirationDate(0);
700        $subKey->setCanSign(false);
701        $subKey->setCanEncrypt(true);
702        $subKey->setHasPrivate(true);
703        $expectedKey->addSubKey($subKey);
704        // }}}
705
706        $key = $this->generator->setPassphrase('test1')->generateKey(
707            new Crypt_GPG_UserId(
708                'Test Keypair <generate-test@example.com>'
709            )
710        );
711
712        $this->assertKeyEquals($expectedKey, $key);
713    }
714
715    /**
716     * @group generate-key
717     */
718    public function testGenerateKeyWithExpirationDate()
719    {
720        if (!$this->config['enable-key-generation']) {
721            $this->markTestSkipped(
722                'Key generation tests are disabled. To run key generation '
723                . 'tests, enable them in the test configuration. See the '
724                . 'configuration in \'config.php.dist\' for an exampe.'
725            );
726        }
727
728        // {{{ generate-test@example.com
729        $expectedKey = new Crypt_GPG_Key();
730
731        $userId = new Crypt_GPG_UserId();
732        $userId->setName('Test Keypair');
733        $userId->setEmail('generate-test@example.com');
734        $expectedKey->addUserId($userId);
735
736        $subKey = new Crypt_GPG_SubKey();
737        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_DSA);
738        $subKey->setLength(1024);
739        $subKey->setExpirationDate(1999998000); // truncated to day
740        $subKey->setCanSign(true);
741        $subKey->setCanEncrypt(false);
742        $subKey->setHasPrivate(true);
743        $expectedKey->addSubKey($subKey);
744
745        $subKey = new Crypt_GPG_SubKey();
746        $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC);
747        $subKey->setLength(2048);
748        $subKey->setExpirationDate(1999998000); // truncated to day
749        $subKey->setCanSign(false);
750        $subKey->setCanEncrypt(true);
751        $subKey->setHasPrivate(true);
752        $expectedKey->addSubKey($subKey);
753        // }}}
754
755        $key = $this->generator->setExpirationDate(2000000000)->generateKey(
756            new Crypt_GPG_UserId(
757                'Test Keypair <generate-test@example.com>'
758            )
759        );
760
761        // @TODO: I've got difference in expiration dates here
762
763        $this->assertKeyEquals($expectedKey, $key);
764    }
765
766    /**
767     * @group generate-key
768     */
769    public function testGenerateKeyWithInvalidPrimaryKeyAlgorithm()
770    {
771        $this->expectException('Crypt_GPG_InvalidKeyParamsException');
772
773        if (!$this->config['enable-key-generation']) {
774            $this->markTestSkipped(
775                'Key generation tests are disabled. To run key generation '
776                . 'tests, enable them in the test configuration. See the '
777                . 'configuration in \'config.php.dist\' for an exampe.'
778            );
779        }
780
781        $key = $this->generator
782            ->setKeyParams(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN)
783            ->generateKey(
784                new Crypt_GPG_UserId(
785                    'Test Keypair <generate-test@example.com>'
786                )
787            );
788    }
789
790    /**
791     * @group generate-key
792     */
793    public function testGenerateKeyWithInvalidSubKeyAlgorithm()
794    {
795        $this->expectException('Crypt_GPG_InvalidKeyParamsException');
796
797        if (!$this->config['enable-key-generation']) {
798            $this->markTestSkipped(
799                'Key generation tests are disabled. To run key generation '
800                . 'tests, enable them in the test configuration. See the '
801                . 'configuration in \'config.php.dist\' for an exampe.'
802            );
803        }
804
805        $key = $this->generator
806            ->setSubKeyParams(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN)
807            ->generateKey(
808                new Crypt_GPG_UserId(
809                    'Test Keypair <generate-test@example.com>'
810                )
811            );
812    }
813}
814