1--TEST--
2User-space streams: stream_truncate()
3--FILE--
4<?php
5class test_wrapper_base {
6    public $mode;
7    function stream_open($path, $mode, $openedpath) {
8        return true;
9    }
10    function stream_eof() {
11        return false;
12    }
13}
14class test_wrapper extends test_wrapper_base {
15    function stream_truncate($new_size) {
16        echo "truncation with new_size=$new_size\n";
17        return true;
18    }
19}
20class test_wrapper_bad extends test_wrapper_base {
21    function stream_truncate($new_size) {
22        echo "truncation with new_size=$new_size\n";
23        return "kkk";
24    }
25}
26function test($name, $fd, $dest_size) {
27    echo "------ $name: -------\n";
28    var_dump(ftruncate($fd, $dest_size));
29}
30var_dump(stream_wrapper_register('test', 'test_wrapper'));
31var_dump(stream_wrapper_register('test2', 'test_wrapper_base'));
32var_dump(stream_wrapper_register('test3', 'test_wrapper_bad'));
33
34$fd = fopen("test://foo","r");
35$fd2 = fopen("test2://foo","r");
36$fd3 = fopen("test3://foo","r");
37
38test("stream_truncate not implemented", $fd2, 0);
39test("stream_truncate size 0", $fd, 0);
40test("stream_truncate size 10", $fd, 10);
41try {
42    test("stream_truncate negative size", $fd, -1);
43} catch (\ValueError $e) {
44    echo $e->getMessage() . \PHP_EOL;
45}
46test("stream_truncate bad return", $fd3, 0);
47?>
48--EXPECTF--
49bool(true)
50bool(true)
51bool(true)
52------ stream_truncate not implemented: -------
53
54Warning: ftruncate(): Can't truncate this stream! in %s on line %d
55bool(false)
56------ stream_truncate size 0: -------
57truncation with new_size=0
58bool(true)
59------ stream_truncate size 10: -------
60truncation with new_size=10
61bool(true)
62------ stream_truncate negative size: -------
63ftruncate(): Argument #2 ($size) must be greater than or equal to 0
64------ stream_truncate bad return: -------
65truncation with new_size=0
66
67Warning: ftruncate(): test_wrapper_bad::stream_truncate did not return a boolean! in %s on line %d
68bool(false)
69