comparison vendor/pear/crypt_gpg/tests/KeyGeneratorTest.php @ 0:1e000243b222

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:50:29 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1e000243b222
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 */
46 require_once 'TestCase.php';
47
48 /**
49 * The Crypt_GPG class to test
50 */
51 require_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 */
63 class KeyGeneratorTestCase extends Crypt_GPG_TestCase
64 {
65 // helper methods
66 // {{{ assertKeyEquals()
67
68 protected function assertKeyEquals(
69 Crypt_GPG_Key $key1,
70 Crypt_GPG_Key $key2
71 ) {
72 $userIds1 = $key1->getUserIds();
73 $userIds2 = $key2->getUserIds();
74 $userId1 = $userIds1[0];
75 $userId2 = $userIds2[0];
76 $subKeys1 = $key1->getSubKeys();
77 $subKeys2 = $key2->getSubKeys();
78 $subKeyA1 = $subKeys1[0];
79 $subKeyB1 = $subKeys1[1];
80 $subKeyA2 = $subKeys2[0];
81 $subKeyB2 = $subKeys2[1];
82
83 $this->assertEquals(
84 $userId1->getName(),
85 $userId2->getName(),
86 'User id names do not match.'
87 );
88
89 $this->assertEquals(
90 $userId1->getEmail(),
91 $userId2->getEmail(),
92 'User id email addresses do not match.'
93 );
94
95 $this->assertEquals(
96 $userId1->getComment(),
97 $userId2->getComment(),
98 'User id comments do not match.'
99 );
100
101 $this->assertEquals(
102 $subKeyA1->getAlgorithm(),
103 $subKeyA2->getAlgorithm(),
104 'Primary key algorithms do not match.'
105 );
106
107 $this->assertEquals(
108 $subKeyA1->getLength(),
109 $subKeyA2->getLength(),
110 'Primary key lengths do not match.'
111 );
112
113 $this->assertEquals(
114 $subKeyA1->getExpirationDate(),
115 $subKeyA2->getExpirationDate(),
116 'Primary key expiration dates do not match.'
117 );
118
119 $this->assertEquals(
120 $subKeyA1->canSign(),
121 $subKeyA2->canSign(),
122 'Primary key signing abilities do not match.'
123 );
124
125 $this->assertEquals(
126 $subKeyA1->canEncrypt(),
127 $subKeyA2->canEncrypt(),
128 'Primary key encrypting abilities do not match.'
129 );
130
131 $this->assertEquals(
132 $subKeyA1->hasPrivate(),
133 $subKeyA2->hasPrivate(),
134 'Primary key private keys do not match.'
135 );
136
137 $this->assertEquals(
138 $subKeyB1->getAlgorithm(),
139 $subKeyB2->getAlgorithm(),
140 'Secondary key algorithms do not match.'
141 );
142
143 $this->assertEquals(
144 $subKeyB1->getLength(),
145 $subKeyB2->getLength(),
146 'Secondary key lengths do not match.'
147 );
148
149 $this->assertEquals(
150 $subKeyB1->getExpirationDate(),
151 $subKeyB2->getExpirationDate(),
152 'Secondary key expiration dates do not match.'
153 );
154
155 $this->assertEquals(
156 $subKeyB1->canSign(),
157 $subKeyB2->canSign(),
158 'Secondary key signing abilities do not match.'
159 );
160
161 $this->assertEquals(
162 $subKeyB1->canEncrypt(),
163 $subKeyB2->canEncrypt(),
164 'Secondary key encrypting abilities do not match.'
165 );
166
167 $this->assertEquals(
168 $subKeyB1->hasPrivate(),
169 $subKeyB2->hasPrivate(),
170 'Secondary key private keys do not match.'
171 );
172 }
173
174 // }}}
175 // {{{ setUp()
176
177 public function setUp()
178 {
179 parent::setUp();
180 $this->generator = new Crypt_GPG_KeyGenerator($this->getOptions());
181 }
182
183 // }}}
184
185 // mutators
186 // {{{ testSetExpirationDate_zero()
187
188 /**
189 * @group mutators
190 */
191 public function testSetExpirationDate_zero()
192 {
193 $expectedDate = 0;
194 $this->generator->setExpirationDate(0);
195
196 $this->assertAttributeEquals(
197 $expectedDate,
198 'expirationDate',
199 $this->generator,
200 'Setting expiration date to zero failed.'
201 );
202 }
203
204 // }}}
205 // {{{ testSetExpirationDate_integer()
206
207 /**
208 * @group mutators
209 */
210 public function testSetExpirationDate_integer()
211 {
212 $expectedDate = 2000000000;
213 $this->generator->setExpirationDate(2000000000);
214
215 $this->assertAttributeEquals(
216 $expectedDate,
217 'expirationDate',
218 $this->generator,
219 'Setting expiration date by integer failed.'
220 );
221 }
222
223 // }}}
224 // {{{ testSetExpirationDate_string()
225
226 /**
227 * @group mutators
228 */
229 public function testSetExpirationDate_string()
230 {
231 date_default_timezone_set('UTC');
232
233 $expectedDate = 2000000000;
234 $this->generator->setExpirationDate('2033-05-18T03:33:20');
235
236 $this->assertAttributeEquals(
237 $expectedDate,
238 'expirationDate',
239 $this->generator,
240 'Setting expiration date by string failed.'
241 );
242 }
243
244 // }}}
245 // {{{ testSetExpirationDate_invalid_format()
246
247 /**
248 * @group mutators
249 * @expectedException InvalidArgumentException
250 */
251 public function testSetExpirationDate_invalid_format()
252 {
253 date_default_timezone_set('UTC');
254
255 $this->generator->setExpirationDate('this is not a date');
256 }
257
258 // }}}
259 // {{{ testSetExpirationDate_too_early_date()
260
261 /**
262 * @group mutators
263 * @expectedException InvalidArgumentException
264 */
265 public function testSetExpirationDate_too_early_date()
266 {
267 $this->generator->setExpirationDate(1301088055);
268 }
269
270 // }}}
271 // {{{ testSetExpirationDate_today()
272
273 /**
274 * @group mutators
275 * @expectedException InvalidArgumentException
276 */
277 public function testSetExpirationDate_today()
278 {
279 $this->generator->setExpirationDate(time());
280 }
281
282 // }}}
283 // {{{ testSetExpirationDate_too_late_date()
284
285 /**
286 * @group mutators
287 * @expectedException InvalidArgumentException
288 */
289 public function testSetExpirationDate_too_late_date()
290 {
291 $this->generator->setExpirationDate(2147483648);
292 }
293
294 // }}}
295 // {{{ testSetPassphrase()
296
297 /**
298 * @group mutators
299 */
300 public function testSetPassphrase()
301 {
302 $expectedPassphrase = 'test1';
303 $this->generator->setPassphrase('test1');
304
305 $this->assertAttributeEquals(
306 $expectedPassphrase,
307 'passphrase',
308 $this->generator,
309 'Setting passphrase failed.'
310 );
311 }
312
313 // }}}
314 // {{{ testSetKeyParams_algorithm()
315
316 /**
317 * @group mutators
318 */
319 public function testSetKeyParams_algorithm()
320 {
321 $expectedAlgorithm = Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN;
322 $expectedSize = 1024;
323 $expectedUsage = Crypt_GPG_SubKey::USAGE_SIGN
324 | Crypt_GPG_SubKey::USAGE_CERTIFY;
325
326 $this->generator->setKeyParams(
327 Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN
328 );
329
330 $this->assertAttributeEquals(
331 $expectedAlgorithm,
332 'keyAlgorithm',
333 $this->generator,
334 'Setting key algorithm failed.'
335 );
336
337 $this->assertAttributeEquals(
338 $expectedSize,
339 'keySize',
340 $this->generator,
341 'Setting key algorithm changed key size.'
342 );
343
344 $this->assertAttributeEquals(
345 $expectedUsage,
346 'keyUsage',
347 $this->generator,
348 'Setting key algorithm changed key usage.'
349 );
350 }
351
352 // }}}
353 // {{{ testSetKeyParams_algorithm_and_size()
354
355 /**
356 * @group mutators
357 */
358 public function testSetKeyParams_algorithm_and_size()
359 {
360 $expectedAlgorithm = Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN;
361 $expectedSize = 512;
362 $expectedUsage = Crypt_GPG_SubKey::USAGE_SIGN
363 | Crypt_GPG_SubKey::USAGE_CERTIFY;
364
365 $this->generator->setKeyParams(
366 Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN,
367 512
368 );
369
370 $this->assertAttributeEquals(
371 $expectedAlgorithm,
372 'keyAlgorithm',
373 $this->generator,
374 'Setting key algorithm failed.'
375 );
376
377 $this->assertAttributeEquals(
378 $expectedSize,
379 'keySize',
380 $this->generator,
381 'Setting key size failed.'
382 );
383
384 $this->assertAttributeEquals(
385 $expectedUsage,
386 'keyUsage',
387 $this->generator,
388 'Setting key algorithm and size changed key usage.'
389 );
390 }
391
392 // }}}
393 // {{{ testSetKeyParams_algorithm_size_and_usage()
394
395 /**
396 * @group mutators
397 */
398 public function testSetKeyParams_algorithm_size_and_usage()
399 {
400 $expectedAlgorithm = Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN;
401 $expectedSize = 512;
402 $expectedUsage = Crypt_GPG_SubKey::USAGE_SIGN
403 | Crypt_GPG_SubKey::USAGE_CERTIFY
404 | Crypt_GPG_SubKey::USAGE_ENCRYPT;
405
406 $this->generator->setKeyParams(
407 Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN,
408 512,
409 Crypt_GPG_SubKey::USAGE_SIGN
410 | Crypt_GPG_SubKey::USAGE_CERTIFY
411 | Crypt_GPG_SubKey::USAGE_ENCRYPT
412 );
413
414 $this->assertAttributeEquals(
415 $expectedAlgorithm,
416 'keyAlgorithm',
417 $this->generator,
418 'Setting key algorithm failed.'
419 );
420
421 $this->assertAttributeEquals(
422 $expectedSize,
423 'keySize',
424 $this->generator,
425 'Setting key size failed.'
426 );
427
428 $this->assertAttributeEquals(
429 $expectedUsage,
430 'keyUsage',
431 $this->generator,
432 'Setting key usage failed.'
433 );
434 }
435
436 // }}}
437 // {{{ testSetKeyParams_invalid_algorithm()
438
439 /**
440 * @group mutators
441 * @expectedException Crypt_GPG_InvalidKeyParamsException
442 */
443 public function testSetKeyParams_invalid_algorithm()
444 {
445 $this->generator->setKeyParams(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC);
446 }
447
448 // }}}
449 // {{{ testSetKeyParams_invalid_dsa_usage()
450
451 /**
452 * @group mutators
453 * @expectedException Crypt_GPG_InvalidKeyParamsException
454 */
455 public function testSetKeyParams_invalid_dsa_usage()
456 {
457 $this->generator->setKeyParams(
458 Crypt_GPG_SubKey::ALGORITHM_DSA,
459 2048,
460 Crypt_GPG_SubKey::USAGE_ENCRYPT | Crypt_GPG_SubKey::USAGE_CERTIFY
461 );
462 }
463
464 // }}}
465 // {{{ testSetSubKeyParams_algorithm()
466
467 /**
468 * @group mutators
469 */
470 public function testSetSubKeyParams_algorithm()
471 {
472 $expectedAlgorithm = Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN;
473 $expectedSize = 2048;
474 $expectedUsage = Crypt_GPG_SubKey::USAGE_ENCRYPT;
475
476 $this->generator->setSubKeyParams(
477 Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN
478 );
479
480 $this->assertAttributeEquals(
481 $expectedAlgorithm,
482 'subKeyAlgorithm',
483 $this->generator,
484 'Setting sub-key algorithm failed.'
485 );
486
487 $this->assertAttributeEquals(
488 $expectedSize,
489 'subKeySize',
490 $this->generator,
491 'Setting sub-key algorithm changed key size.'
492 );
493
494 $this->assertAttributeEquals(
495 $expectedUsage,
496 'subKeyUsage',
497 $this->generator,
498 'Setting sub-key algorithm changed key usage.'
499 );
500 }
501
502 // }}}
503 // {{{ testSetSubKeyParams_algorithm_and_size()
504
505 /**
506 * @group mutators
507 */
508 public function testSetSubKeyParams_algorithm_and_size()
509 {
510 $expectedAlgorithm = Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN;
511 $expectedSize = 1024;
512 $expectedUsage = Crypt_GPG_SubKey::USAGE_ENCRYPT;
513
514 $this->generator->setSubKeyParams(
515 Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN,
516 1024
517 );
518
519 $this->assertAttributeEquals(
520 $expectedAlgorithm,
521 'subKeyAlgorithm',
522 $this->generator,
523 'Setting sub-key algorithm failed.'
524 );
525
526 $this->assertAttributeEquals(
527 $expectedSize,
528 'subKeySize',
529 $this->generator,
530 'Setting sub-key size failed.'
531 );
532
533 $this->assertAttributeEquals(
534 $expectedUsage,
535 'subKeyUsage',
536 $this->generator,
537 'Setting sub-key algorithm and size changed key usage.'
538 );
539 }
540
541 // }}}
542 // {{{ testSetSubKeyParams_algorithm_size_and_usage()
543
544 /**
545 * @group mutators
546 */
547 public function testSetSubKeyParams_algorithm_size_and_usage()
548 {
549 $expectedAlgorithm = Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN;
550 $expectedSize = 1024;
551 $expectedUsage = Crypt_GPG_SubKey::USAGE_SIGN
552 | Crypt_GPG_SubKey::USAGE_ENCRYPT;
553
554 $this->generator->setSubKeyParams(
555 Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN,
556 1024,
557 Crypt_GPG_SubKey::USAGE_SIGN
558 | Crypt_GPG_SubKey::USAGE_ENCRYPT
559 );
560
561 $this->assertAttributeEquals(
562 $expectedAlgorithm,
563 'subKeyAlgorithm',
564 $this->generator,
565 'Setting sub-key algorithm failed.'
566 );
567
568 $this->assertAttributeEquals(
569 $expectedSize,
570 'subKeySize',
571 $this->generator,
572 'Setting sub-key size failed.'
573 );
574
575 $this->assertAttributeEquals(
576 $expectedUsage,
577 'subKeyUsage',
578 $this->generator,
579 'Setting sub-key usage failed.'
580 );
581 }
582
583 // }}}
584 // {{{ testSetSubKeyParams_invalid_elgamal_usage()
585
586 /**
587 * @group mutators
588 * @expectedException Crypt_GPG_InvalidKeyParamsException
589 */
590 public function testSetSubKeyParams_invalid_elgamal_usage()
591 {
592 $this->generator->setSubKeyParams(
593 Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC,
594 2048,
595 Crypt_GPG_SubKey::USAGE_SIGN | Crypt_GPG_SubKey::USAGE_ENCRYPT
596 );
597 }
598
599 // }}}
600 // {{{ testSetSubKeyParams_invalid_dsa_usage()
601
602 /**
603 * @group mutators
604 * @expectedException Crypt_GPG_InvalidKeyParamsException
605 */
606 public function testSetSubKeyParams_invalid_dsa_usage()
607 {
608 $this->generator->setSubKeyParams(
609 Crypt_GPG_SubKey::ALGORITHM_DSA,
610 2048,
611 Crypt_GPG_SubKey::USAGE_SIGN | Crypt_GPG_SubKey::USAGE_ENCRYPT
612 );
613 }
614
615 // }}}
616
617 // generate key tests
618 // {{{ testGenerateKeyWithName()
619
620 /**
621 * @group generate-key
622 */
623 public function testGenerateKeyWithName()
624 {
625 if (!$this->config['enable-key-generation']) {
626 $this->markTestSkipped(
627 'Key generation tests are disabled. To run key generation '
628 . 'tests, enable them in the test configuration. See the '
629 . 'configuration in \'config.php.dist\' for an exampe.'
630 );
631 }
632
633 // {{{ generate-test@example.com
634 $expectedKey = new Crypt_GPG_Key();
635
636 $userId = new Crypt_GPG_UserId();
637 $userId->setName('Test Keypair');
638 $expectedKey->addUserId($userId);
639
640 $subKey = new Crypt_GPG_SubKey();
641 $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_DSA);
642 $subKey->setLength(1024);
643 $subKey->setExpirationDate(0);
644 $subKey->setCanSign(true);
645 $subKey->setCanEncrypt(false);
646 $subKey->setHasPrivate(true);
647 $expectedKey->addSubKey($subKey);
648
649 $subKey = new Crypt_GPG_SubKey();
650 $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC);
651 $subKey->setLength(2048);
652 $subKey->setExpirationDate(0);
653 $subKey->setCanSign(false);
654 $subKey->setCanEncrypt(true);
655 $subKey->setHasPrivate(true);
656 $expectedKey->addSubKey($subKey);
657 // }}}
658
659 $key = $this->generator->generateKey('Test Keypair');
660
661 $this->assertKeyEquals($expectedKey, $key);
662 }
663
664 // }}}
665 // {{{ testGenerateKeyWithNameAndEmail()
666
667 /**
668 * @group generate-key
669 */
670 public function testGenerateKeyWithNameAndEmail()
671 {
672 if (!$this->config['enable-key-generation']) {
673 $this->markTestSkipped(
674 'Key generation tests are disabled. To run key generation '
675 . 'tests, enable them in the test configuration. See the '
676 . 'configuration in \'config.php.dist\' for an exampe.'
677 );
678 }
679
680 // {{{ generate-test@example.com
681 $expectedKey = new Crypt_GPG_Key();
682
683 $userId = new Crypt_GPG_UserId();
684 $userId->setName('Test Keypair');
685 $userId->setEmail('generate-test@example.com');
686 $expectedKey->addUserId($userId);
687
688 $subKey = new Crypt_GPG_SubKey();
689 $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_DSA);
690 $subKey->setLength(1024);
691 $subKey->setExpirationDate(0);
692 $subKey->setCanSign(true);
693 $subKey->setCanEncrypt(false);
694 $subKey->setHasPrivate(true);
695 $expectedKey->addSubKey($subKey);
696
697 $subKey = new Crypt_GPG_SubKey();
698 $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC);
699 $subKey->setLength(2048);
700 $subKey->setExpirationDate(0);
701 $subKey->setCanSign(false);
702 $subKey->setCanEncrypt(true);
703 $subKey->setHasPrivate(true);
704 $expectedKey->addSubKey($subKey);
705 // }}}
706
707 $key = $this->generator->generateKey(
708 'Test Keypair',
709 'generate-test@example.com'
710 );
711
712 $this->assertKeyEquals($expectedKey, $key);
713 }
714
715 // }}}
716 // {{{ testGenerateKeyWithNameEmailAndComment()
717
718 /**
719 * @group generate-key
720 */
721 public function testGenerateKeyWithNameEmailAndComment()
722 {
723 if (!$this->config['enable-key-generation']) {
724 $this->markTestSkipped(
725 'Key generation tests are disabled. To run key generation '
726 . 'tests, enable them in the test configuration. See the '
727 . 'configuration in \'config.php.dist\' for an exampe.'
728 );
729 }
730
731 // {{{ generate-test@example.com
732 $expectedKey = new Crypt_GPG_Key();
733
734 $userId = new Crypt_GPG_UserId();
735 $userId->setName('Test Keypair');
736 $userId->setComment('do not use this key');
737 $userId->setEmail('generate-test@example.com');
738 $expectedKey->addUserId($userId);
739
740 $subKey = new Crypt_GPG_SubKey();
741 $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_DSA);
742 $subKey->setLength(1024);
743 $subKey->setExpirationDate(0);
744 $subKey->setCanSign(true);
745 $subKey->setCanEncrypt(false);
746 $subKey->setHasPrivate(true);
747 $expectedKey->addSubKey($subKey);
748
749 $subKey = new Crypt_GPG_SubKey();
750 $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC);
751 $subKey->setLength(2048);
752 $subKey->setExpirationDate(0);
753 $subKey->setCanSign(false);
754 $subKey->setCanEncrypt(true);
755 $subKey->setHasPrivate(true);
756 $expectedKey->addSubKey($subKey);
757 // }}}
758
759 $key = $this->generator->generateKey(
760 'Test Keypair',
761 'generate-test@example.com',
762 'do not use this key'
763 );
764
765 $this->assertKeyEquals($expectedKey, $key);
766 }
767
768 // }}}
769 // {{{ testGenerateKeyWithUserId()
770
771 /**
772 * @group generate-key
773 */
774 public function testGenerateKeyWithUserId()
775 {
776 if (!$this->config['enable-key-generation']) {
777 $this->markTestSkipped(
778 'Key generation tests are disabled. To run key generation '
779 . 'tests, enable them in the test configuration. See the '
780 . 'configuration in \'config.php.dist\' for an exampe.'
781 );
782 }
783
784 // {{{ generate-test@example.com
785 $expectedKey = new Crypt_GPG_Key();
786
787 $userId = new Crypt_GPG_UserId();
788 $userId->setName('Test Keypair');
789 $userId->setEmail('generate-test@example.com');
790 $expectedKey->addUserId($userId);
791
792 $subKey = new Crypt_GPG_SubKey();
793 $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_DSA);
794 $subKey->setLength(1024);
795 $subKey->setExpirationDate(0);
796 $subKey->setCanSign(true);
797 $subKey->setCanEncrypt(false);
798 $subKey->setHasPrivate(true);
799 $expectedKey->addSubKey($subKey);
800
801 $subKey = new Crypt_GPG_SubKey();
802 $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC);
803 $subKey->setLength(2048);
804 $subKey->setExpirationDate(0);
805 $subKey->setCanSign(false);
806 $subKey->setCanEncrypt(true);
807 $subKey->setHasPrivate(true);
808 $expectedKey->addSubKey($subKey);
809 // }}}
810
811 $key = $this->generator->generateKey(
812 new Crypt_GPG_UserId(
813 'Test Keypair <generate-test@example.com>'
814 )
815 );
816
817 $this->assertKeyEquals($expectedKey, $key);
818 }
819
820 // }}}
821 // {{{ testGenerateKeyWithPassphrase()
822
823 /**
824 * @group generate-key
825 */
826 public function testGenerateKeyWithPassphrase()
827 {
828 if (!$this->config['enable-key-generation']) {
829 $this->markTestSkipped(
830 'Key generation tests are disabled. To run key generation '
831 . 'tests, enable them in the test configuration. See the '
832 . 'configuration in \'config.php.dist\' for an exampe.'
833 );
834 }
835
836 // {{{ generate-test@example.com
837 $expectedKey = new Crypt_GPG_Key();
838
839 $userId = new Crypt_GPG_UserId();
840 $userId->setName('Test Keypair');
841 $userId->setEmail('generate-test@example.com');
842 $expectedKey->addUserId($userId);
843
844 $subKey = new Crypt_GPG_SubKey();
845 $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_DSA);
846 $subKey->setLength(1024);
847 $subKey->setExpirationDate(0);
848 $subKey->setCanSign(true);
849 $subKey->setCanEncrypt(false);
850 $subKey->setHasPrivate(true);
851 $expectedKey->addSubKey($subKey);
852
853 $subKey = new Crypt_GPG_SubKey();
854 $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC);
855 $subKey->setLength(2048);
856 $subKey->setExpirationDate(0);
857 $subKey->setCanSign(false);
858 $subKey->setCanEncrypt(true);
859 $subKey->setHasPrivate(true);
860 $expectedKey->addSubKey($subKey);
861 // }}}
862
863 $key = $this->generator->setPassphrase('test1')->generateKey(
864 new Crypt_GPG_UserId(
865 'Test Keypair <generate-test@example.com>'
866 )
867 );
868
869 $this->assertKeyEquals($expectedKey, $key);
870 }
871
872 // }}}
873 // {{{ testGenerateKeyWithExpirationDate()
874
875 /**
876 * @group generate-key
877 */
878 public function testGenerateKeyWithExpirationDate()
879 {
880 if (!$this->config['enable-key-generation']) {
881 $this->markTestSkipped(
882 'Key generation tests are disabled. To run key generation '
883 . 'tests, enable them in the test configuration. See the '
884 . 'configuration in \'config.php.dist\' for an exampe.'
885 );
886 }
887
888 // {{{ generate-test@example.com
889 $expectedKey = new Crypt_GPG_Key();
890
891 $userId = new Crypt_GPG_UserId();
892 $userId->setName('Test Keypair');
893 $userId->setEmail('generate-test@example.com');
894 $expectedKey->addUserId($userId);
895
896 $subKey = new Crypt_GPG_SubKey();
897 $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_DSA);
898 $subKey->setLength(1024);
899 $subKey->setExpirationDate(1999998000); // truncated to day
900 $subKey->setCanSign(true);
901 $subKey->setCanEncrypt(false);
902 $subKey->setHasPrivate(true);
903 $expectedKey->addSubKey($subKey);
904
905 $subKey = new Crypt_GPG_SubKey();
906 $subKey->setAlgorithm(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC);
907 $subKey->setLength(2048);
908 $subKey->setExpirationDate(1999998000); // truncated to day
909 $subKey->setCanSign(false);
910 $subKey->setCanEncrypt(true);
911 $subKey->setHasPrivate(true);
912 $expectedKey->addSubKey($subKey);
913 // }}}
914
915 $key = $this->generator->setExpirationDate(2000000000)->generateKey(
916 new Crypt_GPG_UserId(
917 'Test Keypair <generate-test@example.com>'
918 )
919 );
920
921 // @TODO: I've got difference in expiration dates here
922
923 $this->assertKeyEquals($expectedKey, $key);
924 }
925
926 // }}}
927 // {{{ testGenerateKeyWithInvalidPrimaryKeyAlgorithm()
928
929 /**
930 * @group generate-key
931 * @expectedException Crypt_GPG_InvalidKeyParamsException
932 */
933 public function testGenerateKeyWithInvalidPrimaryKeyAlgorithm()
934 {
935 if (!$this->config['enable-key-generation']) {
936 $this->markTestSkipped(
937 'Key generation tests are disabled. To run key generation '
938 . 'tests, enable them in the test configuration. See the '
939 . 'configuration in \'config.php.dist\' for an exampe.'
940 );
941 }
942
943 $key = $this->generator
944 ->setKeyParams(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN)
945 ->generateKey(
946 new Crypt_GPG_UserId(
947 'Test Keypair <generate-test@example.com>'
948 )
949 );
950 }
951
952 // }}}
953 // {{{ testGenerateKeyWithInvalidSubKeyAlgorithm()
954
955 /**
956 * @group generate-key
957 * @expectedException Crypt_GPG_InvalidKeyParamsException
958 */
959 public function testGenerateKeyWithInvalidSubKeyAlgorithm()
960 {
961 if (!$this->config['enable-key-generation']) {
962 $this->markTestSkipped(
963 'Key generation tests are disabled. To run key generation '
964 . 'tests, enable them in the test configuration. See the '
965 . 'configuration in \'config.php.dist\' for an exampe.'
966 );
967 }
968
969 $key = $this->generator
970 ->setSubKeyParams(Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC_SGN)
971 ->generateKey(
972 new Crypt_GPG_UserId(
973 'Test Keypair <generate-test@example.com>'
974 )
975 );
976 }
977
978 // }}}
979 }
980
981 ?>