Init Commit: Moved bproto to seperate repo
This commit is contained in:
48
test/output/c/CMakeLists.txt
Normal file
48
test/output/c/CMakeLists.txt
Normal file
@@ -0,0 +1,48 @@
|
||||
cmake_minimum_required(VERSION 3.25)
|
||||
project(bproto_test)
|
||||
enable_testing()
|
||||
|
||||
include("../../backend_output/c/HCP.cmake")
|
||||
|
||||
# include CTest support
|
||||
include(CTest)
|
||||
set(CMAKE_BUILD_TYPE "Debug")
|
||||
|
||||
# Adding Unity test framework
|
||||
add_library(unity STATIC Unity/src/unity.c)
|
||||
target_include_directories(unity PUBLIC Unity/src)
|
||||
|
||||
# Normal test target
|
||||
add_executable(test_runner
|
||||
src/main.c
|
||||
src/test_messages.c
|
||||
)
|
||||
|
||||
target_link_libraries(test_runner PUBLIC
|
||||
bproto
|
||||
unity
|
||||
gcov
|
||||
)
|
||||
|
||||
target_include_directories(test_runner PUBLIC
|
||||
${CMAKE_CURRENT_LIST_DIR}/src
|
||||
)
|
||||
|
||||
target_compile_options(test_runner PRIVATE -fprofile-arcs -ftest-coverage -fPIC -O0 --coverage)
|
||||
target_compile_options(bproto PRIVATE -fprofile-arcs -ftest-coverage -fPIC -O0 --coverage)
|
||||
target_link_options(test_runner PRIVATE -fprofile-arcs -ftest-coverage -fPIC --coverage)
|
||||
target_link_options(bproto PRIVATE -fprofile-arcs -ftest-coverage -fPIC --coverage)
|
||||
|
||||
# Python Integration test build
|
||||
add_executable(test_integration_python
|
||||
src/test_python_integration.c
|
||||
)
|
||||
|
||||
target_link_libraries(test_integration_python PUBLIC
|
||||
bproto
|
||||
gcov
|
||||
)
|
||||
|
||||
target_include_directories(test_integration_python PUBLIC
|
||||
${CMAKE_CURRENT_LIST_DIR}/src
|
||||
)
|
||||
1
test/output/c/src/Unity
Submodule
1
test/output/c/src/Unity
Submodule
Submodule test/output/c/src/Unity added at 23e8edbd64
17
test/output/c/src/main.c
Normal file
17
test/output/c/src/main.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "unity.h"
|
||||
#include "suit.h"
|
||||
|
||||
void setUp(void) {};
|
||||
void tearDown(void) {};
|
||||
|
||||
int main() {
|
||||
UNITY_BEGIN();
|
||||
RUN_TEST(test_encdec_generic_Error);
|
||||
RUN_TEST(test_encdec_generic_MotionUpdate);
|
||||
|
||||
RUN_TEST(test_encdec_generic_Error_invalid_id);
|
||||
RUN_TEST(test_encdec_generic_Error_invalid_size);
|
||||
RUN_TEST(test_encdec_generic_Error_large_size);
|
||||
RUN_TEST(test_encdec_generic_Error_invalid_crc);
|
||||
return UNITY_END();
|
||||
}
|
||||
9
test/output/c/src/suit.h
Normal file
9
test/output/c/src/suit.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
// Enc/Dec with generic data buffer
|
||||
void test_encdec_generic_MotionUpdate();
|
||||
void test_encdec_generic_Error();
|
||||
void test_encdec_generic_Error_invalid_id();
|
||||
void test_encdec_generic_Error_invalid_size();
|
||||
void test_encdec_generic_Error_large_size();
|
||||
void test_encdec_generic_Error_invalid_crc();
|
||||
224
test/output/c/src/test_messages.c
Normal file
224
test/output/c/src/test_messages.c
Normal file
@@ -0,0 +1,224 @@
|
||||
#include "suit.h"
|
||||
#include <stdint.h>
|
||||
#include "unity.h"
|
||||
#include "HCP_message.h"
|
||||
|
||||
void test_encdec_generic_Error() {
|
||||
uint8_t buffer_msg_data[MAX_MESSAGE_DATA_SIZE];
|
||||
|
||||
Hcp_message_MotionUpdate msg = {
|
||||
.speed = {3, 6, 9},
|
||||
.stearing = 0.5,
|
||||
.name = "This is a test",
|
||||
.heading = 'B',
|
||||
.enable = {true, false, true, false},
|
||||
.enables = {
|
||||
.a = false, .b = true, .c = false
|
||||
},
|
||||
.dangerLvl = HCP_ENUM_INLINE_MOTION_UPDATE_DANGER_LVL_VALUE_MELTDOWN
|
||||
};
|
||||
|
||||
size_t length = toBytes_Hcp_message_MotionUpdate(&msg, buffer_msg_data);
|
||||
|
||||
TEST_ASSERT_EQUAL(length, 58);
|
||||
TEST_ASSERT_EQUAL(buffer_msg_data[0], 0x01);
|
||||
|
||||
Hcp_message_MotionUpdate rx_msg;
|
||||
size_t parsed_length = 0;
|
||||
HCP_MSG_ID msg_id = 0xff;
|
||||
HCP_PARSER_RESULT result = fromBytes(&rx_msg, buffer_msg_data, length, &parsed_length, &msg_id);
|
||||
|
||||
TEST_ASSERT_EQUAL(result, HCP_PARSER_RESULT_OK);
|
||||
TEST_ASSERT_EQUAL(parsed_length, 58);
|
||||
TEST_ASSERT_EQUAL(msg_id, HCP_MESSAGE_MOTION_UPDATE_ID);
|
||||
|
||||
TEST_ASSERT_EQUAL(msg.stearing, rx_msg.stearing);
|
||||
TEST_ASSERT_EQUAL_STRING(msg.name, rx_msg.name);
|
||||
TEST_ASSERT_EQUAL(msg.heading, rx_msg.heading);
|
||||
TEST_ASSERT_EQUAL(msg.dangerLvl, rx_msg.dangerLvl);
|
||||
|
||||
TEST_ASSERT_EQUAL(msg.speed[0], rx_msg.speed[0]);
|
||||
TEST_ASSERT_EQUAL(msg.speed[1], rx_msg.speed[1]);
|
||||
TEST_ASSERT_EQUAL(msg.speed[2], rx_msg.speed[2]);
|
||||
|
||||
TEST_ASSERT_EQUAL(msg.enable[0], rx_msg.enable[0]);
|
||||
TEST_ASSERT_EQUAL(msg.enable[1], rx_msg.enable[1]);
|
||||
TEST_ASSERT_EQUAL(msg.enable[2], rx_msg.enable[2]);
|
||||
TEST_ASSERT_EQUAL(msg.enable[3], rx_msg.enable[3]);
|
||||
|
||||
TEST_ASSERT_EQUAL(msg.enables.a, rx_msg.enables.a);
|
||||
TEST_ASSERT_EQUAL(msg.enables.b, rx_msg.enables.b);
|
||||
TEST_ASSERT_EQUAL(msg.enables.c, rx_msg.enables.c);
|
||||
}
|
||||
|
||||
void test_encdec_generic_MotionUpdate() {
|
||||
uint8_t buffer_msg_data[MAX_MESSAGE_DATA_SIZE];
|
||||
|
||||
Hcp_message_Error msg = {
|
||||
.enables = {
|
||||
.aa = true,
|
||||
.asd = false,
|
||||
.b = true,
|
||||
.c = false,
|
||||
.test = true
|
||||
},
|
||||
.recoveryStatus = HCP_ENUM_INLINE_ERROR_RECOVERY_STATUS_VALUE_NO
|
||||
};
|
||||
|
||||
size_t length = toBytes_Hcp_message_Error(&msg, buffer_msg_data);
|
||||
|
||||
TEST_ASSERT_EQUAL(length, 5);
|
||||
TEST_ASSERT_EQUAL(buffer_msg_data[0], 0x02);
|
||||
|
||||
Hcp_message_Error rx_msg;
|
||||
size_t parsed_length = 0;
|
||||
HCP_MSG_ID msg_id = 0xff;
|
||||
HCP_PARSER_RESULT result = fromBytes(&rx_msg, buffer_msg_data, length, &parsed_length, &msg_id);
|
||||
|
||||
TEST_ASSERT_EQUAL(result, HCP_PARSER_RESULT_OK);
|
||||
TEST_ASSERT_EQUAL(parsed_length, 5);
|
||||
TEST_ASSERT_EQUAL(msg_id, HCP_MESSAGE_ERROR_ID);
|
||||
|
||||
TEST_ASSERT_EQUAL(rx_msg.enables.aa, msg.enables.aa);
|
||||
TEST_ASSERT_EQUAL(rx_msg.enables.asd, msg.enables.asd);
|
||||
TEST_ASSERT_EQUAL(rx_msg.enables.b, msg.enables.b);
|
||||
TEST_ASSERT_EQUAL(rx_msg.enables.c, msg.enables.c);
|
||||
TEST_ASSERT_EQUAL(rx_msg.enables.test, msg.enables.test);
|
||||
|
||||
TEST_ASSERT_EQUAL(rx_msg.recoveryStatus, msg.recoveryStatus);
|
||||
}
|
||||
|
||||
void test_encdec_generic_Error_invalid_id() {
|
||||
uint8_t buffer_msg_data[MAX_MESSAGE_DATA_SIZE];
|
||||
|
||||
Hcp_message_Error msg = {
|
||||
.enables = {
|
||||
.aa = true,
|
||||
.asd = false,
|
||||
.b = true,
|
||||
.c = false,
|
||||
.test = true
|
||||
},
|
||||
.recoveryStatus = HCP_ENUM_INLINE_ERROR_RECOVERY_STATUS_VALUE_NO
|
||||
};
|
||||
|
||||
size_t length = toBytes_Hcp_message_Error(&msg, buffer_msg_data);
|
||||
|
||||
TEST_ASSERT_EQUAL(length, 5);
|
||||
TEST_ASSERT_EQUAL(buffer_msg_data[0], 0x02);
|
||||
|
||||
// Mofiy data
|
||||
buffer_msg_data[0] = 0xf2;
|
||||
|
||||
Hcp_message_Error rx_msg;
|
||||
size_t parsed_length = 0;
|
||||
HCP_MSG_ID msg_id = 0xff;
|
||||
HCP_PARSER_RESULT result = fromBytes(&rx_msg, buffer_msg_data, length, &parsed_length, &msg_id);
|
||||
|
||||
TEST_ASSERT_EQUAL(parsed_length, 0);
|
||||
TEST_ASSERT_EQUAL(result, HCP_PARSER_RESULT_ERROR_INVALID_ID);
|
||||
TEST_ASSERT_EQUAL(msg_id, 0xff);
|
||||
}
|
||||
|
||||
void test_encdec_generic_Error_invalid_size() {
|
||||
uint8_t buffer_msg_data[MAX_MESSAGE_DATA_SIZE];
|
||||
|
||||
Hcp_message_Error msg = {
|
||||
.enables = {
|
||||
.aa = true,
|
||||
.asd = false,
|
||||
.b = true,
|
||||
.c = false,
|
||||
.test = true
|
||||
},
|
||||
.recoveryStatus = HCP_ENUM_INLINE_ERROR_RECOVERY_STATUS_VALUE_NO
|
||||
};
|
||||
|
||||
size_t length = toBytes_Hcp_message_Error(&msg, buffer_msg_data);
|
||||
|
||||
TEST_ASSERT_EQUAL(length, 5);
|
||||
TEST_ASSERT_EQUAL(buffer_msg_data[0], 0x02);
|
||||
|
||||
// Mofiy data
|
||||
length = 2;
|
||||
|
||||
Hcp_message_Error rx_msg;
|
||||
size_t parsed_length = 0;
|
||||
HCP_MSG_ID msg_id = 0xff;
|
||||
HCP_PARSER_RESULT result = fromBytes(&rx_msg, buffer_msg_data, length, &parsed_length, &msg_id);
|
||||
|
||||
TEST_ASSERT_EQUAL(parsed_length, 0);
|
||||
TEST_ASSERT_EQUAL(result, HCP_PARSER_RESULT_ERROR_INVALID_SIZE);
|
||||
TEST_ASSERT_EQUAL(msg_id, 0xff);
|
||||
}
|
||||
|
||||
void test_encdec_generic_Error_large_size() {
|
||||
uint8_t buffer_msg_data[MAX_MESSAGE_DATA_SIZE];
|
||||
|
||||
Hcp_message_Error msg = {
|
||||
.enables = {
|
||||
.aa = true,
|
||||
.asd = false,
|
||||
.b = true,
|
||||
.c = false,
|
||||
.test = true
|
||||
},
|
||||
.recoveryStatus = HCP_ENUM_INLINE_ERROR_RECOVERY_STATUS_VALUE_NO
|
||||
};
|
||||
|
||||
size_t length = toBytes_Hcp_message_Error(&msg, buffer_msg_data);
|
||||
|
||||
TEST_ASSERT_EQUAL(length, 5);
|
||||
TEST_ASSERT_EQUAL(buffer_msg_data[0], 0x02);
|
||||
|
||||
length += 5;
|
||||
|
||||
Hcp_message_Error rx_msg;
|
||||
size_t parsed_length = 0;
|
||||
HCP_MSG_ID msg_id = 0xff;
|
||||
HCP_PARSER_RESULT result = fromBytes(&rx_msg, buffer_msg_data, length, &parsed_length, &msg_id);
|
||||
|
||||
TEST_ASSERT_EQUAL(result, HCP_PARSER_RESULT_OK);
|
||||
TEST_ASSERT_EQUAL(parsed_length, 5);
|
||||
TEST_ASSERT_EQUAL(msg_id, HCP_MESSAGE_ERROR_ID);
|
||||
|
||||
TEST_ASSERT_EQUAL(rx_msg.enables.aa, msg.enables.aa);
|
||||
TEST_ASSERT_EQUAL(rx_msg.enables.asd, msg.enables.asd);
|
||||
TEST_ASSERT_EQUAL(rx_msg.enables.b, msg.enables.b);
|
||||
TEST_ASSERT_EQUAL(rx_msg.enables.c, msg.enables.c);
|
||||
TEST_ASSERT_EQUAL(rx_msg.enables.test, msg.enables.test);
|
||||
|
||||
TEST_ASSERT_EQUAL(rx_msg.recoveryStatus, msg.recoveryStatus);
|
||||
}
|
||||
|
||||
void test_encdec_generic_Error_invalid_crc() {
|
||||
uint8_t buffer_msg_data[MAX_MESSAGE_DATA_SIZE];
|
||||
|
||||
Hcp_message_Error msg = {
|
||||
.enables = {
|
||||
.aa = true,
|
||||
.asd = false,
|
||||
.b = true,
|
||||
.c = false,
|
||||
.test = true
|
||||
},
|
||||
.recoveryStatus = HCP_ENUM_INLINE_ERROR_RECOVERY_STATUS_VALUE_NO
|
||||
};
|
||||
|
||||
size_t length = toBytes_Hcp_message_Error(&msg, buffer_msg_data);
|
||||
|
||||
TEST_ASSERT_EQUAL(length, 5);
|
||||
TEST_ASSERT_EQUAL(buffer_msg_data[0], 0x02);
|
||||
|
||||
// Mofiy data
|
||||
buffer_msg_data[3] = 0xff;
|
||||
|
||||
Hcp_message_Error rx_msg;
|
||||
size_t parsed_length = 0;
|
||||
HCP_MSG_ID msg_id = 0xff;
|
||||
HCP_PARSER_RESULT result = fromBytes(&rx_msg, buffer_msg_data, length, &parsed_length, &msg_id);
|
||||
|
||||
TEST_ASSERT_EQUAL(parsed_length, 0);
|
||||
TEST_ASSERT_EQUAL(result, HCP_PARSER_RESULT_ERROR_INVALID_CRC);
|
||||
TEST_ASSERT_EQUAL(msg_id, 0xff);
|
||||
}
|
||||
84
test/output/c/src/test_python_integration.c
Normal file
84
test/output/c/src/test_python_integration.c
Normal file
@@ -0,0 +1,84 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "HCP_message.h"
|
||||
#include "HCP_enum.h"
|
||||
|
||||
int main() {
|
||||
char hex_string[1024];
|
||||
uint8_t data[sizeof(hex_string)/2];
|
||||
|
||||
size_t bytesRead = read(STDIN_FILENO, hex_string, sizeof(hex_string));
|
||||
|
||||
for(int i=0; i<(bytesRead/2); i+=1) {
|
||||
sscanf(&hex_string[i*2], "%02hhx", &data[i]);
|
||||
}
|
||||
|
||||
// Decode the package
|
||||
char package_buffer[MAX_MESSAGE_STRUCT_SIZE];
|
||||
|
||||
size_t parsed_size = 0;
|
||||
HCP_MSG_ID msg_id;
|
||||
HCP_PARSER_RESULT result = fromBytes(
|
||||
(void *)(package_buffer), data, bytesRead/2, &parsed_size, &msg_id
|
||||
);
|
||||
|
||||
// Early exit if the decode failed
|
||||
if(result != HCP_PARSER_RESULT_OK) {
|
||||
if(result == HCP_PARSER_RESULT_ERROR_INVALID_ID) {
|
||||
printf("ID\n");
|
||||
return -1;
|
||||
} else if(result == HCP_PARSER_RESULT_ERROR_INVALID_CRC) {
|
||||
printf("CRC\n");
|
||||
return -1;
|
||||
} else if(result == HCP_PARSER_RESULT_ERROR_INVALID_SIZE) {
|
||||
printf("SIZE\n");
|
||||
return -1;
|
||||
} else {
|
||||
printf("???\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Modify and encode the package
|
||||
char send_buffer[MAX_MESSAGE_DATA_SIZE];
|
||||
size_t send_size;
|
||||
|
||||
switch (msg_id) {
|
||||
case HCP_MESSAGE_MOTION_UPDATE_ID: {
|
||||
Hcp_message_MotionUpdate *msg = (struct Hcp_message_MotionUpdate *)package_buffer;
|
||||
msg->speed[0] += 1;
|
||||
msg->speed[1] += 2;
|
||||
msg->speed[2] += 3;
|
||||
msg->stearing *= 0.5;
|
||||
msg->name[0] = 'A';
|
||||
msg->enables.a = !msg->enables.a;
|
||||
msg->enables.b = !msg->enables.b;
|
||||
msg->enables.c = !msg->enables.c;
|
||||
msg->dangerLvl = HCP_ENUM_INLINE_MOTION_UPDATE_DANGER_LVL_VALUE_MELTDOWN;
|
||||
send_size = toBytes_Hcp_message_MotionUpdate(msg, send_buffer);
|
||||
break;
|
||||
}
|
||||
case HCP_MESSAGE_ERROR_ID: {
|
||||
Hcp_message_Error *msg = (struct Hcp_message_Error *)package_buffer;
|
||||
msg->enables.b = !msg->enables.b;
|
||||
msg->enables.c = !msg->enables.c;
|
||||
send_size = toBytes_Hcp_message_Error(msg, send_buffer);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Print the encoded package
|
||||
printf("OK\n");
|
||||
for(int i=0; i<send_size; i++) {
|
||||
uint8_t byte = send_buffer[i];
|
||||
printf("%02x", byte);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
162
test/output/test_c_integration.py
Normal file
162
test/output/test_c_integration.py
Normal file
@@ -0,0 +1,162 @@
|
||||
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"
|
||||
63
test/output/test_crc.py
Normal file
63
test/output/test_crc.py
Normal file
@@ -0,0 +1,63 @@
|
||||
|
||||
def crc16(data: bytearray, offset: int, length: int):
|
||||
if data is None or offset < 0 or offset > len(data) - 1 and offset + length > len(data):
|
||||
return 0
|
||||
crc = 0xFFFF
|
||||
for i in range(0, length):
|
||||
crc ^= data[offset + i] << 8
|
||||
for j in range(0, 8):
|
||||
if (crc & 0x8000) > 0:
|
||||
crc = (crc << 1) ^ 0x1021
|
||||
else:
|
||||
crc = crc << 1
|
||||
return crc & 0xFFFF
|
||||
|
||||
|
||||
def test_crc_sum():
|
||||
# ignore: F401
|
||||
from backend_output.python.bproto_base import crc16_calc_inital, calc_crc_sum
|
||||
|
||||
protocol_hash = bytes.fromhex("4a07789a41ef64c38a7edb78574497d4")
|
||||
protocol_data = bytes.fromhex("c9850ff561ea6d27f3171e1c680a4607ea8fa5278ee96de5c8e0ade101bdeb4332d07cb2269a47c910ca2cfe95de9ceda0bb9ee05b0909dcaf68f6a605df7dd4")
|
||||
|
||||
inital = crc16_calc_inital(protocol_hash)
|
||||
checksum = calc_crc_sum(protocol_data, inital)
|
||||
ref_data = protocol_hash + protocol_data
|
||||
assert crc16(ref_data, 0, len(ref_data)) == checksum
|
||||
|
||||
protocol_hash = bytes.fromhex("4a07789a41ef64c38a7edb78574497d4")
|
||||
protocol_data = bytes.fromhex("8898ebbd28e82663b71a7bbf8bc5ec72872ca057a77e31c1c11deff5004e8985767acb75da9b2d1dea643b08ed0458477e4a")
|
||||
|
||||
inital = crc16_calc_inital(protocol_hash)
|
||||
checksum = calc_crc_sum(protocol_data, inital)
|
||||
ref_data = protocol_hash + protocol_data
|
||||
assert crc16(ref_data, 0, len(ref_data)) == checksum
|
||||
|
||||
protocol_hash = bytes.fromhex("4a07789a41ef64c38a7edb78574497d4")
|
||||
protocol_data = bytes.fromhex("ca321a923e202614482cb392d858175ffe9f117d38")
|
||||
|
||||
inital = crc16_calc_inital(protocol_hash)
|
||||
checksum = calc_crc_sum(protocol_data, inital)
|
||||
ref_data = protocol_hash + protocol_data
|
||||
assert crc16(ref_data, 0, len(ref_data)) == checksum
|
||||
|
||||
|
||||
def test_crc_sum_change():
|
||||
# ignore: F401
|
||||
from backend_output.python.bproto_base import crc16_calc_inital, calc_crc_sum
|
||||
|
||||
protocol_hash = bytes.fromhex("4a07789a41ef64c38a7edb78574497d4")
|
||||
protocol_data = bytes.fromhex("ca321a923e202614482cb392d858175ffe9f117d38")
|
||||
|
||||
inital = crc16_calc_inital(protocol_hash)
|
||||
checksum = calc_crc_sum(protocol_data + b"\xaa", inital)
|
||||
ref_data = protocol_hash + protocol_data
|
||||
assert crc16(ref_data, 0, len(ref_data)) != checksum
|
||||
|
||||
protocol_hash = bytes.fromhex("4a07789a41ef64c38a7edb78574497d4")
|
||||
protocol_data = bytes.fromhex("ca321a923e202614482cb392d858175ffe9f117d38")
|
||||
|
||||
inital = crc16_calc_inital(protocol_hash[-1:] + b"\xaa")
|
||||
checksum = calc_crc_sum(protocol_data, inital)
|
||||
ref_data = protocol_hash + protocol_data
|
||||
assert crc16(ref_data, 0, len(ref_data)) != checksum
|
||||
197
test/output/test_encdec.py
Normal file
197
test/output/test_encdec.py
Normal file
@@ -0,0 +1,197 @@
|
||||
import sys
|
||||
sys.path.append('src/')
|
||||
sys.path.append('test/')
|
||||
sys.path.append('test/backend_output/python')
|
||||
|
||||
|
||||
def test_HCP_encdec_MessageMotionUpdate():
|
||||
# ignore: F401
|
||||
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
|
||||
|
||||
src_msg = HcpMessageMotionUpdate(
|
||||
speed=[2, 3, 4],
|
||||
stearing=0.3,
|
||||
name="Hello HCP!",
|
||||
enable=[True, False, True, False],
|
||||
heading="N",
|
||||
enables=HcpBitfieldEnables(
|
||||
a=True, b=False, c=True
|
||||
),
|
||||
danger_lvl=HcpEnumInlineMotionUpdateDangerLvl.ERROR
|
||||
)
|
||||
|
||||
data = src_msg.to_bytes()
|
||||
|
||||
isinstance(data, bytes)
|
||||
|
||||
dest_msg = HcpMessageMotionUpdate().from_bytes(data)
|
||||
|
||||
assert isinstance(dest_msg.speed, list)
|
||||
assert isinstance(dest_msg.speed[0], int)
|
||||
assert src_msg.speed == dest_msg.speed
|
||||
|
||||
assert isinstance(dest_msg.stearing, float)
|
||||
assert round(src_msg.stearing * 1000) == round(dest_msg.stearing * 1000)
|
||||
|
||||
assert isinstance(dest_msg.name, str)
|
||||
assert src_msg.name == dest_msg.name
|
||||
|
||||
assert isinstance(dest_msg.enable, list)
|
||||
assert isinstance(dest_msg.enable[0], bool)
|
||||
assert src_msg.enable == dest_msg.enable
|
||||
|
||||
assert isinstance(dest_msg.heading, str)
|
||||
assert src_msg.heading == dest_msg.heading
|
||||
|
||||
assert isinstance(dest_msg.enables.a, bool)
|
||||
assert isinstance(dest_msg.enables.b, bool)
|
||||
assert isinstance(dest_msg.enables.c, bool)
|
||||
assert src_msg.enables.a == dest_msg.enables.a
|
||||
assert src_msg.enables.b == dest_msg.enables.b
|
||||
assert src_msg.enables.c == dest_msg.enables.c
|
||||
|
||||
assert isinstance(src_msg.danger_lvl, HcpEnumInlineMotionUpdateDangerLvl)
|
||||
assert src_msg.danger_lvl.value == dest_msg.danger_lvl.value
|
||||
|
||||
|
||||
def test_HCP_encdec_MessageMotionUpdate_failure():
|
||||
# ignore: F401
|
||||
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
|
||||
from backend_output.python.bproto_error import bproto_PackageErrorInvalidMessageID, bproto_PackageErrorInvalidSize
|
||||
|
||||
src_msg = HcpMessageMotionUpdate(
|
||||
speed=[2, 3, 4],
|
||||
stearing=0.3,
|
||||
name="Hello HCP!",
|
||||
enable=[True, False, True, False],
|
||||
heading="N",
|
||||
enables=HcpBitfieldEnables(
|
||||
a=True, b=False, c=True
|
||||
),
|
||||
danger_lvl=HcpEnumInlineMotionUpdateDangerLvl.ERROR
|
||||
)
|
||||
|
||||
data = src_msg.to_bytes()
|
||||
|
||||
try:
|
||||
HcpMessageMotionUpdate().from_bytes(b"\xaa" + data[1:])
|
||||
except Exception as e:
|
||||
isinstance(e, bproto_PackageErrorInvalidMessageID)
|
||||
|
||||
data = src_msg.to_bytes()
|
||||
|
||||
try:
|
||||
HcpMessageMotionUpdate().from_bytes(data[:10])
|
||||
except Exception as e:
|
||||
isinstance(e, bproto_PackageErrorInvalidSize)
|
||||
|
||||
|
||||
def test_HCP_encdec_HcpMessageError():
|
||||
# ignore: F401
|
||||
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
|
||||
|
||||
src_msg = HcpMessageError(
|
||||
recovery_status=HcpEnumInlineErrorRecoveryStatus.YES,
|
||||
enables=HcpBitfieldInlineErrorEnables(
|
||||
test=True, asd=False, b=True, c=False, aa=True
|
||||
)
|
||||
)
|
||||
|
||||
data = src_msg.to_bytes()
|
||||
|
||||
isinstance(data, bytes)
|
||||
|
||||
dest_msg = HcpMessageError().from_bytes(data)
|
||||
|
||||
assert src_msg.recovery_status.value == dest_msg.recovery_status.value
|
||||
|
||||
assert src_msg.enables.test == dest_msg.enables.test
|
||||
assert src_msg.enables.asd == dest_msg.enables.asd
|
||||
assert src_msg.enables.b == dest_msg.enables.b
|
||||
assert src_msg.enables.c == dest_msg.enables.c
|
||||
assert src_msg.enables.aa == dest_msg.enables.aa
|
||||
|
||||
|
||||
def test_HCP_encdec_HcpMessageError_failure():
|
||||
# ignore: F401
|
||||
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
|
||||
from backend_output.python.bproto_error import bproto_PackageErrorInvalidMessageID, bproto_PackageErrorInvalidSize
|
||||
|
||||
src_msg = HcpMessageError(
|
||||
recovery_status=HcpEnumInlineErrorRecoveryStatus.YES,
|
||||
enables=HcpBitfieldInlineErrorEnables(
|
||||
test=True, asd=False, b=True, c=False, aa=True
|
||||
)
|
||||
)
|
||||
|
||||
data = src_msg.to_bytes()
|
||||
|
||||
try:
|
||||
HcpMessageError().from_bytes(b"\xaa" + data[1:])
|
||||
except Exception as e:
|
||||
isinstance(e, bproto_PackageErrorInvalidMessageID)
|
||||
|
||||
data = src_msg.to_bytes()
|
||||
|
||||
try:
|
||||
HcpMessageError().from_bytes(data[:3])
|
||||
except Exception as e:
|
||||
isinstance(e, bproto_PackageErrorInvalidSize)
|
||||
|
||||
|
||||
def test_generic_parse_package():
|
||||
# ignore: F401
|
||||
from backend_output.python.HCP_protocol_packets import HcpMessageError, HcpMessageMotionUpdate, parse_package
|
||||
from backend_output.python.HCP_protocol_bitfield import HcpBitfieldInlineErrorEnables, HcpBitfieldEnables
|
||||
from backend_output.python.HCP_protocol_enum import HcpEnumInlineErrorRecoveryStatus, HcpEnumInlineMotionUpdateDangerLvl
|
||||
from backend_output.python.bproto_error import bproto_PackageErrorInvalidMessageID, bproto_PackageErrorInvalidSize
|
||||
|
||||
src_msg1 = HcpMessageError(
|
||||
recovery_status=HcpEnumInlineErrorRecoveryStatus.YES,
|
||||
enables=HcpBitfieldInlineErrorEnables(
|
||||
test=True, asd=False, b=True, c=False, aa=True
|
||||
)
|
||||
)
|
||||
|
||||
src_msg2 = HcpMessageMotionUpdate(
|
||||
speed=[2, 3, 4],
|
||||
stearing=0.3,
|
||||
name="Hello HCP!",
|
||||
enable=[True, False, True, False],
|
||||
heading="N",
|
||||
enables=HcpBitfieldEnables(
|
||||
a=True, b=False, c=True
|
||||
),
|
||||
danger_lvl=HcpEnumInlineMotionUpdateDangerLvl.ERROR
|
||||
)
|
||||
|
||||
dest_msg1, parsed_size = parse_package(src_msg1.to_bytes())
|
||||
assert parsed_size == 5
|
||||
assert isinstance(dest_msg1, HcpMessageError)
|
||||
|
||||
dest_msg2, parsed_size = parse_package(src_msg2.to_bytes())
|
||||
assert parsed_size == 58
|
||||
assert isinstance(dest_msg2, HcpMessageMotionUpdate)
|
||||
|
||||
try:
|
||||
parse_package(b"\xaa" + src_msg2.to_bytes()[1:])
|
||||
except Exception as e:
|
||||
isinstance(e, bproto_PackageErrorInvalidMessageID)
|
||||
|
||||
try:
|
||||
parse_package(src_msg1.to_bytes()[:2])
|
||||
except Exception as e:
|
||||
isinstance(e, bproto_PackageErrorInvalidSize)
|
||||
|
||||
try:
|
||||
parse_package(src_msg2.to_bytes()[:2])
|
||||
except Exception as e:
|
||||
isinstance(e, bproto_PackageErrorInvalidSize)
|
||||
Reference in New Issue
Block a user