Skip to content

Commit

Permalink
Parametrise a few tests over alphabet
Browse files Browse the repository at this point in the history
  • Loading branch information
keis committed Jan 9, 2021
1 parent 3eea01a commit efa4642
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 61 deletions.
45 changes: 1 addition & 44 deletions test_base45.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from itertools import product
from hamcrest import assert_that, equal_to, calling, raises
from base58 import (
b58encode, b58decode, b58encode_check, b58decode_check, b58encode_int,
b58decode_int)
from base58 import (b58encode, b58decode, b58encode_check, b58decode_check)

BASE45_ALPHABET = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"

Expand Down Expand Up @@ -58,13 +55,6 @@ def test_empty_decode_bytes():
assert_that(data, equal_to(b'\x01'))


def test_check_identity():
data = b'hello world'
out = b58decode_check(b58encode_check(data, alphabet=BASE45_ALPHABET),
alphabet=BASE45_ALPHABET)
assert_that(out, equal_to(data))


def test_check_str():
data = 'hello world'
out = b58encode_check(data, alphabet=BASE45_ALPHABET)
Expand Down Expand Up @@ -93,39 +83,6 @@ def test_check_failure():
assert_that(calling(b58decode_check).with_args(data), raises(ValueError))


def test_round_trips():
possible_bytes = [b'\x00', b'\x01', b'\x10', b'\xff']
for length in range(0, 5):
for bytes_to_test in product(possible_bytes, repeat=length):
bytes_in = b''.join(bytes_to_test)
bytes_out = b58decode(
b58encode(bytes_in, alphabet=BASE45_ALPHABET),
alphabet=BASE45_ALPHABET)
assert_that(bytes_in, equal_to(bytes_out))


# invmap is only required for base58 conversion.
def test_simple_integers():
for idx, char in enumerate(BASE45_ALPHABET):
charbytes = bytes([char])
assert_that(
b58decode_int(charbytes, alphabet=BASE45_ALPHABET),
equal_to(idx))
assert_that(
b58encode_int(idx, alphabet=BASE45_ALPHABET),
equal_to(charbytes))


def test_large_integer():
number = 128132047753791913995170032229035480239943482175957729505020971140585654 # noqa
assert_that(
b58decode_int(BASE45_ALPHABET, alphabet=BASE45_ALPHABET),
equal_to(number))
assert_that(
b58encode_int(number, alphabet=BASE45_ALPHABET),
equal_to(BASE45_ALPHABET[1:]))


def test_invalid_input():
data = 'xyz0' # 0 is not part of the bitcoin base58 alphabet
assert_that(
Expand Down
45 changes: 28 additions & 17 deletions test_base58.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@
from hamcrest import assert_that, equal_to, calling, raises
from base58 import (
b58encode, b58decode, b58encode_check, b58decode_check, b58encode_int,
b58decode_int, BITCOIN_ALPHABET, alphabet)
b58decode_int,
BITCOIN_ALPHABET,
XRP_ALPHABET)


BASE45_ALPHABET = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"


@pytest.fixture(params=[BITCOIN_ALPHABET, XRP_ALPHABET, BASE45_ALPHABET])
def alphabet(request) -> str:
return request.param


def test_simple_encode():
Expand Down Expand Up @@ -57,12 +67,6 @@ def test_empty_decode_bytes():
assert_that(data, equal_to(b'\0'))


def test_check_identity():
data = b'hello world'
out = b58decode_check(b58encode_check(data))
assert_that(out, equal_to(data))


def test_check_str():
data = 'hello world'
out = b58encode_check(data)
Expand Down Expand Up @@ -90,20 +94,31 @@ def test_check_failure():
assert_that(calling(b58decode_check).with_args(data), raises(ValueError))


def test_round_trips():
def test_check_identity(alphabet):
data = b'hello world'
out = b58decode_check(
b58encode_check(data, alphabet=alphabet),
alphabet=alphabet
)
assert_that(out, equal_to(data))


def test_round_trips(alphabet):
possible_bytes = [b'\x00', b'\x01', b'\x10', b'\xff']
for length in range(0, 5):
for bytes_to_test in product(possible_bytes, repeat=length):
bytes_in = b''.join(bytes_to_test)
bytes_out = b58decode(b58encode(bytes_in))
bytes_out = b58decode(
b58encode(bytes_in, alphabet=alphabet),
alphabet=alphabet)
assert_that(bytes_in, equal_to(bytes_out))


def test_simple_integers():
for idx, char in enumerate(BITCOIN_ALPHABET):
def test_simple_integers(alphabet):
for idx, char in enumerate(alphabet):
charbytes = bytes([char])
assert_that(b58decode_int(charbytes), equal_to(idx))
assert_that(b58encode_int(idx), equal_to(charbytes))
assert_that(b58decode_int(charbytes, alphabet=alphabet), equal_to(idx))
assert_that(b58encode_int(idx, alphabet=alphabet), equal_to(charbytes))


def test_large_integer():
Expand All @@ -112,10 +127,6 @@ def test_large_integer():
assert_that(b58encode_int(number), equal_to(BITCOIN_ALPHABET[1:]))


def test_alphabet_alias_exists_and_equals_bitcoin_alphabet():
assert_that(alphabet, equal_to(BITCOIN_ALPHABET))


def test_invalid_input():
data = 'xyz0' # 0 is not part of the bitcoin base58 alphabet
assert_that(
Expand Down

0 comments on commit efa4642

Please sign in to comment.