Mercurial > hg > rc1
comparison vendor/pear/crypt_gpg/tests/EncryptAndSignTest.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 * Encrypt and sign 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 EncryptSignTestCase | |
| 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-2009 silverorange | |
| 38 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 | |
| 39 * @version $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 encrypt and sign abilities of Crypt_GPG. | |
| 50 * | |
| 51 * @category Encryption | |
| 52 * @package Crypt_GPG | |
| 53 * @author Michael Gauthier <mike@silverorange.com> | |
| 54 * @copyright 2005-2009 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 EncryptAndSignTestCase extends Crypt_GPG_TestCase | |
| 59 { | |
| 60 // string | |
| 61 // {{{ testEncryptAndSignKeyNotFoundException_invalid_sign_key() | |
| 62 | |
| 63 /** | |
| 64 * @expectedException Crypt_GPG_KeyNotFoundException | |
| 65 * | |
| 66 * @group string | |
| 67 */ | |
| 68 public function testEncryptAndSignKeyNotFoundException_invalid_sign_key() | |
| 69 { | |
| 70 $data = 'Hello, Alice! Goodbye, Bob!'; | |
| 71 $this->gpg->addSignKey('non-existent-key@example.com'); | |
| 72 $this->gpg->addEncryptKey('first-keypair@example.com'); | |
| 73 $this->gpg->encryptAndSign($data); | |
| 74 } | |
| 75 | |
| 76 // }}} | |
| 77 // {{{ testEncryptAndSignKeyNotFoundException_no_sign_key() | |
| 78 | |
| 79 /** | |
| 80 * @expectedException Crypt_GPG_KeyNotFoundException | |
| 81 * | |
| 82 * @group string | |
| 83 */ | |
| 84 public function testEncryptAndSignKeyNotFoundException_no_sign_key() | |
| 85 { | |
| 86 $data = 'Hello, Alice! Goodbye, Bob!'; | |
| 87 $this->gpg->addEncryptKey('first-keypair@example.com'); | |
| 88 $this->gpg->encryptAndSign($data); | |
| 89 } | |
| 90 | |
| 91 // }}} | |
| 92 // {{{ testEncryptAndSignKeyNotFoundException_invalid_encrypt_key() | |
| 93 | |
| 94 /** | |
| 95 * @expectedException Crypt_GPG_KeyNotFoundException | |
| 96 * | |
| 97 * @group string | |
| 98 */ | |
| 99 public function testEncryptAndSignKeyNotFoundException_invalid_encrypt_key() | |
| 100 { | |
| 101 $data = 'Hello, Alice! Goodbye, Bob!'; | |
| 102 $this->gpg->addSignKey('first-keypair@example.com', 'test1'); | |
| 103 $this->gpg->addEncryptKey('non-existent-key@example.com'); | |
| 104 $this->gpg->encryptAndSign($data); | |
| 105 } | |
| 106 | |
| 107 // }}} | |
| 108 // {{{ testEncryptAndSignKeyNotFoundException_no_encrypt_key() | |
| 109 | |
| 110 /** | |
| 111 * @expectedException Crypt_GPG_KeyNotFoundException | |
| 112 * | |
| 113 * @group string | |
| 114 */ | |
| 115 public function testEncryptAndSignKeyNotFoundException_no_encrypt_key() | |
| 116 { | |
| 117 $data = 'Hello, Alice! Goodbye, Bob!'; | |
| 118 $this->gpg->addSignKey('first-keypair@example.com', 'test1'); | |
| 119 $this->gpg->encryptAndSign($data); | |
| 120 } | |
| 121 | |
| 122 // }}} | |
| 123 // {{{ testEncryptAndSignBadPassphraseException_missing_sign_key() | |
| 124 | |
| 125 /** | |
| 126 * @expectedException Crypt_GPG_BadPassphraseException | |
| 127 * | |
| 128 * @group string | |
| 129 */ | |
| 130 public function testEncryptAndSignBadPassphraseException_missing_sign_key() | |
| 131 { | |
| 132 $data = 'Hello, Alice! Goodbye, Bob!'; | |
| 133 $this->gpg->addSignKey('first-keypair@example.com'); | |
| 134 $this->gpg->addEncryptKey('first-keypair@example.com'); | |
| 135 $this->gpg->encryptAndSign($data); | |
| 136 } | |
| 137 | |
| 138 // }}} | |
| 139 // {{{ testEncryptAndSignBadPassphraseException_bad_sign_key() | |
| 140 | |
| 141 /** | |
| 142 * @expectedException Crypt_GPG_BadPassphraseException | |
| 143 * | |
| 144 * @group string | |
| 145 */ | |
| 146 public function testEncryptAndSignBadPassphraseException_bad_sign_key() | |
| 147 { | |
| 148 $data = 'Hello, Alice! Goodbye, Bob!'; | |
| 149 $this->gpg->addSignKey('first-keypair@example.com', 'incorrect'); | |
| 150 $this->gpg->addEncryptKey('first-keypair@example.com'); | |
| 151 $this->gpg->encryptAndSign($data); | |
| 152 } | |
| 153 | |
| 154 // }}} | |
| 155 // {{{ testEncryptAndSignNoPassphrase() | |
| 156 | |
| 157 /** | |
| 158 * @group string | |
| 159 */ | |
| 160 public function testEncryptAndSignNoPassphrase() | |
| 161 { | |
| 162 $data = 'Hello, Alice! Goodbye, Bob!'; | |
| 163 | |
| 164 $signKey = 'no-passphrase@example.com'; | |
| 165 $encryptKey = 'first-keypair@example.com'; | |
| 166 $decryptPassphrase = 'test1'; | |
| 167 | |
| 168 $this->gpg->addSignKey($signKey); | |
| 169 $this->gpg->addEncryptKey($encryptKey); | |
| 170 $encryptedSignedData = $this->gpg->encryptAndSign($data); | |
| 171 | |
| 172 $this->gpg->addDecryptKey($encryptKey, $decryptPassphrase); | |
| 173 $results = $this->gpg->decryptAndVerify($encryptedSignedData); | |
| 174 | |
| 175 $this->assertEquals($data, $results['data']); | |
| 176 $this->assertEquals(1, count($results['signatures'])); | |
| 177 foreach ($results['signatures'] as $signature) { | |
| 178 $this->assertTrue($signature->isValid()); | |
| 179 } | |
| 180 } | |
| 181 | |
| 182 // }}} | |
| 183 // {{{ testEncryptAndSign() | |
| 184 | |
| 185 /** | |
| 186 * @group string | |
| 187 */ | |
| 188 public function testEncryptAndSign() | |
| 189 { | |
| 190 $data = 'Hello, Alice! Goodbye, Bob!'; | |
| 191 | |
| 192 $signKey = 'first-keypair@example.com'; | |
| 193 $signPassphrase = 'test1'; | |
| 194 $encryptKey = 'first-keypair@example.com'; | |
| 195 $decryptPassphrase = 'test1'; | |
| 196 | |
| 197 $this->gpg->addSignKey($signKey, $signPassphrase); | |
| 198 $this->gpg->addEncryptKey($encryptKey); | |
| 199 $encryptedSignedData = $this->gpg->encryptAndSign($data); | |
| 200 | |
| 201 $this->gpg->addDecryptKey($encryptKey, $decryptPassphrase); | |
| 202 $results = $this->gpg->decryptAndVerify($encryptedSignedData); | |
| 203 | |
| 204 $this->assertEquals($data, $results['data']); | |
| 205 $this->assertEquals(1, count($results['signatures'])); | |
| 206 foreach ($results['signatures'] as $signature) { | |
| 207 $this->assertTrue($signature->isValid()); | |
| 208 } | |
| 209 } | |
| 210 | |
| 211 // }}} | |
| 212 // {{{ testEncryptAndSignDualOnePassphrase() | |
| 213 | |
| 214 /** | |
| 215 * @group string | |
| 216 */ | |
| 217 public function testEncryptAndSignDualOnePassphrase() | |
| 218 { | |
| 219 $data = 'Hello, Alice! Goodbye, Bob!'; | |
| 220 | |
| 221 $signKey1 = 'first-keypair@example.com'; | |
| 222 $signPassphrase1 = 'test1'; | |
| 223 $signKey2 = 'no-passphrase@example.com'; | |
| 224 $encryptKey = 'first-keypair@example.com'; | |
| 225 $decryptPassphrase = 'test1'; | |
| 226 | |
| 227 $this->gpg->addSignKey($signKey1, $signPassphrase1); | |
| 228 $this->gpg->addSignKey($signKey2); | |
| 229 $this->gpg->addEncryptKey($encryptKey); | |
| 230 $encryptedSignedData = $this->gpg->encryptAndSign($data); | |
| 231 | |
| 232 $this->gpg->addDecryptKey($encryptKey, $decryptPassphrase); | |
| 233 $results = $this->gpg->decryptAndVerify($encryptedSignedData); | |
| 234 | |
| 235 $this->assertEquals($data, $results['data']); | |
| 236 $this->assertEquals(2, count($results['signatures'])); | |
| 237 foreach ($results['signatures'] as $signature) { | |
| 238 $this->assertTrue($signature->isValid()); | |
| 239 } | |
| 240 } | |
| 241 | |
| 242 // }}} | |
| 243 // {{{ testEncryptAndSignDual() | |
| 244 | |
| 245 /** | |
| 246 * @group string | |
| 247 */ | |
| 248 public function testEncryptAndSignDual() | |
| 249 { | |
| 250 $data = 'Hello, Alice! Goodbye, Bob!'; | |
| 251 | |
| 252 $signKey1 = 'first-keypair@example.com'; | |
| 253 $signPassphrase1 = 'test1'; | |
| 254 $signKey2 = 'second-keypair@example.com'; | |
| 255 $signPassphrase2 = 'test2'; | |
| 256 $encryptKey = 'first-keypair@example.com'; | |
| 257 $decryptPassphrase = 'test1'; | |
| 258 | |
| 259 $this->gpg->addSignKey($signKey1, $signPassphrase1); | |
| 260 $this->gpg->addSignKey($signKey2, $signPassphrase2); | |
| 261 $this->gpg->addEncryptKey($encryptKey); | |
| 262 $encryptedSignedData = $this->gpg->encryptAndSign($data); | |
| 263 | |
| 264 $this->gpg->addDecryptKey($encryptKey, $decryptPassphrase); | |
| 265 $results = $this->gpg->decryptAndVerify($encryptedSignedData); | |
| 266 | |
| 267 $this->assertEquals($data, $results['data']); | |
| 268 $this->assertEquals(2, count($results['signatures'])); | |
| 269 foreach ($results['signatures'] as $signature) { | |
| 270 $this->assertTrue($signature->isValid()); | |
| 271 } | |
| 272 } | |
| 273 | |
| 274 // }}} | |
| 275 // {{{ testEncryptAndSignEmpty() | |
| 276 | |
| 277 /** | |
| 278 * @group string | |
| 279 */ | |
| 280 public function testEncryptAndSignEmpty() | |
| 281 { | |
| 282 $data = ''; | |
| 283 | |
| 284 $this->gpg->addSignKey('first-keypair@example.com', 'test1'); | |
| 285 $this->gpg->addEncryptKey('first-keypair@example.com'); | |
| 286 $encryptedSignedData = $this->gpg->encryptAndSign($data); | |
| 287 | |
| 288 $this->gpg->addDecryptKey('first-keypair@example.com', 'test1'); | |
| 289 $results = $this->gpg->decryptAndVerify($encryptedSignedData); | |
| 290 | |
| 291 $this->assertEquals('', $results['data']); | |
| 292 $this->assertEquals(1, count($results['signatures'])); | |
| 293 foreach ($results['signatures'] as $signature) { | |
| 294 $this->assertTrue($signature->isValid()); | |
| 295 } | |
| 296 } | |
| 297 | |
| 298 // }}} | |
| 299 // {{{ testEncryptAndSignFileNoPassphrase() | |
| 300 | |
| 301 /** | |
| 302 * @group file | |
| 303 */ | |
| 304 public function testEncryptAndSignFileNoPassphrase() | |
| 305 { | |
| 306 $expectedMd5Sum = 'f96267d87551ee09bfcac16921e351c1'; | |
| 307 $originalFilename = $this->getDataFilename('testFileMedium.plain'); | |
| 308 $encryptedFilename = | |
| 309 $this->getTempFilename('testEncryptAndSignFileNoPassphrase.asc'); | |
| 310 | |
| 311 $decryptedFilename = | |
| 312 $this->getTempFilename('testEncryptAndSignFileNoPassphrase.plain'); | |
| 313 | |
| 314 $this->gpg->addSignKey('no-passphrase@example.com'); | |
| 315 $this->gpg->addEncryptKey('first-keypair@example.com'); | |
| 316 $this->gpg->encryptAndSignFile($originalFilename, $encryptedFilename); | |
| 317 | |
| 318 $this->gpg->addDecryptKey('first-keypair@example.com', 'test1'); | |
| 319 $results = $this->gpg->decryptAndVerifyFile($encryptedFilename, | |
| 320 $decryptedFilename); | |
| 321 | |
| 322 $md5Sum = $this->getMd5Sum($decryptedFilename); | |
| 323 $this->assertEquals($expectedMd5Sum, $md5Sum); | |
| 324 | |
| 325 $this->assertEquals(1, count($results['signatures'])); | |
| 326 foreach ($results['signatures'] as $signature) { | |
| 327 $this->assertTrue($signature->isValid()); | |
| 328 } | |
| 329 } | |
| 330 | |
| 331 // }}} | |
| 332 // {{{ testEncryptAndSignFile() | |
| 333 | |
| 334 /** | |
| 335 * @group file | |
| 336 */ | |
| 337 public function testEncryptAndSignFile() | |
| 338 { | |
| 339 $expectedMd5Sum = 'f96267d87551ee09bfcac16921e351c1'; | |
| 340 $originalFilename = $this->getDataFilename('testFileMedium.plain'); | |
| 341 $encryptedFilename = | |
| 342 $this->getTempFilename('testEncryptAndSignFileNoPassphrase.asc'); | |
| 343 | |
| 344 $decryptedFilename = | |
| 345 $this->getTempFilename('testEncryptAndSignFileNoPassphrase.plain'); | |
| 346 | |
| 347 $this->gpg->addSignKey('first-keypair@example.com', 'test1'); | |
| 348 $this->gpg->addEncryptKey('first-keypair@example.com'); | |
| 349 $this->gpg->encryptAndSignFile($originalFilename, $encryptedFilename); | |
| 350 | |
| 351 $this->gpg->addDecryptKey('first-keypair@example.com', 'test1'); | |
| 352 $results = $this->gpg->decryptAndVerifyFile($encryptedFilename, | |
| 353 $decryptedFilename); | |
| 354 | |
| 355 $md5Sum = $this->getMd5Sum($decryptedFilename); | |
| 356 $this->assertEquals($expectedMd5Sum, $md5Sum); | |
| 357 | |
| 358 $this->assertEquals(1, count($results['signatures'])); | |
| 359 foreach ($results['signatures'] as $signature) { | |
| 360 $this->assertTrue($signature->isValid()); | |
| 361 } | |
| 362 } | |
| 363 | |
| 364 // }}} | |
| 365 // {{{ testEncryptAndSignFileDualOnePassphrase() | |
| 366 | |
| 367 /** | |
| 368 * @group file | |
| 369 */ | |
| 370 public function testEncryptAndSignFileDualOnePassphrase() | |
| 371 { | |
| 372 $expectedMd5Sum = 'f96267d87551ee09bfcac16921e351c1'; | |
| 373 $originalFilename = $this->getDataFilename('testFileMedium.plain'); | |
| 374 $encryptedFilename = | |
| 375 $this->getTempFilename('testEncryptAndSignFileNoPassphrase.asc'); | |
| 376 | |
| 377 $decryptedFilename = | |
| 378 $this->getTempFilename('testEncryptAndSignFileNoPassphrase.plain'); | |
| 379 | |
| 380 $this->gpg->addSignKey('first-keypair@example.com', 'test1'); | |
| 381 $this->gpg->addSignKey('no-passphrase@example.com'); | |
| 382 $this->gpg->addEncryptKey('first-keypair@example.com'); | |
| 383 $this->gpg->encryptAndSignFile($originalFilename, $encryptedFilename); | |
| 384 | |
| 385 $this->gpg->addDecryptKey('first-keypair@example.com', 'test1'); | |
| 386 $results = $this->gpg->decryptAndVerifyFile($encryptedFilename, | |
| 387 $decryptedFilename); | |
| 388 | |
| 389 $md5Sum = $this->getMd5Sum($decryptedFilename); | |
| 390 $this->assertEquals($expectedMd5Sum, $md5Sum); | |
| 391 | |
| 392 $this->assertEquals(2, count($results['signatures'])); | |
| 393 foreach ($results['signatures'] as $signature) { | |
| 394 $this->assertTrue($signature->isValid()); | |
| 395 } | |
| 396 } | |
| 397 | |
| 398 // }}} | |
| 399 // {{{ testEncryptAndSignFileDual() | |
| 400 | |
| 401 /** | |
| 402 * @group file | |
| 403 */ | |
| 404 public function testEncryptAndSignFileDual() | |
| 405 { | |
| 406 $expectedMd5Sum = 'f96267d87551ee09bfcac16921e351c1'; | |
| 407 $originalFilename = $this->getDataFilename('testFileMedium.plain'); | |
| 408 $encryptedFilename = | |
| 409 $this->getTempFilename('testEncryptAndSignFileNoPassphrase.asc'); | |
| 410 | |
| 411 $decryptedFilename = | |
| 412 $this->getTempFilename('testEncryptAndSignFileNoPassphrase.plain'); | |
| 413 | |
| 414 $this->gpg->addSignKey('first-keypair@example.com', 'test1'); | |
| 415 $this->gpg->addSignKey('second-keypair@example.com', 'test2'); | |
| 416 $this->gpg->addEncryptKey('first-keypair@example.com'); | |
| 417 $this->gpg->encryptAndSignFile($originalFilename, $encryptedFilename); | |
| 418 | |
| 419 $this->gpg->addDecryptKey('first-keypair@example.com', 'test1'); | |
| 420 $results = $this->gpg->decryptAndVerifyFile($encryptedFilename, | |
| 421 $decryptedFilename); | |
| 422 | |
| 423 $md5Sum = $this->getMd5Sum($decryptedFilename); | |
| 424 $this->assertEquals($expectedMd5Sum, $md5Sum); | |
| 425 | |
| 426 $this->assertEquals(2, count($results['signatures'])); | |
| 427 foreach ($results['signatures'] as $signature) { | |
| 428 $this->assertTrue($signature->isValid()); | |
| 429 } | |
| 430 } | |
| 431 | |
| 432 // }}} | |
| 433 // {{{ testEncryptAndSignFileFileException_input() | |
| 434 | |
| 435 /** | |
| 436 * @expectedException Crypt_GPG_FileException | |
| 437 * | |
| 438 * @group file | |
| 439 */ | |
| 440 public function testEncryptAndSignFileFileException_input() | |
| 441 { | |
| 442 // input file does not exist | |
| 443 $inputFilename = $this->getDataFilename( | |
| 444 'testEncryptAndSignFileFileFileException_input.plain'); | |
| 445 | |
| 446 $this->gpg->addSignKey('first-keypair@example.com', 'test1'); | |
| 447 $this->gpg->addEncryptKey('first-keypair@example.com'); | |
| 448 $this->gpg->encryptAndSignFile($inputFilename); | |
| 449 } | |
| 450 | |
| 451 // }}} | |
| 452 // {{{ testEncryptAndSignFileFileException_output() | |
| 453 | |
| 454 /** | |
| 455 * @expectedException Crypt_GPG_FileException | |
| 456 * | |
| 457 * @group file | |
| 458 */ | |
| 459 public function testEncryptAndSignFileFileException_output() | |
| 460 { | |
| 461 // input file is plaintext | |
| 462 // output file does not exist | |
| 463 $inputFilename = $this->getDataFilename('testFileMedium.plain'); | |
| 464 $outputFilename = './non-existent' . | |
| 465 '/testEncryptAndSignFileFileException_output.plain'; | |
| 466 | |
| 467 $this->gpg->addSignKey('first-keypair@example.com', 'test1'); | |
| 468 $this->gpg->addEncryptKey('first-keypair@example.com'); | |
| 469 $this->gpg->encryptAndSignFile($inputFilename, $outputFilename); | |
| 470 } | |
| 471 | |
| 472 // }}} | |
| 473 // {{{ testEncryptAndSignFileEmpty() | |
| 474 | |
| 475 /** | |
| 476 * @group file | |
| 477 */ | |
| 478 public function testEncryptAndSignFileEmpty() | |
| 479 { | |
| 480 $originalFilename = $this->getDataFilename('testFileEmpty.plain'); | |
| 481 $encryptedFilename = | |
| 482 $this->getTempFilename('testEncryptAndSignFileEmpty.asc'); | |
| 483 | |
| 484 $this->gpg->addSignKey('first-keypair@example.com', 'test1'); | |
| 485 $this->gpg->addEncryptKey('first-keypair@example.com'); | |
| 486 $this->gpg->encryptAndSignFile($originalFilename, $encryptedFilename); | |
| 487 | |
| 488 $this->gpg->addDecryptKey('first-keypair@example.com', 'test1'); | |
| 489 $results = $this->gpg->decryptAndVerifyFile($encryptedFilename); | |
| 490 | |
| 491 $this->assertEquals('', $results['data']); | |
| 492 | |
| 493 $this->assertEquals(1, count($results['signatures'])); | |
| 494 foreach ($results['signatures'] as $signature) { | |
| 495 $this->assertTrue($signature->isValid()); | |
| 496 } | |
| 497 } | |
| 498 | |
| 499 // }}} | |
| 500 } | |
| 501 | |
| 502 ?> |
