ftruncate_variation4.phpt 14 KB

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