[442] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
| 5 | * Copyright 2001-2012 Strangecode, LLC |
---|
[446] | 6 | * |
---|
[442] | 7 | * This file is part of The Strangecode Codebase. |
---|
| 8 | * |
---|
| 9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License as published by the |
---|
| 11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
| 12 | * any later version. |
---|
[446] | 13 | * |
---|
[442] | 14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 17 | * details. |
---|
[446] | 18 | * |
---|
[442] | 19 | * You should have received a copy of the GNU General Public License along with |
---|
| 20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /** |
---|
| 24 | * PHPUnit test case for codebase/lib/Auth_SQL.inc.php |
---|
| 25 | * |
---|
| 26 | * The method skeletons below need to be filled in with |
---|
| 27 | * real data so that the tests will run correctly. Replace |
---|
| 28 | * all EXPECTED_VAL and PARAM strings with real data. |
---|
| 29 | * |
---|
| 30 | * Created with PHPUnit_Skeleton on 2005-08-09 |
---|
| 31 | */ |
---|
| 32 | class AuthSQLTest extends PHPUnit_Framework_TestCase { |
---|
| 33 | |
---|
| 34 | var $Auth_SQL; |
---|
[446] | 35 | |
---|
[442] | 36 | static $shared_session; |
---|
[446] | 37 | |
---|
[442] | 38 | function setUp() |
---|
| 39 | { |
---|
[479] | 40 | require dirname(__FILE__) . '/_config.inc.php'; |
---|
[442] | 41 | require_once '../lib/Auth_SQL.inc.php'; |
---|
| 42 | $this->Auth_SQL = new Auth_SQL('testauth'); |
---|
| 43 | $this->Auth_SQL->setParam(array( |
---|
| 44 | 'db_table' => 'test_user_tbl', |
---|
| 45 | 'db_primary_key' => 'user_id', |
---|
| 46 | 'db_login_table' => 'test_login_tbl', |
---|
| 47 | 'login_url' => '/login.php', |
---|
| 48 | 'blocking' => true, |
---|
[447] | 49 | 'encryption_type' => Auth_SQL::ENCRYPT_MD5_HARDENED, |
---|
[442] | 50 | )); |
---|
| 51 | |
---|
| 52 | // Use fresh user table. |
---|
| 53 | $this->Auth_SQL->initDB(true); |
---|
| 54 | |
---|
| 55 | // Insert test data. |
---|
| 56 | $db =& DB::getInstance(); |
---|
| 57 | $db->query(" |
---|
| 58 | INSERT INTO test_user_tbl ( |
---|
| 59 | username, |
---|
| 60 | userpass, |
---|
| 61 | first_name, |
---|
| 62 | last_name, |
---|
| 63 | email |
---|
| 64 | ) VALUES ( |
---|
| 65 | 'testuser', |
---|
| 66 | '" . $this->Auth_SQL->encryptPassword('testpass') . "', |
---|
| 67 | 'John', |
---|
| 68 | 'Doe', |
---|
| 69 | 'root@localhost' |
---|
| 70 | ) |
---|
| 71 | "); |
---|
| 72 | $_SESSION = AuthSQLTest::$shared_session; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | function tearDown() |
---|
| 76 | { |
---|
[479] | 77 | $db =& DB::getInstance(); |
---|
[446] | 78 | |
---|
[479] | 79 | unset($this->Auth_SQL); |
---|
[442] | 80 | $db->query("DROP TABLE IF EXISTS test_user_tbl"); |
---|
| 81 | $db->query("DROP TABLE IF EXISTS test_login_tbl"); |
---|
| 82 | AuthSQLTest::$shared_session = $_SESSION; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | function test_set() |
---|
| 86 | { |
---|
| 87 | $this->Auth_SQL->set('testuserkey', 'testuserval'); |
---|
| 88 | $this->assertEquals('testuserval', $_SESSION['_auth_sql']['testauth']['user_data']['testuserkey']); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | function test_get() |
---|
| 92 | { |
---|
| 93 | $_SESSION['_auth_sql']['testauth']['user_data']['testuserkey'] = 'testuserval'; |
---|
| 94 | $val = $this->Auth_SQL->get('testuserkey'); |
---|
| 95 | $this->assertEquals('testuserval', $val); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | function test_setparam() |
---|
| 99 | { |
---|
| 100 | $this->Auth_SQL->setParam(array( |
---|
| 101 | 'login_url' => 'testloginurl.php' |
---|
| 102 | )); |
---|
| 103 | $this->assertEquals('testloginurl.php', $this->Auth_SQL->getParam('login_url')); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | function test_getparam() |
---|
| 107 | { |
---|
| 108 | //$this->Auth_SQL->_params['login_url'] = 'testloginurl.php'; |
---|
| 109 | $this->Auth_SQL->setParam(array( |
---|
| 110 | 'login_url' => 'testloginurl.php' |
---|
| 111 | )); |
---|
| 112 | $param = $this->Auth_SQL->getParam('login_url'); |
---|
| 113 | $this->assertEquals('testloginurl.php', $param); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | function test_clear() |
---|
| 117 | { |
---|
| 118 | $login = $this->Auth_SQL->login('testuser', 'testpass'); |
---|
| 119 | $this->assertTrue($login, 'User login failed, but should have succeeded.'); |
---|
| 120 | $before_logged_in = $this->Auth_SQL->isloggedin(); |
---|
| 121 | $this->assertTrue($before_logged_in, 'User is not logged in, but should be.'); |
---|
| 122 | $this->Auth_SQL->clear(); |
---|
| 123 | $after_logged_in = $this->Auth_SQL->isloggedin(); |
---|
| 124 | $this->assertFalse($after_logged_in, 'User is still logged in but should not be.'); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | function test_authenticate() |
---|
| 128 | { |
---|
| 129 | $true = $this->Auth_SQL->authenticate('testuser', 'testpass'); |
---|
| 130 | $this->assertInternalType('array', $true, 'User login failed, but should have succeeded.'); |
---|
| 131 | |
---|
| 132 | // Testing wrong password. |
---|
| 133 | $false = $this->Auth_SQL->authenticate('testuser', 'wrongpass'); |
---|
| 134 | |
---|
| 135 | $this->assertfalse($false, 'User login succeeded, but should have failed.'); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | function test_login_and_isLoggedIn() |
---|
| 139 | { |
---|
| 140 | $login = $this->Auth_SQL->login('testuser', 'testpass'); |
---|
| 141 | $this->assertTrue($login, '1. User login failed, but should have succeeded.'); |
---|
| 142 | $before_logged_in = $this->Auth_SQL->isloggedin(); |
---|
| 143 | $this->assertTrue($before_logged_in, '2. User is not logged in, but should be.'); |
---|
| 144 | $this->Auth_SQL->clear(); |
---|
| 145 | $after_logged_in = $this->Auth_SQL->isloggedin(); |
---|
| 146 | $this->assertFalse($after_logged_in, '3. User is still logged in but should not be.'); |
---|
[446] | 147 | |
---|
[442] | 148 | // Testing wrong password. |
---|
| 149 | $login2 = $this->Auth_SQL->login('testuser', 'wrongpass'); |
---|
| 150 | $this->assertFalse($login2, '4. User login succeeded, but should have failed.'); |
---|
| 151 | $before_logged_in2 = $this->Auth_SQL->isloggedin(); |
---|
| 152 | $this->assertFalse($before_logged_in2, '5. User is logged in, but should not be.'); |
---|
| 153 | $this->Auth_SQL->clear(); |
---|
| 154 | $after_logged_in2 = $this->Auth_SQL->isloggedin(); |
---|
| 155 | $this->assertFalse($after_logged_in2, '6. Wrong user is still logged in but should not be.'); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | function test_requirelogin() |
---|
| 159 | { |
---|
| 160 | // $this->Auth_SQL->requirelogin('Login is required!'); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | function test_blockaccount() |
---|
| 164 | { |
---|
[479] | 165 | $db =& DB::getInstance(); |
---|
[446] | 166 | |
---|
[442] | 167 | $this->Auth_SQL->login('testuser', 'testpass'); |
---|
| 168 | $this->Auth_SQL->blockaccount(null, 'blocktestuser'); |
---|
| 169 | $qid = $db->query(" |
---|
| 170 | SELECT blocked_reason |
---|
| 171 | FROM test_user_tbl |
---|
| 172 | "); |
---|
| 173 | list($reason) = mysql_fetch_row($qid); |
---|
| 174 | $this->assertEquals('blocktestuser', $reason, "Block not found in DB record."); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | function test_unblockaccount() |
---|
| 178 | { |
---|
[479] | 179 | $db =& DB::getInstance(); |
---|
[446] | 180 | |
---|
[442] | 181 | $db->query(" |
---|
| 182 | UPDATE test_user_tbl SET blocked_reason = 'blocktestuser' |
---|
| 183 | "); |
---|
| 184 | $this->Auth_SQL->unblockaccount(); |
---|
| 185 | |
---|
| 186 | $qid = $db->query(" |
---|
| 187 | SELECT blocked_reason |
---|
| 188 | FROM test_user_tbl |
---|
| 189 | "); |
---|
| 190 | list($reason) = mysql_fetch_row($qid); |
---|
| 191 | $this->assertTrue('' == $reason, "Block not removed from DB record."); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | function test_usernameexists() |
---|
| 195 | { |
---|
| 196 | $result = $this->Auth_SQL->usernameexists('testuser'); |
---|
| 197 | $this->assertTrue($result); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | function test_getusername() |
---|
| 201 | { |
---|
| 202 | $result = $this->Auth_SQL->getusername(1); |
---|
| 203 | $this->assertEquals('testuser', $result); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | function test_generatepassword() |
---|
| 207 | { |
---|
| 208 | $result = $this->Auth_SQL->generatepassword('xCVcvd'); |
---|
[448] | 209 | $this->assertRegExp('/[bcdfghjklmnprstvwxzBCDFGHJKLMNPRSTVWXZaeiouyAEIOUY0123456789!@#%&*-=+.?][bcdfghjklmnprstvwxzBCDFGHJKLMNPRSTVWXZ][aeiouyAEIOUY][bcdfghjklmnprstvwxz][aeiouy][0123456789]/', $result, 'Generated password does not match intended pattern'); |
---|
[442] | 210 | } |
---|
| 211 | |
---|
| 212 | function test_encryptpassword() |
---|
| 213 | { |
---|
[447] | 214 | $this->Auth_SQL->setParam(array('encryption_type' => Auth_SQL::ENCRYPT_MD5)); |
---|
[442] | 215 | $result = $this->Auth_SQL->encryptpassword('123'); |
---|
| 216 | $this->assertEquals('202cb962ac59075b964b07152d234b70', $result); |
---|
| 217 | |
---|
[447] | 218 | $this->Auth_SQL->setParam(array('encryption_type' => Auth_SQL::ENCRYPT_MD5_HARDENED)); |
---|
[442] | 219 | $result = $this->Auth_SQL->encryptpassword('123'); |
---|
| 220 | $this->assertEquals('c55e4ac608a8768ecd758fab971b0646', $result); |
---|
| 221 | |
---|
[447] | 222 | $this->Auth_SQL->setParam(array('encryption_type' => Auth_SQL::ENCRYPT_SHA1)); |
---|
[442] | 223 | $result = $this->Auth_SQL->encryptpassword('123'); |
---|
| 224 | $this->assertEquals('40bd001563085fc35165329ea1ff5c5ecbdbbeef', $result); |
---|
| 225 | |
---|
[447] | 226 | $this->Auth_SQL->setParam(array('encryption_type' => Auth_SQL::ENCRYPT_SHA1_HARDENED)); |
---|
[442] | 227 | $result = $this->Auth_SQL->encryptpassword('123'); |
---|
| 228 | $this->assertEquals('33d90af96a5928ac93cbd41fc436e8c55d2768c2', $result); |
---|
| 229 | |
---|
[447] | 230 | $this->Auth_SQL->setParam(array('encryption_type' => Auth_SQL::ENCRYPT_PLAINTEXT)); |
---|
[442] | 231 | $result = $this->Auth_SQL->encryptpassword('123'); |
---|
| 232 | $this->assertEquals('123', $result); |
---|
| 233 | |
---|
[447] | 234 | $this->Auth_SQL->setParam(array('encryption_type' => Auth_SQL::ENCRYPT_CRYPT)); |
---|
[442] | 235 | $result = $this->Auth_SQL->encryptpassword('123', 'saltstring'); |
---|
| 236 | $this->assertEquals('saEZ6MlWYV9nQ', $result); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | function test_setpassword() |
---|
| 240 | { |
---|
[479] | 241 | $db =& DB::getInstance(); |
---|
[446] | 242 | |
---|
[447] | 243 | $this->Auth_SQL->setParam(array('encryption_type' => Auth_SQL::ENCRYPT_SHA1_HARDENED)); |
---|
[442] | 244 | $this->Auth_SQL->setpassword(null, '123'); |
---|
| 245 | $qid = $db->query(" |
---|
| 246 | SELECT userpass |
---|
| 247 | FROM test_user_tbl |
---|
| 248 | "); |
---|
| 249 | list($pass) = mysql_fetch_row($qid); |
---|
| 250 | $this->assertEquals('33d90af96a5928ac93cbd41fc436e8c55d2768c2', $pass); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | function test_resetpassword() |
---|
| 254 | { |
---|
| 255 | $result = $this->Auth_SQL->resetpassword(1, 'Because this is a test.'); |
---|
| 256 | $this->assertInternalType('array', $result); |
---|
[446] | 257 | |
---|
[442] | 258 | } |
---|
| 259 | |
---|
| 260 | // function test_inclearancezone() |
---|
| 261 | // { |
---|
| 262 | // $result = $this->Auth_SQL->inclearancezone(PARAM); |
---|
| 263 | // $expected = EXPECTED_VAL; |
---|
| 264 | // $this->assertEquals($expected, $result); |
---|
| 265 | // } |
---|
| 266 | // |
---|
| 267 | // function test_requireaccessclearance() |
---|
| 268 | // { |
---|
| 269 | // $result = $this->Auth_SQL->requireaccessclearance(PARAM); |
---|
| 270 | // $expected = EXPECTED_VAL; |
---|
| 271 | // $this->assertEquals($expected, $result); |
---|
| 272 | // } |
---|
| 273 | |
---|
| 274 | } |
---|
| 275 | |
---|