2013-07-02 01:43:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Tests for the AbuseFilter parser
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* @group Test
|
|
|
|
* @group AbuseFilter
|
|
|
|
*
|
|
|
|
* @licence GNU GPL v2+
|
|
|
|
* @author Marius Hoch < hoo@online.de >
|
|
|
|
*/
|
|
|
|
class AbuseFilterParserTest extends MediaWikiTestCase {
|
|
|
|
/**
|
|
|
|
* @return AbuseFilterParser
|
|
|
|
*/
|
|
|
|
static function getParser() {
|
|
|
|
static $parser = null;
|
|
|
|
if ( !$parser ) {
|
|
|
|
$parser = new AbuseFilterParser();
|
|
|
|
}
|
|
|
|
return $parser;
|
|
|
|
}
|
|
|
|
|
2016-08-24 04:52:58 +00:00
|
|
|
/**
|
|
|
|
* @return [AbuseFilterParser]
|
|
|
|
*/
|
|
|
|
static function getParsers() {
|
|
|
|
static $parsers = null;
|
|
|
|
if ( !$parsers ) {
|
|
|
|
$parsers = [
|
|
|
|
new AbuseFilterParser(),
|
|
|
|
new AbuseFilterCachingParser()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return $parsers;
|
|
|
|
}
|
|
|
|
|
2013-07-02 01:43:18 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider readTests
|
|
|
|
*/
|
|
|
|
public function testParser( $testName, $rule, $expected ) {
|
2016-08-24 04:52:58 +00:00
|
|
|
foreach ( self::getParsers() as $parser ) {
|
|
|
|
$actual = $parser->parse( $rule );
|
|
|
|
$this->assertEquals( $expected, $actual, 'Running parser test ' . $testName );
|
|
|
|
}
|
2013-07-02 01:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function readTests() {
|
2017-06-15 14:23:34 +00:00
|
|
|
$tests = [];
|
2013-07-02 01:43:18 +00:00
|
|
|
$testPath = __DIR__ . "/../parserTests";
|
|
|
|
$testFiles = glob( $testPath . "/*.t" );
|
|
|
|
|
|
|
|
foreach ( $testFiles as $testFile ) {
|
|
|
|
$testName = substr( $testFile, 0, -2 );
|
|
|
|
|
|
|
|
$resultFile = $testName . '.r';
|
|
|
|
$rule = trim( file_get_contents( $testFile ) );
|
|
|
|
$result = trim( file_get_contents( $resultFile ) ) == 'MATCH';
|
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
$tests[] = [
|
2013-07-02 01:43:18 +00:00
|
|
|
basename( $testName ),
|
|
|
|
$rule,
|
|
|
|
$result
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2013-07-02 01:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $tests;
|
|
|
|
}
|
2015-08-25 17:27:15 +00:00
|
|
|
|
|
|
|
/**
|
2016-04-09 13:00:02 +00:00
|
|
|
* Ensure that AbuseFilterTokenizer::OPERATOR_RE matches the contents
|
2015-08-25 19:57:23 +00:00
|
|
|
* and order of AbuseFilterTokenizer::$operators.
|
2015-08-25 17:27:15 +00:00
|
|
|
*/
|
|
|
|
public function testOperatorRe() {
|
|
|
|
$operatorRe = '/(' . implode( '|', array_map( function ( $op ) {
|
|
|
|
return preg_quote( $op, '/' );
|
2015-08-25 19:57:23 +00:00
|
|
|
}, AbuseFilterTokenizer::$operators ) ) . ')/A';
|
|
|
|
$this->assertEquals( $operatorRe, AbuseFilterTokenizer::OPERATOR_RE );
|
2015-08-25 17:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-09 13:00:02 +00:00
|
|
|
* Ensure that AbuseFilterTokenizer::RADIX_RE matches the contents
|
2015-08-25 19:57:23 +00:00
|
|
|
* and order of AbuseFilterTokenizer::$bases.
|
2015-08-25 17:27:15 +00:00
|
|
|
*/
|
|
|
|
public function testRadixRe() {
|
2015-08-25 19:57:23 +00:00
|
|
|
$baseClass = implode( '', array_keys( AbuseFilterTokenizer::$bases ) );
|
2015-08-25 17:27:15 +00:00
|
|
|
$radixRe = "/([0-9A-Fa-f]+(?:\.\d*)?|\.\d+)([$baseClass])?/Au";
|
2015-08-25 19:57:23 +00:00
|
|
|
$this->assertEquals( $radixRe, AbuseFilterTokenizer::RADIX_RE );
|
2015-08-25 17:27:15 +00:00
|
|
|
}
|
2016-04-09 13:00:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure the number of conditions counted for given expressions is right.
|
|
|
|
*
|
|
|
|
* @dataProvider condCountCases
|
|
|
|
*/
|
|
|
|
public function testCondCount( $rule, $expected ) {
|
|
|
|
$parser = self::getParser();
|
|
|
|
// Set some variables for convenience writing test cases
|
|
|
|
$parser->setVars( array_combine( range( 'a', 'f' ), range( 'a', 'f' ) ) );
|
|
|
|
$countBefore = AbuseFilter::$condCount;
|
|
|
|
$parser->parse( $rule );
|
|
|
|
$countAfter = AbuseFilter::$condCount;
|
|
|
|
$actual = $countAfter - $countBefore;
|
|
|
|
$this->assertEquals( $expected, $actual, 'Condition count for ' . $rule );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function condCountCases() {
|
2017-06-15 14:23:34 +00:00
|
|
|
return [
|
|
|
|
[ '(((a == b)))', 1 ],
|
|
|
|
[ 'contains_any(a, b, c)', 1 ],
|
|
|
|
[ 'a == b == c', 2 ],
|
|
|
|
[ 'a in b + c in d + e in f', 3 ],
|
|
|
|
[ 'true', 0 ],
|
|
|
|
[ 'a == a | c == d', 1 ],
|
|
|
|
[ 'a == b & c == d', 1 ],
|
|
|
|
];
|
2016-04-09 13:00:02 +00:00
|
|
|
}
|
2013-07-02 01:43:18 +00:00
|
|
|
}
|