BufferTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. declare(strict_types=1);
  3. namespace BitWasp\Buffertools\Tests;
  4. use \BitWasp\Buffertools\Buffer;
  5. use PHPUnit\Framework\TestCase;
  6. class BufferTest extends TestCase
  7. {
  8. public function testBufferDebug()
  9. {
  10. $buffer = new Buffer('AAAA', 4);
  11. $debug = $buffer->__debugInfo();
  12. $this->assertTrue(isset($debug['buffer']));
  13. $this->assertTrue(isset($debug['size']));
  14. $str = $debug['buffer'];
  15. $this->assertEquals('0x', substr($str, 0, 2));
  16. $this->assertEquals('41414141', substr($str, 2));
  17. }
  18. public function testCreateEmptyBuffer()
  19. {
  20. $buffer = new Buffer();
  21. $this->assertInstanceOf(Buffer::class, $buffer);
  22. $this->assertEmpty($buffer->getBinary());
  23. }
  24. public function testCreateEmptyHexBuffer()
  25. {
  26. $buffer = Buffer::hex();
  27. $this->assertInstanceOf(Buffer::class, $buffer);
  28. $this->assertEmpty($buffer->getBinary());
  29. }
  30. public function testCreateBuffer()
  31. {
  32. $hex = '80000000';
  33. $buffer = Buffer::hex($hex);
  34. $this->assertInstanceOf(Buffer::class, $buffer);
  35. $this->assertNotEmpty($buffer->getBinary());
  36. }
  37. /**
  38. * @expectedException \Exception
  39. * @expectedExceptionMessage Byte string exceeds maximum size
  40. */
  41. public function testCreateMaxBufferExceeded()
  42. {
  43. $lim = 4;
  44. Buffer::hex('4141414111', $lim);
  45. }
  46. public function testCreateHexBuffer()
  47. {
  48. $hex = '41414141';
  49. $buffer = Buffer::hex($hex);
  50. $this->assertInstanceOf(Buffer::class, $buffer);
  51. $this->assertNotEmpty($buffer->getBinary());
  52. }
  53. public function testPadding()
  54. {
  55. $buffer = Buffer::hex('41414141', 6);
  56. $this->assertEquals(4, $buffer->getInternalSize());
  57. $this->assertEquals(6, $buffer->getSize());
  58. $this->assertEquals("000041414141", $buffer->getHex());
  59. }
  60. public function testSerialize()
  61. {
  62. $hex = '41414141';
  63. $dec = gmp_strval(gmp_init($hex, 16), 10);
  64. $bin = pack("H*", $hex);
  65. $buffer = Buffer::hex($hex);
  66. // Check Binary
  67. $retBinary = $buffer->getBinary();
  68. $this->assertSame($bin, $retBinary);
  69. // Check Hex
  70. $this->assertSame($hex, $buffer->getHex());
  71. // Check Decimal
  72. $this->assertSame($dec, $buffer->getInt());
  73. $this->assertInstanceOf(\GMP::class, $buffer->getGmp());
  74. }
  75. public function testGetSize()
  76. {
  77. $this->assertEquals(1, Buffer::hex('41')->getSize());
  78. $this->assertEquals(4, Buffer::hex('41414141')->getSize());
  79. $this->assertEquals(4, Buffer::hex('41', 4)->getSize());
  80. }
  81. /**
  82. * @return array
  83. */
  84. public function getIntVectors(): array
  85. {
  86. return [
  87. ['1', '01', 1, ],
  88. ['1', '01', null,],
  89. ['20', '14', 1, ]
  90. ];
  91. }
  92. /**
  93. * @dataProvider getIntVectors
  94. * @param int|string $int
  95. * @param int|null $size
  96. * @param string $expectedHex
  97. */
  98. public function testIntConstruct($int, string $expectedHex, int $size = null)
  99. {
  100. $buffer = Buffer::int($int, $size);
  101. $this->assertEquals($expectedHex, $buffer->getHex());
  102. }
  103. /**
  104. * @return array
  105. */
  106. public function getGmpVectors(): array
  107. {
  108. return [
  109. [ gmp_init('0A', 16) ],
  110. [ gmp_init('237852977508946591877284351678975096651401224047304305322504192889595623579202', 10) ],
  111. ];
  112. }
  113. /**
  114. * @dataProvider getGmpVectors
  115. * @param \GMP $gmp
  116. */
  117. public function testGmpConstruction(\GMP $gmp)
  118. {
  119. $this->assertTrue(gmp_cmp($gmp, Buffer::gmp($gmp)->getGmp()) === 0);
  120. }
  121. public function testGmpConstructionNegative()
  122. {
  123. $gmp = gmp_init('-1234', 10);
  124. $this->expectException(\InvalidArgumentException::class);
  125. Buffer::gmp($gmp);
  126. }
  127. public function testSlice()
  128. {
  129. $a = Buffer::hex("11000011");
  130. $this->assertEquals("1100", $a->slice(0, 2)->getHex());
  131. $this->assertEquals("0011", $a->slice(2, 4)->getHex());
  132. $b = Buffer::hex("00111100");
  133. $this->assertEquals("0011", $b->slice(0, 2)->getHex());
  134. $this->assertEquals("1100", $b->slice(2, 4)->getHex());
  135. $c = Buffer::hex("111100", 4);
  136. $this->assertEquals("0011", $c->slice(0, 2)->getHex());
  137. $this->assertEquals("1100", $c->slice(2, 4)->getHex());
  138. }
  139. public function testEquals()
  140. {
  141. $first = Buffer::hex('ab');
  142. $second = Buffer::hex('ab');
  143. $firstExtraLong = Buffer::hex('ab', 10);
  144. $firstShort = new Buffer('', 0);
  145. $this->assertTrue($first->equals($second));
  146. $this->assertFalse($first->equals($firstExtraLong));
  147. $this->assertFalse($first->equals($firstExtraLong));
  148. $this->assertFalse($first->equals($firstShort));
  149. }
  150. }