comparison vendor/pear/crypt_gpg/tests/SignTest.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 * Signing 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 SignTestCase
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 */
46 require_once 'TestCase.php';
47
48 /**
49 * Tests signing abilities 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 */
58 class SignTestCase extends Crypt_GPG_TestCase
59 {
60 public function testHasSignKeys()
61 {
62 $this->assertFalse($this->gpg->hasSignKeys());
63 $this->gpg->addSignKey('no-passphrase@example.com');
64 $this->assertTrue($this->gpg->hasSignKeys());
65 }
66
67 // {{{ testSignKeyNotFoundException_invalid()
68
69 /**
70 * @expectedException Crypt_GPG_KeyNotFoundException
71 *
72 * @group string
73 */
74 public function testSignKeyNotFoundException_invalid()
75 {
76 $data = 'Hello, Alice! Goodbye, Bob!';
77 $this->gpg->addSignKey('non-existent-key@example.com');
78 $this->gpg->sign($data);
79 }
80
81 // }}}
82 // {{{ testSignKeyNotFoundException_none()
83
84 /**
85 * @expectedException Crypt_GPG_KeyNotFoundException
86 *
87 * @group string
88 */
89 public function testSignKeyNotFoundException_none()
90 {
91 $data = 'Hello, Alice! Goodbye, Bob!';
92 $this->gpg->sign($data);
93 }
94
95 // }}}
96 // {{{ testSignBadPassphraseException_missing()
97
98 /**
99 * @expectedException Crypt_GPG_BadPassphraseException
100 *
101 * @group string
102 */
103 public function testSignBadPassphraseException_missing()
104 {
105 $data = 'Hello, Alice! Goodbye, Bob!';
106 $this->gpg->addSignKey('first-keypair@example.com');
107 $this->gpg->sign($data);
108 }
109
110 // }}}
111 // {{{ testSignBadPassphraseException_bad()
112
113 /**
114 * @expectedException Crypt_GPG_BadPassphraseException
115 *
116 * @group string
117 */
118 public function testSignBadPassphraseException_bad()
119 {
120 $data = 'Hello, Alice! Goodbye, Bob!';
121 $this->gpg->addSignKey('first-keypair@example.com', 'incorrect');
122 $this->gpg->sign($data);
123 }
124
125 // }}}
126 // {{{ testSignNoPassphrase()
127
128 /**
129 * @group string
130 */
131 public function testSignNoPassphrase()
132 {
133 $data = 'Hello, Alice! Goodbye, Bob!';
134 $this->gpg->addSignKey('no-passphrase@example.com');
135 $signedData = $this->gpg->sign($data);
136
137 $signatures = $this->gpg->verify($signedData);
138 $this->assertEquals(1, count($signatures));
139 foreach ($signatures as $signature) {
140 $this->assertTrue($signature->isValid());
141 }
142 }
143
144 // }}}
145 // {{{ testSignNormal()
146
147 /**
148 * @group string
149 */
150 public function testSignNormal()
151 {
152 $data = 'Hello, Alice! Goodbye, Bob!';
153 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
154 $signedData = $this->gpg->sign($data);
155
156 $signatures = $this->gpg->verify($signedData);
157 $this->assertEquals(1, count($signatures));
158 foreach ($signatures as $signature) {
159 $this->assertTrue($signature->isValid());
160 }
161 }
162
163 // }}}
164 // {{{ testSignClear()
165
166 /**
167 * @group string
168 */
169 public function testSignClear()
170 {
171 $data = 'Hello, Alice! Goodbye, Bob!';
172 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
173 $signedData = $this->gpg->sign($data, Crypt_GPG::SIGN_MODE_CLEAR);
174
175 $signatures = $this->gpg->verify($signedData);
176 $this->assertEquals(1, count($signatures));
177 foreach ($signatures as $signature) {
178 $this->assertTrue($signature->isValid());
179 }
180 }
181
182 // }}}
183 // {{{ testSignDetached()
184
185 /**
186 * @group string
187 */
188 public function testSignDetached()
189 {
190 $data = 'Hello, Alice! Goodbye, Bob!';
191 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
192 $signatureData = $this->gpg->sign($data,
193 Crypt_GPG::SIGN_MODE_DETACHED);
194
195 $signatures = $this->gpg->verify($data, $signatureData);
196 $this->assertEquals(1, count($signatures));
197 foreach ($signatures as $signature) {
198 $this->assertTrue($signature->isValid());
199 }
200 }
201
202 // }}}
203 // {{{ testSignDualOnePassphrase()
204
205 /**
206 * @group string
207 */
208 public function testSignDualOnePassphrase()
209 {
210 $data = 'Hello, Alice! Goodbye, Bob!';
211 $this->gpg->addSignKey('no-passphrase@example.com');
212 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
213 $signedData = $this->gpg->sign($data);
214
215 $signatures = $this->gpg->verify($signedData);
216 $this->assertEquals(2, count($signatures));
217 foreach ($signatures as $signature) {
218 $this->assertTrue($signature->isValid());
219 }
220 }
221
222 // }}}
223 // {{{ testSignDualNormal()
224
225 /**
226 * @group string
227 */
228 public function testSignDualNormal()
229 {
230 $data = 'Hello, Alice! Goodbye, Bob!';
231 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
232 $this->gpg->addSignKey('second-keypair@example.com', 'test2');
233 $signedData = $this->gpg->sign($data);
234
235 $signatures = $this->gpg->verify($signedData);
236 $this->assertEquals(2, count($signatures));
237 foreach ($signatures as $signature) {
238 $this->assertTrue($signature->isValid());
239 }
240 }
241
242 // }}}
243 // {{{ testSignDualClear()
244
245 /**
246 * @group string
247 */
248 public function testSignDualClear()
249 {
250 $data = 'Hello, Alice! Goodbye, Bob!';
251 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
252 $this->gpg->addSignKey('second-keypair@example.com', 'test2');
253 $signedData = $this->gpg->sign($data, Crypt_GPG::SIGN_MODE_CLEAR);
254
255 $signatures = $this->gpg->verify($signedData);
256 $this->assertEquals(2, count($signatures));
257 foreach ($signatures as $signature) {
258 $this->assertTrue($signature->isValid());
259 }
260 }
261
262 // }}}
263 // {{{ testSignDualDetached()
264
265 /**
266 * @group string
267 */
268 public function testSignDualDetached()
269 {
270 $data = 'Hello, Alice! Goodbye, Bob!';
271 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
272 $this->gpg->addSignKey('second-keypair@example.com', 'test2');
273 $signatureData = $this->gpg->sign($data,
274 Crypt_GPG::SIGN_MODE_DETACHED);
275
276 $signatures = $this->gpg->verify($data, $signatureData);
277 $this->assertEquals(2, count($signatures));
278 foreach ($signatures as $signature) {
279 $this->assertTrue($signature->isValid());
280 }
281 }
282
283 // }}}
284 // {{{ testSignEmpty()
285
286 /**
287 * @group string
288 */
289 public function testSignEmpty()
290 {
291 $data = '';
292
293 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
294
295 $signedData = $this->gpg->sign($data);
296 $signatures = $this->gpg->verify($signedData);
297
298 $this->assertEquals(1, count($signatures));
299 foreach ($signatures as $signature) {
300 $this->assertTrue($signature->isValid());
301 }
302 }
303
304 // }}}
305 // {{{ testSignDetachedTextmode()
306
307 /**
308 * @group string
309 */
310 public function testSignDetachedTextmode()
311 {
312 // data with Unix line endings
313 $data = "It was the best of times,\n"
314 . "it was the worst of times,\n"
315 . "it was the age of wisdom,\n"
316 . "it was the age of foolishness,\n"
317 . "it was the epoch of belief,\n"
318 . "it was the epoch of incredulity,\n"
319 . "it was the season of Light,\n"
320 . "it was the season of Darkness,\n"
321 . "it was the spring of hope,\n"
322 . "it was the winter of despair,";
323
324 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
325 $signatureData = $this->gpg->sign(
326 $data,
327 Crypt_GPG::SIGN_MODE_DETACHED,
328 true,
329 true
330 );
331
332 // convert data to Windows line endings
333 $data = str_replace("\n", "\r\n", $data);
334
335 // verify data
336 $signatures = $this->gpg->verify($data, $signatureData);
337 $this->assertEquals(1, count($signatures));
338 foreach ($signatures as $signature) {
339 $this->assertTrue(
340 $signature->isValid(),
341 'Failed asserting textmode signature is valid.'
342 );
343 }
344 }
345
346 // }}}
347 // {{{ testSignFileNoPassphrase()
348
349 /**
350 * @group file
351 */
352 public function testSignFileNoPassphrase()
353 {
354 $inputFilename = $this->getDataFilename('testFileMedium.plain');
355 $outputFilename =
356 $this->getTempFilename('testSignFileNoPassphrase.asc');
357
358 $this->gpg->addSignKey('no-passphrase@example.com');
359 $this->gpg->signFile($inputFilename, $outputFilename);
360
361 $signatures = $this->gpg->verifyFile($outputFilename);
362 $this->assertEquals(1, count($signatures));
363 foreach ($signatures as $signature) {
364 $this->assertTrue($signature->isValid());
365 }
366 }
367
368 // }}}
369 // {{{ testSignFileNormal()
370
371 /**
372 * @group file
373 */
374 public function testSignFileNormal()
375 {
376 $inputFilename = $this->getDataFilename('testFileMedium.plain');
377 $outputFilename = $this->getTempFilename('testSignFileNormal.asc');
378
379 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
380 $this->gpg->signFile($inputFilename, $outputFilename);
381
382 $signatures = $this->gpg->verifyFile($outputFilename);
383 $this->assertEquals(1, count($signatures));
384 foreach ($signatures as $signature) {
385 $this->assertTrue($signature->isValid());
386 }
387 }
388
389 // }}}
390 // {{{ testSignFileClear()
391
392 /**
393 * @group file
394 */
395 public function testSignFileClear()
396 {
397 $inputFilename = $this->getDataFilename('testFileMedium.plain');
398 $outputFilename = $this->getTempFilename('testSignFileClear.asc');
399
400 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
401 $this->gpg->signFile($inputFilename, $outputFilename,
402 Crypt_GPG::SIGN_MODE_CLEAR);
403
404 $signatures = $this->gpg->verifyFile($outputFilename);
405 $this->assertEquals(1, count($signatures));
406 foreach ($signatures as $signature) {
407 $this->assertTrue($signature->isValid());
408 }
409 }
410
411 // }}}
412 // {{{ testSignFileDetached()
413
414 /**
415 * @group file
416 */
417 public function testSignFileDetached()
418 {
419 $inputFilename = $this->getDataFilename('testFileMedium.plain');
420 $outputFilename = $this->getTempFilename('testSignFileDetached.asc');
421
422 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
423 $this->gpg->signFile($inputFilename, $outputFilename,
424 Crypt_GPG::SIGN_MODE_DETACHED);
425
426 $signatureData = file_get_contents($outputFilename);
427
428 $signatures = $this->gpg->verifyFile($inputFilename, $signatureData);
429 $this->assertEquals(1, count($signatures));
430 foreach ($signatures as $signature) {
431 $this->assertTrue($signature->isValid());
432 }
433 }
434
435 // }}}
436 // {{{ testSignFileDetachedToString()
437
438 /**
439 * @group file
440 */
441 public function testSignFileDetachedToString()
442 {
443 $filename = $this->getDataFilename('testFileMedium.plain');
444
445 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
446 $signatureData = $this->gpg->signFile($filename, null,
447 Crypt_GPG::SIGN_MODE_DETACHED);
448
449 $signatures = $this->gpg->verifyFile($filename, $signatureData);
450 $this->assertEquals(1, count($signatures));
451 foreach ($signatures as $signature) {
452 $this->assertTrue($signature->isValid());
453 }
454 }
455
456 // }}}
457 // {{{ testSignFileDualOnePassphrase()
458
459 /**
460 * @group file
461 */
462 public function testSignFileDualOnePassphrase()
463 {
464 $inputFilename = $this->getDataFilename('testFileMedium.plain');
465 $outputFilename =
466 $this->getTempFilename('testSignFileDualOnePassphrase.asc');
467
468 $this->gpg->addSignKey('no-passphrase@example.com');
469 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
470 $this->gpg->signFile($inputFilename, $outputFilename);
471
472 $signatures = $this->gpg->verifyFile($outputFilename);
473 $this->assertEquals(2, count($signatures));
474 foreach ($signatures as $signature) {
475 $this->assertTrue($signature->isValid());
476 }
477 }
478
479 // }}}
480 // {{{ testSignFileDualNormal()
481
482 /**
483 * @group file
484 */
485 public function testSignFileDualNormal()
486 {
487 $inputFilename = $this->getDataFilename('testFileMedium.plain');
488 $outputFilename = $this->getTempFilename('testSignFileDualNormal.asc');
489
490 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
491 $this->gpg->addSignKey('second-keypair@example.com', 'test2');
492 $this->gpg->signFile($inputFilename, $outputFilename);
493
494 $signatures = $this->gpg->verifyFile($outputFilename);
495 $this->assertEquals(2, count($signatures));
496 foreach ($signatures as $signature) {
497 $this->assertTrue($signature->isValid());
498 }
499 }
500
501 // }}}
502 // {{{ testSignFileDualClear()
503
504 /**
505 * @group file
506 */
507 public function testSignFileDualClear()
508 {
509 $inputFilename = $this->getDataFilename('testFileMedium.plain');
510 $outputFilename = $this->getTempFilename('testSignFileDualClear.asc');
511
512 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
513 $this->gpg->addSignKey('second-keypair@example.com', 'test2');
514 $this->gpg->signFile($inputFilename, $outputFilename,
515 Crypt_GPG::SIGN_MODE_CLEAR);
516
517 $signatures = $this->gpg->verifyFile($outputFilename);
518 $this->assertEquals(2, count($signatures));
519 foreach ($signatures as $signature) {
520 $this->assertTrue($signature->isValid());
521 }
522 }
523
524 // }}}
525 // {{{ testSignFileDualDetached()
526
527 /**
528 * @group file
529 */
530 public function testSignFileDualDetached()
531 {
532 $inputFilename = $this->getDataFilename('testFileMedium.plain');
533 $outputFilename =
534 $this->getTempFilename('testSignFileDualDetached.asc');
535
536 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
537 $this->gpg->addSignKey('second-keypair@example.com', 'test2');
538 $this->gpg->signFile($inputFilename, $outputFilename,
539 Crypt_GPG::SIGN_MODE_DETACHED);
540
541 $signatureData = file_get_contents($outputFilename);
542
543 $signatures = $this->gpg->verifyFile($inputFilename, $signatureData);
544 $this->assertEquals(2, count($signatures));
545 foreach ($signatures as $signature) {
546 $this->assertTrue($signature->isValid());
547 }
548 }
549
550 // }}}
551 // {{{ testSignFileFileException_input()
552
553 /**
554 * @expectedException Crypt_GPG_FileException
555 *
556 * @group file
557 */
558 public function testSignFileFileException_input()
559 {
560 // input file does not exist
561 $inputFilename =
562 $this->getDataFilename('testSignFileFileFileException_input.plain');
563
564 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
565 $this->gpg->signFile($inputFilename);
566 }
567
568 // }}}
569 // {{{ testSignFileFileException_output()
570
571 /**
572 * @expectedException Crypt_GPG_FileException
573 *
574 * @group file
575 */
576 public function testSignFileFileException_output()
577 {
578 // input file is encrypted with first-keypair@example.com
579 // output file does not exist
580 $inputFilename = $this->getDataFilename('testFileMedium.plain');
581 $outputFilename = './non-existent' .
582 '/testSignFileFileException_output.plain';
583
584 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
585 $this->gpg->signFile($inputFilename, $outputFilename);
586 }
587
588 // }}}
589 // {{{ testSignFileEmpty()
590
591 /**
592 * @group file
593 */
594 public function testSignFileEmpty()
595 {
596 $filename = $this->getDataFilename('testFileEmpty.plain');
597
598 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
599
600 $signedData = $this->gpg->signFile($filename);
601 $signatures = $this->gpg->verify($signedData);
602
603 $this->assertEquals(1, count($signatures));
604 foreach ($signatures as $signature) {
605 $this->assertTrue($signature->isValid());
606 }
607 }
608
609 // }}}
610 // {{{ testGetLastSignatureInfo()
611
612 public function testGetLastSignatureInfo()
613 {
614 $this->gpg->addSignKey('first-keypair@example.com', 'test1');
615 $signedData = $this->gpg->sign('test', Crypt_GPG::SIGN_MODE_DETACHED);
616
617 $sigInfo = $this->gpg->getLastSignatureInfo();
618 $this->assertInstanceOf('Crypt_GPG_SignatureCreationInfo', $sigInfo);
619 $this->assertTrue($sigInfo->isValid());
620 $this->assertEquals(date('Y-m-d'), date('Y-m-d', $sigInfo->getTimestamp()));
621 $this->assertEquals(Crypt_GPG::SIGN_MODE_DETACHED, $sigInfo->getMode());
622 $this->assertEquals(
623 '8D2299D9C5C211128B32BBB0C097D9EC94C06363',
624 $sigInfo->getKeyFingerprint()
625 );
626 $this->assertNotNull($sigInfo->getHashAlgorithmName());
627 }
628
629 // }}}
630 }
631
632 ?>