PageRenderTime 45ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/test/functional/rpc_invalid_address_message.py

https://github.com/bitcoin/bitcoin
Python | 115 lines | 111 code | 0 blank | 4 comment | 6 complexity | b1075d281538b0e37847bb81ca1d46fe MD5 | raw file
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2020-2021 The Bitcoin Core developers
  3. # Distributed under the MIT software license, see the accompanying
  4. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. """Test error messages for 'getaddressinfo' and 'validateaddress' RPC commands."""
  6. from test_framework.test_framework import BitcoinTestFramework
  7. from test_framework.util import (
  8. assert_equal,
  9. assert_raises_rpc_error,
  10. )
  11. BECH32_VALID = 'bcrt1qtmp74ayg7p24uslctssvjm06q5phz4yrxucgnv'
  12. BECH32_VALID_CAPITALS = 'BCRT1QPLMTZKC2XHARPPZDLNPAQL78RSHJ68U33RAH7R'
  13. BECH32_VALID_MULTISIG = 'bcrt1qdg3myrgvzw7ml9q0ejxhlkyxm7vl9r56yzkfgvzclrf4hkpx9yfqhpsuks'
  14. BECH32_INVALID_BECH32 = 'bcrt1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqdmchcc'
  15. BECH32_INVALID_BECH32M = 'bcrt1qw508d6qejxtdg4y5r3zarvary0c5xw7k35mrzd'
  16. BECH32_INVALID_VERSION = 'bcrt130xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqynjegk'
  17. BECH32_INVALID_SIZE = 'bcrt1s0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v8n0nx0muaewav25430mtr'
  18. BECH32_INVALID_V0_SIZE = 'bcrt1qw508d6qejxtdg4y5r3zarvary0c5xw7kqqq5k3my'
  19. BECH32_INVALID_PREFIX = 'bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx'
  20. BECH32_TOO_LONG = 'bcrt1q049edschfnwystcqnsvyfpj23mpsg3jcedq9xv049edschfnwystcqnsvyfpj23mpsg3jcedq9xv049edschfnwystcqnsvyfpj23m'
  21. BECH32_ONE_ERROR = 'bcrt1q049edschfnwystcqnsvyfpj23mpsg3jcedq9xv'
  22. BECH32_ONE_ERROR_CAPITALS = 'BCRT1QPLMTZKC2XHARPPZDLNPAQL78RSHJ68U32RAH7R'
  23. BECH32_TWO_ERRORS = 'bcrt1qax9suht3qv95sw33xavx8crpxduefdrsvgsklu' # should be bcrt1qax9suht3qv95sw33wavx8crpxduefdrsvgsklx
  24. BECH32_NO_SEPARATOR = 'bcrtq049ldschfnwystcqnsvyfpj23mpsg3jcedq9xv'
  25. BECH32_INVALID_CHAR = 'bcrt1q04oldschfnwystcqnsvyfpj23mpsg3jcedq9xv'
  26. BECH32_MULTISIG_TWO_ERRORS = 'bcrt1qdg3myrgvzw7ml8q0ejxhlkyxn7vl9r56yzkfgvzclrf4hkpx9yfqhpsuks'
  27. BECH32_WRONG_VERSION = 'bcrt1ptmp74ayg7p24uslctssvjm06q5phz4yrxucgnv'
  28. BASE58_VALID = 'mipcBbFg9gMiCh81Kj8tqqdgoZub1ZJRfn'
  29. BASE58_INVALID_PREFIX = '17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhem'
  30. BASE58_INVALID_CHECKSUM = 'mipcBbFg9gMiCh81Kj8tqqdgoZub1ZJJfn'
  31. BASE58_INVALID_LENGTH = '2VKf7XKMrp4bVNVmuRbyCewkP8FhGLP2E54LHDPakr9Sq5mtU2'
  32. INVALID_ADDRESS = 'asfah14i8fajz0123f'
  33. INVALID_ADDRESS_2 = '1q049ldschfnwystcqnsvyfpj23mpsg3jcedq9xv'
  34. class InvalidAddressErrorMessageTest(BitcoinTestFramework):
  35. def set_test_params(self):
  36. self.setup_clean_chain = True
  37. self.num_nodes = 1
  38. def check_valid(self, addr):
  39. info = self.nodes[0].validateaddress(addr)
  40. assert info['isvalid']
  41. assert 'error' not in info
  42. assert 'error_locations' not in info
  43. def check_invalid(self, addr, error_str, error_locations=None):
  44. res = self.nodes[0].validateaddress(addr)
  45. assert not res['isvalid']
  46. assert_equal(res['error'], error_str)
  47. if error_locations:
  48. assert_equal(res['error_locations'], error_locations)
  49. else:
  50. assert_equal(res['error_locations'], [])
  51. def test_validateaddress(self):
  52. # Invalid Bech32
  53. self.check_invalid(BECH32_INVALID_SIZE, 'Invalid Bech32 address data size')
  54. self.check_invalid(BECH32_INVALID_PREFIX, 'Not a valid Bech32 or Base58 encoding')
  55. self.check_invalid(BECH32_INVALID_BECH32, 'Version 1+ witness address must use Bech32m checksum')
  56. self.check_invalid(BECH32_INVALID_BECH32M, 'Version 0 witness address must use Bech32 checksum')
  57. self.check_invalid(BECH32_INVALID_VERSION, 'Invalid Bech32 address witness version')
  58. self.check_invalid(BECH32_INVALID_V0_SIZE, 'Invalid Bech32 v0 address data size')
  59. self.check_invalid(BECH32_TOO_LONG, 'Bech32 string too long', list(range(90, 108)))
  60. self.check_invalid(BECH32_ONE_ERROR, 'Invalid Bech32 checksum', [9])
  61. self.check_invalid(BECH32_TWO_ERRORS, 'Invalid Bech32 checksum', [22, 43])
  62. self.check_invalid(BECH32_ONE_ERROR_CAPITALS, 'Invalid Bech32 checksum', [38])
  63. self.check_invalid(BECH32_NO_SEPARATOR, 'Missing separator')
  64. self.check_invalid(BECH32_INVALID_CHAR, 'Invalid Base 32 character', [8])
  65. self.check_invalid(BECH32_MULTISIG_TWO_ERRORS, 'Invalid Bech32 checksum', [19, 30])
  66. self.check_invalid(BECH32_WRONG_VERSION, 'Invalid Bech32 checksum', [5])
  67. # Valid Bech32
  68. self.check_valid(BECH32_VALID)
  69. self.check_valid(BECH32_VALID_CAPITALS)
  70. self.check_valid(BECH32_VALID_MULTISIG)
  71. # Invalid Base58
  72. self.check_invalid(BASE58_INVALID_PREFIX, 'Invalid prefix for Base58-encoded address')
  73. self.check_invalid(BASE58_INVALID_CHECKSUM, 'Invalid checksum or length of Base58 address')
  74. self.check_invalid(BASE58_INVALID_LENGTH, 'Invalid checksum or length of Base58 address')
  75. # Valid Base58
  76. self.check_valid(BASE58_VALID)
  77. # Invalid address format
  78. self.check_invalid(INVALID_ADDRESS, 'Not a valid Bech32 or Base58 encoding')
  79. self.check_invalid(INVALID_ADDRESS_2, 'Not a valid Bech32 or Base58 encoding')
  80. def test_getaddressinfo(self):
  81. node = self.nodes[0]
  82. assert_raises_rpc_error(-5, "Invalid Bech32 address data size", node.getaddressinfo, BECH32_INVALID_SIZE)
  83. assert_raises_rpc_error(-5, "Not a valid Bech32 or Base58 encoding", node.getaddressinfo, BECH32_INVALID_PREFIX)
  84. assert_raises_rpc_error(-5, "Invalid prefix for Base58-encoded address", node.getaddressinfo, BASE58_INVALID_PREFIX)
  85. assert_raises_rpc_error(-5, "Not a valid Bech32 or Base58 encoding", node.getaddressinfo, INVALID_ADDRESS)
  86. def run_test(self):
  87. self.test_validateaddress()
  88. if self.is_wallet_compiled():
  89. self.init_wallet(node=0)
  90. self.test_getaddressinfo()
  91. if __name__ == '__main__':
  92. InvalidAddressErrorMessageTest().main()