Refactor tests a little bit

This commit is contained in:
Lukas Reschke 2014-09-03 13:51:44 +02:00
parent 20a7fb0334
commit 1ccbaae846
3 changed files with 61 additions and 31 deletions

View file

@ -10,15 +10,28 @@ use \OC\Security\Crypto;
class CryptoTest extends \PHPUnit_Framework_TestCase {
function testDefaultEncrypt() {
$stringToEncrypt = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt.';
$crypto = new Crypto(\OC::$server->getConfig(), \OC::$server->getSecureRandom());
$ciphertext = $crypto->encrypt($stringToEncrypt);
$this->assertEquals($stringToEncrypt, $crypto->decrypt($ciphertext));
public function defaultEncryptionProvider()
{
return array(
array('Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt.'),
array(''),
array('我看这本书。 我看這本書')
);
}
$stringToEncrypt = '';
$ciphertext = $crypto->encrypt($stringToEncrypt);
$this->assertEquals($stringToEncrypt, $crypto->decrypt($ciphertext));
/** @var Crypto */
protected $crypto;
protected function setUp() {
$this->crypto = new Crypto(\OC::$server->getConfig(), \OC::$server->getSecureRandom());
}
/**
* @dataProvider defaultEncryptionProvider
*/
function testDefaultEncrypt($stringToEncrypt) {
$ciphertext = $this->crypto->encrypt($stringToEncrypt);
$this->assertEquals($stringToEncrypt, $this->crypto->decrypt($ciphertext));
}
/**
@ -27,18 +40,14 @@ class CryptoTest extends \PHPUnit_Framework_TestCase {
*/
function testWrongPassword() {
$stringToEncrypt = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt.';
$encryptCrypto = new Crypto(\OC::$server->getConfig(), \OC::$server->getSecureRandom());
$ciphertext = $encryptCrypto->encrypt($stringToEncrypt);
$decryptCrypto = new Crypto(\OC::$server->getConfig(), \OC::$server->getSecureRandom());
$this->assertFalse($decryptCrypto->decrypt($ciphertext, 'A wrong password!'));
$ciphertext = $this->crypto->encrypt($stringToEncrypt);
$this->crypto->decrypt($ciphertext, 'A wrong password!');
}
function testLaterDecryption() {
$stringToEncrypt = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt.';
$encryptedString = '44a35023cca2e7a6125e06c29fc4b2ad9d8a33d0873a8b45b0de4ef9284f260c6c46bf25dc62120644c59b8bafe4281ddc47a70c35ae6c29ef7a63d79eefacc297e60b13042ac582733598d0a6b4de37311556bb5c480fd2633de4e6ebafa868c2d1e2d80a5d24f9660360dba4d6e0c8|lhrFgK0zd9U160Wo|a75e57ab701f9124e1113543fd1dc596f21e20d456a0d1e813d5a8aaec9adcb11213788e96598b67fe9486a9f0b99642c18296d0175db44b1ae426e4e91080ee';
$crypto = new Crypto(\OC::$server->getConfig(), \OC::$server->getSecureRandom());
$this->assertEquals($stringToEncrypt, $crypto->decrypt($encryptedString, 'ThisIsAVeryS3cur3P4ssw0rd'));
$this->assertEquals($stringToEncrypt, $this->crypto->decrypt($encryptedString, 'ThisIsAVeryS3cur3P4ssw0rd'));
}
/**
@ -47,8 +56,7 @@ class CryptoTest extends \PHPUnit_Framework_TestCase {
*/
function testWrongIV() {
$encryptedString = '560f5436ba864b9f12f7f7ca6d41c327554a6f2c0a160a03316b202af07c65163274993f3a46e7547c07ba89304f00594a2f3bd99f83859097c58049c39d0d4ade10e0de914ff0604961e7c849d0271ed6c0b23f984ba16e7d033e3305fb0910e7b6a2a65c988d17dbee71d8f953684d|d2kdFUspVjC0o0sr|1a5feacf87eaa6869a6abdfba9a296e7bbad45b6ad89f7dce67cdc98e2da5dc4379cc672cc655e52bbf19599bf59482fbea13a73937697fa656bf10f3fc4f1aa';
$crypto = new Crypto(\OC::$server->getConfig(), \OC::$server->getSecureRandom());
$crypto->decrypt($encryptedString, 'ThisIsAVeryS3cur3P4ssw0rd');
$this->crypto->decrypt($encryptedString, 'ThisIsAVeryS3cur3P4ssw0rd');
}
/**
@ -57,7 +65,6 @@ class CryptoTest extends \PHPUnit_Framework_TestCase {
*/
function testWrongParameters() {
$encryptedString = '1|2';
$crypto = new Crypto(\OC::$server->getConfig(), \OC::$server->getSecureRandom());
$crypto->decrypt($encryptedString, 'ThisIsAVeryS3cur3P4ssw0rd');
$this->crypto->decrypt($encryptedString, 'ThisIsAVeryS3cur3P4ssw0rd');
}
}

View file

@ -6,6 +6,8 @@
* See the COPYING-README file.
*/
use \OC\Security\SecureRandom;
class SecureRandomTest extends \PHPUnit_Framework_TestCase {
public function stringGenerationProvider() {
@ -20,12 +22,18 @@ class SecureRandomTest extends \PHPUnit_Framework_TestCase {
);
}
/** @var SecureRandom */
protected $rng;
protected function setUp() {
$this->rng = new \OC\Security\SecureRandom();
}
/**
* @dataProvider stringGenerationProvider
*/
function testGetLowStrengthGeneratorLength($length, $expectedLength) {
$rng = new \OC\Security\SecureRandom();
$generator = $rng->getLowStrengthGenerator();
$generator = $this->rng->getLowStrengthGenerator();
$this->assertEquals($expectedLength, strlen($generator->generate($length)));
}
@ -34,8 +42,7 @@ class SecureRandomTest extends \PHPUnit_Framework_TestCase {
* @dataProvider stringGenerationProvider
*/
function testMediumLowStrengthGeneratorLength($length, $expectedLength) {
$rng = new \OC\Security\SecureRandom();
$generator = $rng->getMediumStrengthGenerator();
$generator = $this->rng->getMediumStrengthGenerator();
$this->assertEquals($expectedLength, strlen($generator->generate($length)));
}
@ -45,7 +52,6 @@ class SecureRandomTest extends \PHPUnit_Framework_TestCase {
* @expectedExceptionMessage Generator is not initialized
*/
function testUninitializedGenerate() {
$rng = new \OC\Security\SecureRandom();
$rng->generate(30);
$this->rng->generate(30);
}
}

View file

@ -10,12 +10,29 @@ use \OC\Security\StringUtils;
class StringUtilsTest extends \PHPUnit_Framework_TestCase {
function testEquals() {
$this->assertTrue(StringUtils::equals('GpKY9fSnWRaeFNJbES99zVGvA', 'GpKY9fSnWRaeFNJbES99zVGvA'));
$this->assertFalse(StringUtils::equals('GpKY9fSnWNJbES99zVGvA', 'GpKY9fSnWRaeFNJbES99zVGvA'));
$this->assertFalse(StringUtils::equals('', 'GpKY9fSnWRaeFNJbES99zVGvA'));
$this->assertFalse(StringUtils::equals('GpKY9fSnWRaeFNJbES99zVGvA', ''));
$this->assertTrue(StringUtils::equals('', ''));
public function dataProvider()
{
return array(
array('Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt.', 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt.'),
array('', ''),
array('我看这本书。 我看這本書', '我看这本书。 我看這本書'),
array('GpKY9fSnWNJbES99zVGvA', 'GpKY9fSnWNJbES99zVGvA')
);
}
/**
* @dataProvider dataProvider
*/
function testWrongEquals($string) {
$this->assertFalse(StringUtils::equals($string, 'A Completely Wrong String'));
$this->assertFalse(StringUtils::equals($string, null));
}
/**
* @dataProvider dataProvider
*/
function testTrueEquals($string, $expected) {
$this->assertTrue(StringUtils::equals($string, $expected));
}
}