Skip to content

Commit 16d4fce

Browse files
committed
Add assert_is_hex_string and assert_is_hash_string to RPC test utils.
1 parent 9ee02cf commit 16d4fce

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

qa/rpc-tests/test_framework/util.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,5 +407,22 @@ def assert_raises(exc, fun, *args, **kwds):
407407
else:
408408
raise AssertionError("No exception raised")
409409

410+
def assert_is_hex_string(string):
411+
try:
412+
int(string, 16)
413+
except Exception as e:
414+
raise AssertionError(
415+
"Couldn't interpret %r as hexadecimal; raised: %s" % (string, e))
416+
417+
def assert_is_hash_string(string, length=64):
418+
if not isinstance(string, basestring):
419+
raise AssertionError("Expected a string, got type %r" % type(string))
420+
elif length and len(string) != length:
421+
raise AssertionError(
422+
"String of length %d expected; got %d" % (length, len(string)))
423+
elif not re.match('[abcdef0-9]+$', string):
424+
raise AssertionError(
425+
"String %r contains invalid characters for a hash." % string)
426+
410427
def satoshi_round(amount):
411428
return Decimal(amount).quantize(Decimal('0.00000001'), rounding=ROUND_DOWN)

0 commit comments

Comments
 (0)