CipherTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_InfoCard
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_InfoCard_ProcessTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_InfoCard_CipherTest::main");
  25. }
  26. require_once 'Zend/InfoCard.php';
  27. require_once 'Zend/InfoCard/Cipher/Pki/Adapter/Rsa.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_InfoCard
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_InfoCard
  35. */
  36. class Zend_InfoCard_CipherTest extends PHPUnit_Framework_TestCase
  37. {
  38. public function testPkiPadding()
  39. {
  40. if (!extension_loaded('openssl')) {
  41. $this->markTestSkipped('The openssl extension is not loaded.');
  42. }
  43. try {
  44. $obj = new Zend_InfoCard_Cipher_Pki_Adapter_Rsa("thiswillbreak");
  45. $this->fail("Exception not thrown as expected");
  46. } catch(Exception $e) {
  47. /* yay */
  48. }
  49. $obj = new Zend_InfoCard_Cipher_Pki_Adapter_Rsa();
  50. $prv_key = file_get_Contents(dirname(__FILE__) . "/_files/ssl_private.cert");
  51. try {
  52. $obj->decrypt("Foo", $prv_key, null, "foo");
  53. $this->fail("Expected Exception Not Thrown");
  54. } catch(Exception $e) {
  55. /* yay */
  56. }
  57. $result = $obj->decrypt("foo", $prv_key, null, Zend_InfoCard_Cipher_Pki_Adapter_Abstract::NO_PADDING);
  58. // This is sort of werid, but since we don't have a real PK-encrypted string to test against for NO_PADDING
  59. // mode we decrypt the string "foo" instead. Mathmatically we will always arrive at the same resultant
  60. // string so if our hash doesn't match then something broke.
  61. $this->assertSame(md5($result), "286c1991e1f7040229a6f223065b91b5");
  62. }
  63. public function testPKIDecryptBadKey()
  64. {
  65. if (!extension_loaded('openssl')) {
  66. $this->markTestSkipped('The openssl extension is not loaded.');
  67. }
  68. $obj = new Zend_InfoCard_Cipher_Pki_Adapter_Rsa();
  69. try {
  70. $obj->decrypt("Foo", "bar");
  71. $this->fail("Exception not thrown as expected");
  72. } catch(Exception $e) {
  73. /* yay */
  74. }
  75. }
  76. public function testCipherFactory()
  77. {
  78. if (!defined('MCRYPT_RIJNDAEL_128')) {
  79. $this->markTestSkipped('Use of the Zend_InfoCard component requires the mcrypt extension to be enabled in PHP');
  80. }
  81. $this->assertTrue(Zend_InfoCard_Cipher::getInstanceByURI(Zend_InfoCard_Cipher::ENC_AES128CBC)
  82. instanceof Zend_InfoCard_Cipher_Symmetric_Adapter_Aes128cbc);
  83. $this->assertTrue(Zend_InfoCard_Cipher::getInstanceByURI(Zend_InfoCard_Cipher::ENC_RSA)
  84. instanceof Zend_InfoCard_Cipher_Pki_Adapter_Rsa);
  85. try {
  86. Zend_InfoCard_Cipher::getInstanceByURI("Broken");
  87. $this->fail("Exception not thrown as expected");
  88. } catch(Exception $e) {
  89. /* yay */
  90. }
  91. }
  92. }