| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Tests\Unit; |
| 22 |
|
| 23 |
use MediaWiki\HookContainer\HookContainer; |
| 24 |
use MediaWiki\Request\ProxyLookup; |
| 25 |
use MediaWikiUnitTestCase; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class ProxyLookupTest extends MediaWikiUnitTestCase { |
| 33 |
|
| 34 |
|
| 35 |
|
| 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 |
|
| 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 |
|
| 58 |
|
| 59 |
|
| 60 |
public function testIsConfiguredProxy( string $ip, bool $expected ) { |
| 61 |
|
| 62 |
$hookContainer = $this->createNoOpMock( HookContainer::class ); |
| 63 |
|
| 64 |
|
| 65 |
|
| 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 |
|
| 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 |
|
| 91 |
|
| 92 |
|
| 93 |
public function testIsTrustedProxy( |
| 94 |
string $ip, |
| 95 |
bool $expectedForHookCall, |
| 96 |
bool $hookResult |
| 97 |
) { |
| 98 |
|
| 99 |
$hookCalled = false; |
| 100 |
$hookCallback = function ( $hookIP, &$trusted ) use ( &$hookCalled, $ip, $expectedForHookCall, $hookResult ) { |
| 101 |
$hookCalled = true; |
| 102 |
|
| 103 |
|
| 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 |
} |