Skip to content

Commit

Permalink
unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ahangsu committed Aug 23, 2022
1 parent 50621ed commit e96fc81
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .test-env
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Configs for testing repo download:
SDK_TESTING_URL="https://github.com/algorand/algorand-sdk-testing"
SDK_TESTING_BRANCH="master"
# TODO revert back to "master" after sdk-testing is merged
SDK_TESTING_BRANCH="deprecate-langspec"
SDK_TESTING_HARNESS="test-harness"

VERBOSE_HARNESS=0
Expand Down
3 changes: 2 additions & 1 deletion algosdk/future/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2505,8 +2505,9 @@ class LogicSig:
"""

def __init__(self, program, args=None):
if not program or not logic.sanity_check_program(program):
if not program:
raise error.InvalidProgram()
logic.sanity_check_program(program)
self.logic = program
self.args = args
self.sig = None
Expand Down
44 changes: 27 additions & 17 deletions algosdk/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,34 @@


def sanity_check_program(program):
if not program:
raise error.InvalidProgram("empty program")
def is_ascii_printable(program_bytes):
return all(
map(
lambda x: x == ord("\n") or (ord(" ") <= x <= ord("~")),
program_bytes,
)
)

if is_ascii_printable(program):
try:
encoding.decode_address(program.decode("utf-8"))
raise error.InvalidProgram(
"requesting program bytes, get Algorand address"
)
except error.WrongChecksumError:
pass
except error.WrongKeyLengthError:
pass

try:
base64.b64decode(program.decode("utf-8"))
raise error.InvalidProgram("program should not be b64 encoded")
except binascii.Error:
pass

try:
if base64.encodebytes(base64.b64decode(str(program))) == str(program):
return False
except binascii.Error:
pass

try:
encoding.decode_address(str(program))
return False
except error.WrongChecksumError:
pass
except error.WrongKeyLengthError:
pass

return True
raise error.InvalidProgram(
"program bytes are all ASCII printable characters, not looking like Teal byte code"
)


def check_program(program, args=None):
Expand Down
3 changes: 2 additions & 1 deletion algosdk/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1318,8 +1318,9 @@ class LogicSig:
"""

def __init__(self, program, args=None):
if not program or not logic.sanity_check_program(program):
if not program:
raise error.InvalidProgram()
logic.sanity_check_program(program)
self.logic = program
self.args = args
self.sig = None
Expand Down
39 changes: 38 additions & 1 deletion tests/steps/steps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import base64
import random
import time
import parse

from algosdk import (
account,
Expand All @@ -14,9 +15,18 @@
wallet,
)
from algosdk.future import transaction
from behave import given, then, when
from behave import given, then, when, register_type
from nacl.signing import SigningKey


@parse.with_pattern(r".*")
def parse_string(text):
return text


register_type(MaybeString=parse_string)


token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
algod_port = 60000
kmd_port = 60001
Expand Down Expand Up @@ -984,3 +994,30 @@ def buildTxn(t, sender, params):
elif "nonparticipation" in t:
txn = transaction.KeyregNonparticipatingTxn(sender, params)
return txn


@given(
'a base64 encoded program bytes for heuristic sanity check "{b64encoded}"'
)
def take_b64_encoded_bytes(context, b64encoded):
context.seemingly_program = base64.b64decode(b64encoded)


@when("I start heuristic sanity check over the bytes")
def heuristic_check_over_bytes(context):
context.sanity_check_err = ""

try:
logic.sanity_check_program(context.seemingly_program)
except Exception as e:
print(e)
context.sanity_check_err = str(e)


@then('if there exists an error, the error contains "{err_msg:MaybeString}"')
def check_error_if_matching(context, err_msg: str = None):
print(err_msg)
if len(err_msg) > 0:
assert err_msg in context.sanity_check_err
else:
assert len(context.sanity_check_err) == 0
3 changes: 2 additions & 1 deletion tests/unit.tags
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@unit.indexer.ledger_refactoring
@unit.indexer.logs
@unit.offline
@unit.program_sanity_check
@unit.rekey
@unit.responses
@unit.responses.231
Expand All @@ -20,4 +21,4 @@
@unit.tealsign
@unit.transactions
@unit.transactions.keyreg
@unit.transactions.payment
@unit.transactions.payment

0 comments on commit e96fc81

Please sign in to comment.