Skip to content

Commit

Permalink
add test to int2base36 used in BBQr
Browse files Browse the repository at this point in the history
  • Loading branch information
odudex committed Jun 17, 2024
1 parent 5adbef6 commit 5b6f9d6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
13 changes: 13 additions & 0 deletions src/krux/bbqr.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,16 @@ def base32_encode_stream(data, add_padding=False):
padding_length = (8 - (encoded_length % 8)) % 8
for _ in range(padding_length):
yield "="


def int2base36(n):
"""Convert integer n to a base36 string."""
if not 0 <= n <= 1295: # ensure the number is within the valid range
raise ValueError("Number out of range")

def tostr(x):
"""Convert integer x to a base36 character."""
return chr(48 + x) if x < 10 else chr(65 + x - 10)

quotient, remainder = divmod(n, 36)
return tostr(quotient) + tostr(remainder)
15 changes: 2 additions & 13 deletions src/krux/qr.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,6 @@ def result(self):
return code


def int2base36(n):
"""Convert integer n to a base36 string."""
if not 0 <= n <= 1295: # ensure the number is within the valid range
raise ValueError("Number out of range")

def tostr(x):
"""Convert integer x to a base36 character."""
return chr(48 + x) if x < 10 else chr(65 + x - 10)

quotient, remainder = divmod(n, 36)
return tostr(quotient) + tostr(remainder)


def to_qr_codes(data, max_width, qr_format):
"""Returns the list of QR codes necessary to represent the data in the qr format, given
the max_width constraint
Expand Down Expand Up @@ -240,6 +227,8 @@ def to_qr_codes(data, max_width, qr_format):
code = qrcode.encode(part)
yield (code, encoder.fountain_encoder.seq_len())
elif qr_format == FORMAT_BBQR:
from .bbqr import int2base36

part_index = 0
while True:
header = "B$%s%s%s%s" % (
Expand Down
6 changes: 4 additions & 2 deletions tests/pages/home_pages/test_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ def test_change_passphrase_menu(mocker, amigo, tdata):
assert wallet.key.fingerprint != ctx.wallet.key.fingerprint
assert ctx.wallet.key.fingerprint_hex_str() == FINGERPRINT_WITH_PASSPHRASE


def test_customize_wallet_menu(mocker, amigo, tdata):
from krux.pages.home_pages.home import Home
from krux.wallet import Wallet
Expand All @@ -230,13 +231,14 @@ def test_customize_wallet_menu(mocker, amigo, tdata):
assert ctx.input.wait_for_button.call_count == len(BTN_SEQUENCE)
assert ctx.wallet.key.network["name"] == "Testnet"


def test_load_bip85_from_wallet_menu(mocker, amigo, tdata):
from krux.pages.home_pages.home import Home
from krux.wallet import Wallet
from krux.input import BUTTON_ENTER, BUTTON_PAGE, BUTTON_PAGE_PREV

INDEX_0_B85_FINGERPRINT = "02e8bff2"

BTN_SEQUENCE = [
*([BUTTON_PAGE] * 3), # Go to BIP85
BUTTON_ENTER, # Enter BIP85
Expand All @@ -246,7 +248,7 @@ def test_load_bip85_from_wallet_menu(mocker, amigo, tdata):
BUTTON_PAGE_PREV, # Move to "Go"
BUTTON_ENTER, # Go
BUTTON_ENTER, # Load words
BUTTON_PAGE, # Move to "Back"
BUTTON_PAGE, # Move to "Back"
BUTTON_ENTER, # Exit
]

Expand Down
19 changes: 19 additions & 0 deletions tests/test_bbqr.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,22 @@ def test_encode_bbqr_always_compress_large_data(m5stickv):
bbqr_code = encode_bbqr(BBQR_CONTENT.encode("utf-8"), file_type="U")
assert bbqr_code.encoding == "Z"
assert bbqr_code.file_type == "U"


def test_int2base36():
from krux.bbqr import int2base36

# Test cases for valid inputs
assert int2base36(0) == "00"
assert int2base36(1) == "01"
assert int2base36(9) == "09"
assert int2base36(10) == "0A"
assert int2base36(35) == "0Z"
assert int2base36(36) == "10"
assert int2base36(1295) == "ZZ"

# Test cases for invalid inputs
with pytest.raises(ValueError):
int2base36(-1)
with pytest.raises(ValueError):
int2base36(1296)

0 comments on commit 5b6f9d6

Please sign in to comment.