CipherTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 setUp()
  39. {
  40. if (version_compare(PHP_VERSION, '5.4', '>=')) {
  41. $this->markTestSkipped('SimpleXML implementation changed and CardSpace technology is discontinued');
  42. }
  43. }
  44. public function testPkiPadding()
  45. {
  46. if (!extension_loaded('openssl')) {
  47. $this->markTestSkipped('The openssl extension is not loaded.');
  48. }
  49. try {
  50. $obj = new Zend_InfoCard_Cipher_Pki_Adapter_Rsa("thiswillbreak");
  51. $this->fail("Exception not thrown as expected");
  52. } catch(Exception $e) {
  53. /* yay */
  54. }
  55. $obj = new Zend_InfoCard_Cipher_Pki_Adapter_Rsa();
  56. $prv_key = file_get_Contents(dirname(__FILE__) . "/_files/ssl_private.cert");
  57. try {
  58. $obj->decrypt("Foo", $prv_key, null, "foo");
  59. $this->fail("Expected Exception Not Thrown");
  60. } catch(Exception $e) {
  61. /* yay */
  62. }
  63. $result = $obj->decrypt("foo", $prv_key, null, Zend_InfoCard_Cipher_Pki_Adapter_Abstract::NO_PADDING);
  64. // This is sort of werid, but since we don't have a real PK-encrypted string to test against for NO_PADDING
  65. // mode we decrypt the string "foo" instead. Mathmatically we will always arrive at the same resultant
  66. // string so if our hash doesn't match then something broke.
  67. $this->assertSame(md5($result), "286c1991e1f7040229a6f223065b91b5");
  68. }
  69. public function testPKIDecryptBadKey()
  70. {
  71. if (!extension_loaded('openssl')) {
  72. $this->markTestSkipped('The openssl extension is not loaded.');
  73. }
  74. $obj = new Zend_InfoCard_Cipher_Pki_Adapter_Rsa();
  75. try {
  76. $obj->decrypt("Foo", "bar");
  77. $this->fail("Exception not thrown as expected");
  78. } catch(Exception $e) {
  79. /* yay */
  80. }
  81. }
  82. public function testCipherFactory()
  83. {
  84. if (!defined('MCRYPT_RIJNDAEL_128')) {
  85. $this->markTestSkipped('Use of the Zend_InfoCard component requires the mcrypt extension to be enabled in PHP');
  86. }
  87. $this->assertTrue(Zend_InfoCard_Cipher::getInstanceByURI(Zend_InfoCard_Cipher::ENC_AES128CBC)
  88. instanceof Zend_InfoCard_Cipher_Symmetric_Adapter_Aes128cbc);
  89. $this->assertTrue(Zend_InfoCard_Cipher::getInstanceByURI(Zend_InfoCard_Cipher::ENC_RSA)
  90. instanceof Zend_InfoCard_Cipher_Pki_Adapter_Rsa);
  91. try {
  92. Zend_InfoCard_Cipher::getInstanceByURI("Broken");
  93. $this->fail("Exception not thrown as expected");
  94. } catch(Exception $e) {
  95. /* yay */
  96. }
  97. }
  98. }