xref: /PHP-7.3/ext/shmop/tests/001.phpt (revision 782352c5)
1--TEST--
2shmop extension test
3--SKIPIF--
4<?php
5	if (!extension_loaded("shmop")) {
6		die("skip shmop() extension not available");
7	}
8?>
9--FILE--
10<?php
11	$hex_shm_id = 0xff3;
12	$write_d1 = "test #1 of the shmop() extension";
13	$write_d2 = "test #2 append data to shared memory segment";
14
15	echo "shm open for create: ";
16	$shm_id = shmop_open($hex_shm_id, "n", 0644, 1024);
17	if (!$shm_id) {
18		die("failed\n");
19	} else {
20		echo "ok\n";
21	}
22
23	echo "shm size is: " . ($shm_size = shmop_size($shm_id)) . "\n";
24
25	echo "shm write test #1: ";
26	$written = shmop_write($shm_id, $write_d1, 0);
27	if ($written != strlen($write_d1)) {
28		echo "failed\n";
29	} else {
30		echo "ok\n";
31	}
32
33	echo "data in memory is: " . shmop_read($shm_id, 0, $written) . "\n";
34
35	shmop_close($shm_id);
36
37	echo "shm open for read only: ";
38	$shm_id = shmop_open($hex_shm_id, "a", 0644, 1024);
39	if (!$shm_id) {
40		echo "failed\n";
41	} else {
42		echo "ok\n";
43	}
44
45	echo "data in memory is: " . shmop_read($shm_id, 0, $written) . "\n";
46
47	/* try to append data to the shared memory segment, this should fail */
48	shmop_write($shm_id, $write_d1, $written);
49
50	shmop_close($shm_id);
51
52	echo "shm open for read only: ";
53	$shm_id = shmop_open($hex_shm_id, "w", 0644, 1024);
54	if (!$shm_id) {
55		echo "failed\n";
56	} else {
57		echo "ok\n";
58	}
59
60	echo "shm write test #1: ";
61	$written = shmop_write($shm_id, $write_d2, $written);
62	if ($written != strlen($write_d2)) {
63		die("failed\n");
64	} else {
65		echo "ok\n";
66	}
67
68	echo "data in memory is: " . shmop_read($shm_id, 0, strlen($write_d1 . $write_d2)) . "\n";
69
70	echo "deletion of shm segment: ";
71	if (!shmop_delete($shm_id)) {
72		echo "failed\n";
73	} else {
74		echo "ok\n";
75	}
76
77	shmop_close($shm_id);
78?>
79--EXPECTF--
80shm open for create: ok
81shm size is: 1024
82shm write test #1: ok
83data in memory is: test #1 of the shmop() extension
84shm open for read only: ok
85data in memory is: test #1 of the shmop() extension
86
87Warning: shmop_write(): trying to write to a read only segment in %s on line %d
88shm open for read only: ok
89shm write test #1: ok
90data in memory is: test #1 of the shmop() extensiontest #2 append data to shared memory segment
91deletion of shm segment: ok
92