1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
<?php
class MWRestrictionsTest extends MediaWikiUnitTestCase {
protected static $restrictionsForChecks;
public static function setUpBeforeClass() : void {
self::$restrictionsForChecks = MWRestrictions::newFromArray( [
'IPAddresses' => [
'10.0.0.0/8',
'172.16.0.0/12',
'2001:db8::/33',
]
] );
}
/**
* @covers MWRestrictions::newDefault
* @covers MWRestrictions::__construct
*/
public function testNewDefault() {
$ret = MWRestrictions::newDefault();
$this->assertInstanceOf( MWRestrictions::class, $ret );
$this->assertSame(
'{"IPAddresses":["0.0.0.0/0","::/0"]}',
$ret->toJson()
);
}
/**
* @covers MWRestrictions::newFromArray
* @covers MWRestrictions::__construct
* @covers MWRestrictions::loadFromArray
* @covers MWRestrictions::toArray
* @dataProvider provideArray
* @param array $data
* @param bool|InvalidArgumentException $expect True if the call succeeds,
* otherwise the exception that should be thrown.
*/
public function testArray( $data, $expect ) {
if ( $expect === true ) {
$ret = MWRestrictions::newFromArray( $data );
$this->assertInstanceOf( MWRestrictions::class, $ret );
$this->assertSame( $data, $ret->toArray() );
} else {
try {
MWRestrictions::newFromArray( $data );
$this->fail( 'Expected exception not thrown' );
} catch ( InvalidArgumentException $ex ) {
$this->assertEquals( $expect, $ex );
}
}
}
public static function provideArray() {
return [
[ [ 'IPAddresses' => [] ], true ],
[ [ 'IPAddresses' => [ '127.0.0.1/32' ] ], true ],
[
[ 'IPAddresses' => [ '256.0.0.1/32' ] ],
new InvalidArgumentException( 'Invalid IP address: 256.0.0.1/32' )
],
[
[ 'IPAddresses' => '127.0.0.1/32' ],
new InvalidArgumentException( 'IPAddresses is not an array' )
],
[
[],
new InvalidArgumentException( 'Array is missing required keys: IPAddresses' )
],
[
[ 'foo' => 'bar', 'bar' => 42 ],
new InvalidArgumentException( 'Array contains invalid keys: foo, bar' )
],
];
}
/**
* @covers MWRestrictions::newFromJson
* @covers MWRestrictions::__construct
* @covers MWRestrictions::loadFromArray
* @covers MWRestrictions::toJson
* @covers MWRestrictions::__toString
* @dataProvider provideJson
* @param string $json
* @param array|InvalidArgumentException $expect
*/
public function testJson( $json, $expect ) {
if ( is_array( $expect ) ) {
$ret = MWRestrictions::newFromJson( $json );
$this->assertInstanceOf( MWRestrictions::class, $ret );
$this->assertSame( $expect, $ret->toArray() );
$this->assertSame( $json, $ret->toJson( false ) );
$this->assertSame( $json, (string)$ret );
$this->assertSame(
FormatJson::encode( $expect, true, FormatJson::ALL_OK ),
$ret->toJson( true )
);
} else {
try {
MWRestrictions::newFromJson( $json );
$this->fail( 'Expected exception not thrown' );
} catch ( InvalidArgumentException $ex ) {
$this->assertTrue( true );
}
}
}
public static function provideJson() {
return [
[
'{"IPAddresses":[]}',
[ 'IPAddresses' => [] ]
],
[
'{"IPAddresses":["127.0.0.1/32"]}',
[ 'IPAddresses' => [ '127.0.0.1/32' ] ]
],
[
'{"IPAddresses":["256.0.0.1/32"]}',
new InvalidArgumentException( 'Invalid IP address: 256.0.0.1/32' )
],
[
'{"IPAddresses":"127.0.0.1/32"}',
new InvalidArgumentException( 'IPAddresses is not an array' )
],
[
'{}',
new InvalidArgumentException( 'Array is missing required keys: IPAddresses' )
],
[
'{"foo":"bar","bar":42}',
new InvalidArgumentException( 'Array contains invalid keys: foo, bar' )
],
[
'{"IPAddresses":[]',
new InvalidArgumentException( 'Invalid restrictions JSON' )
],
[
'"IPAddresses"',
new InvalidArgumentException( 'Invalid restrictions JSON' )
],
];
}
/**
* @covers MWRestrictions::checkIP
* @dataProvider provideCheckIP
* @param string $ip
* @param bool $pass
*/
public function testCheckIP( $ip, $pass ) {
$this->assertSame( $pass, self::$restrictionsForChecks->checkIP( $ip ) );
}
public static function provideCheckIP() {
return [
[ '10.0.0.1', true ],
[ '172.16.0.0', true ],
[ '192.0.2.1', false ],
[ '2001:db8:1::', true ],
[ '2001:0db8:0000:0000:0000:0000:0000:0000', true ],
[ '2001:0DB8:8000::', false ],
];
}
/**
* @covers MWRestrictions::check
* @dataProvider provideCheck
* @param WebRequest $request
* @param Status $expect
*/
public function testCheck( $request, $expect ) {
$this->assertEquals( $expect, self::$restrictionsForChecks->check( $request ) );
}
public function provideCheck() {
$ret = [];
$mockBuilder = $this->getMockBuilder( FauxRequest::class )
->setMethods( [ 'getIP' ] );
foreach ( self::provideCheckIP() as $checkIP ) {
$ok = [];
$request = $mockBuilder->getMock();
$request->expects( $this->any() )->method( 'getIP' )
->will( $this->returnValue( $checkIP[0] ) );
$ok['ip'] = $checkIP[1];
/* If we ever add more restrictions, add nested for loops here:
* foreach ( self::provideCheckFoo() as $checkFoo ) {
* $request->expects( $this->any() )->method( 'getFoo' )
* ->will( $this->returnValue( $checkFoo[0] );
* $ok['foo'] = $checkFoo[1];
*
* foreach ( self::provideCheckBar() as $checkBar ) {
* $request->expects( $this->any() )->method( 'getBar' )
* ->will( $this->returnValue( $checkBar[0] );
* $ok['bar'] = $checkBar[1];
*
* // etc.
* }
* }
*/
$status = Status::newGood();
$status->setResult( $ok === array_filter( $ok ), $ok );
$ret[] = [ $request, $status ];
}
return $ret;
}
}
|