Add test for invalid block hash (#1580)

* Add test for invalid block hash in get block header method

* Add test for invalid squeak hash in unpack offer method

* Add assertions for exception messages in squeak core tests
This commit is contained in:
Jonathan Zernik 2021-10-12 16:45:41 -07:00 committed by GitHub
parent 3dcc2681d5
commit fb3b7fcd3a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -30,11 +30,13 @@ from squeaknode.core.secret_keys import add_tweak
from squeaknode.core.secret_keys import generate_tweak
from squeaknode.core.squeak_core import SqueakCore
from squeaknode.core.squeaks import get_hash
from squeaknode.core.squeaks import make_squeak_with_block
from squeaknode.lightning.info import Info
from squeaknode.lightning.invoice import Invoice
from squeaknode.lightning.lightning_client import LightningClient
from squeaknode.lightning.pay_req import PayReq
from squeaknode.lightning.payment import Payment
from tests.utils import gen_random_hash
from tests.utils import sha256
@ -152,6 +154,26 @@ def failed_payment(payment_request):
)
@pytest.fixture
def other_block_info():
yield BlockInfo(
block_height=5678,
block_hash=gen_random_hash(),
block_header=b'',
)
@pytest.fixture
def other_squeak(signing_key, squeak_content, other_block_info):
squeak, _ = make_squeak_with_block(
signing_key,
squeak_content,
other_block_info.block_height,
other_block_info.block_hash,
)
yield squeak
class MockBitcoinClient(BitcoinClient):
def __init__(self, best_block_info):
@ -161,22 +183,13 @@ class MockBitcoinClient(BitcoinClient):
return self.best_block_info
def get_block_info_by_height(self, block_height: int) -> BlockInfo:
if block_height == 0:
return self.best_block_info
else:
raise Exception("Invalid block height")
return self.best_block_info
def get_block_hash(self, block_height: int) -> bytes:
if block_height == 0:
return self.best_block_info.block_hash
else:
raise Exception("Invalid block height")
return self.best_block_info.block_hash
def get_block_header(self, block_hash: bytes, verbose: bool) -> bytes:
if block_hash == self.best_block_info.block_hash:
return self.best_block_info.block_header
else:
raise Exception("Invalid block hash")
return self.best_block_info.block_header
class MockLightningClient(LightningClient):
@ -308,11 +321,12 @@ def test_make_squeak_with_contact_profile(
contact_profile,
squeak_content,
):
with pytest.raises(Exception):
with pytest.raises(Exception) as excinfo:
created_squeak, created_secret_key = squeak_core.make_squeak(
contact_profile,
squeak_content,
)
assert "Can't make squeak with a contact profile." in str(excinfo.value)
def test_get_block_header(
@ -325,6 +339,15 @@ def test_get_block_header(
assert block_header == genesis_block_info.block_header
def test_get_block_header_invalid_block_hash(
squeak_core,
other_squeak,
):
with pytest.raises(Exception) as excinfo:
squeak_core.get_block_header(other_squeak)
assert "Block hash incorrect." in str(excinfo.value)
def test_check_squeak(squeak_core, squeak):
squeak_core.check_squeak(squeak)
@ -356,14 +379,25 @@ def test_unpacked_offer(unpacked_offer):
assert unpacked_offer is not None
def test_unpack_offer_invalid_squeak_hash(squeak_core, other_squeak, packaged_offer, seller_peer_address):
with pytest.raises(Exception) as excinfo:
squeak_core.unpack_offer(
other_squeak,
packaged_offer,
seller_peer_address,
)
assert "does not match squeak hash" in str(excinfo.value)
def test_unpacked_offer_bad_payment_point(squeak_core, squeak, packaged_offer, seller_peer_address):
with pytest.raises(Exception):
with pytest.raises(Exception) as excinfo:
squeak_core.unpack_offer(
squeak,
packaged_offer,
seller_peer_address,
check_payment_point=True,
)
assert "Invalid payment point." in str(excinfo.value)
def test_sent_payment(sent_payment):