diff options
Diffstat (limited to 'tests/security')
-rw-r--r-- | tests/security/LoginManagerTest.php | 374 | ||||
-rw-r--r-- | tests/security/SessionManagerTest.php | 272 |
2 files changed, 646 insertions, 0 deletions
diff --git a/tests/security/LoginManagerTest.php b/tests/security/LoginManagerTest.php new file mode 100644 index 00000000..f26cd1eb --- /dev/null +++ b/tests/security/LoginManagerTest.php | |||
@@ -0,0 +1,374 @@ | |||
1 | <?php | ||
2 | namespace Shaarli\Security; | ||
3 | |||
4 | require_once 'tests/utils/FakeConfigManager.php'; | ||
5 | use \PHPUnit\Framework\TestCase; | ||
6 | |||
7 | /** | ||
8 | * Test coverage for LoginManager | ||
9 | */ | ||
10 | class LoginManagerTest extends TestCase | ||
11 | { | ||
12 | /** @var \FakeConfigManager Configuration Manager instance */ | ||
13 | protected $configManager = null; | ||
14 | |||
15 | /** @var LoginManager Login Manager instance */ | ||
16 | protected $loginManager = null; | ||
17 | |||
18 | /** @var SessionManager Session Manager instance */ | ||
19 | protected $sessionManager = null; | ||
20 | |||
21 | /** @var string Banned IP filename */ | ||
22 | protected $banFile = 'sandbox/ipbans.php'; | ||
23 | |||
24 | /** @var string Log filename */ | ||
25 | protected $logFile = 'sandbox/shaarli.log'; | ||
26 | |||
27 | /** @var array Simulates the $_COOKIE array */ | ||
28 | protected $cookie = []; | ||
29 | |||
30 | /** @var array Simulates the $GLOBALS array */ | ||
31 | protected $globals = []; | ||
32 | |||
33 | /** @var array Simulates the $_SERVER array */ | ||
34 | protected $server = []; | ||
35 | |||
36 | /** @var array Simulates the $_SESSION array */ | ||
37 | protected $session = []; | ||
38 | |||
39 | /** @var string Advertised client IP address */ | ||
40 | protected $clientIpAddress = '10.1.47.179'; | ||
41 | |||
42 | /** @var string Local client IP address */ | ||
43 | protected $ipAddr = '127.0.0.1'; | ||
44 | |||
45 | /** @var string Trusted proxy IP address */ | ||
46 | protected $trustedProxy = '10.1.1.100'; | ||
47 | |||
48 | /** @var string User login */ | ||
49 | protected $login = 'johndoe'; | ||
50 | |||
51 | /** @var string User password */ | ||
52 | protected $password = 'IC4nHazL0g1n?'; | ||
53 | |||
54 | /** @var string Hash of the salted user password */ | ||
55 | protected $passwordHash = ''; | ||
56 | |||
57 | /** @var string Salt used by hash functions */ | ||
58 | protected $salt = '669e24fa9c5a59a613f98e8e38327384504a4af2'; | ||
59 | |||
60 | /** | ||
61 | * Prepare or reset test resources | ||
62 | */ | ||
63 | public function setUp() | ||
64 | { | ||
65 | if (file_exists($this->banFile)) { | ||
66 | unlink($this->banFile); | ||
67 | } | ||
68 | |||
69 | $this->passwordHash = sha1($this->password . $this->login . $this->salt); | ||
70 | |||
71 | $this->configManager = new \FakeConfigManager([ | ||
72 | 'credentials.login' => $this->login, | ||
73 | 'credentials.hash' => $this->passwordHash, | ||
74 | 'credentials.salt' => $this->salt, | ||
75 | 'resource.ban_file' => $this->banFile, | ||
76 | 'resource.log' => $this->logFile, | ||
77 | 'security.ban_after' => 4, | ||
78 | 'security.ban_duration' => 3600, | ||
79 | 'security.trusted_proxies' => [$this->trustedProxy], | ||
80 | ]); | ||
81 | |||
82 | $this->cookie = []; | ||
83 | |||
84 | $this->globals = &$GLOBALS; | ||
85 | unset($this->globals['IPBANS']); | ||
86 | |||
87 | $this->session = []; | ||
88 | |||
89 | $this->sessionManager = new SessionManager($this->session, $this->configManager); | ||
90 | $this->loginManager = new LoginManager($this->globals, $this->configManager, $this->sessionManager); | ||
91 | $this->server['REMOTE_ADDR'] = $this->ipAddr; | ||
92 | } | ||
93 | |||
94 | /** | ||
95 | * Wipe test resources | ||
96 | */ | ||
97 | public function tearDown() | ||
98 | { | ||
99 | unset($this->globals['IPBANS']); | ||
100 | } | ||
101 | |||
102 | /** | ||
103 | * Instantiate a LoginManager and load ban records | ||
104 | */ | ||
105 | public function testReadBanFile() | ||
106 | { | ||
107 | file_put_contents( | ||
108 | $this->banFile, | ||
109 | "<?php\n\$GLOBALS['IPBANS']=array('FAILURES' => array('127.0.0.1' => 99));\n?>" | ||
110 | ); | ||
111 | new LoginManager($this->globals, $this->configManager, null); | ||
112 | $this->assertEquals(99, $this->globals['IPBANS']['FAILURES']['127.0.0.1']); | ||
113 | } | ||
114 | |||
115 | /** | ||
116 | * Record a failed login attempt | ||
117 | */ | ||
118 | public function testHandleFailedLogin() | ||
119 | { | ||
120 | $this->loginManager->handleFailedLogin($this->server); | ||
121 | $this->assertEquals(1, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]); | ||
122 | |||
123 | $this->loginManager->handleFailedLogin($this->server); | ||
124 | $this->assertEquals(2, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]); | ||
125 | } | ||
126 | |||
127 | /** | ||
128 | * Record a failed login attempt - IP behind a trusted proxy | ||
129 | */ | ||
130 | public function testHandleFailedLoginBehindTrustedProxy() | ||
131 | { | ||
132 | $server = [ | ||
133 | 'REMOTE_ADDR' => $this->trustedProxy, | ||
134 | 'HTTP_X_FORWARDED_FOR' => $this->ipAddr, | ||
135 | ]; | ||
136 | $this->loginManager->handleFailedLogin($server); | ||
137 | $this->assertEquals(1, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]); | ||
138 | |||
139 | $this->loginManager->handleFailedLogin($server); | ||
140 | $this->assertEquals(2, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]); | ||
141 | } | ||
142 | |||
143 | /** | ||
144 | * Record a failed login attempt - IP behind a trusted proxy but not forwarded | ||
145 | */ | ||
146 | public function testHandleFailedLoginBehindTrustedProxyNoIp() | ||
147 | { | ||
148 | $server = [ | ||
149 | 'REMOTE_ADDR' => $this->trustedProxy, | ||
150 | ]; | ||
151 | $this->loginManager->handleFailedLogin($server); | ||
152 | $this->assertFalse(isset($this->globals['IPBANS']['FAILURES'][$this->ipAddr])); | ||
153 | |||
154 | $this->loginManager->handleFailedLogin($server); | ||
155 | $this->assertFalse(isset($this->globals['IPBANS']['FAILURES'][$this->ipAddr])); | ||
156 | } | ||
157 | |||
158 | /** | ||
159 | * Record a failed login attempt and ban the IP after too many failures | ||
160 | */ | ||
161 | public function testHandleFailedLoginBanIp() | ||
162 | { | ||
163 | $this->loginManager->handleFailedLogin($this->server); | ||
164 | $this->assertEquals(1, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]); | ||
165 | $this->assertTrue($this->loginManager->canLogin($this->server)); | ||
166 | |||
167 | $this->loginManager->handleFailedLogin($this->server); | ||
168 | $this->assertEquals(2, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]); | ||
169 | $this->assertTrue($this->loginManager->canLogin($this->server)); | ||
170 | |||
171 | $this->loginManager->handleFailedLogin($this->server); | ||
172 | $this->assertEquals(3, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]); | ||
173 | $this->assertTrue($this->loginManager->canLogin($this->server)); | ||
174 | |||
175 | $this->loginManager->handleFailedLogin($this->server); | ||
176 | $this->assertEquals(4, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]); | ||
177 | $this->assertFalse($this->loginManager->canLogin($this->server)); | ||
178 | |||
179 | // handleFailedLogin is not supposed to be called at this point: | ||
180 | // - no login form should be displayed once an IP has been banned | ||
181 | // - yet this could happen when using custom templates / scripts | ||
182 | $this->loginManager->handleFailedLogin($this->server); | ||
183 | $this->assertEquals(5, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]); | ||
184 | $this->assertFalse($this->loginManager->canLogin($this->server)); | ||
185 | } | ||
186 | |||
187 | /** | ||
188 | * Nothing to do | ||
189 | */ | ||
190 | public function testHandleSuccessfulLogin() | ||
191 | { | ||
192 | $this->assertTrue($this->loginManager->canLogin($this->server)); | ||
193 | |||
194 | $this->loginManager->handleSuccessfulLogin($this->server); | ||
195 | $this->assertTrue($this->loginManager->canLogin($this->server)); | ||
196 | } | ||
197 | |||
198 | /** | ||
199 | * Erase failure records after successfully logging in from this IP | ||
200 | */ | ||
201 | public function testHandleSuccessfulLoginAfterFailure() | ||
202 | { | ||
203 | $this->loginManager->handleFailedLogin($this->server); | ||
204 | $this->loginManager->handleFailedLogin($this->server); | ||
205 | $this->assertEquals(2, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]); | ||
206 | $this->assertTrue($this->loginManager->canLogin($this->server)); | ||
207 | |||
208 | $this->loginManager->handleSuccessfulLogin($this->server); | ||
209 | $this->assertTrue($this->loginManager->canLogin($this->server)); | ||
210 | $this->assertFalse(isset($this->globals['IPBANS']['FAILURES'][$this->ipAddr])); | ||
211 | $this->assertFalse(isset($this->globals['IPBANS']['BANS'][$this->ipAddr])); | ||
212 | } | ||
213 | |||
214 | /** | ||
215 | * The IP is not banned | ||
216 | */ | ||
217 | public function testCanLoginIpNotBanned() | ||
218 | { | ||
219 | $this->assertTrue($this->loginManager->canLogin($this->server)); | ||
220 | } | ||
221 | |||
222 | /** | ||
223 | * The IP is banned | ||
224 | */ | ||
225 | public function testCanLoginIpBanned() | ||
226 | { | ||
227 | // ban the IP for an hour | ||
228 | $this->globals['IPBANS']['FAILURES'][$this->ipAddr] = 10; | ||
229 | $this->globals['IPBANS']['BANS'][$this->ipAddr] = time() + 3600; | ||
230 | |||
231 | $this->assertFalse($this->loginManager->canLogin($this->server)); | ||
232 | } | ||
233 | |||
234 | /** | ||
235 | * The IP is banned, and the ban duration is over | ||
236 | */ | ||
237 | public function testCanLoginIpBanExpired() | ||
238 | { | ||
239 | // ban the IP for an hour | ||
240 | $this->globals['IPBANS']['FAILURES'][$this->ipAddr] = 10; | ||
241 | $this->globals['IPBANS']['BANS'][$this->ipAddr] = time() + 3600; | ||
242 | $this->assertFalse($this->loginManager->canLogin($this->server)); | ||
243 | |||
244 | // lift the ban | ||
245 | $this->globals['IPBANS']['BANS'][$this->ipAddr] = time() - 3600; | ||
246 | $this->assertTrue($this->loginManager->canLogin($this->server)); | ||
247 | } | ||
248 | |||
249 | /** | ||
250 | * Generate a token depending on the user credentials and client IP | ||
251 | */ | ||
252 | public function testGenerateStaySignedInToken() | ||
253 | { | ||
254 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); | ||
255 | |||
256 | $this->assertEquals( | ||
257 | sha1($this->passwordHash . $this->clientIpAddress . $this->salt), | ||
258 | $this->loginManager->getStaySignedInToken() | ||
259 | ); | ||
260 | } | ||
261 | |||
262 | /** | ||
263 | * Check user login - Shaarli has not yet been configured | ||
264 | */ | ||
265 | public function testCheckLoginStateNotConfigured() | ||
266 | { | ||
267 | $configManager = new \FakeConfigManager([ | ||
268 | 'resource.ban_file' => $this->banFile, | ||
269 | ]); | ||
270 | $loginManager = new LoginManager($this->globals, $configManager, null); | ||
271 | $loginManager->checkLoginState([], ''); | ||
272 | |||
273 | $this->assertFalse($loginManager->isLoggedIn()); | ||
274 | } | ||
275 | |||
276 | /** | ||
277 | * Check user login - the client cookie does not match the server token | ||
278 | */ | ||
279 | public function testCheckLoginStateStaySignedInWithInvalidToken() | ||
280 | { | ||
281 | // simulate a previous login | ||
282 | $this->session = [ | ||
283 | 'ip' => $this->clientIpAddress, | ||
284 | 'expires_on' => time() + 100, | ||
285 | ]; | ||
286 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); | ||
287 | $this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = 'nope'; | ||
288 | |||
289 | $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress); | ||
290 | |||
291 | $this->assertTrue($this->loginManager->isLoggedIn()); | ||
292 | $this->assertTrue(empty($this->session['username'])); | ||
293 | } | ||
294 | |||
295 | /** | ||
296 | * Check user login - the client cookie matches the server token | ||
297 | */ | ||
298 | public function testCheckLoginStateStaySignedInWithValidToken() | ||
299 | { | ||
300 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); | ||
301 | $this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = $this->loginManager->getStaySignedInToken(); | ||
302 | |||
303 | $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress); | ||
304 | |||
305 | $this->assertTrue($this->loginManager->isLoggedIn()); | ||
306 | $this->assertEquals($this->login, $this->session['username']); | ||
307 | $this->assertEquals($this->clientIpAddress, $this->session['ip']); | ||
308 | } | ||
309 | |||
310 | /** | ||
311 | * Check user login - the session has expired | ||
312 | */ | ||
313 | public function testCheckLoginStateSessionExpired() | ||
314 | { | ||
315 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); | ||
316 | $this->session['expires_on'] = time() - 100; | ||
317 | |||
318 | $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress); | ||
319 | |||
320 | $this->assertFalse($this->loginManager->isLoggedIn()); | ||
321 | } | ||
322 | |||
323 | /** | ||
324 | * Check user login - the remote client IP has changed | ||
325 | */ | ||
326 | public function testCheckLoginStateClientIpChanged() | ||
327 | { | ||
328 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); | ||
329 | |||
330 | $this->loginManager->checkLoginState($this->cookie, '10.7.157.98'); | ||
331 | |||
332 | $this->assertFalse($this->loginManager->isLoggedIn()); | ||
333 | } | ||
334 | |||
335 | /** | ||
336 | * Check user credentials - wrong login supplied | ||
337 | */ | ||
338 | public function testCheckCredentialsWrongLogin() | ||
339 | { | ||
340 | $this->assertFalse( | ||
341 | $this->loginManager->checkCredentials('', '', 'b4dl0g1n', $this->password) | ||
342 | ); | ||
343 | } | ||
344 | |||
345 | /** | ||
346 | * Check user credentials - wrong password supplied | ||
347 | */ | ||
348 | public function testCheckCredentialsWrongPassword() | ||
349 | { | ||
350 | $this->assertFalse( | ||
351 | $this->loginManager->checkCredentials('', '', $this->login, 'b4dp455wd') | ||
352 | ); | ||
353 | } | ||
354 | |||
355 | /** | ||
356 | * Check user credentials - wrong login and password supplied | ||
357 | */ | ||
358 | public function testCheckCredentialsWrongLoginAndPassword() | ||
359 | { | ||
360 | $this->assertFalse( | ||
361 | $this->loginManager->checkCredentials('', '', 'b4dl0g1n', 'b4dp455wd') | ||
362 | ); | ||
363 | } | ||
364 | |||
365 | /** | ||
366 | * Check user credentials - correct login and password supplied | ||
367 | */ | ||
368 | public function testCheckCredentialsGoodLoginAndPassword() | ||
369 | { | ||
370 | $this->assertTrue( | ||
371 | $this->loginManager->checkCredentials('', '', $this->login, $this->password) | ||
372 | ); | ||
373 | } | ||
374 | } | ||
diff --git a/tests/security/SessionManagerTest.php b/tests/security/SessionManagerTest.php new file mode 100644 index 00000000..7961e771 --- /dev/null +++ b/tests/security/SessionManagerTest.php | |||
@@ -0,0 +1,272 @@ | |||
1 | <?php | ||
2 | require_once 'tests/utils/FakeConfigManager.php'; | ||
3 | |||
4 | // Initialize reference data _before_ PHPUnit starts a session | ||
5 | require_once 'tests/utils/ReferenceSessionIdHashes.php'; | ||
6 | ReferenceSessionIdHashes::genAllHashes(); | ||
7 | |||
8 | use \Shaarli\Security\SessionManager; | ||
9 | use \PHPUnit\Framework\TestCase; | ||
10 | |||
11 | /** | ||
12 | * Test coverage for SessionManager | ||
13 | */ | ||
14 | class SessionManagerTest extends TestCase | ||
15 | { | ||
16 | /** @var array Session ID hashes */ | ||
17 | protected static $sidHashes = null; | ||
18 | |||
19 | /** @var \FakeConfigManager ConfigManager substitute for testing */ | ||
20 | protected $conf = null; | ||
21 | |||
22 | /** @var array $_SESSION array for testing */ | ||
23 | protected $session = []; | ||
24 | |||
25 | /** @var SessionManager Server-side session management abstraction */ | ||
26 | protected $sessionManager = null; | ||
27 | |||
28 | /** | ||
29 | * Assign reference data | ||
30 | */ | ||
31 | public static function setUpBeforeClass() | ||
32 | { | ||
33 | self::$sidHashes = ReferenceSessionIdHashes::getHashes(); | ||
34 | } | ||
35 | |||
36 | /** | ||
37 | * Initialize or reset test resources | ||
38 | */ | ||
39 | public function setUp() | ||
40 | { | ||
41 | $this->conf = new FakeConfigManager([ | ||
42 | 'credentials.login' => 'johndoe', | ||
43 | 'credentials.salt' => 'salt', | ||
44 | 'security.session_protection_disabled' => false, | ||
45 | ]); | ||
46 | $this->session = []; | ||
47 | $this->sessionManager = new SessionManager($this->session, $this->conf); | ||
48 | } | ||
49 | |||
50 | /** | ||
51 | * Generate a session token | ||
52 | */ | ||
53 | public function testGenerateToken() | ||
54 | { | ||
55 | $token = $this->sessionManager->generateToken(); | ||
56 | |||
57 | $this->assertEquals(1, $this->session['tokens'][$token]); | ||
58 | $this->assertEquals(40, strlen($token)); | ||
59 | } | ||
60 | |||
61 | /** | ||
62 | * Check a session token | ||
63 | */ | ||
64 | public function testCheckToken() | ||
65 | { | ||
66 | $token = '4dccc3a45ad9d03e5542b90c37d8db6d10f2b38b'; | ||
67 | $session = [ | ||
68 | 'tokens' => [ | ||
69 | $token => 1, | ||
70 | ], | ||
71 | ]; | ||
72 | $sessionManager = new SessionManager($session, $this->conf); | ||
73 | |||
74 | // check and destroy the token | ||
75 | $this->assertTrue($sessionManager->checkToken($token)); | ||
76 | $this->assertFalse(isset($session['tokens'][$token])); | ||
77 | |||
78 | // ensure the token has been destroyed | ||
79 | $this->assertFalse($sessionManager->checkToken($token)); | ||
80 | } | ||
81 | |||
82 | /** | ||
83 | * Generate and check a session token | ||
84 | */ | ||
85 | public function testGenerateAndCheckToken() | ||
86 | { | ||
87 | $token = $this->sessionManager->generateToken(); | ||
88 | |||
89 | // ensure a token has been generated | ||
90 | $this->assertEquals(1, $this->session['tokens'][$token]); | ||
91 | $this->assertEquals(40, strlen($token)); | ||
92 | |||
93 | // check and destroy the token | ||
94 | $this->assertTrue($this->sessionManager->checkToken($token)); | ||
95 | $this->assertFalse(isset($this->session['tokens'][$token])); | ||
96 | |||
97 | // ensure the token has been destroyed | ||
98 | $this->assertFalse($this->sessionManager->checkToken($token)); | ||
99 | } | ||
100 | |||
101 | /** | ||
102 | * Check an invalid session token | ||
103 | */ | ||
104 | public function testCheckInvalidToken() | ||
105 | { | ||
106 | $this->assertFalse($this->sessionManager->checkToken('4dccc3a45ad9d03e5542b90c37d8db6d10f2b38b')); | ||
107 | } | ||
108 | |||
109 | /** | ||
110 | * Test SessionManager::checkId with a valid ID - TEST ALL THE HASHES! | ||
111 | * | ||
112 | * This tests extensively covers all hash algorithms / bit representations | ||
113 | */ | ||
114 | public function testIsAnyHashSessionIdValid() | ||
115 | { | ||
116 | foreach (self::$sidHashes as $algo => $bpcs) { | ||
117 | foreach ($bpcs as $bpc => $hash) { | ||
118 | $this->assertTrue(SessionManager::checkId($hash)); | ||
119 | } | ||
120 | } | ||
121 | } | ||
122 | |||
123 | /** | ||
124 | * Test checkId with a valid ID - SHA-1 hashes | ||
125 | */ | ||
126 | public function testIsSha1SessionIdValid() | ||
127 | { | ||
128 | $this->assertTrue(SessionManager::checkId(sha1('shaarli'))); | ||
129 | } | ||
130 | |||
131 | /** | ||
132 | * Test checkId with a valid ID - SHA-256 hashes | ||
133 | */ | ||
134 | public function testIsSha256SessionIdValid() | ||
135 | { | ||
136 | $this->assertTrue(SessionManager::checkId(hash('sha256', 'shaarli'))); | ||
137 | } | ||
138 | |||
139 | /** | ||
140 | * Test checkId with a valid ID - SHA-512 hashes | ||
141 | */ | ||
142 | public function testIsSha512SessionIdValid() | ||
143 | { | ||
144 | $this->assertTrue(SessionManager::checkId(hash('sha512', 'shaarli'))); | ||
145 | } | ||
146 | |||
147 | /** | ||
148 | * Test checkId with invalid IDs. | ||
149 | */ | ||
150 | public function testIsSessionIdInvalid() | ||
151 | { | ||
152 | $this->assertFalse(SessionManager::checkId('')); | ||
153 | $this->assertFalse(SessionManager::checkId([])); | ||
154 | $this->assertFalse( | ||
155 | SessionManager::checkId('c0ZqcWF3VFE2NmJBdm1HMVQ0ZHJ3UmZPbTFsNGhkNHI=') | ||
156 | ); | ||
157 | } | ||
158 | |||
159 | /** | ||
160 | * Store login information after a successful login | ||
161 | */ | ||
162 | public function testStoreLoginInfo() | ||
163 | { | ||
164 | $this->sessionManager->storeLoginInfo('ip_id'); | ||
165 | |||
166 | $this->assertGreaterThan(time(), $this->session['expires_on']); | ||
167 | $this->assertEquals('ip_id', $this->session['ip']); | ||
168 | $this->assertEquals('johndoe', $this->session['username']); | ||
169 | } | ||
170 | |||
171 | /** | ||
172 | * Extend a server-side session by SessionManager::$SHORT_TIMEOUT | ||
173 | */ | ||
174 | public function testExtendSession() | ||
175 | { | ||
176 | $this->sessionManager->extendSession(); | ||
177 | |||
178 | $this->assertGreaterThan(time(), $this->session['expires_on']); | ||
179 | $this->assertLessThanOrEqual( | ||
180 | time() + SessionManager::$SHORT_TIMEOUT, | ||
181 | $this->session['expires_on'] | ||
182 | ); | ||
183 | } | ||
184 | |||
185 | /** | ||
186 | * Extend a server-side session by SessionManager::$LONG_TIMEOUT | ||
187 | */ | ||
188 | public function testExtendSessionStaySignedIn() | ||
189 | { | ||
190 | $this->sessionManager->setStaySignedIn(true); | ||
191 | $this->sessionManager->extendSession(); | ||
192 | |||
193 | $this->assertGreaterThan(time(), $this->session['expires_on']); | ||
194 | $this->assertGreaterThan( | ||
195 | time() + SessionManager::$LONG_TIMEOUT - 10, | ||
196 | $this->session['expires_on'] | ||
197 | ); | ||
198 | $this->assertLessThanOrEqual( | ||
199 | time() + SessionManager::$LONG_TIMEOUT, | ||
200 | $this->session['expires_on'] | ||
201 | ); | ||
202 | } | ||
203 | |||
204 | /** | ||
205 | * Unset session variables after logging out | ||
206 | */ | ||
207 | public function testLogout() | ||
208 | { | ||
209 | $this->session = [ | ||
210 | 'ip' => 'ip_id', | ||
211 | 'expires_on' => time() + 1000, | ||
212 | 'username' => 'johndoe', | ||
213 | 'visibility' => 'public', | ||
214 | 'untaggedonly' => false, | ||
215 | ]; | ||
216 | $this->sessionManager->logout(); | ||
217 | |||
218 | $this->assertFalse(isset($this->session['ip'])); | ||
219 | $this->assertFalse(isset($this->session['expires_on'])); | ||
220 | $this->assertFalse(isset($this->session['username'])); | ||
221 | $this->assertFalse(isset($this->session['visibility'])); | ||
222 | $this->assertFalse(isset($this->session['untaggedonly'])); | ||
223 | } | ||
224 | |||
225 | /** | ||
226 | * The session is active and expiration time has been reached | ||
227 | */ | ||
228 | public function testHasExpiredTimeElapsed() | ||
229 | { | ||
230 | $this->session['expires_on'] = time() - 10; | ||
231 | |||
232 | $this->assertTrue($this->sessionManager->hasSessionExpired()); | ||
233 | } | ||
234 | |||
235 | /** | ||
236 | * The session is active and expiration time has not been reached | ||
237 | */ | ||
238 | public function testHasNotExpired() | ||
239 | { | ||
240 | $this->session['expires_on'] = time() + 1000; | ||
241 | |||
242 | $this->assertFalse($this->sessionManager->hasSessionExpired()); | ||
243 | } | ||
244 | |||
245 | /** | ||
246 | * Session hijacking protection is disabled, we assume the IP has not changed | ||
247 | */ | ||
248 | public function testHasClientIpChangedNoSessionProtection() | ||
249 | { | ||
250 | $this->conf->set('security.session_protection_disabled', true); | ||
251 | |||
252 | $this->assertFalse($this->sessionManager->hasClientIpChanged('')); | ||
253 | } | ||
254 | |||
255 | /** | ||
256 | * The client IP identifier has not changed | ||
257 | */ | ||
258 | public function testHasClientIpChangedNope() | ||
259 | { | ||
260 | $this->session['ip'] = 'ip_id'; | ||
261 | $this->assertFalse($this->sessionManager->hasClientIpChanged('ip_id')); | ||
262 | } | ||
263 | |||
264 | /** | ||
265 | * The client IP identifier has changed | ||
266 | */ | ||
267 | public function testHasClientIpChanged() | ||
268 | { | ||
269 | $this->session['ip'] = 'ip_id_one'; | ||
270 | $this->assertTrue($this->sessionManager->hasClientIpChanged('ip_id_two')); | ||
271 | } | ||
272 | } | ||