1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

ext/standard: add a bunch of whacky stream filter tests

This commit is contained in:
Gina Peter Banyard
2025-10-02 19:30:20 +01:00
parent c6d8ccb0d5
commit 9c33091713
12 changed files with 348 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
--TEST--
stream_filter_register() with a class that coerces the $consumed parameter of filter method
--XFAIL--
This leaks memory
--FILE--
<?php
class foo extends php_user_filter {
public function filter($in, $out, &$consumed, bool $closing): int {
$consumed = new stdClass();
return PSFS_PASS_ON;
}
}
var_dump(stream_filter_register("invalid_filter", "foo"));
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
$out = fwrite(STDOUT, "Hello\n");
var_dump($out);
?>
--EXPECTF--
bool(true)
resource(4) of type (stream filter)
Warning: Object of class stdClass could not be converted to int in %s on line %d
Warning: fwrite(): Unprocessed filter buckets remaining on input brigade in %s on line %d
int(1)
Warning: Object of class stdClass could not be converted to int in Unknown on line 0

View File

@@ -0,0 +1,33 @@
--TEST--
stream_filter_register() with a class name that exist but does not extend php_user_filter nor mock anything
--FILE--
<?php
class foo {
}
var_dump(stream_filter_register("invalid_filter", "foo"));
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
$out = fwrite(STDOUT, "Hello\n");
var_dump($out);
?>
--EXPECTF--
bool(true)
Deprecated: Creation of dynamic property foo::$filtername is deprecated in %s on line %d
Deprecated: Creation of dynamic property foo::$params is deprecated in %s on line %d
resource(%d) of type (stream filter)
Warning: fwrite(): Unprocessed filter buckets remaining on input brigade in %s on line %d
Fatal error: Uncaught Error: Invalid callback foo::filter, class foo does not have a method "filter" in %s:%d
Stack trace:
#0 %s(%d): fwrite(Resource id #%d, 'Hello\n')
#1 {main}
thrown in %s on line %d
Fatal error: Invalid callback foo::filter, class foo does not have a method "filter" in Unknown on line 0

View File

@@ -0,0 +1,28 @@
--TEST--
stream_filter_register() with a class name exist but does not extend php_user_filter and defines a $filtername prop with different type
--FILE--
<?php
class foo {
public array $filtername;
}
var_dump(stream_filter_register("invalid_filter", "foo"));
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
$out = fwrite(STDOUT, "Hello\n");
var_dump($out);
?>
--EXPECTF--
bool(true)
Deprecated: Creation of dynamic property foo::$params is deprecated in %s on line %d
Fatal error: Uncaught TypeError: Cannot assign string to property foo::$filtername of type array in %s:%d
Stack trace:
#0 %s(%d): stream_filter_append(Resource id #2, 'invalid_filter')
#1 {main}
thrown in %s on line %d
Fatal error: Invalid callback foo::filter, class foo does not have a method "filter" in Unknown on line 0

View File

@@ -0,0 +1,28 @@
--TEST--
stream_filter_register() with a class name exist but does not extend php_user_filter and cannot have dynamic properties
--FILE--
<?php
var_dump(stream_filter_register("invalid_filter", "SensitiveParameter"));
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
$out = fwrite(STDOUT, "Hello\n");
var_dump($out);
?>
--EXPECTF--
bool(true)
Fatal error: Uncaught Error: Cannot create dynamic property SensitiveParameter::$filtername in %s:%d
Stack trace:
#0 %s(%d): stream_filter_append(Resource id #%d, 'invalid_filter')
#1 {main}
Next Error: Cannot create dynamic property SensitiveParameter::$params in %s:%d
Stack trace:
#0 %s(%d): stream_filter_append(Resource id #%d, 'invalid_filter')
#1 {main}
thrown in %s on line %d
Fatal error: Invalid callback SensitiveParameter::filter, class SensitiveParameter does not have a method "filter" in Unknown on line 0

View File

@@ -0,0 +1,28 @@
--TEST--
stream_filter_register() with a class name exist but does not extend php_user_filter and defines a private $filtername prop
--FILE--
<?php
class foo {
private $filtername;
}
var_dump(stream_filter_register("invalid_filter", "foo"));
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
$out = fwrite(STDOUT, "Hello\n");
var_dump($out);
?>
--EXPECTF--
bool(true)
Deprecated: Creation of dynamic property foo::$params is deprecated in %s on line %d
Fatal error: Uncaught Error: Cannot access private property foo::$filtername in %s:%d
Stack trace:
#0 %s(%d): stream_filter_append(Resource id #2, 'invalid_filter')
#1 {main}
thrown in %s on line %d
Fatal error: Invalid callback foo::filter, class foo does not have a method "filter" in Unknown on line 0

View File

