DecodeTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace BitWasp\Test\Unit\Bech32;
  3. use BitWasp\Bech32\Exception\Bech32Exception;
  4. use BitWasp\Test\Bech32\TestCase;
  5. class DecodeTest extends TestCase
  6. {
  7. public function failedDecodeFixtures()
  8. {
  9. return [
  10. ["\x201nwldj5", "Out of range character in bech32 string"],
  11. ["\x7f1axkwrx", "Out of range character in bech32 string"],
  12. ["\x801eym55h", "Out of range character in bech32 string"],
  13. ["an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx", "Bech32 string cannot exceed 90 characters in length"],
  14. ["10a06t8", "Bech32 string is too short"],
  15. ["1qzzfhee", "Empty HRP"],
  16. ["pzry9x0s0muk", "Missing separator character"],
  17. ["1pzry9x0s0muk", "Empty HRP"],
  18. ["x1b4n0q5v", "Invalid bech32 checksum"],
  19. ["de1lg7wt\xff", "Out of range character in bech32 string"],
  20. ["A1G7SGD8", "Invalid bech32 checksum"],
  21. ["li1dgmt3", "Too short checksum"],
  22. ["bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5", "Invalid bech32 checksum"],
  23. ];
  24. }
  25. /**
  26. * @param string $bech32
  27. * @param string $exceptionMsg
  28. * @dataProvider failedDecodeFixtures
  29. */
  30. public function testDecodeFails($bech32, $exceptionMsg)
  31. {
  32. $this->expectException(Bech32Exception::class);
  33. $this->expectExceptionMessage($exceptionMsg);
  34. \BitWasp\Bech32\decode($bech32);
  35. }
  36. /**
  37. * @return array
  38. */
  39. public function validChecksumProvider()
  40. {
  41. return [
  42. ["A12UEL5L"],
  43. ["a12uel5l"],
  44. ["an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs"],
  45. ["abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw"],
  46. ["11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j"],
  47. ["split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w"],
  48. ["?1ezyfcl"],
  49. ];
  50. }
  51. /**
  52. * https://github.com/sipa/bech32/blob/master/ref/python/tests.py#L90
  53. * @param string $hasValidChecksum
  54. * @dataProvider validChecksumProvider
  55. */
  56. public function testValidChecksum($hasValidChecksum)
  57. {
  58. \BitWasp\Bech32\decode($hasValidChecksum);
  59. $pos = strrpos($hasValidChecksum, "1");
  60. $invalidChecksum = substr($hasValidChecksum, 0, $pos+1) . chr(ord($hasValidChecksum[$pos+1])^1) . substr($hasValidChecksum, $pos+2);
  61. $this->expectException(Bech32Exception::class);
  62. \BitWasp\Bech32\decode($invalidChecksum);
  63. }
  64. /**
  65. * @return array
  66. */
  67. public function invalidChecksumProvider()
  68. {
  69. return [
  70. [" 1nwldj5"],
  71. ["\x7f"."1axkwrx"],
  72. ["an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx"],
  73. ["pzry9x0s0muk"],
  74. ["1pzry9x0s0muk"],
  75. ["x1b4n0q5v"],
  76. ["li1dgmt3"],
  77. ["de1lg7wt"."\xff"],
  78. ];
  79. }
  80. /**
  81. * https://github.com/sipa/bech32/blob/master/ref/python/tests.py#L100
  82. * @param string $hasValidChecksum
  83. * @dataProvider invalidChecksumProvider
  84. */
  85. public function testInvalidChecksum($hasValidChecksum)
  86. {
  87. $this->expectException(Bech32Exception::class);
  88. \BitWasp\Bech32\decode($hasValidChecksum);
  89. }
  90. }