RLPTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace Test\Unit;
  3. use Test\TestCase;
  4. use Web3p\RLP\Types\Str;
  5. class RLPTest extends TestCase
  6. {
  7. /**
  8. * $testCases for rlp
  9. *
  10. * @var array
  11. */
  12. protected $testCases = [
  13. [
  14. "decoded" => ["dog", "god", "cat"],
  15. "encoded" => "cc83646f6783676f6483636174",
  16. "rlpdecoded" => [
  17. "646f67", "676f64", "636174"
  18. ]
  19. ], [
  20. "decoded" => ["0xabcd", "0xdeff", "0xaaaa"],
  21. "encoded" => "c982abcd82deff82aaaa",
  22. "rlpdecoded" => ["abcd", "deff", "aaaa"]
  23. ], [
  24. "decoded" => 0,
  25. "encoded" => "80",
  26. "rlpdecoded" => ""
  27. ], [
  28. "decoded" => [],
  29. "encoded" => "c0",
  30. "rlpdecoded" => []
  31. ], [
  32. "decoded" => "0x00",
  33. "encoded" => "00",
  34. "rlpdecoded" => "00"
  35. ], [
  36. "decoded" => "0x0400",
  37. "encoded" => "820400",
  38. "rlpdecoded" => "0400"
  39. ], [
  40. "decoded" => [[], [[]], [ [], [[]] ]],
  41. "encoded" => "c7c0c1c0c3c0c1c0",
  42. "rlpdecoded" => [[], [[]], [ [], [[]] ]]
  43. ]
  44. ];
  45. /**
  46. * testEncode
  47. *
  48. * @return void
  49. */
  50. public function testEncode()
  51. {
  52. $rlp = $this->rlp;
  53. foreach ($this->testCases as $testCase) {
  54. $encoded = $rlp->encode($testCase["decoded"]);
  55. $this->assertEquals($testCase["encoded"], $encoded);
  56. }
  57. }
  58. /**
  59. * testDecode
  60. *
  61. * @return void
  62. */
  63. public function testDecode()
  64. {
  65. $rlp = $this->rlp;
  66. foreach ($this->testCases as $testCase) {
  67. $decoded = $rlp->decode("0x" . $testCase["encoded"]);
  68. $this->assertEquals($testCase["rlpdecoded"], $decoded);
  69. }
  70. }
  71. /**
  72. * testValidRlp
  73. *
  74. * @return void
  75. */
  76. public function testValidRlp()
  77. {
  78. $rlp = $this->rlp;
  79. $rlptestJson = file_get_contents(sprintf("%s%srlptest.json", __DIR__, DIRECTORY_SEPARATOR));
  80. $this->assertTrue($rlptestJson !== false);
  81. $rlptest = json_decode($rlptestJson, true);
  82. foreach ($rlptest as $test) {
  83. $encoded = $rlp->encode($test["in"]);
  84. $this->assertEquals($test["out"], $encoded);
  85. }
  86. }
  87. /**
  88. * testIssue14
  89. * See: https://github.com/web3p/rlp/issues/14
  90. * You can find test in: https://github.com/ethereum/wiki/wiki/RLP#examples
  91. *
  92. * @return void
  93. */
  94. public function testIssue14()
  95. {
  96. $rlp = $this->rlp;
  97. $this->assertEquals("c0", $rlp->encode([]));
  98. $this->assertEquals("80", $rlp->encode(0));
  99. $this->assertEquals("80", $rlp->encode(0x0));
  100. $this->assertEquals("80", $rlp->encode(-1));
  101. $this->assertEquals("80", $rlp->encode(-2));
  102. $this->assertEquals("30", $rlp->encode("0"));
  103. $this->assertEquals("00", $rlp->encode("0x0"));
  104. $this->assertEquals("80", $rlp->encode(null));
  105. }
  106. /**
  107. * testInvalidRlp
  108. * Try to figure out what invalidrlptest.json is.
  109. *
  110. * @return void
  111. */
  112. // public function testInvalidRlp()
  113. // {
  114. // $rlp = $this->rlp;
  115. // $invalidrlptestJson = file_get_contents(sprintf("%s%sinvalidrlptest.json", __DIR__, DIRECTORY_SEPARATOR));
  116. // $this->assertTrue($invalidrlptestJson !== false);
  117. // $invalidrlptest = json_decode($invalidrlptestJson, true);
  118. // foreach ($invalidrlptest as $test) {
  119. // $encoded = $rlp->encode($test["in"]);
  120. // $this->assertEquals($test["out"], $encoded);
  121. // }
  122. // }
  123. }