Skip to content

Commit

Permalink
Update Ruby tests
Browse files Browse the repository at this point in the history
  • Loading branch information
azuchi committed Aug 27, 2017
1 parent eb3de54 commit 6af00f4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
2 changes: 2 additions & 0 deletions ref/ruby/bech32.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def encode(hrp, data)

# Decode a Bech32 string and determine hrp and data
def decode(bech)
# check invalid bytes
return nil if bech.scrub('?').include?('?')
# check uppercase/lowercase
return nil if (bech.downcase != bech && bech.upcase != bech)
bech.each_char{|c|return nil if c.ord < 33 || c.ord > 126}
Expand Down
2 changes: 1 addition & 1 deletion ref/ruby/segwit_addr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def addr

def parse_addr(addr)
hrp, data = Bech32.decode(addr)
raise 'Invalid address.' if hrp.nil? || (hrp != 'bc' && hrp != 'tb')
raise 'Invalid address.' if hrp.nil? || data[0].nil? || (hrp != 'bc' && hrp != 'tb')
ver = data[0]
raise 'Invalid witness version' if ver > 16
prog = convert_bits(data[1..-1], 5, 8, false)
Expand Down
25 changes: 22 additions & 3 deletions ref/ruby/test_bech32.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ class TestBech32 < Test::Unit::TestCase
"split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w",
]

INVALID_CHECKSUM = [
" 1nwldj5",
"\x7F" + "1axkwrx",
"an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx",
"pzry9x0s0muk",
"1pzry9x0s0muk",
"x1b4n0q5v",
"li1dgmt3",
"de1lg7wt\xff",
]

VALID_ADDRESS = [
["BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4", "0014751e76e8199196d454941c45d1b3a323f1433bd6"],
["tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7",
Expand All @@ -52,11 +63,12 @@ class TestBech32 < Test::Unit::TestCase
"bc10w508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kw5rljs90",
"BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P",
"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sL5k7",
"tb1pw508d6qejxtdg4y5r3zarqfsj6c3",
"bc1zw508d6qejxtdg4y5r3zarvaryvqyzf3du",
"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3pjxtptv",
"bc1gmk9yu",
]

def test_verify_checksum
def test_valid_checksum
VALID_CHECKSUM.each do |bech|
hrp, _ = Bech32.decode(bech)
assert_not_nil (hrp)
Expand All @@ -67,6 +79,13 @@ def test_verify_checksum
end
end

def test_invalid_checksum
INVALID_CHECKSUM.each do |bech|
hrp, _ = Bech32.decode(bech)
assert_nil (hrp)
end
end

def test_valid_address
VALID_ADDRESS.each do |addr, hex|
segwit_addr = SegwitAddr.new(addr)
Expand Down

0 comments on commit 6af00f4

Please sign in to comment.