PythonPickleSerializeProtocol1Test.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_PythonPickleSerializeProtocol1Test extends PHPUnit_Framework_TestCase
  17. {
  18. private $_adapter;
  19. public function setUp()
  20. {
  21. $this->_adapter = new Zend_Serializer_Adapter_PythonPickle(array('protocol' => 1));
  22. }
  23. public function tearDown()
  24. {
  25. $this->_adapter = null;
  26. }
  27. public function testSerializeNull() {
  28. $value = null;
  29. $expected = 'N.';
  30. $data = $this->_adapter->serialize($value);
  31. $this->assertEquals($expected, $data);
  32. }
  33. public function testSerializeTrue() {
  34. $value = true;
  35. $expected = "I01\r\n.";
  36. $data = $this->_adapter->serialize($value);
  37. $this->assertEquals($expected, $data);
  38. }
  39. public function testSerializeFalse() {
  40. $value = false;
  41. $expected = "I00\r\n.";
  42. $data = $this->_adapter->serialize($value);
  43. $this->assertEquals($expected, $data);
  44. }
  45. public function testSerializeBinInt1() {
  46. $value = 255;
  47. $expected = "K\xff.";
  48. $data = $this->_adapter->serialize($value);
  49. $this->assertEquals($expected, $data);
  50. }
  51. public function testSerializeBinInt2() {
  52. $value = 256;
  53. $expected = "M\x00\x01.";
  54. $data = $this->_adapter->serialize($value);
  55. $this->assertEquals($expected, $data);
  56. }
  57. public function testSerializeBinInt() {
  58. $value = -2;
  59. $expected = "J\xfe\xff\xff\xff.";
  60. $data = $this->_adapter->serialize($value);
  61. $this->assertEquals($expected, $data);
  62. }
  63. public function testSerializeBinFloat() {
  64. $value = -12345.6789;
  65. $expected = "G\xc0\xc8\x1c\xd6\xe6\x31\xf8\xa1.";
  66. $data = $this->_adapter->serialize($value);
  67. $this->assertEquals($expected, $data);
  68. }
  69. public function testSerializeShortBinString() {
  70. $value = 'test';
  71. $expected = "U\x04test"
  72. . "q\x00.";
  73. $data = $this->_adapter->serialize($value);
  74. $this->assertEquals($expected, $data);
  75. }
  76. public function testSerializeBinString() {
  77. $value = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
  78. . "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
  79. . "01234567890123456789012345678901234567890123456789012345";
  80. $expected = "T\x00\x01\x00\x00"
  81. . "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
  82. . "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
  83. . "01234567890123456789012345678901234567890123456789012345"
  84. . "q\x00.";
  85. $data = $this->_adapter->serialize($value);
  86. $this->assertEquals($expected, $data);
  87. }
  88. }