summaryrefslogtreecommitdiff
path: root/ext/standard/tests/file/userstreams.phpt
blob: 124825f24ed54c23709c1a4ead230a3f8ec03e12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
--TEST--
User-space streams
--FILE--
<?php
# vim600:syn=php:

/* This is a fairly aggressive test that looks at
 * user streams and also gives the seek/gets/buffer
 * layer of streams a thorough testing */

$lyrics = <<<EOD
...and the road becomes my bride
I have stripped of all but pride
so in her I do confide
and she keeps me satisfied
gives me all I need
...and with dust in throat I crave
to the game you stay a slave
rover  wanderer
nomad  vagabond
call me what you will
   But Ill take my time anywhere
   Free to speak my mind anywhere
   and Ill redefine anywhere
      Anywhere I roam
	  Where I lay my head is home
...and the earth becomes my throne
I adapt to the unknown
under wandering stars Ive grown
by myself but not alone
I ask no one
...and my ties are severed clean
the less I have the more I gain
off the beaten path I reign
rover  wanderer
nomad  vagabond
call me what you will
   But Ill take my time anywhere
   Free to speak my mind anywhere
   and Ill never mind anywhere
      Anywhere I roam
	  Where I lay my head is home
   But Ill take my time anywhere
   Free to speak my mind anywhere
   and Ill take my find anywhere
      Anywhere I roam
	  Where I lay my head is home
   carved upon my stone
   my body lie but still I roam
      Wherever I may roam.

Wherever I May Roam

EOD;

/* repeat the data a few times so that it grows larger than
 * the default cache chunk size and that we have something
 * to seek around... */
$DATA = "";
for ($i = 0; $i < 30; $i++) {
	if ($i % 2 == 0)
		$DATA .= str_rot13($lyrics);
	else
		$DATA .= $lyrics;
}

/* store the data in a regular file so that we can compare
 * the results */
$tf = tmpfile();
fwrite($tf, $DATA);
$n = ftell($tf);
rewind($tf) or die("failed to rewind tmp file!");
if (ftell($tf) != 0)
	die("tmpfile is not at start!");
$DATALEN = strlen($DATA);
if ($n != $DATALEN)
	die("tmpfile stored $n bytes; should be $DATALEN!");

class uselessstream 
{
}

class mystream 
{
	public $path;
	public $mode;
	public $options;

	public $position;
	public $varname;

	function stream_open($path, $mode, $options, &$opened_path)
	{
		$this->path = $path;
		$this->mode = $mode;
		$this->options = $options;

		$split = parse_url($path);
		$this->varname = $split["host"];

		if (strchr($mode, 'a'))
			$this->position = strlen($GLOBALS[$this->varname]);
		else
			$this->position = 0;
		
		return true;
	}

	function stream_read($count)
	{
		$ret = substr($GLOBALS[$this->varname], $this->position, $count);
		$this->position += strlen($ret);
		return $ret;
	}

	function stream_tell()
	{
		return $this->position;
	}

	function stream_eof()
	{
		return $this->position >= strlen($GLOBALS[$this->varname]);
	}

	function stream_seek($offset, $whence)
	{
		switch($whence) {
			case SEEK_SET:
				if ($offset < strlen($GLOBALS[$this->varname]) && $offset >= 0) {
					$this->position = $offset;
					return true;
				} else {
					return false;
				}
				break;
			case SEEK_CUR:
				if ($offset >= 0) {
					$this->position += $offset;
					return true;
				} else {
					return false;
				}
				break;
			case SEEK_END:
				if (strlen($GLOBALS[$this->varname]) + $offset >= 0) {
					$this->position = strlen($GLOBALS[$this->varname]) + $offset;
					return true;
				} else {
					return false;
				}
				break;
			default:
				return false;
		}
	}

}

if (@stream_wrapper_register("bogus", "class_not_exist")) {
	die("Registered a non-existant class!!!???");
}
echo "Not Registered\n";

if (!stream_wrapper_register("test", "mystream")) {
	die("test wrapper registration failed");
}
echo "Registered\n";

if (!stream_wrapper_register("bogon", "uselessstream")) {
	die("bogon wrapper registration failed");
}
echo "Registered\n";

$b = @fopen("bogon://url", "rb");
if (is_resource($b)) {
	die("Opened a bogon??");
}

$fp = fopen("test://DATA", "rb");
if (!$fp || !is_resource($fp)) {
	die("Failed to open resource");
}

