Debian Codegraph @

tests/phpunit/unit/includes/Request/ProxyLookupTest.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Tests\Unit;
22
23 use MediaWiki\HookContainer\HookContainer;
24 use MediaWiki\Request\ProxyLookup;
25 use MediaWikiUnitTestCase;
26
27 /**
28 * @author DannyS712
29 *
30 * @coversDefaultClass \MediaWiki\Request\ProxyLookup
31 */
32 class ProxyLookupTest extends MediaWikiUnitTestCase {
33
34 /**
35 * @covers ::__construct
36 */
37 public function testConstruct() {
38 $proxyLookup = new ProxyLookup(
39 [],
40 [],
41 $this->createNoOpMock( HookContainer::class )
42 );
43 $this->assertInstanceOf( ProxyLookup::class, $proxyLookup, 'No errors' );
44 }
45
46 public static function provideIsConfiguredProxy() {
47 // $ip, $expected
48 yield 'Listed ip #1' => [ '1.1.1.1', true ];
49 yield 'Listed ip #2' => [ '2.2.2.2', true ];
50 yield 'In complex list #1' => [ '127.0.0.127', true ];
51 yield 'In complex list #2' => [ '255.0.0.127', true ];
52 yield 'Not listed #1' => [ '3.3.3.3', false ];
53 yield 'Not listed #2' => [ '255.255.255.255', false ];
54 }
55
56 /**
57 * @covers ::isConfiguredProxy
58 * @dataProvider provideIsConfiguredProxy
59 */
60 public function testIsConfiguredProxy( string $ip, bool $expected ) {
61 // Should never be called
62 $hookContainer = $this->createNoOpMock( HookContainer::class );
63
64 // Not an exhaustive test of the functionality of IPSet since that has its own
65 // tests, just need to make sure it works
66 $proxyLookup = new ProxyLookup(
67 [
68 '1.1.1.1',
69 '2.2.2.2',
70 ],
71 [
72 '127.0.0.0/24',
73 '255.0.0.0/24',
74 ],
75 $hookContainer
76 );
77
78 $this->assertSame( $expected, $proxyLookup->isConfiguredProxy( $ip ) );
79 }
80
81 public static function provideIsTrustedProxy() {
82 // $ip, $expectedForHookCall, $hookResult
83 yield 'Listed, hook return true' => [ '1.1.1.1', true, true ];
84 yield 'Listed, hook return false' => [ '1.1.1.1', true, false ];
85 yield 'Not listed, hook return true' => [ '2.2.2.2', false, true ];
86 yield 'Not listed, hook return false' => [ '2.2.2.2', false, false ];
87 }
88
89 /**
90 * @covers ::isTrustedProxy
91 * @dataProvider provideIsTrustedProxy
92 */
93 public function testIsTrustedProxy(
94 string $ip,
95 bool $expectedForHookCall,
96 bool $hookResult
97 ) {
98 // Hook should be called with the correct parameters
99 $hookCalled = false;
100 $hookCallback = function ( $hookIP, &$trusted ) use ( &$hookCalled, $ip, $expectedForHookCall, $hookResult ) {
101 $hookCalled = true;
102
103 // Make sure called correctly
104 $this->assertSame( $ip, $hookIP, 'Hook called with the right IP' );
105 $this->assertSame( $expectedForHookCall, $trusted, 'Hook called with the correct $trusted' );
106 $trusted = $hookResult;
107 };
108
109 $hookContainer = $this->createHookContainer( [
110 'IsTrustedProxy' => $hookCallback,
111 ] );
112
113 $proxyLookup = new ProxyLookup(
114 [ '1.1.1.1' ],
115 [],
116 $hookContainer
117 );
118
119 $this->assertSame( $hookResult, $proxyLookup->isTrustedProxy( $ip ) );
120 $this->assertTrue( $hookCalled );
121 }
122
123 }