UtilsTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. * Utilities' tests
  4. */
  5. require_once 'application/Utils.php';
  6. require_once 'tests/utils/ReferenceSessionIdHashes.php';
  7. // Initialize reference data before PHPUnit starts a session
  8. ReferenceSessionIdHashes::genAllHashes();
  9. /**
  10. * Unitary tests for Shaarli utilities
  11. */
  12. class UtilsTest extends PHPUnit_Framework_TestCase
  13. {
  14. // Session ID hashes
  15. protected static $sidHashes = null;
  16. // Log file
  17. protected static $testLogFile = 'tests.log';
  18. // Expected log date format
  19. protected static $dateFormat = 'Y/m/d H:i:s';
  20. /**
  21. * Assign reference data
  22. */
  23. public static function setUpBeforeClass()
  24. {
  25. self::$sidHashes = ReferenceSessionIdHashes::getHashes();
  26. }
  27. /**
  28. * Resets test data before each test
  29. */
  30. protected function setUp()
  31. {
  32. if (file_exists(self::$testLogFile)) {
  33. unlink(self::$testLogFile);
  34. }
  35. }
  36. /**
  37. * Returns a list of the elements from the last logged entry
  38. *
  39. * @return list (date, ip address, message)
  40. */
  41. protected function getLastLogEntry()
  42. {
  43. $logFile = file(self::$testLogFile);
  44. return explode(' - ', trim(array_pop($logFile), PHP_EOL));
  45. }
  46. /**
  47. * Log a message to a file - IPv4 client address
  48. */
  49. public function testLogmIp4()
  50. {
  51. $logMessage = 'IPv4 client connected';
  52. logm(self::$testLogFile, '127.0.0.1', $logMessage);
  53. list($date, $ip, $message) = $this->getLastLogEntry();
  54. $this->assertInstanceOf(
  55. 'DateTime',
  56. DateTime::createFromFormat(self::$dateFormat, $date)
  57. );
  58. $this->assertTrue(
  59. filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false
  60. );
  61. $this->assertEquals($logMessage, $message);
  62. }
  63. /**
  64. * Log a message to a file - IPv6 client address
  65. */
  66. public function testLogmIp6()
  67. {
  68. $logMessage = 'IPv6 client connected';
  69. logm(self::$testLogFile, '2001:db8::ff00:42:8329', $logMessage);
  70. list($date, $ip, $message) = $this->getLastLogEntry();
  71. $this->assertInstanceOf(
  72. 'DateTime',
  73. DateTime::createFromFormat(self::$dateFormat, $date)
  74. );
  75. $this->assertTrue(
  76. filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false
  77. );
  78. $this->assertEquals($logMessage, $message);
  79. }
  80. /**
  81. * Represent a link by its hash
  82. */
  83. public function testSmallHash()
  84. {
  85. $this->assertEquals('CyAAJw', smallHash('http://test.io'));
  86. $this->assertEquals(6, strlen(smallHash('https://github.com')));
  87. }
  88. /**
  89. * Look for a substring at the beginning of a string
  90. */
  91. public function testStartsWithCaseInsensitive()
  92. {
  93. $this->assertTrue(startsWith('Lorem ipsum', 'lorem', false));
  94. $this->assertTrue(startsWith('Lorem ipsum', 'LoReM i', false));
  95. }
  96. /**
  97. * Look for a substring at the beginning of a string (case-sensitive)
  98. */
  99. public function testStartsWithCaseSensitive()
  100. {
  101. $this->assertTrue(startsWith('Lorem ipsum', 'Lorem', true));
  102. $this->assertFalse(startsWith('Lorem ipsum', 'lorem', true));
  103. $this->assertFalse(startsWith('Lorem ipsum', 'LoReM i', true));
  104. }
  105. /**
  106. * Look for a substring at the beginning of a string (Unicode)
  107. */
  108. public function testStartsWithSpecialChars()
  109. {
  110. $this->assertTrue(startsWith('å!ùµ', 'å!', false));
  111. $this->assertTrue(startsWith('µ$åù', 'µ$', true));
  112. }
  113. /**
  114. * Look for a substring at the end of a string
  115. */
  116. public function testEndsWithCaseInsensitive()
  117. {
  118. $this->assertTrue(endsWith('Lorem ipsum', 'ipsum', false));
  119. $this->assertTrue(endsWith('Lorem ipsum', 'm IpsUM', false));
  120. }
  121. /**
  122. * Look for a substring at the end of a string (case-sensitive)
  123. */
  124. public function testEndsWithCaseSensitive()
  125. {
  126. $this->assertTrue(endsWith('lorem Ipsum', 'Ipsum', true));
  127. $this->assertFalse(endsWith('lorem Ipsum', 'ipsum', true));
  128. $this->assertFalse(endsWith('lorem Ipsum', 'M IPsuM', true));
  129. }
  130. /**
  131. * Look for a substring at the end of a string (Unicode)
  132. */
  133. public function testEndsWithSpecialChars()
  134. {
  135. $this->assertTrue(endsWith('å!ùµ', 'ùµ', false));
  136. $this->assertTrue(endsWith('µ$åù', 'åù', true));
  137. }
  138. /**
  139. * Check valid date strings, according to a DateTime format
  140. */
  141. public function testCheckValidDateFormat()
  142. {
  143. $this->assertTrue(checkDateFormat('Ymd', '20150627'));
  144. $this->assertTrue(checkDateFormat('Y-m-d', '2015-06-27'));
  145. }
  146. /**
  147. * Check erroneous date strings, according to a DateTime format
  148. */
  149. public function testCheckInvalidDateFormat()
  150. {
  151. $this->assertFalse(checkDateFormat('Ymd', '2015'));
  152. $this->assertFalse(checkDateFormat('Y-m-d', '2015-06'));
  153. $this->assertFalse(checkDateFormat('Ymd', 'DeLorean'));
  154. }
  155. /**
  156. * Test generate location with valid data.
  157. */
  158. public function testGenerateLocation() {
  159. $ref = 'http://localhost/?test';
  160. $this->assertEquals($ref, generateLocation($ref, 'localhost'));
  161. $ref = 'http://localhost:8080/?test';
  162. $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
  163. $ref = '?localreferer#hash';
  164. $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
  165. }
  166. /**
  167. * Test generate location - anti loop.
  168. */
  169. public function testGenerateLocationLoop() {
  170. $ref = 'http://localhost/?test';
  171. $this->assertEquals('?', generateLocation($ref, 'localhost', array('test')));
  172. }
  173. /**
  174. * Test generate location - from other domain.
  175. */
  176. public function testGenerateLocationOut() {
  177. $ref = 'http://somewebsite.com/?test';
  178. $this->assertEquals('?', generateLocation($ref, 'localhost'));
  179. }
  180. /**
  181. * Test is_session_id_valid with a valid ID - TEST ALL THE HASHES!
  182. *
  183. * This tests extensively covers all hash algorithms / bit representations
  184. */
  185. public function testIsAnyHashSessionIdValid()
  186. {
  187. foreach (self::$sidHashes as $algo => $bpcs) {
  188. foreach ($bpcs as $bpc => $hash) {
  189. $this->assertTrue(is_session_id_valid($hash));
  190. }
  191. }
  192. }
  193. /**
  194. * Test is_session_id_valid with a valid ID - SHA-1 hashes
  195. */
  196. public function testIsSha1SessionIdValid()
  197. {
  198. $this->assertTrue(is_session_id_valid(sha1('shaarli')));
  199. }
  200. /**
  201. * Test is_session_id_valid with a valid ID - SHA-256 hashes
  202. */
  203. public function testIsSha256SessionIdValid()
  204. {
  205. $this->assertTrue(is_session_id_valid(hash('sha256', 'shaarli')));
  206. }
  207. /**
  208. * Test is_session_id_valid with a valid ID - SHA-512 hashes
  209. */
  210. public function testIsSha512SessionIdValid()
  211. {
  212. $this->assertTrue(is_session_id_valid(hash('sha512', 'shaarli')));
  213. }
  214. /**
  215. * Test is_session_id_valid with invalid IDs.
  216. */
  217. public function testIsSessionIdInvalid()
  218. {
  219. $this->assertFalse(is_session_id_valid(''));
  220. $this->assertFalse(is_session_id_valid(array()));
  221. $this->assertFalse(
  222. is_session_id_valid('c0ZqcWF3VFE2NmJBdm1HMVQ0ZHJ3UmZPbTFsNGhkNHI=')
  223. );
  224. }
  225. /**
  226. * Test text2clickable without a redirector being set.
  227. */
  228. public function testText2clickableWithoutRedirector()
  229. {
  230. $text = 'stuff http://hello.there/is=someone#here otherstuff';
  231. $expectedText = 'stuff <a href="http://hello.there/is=someone#here">http://hello.there/is=someone#here</a> otherstuff';
  232. $processedText = text2clickable($text, '');
  233. $this->assertEquals($expectedText, $processedText);
  234. }
  235. /**
  236. * Test text2clickable a redirector set.
  237. */
  238. public function testText2clickableWithRedirector()
  239. {
  240. $text = 'stuff http://hello.there/is=someone#here otherstuff';
  241. $redirector = 'http://redirector.to';
  242. $expectedText = 'stuff <a href="'.
  243. $redirector .
  244. urlencode('http://hello.there/is=someone#here') .
  245. '">http://hello.there/is=someone#here</a> otherstuff';
  246. $processedText = text2clickable($text, $redirector);
  247. $this->assertEquals($expectedText, $processedText);
  248. }
  249. /**
  250. * Test testSpace2nbsp.
  251. */
  252. public function testSpace2nbsp()
  253. {
  254. $text = ' Are you thrilled by flags ?'. PHP_EOL .' Really?';
  255. $expectedText = '&nbsp; Are you &nbsp; thrilled &nbsp;by flags &nbsp; ?'. PHP_EOL .'&nbsp;Really?';
  256. $processedText = space2nbsp($text);
  257. $this->assertEquals($expectedText, $processedText);
  258. }
  259. }