@@ -0,0 +1,30 @@
--TEST--
stream_filter_register() with a class that has a onclose method that throws
--FILE--
<?php
class foo extends php_user_filter {
public function onclose(): void {
throw new Exception("No");
}
}
var_dump(stream_filter_register("invalid_filter", "foo"));
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
$out = fwrite(STDOUT, "Hello\n");
var_dump($out);
?>
--EXPECTF--
bool(true)
resource(4) of type (stream filter)
Warning: fwrite(): Unprocessed filter buckets remaining on input brigade in %s on line %d
bool(false)
Fatal error: Uncaught Exception: No in %s:%d
Stack trace:
#0 [internal function]: foo->onclose()
#1 {main}
thrown in %s on line %d

View File

@@ -0,0 +1,27 @@
--TEST--
stream_filter_register() with a class that has a oncreate method that throws
--FILE--
<?php
class foo extends php_user_filter {
public function oncreate(): bool {
throw new Exception("No");
}
}
var_dump(stream_filter_register("invalid_filter", "foo"));
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
$out = fwrite(STDOUT, "Hello\n");
var_dump($out);
?>
--EXPECTF--
bool(true)
Fatal error: Uncaught Exception: No in %s:%d
Stack trace:
#0 [internal function]: foo->oncreate()
#1 %s(%d): stream_filter_append(Resource id #2, 'invalid_filter')
#2 {main}
thrown in %s on line %d

View File

@@ -0,0 +1,24 @@
--TEST--
stream_filter_register() with a filter method always returning PSFS_FEED_ME
--FILE--
<?php
class foo extends php_user_filter {
public function filter($in, $out, &$consumed, bool $closing): int {
return PSFS_FEED_ME;
}
}
var_dump(stream_filter_register("invalid_filter", "foo"));
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
$out = fwrite(STDOUT, "Hello\n");
var_dump($out);
?>
--EXPECTF--
bool(true)
resource(4) of type (stream filter)
Warning: fwrite(): Unprocessed filter buckets remaining on input brigade in %s on line %d
int(0)

View File

@@ -0,0 +1,30 @@
--TEST--
stream_filter_register() with a class name exist that mocks php_user_filter with a filter method
--XFAIL--
This leaks memory
--FILE--
<?php
class foo {
public $filtername;
public $params;
public function filter($in, $out, &$consumed, bool $closing): int {
return PSFS_PASS_ON;
}
}
var_dump(stream_filter_register("invalid_filter", "foo"));
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
$out = fwrite(STDOUT, "Hello\n");
var_dump($out);
?>
--EXPECTF--
bool(true)
resource(4) of type (stream filter)
Warning: fwrite(): Unprocessed filter buckets remaining on input brigade in %s on line %d
int(0)

View File

@@ -0,0 +1,32 @@
--TEST--
stream_filter_register() with a class name exist that mocks php_user_filter with a filter method returning not an int
--FILE--
<?php
class foo {
public $filtername;
public $params;
public function filter($in, $out, &$consumed, bool $closing) {
return new stdClass();
}
}
var_dump(stream_filter_register("invalid_filter", "foo"));
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
$out = fwrite(STDOUT, "Hello\n");
var_dump($out);
?>
--EXPECTF--
bool(true)
resource(4) of type (stream filter)
Warning: Object of class stdClass could not be converted to int in %s on line %d
Warning: fwrite(): Unprocessed filter buckets remaining on input brigade in %s on line %d
int(0)
Warning: Object of class stdClass could not be converted to int in Unknown on line 0

View File

@@ -0,0 +1,35 @@
--TEST--
stream_filter_register() with a class name exist that mocks php_user_filter with a private filter method
--FILE--
<?php
class foo {
public $filtername;
public $params;
private function filter($in, $out, &$consumed, bool $closing): int {
return PSFS_PASS_ON;
}
}
var_dump(stream_filter_register("invalid_filter", "foo"));
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
$out = fwrite(STDOUT, "Hello\n");
var_dump($out);
?>
--EXPECTF--
bool(true)
resource(%d) of type (stream filter)
Warning: fwrite(): Unprocessed filter buckets remaining on input brigade in %s on line %d
Fatal error: Uncaught Error: Invalid callback foo::filter, cannot access private method foo::filter() in %s:%d
Stack trace:
#0 %s(%d): fwrite(Resource id #%d, 'Hello\n')
#1 {main}
thrown in %s on line %d
Fatal error: Invalid callback foo::filter, cannot access private method foo::filter() in Unknown on line 0

View File

@@ -0,0 +1,21 @@
--TEST--
stream_filter_register() with a class name that does not exist
--FILE--
<?php
var_dump(stream_filter_register("not_existing_filter", "not_existing"));
stream_filter_append(STDOUT, "not_existing_filter");
$out = fwrite(STDOUT, "Hello\n");
var_dump($out);
?>
--EXPECTF--
bool(true)
Warning: stream_filter_append(): User-filter "not_existing_filter" requires class "not_existing", but that class is not defined in %s on line %d
Warning: stream_filter_append(): Unable to create or locate filter "not_existing_filter" in %s on line %d
Hello
int(6)