145 lines
4.0 KiB
PHP
145 lines
4.0 KiB
PHP
<?php
|
|
/** DomFramework - Tests
|
|
* @package domframework
|
|
* @author Dominique Fournier <dominique@fournier38.fr>
|
|
*/
|
|
|
|
namespace Domframework\Tests;
|
|
|
|
use Domframework\Encrypt;
|
|
|
|
/** Test the Encrypt.php file */
|
|
class EncryptTest extends \PHPUnit_Framework_TestCase
|
|
{
|
|
/** Check the length of the otken with cipher
|
|
*/
|
|
public function testEncrypt1 ()
|
|
{
|
|
$encrypt = new Encrypt ();
|
|
$res = $encrypt->encrypt ("TextToEncode",
|
|
"123456789012345678901234");
|
|
$this->assertSame (strlen ($res), 24);
|
|
}
|
|
|
|
/** Check if the encrypt/decrypt process return the same result
|
|
*/
|
|
public function testEncrypt2 ()
|
|
{
|
|
$encrypt = new Encrypt ();
|
|
$payload = "TextToEncode";
|
|
$ckey = "123456789012345678901234";
|
|
$token = $encrypt->encrypt ($payload, $ckey);
|
|
$res = $encrypt->decrypt ($token, $ckey);
|
|
$this->assertSame ($res, $payload);
|
|
}
|
|
|
|
/** Check if the encrypted part is well unreadable
|
|
*/
|
|
public function testEncrypt3 ()
|
|
{
|
|
$encrypt = new Encrypt ();
|
|
$payload = "TextToEncode";
|
|
$token = $encrypt->encrypt ($payload, "123456789012345678901234");
|
|
$res = strpos ($token, "Text");
|
|
$this->assertSame ($res, false);
|
|
}
|
|
|
|
/** Encrypt : Invalid Cipher method
|
|
*/
|
|
public function testInvalidCypher1 ()
|
|
{
|
|
$this->expectException ("Exception",
|
|
"Invalid cipher provided to encrypt method : doesn't exists in OpenSSL",
|
|
500);
|
|
$encrypt = new Encrypt ();
|
|
$payload = "TextToEncode";
|
|
$token = $encrypt->encrypt ($payload, "123456789012345678901234", "TOTO");
|
|
}
|
|
|
|
/** Encrypt : Invalid Payload to encrypt
|
|
*/
|
|
public function testInvalidPayload ()
|
|
{
|
|
$this->expectException ("Exception",
|
|
"Invalid payload provided to encrypt method : Not a string",
|
|
500);
|
|
$encrypt = new Encrypt ();
|
|
$payload = array ("Invalid");
|
|
$token = $encrypt->encrypt ($payload, "123456789012345678901234");
|
|
}
|
|
|
|
/** Encrypt : Invalid Cipher Key to encrypt
|
|
*/
|
|
public function testInvalidCipherKey ()
|
|
{
|
|
$this->expectException ("Exception",
|
|
"Invalid cipherKey provided to encrypt method : ".
|
|
"length different of 24 chars", 500);
|
|
$encrypt = new Encrypt ();
|
|
$payload = "Payload";
|
|
$token = $encrypt->encrypt ($payload, "124");
|
|
}
|
|
|
|
/** Decrypt : invalid cipher text
|
|
*/
|
|
public function testDecryptInvalidCipherKey ()
|
|
{
|
|
$this->expectException ("Exception",
|
|
"Invalid cipherKey provided to decrypt method : ".
|
|
"length different of 24 chars", 500);
|
|
$encrypt = new Encrypt ();
|
|
$token = $encrypt->decrypt ("zfz", "124");
|
|
}
|
|
|
|
/** Decrypt : empty cipher string
|
|
*/
|
|
public function testDecryptEmptyCipherString ()
|
|
{
|
|
$this->expectException ("Exception",
|
|
"Invalid ciphertext provided to decrypt method : empty string", 500);
|
|
$encrypt = new Encrypt ();
|
|
$token = $encrypt->decrypt ("", "124");
|
|
}
|
|
|
|
/** Decrypt : Not a string cypher
|
|
*/
|
|
public function testDecryptNotStringCipherString ()
|
|
{
|
|
$this->expectException ("Exception",
|
|
"Invalid ciphertext provided to decrypt method : not a string", 500);
|
|
$encrypt = new Encrypt ();
|
|
$token = $encrypt->decrypt (array (), "124");
|
|
}
|
|
|
|
/** Decrypt : Not a string cypher key
|
|
*/
|
|
public function testDecryptNotStringCipherKey ()
|
|
{
|
|
$this->expectException ("Exception",
|
|
"Invalid cipherkey provided to decrypt method : not a string", 500);
|
|
$encrypt = new Encrypt ();
|
|
$token = $encrypt->decrypt ("1224", array ());
|
|
}
|
|
|
|
/** Decrypt : Not a cipher method string
|
|
*/
|
|
public function testDecryptNotStringCipherMethod ()
|
|
{
|
|
$this->expectException ("Exception",
|
|
"Invalid cipherMethod provided to decrypt method : not a string", 500);
|
|
$encrypt = new Encrypt ();
|
|
$token = $encrypt->decrypt ("1224", "1234", array ());
|
|
}
|
|
|
|
/** Decrypt : Not a known cipher method
|
|
*/
|
|
public function testDecryptUnknownCipherMethod ()
|
|
{
|
|
$this->expectException ("Exception",
|
|
"Invalid cipherMethod provided to decrypt method : ".
|
|
"doesn't exists in OpenSSL", 500);
|
|
$encrypt = new Encrypt ();
|
|
$token = $encrypt->decrypt ("1224", "1234", "unknown");
|
|
}
|
|
}
|