Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Skip tests based on generateKeys
Browse files Browse the repository at this point in the history
- openssl_pkey_new requires a known openssl.cnf location; allow defining
  in test configuration
- note: still has problems even when value is known. Likely a PHP bug
  • Loading branch information
weierophinney committed Oct 26, 2011
1 parent d0565c8 commit a05a234
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/Rsa.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function setOptions(array $options)
if (isset($options['passPhrase'])) {
$this->_passPhrase = $options['passPhrase'];
}
foreach ($options as $option=>$value) {
foreach ($options as $option => $value) {
switch ($option) {
case 'pemString':
$this->setPemString($value);
Expand Down Expand Up @@ -196,8 +196,9 @@ public function decrypt($data, Rsa\Key $key, $format = null)

public function generateKeys(array $configargs = null)
{
$config = null;
$passPhrase = null;
$config = array();
$passPhrase = $this->_passPhrase;
$private = $this->getPrivateKey();
if ($configargs !== null) {
if (isset($configargs['passPhrase'])) {
$passPhrase = $configargs['passPhrase'];
Expand Down
56 changes: 46 additions & 10 deletions test/RSATest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ class RSATest extends \PHPUnit_Framework_TestCase

public function setUp()
{
$openSslConf = false;
if (isset($_ENV['OPENSSL_CONF'])) {
$openSslConf = $_ENV['OPENSSL_CONF'];
} elseif (isset($_ENV['SSLEAY_CONF'])) {
$openSslConf = $_ENV['SSLEAY_CONF'];
} elseif (constant('TESTS_ZEND_CRYPT_OPENSSL_CONF')) {
$openSslConf = constant('TESTS_ZEND_CRYPT_OPENSSL_CONF');
}
$this->openSslConf = $openSslConf;

try {
$math = new \Zend\Crypt\Rsa();
} catch (\Zend\Crypt\Rsa\Exception $e) {
Expand Down Expand Up @@ -272,37 +282,59 @@ public function testEncryptionUsingPrivateKeyBase64Encryption()

public function testKeyGenerationCreatesArrayObjectResult()
{
$rsa = new RSA;
$keys = $rsa->generateKeys(array('private_key_bits'=>512));
if (!$this->openSslConf) {
$this->markTestSkipped('No openssl.cnf found or defined; cannot generate keys');
}
$rsa = new RSA;
$keys = $rsa->generateKeys(array(
'config' => $this->openSslConf,
'private_key_bits' => 512,
));
$this->assertInstanceOf('ArrayObject', $keys);
}

public function testKeyGenerationCreatesPrivateKeyInArrayObject()
{
if (!$this->openSslConf) {
$this->markTestSkipped('No openssl.cnf found or defined; cannot generate keys');
}
$rsa = new RSA;
$keys = $rsa->generateKeys(array('private_key_bits'=>512));
$keys = $rsa->generateKeys(array(
'config' => $this->openSslConf,
'private_key_bits' => 512,
));
$this->assertInstanceOf('Zend\\Crypt\\RSA\\PrivateKey', $keys->privateKey);
}

public function testKeyGenerationCreatesPublicKeyInArrayObject()
{
if (!$this->openSslConf) {
$this->markTestSkipped('No openssl.cnf found or defined; cannot generate keys');
}
$rsa = new RSA;
$keys = $rsa->generateKeys(array('privateKeyBits'=>512));
$keys = $rsa->generateKeys(array(
'config' => $this->openSslConf,
'privateKeyBits' => 512,
));
$this->assertInstanceOf('Zend\\Crypt\\RSA\\PublicKey', $keys->publicKey);
}

public function testKeyGenerationCreatesPassphrasedPrivateKey()
{
if (!$this->openSslConf) {
$this->markTestSkipped('No openssl.cnf found or defined; cannot generate keys');
}
$rsa = new RSA;
$config = array(
'config' => $this->openSslConf,
'privateKeyBits' => 512,
'passPhrase' => '0987654321'
'passPhrase' => '0987654321'
);
$keys = $rsa->generateKeys($config);
try {
$rsa = new RSA(array(
'passPhrase'=>'1234567890',
'pemString'=>$keys->privateKey->toString()
'passPhrase' => '1234567890',
'pemString' => $keys->privateKey->toString()
));
$this->fail('Expected exception not thrown');
} catch (Crypt\Exception $e) {
Expand All @@ -311,16 +343,20 @@ public function testKeyGenerationCreatesPassphrasedPrivateKey()

public function testConstructorLoadsPassphrasedKeys()
{
if (!$this->openSslConf) {
$this->markTestSkipped('No openssl.cnf found or defined; cannot generate keys');
}
$rsa = new RSA;
$config = array(
'config' => $this->openSslConf,
'privateKeyBits' => 512,
'passPhrase' => '0987654321'
'passPhrase' => '0987654321'
);
$keys = $rsa->generateKeys($config);
try {
$rsa = new RSA(array(
'passPhrase'=>'0987654321',
'pemString'=>$keys->privateKey->toString()
'passPhrase' => '0987654321',
'pemString' => $keys->privateKey->toString()
));
} catch (Crypt\Exception $e) {
$this->fail('Passphrase loading failed of a private key');
Expand Down

0 comments on commit a05a234

Please sign in to comment.