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