summaryrefslogtreecommitdiff
path: root/tests/unit/Security/TSecurityManagerTest.php
blob: 09c261cfb668cc9e08ca9efc7c6b07c1c208fc34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php

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() {
		$sec=new TSecurityManager ();
		$sec->init(null);
		self::assertEquals ($sec, self::$app->getSecurityManager());
	}
	
	public function testValidationKey() {
		$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() {
		$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() {
		$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() {
		$sec=new TSecurityManager ();
		$sec->init (null);
		try {
			$sec->setCryptAlgorithm('NotExisting');
			$foo=$sec->encrypt('dummy');
			self::fail ('Expected TNotSupportedException not thrown');
		} catch (TNotSupportedException $e) {
			self::assertEquals('NotExisting', $sec->getCryptAlgorithm());
		}
	}
	
	public function testEncryptDecrypt() {
		$sec=new TSecurityManager ();
		$sec->init (null);
		// loop through different string size
		$testText = md5('a text (not) full of entrophy');
		for($i=1; $i<strlen($testText); $i++)
		{
			$sec->setEncryptionKey ('aKey');
			$plainText = substr($testText, 0, $i);
			try {
				$encrypted = $sec->encrypt($plainText);
			} catch (TNotSupportedException $e) {
				self::markTestSkipped('mcrypt extension not loaded');
				return;
			}
			$decrypted = $sec->decrypt($encrypted);
			// the decrypted string is padded with \0
			$decrypted = strstr($decrypted, "\0", TRUE);

			self::assertEquals($plainText,$decrypted);

			// try change key 
			$sec->setEncryptionKey ('anotherKey');
			self::assertNotEquals($plainText, $sec->decrypt($encrypted));
		}
	}
	
	
	public function testHashData() {
		$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() {
		$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'));
	}
	

}

?>