fread_variation3-win32-mb.phpt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. --TEST--
  2. Test fread() function : usage variations - read beyond file size, read/write mode
  3. --SKIPIF--
  4. <?php
  5. if (substr(PHP_OS, 0, 3) != 'WIN') {
  6. die('skip...only valid for Windows');
  7. }
  8. ?>
  9. --FILE--
  10. <?php
  11. // include the file.inc for common functions for test
  12. include ("file.inc");
  13. /* Function : function check_read(resource $file_handle, int $read_size, int $expect_size)
  14. Description : Read data from file of size $read_size and verifies that $expected_size no. of
  15. bytes are read.
  16. $file_handle : File Handle
  17. $read_size : No. of bytes to be read.
  18. $expect_size : Expected data length
  19. Returns: returns the data read
  20. */
  21. function check_read($file_handle, $read_size, $expect_size) {
  22. // print file pointer position before read
  23. var_dump( ftell($file_handle) );
  24. var_dump( feof($file_handle) );
  25. // read the data of size $read_size
  26. echo "Reading $read_size bytes from file, expecting $expect_size bytes ... ";
  27. $data_from_file = fread($file_handle, $read_size);
  28. // check if data read is of expected size
  29. if ( strlen($data_from_file) == $expect_size)
  30. echo "OK\n";
  31. else
  32. echo "Error reading file, total number of bytes read = ".strlen($data_from_file)."\n";
  33. // file pointer position after read
  34. var_dump( ftell($file_handle) );
  35. // check if file pointer at eof()
  36. var_dump( feof($file_handle) );
  37. return $data_from_file;
  38. }
  39. echo "*** Testing fread() : usage variations ***\n";
  40. $file_modes = array("a+","a+b","a+t",
  41. "w+","w+b","w+t",
  42. "x+","x+b","x+t");
  43. $file_content_types = array("numeric","text","text_with_new_line");
  44. foreach($file_content_types as $file_content_type) {
  45. echo "\n-- Testing fread() with file having content of type ". $file_content_type ." --\n";
  46. /* open the file using $files_modes and perform fread() on it */
  47. foreach($file_modes as $file_mode) {
  48. if(!strstr($file_mode,"x")){
  49. /* create files with $file_content_type */
  50. create_files ( __DIR__, 1, $file_content_type, 0755, 1, "w", "私はガラスを食べられますfread_variation", 3);
  51. }
  52. $filename = __DIR__."/私はガラスを食べられますfread_variation3.tmp"; // this is name of the file created by create_files()
  53. echo "-- File opened in mode ".$file_mode." --\n";
  54. $file_handle = fopen($filename, $file_mode);
  55. if (!$file_handle) {
  56. echo "Error: failed to fopen() file: $filename!";
  57. exit();
  58. }
  59. if(strstr($file_mode,"w") || strstr($file_mode,"x") ) {
  60. $data_to_be_written="";
  61. fill_file($file_handle, $file_content_type, 1024);
  62. }
  63. rewind($file_handle);
  64. // read file by giving size more than its size
  65. echo "-- Reading beyond filesize, expected : 1024 bytes --\n";
  66. rewind($file_handle);
  67. $data_from_file = check_read($file_handle, 1030, ( strstr($file_mode, "+") ? 1024 : 1024) );
  68. if ( $data_from_file != false)
  69. var_dump( md5($data_from_file) );
  70. rewind($file_handle);
  71. echo "-- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --\n";
  72. // try fread when file pointer at end
  73. fseek($file_handle, 0, SEEK_END);
  74. //reading file when file pointer at end
  75. $data_from_file = check_read($file_handle, 10, 0);
  76. if ( $data_from_file != false)
  77. var_dump( md5($data_from_file) );
  78. // now close the file
  79. fclose($file_handle);
  80. // delete the file created
  81. delete_file($filename); // delete file
  82. } // end of inner foreach loop
  83. }// end of outer foreach loop
  84. echo"Done\n";
  85. ?>
  86. --EXPECT--
  87. *** Testing fread() : usage variations ***
  88. -- Testing fread() with file having content of type numeric --
  89. -- File opened in mode a+ --
  90. -- Reading beyond filesize, expected : 1024 bytes --
  91. int(0)
  92. bool(false)
  93. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  94. int(1024)
  95. bool(true)
  96. string(32) "950b7457d1deb6332f2fc5d42f3129d6"
  97. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  98. int(1024)
  99. bool(false)
  100. Reading 10 bytes from file, expecting 0 bytes ... OK
  101. int(1024)
  102. bool(true)
  103. -- File opened in mode a+b --
  104. -- Reading beyond filesize, expected : 1024 bytes --
  105. int(0)
  106. bool(false)
  107. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  108. int(1024)
  109. bool(true)
  110. string(32) "950b7457d1deb6332f2fc5d42f3129d6"
  111. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  112. int(1024)
  113. bool(false)
  114. Reading 10 bytes from file, expecting 0 bytes ... OK
  115. int(1024)
  116. bool(true)
  117. -- File opened in mode a+t --
  118. -- Reading beyond filesize, expected : 1024 bytes --
  119. int(0)
  120. bool(false)
  121. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  122. int(1024)
  123. bool(true)
  124. string(32) "950b7457d1deb6332f2fc5d42f3129d6"
  125. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  126. int(1024)
  127. bool(false)
  128. Reading 10 bytes from file, expecting 0 bytes ... OK
  129. int(1024)
  130. bool(true)
  131. -- File opened in mode w+ --
  132. -- Reading beyond filesize, expected : 1024 bytes --
  133. int(0)
  134. bool(false)
  135. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  136. int(1024)
  137. bool(true)
  138. string(32) "950b7457d1deb6332f2fc5d42f3129d6"
  139. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  140. int(1024)
  141. bool(false)
  142. Reading 10 bytes from file, expecting 0 bytes ... OK
  143. int(1024)
  144. bool(true)
  145. -- File opened in mode w+b --
  146. -- Reading beyond filesize, expected : 1024 bytes --
  147. int(0)
  148. bool(false)
  149. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  150. int(1024)
  151. bool(true)
  152. string(32) "950b7457d1deb6332f2fc5d42f3129d6"
  153. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  154. int(1024)
  155. bool(false)
  156. Reading 10 bytes from file, expecting 0 bytes ... OK
  157. int(1024)
  158. bool(true)
  159. -- File opened in mode w+t --
  160. -- Reading beyond filesize, expected : 1024 bytes --
  161. int(0)
  162. bool(false)
  163. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  164. int(1024)
  165. bool(true)
  166. string(32) "950b7457d1deb6332f2fc5d42f3129d6"
  167. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  168. int(1024)
  169. bool(false)
  170. Reading 10 bytes from file, expecting 0 bytes ... OK
  171. int(1024)
  172. bool(true)
  173. -- File opened in mode x+ --
  174. -- Reading beyond filesize, expected : 1024 bytes --
  175. int(0)
  176. bool(false)
  177. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  178. int(1024)
  179. bool(true)
  180. string(32) "950b7457d1deb6332f2fc5d42f3129d6"
  181. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  182. int(1024)
  183. bool(false)
  184. Reading 10 bytes from file, expecting 0 bytes ... OK
  185. int(1024)
  186. bool(true)
  187. -- File opened in mode x+b --
  188. -- Reading beyond filesize, expected : 1024 bytes --
  189. int(0)
  190. bool(false)
  191. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  192. int(1024)
  193. bool(true)
  194. string(32) "950b7457d1deb6332f2fc5d42f3129d6"
  195. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  196. int(1024)
  197. bool(false)
  198. Reading 10 bytes from file, expecting 0 bytes ... OK
  199. int(1024)
  200. bool(true)
  201. -- File opened in mode x+t --
  202. -- Reading beyond filesize, expected : 1024 bytes --
  203. int(0)
  204. bool(false)
  205. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  206. int(1024)
  207. bool(true)
  208. string(32) "950b7457d1deb6332f2fc5d42f3129d6"
  209. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  210. int(1024)
  211. bool(false)
  212. Reading 10 bytes from file, expecting 0 bytes ... OK
  213. int(1024)
  214. bool(true)
  215. -- Testing fread() with file having content of type text --
  216. -- File opened in mode a+ --
  217. -- Reading beyond filesize, expected : 1024 bytes --
  218. int(0)
  219. bool(false)
  220. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  221. int(1024)
  222. bool(true)
  223. string(32) "e486000c4c8452774f746a27658d87fa"
  224. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  225. int(1024)
  226. bool(false)
  227. Reading 10 bytes from file, expecting 0 bytes ... OK
  228. int(1024)
  229. bool(true)
  230. -- File opened in mode a+b --
  231. -- Reading beyond filesize, expected : 1024 bytes --
  232. int(0)
  233. bool(false)
  234. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  235. int(1024)
  236. bool(true)
  237. string(32) "e486000c4c8452774f746a27658d87fa"
  238. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  239. int(1024)
  240. bool(false)
  241. Reading 10 bytes from file, expecting 0 bytes ... OK
  242. int(1024)
  243. bool(true)
  244. -- File opened in mode a+t --
  245. -- Reading beyond filesize, expected : 1024 bytes --
  246. int(0)
  247. bool(false)
  248. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  249. int(1024)
  250. bool(true)
  251. string(32) "e486000c4c8452774f746a27658d87fa"
  252. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  253. int(1024)
  254. bool(false)
  255. Reading 10 bytes from file, expecting 0 bytes ... OK
  256. int(1024)
  257. bool(true)
  258. -- File opened in mode w+ --
  259. -- Reading beyond filesize, expected : 1024 bytes --
  260. int(0)
  261. bool(false)
  262. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  263. int(1024)
  264. bool(true)
  265. string(32) "e486000c4c8452774f746a27658d87fa"
  266. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  267. int(1024)
  268. bool(false)
  269. Reading 10 bytes from file, expecting 0 bytes ... OK
  270. int(1024)
  271. bool(true)
  272. -- File opened in mode w+b --
  273. -- Reading beyond filesize, expected : 1024 bytes --
  274. int(0)
  275. bool(false)
  276. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  277. int(1024)
  278. bool(true)
  279. string(32) "e486000c4c8452774f746a27658d87fa"
  280. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  281. int(1024)
  282. bool(false)
  283. Reading 10 bytes from file, expecting 0 bytes ... OK
  284. int(1024)
  285. bool(true)
  286. -- File opened in mode w+t --
  287. -- Reading beyond filesize, expected : 1024 bytes --
  288. int(0)
  289. bool(false)
  290. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  291. int(1024)
  292. bool(true)
  293. string(32) "e486000c4c8452774f746a27658d87fa"
  294. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  295. int(1024)
  296. bool(false)
  297. Reading 10 bytes from file, expecting 0 bytes ... OK
  298. int(1024)
  299. bool(true)
  300. -- File opened in mode x+ --
  301. -- Reading beyond filesize, expected : 1024 bytes --
  302. int(0)
  303. bool(false)
  304. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  305. int(1024)
  306. bool(true)
  307. string(32) "e486000c4c8452774f746a27658d87fa"
  308. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  309. int(1024)
  310. bool(false)
  311. Reading 10 bytes from file, expecting 0 bytes ... OK
  312. int(1024)
  313. bool(true)
  314. -- File opened in mode x+b --
  315. -- Reading beyond filesize, expected : 1024 bytes --
  316. int(0)
  317. bool(false)
  318. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  319. int(1024)
  320. bool(true)
  321. string(32) "e486000c4c8452774f746a27658d87fa"
  322. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  323. int(1024)
  324. bool(false)
  325. Reading 10 bytes from file, expecting 0 bytes ... OK
  326. int(1024)
  327. bool(true)
  328. -- File opened in mode x+t --
  329. -- Reading beyond filesize, expected : 1024 bytes --
  330. int(0)
  331. bool(false)
  332. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  333. int(1024)
  334. bool(true)
  335. string(32) "e486000c4c8452774f746a27658d87fa"
  336. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  337. int(1024)
  338. bool(false)
  339. Reading 10 bytes from file, expecting 0 bytes ... OK
  340. int(1024)
  341. bool(true)
  342. -- Testing fread() with file having content of type text_with_new_line --
  343. -- File opened in mode a+ --
  344. -- Reading beyond filesize, expected : 1024 bytes --
  345. int(0)
  346. bool(false)
  347. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  348. int(1024)
  349. bool(true)
  350. string(32) "b09c8026a64a88d36d4c2f17983964bb"
  351. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  352. int(1024)
  353. bool(false)
  354. Reading 10 bytes from file, expecting 0 bytes ... OK
  355. int(1024)
  356. bool(true)
  357. -- File opened in mode a+b --
  358. -- Reading beyond filesize, expected : 1024 bytes --
  359. int(0)
  360. bool(false)
  361. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  362. int(1024)
  363. bool(true)
  364. string(32) "b09c8026a64a88d36d4c2f17983964bb"
  365. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  366. int(1024)
  367. bool(false)
  368. Reading 10 bytes from file, expecting 0 bytes ... OK
  369. int(1024)
  370. bool(true)
  371. -- File opened in mode a+t --
  372. -- Reading beyond filesize, expected : 1024 bytes --
  373. int(0)
  374. bool(false)
  375. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  376. int(1024)
  377. bool(true)
  378. string(32) "b09c8026a64a88d36d4c2f17983964bb"
  379. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  380. int(1024)
  381. bool(false)
  382. Reading 10 bytes from file, expecting 0 bytes ... OK
  383. int(1024)
  384. bool(true)
  385. -- File opened in mode w+ --
  386. -- Reading beyond filesize, expected : 1024 bytes --
  387. int(0)
  388. bool(false)
  389. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  390. int(1024)
  391. bool(true)
  392. string(32) "b09c8026a64a88d36d4c2f17983964bb"
  393. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  394. int(1024)
  395. bool(false)
  396. Reading 10 bytes from file, expecting 0 bytes ... OK
  397. int(1024)
  398. bool(true)
  399. -- File opened in mode w+b --
  400. -- Reading beyond filesize, expected : 1024 bytes --
  401. int(0)
  402. bool(false)
  403. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  404. int(1024)
  405. bool(true)
  406. string(32) "b09c8026a64a88d36d4c2f17983964bb"
  407. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  408. int(1024)
  409. bool(false)
  410. Reading 10 bytes from file, expecting 0 bytes ... OK
  411. int(1024)
  412. bool(true)
  413. -- File opened in mode w+t --
  414. -- Reading beyond filesize, expected : 1024 bytes --
  415. int(0)
  416. bool(false)
  417. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  418. int(1024)
  419. bool(true)
  420. string(32) "b09c8026a64a88d36d4c2f17983964bb"
  421. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  422. int(1137)
  423. bool(false)
  424. Reading 10 bytes from file, expecting 0 bytes ... OK
  425. int(1137)
  426. bool(true)
  427. -- File opened in mode x+ --
  428. -- Reading beyond filesize, expected : 1024 bytes --
  429. int(0)
  430. bool(false)
  431. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  432. int(1024)
  433. bool(true)
  434. string(32) "b09c8026a64a88d36d4c2f17983964bb"
  435. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  436. int(1024)
  437. bool(false)
  438. Reading 10 bytes from file, expecting 0 bytes ... OK
  439. int(1024)
  440. bool(true)
  441. -- File opened in mode x+b --
  442. -- Reading beyond filesize, expected : 1024 bytes --
  443. int(0)
  444. bool(false)
  445. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  446. int(1024)
  447. bool(true)
  448. string(32) "b09c8026a64a88d36d4c2f17983964bb"
  449. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  450. int(1024)
  451. bool(false)
  452. Reading 10 bytes from file, expecting 0 bytes ... OK
  453. int(1024)
  454. bool(true)
  455. -- File opened in mode x+t --
  456. -- Reading beyond filesize, expected : 1024 bytes --
  457. int(0)
  458. bool(false)
  459. Reading 1030 bytes from file, expecting 1024 bytes ... OK
  460. int(1024)
  461. bool(true)
  462. string(32) "b09c8026a64a88d36d4c2f17983964bb"
  463. -- Reading beyond filesize when file pointer pointing to EOF, expected : 0 bytes --
  464. int(1137)
  465. bool(false)
  466. Reading 10 bytes from file, expecting 0 bytes ... OK
  467. int(1137)
  468. bool(true)
  469. Done