1<?php
2
3/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5/**
6 * Signature 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 SignatureTestCase
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$
40 * @link      http://pear.php.net/package/Crypt_GPG
41 */
42
43/**
44 * Base test case.
45 */
46require_once 'TestCase.php';
47
48/**
49 * Signature class.
50 */
51require_once 'Crypt/GPG/Signature.php';
52
53/**
54 * Signature class tests for Crypt_GPG.
55 *
56 * @category  Encryption
57 * @package   Crypt_GPG
58 * @author    Michael Gauthier <mike@silverorange.com>
59 * @copyright 2008-2010 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 SignatureTest extends Crypt_GPG_TestCase
64{
65    /**
66     * @group construct
67     */
68    public function testConstructFromSignature()
69    {
70        $expectedSignature = new Crypt_GPG_Signature(array(
71            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
72            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
73            'keyId'       => '0C097D9EC94C06363',
74            'creation'    => 1221785858,
75            'expiration'  => 1421785858,
76            'valid'       => false,
77            'userId'      => 'Alice <alice@example.com>'
78        ));
79
80        $signature = new Crypt_GPG_Signature($expectedSignature);
81
82        $this->assertEquals($expectedSignature, $signature);
83    }
84
85    /**
86     * @group construct
87     */
88    public function testConstructFromArray()
89    {
90        $signature = new Crypt_GPG_Signature(array(
91            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
92            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
93            'keyId'       => '0C097D9EC94C06363',
94            'creation'    => 1221785858,
95            'expiration'  => 1421785858,
96            'valid'       => false,
97            'userId'      => 'Alice <alice@example.com>'
98        ));
99
100        $this->assertEquals('KuhELanvhPRXozEjFWb2mam1q20',
101            $signature->getId());
102
103        $this->assertEquals('8D2299D9C5C211128B32BBB0C097D9EC94C06363',
104            $signature->getKeyFingerprint());
105
106        $this->assertEquals('0C097D9EC94C06363', $signature->getKeyId());
107
108        $this->assertEquals(1221785858, $signature->getCreationDate());
109        $this->assertEquals(1421785858, $signature->getExpirationDate());
110
111        $this->assertFalse($signature->isValid());
112
113        $this->assertEquals('Alice <alice@example.com>',
114            strval($signature->getUserId()));
115    }
116
117    /**
118     * @group accessors
119     */
120    public function testGetId()
121    {
122        $signature = new Crypt_GPG_Signature(array(
123            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
124            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
125            'creation'    => 1221785858,
126            'expiration'  => 1421785858,
127            'valid'       => false,
128            'userId'      => 'Alice <alice@example.com>'
129        ));
130
131        $this->assertEquals('KuhELanvhPRXozEjFWb2mam1q20', $signature->getId());
132    }
133
134    /**
135     * @group accessors
136     */
137    public function testGetKeyFingerprint()
138    {
139        $signature = new Crypt_GPG_Signature(array(
140            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
141            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
142            'creation'    => 1221785858,
143            'expiration'  => 1421785858,
144            'valid'       => false,
145            'userId'      => 'Alice <alice@example.com>'
146        ));
147
148        $this->assertEquals('8D2299D9C5C211128B32BBB0C097D9EC94C06363',
149            $signature->getKeyFingerprint());
150    }
151
152    /**
153     * @group accessors
154     */
155    public function testGetKeyId()
156    {
157        $signature = new Crypt_GPG_Signature(array(
158            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
159            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
160            'keyId'       => '0C097D9EC94C06363',
161            'creation'    => 1221785858,
162            'expiration'  => 1421785858,
163            'valid'       => false,
164            'userId'      => 'Alice <alice@example.com>'
165        ));
166
167        $this->assertEquals('0C097D9EC94C06363', $signature->getKeyId());
168    }
169
170    /**
171     * @group accessors
172     */
173    public function testGetCreationDate()
174    {
175        $signature = new Crypt_GPG_Signature(array(
176            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
177            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
178            'creation'    => 1221785858,
179            'expiration'  => 1421785858,
180            'valid'       => false,
181            'userId'      => 'Alice <alice@example.com>'
182        ));
183
184        $this->assertEquals(1221785858, $signature->getCreationDate());
185    }
186
187    /**
188     * @group accessors
189     */
190    public function testGetExpirationDate()
191    {
192        $signature = new Crypt_GPG_Signature(array(
193            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
194            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
195            'creation'    => 1221785858,
196            'expiration'  => 1421785858,
197            'valid'       => false,
198            'userId'      => 'Alice <alice@example.com>'
199        ));
200
201        $this->assertEquals(1421785858, $signature->getExpirationDate());
202    }
203
204    /**
205     * @group accessors
206     */
207    public function testIsValid()
208    {
209        $signature = new Crypt_GPG_Signature(array(
210            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
211            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
212            'creation'    => 1221785858,
213            'expiration'  => 1421785858,
214            'valid'       => true,
215            'userId'      => 'Alice <alice@example.com>'
216        ));
217
218        $this->assertTrue($signature->isValid());
219
220        $signature = new Crypt_GPG_Signature(array(
221            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
222            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
223            'creation'    => 1221785858,
224            'expiration'  => 1421785858,
225            'valid'       => false,
226            'userId'      => 'Alice <alice@example.com>'
227        ));
228
229
230        $this->assertFalse($signature->isValid());
231    }
232
233    /**
234     * @group accessors
235     */
236    public function testGetUserId()
237    {
238        $signature = new Crypt_GPG_Signature(array(
239            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
240            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
241            'creation'    => 1221785858,
242            'expiration'  => 1421785858,
243            'valid'       => true,
244            'userId'      => 'Alice <alice@example.com>'
245        ));
246
247        $expectedUserId = new Crypt_GPG_UserId(array(
248            'name'  => 'Alice',
249            'email' => 'alice@example.com'
250        ));
251
252        $this->assertEquals($expectedUserId, $signature->getUserId());
253    }
254
255    /**
256     * @group mutators
257     */
258    public function testSetId()
259    {
260        $expectedSignature = new Crypt_GPG_Signature(array(
261            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
262            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
263            'creation'    => 1221785858,
264            'expiration'  => 1421785858,
265            'valid'       => true,
266            'userId'      => 'Alice <alice@example.com>'
267        ));
268
269        $signature = new Crypt_GPG_Signature(array(
270            'id'          => 'something different',
271            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
272            'creation'    => 1221785858,
273            'expiration'  => 1421785858,
274            'valid'       => true,
275            'userId'      => 'Alice <alice@example.com>'
276        ));
277
278        $signature->setId('KuhELanvhPRXozEjFWb2mam1q20');
279
280        $this->assertEquals($expectedSignature, $signature);
281    }
282
283    /**
284     * @group mutators
285     */
286    public function testSetKeyFingerprint()
287    {
288        $expectedSignature = new Crypt_GPG_Signature(array(
289            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
290            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
291            'creation'    => 1221785858,
292            'expiration'  => 1421785858,
293            'valid'       => true,
294            'userId'      => 'Alice <alice@example.com>'
295        ));
296
297        $signature = new Crypt_GPG_Signature(array(
298            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
299            'fingerprint' => 'bad fingerprint',
300            'creation'    => 1221785858,
301            'expiration'  => 1421785858,
302            'valid'       => true,
303            'userId'      => 'Alice <alice@example.com>'
304        ));
305
306        $signature->setKeyFingerprint(
307            '8D2299D9C5C211128B32BBB0C097D9EC94C06363');
308
309        $this->assertEquals($expectedSignature, $signature);
310    }
311
312    /**
313     * @group mutators
314     */
315    public function testSetKeyId()
316    {
317        $expectedSignature = new Crypt_GPG_Signature(array(
318            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
319            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
320            'keyId'       => '0C097D9EC94C06363',
321            'creation'    => 1221785858,
322            'expiration'  => 1421785858,
323            'valid'       => true,
324            'userId'      => 'Alice <alice@example.com>'
325        ));
326
327        $signature = new Crypt_GPG_Signature(array(
328            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
329            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
330            'keyId'       => 'bad key id',
331            'creation'    => 1221785858,
332            'expiration'  => 1421785858,
333            'valid'       => true,
334            'userId'      => 'Alice <alice@example.com>'
335        ));
336
337        $signature->setKeyId('0C097D9EC94C06363');
338
339        $this->assertEquals($expectedSignature, $signature);
340    }
341
342    /**
343     * @group mutators
344     */
345    public function testSetCreationDate()
346    {
347        $expectedSignature = new Crypt_GPG_Signature(array(
348            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
349            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
350            'creation'    => 1221785858,
351            'expiration'  => 1421785858,
352            'valid'       => true,
353            'userId'      => 'Alice <alice@example.com>'
354        ));
355
356        $signature = new Crypt_GPG_Signature(array(
357            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
358            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
359            'creation'    => 1111111111,
360            'expiration'  => 1421785858,
361            'valid'       => true,
362            'userId'      => 'Alice <alice@example.com>'
363        ));
364
365        $signature->setCreationDate(1221785858);
366
367        $this->assertEquals($expectedSignature, $signature);
368    }
369
370    /**
371     * @group mutators
372     */
373    public function testSetExpirationDate()
374    {
375        $expectedSignature = new Crypt_GPG_Signature(array(
376            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
377            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
378            'creation'    => 1221785858,
379            'expiration'  => 1421785858,
380            'valid'       => true,
381            'userId'      => 'Alice <alice@example.com>'
382        ));
383
384        $signature = new Crypt_GPG_Signature(array(
385            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
386            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
387            'creation'    => 1221785858,
388            'expiration'  => 0,
389            'valid'       => true,
390            'userId'      => 'Alice <alice@example.com>'
391        ));
392
393        $signature->setExpirationDate(1421785858);
394
395        $this->assertEquals($expectedSignature, $signature);
396    }
397
398    /**
399     * @group mutators
400     */
401    public function testSetValid()
402    {
403        $expectedSignature = new Crypt_GPG_Signature(array(
404            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
405            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
406            'creation'    => 1221785858,
407            'expiration'  => 1421785858,
408            'valid'       => true,
409            'userId'      => 'Alice <alice@example.com>'
410        ));
411
412        $signature = new Crypt_GPG_Signature(array(
413            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
414            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
415            'creation'    => 1221785858,
416            'expiration'  => 1421785858,
417            'valid'       => false,
418            'userId'      => 'Alice <alice@example.com>'
419        ));
420
421        $signature->setValid(true);
422
423        $this->assertEquals($expectedSignature, $signature);
424    }
425
426    /**
427     * @group accessors
428     */
429    public function testSetUserId()
430    {
431        $expectedSignature = new Crypt_GPG_Signature(array(
432            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
433            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
434            'creation'    => 1221785858,
435            'expiration'  => 1421785858,
436            'valid'       => true,
437            'userId'      => 'Alice <alice@example.com>'
438        ));
439
440        $signature = new Crypt_GPG_Signature(array(
441            'id'          => 'KuhELanvhPRXozEjFWb2mam1q20',
442            'fingerprint' => '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
443            'creation'    => 1221785858,
444            'expiration'  => 1421785858,
445            'valid'       => true,
446            'userId'      => 'Bob <bob@example.com>'
447        ));
448
449        $userId = new Crypt_GPG_UserId(array(
450            'name'  => 'Alice',
451            'email' => 'alice@example.com'
452        ));
453
454        $signature->setUserId($userId);
455
456        $this->assertEquals($expectedSignature, $signature);
457    }
458
459    /**
460     * @group fluent
461     */
462    public function testFluentInterface()
463    {
464        $signature         = new Crypt_GPG_Signature();
465        $returnedSignature = $signature->setId('KuhELanvhPRXozEjFWb2mam1q20');
466        $this->assertEquals(
467            $signature,
468            $returnedSignature,
469            'Failed asserting fluent interface works for setId() method.'
470        );
471
472        $signature         = new Crypt_GPG_Signature();
473        $returnedSignature = $signature->setKeyFingerprint(
474            '8D2299D9C5C211128B32BBB0C097D9EC94C06363'
475        );
476        $this->assertEquals(
477            $signature,
478            $returnedSignature,
479            'Failed asserting fluent interface works for setKeyFingerprint() ' .
480            'method.'
481        );
482
483        $signature         = new Crypt_GPG_Signature();
484        $returnedSignature = $signature->setKeyId('0C097D9EC94C06363');
485        $this->assertEquals(
486            $signature,
487            $returnedSignature,
488            'Failed asserting fluent interface works for setKeyId() method'
489        );
490
491        $signature         = new Crypt_GPG_Signature();
492        $returnedSignature = $signature->setCreationDate(1234567890);
493        $this->assertEquals(
494            $signature,
495            $returnedSignature,
496            'Failed asserting fluent interface works for setCreationDate() ' .
497            'method.'
498        );
499
500        $signature         = new Crypt_GPG_Signature();
501        $returnedSignature = $signature->setExpirationDate(1234567890);
502        $this->assertEquals(
503            $signature,
504            $returnedSignature,
505            'Failed asserting fluent interface works for setExpirationDate() ' .
506            'method.'
507        );
508
509        $signature         = new Crypt_GPG_Signature();
510        $returnedSignature = $signature->setValid(true);
511        $this->assertEquals(
512            $signature,
513            $returnedSignature,
514            'Failed asserting fluent interface works for setValid() method.'
515        );
516
517        $signature         = new Crypt_GPG_Signature();
518        $returnedSignature = $signature->setUserId(new Crypt_GPG_UserId());
519        $this->assertEquals(
520            $signature,
521            $returnedSignature,
522            'Failed asserting fluent interface works for setUserId() method.'
523        );
524    }
525}
526