CipherTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. // Call Zend_InfoCard_ProcessTest::main() if this source file is executed directly.
  22. if (!defined("PHPUnit_MAIN_METHOD")) {
  23. define("PHPUnit_MAIN_METHOD", "Zend_InfoCard_CipherTest::main");
  24. }
  25. /**
  26. * Test helper
  27. */
  28. require_once dirname(__FILE__) . '/../../TestHelper.php';
  29. require_once "PHPUnit/Framework/TestCase.php";
  30. require_once "PHPUnit/Framework/TestSuite.php";
  31. require_once 'Zend/InfoCard.php';
  32. require_once 'Zend/InfoCard/Cipher/Pki/Adapter/Rsa.php';
  33. class Zend_InfoCard_CipherTest extends PHPUnit_Framework_TestCase
  34. {
  35. public function testPkiPadding()
  36. {
  37. if (!extension_loaded('openssl')) {
  38. $this->markTestSkipped('The openssl extension is not loaded.');
  39. }
  40. try {
  41. $obj = new Zend_InfoCard_Cipher_Pki_Adapter_Rsa("thiswillbreak");
  42. $this->fail("Exception not thrown as expected");
  43. } catch(Exception $e) {
  44. /* yay */
  45. }
  46. $obj = new Zend_InfoCard_Cipher_Pki_Adapter_Rsa();
  47. $prv_key = file_get_Contents(dirname(__FILE__) . "/_files/ssl_private.cert");
  48. try {
  49. $obj->decrypt("Foo", $prv_key, null, "foo");
  50. $this->fail("Expected Exception Not Thrown");
  51. } catch(Exception $e) {
  52. /* yay */
  53. }
  54. $result = $obj->decrypt("foo", $prv_key, null, Zend_InfoCard_Cipher_Pki_Adapter_Abstract::NO_PADDING);
  55. // This is sort of werid, but since we don't have a real PK-encrypted string to test against for NO_PADDING
  56. // mode we decrypt the string "foo" instead. Mathmatically we will always arrive at the same resultant
  57. // string so if our hash doesn't match then something broke.
  58. $this->assertSame(md5($result), "286c1991e1f7040229a6f223065b91b5");
  59. }
  60. public function testPKIDecryptBadKey()
  61. {
  62. if (!extension_loaded('openssl')) {
  63. $this->markTestSkipped('The openssl extension is not loaded.');
  64. }
  65. $obj = new Zend_InfoCard_Cipher_Pki_Adapter_Rsa();
  66. try {
  67. $obj->decrypt("Foo", "bar");
  68. $this->fail("Exception not thrown as expected");
  69. } catch(Exception $e) {
  70. /* yay */
  71. }
  72. }
  73. public function testCipherFactory()
  74. {
  75. if (!defined('MCRYPT_RIJNDAEL_128')) {
  76. $this->markTestSkipped('Use of the Zend_InfoCard component requires the mcrypt extension to be enabled in PHP');
  77. }
  78. $this->assertTrue(Zend_InfoCard_Cipher::getInstanceByURI(Zend_InfoCard_Cipher::ENC_AES128CBC)
  79. instanceof Zend_InfoCard_Cipher_Symmetric_Adapter_Aes128cbc);
  80. $this->assertTrue(Zend_InfoCard_Cipher::getInstanceByURI(Zend_InfoCard_Cipher::ENC_RSA)
  81. instanceof Zend_InfoCard_Cipher_Pki_Adapter_Rsa);
  82. try {
  83. Zend_InfoCard_Cipher::getInstanceByURI("Broken");
  84. $this->fail("Exception not thrown as expected");
  85. } catch(Exception $e) {
  86. /* yay */
  87. }
  88. }
  89. }