PythonPickleSerializeProtocol1Test.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_PythonPickleSerializeProtocol1Test extends PHPUnit_Framework_TestCase
  34. {
  35. private $_adapter;
  36. public function setUp()
  37. {
  38. $this->_adapter = new Zend_Serializer_Adapter_PythonPickle(array('protocol' => 1));
  39. }
  40. public function tearDown()
  41. {
  42. $this->_adapter = null;
  43. }
  44. public function testSerializeNull()
  45. {
  46. $value = null;
  47. $expected = 'N.';
  48. $data = $this->_adapter->serialize($value);
  49. $this->assertEquals($expected, $data);
  50. }
  51. public function testSerializeTrue()
  52. {
  53. $value = true;
  54. $expected = "I01\r\n.";
  55. $data = $this->_adapter->serialize($value);
  56. $this->assertEquals($expected, $data);
  57. }
  58. public function testSerializeFalse()
  59. {
  60. $value = false;
  61. $expected = "I00\r\n.";
  62. $data = $this->_adapter->serialize($value);
  63. $this->assertEquals($expected, $data);
  64. }
  65. public function testSerializeBinInt1()
  66. {
  67. $value = 255;
  68. $expected = "K\xff.";
  69. $data = $this->_adapter->serialize($value);
  70. $this->assertEquals($expected, $data);
  71. }
  72. public function testSerializeBinInt2()
  73. {
  74. $value = 256;
  75. $expected = "M\x00\x01.";
  76. $data = $this->_adapter->serialize($value);
  77. $this->assertEquals($expected, $data);
  78. }
  79. public function testSerializeBinInt()
  80. {
  81. $value = -2;
  82. $expected = "J\xfe\xff\xff\xff.";
  83. $data = $this->_adapter->serialize($value);
  84. $this->assertEquals($expected, $data);
  85. }
  86. public function testSerializeBinFloat()
  87. {
  88. $value = -12345.6789;
  89. $expected = "G\xc0\xc8\x1c\xd6\xe6\x31\xf8\xa1.";
  90. $data = $this->_adapter->serialize($value);
  91. $this->assertEquals($expected, $data);
  92. }
  93. public function testSerializeShortBinString()
  94. {
  95. $value = 'test';
  96. $expected = "U\x04test"
  97. . "q\x00.";
  98. $data = $this->_adapter->serialize($value);
  99. $this->assertEquals($expected, $data);
  100. }
  101. public function testSerializeBinString()
  102. {
  103. $value = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
  104. . "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
  105. . "01234567890123456789012345678901234567890123456789012345";
  106. $expected = "T\x00\x01\x00\x00"
  107. . "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
  108. . "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
  109. . "01234567890123456789012345678901234567890123456789012345"
  110. . "q\x00.";
  111. $data = $this->_adapter->serialize($value);
  112. $this->assertEquals($expected, $data);
  113. }
  114. }