comparison vendor/pear/crypt_gpg/tests/KeyTest.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 class test cases 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 KeyTestCase
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 2008-2010 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 */
46 require_once 'TestCase.php';
47
48 /**
49 * Key class.
50 */
51 require_once 'Crypt/GPG/Key.php';
52
53 /**
54 * User id class.
55 */
56 require_once 'Crypt/GPG/UserId.php';
57
58 /**
59 * Sub-key class.
60 */
61 require_once 'Crypt/GPG/SubKey.php';
62
63 /**
64 * Key class tests for Crypt_GPG.
65 *
66 * @category Encryption
67 * @package Crypt_GPG
68 * @author Michael Gauthier <mike@silverorange.com>
69 * @copyright 2008-2010 silverorange
70 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
71 * @link http://pear.php.net/package/Crypt_GPG
72 */
73 class KeyTestCase extends Crypt_GPG_TestCase
74 {
75 // accessors
76 // {{{ testGetSubKeys()
77
78 /**
79 * @group accessors
80 */
81 public function testGetSubKeys()
82 {
83 $key = new Crypt_GPG_Key();
84
85 $firstSubKey = new Crypt_GPG_SubKey(array(
86 'id' => 'C097D9EC94C06363',
87 'algorithm' => Crypt_GPG_SubKey::ALGORITHM_DSA,
88 'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
89 'length' => 1024,
90 'creation' => 1221785805,
91 'expiration' => 0,
92 'canSign' => true,
93 'canEncrypt' => false,
94 'hasPrivate' => true
95 ));
96
97 $key->addSubKey($firstSubKey);
98
99 $secondSubKey = new Crypt_GPG_SubKey(array(
100 'id' => '9F93F9116728EF12',
101 'algorithm' => Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC,
102 'fingerprint' => 'C9C65B3BBF040E40D0EA27B79F93F9116728EF12',
103 'length' => 2048,
104 'creation' => 1221785821,
105 'expiration' => 0,
106 'canSign' => false,
107 'canEncrypt' => true,
108 'hasPrivate' => true
109 ));
110
111 $key->addSubKey($secondSubKey);
112
113 $subKeys = $key->getSubKeys();
114 $this->assertTrue(is_array($subKeys),
115 'Failed to assert returned sub-keys is an array.');
116
117 $this->assertEquals(2, count($subKeys),
118 'Failed to assert number of returned sub-keys is the same as ' .
119 'the number of sub-keys added.');
120
121 $this->assertContainsOnly('Crypt_GPG_SubKey', $subKeys, false,
122 'Failed to assert all returned sub-keys are Crypt_GPG_SubKey ' .
123 'objects.');
124
125 $this->assertArrayHasKey(0, $subKeys);
126 $this->assertEquals($subKeys[0], $firstSubKey,
127 'Failed to assert the first sub-key is the same as the first ' .
128 'added sub-key.');
129
130 $this->assertArrayHasKey(1, $subKeys);
131 $this->assertEquals($subKeys[1], $secondSubKey,
132 'Failed to assert the second sub-key is the same as the second ' .
133 'added sub-key.');
134 }
135
136 // }}}
137 // {{{ testGetUserIds()
138
139 /**
140 * @group accessors
141 */
142 public function testGetUserIds()
143 {
144 $key = new Crypt_GPG_Key();
145
146 $firstUserId = new Crypt_GPG_UserId(array(
147 'name' => 'Alice',
148 'comment' => 'shipping',
149 'email' => 'alice@example.com'
150 ));
151
152 $key->addUserId($firstUserId);
153
154 $secondUserId = new Crypt_GPG_UserId(array(
155 'name' => 'Bob',
156 'comment' => 'receiving',
157 'email' => 'bob@example.com'
158 ));
159
160 $key->addUserId($secondUserId);
161
162 $userIds = $key->getUserIds();
163 $this->assertTrue(is_array($userIds),
164 'Failed to assert returned user ids is an array.');
165
166 $this->assertEquals(2, count($userIds),
167 'Failed to assert number of returned user ids is the same as ' .
168 'the number of user ids added.');
169
170 $this->assertContainsOnly('Crypt_GPG_UserId', $userIds, false,
171 'Failed to assert all returned user ids are Crypt_GPG_UserId ' .
172 'objects.');
173
174 $this->assertArrayHasKey(0, $userIds);
175 $this->assertEquals($userIds[0], $firstUserId,
176 'Failed to assert the first user id is the same as the first ' .
177 'added user id.');
178
179 $this->assertArrayHasKey(1, $userIds);
180 $this->assertEquals($userIds[1], $secondUserId,
181 'Failed to assert the second user id is the same as the second ' .
182 'added user id.');
183 }
184
185 // }}}
186 // {{{ testGetPrimaryKey()
187
188 /**
189 * @group accessors
190 */
191 public function testGetPrimaryKey()
192 {
193 $key = new Crypt_GPG_Key();
194
195 $firstSubKey = new Crypt_GPG_SubKey(array(
196 'id' => 'C097D9EC94C06363',
197 'algorithm' => Crypt_GPG_SubKey::ALGORITHM_DSA,
198 'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
199 'length' => 1024,
200 'creation' => 1221785805,
201 'expiration' => 0,
202 'canSign' => true,
203 'canEncrypt' => false,
204 'hasPrivate' => true
205 ));
206
207 $key->addSubKey($firstSubKey);
208
209 $secondSubKey = new Crypt_GPG_SubKey(array(
210 'id' => '9F93F9116728EF12',
211 'algorithm' => Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC,
212 'fingerprint' => 'C9C65B3BBF040E40D0EA27B79F93F9116728EF12',
213 'length' => 2048,
214 'creation' => 1221785821,
215 'expiration' => 0,
216 'canSign' => false,
217 'canEncrypt' => true,
218 'hasPrivate' => true
219 ));
220
221 $key->addSubKey($secondSubKey);
222
223 $primaryKey = $key->getPrimaryKey();
224
225 $this->assertEquals($firstSubKey, $primaryKey,
226 'Failed to assert the primary key is the same as the first added ' .
227 'sub-key.');
228 }
229
230 // }}}
231 // {{{ testCanSign_none()
232
233 /**
234 * @group accessors
235 */
236 public function testCanSign_none()
237 {
238 $key = new Crypt_GPG_Key();
239
240 $subKey = new Crypt_GPG_SubKey(array('canSign' => false));
241 $key->addSubKey($subKey);
242
243 $subKey = new Crypt_GPG_SubKey(array('canSign' => false));
244 $key->addSubKey($subKey);
245
246 $subKey = new Crypt_GPG_SubKey(array('canSign' => false));
247 $key->addSubKey($subKey);
248
249 $this->assertFalse($key->canSign());
250 }
251
252 // }}}
253 // {{{ testCanSign_one()
254
255 /**
256 * @group accessors
257 */
258 public function testCanSign_one()
259 {
260 $key = new Crypt_GPG_Key();
261
262 $subKey = new Crypt_GPG_SubKey(array('canSign' => false));
263 $key->addSubKey($subKey);
264
265 $subKey = new Crypt_GPG_SubKey(array('canSign' => false));
266 $key->addSubKey($subKey);
267
268 $subKey = new Crypt_GPG_SubKey(array('canSign' => true));
269 $key->addSubKey($subKey);
270
271 $this->assertTrue($key->canSign());
272 }
273
274 // }}}
275 // {{{ testCanSign_all()
276
277 /**
278 * @group accessors
279 */
280 public function testCanSign_all()
281 {
282 $key = new Crypt_GPG_Key();
283
284 $subKey = new Crypt_GPG_SubKey(array('canSign' => true));
285 $key->addSubKey($subKey);
286
287 $subKey = new Crypt_GPG_SubKey(array('canSign' => true));
288 $key->addSubKey($subKey);
289
290 $subKey = new Crypt_GPG_SubKey(array('canSign' => true));
291 $key->addSubKey($subKey);
292
293 $this->assertTrue($key->canSign());
294 }
295
296 // }}}
297 // {{{ testCanEncrypt_none()
298
299 /**
300 * @group accessors
301 */
302 public function testCanEncrypt_none()
303 {
304 $key = new Crypt_GPG_Key();
305
306 $subKey = new Crypt_GPG_SubKey(array('canEncrypt' => false));
307 $key->addSubKey($subKey);
308
309 $subKey = new Crypt_GPG_SubKey(array('canEncrypt' => false));
310 $key->addSubKey($subKey);
311
312 $subKey = new Crypt_GPG_SubKey(array('canEncrypt' => false));
313 $key->addSubKey($subKey);
314
315 $this->assertFalse($key->canEncrypt());
316 }
317
318 // }}}
319 // {{{ testCanEncrypt_one()
320
321 /**
322 * @group accessors
323 */
324 public function testCanEncrypt_one()
325 {
326 $key = new Crypt_GPG_Key();
327
328 $subKey = new Crypt_GPG_SubKey(array('canEncrypt' => false));
329 $key->addSubKey($subKey);
330
331 $subKey = new Crypt_GPG_SubKey(array('canEncrypt' => false));
332 $key->addSubKey($subKey);
333
334 $subKey = new Crypt_GPG_SubKey(array('canEncrypt' => true));
335 $key->addSubKey($subKey);
336
337 $this->assertTrue($key->canEncrypt());
338 }
339
340 // }}}
341 // {{{ testCanEncrypt_all()
342
343 /**
344 * @group accessors
345 */
346 public function testCanEncrypt_all()
347 {
348 $key = new Crypt_GPG_Key();
349
350 $subKey = new Crypt_GPG_SubKey(array('canEncrypt' => true));
351 $key->addSubKey($subKey);
352
353 $subKey = new Crypt_GPG_SubKey(array('canEncrypt' => true));
354 $key->addSubKey($subKey);
355
356 $subKey = new Crypt_GPG_SubKey(array('canEncrypt' => true));
357 $key->addSubKey($subKey);
358
359 $this->assertTrue($key->canEncrypt());
360 }
361
362 // }}}
363 // {{{ test__toString()
364
365 /**
366 * @group accessors
367 */
368 public function test__toString()
369 {
370 $key = new Crypt_GPG_Key();
371
372 $firstSubKey = new Crypt_GPG_SubKey(array(
373 'id' => 'C097D9EC94C06363',
374 'algorithm' => Crypt_GPG_SubKey::ALGORITHM_DSA,
375 'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
376 'length' => 1024,
377 'creation' => 1221785805,
378 'expiration' => 0,
379 'canSign' => true,
380 'canEncrypt' => false,
381 'hasPrivate' => true
382 ));
383
384 $this->assertSame((string) $key, '');
385
386 $key->addSubKey($firstSubKey);
387
388 $this->assertSame((string) $key, $firstSubKey->getId());
389 }
390
391 // }}}
392
393 // mutators
394 // {{{ testAddSubKey()
395
396 /**
397 * @group mutators
398 */
399 public function testAddSubKey()
400 {
401 $key = new Crypt_GPG_Key();
402
403 $subKeys = $key->getSubKeys();
404 $this->assertTrue(is_array($subKeys),
405 'Failed to assert returned sub-keys is an array.');
406
407 $this->assertEquals(0, count($subKeys),
408 'Failed to assert there are no sub-keys.');
409
410 // add first sub-key
411 $firstSubKey = new Crypt_GPG_SubKey(array(
412 'id' => 'C097D9EC94C06363',
413 'algorithm' => Crypt_GPG_SubKey::ALGORITHM_DSA,
414 'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
415 'length' => 1024,
416 'creation' => 1221785805,
417 'expiration' => 0,
418 'canSign' => true,
419 'canEncrypt' => false,
420 'hasPrivate' => true
421 ));
422
423 $key->addSubKey($firstSubKey);
424
425 $subKeys = $key->getSubKeys();
426 $this->assertTrue(is_array($subKeys),
427 'Failed to assert returned sub-keys is an array.');
428
429 $this->assertEquals(1, count($subKeys),
430 'Failed to assert number of returned sub-keys is the same as ' .
431 'the number of sub-keys added.');
432
433 $this->assertContainsOnly('Crypt_GPG_SubKey', $subKeys, false,
434 'Failed to assert all returned sub-keys are Crypt_GPG_SubKey ' .
435 'objects.');
436
437 $this->assertArrayHasKey(0, $subKeys);
438 $this->assertEquals($subKeys[0], $firstSubKey,
439 'Failed to assert the first sub-key is the same as the first ' .
440 'added sub-key.');
441
442 // add second sub-key
443 $secondSubKey = new Crypt_GPG_SubKey(array(
444 'id' => '9F93F9116728EF12',
445 'algorithm' => Crypt_GPG_SubKey::ALGORITHM_ELGAMAL_ENC,
446 'fingerprint' => 'C9C65B3BBF040E40D0EA27B79F93F9116728EF12',
447 'length' => 2048,
448 'creation' => 1221785821,
449 'expiration' => 0,
450 'canSign' => false,
451 'canEncrypt' => true,
452 'hasPrivate' => true
453 ));
454
455 $key->addSubKey($secondSubKey);
456
457 $subKeys = $key->getSubKeys();
458 $this->assertTrue(is_array($subKeys),
459 'Failed to assert returned sub-keys is an array.');
460
461 $this->assertEquals(2, count($subKeys),
462 'Failed to assert number of returned sub-keys is the same as ' .
463 'the number of sub-keys added.');
464
465 $this->assertContainsOnly('Crypt_GPG_SubKey', $subKeys, false,
466 'Failed to assert all returned sub-keys are Crypt_GPG_SubKey ' .
467 'objects.');
468
469 $this->assertArrayHasKey(0, $subKeys);
470 $this->assertEquals($subKeys[0], $firstSubKey,
471 'Failed to assert the first sub-key is the same as the first ' .
472 'added sub-key.');
473
474 $this->assertArrayHasKey(1, $subKeys);
475 $this->assertEquals($subKeys[1], $secondSubKey,
476 'Failed to assert the second sub-key is the same as the second ' .
477 'added sub-key.');
478 }
479
480 // }}}
481 // {{{ testAddUserId()
482
483 /**
484 * @group mutators
485 */
486 public function testAddUserId()
487 {
488 $key = new Crypt_GPG_Key();
489
490 $userIds = $key->getUserIds();
491 $this->assertTrue(is_array($userIds),
492 'Failed to assert returned user ids is an array.');
493
494 $this->assertEquals(0, count($userIds),
495 'Failed to assert there are no user ids.');
496
497 // add first user id
498 $firstUserId = new Crypt_GPG_UserId(array(
499 'name' => 'Alice',
500 'comment' => 'shipping',
501 'email' => 'alice@example.com'
502 ));
503
504 $key->addUserId($firstUserId);
505
506 $userIds = $key->getUserIds();
507 $this->assertTrue(is_array($userIds),
508 'Failed to assert returned user ids is an array.');
509
510 $this->assertEquals(1, count($userIds),
511 'Failed to assert number of returned user ids is the same as ' .
512 'the number of user ids added.');
513
514 $this->assertContainsOnly('Crypt_GPG_UserId', $userIds, false,
515 'Failed to assert all returned user ids are Crypt_GPG_UserId ' .
516 'objects.');
517
518 $this->assertArrayHasKey(0, $userIds);
519 $this->assertEquals($userIds[0], $firstUserId,
520 'Failed to assert the first user id is the same as the first ' .
521 'added user id.');
522
523 // add second user id
524 $secondUserId = new Crypt_GPG_UserId(array(
525 'name' => 'Bob',
526 'comment' => 'receiving',
527 'email' => 'bob@example.com'
528 ));
529
530 $key->addUserId($secondUserId);
531
532 $userIds = $key->getUserIds();
533 $this->assertTrue(is_array($userIds),
534 'Failed to assert returned user ids is an array.');
535
536 $this->assertEquals(2, count($userIds),
537 'Failed to assert number of returned user ids is the same as ' .
538 'the number of user ids added.');
539
540 $this->assertContainsOnly('Crypt_GPG_UserId', $userIds, false,
541 'Failed to assert all returned user ids are Crypt_GPG_UserId ' .
542 'objects.');
543
544 $this->assertArrayHasKey(0, $userIds);
545 $this->assertEquals($userIds[0], $firstUserId,
546 'Failed to assert the first user id is the same as the first ' .
547 'added user id.');
548
549 $this->assertArrayHasKey(1, $userIds);
550 $this->assertEquals($userIds[1], $secondUserId,
551 'Failed to assert the second user id is the same as the second ' .
552 'added user id.');
553 }
554
555 // }}}
556
557 // fluent interface
558 // {{{ testFluentInterface
559
560 /**
561 * @group fluent
562 */
563 public function testFluentInterface()
564 {
565 $key = new Crypt_GPG_Key();
566
567 // add first sub-key
568 $firstSubKey = new Crypt_GPG_SubKey(array(
569 'id' => 'C097D9EC94C06363',
570 'algorithm' => Crypt_GPG_SubKey::ALGORITHM_DSA,
571 'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
572 'length' => 1024,
573 'creation' => 1221785805,
574 'expiration' => 0,
575 'canSign' => true,
576 'canEncrypt' => false,
577 'hasPrivate' => true
578 ));
579
580 $returnedKey = $key->addSubKey($firstSubKey);
581
582 $this->assertEquals(
583 $key,
584 $returnedKey,
585 'Failed asserting fluent interface works for addSubKey() method.'
586 );
587
588 $firstUserId = new Crypt_GPG_UserId(array(
589 'name' => 'Alice',
590 'comment' => 'shipping',
591 'email' => 'alice@example.com'
592 ));
593
594 $returnedKey = $key->addUserId($firstUserId);
595
596 $this->assertEquals(
597 $key,
598 $returnedKey,
599 'Failed asserting fluent interface works for addUserId() method.'
600 );
601 }
602
603 // }}}
604 }
605
606 ?>