Bläddra i källkod

Zend_Crypt: improve required extensions management, correct unit tests.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23438 44c647ce-9c0f-0410-b52a-842ac1e357ba
alexander 15 år sedan
förälder
incheckning
5315d97935

+ 1 - 1
library/Zend/Crypt/Math/BigInteger.php

@@ -105,7 +105,7 @@ class Zend_Crypt_Math_BigInteger
         //} elseif($extension == 'bigint' && extension_loaded('big_int')) {
         //    require_once 'Zend/Crypt_Math/BigInteger/Bigint.php';
         //    $this->_math = new Zend_Crypt_Math_BigInteger_Bigint();
-        } elseif ($extension == 'bcmath') {
+        } elseif ($extension == 'bcmath' && extension_loaded('bcmath')) {
             require_once 'Zend/Crypt/Math/BigInteger/Bcmath.php';
             $this->_math = new Zend_Crypt_Math_BigInteger_Bcmath();
         } else {

+ 1 - 1
library/Zend/Crypt/Math/BigInteger/Gmp.php

@@ -28,7 +28,7 @@ require_once 'Zend/Crypt/Math/BigInteger/Interface.php';
 /**
  * Support for arbitrary precision mathematics in PHP.
  *
- * Zend_Crypt_Math_BigInteger_Bcmath is a wrapper across the PHP BCMath
+ * Zend_Crypt_Math_BigInteger_Gmp is a wrapper across the PHP BCMath
  * extension.
  *
  * @category   Zend

+ 23 - 8
library/Zend/Crypt/Rsa.php

@@ -42,27 +42,42 @@ class Zend_Crypt_Rsa
     const BINARY = 'binary';
     const BASE64 = 'base64';
 
-    protected $_privateKey = null;
+    protected $_privateKey;
 
-    protected $_publicKey = null;
+    protected $_publicKey;
 
     /**
      * @var string
      */
-    protected $_pemString = null;
+    protected $_pemString;
 
-    protected $_pemPath = null;
+    protected $_pemPath;
 
-    protected $_certificateString = null;
+    protected $_certificateString;
 
-    protected $_certificatePath = null;
+    protected $_certificatePath;
 
-    protected $_hashAlgorithm = OPENSSL_ALGO_SHA1;
+    protected $_hashAlgorithm;
 
-    protected $_passPhrase = null;
+    protected $_passPhrase;
 
+    /**
+     * Class constructor
+     *
+     * @param array $options
+     * @throws Zend_Crypt_Rsa_Exception
+     */
     public function __construct(array $options = null)
     {
+        if (!extension_loaded('openssl')) {
+            require_once 'Zend/Crypt/Rsa/Exception.php';
+            throw new Zend_Crypt_Rsa_Exception('Zend_Crypt_Rsa requires openssl extention to be loaded.');
+        }
+
+        // Set _hashAlgorithm property when we are sure, that openssl extension is loaded
+        // and OPENSSL_ALGO_SHA1 constant is available
+        $this->_hashAlgorithm = OPENSSL_ALGO_SHA1;
+
         if (isset($options)) {
             $this->setOptions($options);
         }

+ 36 - 0
library/Zend/Crypt/Rsa/Exception.php

@@ -0,0 +1,36 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Crypt
+ * @subpackage Math
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: Exception.php 20096 2010-01-06 02:05:09Z bkarwin $
+ */
+
+/**
+ * @see Zend_Crypt_Exception
+ */
+require_once 'Zend/Crypt/Exception.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Crypt
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Crypt_Rsa_Exception extends Zend_Crypt_Exception
+{
+}

+ 13 - 0
tests/Zend/Crypt/DiffieHellmanTest.php

@@ -21,6 +21,7 @@
  */
 
 require_once 'Zend/Crypt/DiffieHellman.php';
+require_once 'Zend/Crypt/Math/BigInteger.php';
 require_once 'PHPUnit/Framework/TestCase.php';
 
 /**
@@ -33,6 +34,18 @@ require_once 'PHPUnit/Framework/TestCase.php';
  */
 class Zend_Crypt_DiffieHellmanTest extends PHPUnit_Framework_TestCase
 {
+    public function setUp()
+    {
+        try {
+            $math = new Zend_Crypt_Math_BigInteger();
+        } catch (Zend_Crypt_Math_BigInteger_Exception $e) {
+            if (strpos($e->getMessage(), 'big integer precision math support not detected') !== false) {
+                $this->markTestSkipped($e->getMessage());
+            } else {
+                throw $e;
+            }
+        }
+    }
 
     public function testDiffieWithSpec()
     {

+ 10 - 1
tests/Zend/Crypt/MathTest.php

@@ -37,7 +37,16 @@ class Zend_Crypt_MathTest extends PHPUnit_Framework_TestCase
 
     public function testRand()
     {
-        $math = new Zend_Crypt_Math;
+        try {
+            $math = new Zend_Crypt_Math_BigInteger();
+        } catch (Zend_Crypt_Math_BigInteger_Exception $e) {
+            if (strpos($e->getMessage(), 'big integer precision math support not detected') !== false) {
+                $this->markTestSkipped($e->getMessage());
+            } else {
+                throw $e;
+            }
+        }
+
         $higher = '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443';
         $lower  = '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638442';
         $result = $math->rand($lower, $higher);

+ 11 - 1
tests/Zend/Crypt/Rsa/RsaTest.php

@@ -39,8 +39,18 @@ class Zend_Crypt_RsaTest extends PHPUnit_Framework_TestCase
 
     protected $_testPemPath = null;
 
-    public function setup()
+    public function setUp()
     {
+        try {
+            $rsaObject = new Zend_Crypt_Rsa();
+        } catch (Zend_Crypt_Rsa_Exception $e) {
+            if (strpos($e->getMessage(), 'requires openssl extention') !== false) {
+                $this->markTestSkipped($e->getMessage());
+            } else {
+                throw $e;
+            }
+        }
+
         $this->_testPemString = <<<RSAKEY
 -----BEGIN RSA PRIVATE KEY-----
 MIIBOgIBAAJBANDiE2+Xi/WnO+s120NiiJhNyIButVu6zxqlVzz0wy2j4kQVUC4Z