PythonPickleUnserializeTest.php 11 KB

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