ParserTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. declare(strict_types=1);
  3. namespace BitWasp\Buffertools\Tests;
  4. use \BitWasp\Buffertools\Buffer;
  5. use \BitWasp\Buffertools\Parser;
  6. use PHPUnit\Framework\TestCase;
  7. class ParserTest extends TestCase
  8. {
  9. public function testParserEmpty()
  10. {
  11. $parser = new Parser();
  12. $this->assertInstanceOf(Parser::class, $parser);
  13. $this->assertSame(0, $parser->getPosition());
  14. $this->assertInstanceOf(Buffer::class, $parser->getBuffer());
  15. $this->assertEmpty($parser->getBuffer()->getHex());
  16. }
  17. public function testGetBuffer()
  18. {
  19. $buffer = Buffer::hex('41414141');
  20. $parser = new Parser($buffer);
  21. $this->assertSame($parser->getBuffer()->getBinary(), $buffer->getBinary());
  22. }
  23. public function testGetBufferEmptyNull()
  24. {
  25. $buffer = new Buffer();
  26. $parser = new Parser($buffer);
  27. $parserData = $parser->getBuffer()->getBinary();
  28. $bufferData = $buffer->getBinary();
  29. $this->assertSame($parserData, $bufferData);
  30. }
  31. public function testWriteBytes()
  32. {
  33. $bytes = '41424344';
  34. $parser = new Parser();
  35. $parser->writeBytes(4, Buffer::hex($bytes));
  36. $returned = $parser->getBuffer()->getHex();
  37. $this->assertSame($returned, '41424344');
  38. }
  39. public function testWriteBytesFlip()
  40. {
  41. $bytes = '41424344';
  42. $parser = new Parser();
  43. $parser->writeBytes(4, Buffer::hex($bytes), true);
  44. $returned = $parser->getBuffer()->getHex();
  45. $this->assertSame($returned, '44434241');
  46. }
  47. public function testWriteBytesPadded()
  48. {
  49. $parser = new Parser();
  50. $parser->writeBytes(4, Buffer::hex('34'));
  51. $this->assertEquals("00000034", $parser->getBuffer()->getHex());
  52. }
  53. public function testWriteBytesFlipPadded()
  54. {
  55. $parser = new Parser();
  56. $parser->writeBytes(4, Buffer::hex('34'), true);
  57. $this->assertEquals("34000000", $parser->getBuffer()->getHex());
  58. }
  59. public function testReadBytes()
  60. {
  61. $bytes = '41424344';
  62. $parser = new Parser($bytes);
  63. $read = $parser->readBytes(4);
  64. $this->assertInstanceOf(Buffer::class, $read);
  65. $hex = $read->getHex();
  66. $this->assertSame($bytes, $hex);
  67. }
  68. public function testReadBytesFlip()
  69. {
  70. $bytes = '41424344';
  71. $parser = new Parser($bytes);
  72. $read = $parser->readBytes(4, true);
  73. $this->assertInstanceOf(Buffer::class, $read);
  74. $hex = $read->getHex();
  75. $this->assertSame('44434241', $hex);
  76. }
  77. /**
  78. * @expectedException \BitWasp\Buffertools\Exceptions\ParserOutOfRange
  79. * @expectedExceptionMessage Could not parse string of required length (empty)
  80. */
  81. public function testReadBytesEmpty()
  82. {
  83. // Should return false because position is zero,
  84. // and length is zero.
  85. $parser = new Parser();
  86. $parser->readBytes(0);
  87. }
  88. /**
  89. * @expectedException \BitWasp\Buffertools\Exceptions\ParserOutOfRange
  90. * @expectedExceptionMessage Could not parse string of required length (empty)
  91. */
  92. public function testReadBytesEndOfString()
  93. {
  94. $parser = new Parser('4041414142414141');
  95. $bytes1 = $parser->readBytes(4);
  96. $bytes2 = $parser->readBytes(4);
  97. $this->assertSame($bytes1->getHex(), '40414141');
  98. $this->assertSame($bytes2->getHex(), '42414141');
  99. $parser->readBytes(1);
  100. }
  101. /**
  102. * @expectedException \Exception
  103. */
  104. public function testReadBytesBeyondLength()
  105. {
  106. $bytes = '41424344';
  107. $parser = new Parser($bytes);
  108. $parser->readBytes(5);
  109. }
  110. }