1--TEST--
2Test ftruncate() function : usage variations - truncate file to negative size
3--FILE--
4<?php
5// include common file related test functions
6include ("file.inc");
7
8echo "*** Testing ftruncate() : usage variations ***\n";
9
10/* test ftruncate with file opened in different modes */
11$file_modes = array("r", "rb", "rt", "r+", "r+b", "r+t",
12                    "w", "wb", "wt", "w+", "w+b", "w+t",
13                    "x", "xb", "xt", "x+", "x+b", "x+t",
14                    "a", "ab", "at", "a+", "a+b", "a+t");
15
16$file_content_types = array("numeric","text_with_new_line");
17
18foreach($file_content_types as $file_content_type) {
19    echo "\n-- Testing ftruncate() with file having data of type ". $file_content_type ." --\n";
20
21    for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
22        echo "-- Testing ftruncate() with file opening using $file_modes[$mode_counter] mode --\n";
23
24        // create 1 file with some contents
25        $filename = __DIR__."/ftruncate_variation4.tmp";
26        if( strstr($file_modes[$mode_counter], "x") || strstr($file_modes[$mode_counter], "w") ) {
27          // fopen the file using the $file_modes
28          $file_handle = fopen($filename, $file_modes[$mode_counter]);
29          fill_file($file_handle, $file_content_type, 1024);
30        } else {
31          create_files ( __DIR__, 1, $file_content_type, 0755, 1, "w", "ftruncate_variation", 4);
32          // fopen the file using the $file_modes
33          $file_handle = fopen($filename, $file_modes[$mode_counter]);
34        }
35        if (!$file_handle) {
36          echo "Error: failed to open file $filename!\n";
37          exit();
38        }
39
40        rewind($file_handle); // file pointer to 0
41
42        echo "-- Testing ftruncate(): try truncating file to a negative size --\n";
43        /* try to truncate it to a negative size, size should not change*/
44
45        $new_size = -1000;
46        $file_size = filesize($filename); // current filesize
47        var_dump($file_size === 1024 || $file_size === 1137); // 1137 is for Windows with 't' mode
48        var_dump( ftell($file_handle) );
49        try {
50            var_dump( ftruncate($file_handle, $new_size) ); // truncate it
51        } catch (\ValueError $e) {
52            echo $e->getMessage() . \PHP_EOL;
53        }
54        var_dump( ftell($file_handle) );
55        var_dump( feof($file_handle) );
56        fclose($file_handle);
57        clearstatcache(); // clear previous size value in cache
58        $file_size = filesize($filename); // new file size = actual size, no change
59        var_dump($file_size === 1024 || $file_size === 1137); // 1137 is for Windows with 't' mode
60
61        //delete all files created
62        delete_file( $filename );
63    }//end of inner for loop
64}//end of outer foreach loop
65echo "Done\n";
66?>
67--EXPECT--
68*** Testing ftruncate() : usage variations ***
69
70-- Testing ftruncate() with file having data of type numeric --
71-- Testing ftruncate() with file opening using r mode --
72-- Testing ftruncate(): try truncating file to a negative size --
73bool(true)
74int(0)
75ftruncate(): Argument #2 ($size) must be greater than or equal to 0
76int(0)
77bool(false)
78bool(true)
79-- Testing ftruncate() with file opening using rb mode --
80-- Testing ftruncate(): try truncating file to a negative size --
81bool(true)
82int(0)
83ftruncate(): Argument #2 ($size) must be greater than or equal to 0
84int(0)
85bool(false)
86bool(true)
87-- Testing ftruncate() with file opening using rt mode --
88-- Testing ftruncate(): try truncating file to a negative size --
89bool(true)
90int(0)
91ftruncate(): Argument #2 ($size) must be greater than or equal to 0
92int(0)
93bool(false)
94bool(true)
95-- Testing ftruncate() with file opening using r+ mode --
96-- Testing ftruncate(): try truncating file to a negative size --
97bool(true)
98int(0)
99ftruncate(): Argument #2 ($size) must be greater than or equal to 0
100int(0)
101bool(false)
102bool(true)
103-- Testing ftruncate() with file opening using r+b mode --
104-- Testing ftruncate(): try truncating file to a negative size --
105bool(true)
106int(0)
107ftruncate(): Argument #2 ($size) must be greater than or equal to 0
108int(0)
109bool(false)
110bool(true)
111-- Testing ftruncate() with file opening using r+t mode --
112-- Testing ftruncate(): try truncating file to a negative size --
113bool(true)
114int(0)
115ftruncate(): Argument #2 ($size) must be greater than or equal to 0
116int(0)
117bool(false)
118bool(true)
119-- Testing ftruncate() with file opening using w mode --
120-- Testing ftruncate(): try truncating file to a negative size --
121bool(true)
122int(0)
123ftruncate(): Argument #2 ($size) must be greater than or equal to 0
124int(0)
125bool(false)
126bool(true)
127-- Testing ftruncate() with file opening using wb mode --
128-- Testing ftruncate(): try truncating file to a negative size --
129bool(true)
130int(0)
131ftruncate(): Argument #2 ($size) must be greater than or equal to 0
132int(0)
133bool(false)
134bool(true)
135-- Testing ftruncate() with file opening using wt mode --
136-- Testing ftruncate(): try truncating file to a negative size --
137bool(true)
138int(0)
139ftruncate(): Argument #2 ($size) must be greater than or equal to 0
140int(0)
141bool(false)
142bool(true)
143-- Testing ftruncate() with file opening using w+ mode --
144-- Testing ftruncate(): try truncating file to a negative size --
145bool(true)
146int(0)
147ftruncate(): Argument #2 ($size) must be greater than or equal to 0
148int(0)
149bool(false)
150bool(true)
151-- Testing ftruncate() with file opening using w+b mode --
152-- Testing ftruncate(): try truncating file to a negative size --
153bool(true)
154int(0)
155ftruncate(): Argument #2 ($size) must be greater than or equal to 0
156int(0)
157bool(false)
158bool(true)
159-- Testing ftruncate() with file opening using w+t mode --
160-- Testing ftruncate(): try truncating file to a negative size --
161bool(true)
162int(0)
163ftruncate(): Argument #2 ($size) must be greater than or equal to 0
164int(0)
165bool(false)
166bool(true)
167-- Testing ftruncate() with file opening using x mode --
168-- Testing ftruncate(): try truncating file to a negative size --
169bool(true)
170int(0)
171ftruncate(): Argument #2 ($size) must be greater than or equal to 0
172int(0)
173bool(false)
174bool(true)
175-- Testing ftruncate() with file opening using xb mode --
176-- Testing ftruncate(): try truncating file to a negative size --
177bool(true)
178int(0)
179ftruncate(): Argument #2 ($size) must be greater than or equal to 0
180int(0)
181bool(false)
182bool(true)
183-- Testing ftruncate() with file opening using xt mode --
184-- Testing ftruncate(): try truncating file to a negative size --
185bool(true)
186int(0)
187ftruncate(): Argument #2 ($size) must be greater than or equal to 0
188int(0)
189bool(false)
190bool(true)
191-- Testing ftruncate() with file opening using x+ mode --
192-- Testing ftruncate(): try truncating file to a negative size --
193bool(true)
194int(0)
195ftruncate(): Argument #2 ($size) must be greater than or equal to 0
196int(0)
197bool(false)
198bool(true)
199-- Testing ftruncate() with file opening using x+b mode --
200-- Testing ftruncate(): try truncating file to a negative size --
201bool(true)
202int(0)
203ftruncate(): Argument #2 ($size) must be greater than or equal to 0
204int(0)
205bool(false)
206bool(true)
207-- Testing ftruncate() with file opening using x+t mode --
208-- Testing ftruncate(): try truncating file to a negative size --
209bool(true)
210int(0)
211ftruncate(): Argument #2 ($size) must be greater than or equal to 0
212int(0)
213bool(false)
214bool(true)
215-- Testing ftruncate() with file opening using a mode --
216-- Testing ftruncate(): try truncating file to a negative size --
217bool(true)
218int(0)
219ftruncate(): Argument #2 ($size) must be greater than or equal to 0
220int(0)
221bool(false)
222bool(true)
223-- Testing ftruncate() with file opening using ab mode --
224-- Testing ftruncate(): try truncating file to a negative size --
225bool(true)
226int(0)
227ftruncate(): Argument #2 ($size) must be greater than or equal to 0
228int(0)
229bool(false)
230bool(true)
231-- Testing ftruncate() with file opening using at mode --
232-- Testing ftruncate(): try truncating file to a negative size --
233bool(true)
234int(0)
235ftruncate(): Argument #2 ($size) must be greater than or equal to 0
236int(0)
237bool(false)
238bool(true)
239-- Testing ftruncate() with file opening using a+ mode --
240-- Testing ftruncate(): try truncating file to a negative size --
241bool(true)
242int(0)
243ftruncate(): Argument #2 ($size) must be greater than or equal to 0
244int(0)
245bool(false)
246bool(true)
247-- Testing ftruncate() with file opening using a+b mode --
248-- Testing ftruncate(): try truncating file to a negative size --
249bool(true)
250int(0)
251ftruncate(): Argument #2 ($size) must be greater than or equal to 0
252int(0)
253bool(false)
254bool(true)
255-- Testing ftruncate() with file opening using a+t mode --
256-- Testing ftruncate(): try truncating file to a negative size --
257bool(true)
258int(0)
259ftruncate(): Argument #2 ($size) must be greater than or equal to 0
260int(0)
261bool(false)
262bool(true)
263
264-- Testing ftruncate() with file having data of type text_with_new_line --
265-- Testing ftruncate() with file opening using r mode --
266-- Testing ftruncate(): try truncating file to a negative size --
267bool(true)
268int(0)
269ftruncate(): Argument #2 ($size) must be greater than or equal to 0
270int(0)
271bool(false)
272bool(true)
273-- Testing ftruncate() with file opening using rb mode --
274-- Testing ftruncate(): try truncating file to a negative size --
275bool(true)
276int(0)
277ftruncate(): Argument #2 ($size) must be greater than or equal to 0
278int(0)
279bool(false)
280bool(true)
281-- Testing ftruncate() with file opening using rt mode --
282-- Testing ftruncate(): try truncating file to a negative size --
283bool(true)
284int(0)
285ftruncate(): Argument #2 ($size) must be greater than or equal to 0
286int(0)
287bool(false)
288bool(true)
289-- Testing ftruncate() with file opening using r+ mode --
290-- Testing ftruncate(): try truncating file to a negative size --
291bool(true)
292int(0)
293ftruncate(): Argument #2 ($size) must be greater than or equal to 0
294int(0)
295bool(false)
296bool(true)
297-- Testing ftruncate() with file opening using r+b mode --
298-- Testing ftruncate(): try truncating file to a negative size --
299bool(true)
300int(0)
301ftruncate(): Argument #2 ($size) must be greater than or equal to 0
302int(0)
303bool(false)
304bool(true)
305-- Testing ftruncate() with file opening using r+t mode --
306-- Testing ftruncate(): try truncating file to a negative size --
307bool(true)
308int(0)
309ftruncate(): Argument #2 ($size) must be greater than or equal to 0
310int(0)
311bool(false)
312bool(true)
313-- Testing ftruncate() with file opening using w mode --
314-- Testing ftruncate(): try truncating file to a negative size --
315bool(true)
316int(0)
317ftruncate(): Argument #2 ($size) must be greater than or equal to 0
318int(0)
319bool(false)
320bool(true)
321-- Testing ftruncate() with file opening using wb mode --
322-- Testing ftruncate(): try truncating file to a negative size --
323bool(true)
324int(0)
325ftruncate(): Argument #2 ($size) must be greater than or equal to 0
326int(0)
327bool(false)
328bool(true)
329-- Testing ftruncate() with file opening using wt mode --
330-- Testing ftruncate(): try truncating file to a negative size --
331bool(true)
332int(0)
333ftruncate(): Argument #2 ($size) must be greater than or equal to 0
334int(0)
335bool(false)
336bool(true)
337-- Testing ftruncate() with file opening using w+ mode --
338-- Testing ftruncate(): try truncating file to a negative size --
339bool(true)
340int(0)
341ftruncate(): Argument #2 ($size) must be greater than or equal to 0
342int(0)
343bool(false)
344bool(true)
345-- Testing ftruncate() with file opening using w+b mode --
346-- Testing ftruncate(): try truncating file to a negative size --
347bool(true)
348int(0)
349ftruncate(): Argument #2 ($size) must be greater than or equal to 0
350int(0)
351bool(false)
352bool(true)
353-- Testing ftruncate() with file opening using w+t mode --
354-- Testing ftruncate(): try truncating file to a negative size --
355bool(true)
356int(0)
357ftruncate(): Argument #2 ($size) must be greater than or equal to 0
358int(0)
359bool(false)
360bool(true)
361-- Testing ftruncate() with file opening using x mode --
362-- Testing ftruncate(): try truncating file to a negative size --
363bool(true)
364int(0)
365ftruncate(): Argument #2 ($size) must be greater than or equal to 0
366int(0)
367bool(false)
368bool(true)
369-- Testing ftruncate() with file opening using xb mode --
370-- Testing ftruncate(): try truncating file to a negative size --
371bool(true)
372int(0)
373ftruncate(): Argument #2 ($size) must be greater than or equal to 0
374int(0)
375bool(false)
376bool(true)
377-- Testing ftruncate() with file opening using xt mode --
378-- Testing ftruncate(): try truncating file to a negative size --
379bool(true)
380int(0)
381ftruncate(): Argument #2 ($size) must be greater than or equal to 0
382int(0)
383bool(false)
384bool(true)
385-- Testing ftruncate() with file opening using x+ mode --
386-- Testing ftruncate(): try truncating file to a negative size --
387bool(true)
388int(0)
389ftruncate(): Argument #2 ($size) must be greater than or equal to 0
390int(0)
391bool(false)
392bool(true)
393-- Testing ftruncate() with file opening using x+b mode --
394-- Testing ftruncate(): try truncating file to a negative size --
395bool(true)
396int(0)
397ftruncate(): Argument #2 ($size) must be greater than or equal to 0
398int(0)
399bool(false)
400bool(true)
401-- Testing ftruncate() with file opening using x+t mode --
402-- Testing ftruncate(): try truncating file to a negative size --
403bool(true)
404int(0)
405ftruncate(): Argument #2 ($size) must be greater than or equal to 0
406int(0)
407bool(false)
408bool(true)
409-- Testing ftruncate() with file opening using a mode --
410-- Testing ftruncate(): try truncating file to a negative size --
411bool(true)
412int(0)
413ftruncate(): Argument #2 ($size) must be greater than or equal to 0
414int(0)
415bool(false)
416bool(true)
417-- Testing ftruncate() with file opening using ab mode --
418-- Testing ftruncate(): try truncating file to a negative size --
419bool(true)
420int(0)
421ftruncate(): Argument #2 ($size) must be greater than or equal to 0
422int(0)
423bool(false)
424bool(true)
425-- Testing ftruncate() with file opening using at mode --
426-- Testing ftruncate(): try truncating file to a negative size --
427bool(true)
428int(0)
429ftruncate(): Argument #2 ($size) must be greater than or equal to 0
430int(0)
431bool(false)
432bool(true)
433-- Testing ftruncate() with file opening using a+ mode --
434-- Testing ftruncate(): try truncating file to a negative size --
435bool(true)
436int(0)
437ftruncate(): Argument #2 ($size) must be greater than or equal to 0
438int(0)
439bool(false)
440bool(true)
441-- Testing ftruncate() with file opening using a+b mode --
442-- Testing ftruncate(): try truncating file to a negative size --
443bool(true)
444int(0)
445ftruncate(): Argument #2 ($size) must be greater than or equal to 0
446int(0)
447bool(false)
448bool(true)
449-- Testing ftruncate() with file opening using a+t mode --
450-- Testing ftruncate(): try truncating file to a negative size --
451bool(true)
452int(0)
453ftruncate(): Argument #2 ($size) must be greater than or equal to 0
454int(0)
455bool(false)
456bool(true)
457Done
458