PageRenderTime 53ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/zend/bad/ext/standard/tests/file/userstreams.php

https://gitlab.com/iranjith4/hhvm
PHP | 314 lines | 253 code | 44 blank | 17 comment | 40 complexity | e76d62797ae1bd6f23376494152765d0 MD5 | raw file
  1. <?php
  2. # vim600:syn=php:
  3. /* This is a fairly aggressive test that looks at
  4. * user streams and also gives the seek/gets/buffer
  5. * layer of streams a thorough testing */
  6. $lyrics = <<<EOD
  7. ...and the road becomes my bride
  8. I have stripped of all but pride
  9. so in her I do confide
  10. and she keeps me satisfied
  11. gives me all I need
  12. ...and with dust in throat I crave
  13. to the game you stay a slave
  14. rover wanderer
  15. nomad vagabond
  16. call me what you will
  17. But Ill take my time anywhere
  18. Free to speak my mind anywhere
  19. and Ill redefine anywhere
  20. Anywhere I roam
  21. Where I lay my head is home
  22. ...and the earth becomes my throne
  23. I adapt to the unknown
  24. under wandering stars Ive grown
  25. by myself but not alone
  26. I ask no one
  27. ...and my ties are severed clean
  28. the less I have the more I gain
  29. off the beaten path I reign
  30. rover wanderer
  31. nomad vagabond
  32. call me what you will
  33. But Ill take my time anywhere
  34. Free to speak my mind anywhere
  35. and Ill never mind anywhere
  36. Anywhere I roam
  37. Where I lay my head is home
  38. But Ill take my time anywhere
  39. Free to speak my mind anywhere
  40. and Ill take my find anywhere
  41. Anywhere I roam
  42. Where I lay my head is home
  43. carved upon my stone
  44. my body lie but still I roam
  45. Wherever I may roam.
  46. Wherever I May Roam
  47. EOD;
  48. /* repeat the data a few times so that it grows larger than
  49. * the default cache chunk size and that we have something
  50. * to seek around... */
  51. $DATA = "";
  52. for ($i = 0; $i < 30; $i++) {
  53. if ($i % 2 == 0)
  54. $DATA .= str_rot13($lyrics);
  55. else
  56. $DATA .= $lyrics;
  57. }
  58. /* store the data in a regular file so that we can compare
  59. * the results */
  60. $tf = tmpfile();
  61. fwrite($tf, (binary)$DATA);
  62. $n = ftell($tf);
  63. rewind($tf) or die("failed to rewind tmp file!");
  64. if (ftell($tf) != 0)
  65. die("tmpfile is not at start!");
  66. $DATALEN = strlen($DATA);
  67. if ($n != $DATALEN)
  68. die("tmpfile stored $n bytes; should be $DATALEN!");
  69. class uselessstream
  70. {
  71. }
  72. class mystream
  73. {
  74. public $path;
  75. public $mode;
  76. public $options;
  77. public $position;
  78. public $varname;
  79. function stream_open($path, $mode, $options, &$opened_path)
  80. {
  81. $this->path = $path;
  82. $this->mode = $mode;
  83. $this->options = $options;
  84. $split = parse_url($path);
  85. $this->varname = $split["host"];
  86. if (strchr($mode, 'a'))
  87. $this->position = strlen($GLOBALS[$this->varname]);
  88. else
  89. $this->position = 0;
  90. return true;
  91. }
  92. function stream_read($count)
  93. {
  94. $ret = substr($GLOBALS[$this->varname], $this->position, $count);
  95. $this->position += strlen($ret);
  96. return $ret;
  97. }
  98. function stream_tell()
  99. {
  100. return $this->position;
  101. }
  102. function stream_eof()
  103. {
  104. return $this->position >= strlen($GLOBALS[$this->varname]);
  105. }
  106. function stream_seek($offset, $whence)
  107. {
  108. switch($whence) {
  109. case SEEK_SET:
  110. if ($offset < strlen($GLOBALS[$this->varname]) && $offset >= 0) {
  111. $this->position = $offset;
  112. return true;
  113. } else {
  114. return false;
  115. }
  116. break;
  117. case SEEK_CUR:
  118. if ($offset >= 0) {
  119. $this->position += $offset;
  120. return true;
  121. } else {
  122. return false;
  123. }
  124. break;
  125. case SEEK_END:
  126. if (strlen($GLOBALS[$this->varname]) + $offset >= 0) {
  127. $this->position = strlen($GLOBALS[$this->varname]) + $offset;
  128. return true;
  129. } else {
  130. return false;
  131. }
  132. break;
  133. default:
  134. return false;
  135. }
  136. }
  137. }
  138. if (@stream_wrapper_register("bogus", "class_not_exist")) {
  139. die("Registered a non-existent class!!!???");
  140. }
  141. echo "Not Registered\n";
  142. if (!stream_wrapper_register("test", "mystream")) {
  143. die("test wrapper registration failed");
  144. }
  145. echo "Registered\n";
  146. if (!stream_wrapper_register("bogon", "uselessstream")) {
  147. die("bogon wrapper registration failed");
  148. }
  149. echo "Registered\n";
  150. $b = @fopen("bogon://url", "rb");
  151. if (is_resource($b)) {
  152. die("Opened a bogon??");
  153. }
  154. $fp = fopen("test://DATA", "rb");
  155. if (!$fp || !is_resource($fp)) {
  156. die("Failed to open resource");
  157. }
  158. /* some default seeks that will cause buffer/cache misses */
  159. $seeks = array(
  160. array(SEEK_SET, 0, 0),
  161. array(SEEK_CUR, 8450, 8450),
  162. array(SEEK_CUR, -7904, 546),
  163. array(SEEK_CUR, 12456, 13002),
  164. /* end up at BOF so that randomly generated seek offsets
  165. * below will know where they are supposed to be */
  166. array(SEEK_SET, 0, 0)
  167. );
  168. $whence_map = array(
  169. SEEK_CUR,
  170. SEEK_SET,
  171. SEEK_END
  172. );
  173. $whence_names = array(
  174. SEEK_CUR => "SEEK_CUR",
  175. SEEK_SET => "SEEK_SET",
  176. SEEK_END => "SEEK_END"
  177. );
  178. /* generate some random seek offsets */
  179. $position = 0;
  180. for ($i = 0; $i < 256; $i++) {
  181. $whence = $whence_map[array_rand($whence_map, 1)];
  182. switch($whence) {
  183. case SEEK_SET:
  184. $offset = rand(0, $DATALEN - 1);
  185. $position = $offset;
  186. break;
  187. case SEEK_END:
  188. $offset = -rand(0, $DATALEN - 1);
  189. $position = $DATALEN + $offset;
  190. break;
  191. case SEEK_CUR:
  192. $offset = rand(0, $DATALEN - 1);
  193. $offset -= $position;
  194. $position += $offset;
  195. break;
  196. }
  197. $seeks[] = array($whence, $offset, $position);
  198. }
  199. /* we compare the results of fgets using differing line lengths to
  200. * test the fgets layer also */
  201. $line_lengths = array(1024, 256, 64, 16);
  202. $fail_count = 0;
  203. ob_start();
  204. foreach($line_lengths as $line_length) {
  205. /* now compare the real stream with the user stream */
  206. $j = 0;
  207. rewind($tf);
  208. rewind($fp);
  209. foreach($seeks as $seekdata) {
  210. list($whence, $offset, $position) = $seekdata;
  211. $rpb = ftell($tf);
  212. $rr = (int)fseek($tf, $offset, $whence);
  213. $rpa = ftell($tf);
  214. $rline = fgets($tf, $line_length);
  215. (int)fseek($tf, - strlen($rline), SEEK_CUR);
  216. $upb = ftell($fp);
  217. $ur = (int)fseek($fp, $offset, $whence);
  218. $upa = ftell($fp);
  219. $uline = fgets($fp, $line_length);
  220. (int)fseek($fp, - strlen($uline), SEEK_CUR);
  221. printf("\n--[%d] whence=%s offset=%d line_length=%d position_should_be=%d --\n",
  222. $j, $whence_names[$whence], $offset, $line_length, $position);
  223. printf("REAL: pos=(%d,%d,%d) ret=%d line[%d]=`%s'\n", $rpb, $rpa, ftell($tf), $rr, strlen($rline), $rline);
  224. printf("USER: pos=(%d,%d,%d) ret=%d line[%d]=`%s'\n", $upb, $upa, ftell($fp), $ur, strlen($uline), $uline);
  225. if ($rr != $ur || $rline != $uline || $rpa != $position || $upa != $position) {
  226. $fail_count++;
  227. echo "###################################### FAIL!\n";
  228. $dat = stream_get_meta_data($fp);
  229. var_dump($dat);
  230. break;
  231. }
  232. $j++;
  233. }
  234. if ($fail_count)
  235. break;
  236. }
  237. if ($fail_count == 0) {
  238. ob_end_clean();
  239. echo "SEEK: OK\n";
  240. } else {
  241. echo "SEEK: FAIL\n";
  242. ob_end_flush();
  243. }
  244. $fail_count = 0;
  245. fseek($fp, $DATALEN / 2, SEEK_SET);
  246. fseek($tf, $DATALEN / 2, SEEK_SET);
  247. if (ftell($fp) != ftell($tf)) {
  248. echo "SEEK: positions do not match!\n";
  249. }
  250. $n = 0;
  251. while(!feof($fp)) {
  252. $uline = fgets($fp, 1024);
  253. $rline = fgets($tf, 1024);
  254. if ($uline != $rline) {
  255. echo "FGETS: FAIL\niter=$n user=$uline [pos=" . ftell($fp) . "]\nreal=$rline [pos=" . ftell($tf) . "]\n";
  256. $fail_count++;
  257. break;
  258. }
  259. }
  260. if ($fail_count == 0) {
  261. echo "FGETS: OK\n";
  262. }
  263. /* One final test to see if the position is respected when opened for append */
  264. $fp = fopen("test://lyrics", "a+");
  265. rewind($fp);
  266. var_dump(ftell($fp));
  267. $data = fgets($fp);
  268. fclose($fp);
  269. echo $data . "\n";
  270. ?>