PythonPickleUnserializeTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /**
  3. * @package Zend_Cache
  4. * @subpackage UnitTests
  5. */
  6. /** @see Zend_Serializer_Adapter_PythonPickle */
  7. require_once 'Zend/Serializer/Adapter/PythonPickle.php';
  8. /**
  9. * PHPUnit test case
  10. */
  11. require_once 'PHPUnit/Framework/TestCase.php';
  12. /**
  13. * @package Zend_Serializer
  14. * @subpackage UnitTests
  15. */
  16. class Zend_Serializer_Adapter_PythonPickleUnserializeTest extends PHPUnit_Framework_TestCase
  17. {
  18. private $_adapter;
  19. public function setUp()
  20. {
  21. $this->_adapter = new Zend_Serializer_Adapter_PythonPickle();
  22. }
  23. public function tearDown()
  24. {
  25. $this->_adapter = null;
  26. }
  27. public function testUnserializeNone() {
  28. $value = "N.";
  29. $expected = null;
  30. $data = $this->_adapter->unserialize($value);
  31. $this->assertEquals($expected, $data);
  32. }
  33. public function testUnserializeNewTrue() {
  34. $value = "\x80\x02\x88.";
  35. $expected = true;
  36. $data = $this->_adapter->unserialize($value);
  37. $this->assertEquals($expected, $data);
  38. }
  39. public function testUnserializeNewFalse() {
  40. $value = "\x80\x02\x89.";
  41. $expected = false;
  42. $data = $this->_adapter->unserialize($value);
  43. $this->assertEquals($expected, $data);
  44. }
  45. public function testUnserializeIntTrue() {
  46. $value = "I01\r\n.";
  47. $expected = true;
  48. $data = $this->_adapter->unserialize($value);
  49. $this->assertEquals($expected, $data);
  50. }
  51. public function testUnserializeIntFalse() {
  52. $value = "I00\r\n.";
  53. $expected = false;
  54. $data = $this->_adapter->unserialize($value);
  55. $this->assertEquals($expected, $data);
  56. }
  57. public function testUnserializeInt() {
  58. $value = "I1\r\n.";
  59. $expected = 1;
  60. $data = $this->_adapter->unserialize($value);
  61. $this->assertEquals($expected, $data);
  62. }
  63. public function testUnserializeBinInt() {
  64. $value = "\x80\x02J\xc7\xcf\xff\xff.";
  65. $expected = -12345;
  66. $data = $this->_adapter->unserialize($value);
  67. $this->assertEquals($expected, $data);
  68. }
  69. public function testUnserializeBinInt1() {
  70. $value = "\x80\x02K\x02.";
  71. $expected = 2;
  72. $data = $this->_adapter->unserialize($value);
  73. $this->assertEquals($expected, $data);
  74. }
  75. public function testUnserializeBinInt2() {
  76. $value = "\x80\x02M\x00\x01.";
  77. $expected = 256;
  78. $data = $this->_adapter->unserialize($value);
  79. $this->assertEquals($expected, $data);
  80. }
  81. public function testUnserializeLong() {
  82. $value = "L9876543210L\r\n.";
  83. $expected = '9876543210';
  84. $data = $this->_adapter->unserialize($value);
  85. $this->assertEquals($expected, $data);
  86. }
  87. public function testUnserializeLong1() {
  88. $value = "\x80\x02\x8a\x05\xea\x16\xb0\x4c\x02.";
  89. $expected = '9876543210';
  90. $data = $this->_adapter->unserialize($value);
  91. $this->assertEquals($expected, $data);
  92. }
  93. public function testUnserializeLong4Positiv() {
  94. $value = "\x80\x02\x8b\x07\x00\x00\x00"
  95. . str_pad("\xff", 7, "\x7f")
  96. . ".";
  97. $expected = '35887507618889727';
  98. $data = $this->_adapter->unserialize($value);
  99. $this->assertEquals($expected, $data);
  100. }
  101. public function testUnserializeLong4Negativ() {
  102. $value = "\x80\x02\x8b\x07\x00\x00\x00"
  103. . str_pad("\x00", 7, "\x9f")
  104. . ".";
  105. $expected = '-27127564814278912';
  106. $data = $this->_adapter->unserialize($value);
  107. $this->assertEquals($expected, $data);
  108. }
  109. public function testUnserializeLong4InfBcMath() {
  110. $value = "\x80\x02\x8b\x08\x00\x00\x00"
  111. . str_pad("\x00", 8, "\x9f")
  112. . ".";
  113. if (extension_loaded('bcmath')) {
  114. $expected = '-6944656592455360768';
  115. } else {
  116. $expected = INF;
  117. }
  118. $data = $this->_adapter->unserialize($value);
  119. $this->assertEquals($expected, $data);
  120. }
  121. public function testUnserializeFloat() {
  122. $value = "F-12345.6789\r\n.";
  123. $expected = -12345.6789;
  124. $data = $this->_adapter->unserialize($value);
  125. $this->assertEquals($expected, $data);
  126. }
  127. public function testUnserializeBinFloat() {
  128. $value = "\x80\x02G\xc0\xc8\x1c\xd6\xe6\x31\xf8\xa1.";
  129. $expected = -12345.6789;
  130. $data = $this->_adapter->unserialize($value);
  131. $this->assertEquals($expected, $data);
  132. }
  133. public function testUnserializeString() {
  134. $value = "S'test'\r\np0\r\n.";
  135. $expected = 'test';
  136. $data = $this->_adapter->unserialize($value);
  137. $this->assertEquals($expected, $data);
  138. }
  139. public function testUnserializeStringDoubleQuotes() {
  140. $value = "S\"'t'e's't'\"\r\np0\r\n.";
  141. $expected = "'t'e's't'";
  142. $data = $this->_adapter->unserialize($value);
  143. $this->assertEquals($expected, $data);
  144. }
  145. public function testUnserializeStringWithSpecialChars() {
  146. $value = "S'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f"
  147. . "\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f"
  148. . "\\xff\\\\\"\\''\r\n"
  149. . "p0\r\n.";
  150. $expected = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
  151. . "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  152. . "\xff\\\"'";
  153. $data = $this->_adapter->unserialize($value);
  154. $this->assertEquals($expected, $data);
  155. }
  156. public function testUnserializeBinString() {
  157. $value = "\x80\x02T\x00\x01\x00\x00"
  158. . "01234567890123456789012345678901234567890123456789"
  159. . "01234567890123456789012345678901234567890123456789"
  160. . "01234567890123456789012345678901234567890123456789"
  161. . "01234567890123456789012345678901234567890123456789"
  162. . "01234567890123456789012345678901234567890123456789012345"
  163. . "q\x00.";
  164. $expected = '01234567890123456789012345678901234567890123456789'
  165. . '01234567890123456789012345678901234567890123456789'
  166. . '01234567890123456789012345678901234567890123456789'
  167. . '01234567890123456789012345678901234567890123456789'
  168. . '01234567890123456789012345678901234567890123456789012345';
  169. $data = $this->_adapter->unserialize($value);
  170. $this->assertEquals($expected, $data);
  171. }
  172. public function testUnserializeShortBinString() {
  173. $value = "\x80\x02U\x04"
  174. . "test"
  175. . "q\x00.";
  176. $expected = 'test';
  177. $data = $this->_adapter->unserialize($value);
  178. $this->assertEquals($expected, $data);
  179. }
  180. public function testUnserializeUnicode() {
  181. $value = "Vtest\\u0400\r\n" // test + ` + E
  182. . "p0\r\n"
  183. . ".";
  184. $expected = "test\xd0\x80";
  185. $data = $this->_adapter->unserialize($value);
  186. $this->assertEquals($expected, $data);
  187. }
  188. public function testUnserializeBinUnicode() {
  189. $value = "\x80\x02" . "X\x07\x00\x00\x00" . "test\xd0\x80\n.";
  190. $expected = "test\xd0\x80\n"; // test + ` + E + \n
  191. $data = $this->_adapter->unserialize($value);
  192. $this->assertEquals($expected, $data);
  193. }
  194. public function testUnserializeListAppend() {
  195. $value = "(lp0\r\n"
  196. . "I1\r\n"
  197. . "aI2\r\n"
  198. . "aI3\r\n"
  199. . "a.";
  200. $expected = array(1,2,3);
  201. $data = $this->_adapter->unserialize($value);
  202. $this->assertEquals($expected, $data);
  203. }
  204. public function testUnserializeEmptyListAppends() {
  205. $value = "\x80\x02]q\x00(K\x01K\x02K\x03e.";
  206. $expected = array(1,2,3);
  207. $data = $this->_adapter->unserialize($value);
  208. $this->assertEquals($expected, $data);
  209. }
  210. public function testUnserializeDictSetItem() {
  211. $value = "(dp0\r\n"
  212. . "S'test1'\r\n"
  213. . "p1\r\n"
  214. . "I1\r\n"
  215. . "sI0\r\n"
  216. . "I2\r\n"
  217. . "sS'test3'\r\n"
  218. . "p2\r\n"
  219. . "g2\r\n"
  220. . "s.";
  221. $expected = array('test1' => 1, 0 => 2, 'test3' => 'test3');
  222. $data = $this->_adapter->unserialize($value);
  223. $this->assertEquals($expected, $data);
  224. }
  225. public function testUnserializeEmptyDictSetItems() {
  226. $value = "\x80\x02}q\x00(U\x05test1q\x01K\x01K\x00K\x02U\x05test3q\x02h\x02u.";
  227. $expected = array('test1' => 1, 0 => 2, 'test3' => 'test3');
  228. $data = $this->_adapter->unserialize($value);
  229. $this->assertEquals($expected, $data);
  230. }
  231. public function testUnserializeTuple() {
  232. $value = "(I1\r\n"
  233. . "I2\r\n"
  234. . "I3\r\n"
  235. . "tp0\r\n"
  236. . ".";
  237. $expected = array(1,2,3);
  238. $data = $this->_adapter->unserialize($value);
  239. $this->assertEquals($expected, $data);
  240. }
  241. public function testUnserializeTuple1() {
  242. $value = "\x80\x02K\x01\x85q\x00.";
  243. $expected = array(1);
  244. $data = $this->_adapter->unserialize($value);
  245. $this->assertEquals($expected, $data);
  246. }
  247. public function testUnserializeTuple2() {
  248. $value = "\x80\x02K\x01K\x02\x86q\x00.";
  249. $expected = array(1,2);
  250. $data = $this->_adapter->unserialize($value);
  251. $this->assertEquals($expected, $data);
  252. }
  253. public function testUnserializeTuple3() {
  254. $value = "\x80\x02K\x01K\x02K\x03\x87q\x00.";
  255. $expected = array(1,2,3);
  256. $data = $this->_adapter->unserialize($value);
  257. $this->assertEquals($expected, $data);
  258. }
  259. public function testUnserialzeInvalid() {
  260. $value = 'not a serialized string';
  261. $this->setExpectedException('Zend_Serializer_Exception');
  262. $this->_adapter->unserialize($value);
  263. }
  264. }