PythonPickleUnserializeTest.php 11 KB

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