Clean AbuseFilterParserTests

Mostly delete result files and assume the result is always true. The few
exceptions were either moved to standalone test, or inverted.

Change-Id: I6c06e596587750c4ebaabafbd277bc75eeb436a5
This commit is contained in:
Daimona Eaytoy 2018-12-31 18:12:35 +01:00
parent 7a6468fa29
commit 0ff581e246
77 changed files with 57 additions and 156 deletions

View file

@ -1,52 +0,0 @@
<?php
/**
* Runs tests against the PHP parser.
*/
require_once getenv( 'MW_INSTALL_PATH' ) !== false
? getenv( 'MW_INSTALL_PATH' ) . "/maintenance/commandLine.inc"
: __DIR__ . '/../../../maintenance/commandLine.inc';
$tester = new AbuseFilterParser;
$test_path = __DIR__ . "/parserTests";
$tests = glob( $test_path . "/*.t" );
$check = 0;
$pass = 0;
foreach ( $tests as $test ) {
$result = substr( $test, 0, -2 ) . ".r";
$rule = trim( file_get_contents( $test ) );
$output = trim( file_get_contents( $result ) ) === 'MATCH';
$testname = basename( $test );
print "Trying test $testname...\n";
try {
$check++;
$actual = intval( $tester->parse( $rule ) );
if ( $actual === $output ) {
print "-PASSED.\n";
$pass++;
} else {
print "-FAILED - expected output $output, actual output $actual.\n";
print "-Expression: $rule\n";
// export
$vars = var_export( $tester->mTokens, true );
file_put_contents( $test . '.parsed', $vars );
}
} catch ( AFPException $excep ) {
print "-FAILED - exception " . $excep->getMessage() . " with input $rule\n";
// export
$vars = var_export( $tester->mTokens, true );
file_put_contents( $test . '.parsed', $vars );
}
}
print "$pass tests passed out of $check\n";

View file

@ -1 +0,0 @@
NOT_MATCH

View file

@ -1 +0,0 @@
;;;;

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
NOT MATCH

View file

@ -1,7 +0,0 @@
/* See T204841 */
a := [false, false];
b := [false, false];
c := 42;
d := [0,1];
a[0] != false & b[1] != false & (b[5**2/(5*(4+1))] !== a[43-c] | a[d[0]] === b[d[c-41]])

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
NOT MATCH

View file

@ -1 +0,0 @@
( )

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
NOT_MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
NOT_MATCH

View file

@ -1,2 +1,2 @@
/* The division by zero should not be executed and not crash the filter */
false & 1/0
!(false & 1/0)

View file

@ -1 +0,0 @@
NOT_MATCH

View file

@ -1,2 +1,2 @@
/* See T214642 */
1 = 0 & 1 < 3 * 24 * ( 3600 + 47 ) ** 2
!(1 = 0 & 1 < 3 * 24 * ( 3600 + 47 ) ** 2)

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -1 +0,0 @@
MATCH

View file

@ -46,6 +46,7 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
* @return AbuseFilterParser
*/
public static function getParser() {
/** @var AbuseFilterParser */
static $parser = null;
if ( !$parser ) {
$parser = new AbuseFilterParser();
@ -71,43 +72,35 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
}
/**
* @param string $rule The rule to parse
* @dataProvider readTests
*/
public function testParser( $testName, $rule, $expected ) {
public function testParser( $rule ) {
foreach ( self::getParsers() as $parser ) {
$actual = $parser->parse( $rule );
$this->assertEquals( $expected, $actual, 'Running parser test ' . $testName );
$this->assertTrue( $parser->parse( $rule ) );
}
}
/**
* @return array
* @return Generator|array
*/
public function readTests() {
$tests = [];
$testPath = __DIR__ . "/../parserTests";
$testFiles = glob( $testPath . "/*.t" );
foreach ( $testFiles as $testFile ) {
$testName = substr( $testFile, 0, -2 );
$resultFile = $testName . '.r';
$testName = basename( substr( $testFile, 0, -2 ) );
$rule = trim( file_get_contents( $testFile ) );
$result = trim( file_get_contents( $resultFile ) ) === 'MATCH';
$tests[] = [
basename( $testName ),
$rule,
$result
];
yield $testName => [ $rule ];
}
return $tests;
}
/**
* Test expression evaluation
*
* @param string $expr The expression to evaluate
* @param string $expected The expected result
* @dataProvider provideExpressions
*/
public function testEvaluateExpression( $expr, $expected ) {
@ -133,14 +126,43 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
];
}
/**
* Test empty (or almost empty) syntax and ensure it doesn't match
*
* @param string $code
* @dataProvider provideEmptySyntax
*/
public function testEmptySyntax( $code ) {
foreach ( self::getParsers() as $parser ) {
$this->assertFalse( $parser->parse( $code ) );
}
}
/**
* Data provider for testEmptySyntax
*
* @return array
*/
public function provideEmptySyntax() {
return [
[ '' ],
[ '()' ],
[ ';;;;' ]
];
}
/**
* Ensure that AbuseFilterTokenizer::OPERATOR_RE matches the contents
* and order of AbuseFilterTokenizer::$operators.
*/
public function testOperatorRe() {
$operatorRe = '/(' . implode( '|', array_map( function ( $op ) {
return preg_quote( $op, '/' );
}, AbuseFilterTokenizer::$operators ) ) . ')/A';
$quotedOps = array_map(
function ( $op ) {
return preg_quote( $op, '/' );
},
AbuseFilterTokenizer::$operators
);
$operatorRe = '/(' . implode( '|', $quotedOps ) . ')/A';
$this->assertEquals( $operatorRe, AbuseFilterTokenizer::OPERATOR_RE );
}
@ -157,6 +179,8 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
/**
* Ensure the number of conditions counted for given expressions is right.
*
* @param string $rule The rule to parse
* @param int $expected The expected amount of used conditions
* @dataProvider condCountCases
*/
public function testCondCount( $rule, $expected ) {
@ -188,6 +212,15 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
];
}
/**
* Test for T204841
*/
public function testArrayShortcircuit() {
$code = 'a := [false, false]; b := [false, false]; c := 42; d := [0,1];' .
'a[0] != false & b[1] != false & (b[5**2/(5*(4+1))] !== a[43-c] | a[d[0]] === b[d[c-41]])';
$this->assertFalse( self::getParser()->parse( $code ) );
}
/**
* Ensure get_matches function captures returns expected output.
* @param string $needle Regex to pass to get_matches.
@ -743,19 +776,17 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
$this->fail( "The use of the deprecated variable $old was not logged." );
}
$this->assertTrue( $actual, "AbuseFilter deprecated variable $old is not parsed correctly" );
$this->assertTrue( $actual );
}
/**
* Data provider for testDeprecatedVars
* @return array
* @return Generator|array
*/
public function provideDeprecatedVars() {
$deprecated = AbuseFilter::$deprecatedVars;
$data = [];
foreach ( $deprecated as $old => $new ) {
$data[] = [ $old, $new ];
yield $old => [ $old, $new ];
}
return $data;
}
}