SessionManagerTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. require_once 'tests/utils/FakeConfigManager.php';
  3. // Initialize reference data _before_ PHPUnit starts a session
  4. require_once 'tests/utils/ReferenceSessionIdHashes.php';
  5. ReferenceSessionIdHashes::genAllHashes();
  6. use \Shaarli\Security\SessionManager;
  7. use \PHPUnit\Framework\TestCase;
  8. /**
  9. * Test coverage for SessionManager
  10. */
  11. class SessionManagerTest extends TestCase
  12. {
  13. /** @var array Session ID hashes */
  14. protected static $sidHashes = null;
  15. /** @var \FakeConfigManager ConfigManager substitute for testing */
  16. protected $conf = null;
  17. /** @var array $_SESSION array for testing */
  18. protected $session = [];
  19. /** @var SessionManager Server-side session management abstraction */
  20. protected $sessionManager = null;
  21. /**
  22. * Assign reference data
  23. */
  24. public static function setUpBeforeClass()
  25. {
  26. self::$sidHashes = ReferenceSessionIdHashes::getHashes();
  27. }
  28. /**
  29. * Initialize or reset test resources
  30. */
  31. public function setUp()
  32. {
  33. $this->conf = new FakeConfigManager([
  34. 'credentials.login' => 'johndoe',
  35. 'credentials.salt' => 'salt',
  36. 'security.session_protection_disabled' => false,
  37. ]);
  38. $this->session = [];
  39. $this->sessionManager = new SessionManager($this->session, $this->conf);
  40. }
  41. /**
  42. * Generate a session token
  43. */
  44. public function testGenerateToken()
  45. {
  46. $token = $this->sessionManager->generateToken();
  47. $this->assertEquals(1, $this->session['tokens'][$token]);
  48. $this->assertEquals(40, strlen($token));
  49. }
  50. /**
  51. * Check a session token
  52. */
  53. public function testCheckToken()
  54. {
  55. $token = '4dccc3a45ad9d03e5542b90c37d8db6d10f2b38b';
  56. $session = [
  57. 'tokens' => [
  58. $token => 1,
  59. ],
  60. ];
  61. $sessionManager = new SessionManager($session, $this->conf);
  62. // check and destroy the token
  63. $this->assertTrue($sessionManager->checkToken($token));
  64. $this->assertFalse(isset($session['tokens'][$token]));
  65. // ensure the token has been destroyed
  66. $this->assertFalse($sessionManager->checkToken($token));
  67. }
  68. /**
  69. * Generate and check a session token
  70. */
  71. public function testGenerateAndCheckToken()
  72. {
  73. $token = $this->sessionManager->generateToken();
  74. // ensure a token has been generated
  75. $this->assertEquals(1, $this->session['tokens'][$token]);
  76. $this->assertEquals(40, strlen($token));
  77. // check and destroy the token
  78. $this->assertTrue($this->sessionManager->checkToken($token));
  79. $this->assertFalse(isset($this->session['tokens'][$token]));
  80. // ensure the token has been destroyed
  81. $this->assertFalse($this->sessionManager->checkToken($token));
  82. }
  83. /**
  84. * Check an invalid session token
  85. */
  86. public function testCheckInvalidToken()
  87. {
  88. $this->assertFalse($this->sessionManager->checkToken('4dccc3a45ad9d03e5542b90c37d8db6d10f2b38b'));
  89. }
  90. /**
  91. * Test SessionManager::checkId with a valid ID - TEST ALL THE HASHES!
  92. *
  93. * This tests extensively covers all hash algorithms / bit representations
  94. */
  95. public function testIsAnyHashSessionIdValid()
  96. {
  97. foreach (self::$sidHashes as $algo => $bpcs) {
  98. foreach ($bpcs as $bpc => $hash) {
  99. $this->assertTrue(SessionManager::checkId($hash));
  100. }
  101. }
  102. }
  103. /**
  104. * Test checkId with a valid ID - SHA-1 hashes
  105. */
  106. public function testIsSha1SessionIdValid()
  107. {
  108. $this->assertTrue(SessionManager::checkId(sha1('shaarli')));
  109. }
  110. /**
  111. * Test checkId with a valid ID - SHA-256 hashes
  112. */
  113. public function testIsSha256SessionIdValid()
  114. {
  115. $this->assertTrue(SessionManager::checkId(hash('sha256', 'shaarli')));
  116. }
  117. /**
  118. * Test checkId with a valid ID - SHA-512 hashes
  119. */
  120. public function testIsSha512SessionIdValid()
  121. {
  122. $this->assertTrue(SessionManager::checkId(hash('sha512', 'shaarli')));
  123. }
  124. /**
  125. * Test checkId with invalid IDs.
  126. */
  127. public function testIsSessionIdInvalid()
  128. {
  129. $this->assertFalse(SessionManager::checkId(''));
  130. $this->assertFalse(SessionManager::checkId([]));
  131. $this->assertFalse(
  132. SessionManager::checkId('c0ZqcWF3VFE2NmJBdm1HMVQ0ZHJ3UmZPbTFsNGhkNHI=')
  133. );
  134. }
  135. /**
  136. * Store login information after a successful login
  137. */
  138. public function testStoreLoginInfo()
  139. {
  140. $this->sessionManager->storeLoginInfo('ip_id');
  141. $this->assertGreaterThan(time(), $this->session['expires_on']);
  142. $this->assertEquals('ip_id', $this->session['ip']);
  143. $this->assertEquals('johndoe', $this->session['username']);
  144. }
  145. /**
  146. * Extend a server-side session by SessionManager::$SHORT_TIMEOUT
  147. */
  148. public function testExtendSession()
  149. {
  150. $this->sessionManager->extendSession();
  151. $this->assertGreaterThan(time(), $this->session['expires_on']);
  152. $this->assertLessThanOrEqual(
  153. time() + SessionManager::$SHORT_TIMEOUT,
  154. $this->session['expires_on']
  155. );
  156. }
  157. /**
  158. * Extend a server-side session by SessionManager::$LONG_TIMEOUT
  159. */
  160. public function testExtendSessionStaySignedIn()
  161. {
  162. $this->sessionManager->setStaySignedIn(true);
  163. $this->sessionManager->extendSession();
  164. $this->assertGreaterThan(time(), $this->session['expires_on']);
  165. $this->assertGreaterThan(
  166. time() + SessionManager::$LONG_TIMEOUT - 10,
  167. $this->session['expires_on']
  168. );
  169. $this->assertLessThanOrEqual(
  170. time() + SessionManager::$LONG_TIMEOUT,
  171. $this->session['expires_on']
  172. );
  173. }
  174. /**
  175. * Unset session variables after logging out
  176. */
  177. public function testLogout()
  178. {
  179. $this->session = [
  180. 'ip' => 'ip_id',
  181. 'expires_on' => time() + 1000,
  182. 'username' => 'johndoe',
  183. 'visibility' => 'public',
  184. 'untaggedonly' => false,
  185. ];
  186. $this->sessionManager->logout();
  187. $this->assertFalse(isset($this->session['ip']));
  188. $this->assertFalse(isset($this->session['expires_on']));
  189. $this->assertFalse(isset($this->session['username']));
  190. $this->assertFalse(isset($this->session['visibility']));
  191. $this->assertFalse(isset($this->session['untaggedonly']));
  192. }
  193. /**
  194. * The session is active and expiration time has been reached
  195. */
  196. public function testHasExpiredTimeElapsed()
  197. {
  198. $this->session['expires_on'] = time() - 10;
  199. $this->assertTrue($this->sessionManager->hasSessionExpired());
  200. }
  201. /**
  202. * The session is active and expiration time has not been reached
  203. */
  204. public function testHasNotExpired()
  205. {
  206. $this->session['expires_on'] = time() + 1000;
  207. $this->assertFalse($this->sessionManager->hasSessionExpired());
  208. }
  209. /**
  210. * Session hijacking protection is disabled, we assume the IP has not changed
  211. */
  212. public function testHasClientIpChangedNoSessionProtection()
  213. {
  214. $this->conf->set('security.session_protection_disabled', true);
  215. $this->assertFalse($this->sessionManager->hasClientIpChanged(''));
  216. }
  217. /**
  218. * The client IP identifier has not changed
  219. */
  220. public function testHasClientIpChangedNope()
  221. {
  222. $this->session['ip'] = 'ip_id';
  223. $this->assertFalse($this->sessionManager->hasClientIpChanged('ip_id'));
  224. }
  225. /**
  226. * The client IP identifier has changed
  227. */
  228. public function testHasClientIpChanged()
  229. {
  230. $this->session['ip'] = 'ip_id_one';
  231. $this->assertTrue($this->sessionManager->hasClientIpChanged('ip_id_two'));
  232. }
  233. }