]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/BaseURLTest.php
Hardening save method in BaseURL
[friendica.git] / tests / src / Util / BaseURLTest.php
1 <?php
2 namespace Friendica\Test\src\Util;
3
4 use Friendica\Core\Config\Configuration;
5 use Friendica\Test\MockedTest;
6 use Friendica\Util\BaseURL;
7
8 class BaseURLTest extends MockedTest
9 {
10         public function dataDefault()
11         {
12                 return [
13                         'null' => [
14                                 'server' => [],
15                                 'input' => [
16                                 'hostname' => null,
17                                 'urlPath' => null,
18                                 'sslPolicy' => null,
19                                 'url' => null,
20                                         ],
21                                 'assert' => [
22                                         'hostname'  => '',
23                                         'urlPath'   => '',
24                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
25                                         'url'       => 'http://',
26                                         'scheme'    => 'http',
27                                 ],
28                         ],
29                         'WithSubDirectory' => [
30                                 'server' => [
31                                         'SERVER_NAME'  => 'friendica.local',
32                                         'REDIRECT_URI' => 'test/module/more',
33                                         'QUERY_STRING' => 'module/more',
34                                 ],
35                                 'input' => [
36                                         'hostname'  => null,
37                                         'urlPath'   => null,
38                                         'sslPolicy' => null,
39                                         'url'       => null,
40                                 ],
41                                 'assert' => [
42                                         'hostname'  => 'friendica.local',
43                                         'urlPath'   => 'test',
44                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
45                                         'url'       => 'http://friendica.local/test',
46                                         'scheme'    => 'http',
47                                 ],
48                         ],
49                         'input' => [
50                                 'server' => [],
51                                 'input' => [
52                                         'hostname'  => 'friendica.local',
53                                         'urlPath'   => 'test',
54                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
55                                         'url'       => 'http://friendica.local/test',
56                                 ],
57                                 'assert' => [
58                                         'hostname'  => 'friendica.local',
59                                         'urlPath'   => 'test',
60                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
61                                         'url'       => 'http://friendica.local/test',
62                                         'scheme'    => 'http',
63                                 ],
64                         ],
65                         'WithHttpsScheme' => [
66                                 'server' => [
67                                         'SERVER_NAME'    => 'friendica.local',
68                                         'REDIRECT_URI'   => 'test/module/more',
69                                         'QUERY_STRING'   => 'module/more',
70                                         'HTTPS'          => true,
71                                 ],
72                                 'input' => [
73                                         'hostname'  => null,
74                                         'urlPath'   => null,
75                                         'sslPolicy' => null,
76                                         'url'       => null,
77                                 ],
78                                 'assert' => [
79                                         'hostname'  => 'friendica.local',
80                                         'urlPath'   => 'test',
81                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
82                                         'url'       => 'https://friendica.local/test',
83                                         'scheme'    => 'https',
84                                 ],
85                         ],
86                         'WithoutQueryString' => [
87                                 'server' => [
88                                         'SERVER_NAME'    => 'friendica.local',
89                                         'REDIRECT_URI'   => 'test/more',
90                                         'HTTPS'          => true,
91                                 ],
92                                 'input' => [
93                                         'hostname'  => null,
94                                         'urlPath'   => null,
95                                         'sslPolicy' => null,
96                                         'url'       => null,
97                                 ],
98                                 'assert' => [
99                                         'hostname'  => 'friendica.local',
100                                         'urlPath'   => 'test/more',
101                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
102                                         'url'       => 'https://friendica.local/test/more',
103                                         'scheme'    => 'https',
104                                 ],
105                         ],
106                         'WithPort' => [
107                                 'server' => [
108                                         'SERVER_NAME'    => 'friendica.local',
109                                         'SERVER_PORT'    => '1234',
110                                         'REDIRECT_URI'   => 'test/more',
111                                         'HTTPS'          => true,
112                                 ],
113                                 'input' => [
114                                         'hostname'  => null,
115                                         'urlPath'   => null,
116                                         'sslPolicy' => null,
117                                         'url'       => null,
118                                 ],
119                                 'assert' => [
120                                         'hostname'  => 'friendica.local:1234',
121                                         'urlPath'   => 'test/more',
122                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
123                                         'url'       => 'https://friendica.local:1234/test/more',
124                                         'scheme'    => 'https',
125                                 ],
126                         ],
127                         'With443Port' => [
128                                 'server' => [
129                                         'SERVER_NAME'    => 'friendica.local',
130                                         'SERVER_PORT'    => '443',
131                                         'REDIRECT_URI'   => 'test/more',
132                                 ],
133                                 'input' => [
134                                         'hostname'  => null,
135                                         'urlPath'   => null,
136                                         'sslPolicy' => null,
137                                         'url'       => null,
138                                 ],
139                                 'assert' => [
140                                         'hostname'  => 'friendica.local',
141                                         'urlPath'   => 'test/more',
142                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
143                                         'url'       => 'https://friendica.local/test/more',
144                                         'scheme'    => 'https',
145                                 ],
146                         ],
147                         'With80Port' => [
148                                 'server' => [
149                                         'SERVER_NAME'  => 'friendica.local',
150                                         'SERVER_PORT'  => '80',
151                                         'REDIRECT_URI' => 'test/more',
152                                 ],
153                                 'input' => [
154                                         'hostname'  => null,
155                                         'urlPath'   => null,
156                                         'sslPolicy' => null,
157                                         'url'       => null,
158                                 ],
159                                 'assert' => [
160                                         'hostname'  => 'friendica.local',
161                                         'urlPath'   => 'test/more',
162                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
163                                         'url'       => 'http://friendica.local/test/more',
164                                         'scheme'    => 'http',
165                                 ],
166                         ],
167                 ];
168         }
169
170         /**
171          * Test the default config determination
172          * @dataProvider dataDefault
173          */
174         public function testCheck($server, $input, $assert)
175         {
176                 $configMock = \Mockery::mock(Configuration::class);
177                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
178                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
179                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
180                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
181
182                 if (!isset($input['urlPath']) && isset($assert['urlPath'])) {
183                         $configMock->shouldReceive('set')->with('system', 'urlpath', $assert['urlPath'])->once();
184                 }
185
186                 if (!isset($input['sslPolicy']) && isset($assert['sslPolicy'])) {
187                         $configMock->shouldReceive('set')->with('system', 'ssl_policy', $assert['sslPolicy'])->once();
188                 }
189
190                 if (!isset($input['hostname']) && !empty($assert['hostname'])) {
191                         $configMock->shouldReceive('set')->with('config', 'hostname', $assert['hostname'])->once();
192                 }
193
194                 $baseUrl = new BaseURL($configMock, $server);
195
196                 $this->assertEquals($assert['hostname'], $baseUrl->getHostname());
197                 $this->assertEquals($assert['urlPath'], $baseUrl->getUrlPath());
198                 $this->assertEquals($assert['sslPolicy'], $baseUrl->getSSLPolicy());
199                 $this->assertEquals($assert['scheme'], $baseUrl->getScheme());
200                 $this->assertEquals($assert['url'], $baseUrl->get());
201         }
202
203         public function dataSave()
204         {
205                 return [
206                         'default' => [
207                                 'input' => [
208                                         'hostname'  => 'friendica.old',
209                                         'urlPath'   => 'is/old/path',
210                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
211                                         'url'       => 'http://friendica.old/is/old/path',
212                                         'force_ssl' => true,
213                                 ],
214                                 'save' => [
215                                         'hostname'  => 'friendica.local',
216                                         'urlPath'   => 'new/path',
217                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
218                                 ],
219                                 'url' => 'https://friendica.local/new/path',
220                         ],
221                         'null' => [
222                                 'input' => [
223                                         'hostname'  => 'friendica.old',
224                                         'urlPath'   => 'is/old/path',
225                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
226                                         'url'       => 'http://friendica.old/is/old/path',
227                                         'force_ssl' => true,
228                                 ],
229                                 'save' => [
230                                         'hostname'  => null,
231                                         'urlPath'   => null,
232                                         'sslPolicy' => null,
233                                 ],
234                                 'url' => 'http://friendica.old/is/old/path',
235                         ],
236                         'changeHostname' => [
237                                 'input' => [
238                                         'hostname'  => 'friendica.old',
239                                         'urlPath'   => 'is/old/path',
240                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
241                                         'url'       => 'http://friendica.old/is/old/path',
242                                         'force_ssl' => true,
243                                 ],
244                                 'save' => [
245                                         'hostname'  => 'friendica.local',
246                                         'urlPath'   => null,
247                                         'sslPolicy' => null,
248                                 ],
249                                 'url' => 'http://friendica.local/is/old/path',
250                         ],
251                         'changeUrlPath' => [
252                                 'input' => [
253                                         'hostname'  => 'friendica.old',
254                                         'urlPath'   => 'is/old/path',
255                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
256                                         'url'       => 'http://friendica.old/is/old/path',
257                                         'force_ssl' => true,
258                                 ],
259                                 'save' => [
260                                         'hostname'  => null,
261                                         'urlPath'   => 'new/path',
262                                         'sslPolicy' => null,
263                                 ],
264                                 'url' => 'http://friendica.old/new/path',
265                         ],
266                         'changeSSLPolicy' => [
267                                 'input' => [
268                                         'hostname'  => 'friendica.old',
269                                         'urlPath'   => 'is/old/path',
270                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
271                                         'url'       => 'http://friendica.old/is/old/path',
272                                         'force_ssl' => true,
273                                 ],
274                                 'save' => [
275                                         'hostname'  => null,
276                                         'urlPath'   => null,
277                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
278                                 ],
279                                 'url' => 'https://friendica.old/is/old/path',
280                         ],
281                 ];
282         }
283
284         /**
285          * Test the save() method
286          * @dataProvider dataSave
287          */
288         public function testSave($input, $save, $url)
289         {
290                 $configMock = \Mockery::mock(Configuration::class);
291                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
292                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
293                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
294                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
295                 $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($input['force_ssl']);
296
297                 $baseUrl = new BaseURL($configMock, []);
298
299                 if (isset($save['hostname'])) {
300                         $configMock->shouldReceive('set')->with('config', 'hostname', $save['hostname'])->andReturn(true)->once();
301                 }
302
303                 if (isset($save['urlPath'])) {
304                         $configMock->shouldReceive('set')->with('system', 'urlpath', $save['urlPath'])->andReturn(true)->once();
305                 }
306
307                 if (isset($save['sslPolicy'])) {
308                         $configMock->shouldReceive('set')->with('system', 'ssl_policy', $save['sslPolicy'])->andReturn(true)->once();
309                 }
310
311                 $configMock->shouldReceive('set')->with('system', 'url', $url)->andReturn(true)->once();
312
313                 $baseUrl->save($save['hostname'], $save['sslPolicy'], $save['urlPath']);
314
315                 $this->assertEquals($url, $baseUrl->get());
316         }
317
318         /**
319          * Test the saveByUrl() method
320          * @dataProvider dataSave
321          *
322          * @param $input
323          * @param $save
324          * @param $url
325          */
326         public function testSaveByUrl($input, $save, $url)
327         {
328                 $configMock = \Mockery::mock(Configuration::class);
329                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
330                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
331                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
332                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
333                 $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($input['force_ssl']);
334
335                 $baseUrl = new BaseURL($configMock, []);
336
337                 if (isset($save['hostname'])) {
338                         $configMock->shouldReceive('set')->with('config', 'hostname', $save['hostname'])->andReturn(true)->once();
339                 }
340
341                 if (isset($save['urlPath'])) {
342                         $configMock->shouldReceive('set')->with('system', 'urlpath', $save['urlPath'])->andReturn(true)->once();
343                 }
344
345                 if (isset($save['sslPolicy'])) {
346                         $configMock->shouldReceive('set')->with('system', 'ssl_policy', $save['sslPolicy'])->andReturn(true)->once();
347                 }
348
349                 $configMock->shouldReceive('set')->with('system', 'url', $url)->andReturn(true)->once();
350
351                 $baseUrl->saveByURL($url);
352
353                 $this->assertEquals($url, $baseUrl->get());
354         }
355
356         public function dataGetBaseUrl()
357         {
358                 return [
359                         'default'           => [
360                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
361                                 'ssl'       => false,
362                                 'url'       => 'http://friendica.local/new/test',
363                                 'assert'    => 'http://friendica.local/new/test',
364                         ],
365                         'DefaultWithSSL'    => [
366                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
367                                 'ssl'       => true,
368                                 'url'       => 'http://friendica.local/new/test',
369                                 'assert'    => 'https://friendica.local/new/test',
370                         ],
371                         'SSLFullWithSSL'    => [
372                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
373                                 'ssl'       => true,
374                                 'url'       => 'http://friendica.local/new/test',
375                                 'assert'    => 'http://friendica.local/new/test',
376                         ],
377                         'SSLFullWithoutSSL' => [
378                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
379                                 'ssl'       => false,
380                                 'url'       => 'https://friendica.local/new/test',
381                                 'assert'    => 'https://friendica.local/new/test',
382                         ],
383                         'NoSSLWithSSL'      => [
384                                 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
385                                 'ssl'       => true,
386                                 'url'       => 'http://friendica.local/new/test',
387                                 'assert'    => 'http://friendica.local/new/test',
388                         ],
389                         'NoSSLWithoutSSL'   => [
390                                 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
391                                 'ssl'       => false,
392                                 'url'       => 'http://friendica.local/new/test',
393                                 'assert'    => 'http://friendica.local/new/test',
394                         ],
395                 ];
396         }
397
398         /**
399          * Test the get() method
400          * @dataProvider dataGetBaseUrl
401          */
402         public function testGetURL($sslPolicy, $ssl, $url, $assert)
403         {
404                 $configMock = \Mockery::mock(Configuration::class);
405                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
406                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
407                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($sslPolicy);
408                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($url);
409
410                 $baseUrl = new BaseURL($configMock, []);
411
412                 $this->assertEquals($assert, $baseUrl->get($ssl));
413         }
414
415         public function dataCheckRedirectHTTPS()
416         {
417                 return [
418                         'default' => [
419                                 'server' => [
420                                         'REQUEST_METHOD' => 'GET',
421                                         'HTTPS' => true,
422                                 ],
423                                 'forceSSL'  => false,
424                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
425                                 'url'       => 'https://friendica.local',
426                                 'redirect'  => false,
427                         ],
428                         'forceSSL' => [
429                                 'server' => [
430                                         'REQUEST_METHOD' => 'GET',
431                                 ],
432                                 'forceSSL'  => true,
433                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
434                                 'url'       => 'https://friendica.local',
435                                 'redirect'  => false,
436                         ],
437                         'forceSSLWithSSLPolicy' => [
438                                 'server' => [],
439                                 'forceSSL'  => true,
440                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
441                                 'url'       => 'https://friendica.local',
442                                 'redirect'  => false,
443                         ],
444                         'forceSSLWithSSLPolicyAndGet' => [
445                                 'server' => [
446                                         'REQUEST_METHOD' => 'GET',
447                                 ],
448                                 'forceSSL'  => true,
449                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
450                                 'url'       => 'https://friendica.local',
451                                 'redirect'  => true,
452                         ],
453                 ];
454         }
455
456         /**
457          * Test the checkRedirectHTTPS() method
458          * @dataProvider dataCheckRedirectHTTPS
459          */
460         public function testCheckRedirectHTTPS($server, $forceSSL, $sslPolicy, $url, $redirect)
461         {
462                 $configMock = \Mockery::mock(Configuration::class);
463                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
464                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
465                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($sslPolicy);
466                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($url);
467                 $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($forceSSL);
468
469                 $baseUrl = new BaseURL($configMock, $server);
470
471                 $this->assertEquals($redirect, $baseUrl->checkRedirectHttps());
472         }
473
474         public function dataWrongSave()
475         {
476                 return [
477                         'wrongHostname' => [
478                                 'fail' => 'hostname',
479                         ],
480                         'wrongSSLPolicy' => [
481                                 'fail' => 'sslPolicy',
482                         ],
483                         'wrongURLPath' => [
484                                 'fail' => 'urlPath',
485                         ],
486                         'wrongURL' => [
487                                 'fail' => 'url',
488                         ],
489                 ];
490         }
491
492         /**
493          * Test the save() method with wrong parameters
494          * @dataProvider dataWrongSave
495          */
496         public function testWrongSave($fail)
497         {
498                 $configMock = \Mockery::mock(Configuration::class);
499                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
500                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
501                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn(BaseURL::DEFAULT_SSL_SCHEME);
502                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn('http://friendica.local/new/test');
503
504                 switch ($fail) {
505                         case 'hostname':
506                                 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any())->andReturn(false)->once();
507                                 break;
508                         case 'sslPolicy':
509                                 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any())->andReturn(true)->twice();
510                                 $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any())->andReturn(false)->once();
511                                 break;
512                         case 'urlPath':
513                                 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any())->andReturn(true)->twice();
514                                 $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any())->andReturn(true)->twice();
515                                 $configMock->shouldReceive('set')->with('system', 'urlpath', \Mockery::any())->andReturn(false)->once();
516                                 break;
517                         case 'url':
518                                 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any())->andReturn(true)->twice();
519                                 $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any())->andReturn(true)->twice();
520                                 $configMock->shouldReceive('set')->with('system', 'urlpath', \Mockery::any())->andReturn(true)->twice();
521                                 $configMock->shouldReceive('set')->with('system', 'url', \Mockery::any())->andReturn(false)->once();
522                                 break;
523                 }
524
525                 $baseUrl = new BaseURL($configMock, []);
526                 $this->assertFalse($baseUrl->save('test', 10, 'nope'));
527
528                 // nothing should have changed because we never successfully saved anything
529                 $this->assertEquals($baseUrl->getHostname(), 'friendica.local');
530                 $this->assertEquals($baseUrl->getUrlPath(), 'new/test');
531                 $this->assertEquals($baseUrl->getSSLPolicy(), BaseURL::DEFAULT_SSL_SCHEME);
532                 $this->assertEquals($baseUrl->get(), 'http://friendica.local/new/test');
533         }
534 }