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