163 lines
5.4 KiB
Python
163 lines
5.4 KiB
Python
import sys
|
|
import subprocess
|
|
|
|
sys.path.append('src/')
|
|
sys.path.append('test/')
|
|
sys.path.append('test/backend_output/python')
|
|
|
|
|
|
def send_c_binary(data: bytes) -> tuple[int, str, bytes]:
|
|
proc = subprocess.Popen(
|
|
["build/test_integration_python"],
|
|
stdin=subprocess.PIPE,
|
|
stdout=subprocess.PIPE
|
|
)
|
|
stdout, _ = proc.communicate(data.hex().encode() + b"\n")
|
|
return_code = proc.wait()
|
|
|
|
lines = stdout.splitlines()
|
|
|
|
if return_code != 0:
|
|
return (return_code, lines[0].decode("ascii"), b"")
|
|
else:
|
|
return (
|
|
return_code,
|
|
lines[0].decode("ascii"),
|
|
bytes.fromhex(lines[1].decode())
|
|
)
|
|
|
|
|
|
def test_integration_c_encdec_motion_update():
|
|
from backend_output.python.HCP_protocol_packets import HcpMessageMotionUpdate
|
|
from backend_output.python.HCP_protocol_bitfield import HcpBitfieldEnables
|
|
from backend_output.python.HCP_protocol_enum import HcpEnumInlineMotionUpdateDangerLvl
|
|
|
|
pkg = HcpMessageMotionUpdate(
|
|
speed=[1, 2, 3],
|
|
stearing=-0.1,
|
|
name="This is a test",
|
|
enable=[True, False, True, False],
|
|
heading="S",
|
|
enables=HcpBitfieldEnables(
|
|
a=True, b=False, c=True
|
|
),
|
|
danger_lvl=HcpEnumInlineMotionUpdateDangerLvl.WARNING
|
|
)
|
|
|
|
# Send to Integration Test Binary
|
|
ret_code, _, data_incoming = send_c_binary(pkg.to_bytes())
|
|
|
|
assert ret_code == 0
|
|
|
|
# Decode the incoming data.
|
|
pkg_incoming = HcpMessageMotionUpdate().from_bytes(data_incoming)
|
|
|
|
# Check that the incoming data is correctly modified.
|
|
assert pkg.speed[0] + 1 == pkg_incoming.speed[0]
|
|
assert pkg.speed[1] + 2 == pkg_incoming.speed[1]
|
|
assert pkg.speed[2] + 3 == pkg_incoming.speed[2]
|
|
|
|
assert round(pkg.stearing * 0.5 * 1000) == round(pkg_incoming.stearing * 1000)
|
|
|
|
assert pkg_incoming.name[0] == "A"
|
|
assert pkg.name[1:] == pkg_incoming.name[1:]
|
|
|
|
assert pkg.enables.a != pkg_incoming.enables.a
|
|
assert pkg.enables.b != pkg_incoming.enables.b
|
|
assert pkg.enables.c != pkg_incoming.enables.c
|
|
|
|
assert pkg.enable == pkg_incoming.enable
|
|
|
|
assert pkg_incoming.danger_lvl.value == HcpEnumInlineMotionUpdateDangerLvl.MELTDOWN.value
|
|
|
|
|
|
def test_integration_c_encdec_error():
|
|
from backend_output.python.HCP_protocol_packets import HcpMessageError
|
|
from backend_output.python.HCP_protocol_bitfield import HcpBitfieldInlineErrorEnables
|
|
from backend_output.python.HCP_protocol_enum import HcpEnumInlineErrorRecoveryStatus
|
|
|
|
pkg = HcpMessageError(
|
|
recovery_status=HcpEnumInlineErrorRecoveryStatus.NO,
|
|
enables=HcpBitfieldInlineErrorEnables(
|
|
test=True, asd=False, b=False, c=True, aa=True
|
|
),
|
|
)
|
|
|
|
# Send to Integration Test Binary
|
|
ret_code, _, data_incoming = send_c_binary(pkg.to_bytes())
|
|
|
|
assert ret_code == 0
|
|
|
|
# Decode the incoming data.
|
|
pkg_incoming = HcpMessageError().from_bytes(data_incoming)
|
|
|
|
# Check that the incoming data is correctly modified.
|
|
assert pkg_incoming.recovery_status.value == pkg.recovery_status.value
|
|
assert pkg_incoming.enables.test == pkg.enables.test
|
|
assert pkg_incoming.enables.asd == pkg.enables.asd
|
|
assert pkg_incoming.enables.b != pkg.enables.b
|
|
assert pkg_incoming.enables.c != pkg.enables.c
|
|
assert pkg_incoming.enables.aa == pkg.enables.aa
|
|
|
|
|
|
def test_integration_c_encdec_failure_size():
|
|
from backend_output.python.HCP_protocol_packets import HcpMessageError
|
|
from backend_output.python.HCP_protocol_bitfield import HcpBitfieldInlineErrorEnables
|
|
from backend_output.python.HCP_protocol_enum import HcpEnumInlineErrorRecoveryStatus
|
|
|
|
pkg = HcpMessageError(
|
|
recovery_status=HcpEnumInlineErrorRecoveryStatus.NO,
|
|
enables=HcpBitfieldInlineErrorEnables(
|
|
test=True, asd=False, b=False, c=True, aa=True
|
|
),
|
|
)
|
|
|
|
# Send to Integration Test Binary
|
|
data = pkg.to_bytes()
|
|
ret_code, msg, _ = send_c_binary(data[:4])
|
|
|
|
assert ret_code != 0
|
|
assert msg == "SIZE"
|
|
|
|
|
|
def test_integration_c_encdec_failure_crc():
|
|
from backend_output.python.HCP_protocol_packets import HcpMessageError
|
|
from backend_output.python.HCP_protocol_bitfield import HcpBitfieldInlineErrorEnables
|
|
from backend_output.python.HCP_protocol_enum import HcpEnumInlineErrorRecoveryStatus
|
|
|
|
pkg = HcpMessageError(
|
|
recovery_status=HcpEnumInlineErrorRecoveryStatus.NO,
|
|
enables=HcpBitfieldInlineErrorEnables(
|
|
test=True, asd=False, b=False, c=True, aa=True
|
|
),
|
|
)
|
|
|
|
# Send to Integration Test Binary
|
|
data = pkg.to_bytes()
|
|
data = data[:2] + b"\xaa" + data[3:]
|
|
ret_code, msg, _ = send_c_binary(data)
|
|
|
|
assert ret_code != 0
|
|
assert msg == "CRC"
|
|
|
|
|
|
def test_integration_c_encdec_failure_id():
|
|
from backend_output.python.HCP_protocol_packets import HcpMessageError
|
|
from backend_output.python.HCP_protocol_bitfield import HcpBitfieldInlineErrorEnables
|
|
from backend_output.python.HCP_protocol_enum import HcpEnumInlineErrorRecoveryStatus
|
|
|
|
pkg = HcpMessageError(
|
|
recovery_status=HcpEnumInlineErrorRecoveryStatus.NO,
|
|
enables=HcpBitfieldInlineErrorEnables(
|
|
test=True, asd=False, b=False, c=True, aa=True
|
|
),
|
|
)
|
|
|
|
# Send to Integration Test Binary
|
|
data = pkg.to_bytes()
|
|
data = b"\xff" + data[1:]
|
|
ret_code, msg, _ = send_c_binary(data)
|
|
|
|
assert ret_code != 0
|
|
assert msg == "ID"
|