TemplateTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. declare(strict_types=1);
  3. namespace BitWasp\Buffertools\Tests;
  4. use BitWasp\Buffertools\ByteOrder;
  5. use BitWasp\Buffertools\Types\ByteString;
  6. use BitWasp\Buffertools\Types\Uint64;
  7. use BitWasp\Buffertools\Types\Uint32;
  8. use BitWasp\Buffertools\Template;
  9. use BitWasp\Buffertools\Types\VarInt;
  10. use BitWasp\Buffertools\Types\VarString;
  11. use BitWasp\Buffertools\Buffer;
  12. use BitWasp\Buffertools\Parser;
  13. class TemplateTest extends BinaryTest
  14. {
  15. public function testTemplate()
  16. {
  17. $template = new Template();
  18. $this->assertEmpty($template->getItems());
  19. }
  20. /**
  21. * @expectedException \RuntimeException
  22. * @expectedExceptionMessage No items in template
  23. */
  24. public function testTemplateEmptyParse()
  25. {
  26. $template = new Template();
  27. $parser = new Parser('010203040a0b0c0d');
  28. $template->parse($parser);
  29. }
  30. public function testAddItemToTemplate()
  31. {
  32. $item = new Uint64();
  33. $template = new Template();
  34. $this->assertEmpty($template->getItems());
  35. $this->assertEquals(0, $template->count());
  36. $template->addItem($item);
  37. $items = $template->getItems();
  38. $this->assertEquals(1, count($template));
  39. $this->assertEquals($item, $items[0]);
  40. }
  41. public function testAddThroughConstructor()
  42. {
  43. $item = new Uint64();
  44. $template = new Template([$item]);
  45. $items = $template->getItems();
  46. $this->assertEquals(1, count($items));
  47. $this->assertEquals($item, $items[0]);
  48. }
  49. public function testParse()
  50. {
  51. $value = '50c3000000000000';
  52. $varint = '19';
  53. $script = '76a914d04b020dab70a7dd7055db3bbc70d27c1b25a99c88ac';
  54. $buffer = Buffer::hex($value . $varint . $script);
  55. $parser = new Parser($buffer);
  56. $uint64le = new Uint64(ByteOrder::LE);
  57. $varstring = new VarString(new VarInt());
  58. $template = new Template([$uint64le, $varstring]);
  59. list ($foundValue, $foundScript) = $template->parse($parser);
  60. $this->assertInternalType('string', $foundValue);
  61. $this->assertEquals(50000, $foundValue);
  62. $this->assertEquals($script, $foundScript->getHex());
  63. }
  64. public function testWrite()
  65. {
  66. $value = '50c3000000000000';
  67. $varint = '19';
  68. $script = '76a914d04b020dab70a7dd7055db3bbc70d27c1b25a99c88ac';
  69. $hex = $value . $varint . $script;
  70. $uint64le = new Uint64(ByteOrder::LE);
  71. $varstring = new VarString(new VarInt());
  72. $template = new Template([$uint64le, $varstring]);
  73. $binary = $template->write([50000, Buffer::hex($script)]);
  74. $this->assertEquals(pack("H*", $hex), $binary->getBinary());
  75. }
  76. /**
  77. * @expectedException \RuntimeException
  78. * @expectedExceptionMessage Number of items must match template
  79. */
  80. public function testWriteIncomplete()
  81. {
  82. $uint64le = new Uint64(ByteOrder::LE);
  83. $varstring = new VarString(new VarInt());
  84. $template = new Template([$uint64le, $varstring]);
  85. $template->write([50000]);
  86. }
  87. public function testFixedLengthString()
  88. {
  89. $txin = '58891e8f28100642464417f53845c3953a43e31b35d061bdbf6ca3a64fffabb8000000008c493046022100a9d501a6f59c45a24e65e5030903cfd80ba33910f24d6a505961d64fa5042b4f02210089fa7cc00ab2b5fc15499fa259a057e6d0911d4e849f1720cc6bc58e941fe7e20141041a2756dd506e45a1142c7f7f03ae9d3d9954f8543f4c3ca56f025df66f1afcba6086cec8d4135cbb5f5f1d731f25ba0884fc06945c9bbf69b9b543ca91866e79ffffffff';
  90. $txinBuf = Buffer::hex($txin);
  91. $txinParser = new Parser($txinBuf);
  92. $template = new Template(
  93. [
  94. new ByteString(32, ByteOrder::LE),
  95. new Uint32(ByteOrder::LE),
  96. new VarString(new VarInt())
  97. ]
  98. );
  99. $out = $template->parse($txinParser);
  100. /**
  101. * @var Buffer $txhash
  102. */
  103. $txhash = $out[0];
  104. /**
  105. * @var Buffer $script
  106. */
  107. $script = $out[2];
  108. $this->assertEquals('b8abff4fa6a36cbfbd61d0351be3433a95c34538f5174446420610288f1e8958', $txhash->getHex());
  109. $this->assertEquals(0, $out[1]);
  110. $this->assertEquals('493046022100a9d501a6f59c45a24e65e5030903cfd80ba33910f24d6a505961d64fa5042b4f02210089fa7cc00ab2b5fc15499fa259a057e6d0911d4e849f1720cc6bc58e941fe7e20141041a2756dd506e45a1142c7f7f03ae9d3d9954f8543f4c3ca56f025df66f1afcba6086cec8d4135cbb5f5f1d731f25ba0884fc06945c9bbf69b9b543ca91866e79', $script->getHex());
  111. }
  112. }