Skip to content

Commit

Permalink
Convert to data provider
Browse files Browse the repository at this point in the history
  • Loading branch information
slusarz committed Apr 21, 2015
1 parent 48c72f4 commit 5e5baa9
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 78 deletions.
96 changes: 56 additions & 40 deletions framework/Crypt_Blowfish/test/Horde/Crypt/Blowfish/CbcTest.php
Expand Up @@ -12,65 +12,81 @@
*/
class Horde_Crypt_Blowfish_CbcTest extends Horde_Test_Case
{
private $vectors = array();

public function setUp()
{
foreach (file(dirname(__FILE__) . '/fixtures/vectors_cbc.txt') as $val) {
list($key, $iv, $plain) = explode(' ', trim($val));
$this->vectors[] = array(
'key' => pack("H*", $key),
'iv' => pack("H*", $iv),
'plain' => pack("H*", $plain)
);
}
}

public function testOpensslDriver()
/**
* @dataProvider vectorProvider
*/
public function testOpensslDriver($vector)
{
if (!Horde_Crypt_Blowfish_Openssl::supported()) {
$this->markTestSkipped();
} else {
$this->_doTest(0);
}

$this->_doTest($vector, 0);
}

public function testMcryptDriver()
/**
* @dataProvider vectorProvider
*/
public function testMcryptDriver($vector)
{
if (!Horde_Crypt_Blowfish_Mcrypt::supported()) {
$this->markTestSkipped();
} else {
$this->_doTest(Horde_Crypt_Blowfish::IGNORE_OPENSSL);
}

$this->_doTest($vector, Horde_Crypt_Blowfish::IGNORE_OPENSSL);
}

public function testPhpDriver()
/**
* @dataProvider vectorProvider
*/
public function testPhpDriver($vector)
{
$this->_doTest(Horde_Crypt_Blowfish::IGNORE_OPENSSL | Horde_Crypt_Blowfish::IGNORE_MCRYPT);
$this->_doTest(
$vector,
Horde_Crypt_Blowfish::IGNORE_OPENSSL |
Horde_Crypt_Blowfish::IGNORE_MCRYPT
);
}

protected function _doTest($ignore)
public function vectorProvider()
{
foreach ($this->vectors as $val) {
$ob = new Horde_Crypt_Blowfish($val['key'], array(
'cipher' => 'cbc',
'ignore' => $ignore,
'iv' => $val['iv']
));

$encrypt = $ob->encrypt($val['plain']);
$data = file(dirname(__FILE__) . '/fixtures/vectors_cbc.txt');
$vectors = array();

// Let's verify some sort of obfuscation occurred.
$this->assertNotEquals(
$val['plain'],
$encrypt
);

$this->assertEquals(
$val['plain'],
$ob->decrypt($encrypt)
foreach ($data as $val) {
list($key, $iv, $plain) = explode(' ', trim($val));
$vectors[] = array(
array(
'key' => pack("H*", $key),
'iv' => pack("H*", $iv),
'plain' => pack("H*", $plain)
)
);
}

return $vectors;
}

protected function _doTest($v, $ignore)
{
$ob = new Horde_Crypt_Blowfish($v['key'], array(
'cipher' => 'cbc',
'ignore' => $ignore,
'iv' => $v['iv']
));

$encrypt = $ob->encrypt($v['plain']);

// Let's verify some sort of obfuscation occurred.
$this->assertNotEquals(
$v['plain'],
$encrypt
);

$this->assertEquals(
$v['plain'],
$ob->decrypt($encrypt)
);
}

}
90 changes: 52 additions & 38 deletions framework/Crypt_Blowfish/test/Horde/Crypt/Blowfish/EcbTest.php
Expand Up @@ -12,65 +12,79 @@
*/
class Horde_Crypt_Blowfish_EcbTest extends Horde_Test_Case
{
private $vectors = array();

public function setUp()
{
foreach (file(dirname(__FILE__) . '/fixtures/vectors.txt') as $val) {
list($key, $plain) = explode(' ', trim($val));
$this->vectors[] = array(
'key' => pack("H*", $key),
'plain' => pack("H*", $plain)
);
}
}

public function testOpensslDriver()
/**
* @dataProvider vectorProvider
*/
public function testOpensslDriver($vector)
{
if (!Horde_Crypt_Blowfish_Openssl::supported()) {
$this->markTestSkipped('OpenSSL not installed.');
} else {
$this->_doTest(0);
$this->markTestSkipped();
}

$this->_doTest($vector, 0);
}

public function testMcryptDriver()
/**
* @dataProvider vectorProvider
*/
public function testMcryptDriver($vector)
{
if (!Horde_Crypt_Blowfish_Mcrypt::supported()) {
$this->markTestSkipped();
} else {
$this->_doTest(Horde_Crypt_Blowfish::IGNORE_OPENSSL);
}

$this->_doTest($vector, Horde_Crypt_Blowfish::IGNORE_OPENSSL);
}

/**
* @dataProvider vectorProvider
*/
public function testPhpDriver($vector)
{
$this->_doTest(
$vector,
Horde_Crypt_Blowfish::IGNORE_OPENSSL |
Horde_Crypt_Blowfish::IGNORE_MCRYPT
);
}

public function testPhpDriver()
public function vectorProvider()
{
$this->_doTest(Horde_Crypt_Blowfish::IGNORE_OPENSSL | Horde_Crypt_Blowfish::IGNORE_MCRYPT);
$data = file(dirname(__FILE__) . '/fixtures/vectors.txt');
$vectors = array();

foreach ($data as $val) {
list($key, $plain) = explode(' ', trim($val));
$vectors[] = array(
array(
'key' => pack("H*", $key),
'plain' => pack("H*", $plain)
)
);
}

return $vectors;
}

protected function _doTest($ignore)
protected function _doTest($v, $ignore)
{
$ob = new Horde_Crypt_Blowfish('test', array(
$ob = new Horde_Crypt_Blowfish($v['key'], array(
'cipher' => 'ecb',
'ignore' => $ignore
));

foreach ($this->vectors as $val) {
$ob->setKey($val['key']);

$encrypt = $ob->encrypt($val['plain']);
$encrypt = $ob->encrypt($v['plain']);

// Let's verify some sort of obfuscation occurred.
$this->assertNotEquals(
$val['plain'],
$encrypt
);
// Let's verify some sort of obfuscation occurred.
$this->assertNotEquals(
$v['plain'],
$encrypt
);

$this->assertEquals(
$val['plain'],
$ob->decrypt($encrypt)
);
}
$this->assertEquals(
$v['plain'],
$ob->decrypt($encrypt)
);
}

}

0 comments on commit 5e5baa9

Please sign in to comment.