1 | <?php |
---|
2 | /** |
---|
3 | * SecurityTest file |
---|
4 | * |
---|
5 | * PHP versions 4 and 5 |
---|
6 | * |
---|
7 | * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing> |
---|
8 | * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) |
---|
9 | * |
---|
10 | * Licensed under The Open Group Test Suite License |
---|
11 | * Redistributions of files must retain the above copyright notice. |
---|
12 | * |
---|
13 | * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) |
---|
14 | * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests |
---|
15 | * @package cake |
---|
16 | * @subpackage cake.tests.cases.libs |
---|
17 | * @since CakePHP(tm) v 1.2.0.5432 |
---|
18 | * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License |
---|
19 | */ |
---|
20 | App::import('Core', 'Security'); |
---|
21 | |
---|
22 | /** |
---|
23 | * SecurityTest class |
---|
24 | * |
---|
25 | * @package cake |
---|
26 | * @subpackage cake.tests.cases.libs |
---|
27 | */ |
---|
28 | class SecurityTest extends CakeTestCase { |
---|
29 | |
---|
30 | /** |
---|
31 | * sut property |
---|
32 | * |
---|
33 | * @var mixed null |
---|
34 | * @access public |
---|
35 | */ |
---|
36 | var $sut = null; |
---|
37 | |
---|
38 | /** |
---|
39 | * setUp method |
---|
40 | * |
---|
41 | * @access public |
---|
42 | * @return void |
---|
43 | */ |
---|
44 | function setUp() { |
---|
45 | $this->sut =& Security::getInstance(); |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * testInactiveMins method |
---|
50 | * |
---|
51 | * @access public |
---|
52 | * @return void |
---|
53 | */ |
---|
54 | function testInactiveMins() { |
---|
55 | Configure::write('Security.level', 'high'); |
---|
56 | $this->assertEqual(10, Security::inactiveMins()); |
---|
57 | |
---|
58 | Configure::write('Security.level', 'medium'); |
---|
59 | $this->assertEqual(100, Security::inactiveMins()); |
---|
60 | |
---|
61 | Configure::write('Security.level', 'low'); |
---|
62 | $this->assertEqual(300, Security::inactiveMins()); |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * testGenerateAuthkey method |
---|
67 | * |
---|
68 | * @access public |
---|
69 | * @return void |
---|
70 | */ |
---|
71 | function testGenerateAuthkey() { |
---|
72 | $this->assertEqual(strlen(Security::generateAuthKey()), 40); |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * testValidateAuthKey method |
---|
77 | * |
---|
78 | * @access public |
---|
79 | * @return void |
---|
80 | */ |
---|
81 | function testValidateAuthKey() { |
---|
82 | $authKey = Security::generateAuthKey(); |
---|
83 | $this->assertTrue(Security::validateAuthKey($authKey)); |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * testHash method |
---|
88 | * |
---|
89 | * @access public |
---|
90 | * @return void |
---|
91 | */ |
---|
92 | function testHash() { |
---|
93 | $Security =& Security::getInstance(); |
---|
94 | $_hashType = $Security->hashType; |
---|
95 | |
---|
96 | $key = 'someKey'; |
---|
97 | $hash = 'someHash'; |
---|
98 | |
---|
99 | $this->assertIdentical(strlen(Security::hash($key, null, false)), 40); |
---|
100 | $this->assertIdentical(strlen(Security::hash($key, 'sha1', false)), 40); |
---|
101 | $this->assertIdentical(strlen(Security::hash($key, null, true)), 40); |
---|
102 | $this->assertIdentical(strlen(Security::hash($key, 'sha1', true)), 40); |
---|
103 | |
---|
104 | $result = Security::hash($key, null, $hash); |
---|
105 | $this->assertIdentical($result, 'e38fcb877dccb6a94729a81523851c931a46efb1'); |
---|
106 | |
---|
107 | $result = Security::hash($key, 'sha1', $hash); |
---|
108 | $this->assertIdentical($result, 'e38fcb877dccb6a94729a81523851c931a46efb1'); |
---|
109 | |
---|
110 | $hashType = 'sha1'; |
---|
111 | Security::setHash($hashType); |
---|
112 | $this->assertIdentical($this->sut->hashType, $hashType); |
---|
113 | $this->assertIdentical(strlen(Security::hash($key, null, true)), 40); |
---|
114 | $this->assertIdentical(strlen(Security::hash($key, null, false)), 40); |
---|
115 | |
---|
116 | $this->assertIdentical(strlen(Security::hash($key, 'md5', false)), 32); |
---|
117 | $this->assertIdentical(strlen(Security::hash($key, 'md5', true)), 32); |
---|
118 | |
---|
119 | $hashType = 'md5'; |
---|
120 | Security::setHash($hashType); |
---|
121 | $this->assertIdentical($this->sut->hashType, $hashType); |
---|
122 | $this->assertIdentical(strlen(Security::hash($key, null, false)), 32); |
---|
123 | $this->assertIdentical(strlen(Security::hash($key, null, true)), 32); |
---|
124 | |
---|
125 | if (!function_exists('hash') && !function_exists('mhash')) { |
---|
126 | $this->assertIdentical(strlen(Security::hash($key, 'sha256', false)), 32); |
---|
127 | $this->assertIdentical(strlen(Security::hash($key, 'sha256', true)), 32); |
---|
128 | } else { |
---|
129 | $this->assertIdentical(strlen(Security::hash($key, 'sha256', false)), 64); |
---|
130 | $this->assertIdentical(strlen(Security::hash($key, 'sha256', true)), 64); |
---|
131 | } |
---|
132 | |
---|
133 | Security::setHash($_hashType); |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * testCipher method |
---|
138 | * |
---|
139 | * @access public |
---|
140 | * @return void |
---|
141 | */ |
---|
142 | function testCipher() { |
---|
143 | $length = 10; |
---|
144 | $txt = ''; |
---|
145 | for ($i = 0; $i < $length; $i++) { |
---|
146 | $txt .= mt_rand(0, 255); |
---|
147 | } |
---|
148 | $key = 'my_key'; |
---|
149 | $result = Security::cipher($txt, $key); |
---|
150 | $this->assertEqual(Security::cipher($result, $key), $txt); |
---|
151 | |
---|
152 | $txt = ''; |
---|
153 | $key = 'my_key'; |
---|
154 | $result = Security::cipher($txt, $key); |
---|
155 | $this->assertEqual(Security::cipher($result, $key), $txt); |
---|
156 | |
---|
157 | $txt = 'some_text'; |
---|
158 | $key = ''; |
---|
159 | $result = Security::cipher($txt, $key); |
---|
160 | $this->assertError(); |
---|
161 | $this->assertIdentical($result, ''); |
---|
162 | |
---|
163 | $txt = 123456; |
---|
164 | $key = 'my_key'; |
---|
165 | $result = Security::cipher($txt, $key); |
---|
166 | $this->assertEqual(Security::cipher($result, $key), $txt); |
---|
167 | |
---|
168 | $txt = '123456'; |
---|
169 | $key = 'my_key'; |
---|
170 | $result = Security::cipher($txt, $key); |
---|
171 | $this->assertEqual(Security::cipher($result, $key), $txt); |
---|
172 | } |
---|
173 | } |
---|