Skip to content

Commit 9c33091

Browse files
committed
ext/standard: add a bunch of whacky stream filter tests
1 parent c6d8ccb commit 9c33091

12 files changed

+348
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
stream_filter_register() with a class that coerces the $consumed parameter of filter method
3+
--XFAIL--
4+
This leaks memory
5+
--FILE--
6+
<?php
7+
8+
class foo extends php_user_filter {
9+
public function filter($in, $out, &$consumed, bool $closing): int {
10+
$consumed = new stdClass();
11+
return PSFS_PASS_ON;
12+
}
13+
}
14+
15+
var_dump(stream_filter_register("invalid_filter", "foo"));
16+
17+
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
18+
19+
$out = fwrite(STDOUT, "Hello\n");
20+
var_dump($out);
21+
22+
?>
23+
--EXPECTF--
24+
bool(true)
25+
resource(4) of type (stream filter)
26+
27+
Warning: Object of class stdClass could not be converted to int in %s on line %d
28+
29+
Warning: fwrite(): Unprocessed filter buckets remaining on input brigade in %s on line %d
30+
int(1)
31+
32+
Warning: Object of class stdClass could not be converted to int in Unknown on line 0
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
stream_filter_register() with a class name that exist but does not extend php_user_filter nor mock anything
3+
--FILE--
4+
<?php
5+
class foo {
6+
7+
}
8+
9+
var_dump(stream_filter_register("invalid_filter", "foo"));
10+
11+
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
12+
13+
$out = fwrite(STDOUT, "Hello\n");
14+
var_dump($out);
15+
16+
?>
17+
--EXPECTF--
18+
bool(true)
19+
20+
Deprecated: Creation of dynamic property foo::$filtername is deprecated in %s on line %d
21+
22+
Deprecated: Creation of dynamic property foo::$params is deprecated in %s on line %d
23+
resource(%d) of type (stream filter)
24+
25+
Warning: fwrite(): Unprocessed filter buckets remaining on input brigade in %s on line %d
26+
27+
Fatal error: Uncaught Error: Invalid callback foo::filter, class foo does not have a method "filter" in %s:%d
28+
Stack trace:
29+
#0 %s(%d): fwrite(Resource id #%d, 'Hello\n')
30+
#1 {main}
31+
thrown in %s on line %d
32+
33+
Fatal error: Invalid callback foo::filter, class foo does not have a method "filter" in Unknown on line 0
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
stream_filter_register() with a class name exist but does not extend php_user_filter and defines a $filtername prop with different type
3+
--FILE--
4+
<?php
5+
class foo {
6+
public array $filtername;
7+
}
8+
9+
var_dump(stream_filter_register("invalid_filter", "foo"));
10+
11+
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
12+
13+
$out = fwrite(STDOUT, "Hello\n");
14+
var_dump($out);
15+
16+
?>
17+
--EXPECTF--
18+
bool(true)
19+
20+
Deprecated: Creation of dynamic property foo::$params is deprecated in %s on line %d
21+
22+
Fatal error: Uncaught TypeError: Cannot assign string to property foo::$filtername of type array in %s:%d
23+
Stack trace:
24+
#0 %s(%d): stream_filter_append(Resource id #2, 'invalid_filter')
25+
#1 {main}
26+
thrown in %s on line %d
27+
28+
Fatal error: Invalid callback foo::filter, class foo does not have a method "filter" in Unknown on line 0
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
stream_filter_register() with a class name exist but does not extend php_user_filter and cannot have dynamic properties
3+
--FILE--
4+
<?php
5+
6+
var_dump(stream_filter_register("invalid_filter", "SensitiveParameter"));
7+
8+
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
9+
10+
$out = fwrite(STDOUT, "Hello\n");
11+
var_dump($out);
12+
13+
?>
14+
--EXPECTF--
15+
bool(true)
16+
17+
Fatal error: Uncaught Error: Cannot create dynamic property SensitiveParameter::$filtername in %s:%d
18+
Stack trace:
19+
#0 %s(%d): stream_filter_append(Resource id #%d, 'invalid_filter')
20+
#1 {main}
21+
22+
Next Error: Cannot create dynamic property SensitiveParameter::$params in %s:%d
23+
Stack trace:
24+
#0 %s(%d): stream_filter_append(Resource id #%d, 'invalid_filter')
25+
#1 {main}
26+
thrown in %s on line %d
27+
28+
Fatal error: Invalid callback SensitiveParameter::filter, class SensitiveParameter does not have a method "filter" in Unknown on line 0
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
stream_filter_register() with a class name exist but does not extend php_user_filter and defines a private $filtername prop
3+
--FILE--
4+
<?php
5+
class foo {
6+
private $filtername;
7+
}
8+
9+
var_dump(stream_filter_register("invalid_filter", "foo"));
10+
11+
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
12+
13+
$out = fwrite(STDOUT, "Hello\n");
14+
var_dump($out);
15+
16+
?>
17+
--EXPECTF--
18+
bool(true)
19+
20+
Deprecated: Creation of dynamic property foo::$params is deprecated in %s on line %d
21+
22+
Fatal error: Uncaught Error: Cannot access private property foo::$filtername in %s:%d
23+
Stack trace:
24+
#0 %s(%d): stream_filter_append(Resource id #2, 'invalid_filter')
25+
#1 {main}
26+
thrown in %s on line %d
27+
28+
Fatal error: Invalid callback foo::filter, class foo does not have a method "filter" in Unknown on line 0
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
stream_filter_register() with a class that has a onclose method that throws
3+
--FILE--
4+
<?php
5+
class foo extends php_user_filter {
6+
public function onclose(): void {
7+
throw new Exception("No");
8+
}
9+
}
10+
11+
var_dump(stream_filter_register("invalid_filter", "foo"));
12+
13+
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
14+
15+
$out = fwrite(STDOUT, "Hello\n");
16+
var_dump($out);
17+
18+
?>
19+
--EXPECTF--
20+
bool(true)
21+
resource(4) of type (stream filter)
22+
23+
Warning: fwrite(): Unprocessed filter buckets remaining on input brigade in %s on line %d
24+
bool(false)
25+
26+
Fatal error: Uncaught Exception: No in %s:%d
27+
Stack trace:
28+
#0 [internal function]: foo->onclose()
29+
#1 {main}
30+
thrown in %s on line %d
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
stream_filter_register() with a class that has a oncreate method that throws
3+
--FILE--
4+
<?php
5+
class foo extends php_user_filter {
6+
public function oncreate(): bool {
7+
throw new Exception("No");
8+
}
9+
}
10+
11+
var_dump(stream_filter_register("invalid_filter", "foo"));
12+
13+
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
14+
15+
$out = fwrite(STDOUT, "Hello\n");
16+
var_dump($out);
17+
18+
?>
19+
--EXPECTF--
20+
bool(true)
21+
22+
Fatal error: Uncaught Exception: No in %s:%d
23+
Stack trace:
24+
#0 [internal function]: foo->oncreate()
25+
#1 %s(%d): stream_filter_append(Resource id #2, 'invalid_filter')
26+
#2 {main}
27+
thrown in %s on line %d
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
stream_filter_register() with a filter method always returning PSFS_FEED_ME
3+
--FILE--
4+
<?php
5+
class foo extends php_user_filter {
6+
public function filter($in, $out, &$consumed, bool $closing): int {
7+
return PSFS_FEED_ME;
8+
}
9+
}
10+
11+
var_dump(stream_filter_register("invalid_filter", "foo"));
12+
13+
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
14+
15+
$out = fwrite(STDOUT, "Hello\n");
16+
var_dump($out);
17+
18+
?>
19+
--EXPECTF--
20+
bool(true)
21+
resource(4) of type (stream filter)
22+
23+
Warning: fwrite(): Unprocessed filter buckets remaining on input brigade in %s on line %d
24+
int(0)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
stream_filter_register() with a class name exist that mocks php_user_filter with a filter method
3+
--XFAIL--
4+
This leaks memory
5+
--FILE--
6+
<?php
7+
8+
class foo {
9+
public $filtername;
10+
public $params;
11+
12+
public function filter($in, $out, &$consumed, bool $closing): int {
13+
return PSFS_PASS_ON;
14+
}
15+
}
16+
17+
var_dump(stream_filter_register("invalid_filter", "foo"));
18+
19+
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
20+
21+
$out = fwrite(STDOUT, "Hello\n");
22+
var_dump($out);
23+
24+
?>
25+
--EXPECTF--
26+
bool(true)
27+
resource(4) of type (stream filter)
28+
29+
Warning: fwrite(): Unprocessed filter buckets remaining on input brigade in %s on line %d
30+
int(0)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
stream_filter_register() with a class name exist that mocks php_user_filter with a filter method returning not an int
3+
--FILE--
4+
<?php
5+
6+
class foo {
7+
public $filtername;
8+
public $params;
9+
10+
public function filter($in, $out, &$consumed, bool $closing) {
11+
return new stdClass();
12+
}
13+
}
14+
15+
var_dump(stream_filter_register("invalid_filter", "foo"));
16+
17+
var_dump(stream_filter_append(STDOUT, "invalid_filter"));
18+
19+
$out = fwrite(STDOUT, "Hello\n");
20+
var_dump($out);
21+
22+
?>
23+
--EXPECTF--
24+
bool(true)
25+
resource(4) of type (stream filter)
26+
27+
Warning: Object of class stdClass could not be converted to int in %s on line %d
28+
29+
Warning: fwrite(): Unprocessed filter buckets remaining on input brigade in %s on line %d
30+
int(0)
31+
32+
Warning: Object of class stdClass could not be converted to int in Unknown on line 0

0 commit comments

Comments
 (0)