PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Serializer/Adapter/PythonPickleUnserializeTest.php

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