/* some default seeks that will cause buffer/cache misses */
$seeks = array(
	array(SEEK_SET, 0, 0),
	array(SEEK_CUR, 8450, 8450),
	array(SEEK_CUR, -7904, 546),
	array(SEEK_CUR, 12456, 13002),

	/* end up at BOF so that randomly generated seek offsets
	 * below will know where they are supposed to be */
	array(SEEK_SET, 0, 0)
);

$whence_map = array(
	SEEK_CUR,
	SEEK_SET,
	SEEK_END
);
$whence_names = array(
	SEEK_CUR => "SEEK_CUR",
	SEEK_SET => "SEEK_SET",
	SEEK_END => "SEEK_END"
	);

/* generate some random seek offsets */
$position = 0;
for ($i = 0; $i < 256; $i++) {
	$whence = $whence_map[array_rand($whence_map, 1)];
	switch($whence) {
		case SEEK_SET:
			$offset = rand(0, $DATALEN);
			$position = $offset;
			break;
		case SEEK_END:
			$offset = -rand(0, $DATALEN);
			$position = $DATALEN + $offset;
			break;
		case SEEK_CUR:
			$offset = rand(0, $DATALEN);
			$offset -= $position;
			$position += $offset;
			break;
	}

	$seeks[] = array($whence, $offset, $position);
}

/* we compare the results of fgets using differing line lengths to 
 * test the fgets layer also */
$line_lengths = array(1024, 256, 64, 16);
$fail_count = 0;

ob_start();
foreach($line_lengths as $line_length) {
	/* now compare the real stream with the user stream */
	$j = 0;
	rewind($tf);
	rewind($fp);
	foreach($seeks as $seekdata) {
		list($whence, $offset, $position) = $seekdata;

		$rpb = ftell($tf);
		$rr = (int)fseek($tf, $offset, $whence);
		$rpa = ftell($tf);
		$rline = fgets($tf, $line_length);
		(int)fseek($tf, - strlen($rline), SEEK_CUR);

		$upb = ftell($fp);
		$ur = (int)fseek($fp, $offset, $whence);
		$upa = ftell($fp);
		$uline = fgets($fp, $line_length);
		(int)fseek($fp, - strlen($uline), SEEK_CUR);

		printf("\n--[%d] whence=%s offset=%d line_length=%d position_should_be=%d --\n",
			$j, $whence_names[$whence], $offset, $line_length, $position);
		printf("REAL: pos=(%d,%d,%d) ret=%d line[%d]=`%s'\n", $rpb, $rpa, ftell($tf), $rr, strlen($rline), $rline);
		printf("USER: pos=(%d,%d,%d) ret=%d line[%d]=`%s'\n", $upb, $upa, ftell($fp), $ur, strlen($uline), $uline);

		if ($rr != $ur || $rline != $uline || $rpa != $position || $upa != $position) {
			$fail_count++;
			echo "###################################### FAIL!\n";
			$dat = stream_get_meta_data($fp);
			var_dump($dat);
			break;
		}
		
		$j++;
	}
	if ($fail_count)
		break;
}

if ($fail_count == 0) {
	ob_end_clean();
	echo "SEEK: OK\n";
} else {
	echo "SEEK: FAIL\n";
	ob_end_flush();
}

$fail_count = 0;

fseek($fp, $DATALEN / 2, SEEK_SET);
fseek($tf, $DATALEN / 2, SEEK_SET);

if (ftell($fp) != ftell($tf)) {
	echo "SEEK: positions do not match!\n";
}

$n = 0;
while(!feof($fp)) {
	$uline = fgets($fp, 1024);
	$rline = fgets($tf, 1024);

	if ($uline != $rline) {
		echo "FGETS: FAIL\niter=$n user=$uline [pos=" . ftell($fp) . "]\nreal=$rline [pos=" . ftell($tf) . "]\n";
		$fail_count++;
		break;
	}
}

if ($fail_count == 0) {
	echo "FGETS: OK\n";
}

/* One final test to see if the position is respected when opened for append */
$fp = fopen("test://lyrics", "a+");
rewind($fp);
var_dump(ftell($fp));
$data = fgets($fp);
fclose($fp);
echo $data . "\n";

?>
--EXPECT--
Not Registered
Registered
Registered
SEEK: OK
FGETS: OK
int(0)
...and the road becomes my bride