Skip to content

Commit 2d0fa21

Browse files
Eugene LeonovichSean-Der
authored andcommitted
Test null byte key position
1 parent 116cc56 commit 2d0fa21

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

tests/139.phpt

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
--TEST--
2+
Null byte key position while unpacking objects
3+
--SKIPIF--
4+
<?php
5+
if (version_compare(PHP_VERSION, '5.2.0') < 0) {
6+
echo "skip tests in PHP 5.2 or newer";
7+
}
8+
--FILE--
9+
<?php
10+
if(!extension_loaded('msgpack'))
11+
{
12+
dl('msgpack.' . PHP_SHLIB_SUFFIX);
13+
}
14+
15+
function test($type, $array)
16+
{
17+
$stdClass = hex2bin('c0a8737464436c617373'); // "\0" => 'stdClass'
18+
$placeholder = hex2bin('a178a178'); // 'x' => 'x'
19+
20+
$serialized = msgpack_pack($array);
21+
$serialized = str_replace($placeholder, $stdClass, $serialized);
22+
$unserialized = msgpack_unpack($serialized);
23+
24+
var_dump($unserialized);
25+
unset($array['x']);
26+
27+
echo $unserialized == (object) $array ? 'OK' : 'ERROR', PHP_EOL;
28+
}
29+
30+
$array = array('x' => 'x', 'foo' => 1);
31+
test('single property, key at the beginning', $array);
32+
33+
$array = array('foo' => 1, 'x' => 'x');
34+
test('single property, key at the end', $array);
35+
36+
$array = array('x' => 'x', 'foo' => 1, 'bar' => 2);
37+
test('multiple properties, key at the beginning', $array);
38+
39+
$array = array('foo' => 1, 'x' => 'x', 'bar' => 2);
40+
test('multiple properties, key in the middle', $array);
41+
42+
$array = array('foo' => 1, 'bar' => 2, 'x' => 'x');
43+
test('multiple properties, key at the end', $array);
44+
45+
$array = array('null' => null, 'x' => 'x');
46+
test('null, key at the end', $array);
47+
48+
$array = array('int' => 1, 'x' => 'x');
49+
test('int, key at the end', $array);
50+
51+
$array = array('float' => 4.2, 'x' => 'x');
52+
test('float, key at the end', $array);
53+
54+
$array = array('string' => 'str', 'x' => 'x');
55+
test('string, key at the end', $array);
56+
57+
$array = array('array' => array(42), 'x' => 'x');
58+
test('array, key at the end', $array);
59+
60+
class Foo { public $a = null; }
61+
$obj = new Foo();
62+
$array = array('object' => $obj, 'x' => 'x');
63+
test('object, key at the end', $array);
64+
65+
--EXPECTF--
66+
object(stdClass)#%d (1) {
67+
["foo"]=>
68+
int(1)
69+
}
70+
OK
71+
object(stdClass)#%d (1) {
72+
["foo"]=>
73+
int(1)
74+
}
75+
OK
76+
object(stdClass)#%d (2) {
77+
["foo"]=>
78+
int(1)
79+
["bar"]=>
80+
int(2)
81+
}
82+
OK
83+
object(stdClass)#%d (2) {
84+
["foo"]=>
85+
int(1)
86+
["bar"]=>
87+
int(2)
88+
}
89+
OK
90+
object(stdClass)#%d (2) {
91+
["foo"]=>
92+
int(1)
93+
["bar"]=>
94+
int(2)
95+
}
96+
OK
97+
object(stdClass)#%d (1) {
98+
["null"]=>
99+
NULL
100+
}
101+
OK
102+
object(stdClass)#%d (1) {
103+
["int"]=>
104+
int(1)
105+
}
106+
OK
107+
object(stdClass)#%d (1) {
108+
["float"]=>
109+
float(4.2)
110+
}
111+
OK
112+
object(stdClass)#1 (1) {
113+
["string"]=>
114+
string(3) "str"
115+
}
116+
OK
117+
object(stdClass)#1 (1) {
118+
["array"]=>
119+
array(1) {
120+
[0]=>
121+
int(42)
122+
}
123+
}
124+
OK
125+
object(stdClass)#3 (1) {
126+
["object"]=>
127+
object(Foo)#2 (1) {
128+
["a"]=>
129+
NULL
130+
}
131+
}
132+
OK

0 commit comments

Comments
 (0)