summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tests/unit/Security/TSecurityManagerTest.php109
1 files changed, 94 insertions, 15 deletions
diff --git a/tests/unit/Security/TSecurityManagerTest.php b/tests/unit/Security/TSecurityManagerTest.php
index 76029bde..b5b9bb83 100644
--- a/tests/unit/Security/TSecurityManagerTest.php
+++ b/tests/unit/Security/TSecurityManagerTest.php
@@ -7,52 +7,131 @@ Prado::using('System.Security.TSecurityManager');
* @package System.Security
*/
class TSecurityManagerTest extends PHPUnit_Framework_TestCase {
+ public static $app;
public function setUp() {
+ if (self::$app === null) {
+ self::$app = new TApplication (dirname(__FILE).'/app');
+ }
}
public function tearDown() {
}
public function testInit() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $sec=new TSecurityManager ();
+ $sec->init(null);
+ self::assertEquals ($sec, self::$app->getSecurityManager());
}
public function testValidationKey() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $sec=new TSecurityManager ();
+ $sec->init (null);
+ // Random validation key
+ $valkey=$sec->getValidationKey ();
+ self::assertEquals($valkey, self::$app->getGlobalState(TSecurityManager::STATE_VALIDATION_KEY));
+
+ $sec->setValidationKey ('aKey');
+ self::assertEquals('aKey',$sec->getValidationKey());
+
+ try {
+ $sec->setValidationKey ('');
+ self::fail ('Expected TInvalidDataValueException not thrown');
+ } catch (TInvalidDataValueException $e) {}
}
public function testEncryptionKey() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $sec=new TSecurityManager ();
+ $sec->init (null);
+ // Random encryption key
+ $valkey=$sec->getEncryptionKey ();
+ self::assertEquals($valkey, self::$app->getGlobalState(TSecurityManager::STATE_ENCRYPTION_KEY));
+
+ $sec->setEncryptionKey ('aKey');
+ self::assertEquals('aKey',$sec->getEncryptionKey());
+
+ try {
+ $sec->setEncryptionKey ('');
+ self::fail ('Expected TInvalidDataValueException not thrown');
+ } catch (TInvalidDataValueException $e) {}
}
public function testValidation() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $sec=new TSecurityManager ();
+ $sec->init (null);
+ $sec->setValidation ('MD5');
+ self::assertEquals('MD5',$sec->getValidation());
+ $sec->setValidation ('SHA1');
+ self::assertEquals('SHA1',$sec->getValidation());
+ try {
+ $sec->setValidation ('BAD');
+ self::fail ('Expected TInvalidDataValueException not thrown');
+ } catch (TInvalidDataValueException $e) {}
}
public function testEncryption() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $sec=new TSecurityManager ();
+ $sec->init (null);
+ try {
+ $sec->setEncryption ('DES');
+ self::fail ('Expected TNotSupportedException not thrown');
+ } catch (TNotSupportedException $e) {
+ self::assertEquals('3DES', $sec->getEncryption());
+ }
}
- public function testEncrypt() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ public function testEncryptDecrypt() {
+ $sec=new TSecurityManager ();
+ $sec->init (null);
+ $sec->setEncryptionKey ('aKey');
+ try {
+ $encrypted = $sec->encrypt('a text');
+ } catch (TNotSupportedException $e) {
+ self::markTestSkipped('mcrypt extension not loaded');
+ return;
+ }
+ self::assertEquals('a text', $sec->decrypt($encrypted));
+ // try change key
+ $sec->setEncryptionKey ('anotherKey');
+ self::assertNotEquals('a text', $sec->decrypt($encrypted));
}
- public function testDecrypt() {
- throw new PHPUnit_Framework_IncompleteTestError();
- }
public function testHashData() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $sec=new TSecurityManager ();
+ $sec->init (null);
+ $sec->setValidationKey('aKey');
+ $sec->setValidation('SHA1');
+ $hashed=$sec->hashData('A text to hash');
+ // Lenght of SHA1 hashed data must be 54 (40 + strlen data)
+ self::assertEquals (54, strlen($hashed));
+ // The initial text should be after the initial hash
+ self::assertEquals ('A text to hash', substr($hashed,40));
+
+ // Same tests with MD5
+ $sec->setValidationKey('AnotherKey');
+ $sec->setValidation('MD5');
+ $hashed=$sec->hashData('A text to hash');
+ // Lenght of SHA1 hashed data must be 46 (32 + strlen data)
+ self::assertEquals (46, strlen($hashed));
+ // The initial text should be after the initial hash
+ self::assertEquals ('A text to hash', substr($hashed,32));
}
public function testValidateData() {
- throw new PHPUnit_Framework_IncompleteTestError();
+ $sec=new TSecurityManager ();
+ $sec->init (null);
+ $sec->setValidationKey('aKey');
+ $sec->setValidation('SHA1');
+ $hashed=$sec->hashData('A text to hash');
+ self::assertEquals('A text to hash', $sec->validateData($hashed));
+ // try to alter the hashed data
+ $hashed[45]="z";
+ self::assertFalse($sec->validateData($hashed));
+ // and a test without tampered data
+ self::assertFalse($sec->validateData('bad'));
}
- public function testComputeHMAC() {
- throw new PHPUnit_Framework_IncompleteTestError();
- }
}