fix test_msg.py
This commit is contained in:
parent
3a04b50051
commit
bd05d2f4c0
@ -10,17 +10,18 @@ from base64 import b64encode, b64decode
|
||||
from ckcc_protocol.protocol import CCProtocolPacker, CCProtoError, CCUserRefused
|
||||
from ckcc_protocol.constants import *
|
||||
from constants import addr_fmt_names, msg_sign_unmap_addr_fmt
|
||||
from charcodes import KEY_ENTER, KEY_CANCEL, KEY_NFC
|
||||
|
||||
|
||||
@pytest.mark.parametrize('msg', [ 'aZ', 'hello', 'abc def eght', "x"*140, 'a'*240])
|
||||
@pytest.mark.parametrize('path', [ 'm', "m/1/2", "m/1'/100'", 'm/23H/22p'])
|
||||
@pytest.mark.parametrize('addr_fmt', [ AF_CLASSIC, AF_P2WPKH, AF_P2WPKH_P2SH ])
|
||||
def test_sign_msg_good(dev, need_keypress, msg, path, addr_fmt, addr_vs_path):
|
||||
def test_sign_msg_good(dev, need_keypress, msg, path, addr_fmt, addr_vs_path, is_q1):
|
||||
|
||||
msg = msg.encode('ascii')
|
||||
dev.send_recv(CCProtocolPacker.sign_message(msg, path, addr_fmt=addr_fmt), timeout=None)
|
||||
|
||||
need_keypress('y')
|
||||
need_keypress(KEY_ENTER if is_q1 else 'y')
|
||||
|
||||
done = None
|
||||
while done == None:
|
||||
@ -39,12 +40,14 @@ def test_sign_msg_good(dev, need_keypress, msg, path, addr_fmt, addr_vs_path):
|
||||
assert verify_message(addr, sig, msg.decode("ascii")) is True
|
||||
|
||||
|
||||
def test_sign_msg_refused(dev, need_keypress, msg=b'testing 123', path='m'):
|
||||
def test_sign_msg_refused(dev, need_keypress, is_q1):
|
||||
# user can refuse to sign (cancel)
|
||||
|
||||
msg = b'testing 123'
|
||||
path = 'm'
|
||||
dev.send_recv(CCProtocolPacker.sign_message(msg, path), timeout=None)
|
||||
|
||||
need_keypress('x')
|
||||
need_keypress(KEY_CANCEL if is_q1 else 'x')
|
||||
|
||||
with pytest.raises(CCUserRefused):
|
||||
done = None
|
||||
@ -76,11 +79,13 @@ def test_bad_paths(dev, path, expect):
|
||||
assert expect in str(ee)
|
||||
|
||||
@pytest.fixture
|
||||
def sign_on_microsd(open_microsd, cap_story, pick_menu_item, goto_home, need_keypress, microsd_path):
|
||||
def sign_on_microsd(open_microsd, cap_story, pick_menu_item, goto_home,
|
||||
need_keypress, microsd_path, is_q1):
|
||||
|
||||
# sign a file on the microSD card
|
||||
|
||||
def doit(msg, subpath=None, addr_fmt=None, expect_fail=False):
|
||||
confirm = KEY_ENTER if is_q1 else "y"
|
||||
fname = 't-msgsign.txt'
|
||||
result_fname = 't-msgsign-signed.txt'
|
||||
|
||||
@ -103,7 +108,7 @@ def sign_on_microsd(open_microsd, cap_story, pick_menu_item, goto_home, need_key
|
||||
time.sleep(.1)
|
||||
_, story = cap_story()
|
||||
assert 'Choose text file to be signed' in story
|
||||
need_keypress('y')
|
||||
need_keypress(confirm)
|
||||
time.sleep(.1)
|
||||
|
||||
try:
|
||||
@ -128,7 +133,7 @@ def sign_on_microsd(open_microsd, cap_story, pick_menu_item, goto_home, need_key
|
||||
x_subpath = subpath.lower().replace('p', "'").replace('h', "'")
|
||||
assert ('%s =>' % x_subpath) in story
|
||||
|
||||
need_keypress('y')
|
||||
need_keypress(confirm)
|
||||
|
||||
# wait for it to finish
|
||||
for r in range(10):
|
||||
@ -239,7 +244,8 @@ def test_sign_msg_fails(dev, sign_on_microsd, msg, concern, no_file, transport,
|
||||
('Test', 2, 'IDgMx1ljPhLHlKUOwnO/jBIgK+K8n8mvDUDROzTgU8gOaPDMs+eYXJpNXXINUx5WpeV605p5uO6B3TzBVcvs478='),
|
||||
('Test1', 3, 'IEt/v9K95YVFuRtRtWaabPVwWOFv1FSA/e874I8ABgYMbRyVvHhSwLFz0RZuO87ukxDd4TOsRdofQwMEA90LCgI='),
|
||||
])
|
||||
def test_low_R_cases(msg, num_iter, expect, dev, set_seed_words, use_mainnet, need_keypress):
|
||||
def test_low_R_cases(msg, num_iter, expect, dev, set_seed_words, use_mainnet,
|
||||
need_keypress, is_q1):
|
||||
# Thanks to @craigraw of Sparrow for this test case, copied from:
|
||||
# <https://github.com/sparrowwallet/drongo/blob/master/src/test/java/com/sparrowwallet/drongo/crypto/ECKeyTest.java>
|
||||
|
||||
@ -254,7 +260,7 @@ def test_low_R_cases(msg, num_iter, expect, dev, set_seed_words, use_mainnet, ne
|
||||
msg = msg.encode('ascii')
|
||||
dev.send_recv(CCProtocolPacker.sign_message(msg, path, addr_fmt=addr_fmt), timeout=None)
|
||||
|
||||
need_keypress('y')
|
||||
need_keypress(KEY_ENTER if is_q1 else 'y')
|
||||
|
||||
done = None
|
||||
while done == None:
|
||||
@ -302,7 +308,9 @@ def test_nfc_msg_signing_invalid(body, goto_home, pick_menu_item, nfc_write_text
|
||||
@pytest.mark.parametrize("path", ["", "m/84'/0'/0'/300/0", "m/800'", "m/0/0/0/0/1/1/1"])
|
||||
@pytest.mark.parametrize("str_addr_fmt", ["p2pkh", "", "p2wpkh", "p2wpkh-p2sh", "p2sh-p2wpkh"])
|
||||
def test_nfc_msg_signing(msg, path, str_addr_fmt, nfc_write_text, nfc_read_text, pick_menu_item,
|
||||
goto_home, cap_story, need_keypress, addr_vs_path):
|
||||
goto_home, cap_story, need_keypress, addr_vs_path, is_q1):
|
||||
confirm = KEY_ENTER if is_q1 else "y"
|
||||
cancel = KEY_CANCEL if is_q1 else "x"
|
||||
# import pdb;pdb.set_trace()
|
||||
for _ in range(5):
|
||||
# need to wait for ApproveMessageSign to be popped from ux stack
|
||||
@ -328,12 +336,12 @@ def test_nfc_msg_signing(msg, path, str_addr_fmt, nfc_write_text, nfc_read_text,
|
||||
assert "Ok to sign this?" in story
|
||||
assert msg in story
|
||||
assert path in story
|
||||
need_keypress("y")
|
||||
need_keypress(confirm)
|
||||
signed_msg = nfc_read_text()
|
||||
if "BITCOIN SIGNED MESSAGE" not in signed_msg:
|
||||
# missed it? again
|
||||
signed_msg = nfc_read_text()
|
||||
need_keypress("y") # exit NFC animation
|
||||
need_keypress(confirm) # exit NFC animation
|
||||
pmsg, addr, sig = parse_signed_message(signed_msg)
|
||||
assert pmsg == msg
|
||||
addr_vs_path(addr, path, addr_fmt)
|
||||
@ -341,14 +349,15 @@ def test_nfc_msg_signing(msg, path, str_addr_fmt, nfc_write_text, nfc_read_text,
|
||||
time.sleep(0.5)
|
||||
_, story = cap_story()
|
||||
assert "Press OK to share again" in story
|
||||
need_keypress("y")
|
||||
need_keypress(confirm)
|
||||
signed_msg_again = nfc_read_text()
|
||||
assert signed_msg == signed_msg_again
|
||||
need_keypress("x") # exit NFC animation
|
||||
need_keypress("x") # do not want to share again
|
||||
need_keypress(cancel) # exit NFC animation
|
||||
need_keypress(cancel) # do not want to share again
|
||||
|
||||
@pytest.fixture
|
||||
def verify_armored_signature(pick_menu_item, nfc_write_text, need_keypress, cap_story, goto_home):
|
||||
def verify_armored_signature(pick_menu_item, nfc_write_text, need_keypress,
|
||||
cap_story, goto_home, is_q1):
|
||||
def doit(way, fname=None, signed_msg=None):
|
||||
goto_home()
|
||||
pick_menu_item('Advanced/Tools')
|
||||
@ -363,7 +372,7 @@ def verify_armored_signature(pick_menu_item, nfc_write_text, need_keypress, cap_
|
||||
else:
|
||||
_, story = cap_story()
|
||||
assert 'Choose signature file.' in story
|
||||
need_keypress('y')
|
||||
need_keypress(KEY_ENTER if is_q1 else 'y')
|
||||
time.sleep(.1)
|
||||
pick_menu_item(fname)
|
||||
|
||||
@ -380,7 +389,7 @@ def verify_armored_signature(pick_menu_item, nfc_write_text, need_keypress, cap_
|
||||
"coldcard", "blablablablablablablabla", "morecornfor us", 240 * "a",
|
||||
))
|
||||
def test_verify_signature_file(way, addr_fmt, path, msg, sign_on_microsd, goto_home, pick_menu_item,
|
||||
need_keypress, cap_story, bitcoind, microsd_path, nfc_write_text,
|
||||
cap_story, bitcoind, microsd_path, nfc_write_text,
|
||||
verify_armored_signature):
|
||||
sig, addr = sign_on_microsd(msg, path, msg_sign_unmap_addr_fmt[addr_fmt])
|
||||
fname = 't-msgsign-signed.txt'
|
||||
@ -437,7 +446,7 @@ def test_verify_signature_file_header_warning(way, addr_sig, microsd_path, verif
|
||||
("bc1q2c8pym4m755pq4n4shu2wgzr7s58pygz8x6pg0mj0l6netq8am8qw69kss", "KPxCN2edt9w5ukd0feOlFS6PJjsKwm6ii/erZErKDIApIxjHqxBzoDvVqcTX0mtecNTGCkJPhxjRKCjNtdnTAp0=", 1),
|
||||
])
|
||||
def test_verify_signature_file_fail(way, addr_sig, microsd_path, cap_story, goto_home, nfc_write_text,
|
||||
pick_menu_item, need_keypress, verify_armored_signature):
|
||||
pick_menu_item, verify_armored_signature):
|
||||
fname = "fail-signed.txt"
|
||||
addr, sig, err_no = addr_sig
|
||||
|
||||
@ -464,7 +473,10 @@ def test_verify_signature_file_fail(way, addr_sig, microsd_path, cap_story, goto
|
||||
|
||||
@pytest.mark.parametrize("binary", [True, False])
|
||||
def test_verify_signature_file_digest_prob(binary, microsd_path, cap_story, pick_menu_item,
|
||||
need_keypress, goto_home):
|
||||
need_keypress, goto_home, is_q1):
|
||||
confirm = KEY_ENTER if is_q1 else "y"
|
||||
cancel = KEY_CANCEL if is_q1 else "x"
|
||||
|
||||
fpattern = "to_sign"
|
||||
if binary:
|
||||
suffix = ".pdf"
|
||||
@ -487,19 +499,19 @@ def test_verify_signature_file_digest_prob(binary, microsd_path, cap_story, pick
|
||||
pick_menu_item("Advanced/Tools")
|
||||
pick_menu_item("File Management")
|
||||
pick_menu_item("List Files")
|
||||
need_keypress("y")
|
||||
need_keypress(confirm)
|
||||
pick_menu_item(fname)
|
||||
need_keypress("4") # create detached sig
|
||||
need_keypress("y")
|
||||
need_keypress("x")
|
||||
need_keypress(confirm)
|
||||
need_keypress(cancel)
|
||||
pick_menu_item("Verify Sig File")
|
||||
need_keypress("y")
|
||||
need_keypress(confirm)
|
||||
pick_menu_item(sig_name)
|
||||
time.sleep(0.1)
|
||||
title, story = cap_story()
|
||||
assert title == "CORRECT"
|
||||
assert "Good signature" in story
|
||||
need_keypress("y") # back in File Management
|
||||
need_keypress(confirm) # back in File Management
|
||||
|
||||
# modify contents of the file
|
||||
with open(fpath, mode) as f:
|
||||
@ -508,7 +520,7 @@ def test_verify_signature_file_digest_prob(binary, microsd_path, cap_story, pick
|
||||
|
||||
mod_digest = hashlib.sha256(mod_contents if binary else mod_contents.encode()).digest().hex()
|
||||
pick_menu_item("Verify Sig File")
|
||||
need_keypress("y")
|
||||
need_keypress(confirm)
|
||||
pick_menu_item(sig_name)
|
||||
time.sleep(0.1)
|
||||
title, story = cap_story()
|
||||
@ -517,12 +529,12 @@ def test_verify_signature_file_digest_prob(binary, microsd_path, cap_story, pick
|
||||
assert ("'%s' has wrong contents" % fname) in story
|
||||
assert ("Got:\n%s" % orig_digest) in story
|
||||
assert ("Expected:\n%s" % mod_digest) in story
|
||||
need_keypress("y") # back in File Management
|
||||
need_keypress(confirm) # back in File Management
|
||||
|
||||
# remove file
|
||||
os.remove(fpath)
|
||||
pick_menu_item("Verify Sig File")
|
||||
need_keypress("y")
|
||||
need_keypress(confirm)
|
||||
pick_menu_item(sig_name)
|
||||
time.sleep(0.1)
|
||||
title, story = cap_story()
|
||||
@ -530,12 +542,13 @@ def test_verify_signature_file_digest_prob(binary, microsd_path, cap_story, pick
|
||||
assert "Good signature" in story # sig is still correct
|
||||
assert ("'%s' is not present" % fname) in story
|
||||
assert 'Contents verification not possible' in story
|
||||
need_keypress("y") # back in File Management
|
||||
need_keypress(confirm) # back in File Management
|
||||
|
||||
|
||||
@pytest.mark.parametrize("f_num", [2, 10, 20])
|
||||
def test_verify_signature_file_digest_prob_multi(f_num, microsd_path, cap_story, pick_menu_item,
|
||||
need_keypress, goto_home):
|
||||
need_keypress, goto_home, is_q1):
|
||||
confirm = KEY_ENTER if is_q1 else "y"
|
||||
files = []
|
||||
msg = ""
|
||||
for i in range(f_num):
|
||||
@ -567,13 +580,13 @@ def test_verify_signature_file_digest_prob_multi(f_num, microsd_path, cap_story,
|
||||
pick_menu_item("Advanced/Tools")
|
||||
pick_menu_item("File Management")
|
||||
pick_menu_item("Verify Sig File")
|
||||
need_keypress("y")
|
||||
need_keypress(confirm)
|
||||
pick_menu_item(sig_name)
|
||||
time.sleep(0.1)
|
||||
title, story = cap_story()
|
||||
assert title == "CORRECT"
|
||||
assert "Good signature" in story
|
||||
need_keypress("y") # back in File Management
|
||||
need_keypress(confirm) # back in File Management
|
||||
|
||||
# change contents of 0th file
|
||||
fname, orig_digest, fpath, _, _ = files[0]
|
||||
@ -583,7 +596,7 @@ def test_verify_signature_file_digest_prob_multi(f_num, microsd_path, cap_story,
|
||||
f.write(new_contetns)
|
||||
|
||||
pick_menu_item("Verify Sig File")
|
||||
need_keypress("y")
|
||||
need_keypress(confirm)
|
||||
pick_menu_item(sig_name)
|
||||
time.sleep(0.1)
|
||||
title, story = cap_story()
|
||||
@ -592,7 +605,7 @@ def test_verify_signature_file_digest_prob_multi(f_num, microsd_path, cap_story,
|
||||
assert ("'%s' has wrong contents" % fname) in story
|
||||
assert ("Got:\n%s" % orig_digest) in story
|
||||
assert ("Expected:\n%s" % mod_digest) in story
|
||||
need_keypress("y") # back in File Management
|
||||
need_keypress(confirm) # back in File Management
|
||||
|
||||
# change contents of 1st file remove 0th file
|
||||
# both warnings must be visible
|
||||
@ -605,7 +618,7 @@ def test_verify_signature_file_digest_prob_multi(f_num, microsd_path, cap_story,
|
||||
f.write(new_contetns)
|
||||
|
||||
pick_menu_item("Verify Sig File")
|
||||
need_keypress("y")
|
||||
need_keypress(confirm)
|
||||
pick_menu_item(sig_name)
|
||||
time.sleep(0.1)
|
||||
title, story = cap_story()
|
||||
@ -616,12 +629,12 @@ def test_verify_signature_file_digest_prob_multi(f_num, microsd_path, cap_story,
|
||||
assert ("Expected:\n%s" % mod_digest) in story
|
||||
assert ("'%s' is not present" % fname0) in story
|
||||
assert 'Contents verification not possible' in story
|
||||
need_keypress("y") # back in File Management
|
||||
need_keypress(confirm) # back in File Management
|
||||
|
||||
# remove 1st file too
|
||||
os.remove(fpath)
|
||||
pick_menu_item("Verify Sig File")
|
||||
need_keypress("y")
|
||||
need_keypress(confirm)
|
||||
pick_menu_item(sig_name)
|
||||
time.sleep(0.1)
|
||||
title, story = cap_story()
|
||||
@ -630,7 +643,7 @@ def test_verify_signature_file_digest_prob_multi(f_num, microsd_path, cap_story,
|
||||
warn_msg = "Files:\n" + "\n".join("> %s" % fname for fname in (fname0, fname1))
|
||||
assert warn_msg in story
|
||||
assert 'Contents verification not possible' in story
|
||||
need_keypress("y") # back in File Management
|
||||
need_keypress(confirm) # back in File Management
|
||||
|
||||
# reboult valid signed files
|
||||
for tup in files:
|
||||
@ -639,13 +652,13 @@ def test_verify_signature_file_digest_prob_multi(f_num, microsd_path, cap_story,
|
||||
f.write(conts)
|
||||
|
||||
pick_menu_item("Verify Sig File")
|
||||
need_keypress("y")
|
||||
need_keypress(confirm)
|
||||
pick_menu_item(sig_name)
|
||||
time.sleep(0.1)
|
||||
title, story = cap_story()
|
||||
assert title == "CORRECT"
|
||||
assert "Good signature" in story
|
||||
need_keypress("y") # back in File Management
|
||||
need_keypress(confirm) # back in File Management
|
||||
|
||||
@pytest.mark.parametrize("way", ("sd", "nfc"))
|
||||
@pytest.mark.parametrize("truncation_len", (0, 1))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user