mirror of
https://github.com/aljazceru/python-teos.git
synced 2025-12-17 22:24:23 +01:00
Generalizes get_dummy_appointment (there were multiple instances of it in different tests)
Also refactors some unused imports and modifies the watcher tests to use .serialize instead of to_json
This commit is contained in:
@@ -4,13 +4,19 @@ import requests
|
|||||||
from time import sleep
|
from time import sleep
|
||||||
from shutil import rmtree
|
from shutil import rmtree
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
from hashlib import sha256
|
||||||
|
from binascii import unhexlify
|
||||||
|
|
||||||
from pisa.conf import DB_PATH
|
from pisa.conf import DB_PATH
|
||||||
|
from apps.cli.blob import Blob
|
||||||
from pisa.api import start_api
|
from pisa.api import start_api
|
||||||
from pisa.responder import Job
|
from pisa.responder import Job
|
||||||
from pisa.watcher import Watcher
|
from pisa.watcher import Watcher
|
||||||
|
from pisa.tools import bitcoin_cli
|
||||||
from pisa.db_manager import DBManager
|
from pisa.db_manager import DBManager
|
||||||
from pisa.appointment import Appointment
|
from pisa.appointment import Appointment
|
||||||
|
from test.simulator.utils import sha256d
|
||||||
|
from test.simulator.transaction import TX
|
||||||
from test.simulator.bitcoind_sim import run_simulator, HOST, PORT
|
from test.simulator.bitcoind_sim import run_simulator, HOST, PORT
|
||||||
|
|
||||||
|
|
||||||
@@ -52,9 +58,9 @@ def db_manager():
|
|||||||
|
|
||||||
|
|
||||||
def get_random_value_hex(nbytes):
|
def get_random_value_hex(nbytes):
|
||||||
pseudo_random_value = random.getrandbits(8*nbytes)
|
pseudo_random_value = random.getrandbits(8 * nbytes)
|
||||||
prv_hex = '{:x}'.format(pseudo_random_value)
|
prv_hex = '{:x}'.format(pseudo_random_value)
|
||||||
return prv_hex.zfill(2*nbytes)
|
return prv_hex.zfill(2 * nbytes)
|
||||||
|
|
||||||
|
|
||||||
def generate_block():
|
def generate_block():
|
||||||
@@ -67,19 +73,38 @@ def generate_blocks(n):
|
|||||||
generate_block()
|
generate_block()
|
||||||
|
|
||||||
|
|
||||||
def generate_dummy_appointment():
|
def generate_dummy_appointment_data(start_time_offset=5, end_time_offset=30):
|
||||||
locator = get_random_value_hex(32)
|
current_height = bitcoin_cli().getblockcount()
|
||||||
encrypted_blob = get_random_value_hex(250)
|
|
||||||
start_time = 100
|
dispute_tx = TX.create_dummy_transaction()
|
||||||
end_time = 120
|
dispute_txid = sha256d(dispute_tx)
|
||||||
dispute_delta = 20
|
justice_tx = TX.create_dummy_transaction(dispute_txid)
|
||||||
|
|
||||||
|
dummy_appointment_data = {"tx": justice_tx, "tx_id": dispute_txid, "start_time": current_height + start_time_offset,
|
||||||
|
"end_time": current_height + end_time_offset, "dispute_delta": 20}
|
||||||
|
|
||||||
cipher = "AES-GCM-128"
|
cipher = "AES-GCM-128"
|
||||||
hash_function = "SHA256"
|
hash_function = "SHA256"
|
||||||
|
|
||||||
appointment_data = dict(locator=locator, start_time=start_time, end_time=end_time, dispute_delta=dispute_delta,
|
locator = sha256(unhexlify(dispute_txid)).hexdigest()
|
||||||
encrypted_blob=encrypted_blob, cipher=cipher, hash_function=hash_function, triggered=False)
|
blob = Blob(dummy_appointment_data.get("tx"), cipher, hash_function)
|
||||||
|
|
||||||
return Appointment.from_dict(appointment_data)
|
encrypted_blob = blob.encrypt((dummy_appointment_data.get("tx_id")))
|
||||||
|
|
||||||
|
appointment_data = {"locator": locator, "start_time": dummy_appointment_data.get("start_time"),
|
||||||
|
"end_time": dummy_appointment_data.get("end_time"),
|
||||||
|
"dispute_delta": dummy_appointment_data.get("dispute_delta"),
|
||||||
|
"encrypted_blob": encrypted_blob, "cipher": cipher, "hash_function": hash_function,
|
||||||
|
"triggered": False}
|
||||||
|
|
||||||
|
return appointment_data, dispute_tx
|
||||||
|
|
||||||
|
|
||||||
|
def generate_dummy_appointment(start_time_offset=5, end_time_offset=30):
|
||||||
|
appointment_data, dispute_tx = generate_dummy_appointment_data(start_time_offset=start_time_offset,
|
||||||
|
end_time_offset=end_time_offset)
|
||||||
|
|
||||||
|
return Appointment.from_dict(appointment_data), dispute_tx
|
||||||
|
|
||||||
|
|
||||||
def generate_dummy_job():
|
def generate_dummy_job():
|
||||||
@@ -91,4 +116,3 @@ def generate_dummy_job():
|
|||||||
appointment_end=100)
|
appointment_end=100)
|
||||||
|
|
||||||
return Job.from_dict(job_data)
|
return Job.from_dict(job_data)
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,10 @@
|
|||||||
import json
|
import json
|
||||||
import pytest
|
import pytest
|
||||||
import requests
|
import requests
|
||||||
from hashlib import sha256
|
|
||||||
from binascii import unhexlify
|
|
||||||
|
|
||||||
from apps.cli.blob import Blob
|
|
||||||
from pisa import HOST, PORT, c_logger
|
from pisa import HOST, PORT, c_logger
|
||||||
from pisa.tools import bitcoin_cli
|
|
||||||
from test.simulator.utils import sha256d
|
|
||||||
from test.simulator.transaction import TX
|
|
||||||
from pisa.utils.auth_proxy import AuthServiceProxy
|
from pisa.utils.auth_proxy import AuthServiceProxy
|
||||||
from test.unit.conftest import generate_blocks, get_random_value_hex
|
from test.unit.conftest import generate_blocks, get_random_value_hex, generate_dummy_appointment_data
|
||||||
from pisa.conf import BTC_RPC_USER, BTC_RPC_PASSWD, BTC_RPC_HOST, BTC_RPC_PORT, MAX_APPOINTMENTS
|
from pisa.conf import BTC_RPC_USER, BTC_RPC_PASSWD, BTC_RPC_HOST, BTC_RPC_PORT, MAX_APPOINTMENTS
|
||||||
|
|
||||||
c_logger.disabled = True
|
c_logger.disabled = True
|
||||||
@@ -22,33 +16,6 @@ appointments = []
|
|||||||
locator_dispute_tx_map = {}
|
locator_dispute_tx_map = {}
|
||||||
|
|
||||||
|
|
||||||
def generate_dummy_appointment_data():
|
|
||||||
current_height = bitcoin_cli().getblockcount()
|
|
||||||
|
|
||||||
dispute_tx = TX.create_dummy_transaction()
|
|
||||||
dispute_txid = sha256d(dispute_tx)
|
|
||||||
justice_tx = TX.create_dummy_transaction(dispute_txid)
|
|
||||||
|
|
||||||
dummy_appointment_data = {"tx": justice_tx, "tx_id": dispute_txid, "start_time": current_height + 5,
|
|
||||||
"end_time": current_height + 30, "dispute_delta": 20}
|
|
||||||
|
|
||||||
cipher = "AES-GCM-128"
|
|
||||||
hash_function = "SHA256"
|
|
||||||
|
|
||||||
locator = sha256(unhexlify(dispute_txid)).hexdigest()
|
|
||||||
blob = Blob(dummy_appointment_data.get("tx"), cipher, hash_function)
|
|
||||||
|
|
||||||
encrypted_blob = blob.encrypt((dummy_appointment_data.get("tx_id")))
|
|
||||||
|
|
||||||
appointment = {"locator": locator, "start_time": dummy_appointment_data.get("start_time"),
|
|
||||||
"end_time": dummy_appointment_data.get("end_time"),
|
|
||||||
"dispute_delta": dummy_appointment_data.get("dispute_delta"),
|
|
||||||
"encrypted_blob": encrypted_blob, "cipher": cipher, "hash_function": hash_function,
|
|
||||||
"triggered": False}
|
|
||||||
|
|
||||||
return appointment, dispute_tx
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def new_appointment():
|
def new_appointment():
|
||||||
appointment, dispute_tx = generate_dummy_appointment_data()
|
appointment, dispute_tx = generate_dummy_appointment_data()
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ from test.unit.conftest import get_random_value_hex
|
|||||||
c_logger.disabled = True
|
c_logger.disabled = True
|
||||||
|
|
||||||
# Not much to test here, adding it for completeness
|
# Not much to test here, adding it for completeness
|
||||||
|
|
||||||
@fixture
|
@fixture
|
||||||
def appointment_data():
|
def appointment_data():
|
||||||
locator = get_random_value_hex(32)
|
locator = get_random_value_hex(32)
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ def test_build_appointments():
|
|||||||
|
|
||||||
# Create some appointment data
|
# Create some appointment data
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
appointment = generate_dummy_appointment()
|
appointment, _ = generate_dummy_appointment()
|
||||||
uuid = uuid4().hex
|
uuid = uuid4().hex
|
||||||
|
|
||||||
appointments_data[uuid] = appointment.to_dict()
|
appointments_data[uuid] = appointment.to_dict()
|
||||||
@@ -17,7 +17,7 @@ def test_build_appointments():
|
|||||||
# Add some additional appointments that share the same locator to test all the builder's cases
|
# Add some additional appointments that share the same locator to test all the builder's cases
|
||||||
if i % 2 == 0:
|
if i % 2 == 0:
|
||||||
locator = appointment.locator
|
locator = appointment.locator
|
||||||
appointment = generate_dummy_appointment()
|
appointment, _ = generate_dummy_appointment()
|
||||||
uuid = uuid4().hex
|
uuid = uuid4().hex
|
||||||
appointment.locator = locator
|
appointment.locator = locator
|
||||||
|
|
||||||
|
|||||||
@@ -4,8 +4,7 @@ from pisa import c_logger
|
|||||||
from pisa.carrier import Carrier
|
from pisa.carrier import Carrier
|
||||||
from test.simulator.utils import sha256d
|
from test.simulator.utils import sha256d
|
||||||
from test.simulator.transaction import TX
|
from test.simulator.transaction import TX
|
||||||
from test.unit.conftest import generate_blocks
|
from test.unit.conftest import generate_blocks, get_random_value_hex
|
||||||
from test.unit.conftest import get_random_value_hex
|
|
||||||
from pisa.rpc_errors import RPC_VERIFY_ALREADY_IN_CHAIN, RPC_DESERIALIZATION_ERROR
|
from pisa.rpc_errors import RPC_VERIFY_ALREADY_IN_CHAIN, RPC_DESERIALIZATION_ERROR
|
||||||
|
|
||||||
c_logger.disabled = True
|
c_logger.disabled = True
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ from pisa.db_manager import WATCHER_LAST_BLOCK_KEY, RESPONDER_LAST_BLOCK_KEY, LO
|
|||||||
|
|
||||||
@pytest.fixture(scope='module')
|
@pytest.fixture(scope='module')
|
||||||
def watcher_appointments():
|
def watcher_appointments():
|
||||||
return {uuid4().hex: generate_dummy_appointment() for _ in range(10)}
|
return {uuid4().hex: generate_dummy_appointment()[0] for _ in range(10)}
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='module')
|
@pytest.fixture(scope='module')
|
||||||
|
|||||||
@@ -10,8 +10,7 @@ from test.simulator.utils import sha256d
|
|||||||
from pisa.responder import Responder, Job
|
from pisa.responder import Responder, Job
|
||||||
from test.simulator.bitcoind_sim import TX
|
from test.simulator.bitcoind_sim import TX
|
||||||
from pisa.utils.auth_proxy import AuthServiceProxy
|
from pisa.utils.auth_proxy import AuthServiceProxy
|
||||||
from test.unit.conftest import get_random_value_hex
|
from test.unit.conftest import generate_block, generate_blocks, get_random_value_hex
|
||||||
from test.unit.conftest import generate_block, generate_blocks
|
|
||||||
from pisa.conf import BTC_RPC_USER, BTC_RPC_PASSWD, BTC_RPC_HOST, BTC_RPC_PORT
|
from pisa.conf import BTC_RPC_USER, BTC_RPC_PASSWD, BTC_RPC_HOST, BTC_RPC_PORT
|
||||||
|
|
||||||
c_logger.disabled = True
|
c_logger.disabled = True
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
from pisa import c_logger
|
from pisa import c_logger
|
||||||
from pisa.tools import check_txid_format
|
from pisa.tools import can_connect_to_bitcoind, in_correct_network, bitcoin_cli, check_txid_format
|
||||||
from pisa.tools import can_connect_to_bitcoind, in_correct_network, bitcoin_cli
|
|
||||||
|
|
||||||
c_logger.disabled = True
|
c_logger.disabled = True
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
from hashlib import sha256
|
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from binascii import unhexlify
|
|
||||||
from queue import Queue, Empty
|
from queue import Queue, Empty
|
||||||
|
|
||||||
from cryptography.hazmat.backends import default_backend
|
from cryptography.hazmat.backends import default_backend
|
||||||
@@ -12,17 +10,13 @@ from cryptography.hazmat.primitives.asymmetric import ec
|
|||||||
from cryptography.exceptions import InvalidSignature
|
from cryptography.exceptions import InvalidSignature
|
||||||
|
|
||||||
from pisa import c_logger
|
from pisa import c_logger
|
||||||
from apps.cli.blob import Blob
|
|
||||||
from pisa.watcher import Watcher
|
from pisa.watcher import Watcher
|
||||||
from pisa.responder import Responder
|
from pisa.responder import Responder
|
||||||
from pisa.conf import MAX_APPOINTMENTS
|
|
||||||
from pisa.appointment import Appointment
|
|
||||||
from pisa.tools import check_txid_format
|
from pisa.tools import check_txid_format
|
||||||
from test.simulator.utils import sha256d
|
|
||||||
from test.simulator.transaction import TX
|
|
||||||
from pisa.utils.auth_proxy import AuthServiceProxy
|
from pisa.utils.auth_proxy import AuthServiceProxy
|
||||||
from test.unit.conftest import generate_block, generate_blocks
|
from test.unit.conftest import generate_block, generate_blocks, generate_dummy_appointment
|
||||||
from pisa.conf import EXPIRY_DELTA, BTC_RPC_USER, BTC_RPC_PASSWD, BTC_RPC_HOST, BTC_RPC_PORT, PISA_SECRET_KEY
|
from pisa.conf import EXPIRY_DELTA, BTC_RPC_USER, BTC_RPC_PASSWD, BTC_RPC_HOST, BTC_RPC_PORT, PISA_SECRET_KEY, \
|
||||||
|
MAX_APPOINTMENTS
|
||||||
|
|
||||||
c_logger.disabled = True
|
c_logger.disabled = True
|
||||||
|
|
||||||
@@ -42,37 +36,14 @@ def watcher(db_manager):
|
|||||||
return Watcher(db_manager)
|
return Watcher(db_manager)
|
||||||
|
|
||||||
|
|
||||||
def generate_dummy_appointment():
|
|
||||||
bitcoin_cli = AuthServiceProxy("http://%s:%s@%s:%d" % (BTC_RPC_USER, BTC_RPC_PASSWD, BTC_RPC_HOST, BTC_RPC_PORT))
|
|
||||||
|
|
||||||
dispute_tx = TX.create_dummy_transaction()
|
|
||||||
dispute_txid = sha256d(dispute_tx)
|
|
||||||
justice_tx = TX.create_dummy_transaction(dispute_txid)
|
|
||||||
|
|
||||||
start_time = bitcoin_cli.getblockcount() + 1
|
|
||||||
end_time = start_time + 1
|
|
||||||
dispute_delta = 20
|
|
||||||
|
|
||||||
cipher = "AES-GCM-128"
|
|
||||||
hash_function = "SHA256"
|
|
||||||
|
|
||||||
locator = sha256(unhexlify(dispute_txid)).hexdigest()
|
|
||||||
blob = Blob(justice_tx, cipher, hash_function)
|
|
||||||
|
|
||||||
encrypted_blob = blob.encrypt(dispute_txid)
|
|
||||||
|
|
||||||
appointment = Appointment(locator, start_time, end_time, dispute_delta, encrypted_blob, cipher, hash_function)
|
|
||||||
|
|
||||||
return appointment, dispute_tx
|
|
||||||
|
|
||||||
|
|
||||||
def create_appointments(n):
|
def create_appointments(n):
|
||||||
locator_uuid_map = dict()
|
locator_uuid_map = dict()
|
||||||
appointments = dict()
|
appointments = dict()
|
||||||
dispute_txs = []
|
dispute_txs = []
|
||||||
|
|
||||||
for i in range(n):
|
for i in range(n):
|
||||||
appointment, dispute_tx = generate_dummy_appointment()
|
appointment, dispute_tx = generate_dummy_appointment(start_time_offset=START_TIME_OFFSET,
|
||||||
|
end_time_offset=END_TIME_OFFSET)
|
||||||
uuid = uuid4().hex
|
uuid = uuid4().hex
|
||||||
|
|
||||||
appointments[uuid] = appointment
|
appointments[uuid] = appointment
|
||||||
@@ -85,7 +56,7 @@ def create_appointments(n):
|
|||||||
def is_signature_valid(appointment, signature, pk):
|
def is_signature_valid(appointment, signature, pk):
|
||||||
# verify the signature
|
# verify the signature
|
||||||
try:
|
try:
|
||||||
data = appointment.to_json().encode('utf-8')
|
data = appointment.serialize()
|
||||||
pk.verify(signature, data, ec.ECDSA(hashes.SHA256()))
|
pk.verify(signature, data, ec.ECDSA(hashes.SHA256()))
|
||||||
except InvalidSignature:
|
except InvalidSignature:
|
||||||
return False
|
return False
|
||||||
@@ -109,7 +80,8 @@ def test_add_appointment(run_bitcoind, watcher):
|
|||||||
|
|
||||||
# We should be able to add appointments up to the limit
|
# We should be able to add appointments up to the limit
|
||||||
for _ in range(10):
|
for _ in range(10):
|
||||||
appointment, dispute_tx = generate_dummy_appointment()
|
appointment, dispute_tx = generate_dummy_appointment(start_time_offset=START_TIME_OFFSET,
|
||||||
|
end_time_offset=END_TIME_OFFSET)
|
||||||
added_appointment, sig = watcher.add_appointment(appointment)
|
added_appointment, sig = watcher.add_appointment(appointment)
|
||||||
|
|
||||||
assert added_appointment is True
|
assert added_appointment is True
|
||||||
@@ -117,7 +89,8 @@ def test_add_appointment(run_bitcoind, watcher):
|
|||||||
|
|
||||||
|
|
||||||
def test_sign_appointment(watcher):
|
def test_sign_appointment(watcher):
|
||||||
appointment, _ = generate_dummy_appointment()
|
appointment, _ = generate_dummy_appointment(start_time_offset=START_TIME_OFFSET,
|
||||||
|
end_time_offset=END_TIME_OFFSET)
|
||||||
signature = watcher.sign_appointment(appointment)
|
signature = watcher.sign_appointment(appointment)
|
||||||
assert is_signature_valid(appointment, signature, public_key)
|
assert is_signature_valid(appointment, signature, public_key)
|
||||||
|
|
||||||
@@ -127,13 +100,15 @@ def test_add_too_many_appointments(watcher):
|
|||||||
watcher.appointments = dict()
|
watcher.appointments = dict()
|
||||||
|
|
||||||
for _ in range(MAX_APPOINTMENTS):
|
for _ in range(MAX_APPOINTMENTS):
|
||||||
appointment, dispute_tx = generate_dummy_appointment()
|
appointment, dispute_tx = generate_dummy_appointment(start_time_offset=START_TIME_OFFSET,
|
||||||
|
end_time_offset=END_TIME_OFFSET)
|
||||||
added_appointment, sig = watcher.add_appointment(appointment)
|
added_appointment, sig = watcher.add_appointment(appointment)
|
||||||
|
|
||||||
assert added_appointment is True
|
assert added_appointment is True
|
||||||
assert is_signature_valid(appointment, sig, public_key)
|
assert is_signature_valid(appointment, sig, public_key)
|
||||||
|
|
||||||
appointment, dispute_tx = generate_dummy_appointment()
|
appointment, dispute_tx = generate_dummy_appointment(start_time_offset=START_TIME_OFFSET,
|
||||||
|
end_time_offset=END_TIME_OFFSET)
|
||||||
added_appointment, sig = watcher.add_appointment(appointment)
|
added_appointment, sig = watcher.add_appointment(appointment)
|
||||||
|
|
||||||
assert added_appointment is False
|
assert added_appointment is False
|
||||||
|
|||||||
Reference in New Issue
Block a user